-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.py
374 lines (299 loc) · 16.1 KB
/
utils.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import numpy as np
import os
import pickle as pkl
from scipy.stats import pearsonr
os.environ['PYTHONHASHSEED'] = '0'
os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1'
def load_data(data_ids, cul, data_proportion=[1,0,1,1]):
"""
Loads data for a given country
PARAMETERS
data_ids: list of training, validation, and testing IDs
cul: country index
data_proportion: specifies proportions of data for training, validation, and testing;
i.e. given [a,b,c,d],
[:a*rows] = for training, [b*rows:c*rows] for validation, [(1-d)*rows:] for testing
"""
# Creates array with all csv data for country X (specified by filter_str)
# array columns: country ID, child ID, frame, feature array, label
full_raw = None
with open('./data/data_{}.pkl'.format(cul), 'rb') as f:
full_raw = pkl.load(f)
# Gets training data
country_train = None
id_train = None
frame_train = None
x_train = None
y_train = None
for train_id in data_ids[0]: # Loops over each training ID
train_data = full_raw[full_raw[:,1] == train_id] # Retrieves all rows where first column has train_ID
rows, _ = train_data.shape
cutoff_train = int(rows*data_proportion[0]) # Computes cutoff value for training data, based on data_proportions input
if id_train is None:
country_train = train_data[:cutoff_train, :1]
id_train = train_data[:cutoff_train, 1:2]
frame_train = train_data[:cutoff_train, 2:3]
x_train = train_data[:cutoff_train, 3:-1]
y_train = train_data[:cutoff_train, -1:]
else:
country_train = np.vstack((country_train, train_data[:cutoff_train, :1]))
id_train = np.vstack((id_train, train_data[:cutoff_train, 1:2]))
frame_train = np.vstack((frame_train, train_data[:cutoff_train, 2:3]))
x_train = np.vstack((x_train, train_data[:cutoff_train, 3:-1]))
y_train = np.vstack((y_train, train_data[:cutoff_train, -1:]))
# Gets validation data
country_val = None
id_val = None
frame_val = None
x_val = None
y_val = None
for val_id in data_ids[1]: # Loop over each validation ID
val_data = full_raw[full_raw[:,1] == val_id] # Retrieves all rows where first column has val_id
rows, _ = val_data.shape
cutoff_val_1 = int(rows*data_proportion[1]) # Computes lower cutoff value for validation data, based on data_proportions input
cutoff_val_2 = int(rows*data_proportion[2]) # Computes upper cutoff value for validation data, based on data_proportions input
if id_val is None:
country_val = val_data[cutoff_val_1:cutoff_val_2, :1]
id_val = val_data[cutoff_val_1:cutoff_val_2, 1:2]
frame_val = val_data[cutoff_val_1:cutoff_val_2, 2:3]
x_val = val_data[cutoff_val_1:cutoff_val_2, 3:-1]
y_val = val_data[cutoff_val_1:cutoff_val_2, -1:]
else:
country_val = np.vstack((country_val, val_data[cutoff_val_1:cutoff_val_2, :1]))
id_val = np.vstack((id_val, val_data[cutoff_val_1:cutoff_val_2, 1:2]))
frame_val = np.vstack((frame_val, val_data[cutoff_val_1:cutoff_val_2, 2:3]))
x_val = np.vstack((x_val, val_data[cutoff_val_1:cutoff_val_2, 3:-1]))
y_val = np.vstack((y_val, val_data[cutoff_val_1:cutoff_val_2, -1:]))
# Gets testing data
country_test = None
id_test = None
frame_test = None
x_test = None
y_test = None
for test_id in data_ids[2]: # Loops over each testing ID
test_data = full_raw[full_raw[:,1] == test_id] # Retrieves all rows where first column has test_id
rows, _ = test_data.shape
cutoff_test = int(rows*(1-data_proportion[3]))
if id_test is None:
country_test = test_data[cutoff_test:, :1]
id_test = test_data[cutoff_test:, 1:2]
frame_test = test_data[cutoff_test:, 2:3]
x_test = test_data[cutoff_test:, 3:-1]
y_test = test_data[cutoff_test:, -1:]
else:
country_test = np.vstack((country_test, test_data[cutoff_test:, :1]))
id_test = np.vstack((id_test, test_data[cutoff_test:, 1:2]))
frame_test = np.vstack((frame_test, test_data[cutoff_test:, 2:3]))
x_test = np.vstack((x_test, test_data[cutoff_test:, 3:-1]))
y_test = np.vstack((y_test, test_data[cutoff_test:, -1:]))
return id_train, id_val, id_test, x_train, x_val, x_test, y_train, y_val, y_test, country_train, country_val, country_test, frame_train, frame_val, frame_test
def leave_1_out_ids(IDs):
""" Generates leave-1-out ID list """
fold_list = []
for i in range(len(IDs)):
rolled = np.roll(IDs, i).tolist()
fold_list.append([rolled[1:], rolled[1:], rolled[0:1]])
return fold_list
def all_children_ids(IDs):
""" Generates all children ID list """
fold_list = []
for i in range(len(IDs)):
rolled = np.roll(IDs, i).tolist()
fold_list.append([rolled[:], rolled[0:1], rolled[0:1]])
return fold_list
def target_only_ids(IDs):
""" Generates target-only ID list """
return list(map(lambda x:[[x], [x], [x]], IDs))
def merge_data(cA_data, cB_data):
""" Merges data for models 3 and 4 """
id_train = np.concatenate((cA_data[0], cB_data[0], cB_data[1], cB_data[2]), axis=0)
id_val, id_test = cA_data[1], cA_data[2]
x_train = np.concatenate((cA_data[3], cB_data[3], cB_data[4], cB_data[5]), axis=0)
x_val, x_test = cA_data[4], cA_data[5]
y_train = np.concatenate((cA_data[6], cB_data[6], cB_data[7], cB_data[8]), axis=0)
y_val, y_test = cA_data[7], cA_data[8]
culture_train = np.concatenate((cA_data[9], cB_data[9], cB_data[10], cB_data[11]), axis=0)
culture_val, culture_test = cA_data[10], cA_data[11]
frame_train = np.concatenate((cA_data[12], cB_data[12], cB_data[13], cB_data[14]), axis=0)
frame_val, frame_test = cA_data[13], cA_data[14]
prelim_data = (id_train, id_val, id_test, x_train, x_val, x_test, y_train, y_val, y_test, culture_train, culture_val, culture_test, frame_train, frame_val, frame_test)
return prelim_data
def _process(y_hat, y_lab, fun):
"""
Splits y_true and y_pred in lists
Removes frames where labels are unknown (-1)
Returns list of predictions
"""
y1 = [x for x in y_hat.T]
y2 = [x for x in y_lab.T]
out = []
for _, [_y1, _y2] in enumerate(zip(y1, y2)):
idx = _y2!=-1
_y1 = _y1[idx]
_y2 = _y2[idx]
if np.all(_y2==-1):
out.append(np.nan)
else:
out.append(fun(_y1,_y2))
return np.array(out)
def _icc(y_hat, y_lab, cas=3, typ=1):
""" Computes intra-class correlation """
def fun(y_hat,y_lab):
y_hat = y_hat[None,:]
y_lab = y_lab[None,:]
Y = np.array((y_lab, y_hat))
# number of targets
n = Y.shape[2]
# mean per target
mpt = np.mean(Y, 0)
# print mpt.eval()
mpr = np.mean(Y, 2)
# print mpr.eval()
tm = np.mean(mpt, 1)
# within target sum sqrs
WSS = np.sum((Y[0]-mpt)**2 + (Y[1]-mpt)**2, 1)
# within mean sqrs
WMS = WSS/n
# between rater sum sqrs
RSS = np.sum((mpr - tm)**2, 0) * n
# between rater mean sqrs
RMS = RSS
# between target sum sqrs
TM = np.tile(tm, (y_hat.shape[1], 1)).T
BSS = np.sum((mpt - TM)**2, 1) * 2
# between targets mean squares
BMS = BSS / (n - 1)
# residual sum of squares
ESS = WSS - RSS
# residual mean sqrs
EMS = ESS / (n - 1)
if cas == 1:
if typ == 1:
res = (BMS - WMS) / (BMS + WMS)
if typ == 2:
res = (BMS - WMS) / BMS
if cas == 2:
if typ == 1:
res = (BMS - EMS) / (BMS + EMS + 2 * (RMS - EMS) / n)
if typ == 2:
res = (BMS - EMS) / (BMS + (RMS - EMS) / n)
if cas == 3:
if typ == 1:
res = (BMS - EMS) / (BMS + EMS)
if typ == 2:
res = (BMS - EMS) / BMS
res = res[0]
if np.isnan(res) or np.isinf(res):
return 0
else:
return res
return _process(y_hat, y_lab, fun)
def icc(y_hat, y_lab):
return _icc(y_hat, y_lab)
def ccc(y_hat, y_lab):
""" Computes concordance correlation coefficient """
pred_mean = np.mean(y_hat)
ref_mean = np.mean(y_lab)
pred_var = np.var(y_hat)
ref_var = np.var(y_lab)
covariance = np.mean((y_hat - pred_mean) * (y_lab - ref_mean))
return (2*covariance) / (pred_var+ref_var+np.power((pred_mean-ref_mean),2))
def mae(y_hat, y_lab):
""" Computes mean absolute error """
y_hat = np.asarray(y_hat)
y_lab = np.asarray(y_lab)
diff = np.subtract(y_hat, y_lab)
abs_diff = np.fabs(diff)
return float(sum(abs_diff)/len(y_lab))
def process_summary(REPORTS_FOLDER_DIR):
"""
Processes data
Saves file with average ICC per culture +/- STD, PCC per culture +/ STD, and CCC per culture +/ STD
"""
REPORTS_SUB_DIR = np.array([d for d in os.listdir(REPORTS_FOLDER_DIR) if os.path.isdir(os.path.join(REPORTS_FOLDER_DIR,d))])
FINAL_REPORTS_SUB_DIR = [c for c in REPORTS_SUB_DIR if not(c.endswith('prelim'))]
for report in FINAL_REPORTS_SUB_DIR:
CURRENT_REPORTS_SUB_DIR = os.path.join(REPORTS_FOLDER_DIR, report)
ICC_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'icc_report.txt')
ICC_FINAL_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'icc_final_report.csv')
PCC_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'pcc_report.txt')
PCC_FINAL_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'pcc_final_report.csv')
CCC_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'ccc_report.txt')
CCC_FINAL_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'ccc_final_report.csv')
MAE_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'mae_report.txt')
MAE_FINAL_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'mae_final_report.csv')
FINAL_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'final_report.csv')
icc_data = np.genfromtxt(ICC_DIR, delimiter=',')
pcc_data = np.genfromtxt(PCC_DIR, delimiter=',')
ccc_data = np.genfromtxt(CCC_DIR, delimiter=',')
mae_data = np.genfromtxt(MAE_DIR, delimiter=',')
cultures = np.unique(icc_data[:,0])
culture_avg_icc = np.zeros((len(cultures),2))
culture_avg_pcc = np.zeros((len(cultures),2))
culture_avg_ccc = np.zeros((len(cultures),2))
culture_avg_mae = np.zeros((len(cultures),2))
for c in cultures:
ICC_CULTURE_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'c{}_icc_report.txt'.format(int(c)))
PCC_CULTURE_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'c{}_pcc_report.txt'.format(int(c)))
CCC_CULTURE_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'c{}_ccc_report.txt'.format(int(c)))
MAE_CULTURE_DIR = os.path.join(CURRENT_REPORTS_SUB_DIR, 'c{}_mae_report.txt'.format(int(c)))
culture_rows_icc = np.where(icc_data[:,0] == c)[0] # get row numbers for culture c
culture_ids_icc = icc_data[culture_rows_icc,1] # get ID rows for culture c
unique_ids_icc = np.unique(culture_ids_icc) # get unique IDs for culture c
culture_rows_pcc = np.where(pcc_data[:,0] == c)[0] # get row numbers for culture c
culture_ids_pcc = pcc_data[culture_rows_pcc,1] # get ID rows for culture c
unique_ids_pcc = np.unique(culture_ids_pcc) # get unique IDs for culture c
culture_rows_ccc = np.where(ccc_data[:,0] == c)[0] # get row numbers for culture c
culture_ids_ccc = ccc_data[culture_rows_ccc,1] # get ID rows for culture c
unique_ids_ccc = np.unique(culture_ids_ccc) # get unique IDs for culture c
culture_rows_mae = np.where(mae_data[:,0] == c)[0] # get row numbers for culture c
culture_ids_mae = ccc_data[culture_rows_mae,1] # get ID rows for culture c
unique_ids_mae = np.unique(culture_ids_mae) # get unique IDs for culture c
assert np.array_equal(unique_ids_icc, unique_ids_pcc)
assert np.array_equal(unique_ids_icc, unique_ids_ccc)
assert np.array_equal(unique_ids_icc, unique_ids_mae)
culture_icc = None
culture_pcc = None
culture_ccc = None
culture_mae = None
for u in unique_ids_icc:
all_id_rows_icc = np.where(icc_data[:,1] == u)[0]
id_rows_icc = np.intersect1d(all_id_rows_icc, culture_rows_icc) # get ID rows for child u
all_id_rows_pcc = np.where(pcc_data[:,1] == u)[0]
id_rows_pcc = np.intersect1d(all_id_rows_pcc, culture_rows_pcc) # get ID rows for child u
all_id_rows_ccc = np.where(ccc_data[:,1] == u)[0]
id_rows_ccc = np.intersect1d(all_id_rows_ccc, culture_rows_ccc) # get ID rows for child u
all_id_rows_mae = np.where(mae_data[:,1] == u)[0]
id_rows_mae = np.intersect1d(all_id_rows_mae, culture_rows_mae) # get ID rows for child u
id_icc = icc_data[id_rows_icc, 2] # get ICC data for child u
avg_icc = np.mean(id_icc) # get average ICC for child u
id_pcc = pcc_data[id_rows_pcc, 2] # get PCC data for child u
avg_pcc = np.mean(id_pcc) # get average PCC for child u
id_ccc = ccc_data[id_rows_ccc, 2] # get CCC data for child u
avg_ccc = np.mean(id_ccc) # get average CCC for child u
id_mae = mae_data[id_rows_mae, 2] # get MAE data for child u
avg_mae = np.mean(id_mae) # get average MAE for child u
culture_icc = np.array([[u, avg_icc]]) if culture_icc is None else np.vstack((culture_icc, np.array([[u, avg_icc]])))
culture_pcc = np.array([[u, avg_pcc]]) if culture_pcc is None else np.vstack((culture_pcc, np.array([[u, avg_pcc]])))
culture_ccc = np.array([[u, avg_ccc]]) if culture_ccc is None else np.vstack((culture_ccc, np.array([[u, avg_ccc]])))
culture_mae = np.array([[u, avg_mae]]) if culture_mae is None else np.vstack((culture_mae, np.array([[u, avg_mae]])))
np.savetxt(ICC_CULTURE_DIR, culture_icc)
np.savetxt(PCC_CULTURE_DIR, culture_pcc)
np.savetxt(CCC_CULTURE_DIR, culture_ccc)
np.savetxt(MAE_CULTURE_DIR, culture_mae)
culture_avg_icc[cultures.tolist().index(c), 0] = np.mean(culture_icc, axis=0)[1]
culture_avg_icc[cultures.tolist().index(c), 1] = np.std(culture_icc[:,1])
culture_avg_pcc[cultures.tolist().index(c), 0] = np.mean(culture_pcc, axis=0)[1]
culture_avg_pcc[cultures.tolist().index(c), 1] = np.std(culture_pcc[:,1])
culture_avg_ccc[cultures.tolist().index(c), 0] = np.mean(culture_ccc, axis=0)[1]
culture_avg_ccc[cultures.tolist().index(c), 1] = np.std(culture_ccc[:,1])
culture_avg_mae[cultures.tolist().index(c), 0] = np.mean(culture_mae, axis=0)[1]
culture_avg_mae[cultures.tolist().index(c), 1] = np.std(culture_mae[:,1])
np.savetxt(ICC_FINAL_DIR, culture_avg_icc, delimiter=',')
np.savetxt(PCC_FINAL_DIR, culture_avg_pcc, delimiter=',')
np.savetxt(CCC_FINAL_DIR, culture_avg_ccc, delimiter=',')
np.savetxt(MAE_FINAL_DIR, culture_avg_mae, delimiter=',')
np.savetxt(FINAL_DIR, np.hstack((culture_avg_icc, culture_avg_pcc, culture_avg_ccc, culture_avg_mae)), delimiter=',')
return None
if __name__ == '__main__':
pass