Spaces:
Sleeping
Sleeping
Commit
·
8f041e4
1
Parent(s):
fe4e8b5
update
Browse files- channel_mapping.py +70 -37
- template_montage.png +0 -0
channel_mapping.py
CHANGED
@@ -11,41 +11,59 @@ def reorder_data(filename, old_idx, fill_mode):
|
|
11 |
filepath = os.path.dirname(str(filename))
|
12 |
old_data = utils.read_train_data(filename)
|
13 |
new_data = np.zeros((30, old_data.shape[1]))
|
14 |
-
print('old data shape: ', old_data.shape)
|
|
|
|
|
|
|
15 |
|
16 |
-
if fill_mode == 'zero':
|
17 |
-
zero_arr = np.zeros((1, old_data.shape[1]))
|
18 |
-
old_data = np.concatenate((old_data, zero_arr), axis=0)
|
19 |
-
|
20 |
for i in range(30):
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
return
|
26 |
|
27 |
|
28 |
-
class Channel:
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
self.coord = coord
|
39 |
-
self.topo_index = topo_index
|
40 |
-
self.topo_location = topo_location
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
return ret[:len(ret) - 1] if ret[-1] == 'Z' else ret
|
45 |
|
46 |
-
def suffix(self):
|
47 |
-
return -1 if self.name[-1] == 'Z' else int(''.join(filter(str.isdigit, self.name)))
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
def mapping(input_file, loc_file, fill_mode):
|
51 |
second1 = time.time()
|
@@ -71,12 +89,13 @@ def mapping(input_file, loc_file, fill_mode):
|
|
71 |
input_montage.rename_channels({channel: str.upper(channel)})
|
72 |
|
73 |
channel = str.upper(channel)
|
74 |
-
input_dict[channel] = Channel(index=i, coord=input_montage.get_positions()['ch_pos'][channel])
|
75 |
|
76 |
|
77 |
new_idx = [-1]*30
|
78 |
-
new_idx_name = ['']*30 # tmp
|
79 |
missing_channels = []
|
|
|
80 |
z_row_idx = len(input_montage.ch_names)
|
81 |
|
82 |
|
@@ -96,6 +115,7 @@ def mapping(input_file, loc_file, fill_mode):
|
|
96 |
for i in range(30):
|
97 |
channel = template_montage.ch_names[i]
|
98 |
if channel not in input_dict.keys() | alias.keys():
|
|
|
99 |
finish_flag = 0
|
100 |
continue
|
101 |
|
@@ -106,11 +126,12 @@ def mapping(input_file, loc_file, fill_mode):
|
|
106 |
template_dict[alias[channel]].name = alias[channel]
|
107 |
channel = alias[channel]
|
108 |
else:
|
|
|
109 |
finish_flag = 0
|
110 |
continue
|
111 |
|
112 |
new_idx[i] = input_dict[channel].index
|
113 |
-
new_idx_name[i] = channel # tmp
|
114 |
input_dict[channel].used = True
|
115 |
|
116 |
if finish_flag == 1:
|
@@ -119,8 +140,18 @@ def mapping(input_file, loc_file, fill_mode):
|
|
119 |
#print('new idx order:', new_idx)
|
120 |
#print('new_idx_name:', new_idx_name) # tmp
|
121 |
|
122 |
-
reorder_data(input_file, new_idx, fill_mode)
|
123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
124 |
|
125 |
|
126 |
|
@@ -162,7 +193,7 @@ def mapping(input_file, loc_file, fill_mode):
|
|
162 |
min_dist = curr_x**2 + curr_y**2
|
163 |
|
164 |
if input_dict[nearest_channel].used == True:
|
165 |
-
missing_channels.append('CZ')
|
166 |
input_dict[nearest_channel].used = True
|
167 |
input_dict['CZ'] = input_dict[nearest_channel]
|
168 |
print("CZ's nearest neighbor:", nearest_channel)
|
@@ -195,7 +226,7 @@ def mapping(input_file, loc_file, fill_mode):
|
|
195 |
if impute_channel not in input_dict or input_dict[impute_channel].used==True:
|
196 |
impute_channel = ''
|
197 |
new_idx[i] = z_row_idx
|
198 |
-
missing_channels.append(
|
199 |
continue
|
200 |
|
201 |
elif fill_mode == 'adjacent':
|
@@ -255,17 +286,19 @@ def mapping(input_file, loc_file, fill_mode):
|
|
255 |
impute_channel = 'CZ'
|
256 |
|
257 |
new_idx[i] = input_dict[impute_channel].index
|
258 |
-
new_idx_name[i] = impute_channel # tmp
|
259 |
if input_dict[impute_channel].used == True: # this channel is shared with others
|
260 |
-
missing_channels.append(
|
261 |
input_dict[impute_channel].used = True
|
262 |
|
263 |
second2 = time.time()
|
264 |
print('Finish at stage 2 ! (',second2 - second1,'s)')
|
265 |
#print('new_idx:', new_idx)
|
266 |
-
#print('
|
267 |
-
|
268 |
-
reorder_data(input_file, new_idx, fill_mode)
|
269 |
|
270 |
-
|
|
|
|
|
|
|
271 |
|
|
|
11 |
filepath = os.path.dirname(str(filename))
|
12 |
old_data = utils.read_train_data(filename)
|
13 |
new_data = np.zeros((30, old_data.shape[1]))
|
14 |
+
#print('old data shape: ', old_data.shape)
|
15 |
+
|
16 |
+
zero_arr = np.zeros((1, old_data.shape[1]))
|
17 |
+
old_data = np.concatenate((old_data, zero_arr), axis=0)
|
18 |
|
|
|
|
|
|
|
|
|
19 |
for i in range(30):
|
20 |
+
curr_idx_set = old_idx[i]
|
21 |
+
#print("channel_{}'s index set: {}".format(i, curr_idx_set))
|
22 |
+
|
23 |
+
if curr_idx_set == []:
|
24 |
+
new_data[i, :] = zero_arr
|
25 |
+
else:
|
26 |
+
tmp_data = [old_data[j, :] for j in curr_idx_set]
|
27 |
+
new_data[i, :] = np.mean(tmp_data, axis=0)
|
28 |
+
|
29 |
+
#print('new data shape: ', new_data.shape)
|
30 |
+
utils.save_data(new_data, filepath+'/temp_data/mapped.csv')
|
31 |
return
|
32 |
|
33 |
|
34 |
+
class Channel:
|
35 |
|
36 |
+
def __init__(self, index, name=None, used=False, coord=None, topo_index=None, topo_location=None):
|
37 |
|
38 |
+
self.name = name
|
39 |
+
self.index = index
|
40 |
+
self.used = used
|
41 |
+
self.coord = coord
|
42 |
+
self.topo_index = topo_index
|
43 |
+
self.topo_location = topo_location
|
44 |
|
45 |
+
def prefix(self):
|
46 |
+
ret = ''.join(filter(str.isalpha, self.name))
|
47 |
+
return ret[:len(ret) - 1] if ret[-1] == 'Z' else ret
|
|
|
|
|
|
|
48 |
|
49 |
+
def suffix(self):
|
50 |
+
return -1 if self.name[-1] == 'Z' else int(''.join(filter(str.isdigit, self.name)))
|
|
|
51 |
|
|
|
|
|
52 |
|
53 |
+
def pack_data(new_idx, missing_channels, tpl_dict, in_dict, tpl_ordered_name, in_ordered_name):
|
54 |
+
|
55 |
+
# tmp...
|
56 |
+
for ch in in_dict:
|
57 |
+
in_dict[ch].coord = None
|
58 |
+
|
59 |
+
return {
|
60 |
+
"newOrder" : [([i] if i!=-1 else []) for i in new_idx],
|
61 |
+
"missingIndex" : missing_channels,
|
62 |
+
"templateByName" : {k : v.__dict__ for k,v in tpl_dict.items()}, # dict, {name:object}
|
63 |
+
"templateByIndex" : tpl_ordered_name, # list
|
64 |
+
"inputByName" : {k : v.__dict__ for k,v in in_dict.items()},
|
65 |
+
"inputByIndex" : in_ordered_name
|
66 |
+
}
|
67 |
|
68 |
def mapping(input_file, loc_file, fill_mode):
|
69 |
second1 = time.time()
|
|
|
89 |
input_montage.rename_channels({channel: str.upper(channel)})
|
90 |
|
91 |
channel = str.upper(channel)
|
92 |
+
input_dict[channel] = Channel(index=i, name=channel, coord=input_montage.get_positions()['ch_pos'][channel])
|
93 |
|
94 |
|
95 |
new_idx = [-1]*30
|
96 |
+
#new_idx_name = ['']*30 # tmp
|
97 |
missing_channels = []
|
98 |
+
exact_missing_channels = []
|
99 |
z_row_idx = len(input_montage.ch_names)
|
100 |
|
101 |
|
|
|
115 |
for i in range(30):
|
116 |
channel = template_montage.ch_names[i]
|
117 |
if channel not in input_dict.keys() | alias.keys():
|
118 |
+
exact_missing_channels.append(i)
|
119 |
finish_flag = 0
|
120 |
continue
|
121 |
|
|
|
126 |
template_dict[alias[channel]].name = alias[channel]
|
127 |
channel = alias[channel]
|
128 |
else:
|
129 |
+
exact_missing_channels.append(i)
|
130 |
finish_flag = 0
|
131 |
continue
|
132 |
|
133 |
new_idx[i] = input_dict[channel].index
|
134 |
+
#new_idx_name[i] = channel # tmp
|
135 |
input_dict[channel].used = True
|
136 |
|
137 |
if finish_flag == 1:
|
|
|
140 |
#print('new idx order:', new_idx)
|
141 |
#print('new_idx_name:', new_idx_name) # tmp
|
142 |
|
143 |
+
#reorder_data(input_file, new_idx, fill_mode)
|
144 |
+
channels_obj = pack_data(new_idx, [],
|
145 |
+
template_dict, input_dict,
|
146 |
+
template_montage.ch_names, input_montage.ch_names)
|
147 |
+
return channels_obj
|
148 |
+
|
149 |
+
elif fill_mode == "mean":
|
150 |
+
channels_obj = pack_data(new_idx, exact_missing_channels,
|
151 |
+
template_dict, input_dict,
|
152 |
+
template_montage.ch_names, input_montage.ch_names)
|
153 |
+
return channels_obj
|
154 |
+
|
155 |
|
156 |
|
157 |
|
|
|
193 |
min_dist = curr_x**2 + curr_y**2
|
194 |
|
195 |
if input_dict[nearest_channel].used == True:
|
196 |
+
missing_channels.append(template_dict['CZ'].index)
|
197 |
input_dict[nearest_channel].used = True
|
198 |
input_dict['CZ'] = input_dict[nearest_channel]
|
199 |
print("CZ's nearest neighbor:", nearest_channel)
|
|
|
226 |
if impute_channel not in input_dict or input_dict[impute_channel].used==True:
|
227 |
impute_channel = ''
|
228 |
new_idx[i] = z_row_idx
|
229 |
+
missing_channels.append(i)
|
230 |
continue
|
231 |
|
232 |
elif fill_mode == 'adjacent':
|
|
|
286 |
impute_channel = 'CZ'
|
287 |
|
288 |
new_idx[i] = input_dict[impute_channel].index
|
289 |
+
#new_idx_name[i] = impute_channel # tmp
|
290 |
if input_dict[impute_channel].used == True: # this channel is shared with others
|
291 |
+
missing_channels.append(i)
|
292 |
input_dict[impute_channel].used = True
|
293 |
|
294 |
second2 = time.time()
|
295 |
print('Finish at stage 2 ! (',second2 - second1,'s)')
|
296 |
#print('new_idx:', new_idx)
|
297 |
+
#print('missing_channels:', missing_channels)
|
298 |
+
#reorder_data(input_file, new_idx, fill_mode)
|
|
|
299 |
|
300 |
+
channels_obj = pack_data(new_idx, missing_channels,
|
301 |
+
template_dict, input_dict,
|
302 |
+
template_montage.ch_names, input_montage.ch_names)
|
303 |
+
return channels_obj
|
304 |
|
template_montage.png
ADDED
![]() |