-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp3_continuous.py
243 lines (162 loc) · 6.54 KB
/
exp3_continuous.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os
import commands
import math
import csv
import random
import ConfigParser
# Mean Absolute Error (MAE), and Root Mean Square Error (RMSE)
def get_label_set(datafile):
label_set=[]
f = open(datafile, 'r')
reader = csv.reader(f)
next(reader)
for line in reader:
_, _, label = line
if label not in label_set:
label_set.append(label)
return label_set
def getMAE(datafile, truthfile, e2lpd, partialtruthfile):
label_set = get_label_set(datafile)
# in case that e2lpd does not have data in the truthfile, then we randomly sample a label from label_set
e2truth = {}
f = open(truthfile, 'r')
reader = csv.reader(f)
next(reader)
for line in reader:
example, truth = line
e2truth[example] = truth
f = open(partialtruthfile, 'r')
reader = csv.reader(f)
next(reader)
for line in reader:
example, truth = line
del e2truth[example]
value = 0
for e in e2truth:
if e not in e2lpd:
#randomly select a label from label_set
truth = random.choice(label_set)
value += abs( float(truth) - float(e2truth[e]) )
continue
if type(e2lpd[e]) == type({}):
temp = 0
for label in e2lpd[e]:
if temp < e2lpd[e][label]:
temp = e2lpd[e][label]
candidate = []
for label in e2lpd[e]:
if temp == e2lpd[e][label]:
candidate.append(label)
truth = random.choice(candidate)
else:
truth = e2lpd[e]
value += abs( float(truth) - float(e2truth[e]) )
return value*1.0/len(e2truth)
def getRMSE(datafile, truthfile, e2lpd, partialtruthfile):
label_set = get_label_set(datafile)
# in case that e2lpd does not have data in the truthfile, then we randomly sample a label from label_set
e2truth = {}
f = open(truthfile, 'r')
reader = csv.reader(f)
next(reader)
for line in reader:
example, truth = line
e2truth[example] = truth
f = open(partialtruthfile, 'r')
reader = csv.reader(f)
next(reader)
for line in reader:
example, truth = line
del e2truth[example]
value = 0
for e in e2truth:
if e not in e2lpd:
#randomly select a label from label_set
truth = random.choice(label_set)
value += ( float(truth) - float(e2truth[e]) )**2
continue
if type(e2lpd[e]) == type({}):
temp = 0
for label in e2lpd[e]:
if temp < e2lpd[e][label]:
temp = e2lpd[e][label]
candidate = []
for label in e2lpd[e]:
if temp == e2lpd[e][label]:
candidate.append(label)
truth = random.choice(candidate)
else:
truth = e2lpd[e]
value += ( float(truth) - float(e2truth[e]) )**2
return math.sqrt( value*1.0/len(e2truth) )
def select_kfold(datafile):
f = open(datafile, 'r')
reader = csv.reader(f)
next(reader)
count = 0
examples = {}
for line in reader:
example, worker, label = line
examples[example] = 0
count += 1
return int(math.ceil(count*1.0/len(examples)))
def run_datasets(python_command, datasets, methods, iterations, splits):
for dataset in datasets:
print "########"+dataset+"########"
for method in methods:
if not os.path.isdir(r'./truth_methods/' + method):
continue
# dataset & method
datafile = r"'./datasets/" + dataset + r"/answer.csv'"
truthfile = r"'./datasets/" + dataset + r"/truth.csv'"
MAEs = []
RMSEs = []
for iteration in range(iterations):
tempMAEs = []
tempRMSEs = []
for foldno in splits:
partialtruthfile = r"'./truth_data_kfolder/" + dataset + '/' + str(iteration) \
+ r"/truth_" + str(foldno) + ".csv'"
command = python_command + r'./truth_methods/' + method + r'/method.py ' \
+ datafile + ' ' + partialtruthfile + ' ' + '"categorical"'
output = commands.getoutput(command).split('\n')[-1]
MAE = getMAE(eval(datafile), eval(truthfile), eval(output), eval(partialtruthfile))
RMSE = getRMSE(eval(datafile), eval(truthfile), eval(output), eval(partialtruthfile))
tempMAEs.append(str(MAE))
tempRMSEs.append(str(RMSE))
print method + str(iteration) + '_' + str(foldno)
MAEs.append(tempMAEs)
RMSEs.append(tempRMSEs)
# dataset & method finished
folder = r'./output/exp-3/continuous'
if not os.path.isdir(folder):
os.mkdir(folder)
folder = folder + '/' + dataset
if not os.path.isdir(folder):
os.mkdir(folder)
f = open(folder + '/' + 'MAE' + '_' + method, 'w')
for tempresults in MAEs:
f.write('\t'.join(tempresults) + '\n')
f.close()
f = open(folder + '/' + 'RMSE' + '_' + method, 'w')
for tempresults in RMSEs:
f.write('\t'.join(tempresults) + '\n')
f.close()
if __name__ == '__main__':
cf = ConfigParser.ConfigParser()
cf.read('./config.ini')
# split the data in the "./truth_data_kfolder" folder
# import generate_truth_kfolderdata
# iterations = eval(cf.get("exp-3", "iterations"))
# splits = eval(cf.get("exp-3", "splits"))
# generate_truth_kfolderdata.generate_truth_kfolderdata(r'./truth_data_kfolder', iterations, splits)
# get the results of each dataset and each method in "./output/exp-3" folder
datasets_continuous = eval(cf.get("exp-3", "datasets_continuous"))
truth_continuous = eval(cf.get("exp-3", "truth_continuous"))
python_command = eval(cf.get("exp-3", "python_command"))
iterations = eval(cf.get("exp-3", "iterations"))
splits = eval(cf.get("exp-3", "splits"))
run_datasets(python_command, datasets_continuous, truth_continuous, iterations, splits)
# draw graph in "./exp-3-graph" folder
import plot_exp3_continuous
plot_exp3_continuous.plot()