/
dataloader.py
263 lines (230 loc) · 10.7 KB
/
dataloader.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
import os
import numpy as np
import scipy.io as sio
def ROI_loader(subject, fil):
data_path = str(os.path.dirname(os.path.abspath(__file__))) + '/data/subjects'
all_data = sio.loadmat(data_path + subject + '/' + fil)
ROI = all_data['meta']
Gordon_areas = ROI[0][0][11][0][14]
try:
data = all_data['examples']
except KeyError:
data = all_data['examples_passagesentences']
return Gordon_areas, data
def coltocoord_ROI_ordering(subject, fil):
data_path = str(os.path.dirname(os.path.abspath(__file__))) + '/data/subjects'
all_data = sio.loadmat(data_path + subject + '/' + fil)
ROI = all_data['meta']
coord = ROI[0][0][5]
return coord
def matcher(area, last_dim, last, Roi_coord):
small = np.sum(last[1])
big = np.sum(last_dim[1])
difference = big-small
assert 0<=difference
assert small == area.shape[1]
if last_dim.shape[1]>last.shape[1]:
helper = np.zeros_like(last_dim)
helper[:,:last.shape[1]] = last
last = helper
checker = 0
else:
checker = last.shape[1] - last_dim.shape[1]
helper = np.zeros_like(last)
helper[:,checker:] = last_dim
last_dim = helper
# print(last)
area_new = np.zeros((area.shape[0],big))
counter = 0
holder = 0
index = 0
index_old = 0
mean = np.zeros((area.shape[0],1))
while counter<last_dim.shape[1]:
if last_dim[1,counter] > (last[1,counter]+holder) and checker == 0:
if last[1,counter] == 0:
if last[1,counter-1] == 0:
area_new[:,index+last[1,counter]:index+last_dim[1,counter]-holder] = np.tile(mean,(1,last_dim[1,counter] - (last[1,counter]+holder)))
index+= (last_dim[1,counter]-holder)
else:
mean = np.reshape(np.mean(area[:,(Roi_coord[:,2] == last[0,counter-1])], axis=1),(area.shape[0],1))
area_new[:,index+last[1,counter]:index+last_dim[1,counter]-holder] = np.tile(mean,(1,last_dim[1,counter] - (last[1,counter]+holder)))
index+= (last_dim[1,counter]-holder)
else:
mean = np.reshape(np.mean(area[:,(Roi_coord[:,2] == last[0,counter])], axis=1),(area.shape[0],1))
area_new[:,index:index+last[1,counter]] = area[:,index_old:index_old+last[1,counter]]
if difference > 0 and 0<=(difference - (last_dim[1,counter] - (last[1,counter]+holder))):
area_new[:,index+last[1,counter]:index+last_dim[1,counter]-holder] = np.tile(mean,(1,last_dim[1,counter] - (last[1,counter]+holder)))
difference = difference - (last_dim[1,counter] - (last[1,counter]+holder))
index+= (last_dim[1,counter] - holder)
elif difference > 0 :
area_new[:,index+last[1,counter]:index+last[1,counter]+difference] = np.tile(mean,(1,difference))
index += (difference + last[1,counter])
difference = 0
else:
index+= last[1,counter]
holder = 0
else:
area_new[:,index:index+last[1,counter]] = area[:,index_old:index_old+last[1,counter]]
index+= last[1,counter]
if checker > 0:
checker-=1
holder = last[1,counter] + holder
else:
holder = (last[1,counter] + holder) - last_dim[1,counter]
index_old+=last[1,counter]
counter+=1
# assert holder == 0
return area_new
def class_sizer(subject):
no_sent = ['M05','M06','M10', 'M13','M16', 'M17']
only_two = ['M03']
only_three = [ 'M08', 'M09','M14']
both = ['P01','M02','M03','M04','M07','M15']
if subject in no_sent:
value = 4530
value_test = 0
if subject in only_two:
value = 4287
value_test = 243
if subject in only_three:
value = 4146
value_test = 384
if subject in both:
value = 3903
value_test = 627
return value, value_test
def dataloader_sentence_word_split_new_matching_all_subjects(subject):
data_path = str(os.path.dirname(os.path.abspath(__file__))) + '/data/subjects/'
vector_path_180 = str(os.path.dirname(os.path.abspath(__file__))) + '/data/glove_data/180_concepts_real.mat'
vector_path_243 = str(os.path.dirname(os.path.abspath(__file__))) + '/data/glove_data/243_sentences_real.mat'
vector_path_384 =str(os.path.dirname(os.path.abspath(__file__))) + '/data/glove_data/384_sentences_real.mat'
vector_180 = sio.loadmat(vector_path_180)['data']
vector_243 = sio.loadmat(vector_path_243)['data']
vector_384 = sio.loadmat(vector_path_384)['data']
subjects = ['P01','M02','M03','M04','M05','M06','M07', 'M08', 'M09','M15','M10', 'M13','M14','M16', 'M17'] #,'M10', 'M13','M14','M16','M17'
no_sent = ['M05','M06','M10', 'M13','M16', 'M17']
only_two = ['M03']
only_three = [ 'M08', 'M09','M14']
both = ['P01','M02','M03','M04','M07','M15']
sizes = np.load(str(os.path.dirname(os.path.abspath(__file__))) + '/data/look_ups/sizes.npy')
last_dim_all = np.load(str(os.path.dirname(os.path.abspath(__file__))) + '/data/look_ups/last_dim.npz')
if subject in no_sent:
value = 4530
value_test = 0
if subject in only_two:
value = 4287
value_test = 243
if subject in only_three:
value = 4146
value_test = 384
if subject in both:
value = 3903
value_test = 627
data_train = np.zeros((value,65730))
data_fine = np.zeros((7560,65730))
data_test = np.zeros((value_test,65730))
data_fine_test = np.zeros((540,65730))
glove_train = np.zeros((value,300))
glove_fine = np.zeros((7560,300))
glove_test = np.zeros((value_test,300))
glove_fine_test = np.zeros((540,300))
numb = 0
numb_test = 0
numb_fine_test = 0
numb_fine = 0
tot = 0
tot_fine = 0
tot_test= 0
tot_fine_test = 0
for sub in subjects:
folder = os.listdir(data_path +sub)
values = np.zeros((627,212742))
values_fine = np.zeros((540,212742))
numb_fine_tes = 0
numb_fine1 = 0
numb_tes = 0
numb1 = 0
for fil in folder:
if sub == subject:
if fil.startswith('data_180'):
Gordon, data = ROI_loader(sub,fil)
coord = coltocoord_ROI_ordering(sub,fil)
values_fine[numb_fine_tes:numb_fine_tes+data.shape[0],:data.shape[1]] = data
if data.shape[0]==180:
glove_fine_test[numb_fine_test:numb_fine_test+data.shape[0],:] = vector_180
if fil=='data_180concepts_sentences.mat':
holder = numb_tes
numb_fine_test +=data.shape[0]
numb_fine_tes += data.shape[0]
if fil.startswith('data') and not fil.startswith('data_180'):
Gordon, data = ROI_loader(sub,fil)
coord = coltocoord_ROI_ordering(sub,fil)
values[numb_tes:numb_tes+data.shape[0],:data.shape[1]] = data
if data.shape[0]==243:
glove_test[numb_test:numb_test+data.shape[0],:] = vector_243
if data.shape[0]==384:
glove_test[numb_test:numb_test+data.shape[0],:] = vector_384
if fil=='data_180concepts_sentences.mat':
holder = numb_tes
numb_test +=data.shape[0]
numb_tes += data.shape[0]
else:
if fil.startswith('data_180'):
Gordon, data = ROI_loader(sub,fil)
coord = coltocoord_ROI_ordering(sub,fil)
values_fine[numb_fine1:numb_fine1+data.shape[0],:data.shape[1]] = data
if data.shape[0]==180:
glove_fine[numb_fine:numb_fine+data.shape[0],:] = vector_180
numb_fine +=data.shape[0]
numb_fine1+=data.shape[0]
if fil.startswith('data') and not fil.startswith('data_180'):
Gordon, data = ROI_loader(sub,fil)
coord = coltocoord_ROI_ordering(sub,fil)
values[numb1:numb1+data.shape[0],:data.shape[1]] = data
if data.shape[0]==243:
glove_train[numb:numb+data.shape[0],:] = vector_243
if data.shape[0]==384:
glove_train[numb:numb+data.shape[0],:] = vector_384
numb +=data.shape[0]
numb1+=data.shape[0]
values = values[~(values==0).all(1)]
ind_array = 0
for i in range(333):
helper = True
access = 'arr_' +str(i)
last_dim = last_dim_all[access]
Roi_coord = np.squeeze(coord[Gordon[i][0]])
last = np.asarray(np.unique(Roi_coord[:,2], return_counts=True))
big = np.sum(last_dim[1])
assert big == sizes[i]
indexes = Gordon[i][0]
area = values[:,indexes]
area_fine = values_fine[:,indexes]
if area.shape[0]==0:
helper = False
if sub == subject:
if helper:
area = np.reshape(area, (values.shape[0],-1))
area = matcher(area, last_dim, last, Roi_coord)
data_test[tot_test:tot_test+values.shape[0],ind_array:ind_array+area.shape[1]] = area
area_fine = np.reshape(area_fine, (values_fine.shape[0],-1))
area_fine = matcher(area_fine, last_dim, last, Roi_coord)
data_fine_test[tot_fine_test:tot_fine_test+values_fine.shape[0],ind_array:ind_array+area_fine.shape[1]] = area_fine
ind_array+=sizes[i]
else :
if helper:
area = np.reshape(area, (values.shape[0],-1))
area = matcher(area, last_dim, last, Roi_coord)
data_train[tot:(tot+values.shape[0]),ind_array:ind_array+area.shape[1]] = area
area_fine = np.reshape(area_fine, (values_fine.shape[0],-1))
area_fine = matcher(area_fine, last_dim, last, Roi_coord)
data_fine[tot_fine:(tot_fine+values_fine.shape[0]),ind_array:ind_array+area_fine.shape[1]] = area_fine
ind_array+=sizes[i]
if sub == subject:
tot_test+=values.shape[0]
tot_fine_test+= values_fine.shape[0]
else:
tot+=values.shape[0]
tot_fine+= values_fine.shape[0]
return data_train, data_fine_test, glove_train, glove_fine_test, data_fine, data_fine_test, glove_fine, glove_fine_test