audrey06100 commited on
Commit
0ab020b
·
1 Parent(s): dfff50a

update channel_mapping.py

Browse files
Files changed (1) hide show
  1. channel_mapping.py +43 -45
channel_mapping.py CHANGED
@@ -55,8 +55,8 @@ def mapping(input_file, loc_file, fill_mode):
55
  #template_montage.plot()
56
  #input_montage.plot()
57
 
58
- template = {}
59
- input = {}
60
 
61
  # convert all channel names to uppercase
62
  for i in range(30):
@@ -64,14 +64,14 @@ def mapping(input_file, loc_file, fill_mode):
64
  template_montage.rename_channels({channel: str.upper(channel)})
65
 
66
  channel = str.upper(channel)
67
- template[channel] = Channel(index=i, name=channel)
68
 
69
  for i in range(len(input_montage.ch_names)):
70
  channel = input_montage.ch_names[i]
71
  input_montage.rename_channels({channel: str.upper(channel)})
72
 
73
  channel = str.upper(channel)
74
- input[channel] = Channel(index=i, coord=input_montage.get_positions()['ch_pos'][channel])
75
 
76
 
77
  new_idx = [-1]*30
@@ -95,23 +95,23 @@ def mapping(input_file, loc_file, fill_mode):
95
 
96
  for i in range(30):
97
  channel = template_montage.ch_names[i]
98
- if channel not in input.keys() | alias.keys():
99
  finish_flag = 0
100
  continue
101
 
102
- if channel not in input and channel in alias:
103
- if alias[channel] in input:
104
  template_montage.rename_channels({channel: alias[channel]})
105
- template[alias[channel]] = template.pop(channel)
106
- template[alias[channel]].name = alias[channel]
107
  channel = alias[channel]
108
  else:
109
  finish_flag = 0
110
  continue
111
 
112
- new_idx[i] = input[channel].index
113
  new_idx_name[i] = channel # tmp
114
- input[channel].used = True
115
 
116
  if finish_flag == 1:
117
  second2 = time.time()
@@ -119,7 +119,7 @@ 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) # & save data to mapped.csv
123
  return
124
 
125
 
@@ -143,8 +143,8 @@ def mapping(input_file, loc_file, fill_mode):
143
 
144
  ver = 'front' if i<3 else 'center' if i==3 else 'back'
145
  hor = 'left' if j<2 else 'center' if j==2 else 'right'
146
- template[channel].topo_index = [i, j]
147
- template[channel].topo_location = [ver, hor]
148
 
149
  if i > 1 and j in [0, 4]:
150
  temporal_channels.append(channel)
@@ -153,20 +153,18 @@ def mapping(input_file, loc_file, fill_mode):
153
 
154
 
155
  # ensure that CZ is found or imputed by another channel
156
- if 'CZ' not in input and fill_mode=='adjacent':
157
  min_dist = 1e5
158
  for channel in input_montage.ch_names:
159
- curr_x, curr_y, curr_z = input[channel].coord.round(6)
160
-
161
  if curr_x**2 + curr_y**2 < min_dist:
162
  nearest_channel = channel
163
  min_dist = curr_x**2 + curr_y**2
164
 
165
- if input[nearest_channel].used == True:
166
  missing_channels.append('CZ')
167
- input[nearest_channel].used = True
168
- input['CZ'] = input[nearest_channel]
169
-
170
  print("CZ's nearest neighbor:", nearest_channel)
171
 
172
 
@@ -176,13 +174,13 @@ def mapping(input_file, loc_file, fill_mode):
176
 
177
  channel = template_montage.ch_names[i]
178
 
179
- curr_prefix = template[channel].prefix()
180
- curr_suffix = template[channel].suffix()
181
 
182
- curr_row = template[channel].topo_index[0]
183
- curr_col = template[channel].topo_index[1]
184
- curr_ver = template[channel].topo_location[0]
185
- curr_hor = template[channel].topo_location[1]
186
 
187
  impute_channel = ''
188
 
@@ -194,25 +192,25 @@ def mapping(input_file, loc_file, fill_mode):
194
  if fill_mode == 'zero':
195
 
196
  impute_channel = curr_prefix+str(1) if curr_hor=='center' else curr_prefix+str(curr_suffix-2)
197
- if impute_channel not in input or input[impute_channel].used==True:
198
  impute_channel = ''
199
  new_idx[i] = z_row_idx
200
  missing_channels.append(channel)
201
  continue
202
 
203
  elif fill_mode == 'adjacent':
 
 
 
 
204
 
205
- if curr_hor == 'center': # FZ, FPZ, CZ...
206
-
207
- if curr_prefix+str(1) in input: # ex: FZ<-F1
208
  impute_channel = curr_prefix + str(1)
209
- elif curr_ver=='front' and template_topo_pos[curr_row+1][curr_col] in input: # ex: FZ<-FCZ
210
- impute_channel = template_topo_pos[curr_row+1][curr_col]
211
 
212
- elif curr_ver=='back' and template_topo_pos[curr_row-1][curr_col] in input: # ex: PZ<-CPZ
213
- impute_channel = template_topo_pos[curr_row-1][curr_col]
214
 
215
- elif curr_prefix+str(3) in input: # ex: FZ<-F3
216
  impute_channel = curr_prefix + str(3)
217
 
218
  else:
@@ -221,23 +219,23 @@ def mapping(input_file, loc_file, fill_mode):
221
  elif curr_hor == 'left' or curr_hor == 'right':
222
 
223
  ver_ctrl = 1 if curr_ver=='front' else 2 if curr_ver=='back' else 3 # bit0: row+1, bit1: row-1
224
- ver_dir = 1 if curr_ver == 'front' else -1
225
 
226
  # search horizontally
227
  cnt = 0
228
  tmp_suffix = curr_suffix
229
  while tmp_suffix > 0: # ex: F7<-F5/F3/F1
230
  tmp_suffix = curr_suffix - 2*cnt
231
- if curr_prefix+str(tmp_suffix) in input:
232
  impute_channel = curr_prefix + str(tmp_suffix)
233
  break
234
 
235
  if cnt == 2:
236
  # check row+1/row-1
237
- if ver_ctrl&1 and template_topo_pos[curr_row+1][curr_col] in input:
238
  impute_channel = template_topo_pos[curr_row+1][curr_col]
239
  break
240
- if ver_ctrl&2 and template_topo_pos[curr_row-1][curr_col] in input:
241
  impute_channel = template_topo_pos[curr_row-1][curr_col]
242
  break
243
  cnt += 1
@@ -247,7 +245,7 @@ def mapping(input_file, loc_file, fill_mode):
247
  cnt = 0
248
  tmp_row = curr_row + ver_dir
249
  while tmp_row-ver_dir != 3: # terminate if the last channel is a middle one
250
- if template_topo_pos[tmp_row][curr_col] in input:
251
  impute_channel = template_topo_pos[tmp_row][curr_col]
252
  break
253
  tmp_row += ver_dir
@@ -256,18 +254,18 @@ def mapping(input_file, loc_file, fill_mode):
256
  if impute_channel == '':
257
  impute_channel = 'CZ'
258
 
259
- new_idx[i] = input[impute_channel].index
260
  new_idx_name[i] = impute_channel # tmp
261
- if input[impute_channel].used == True: # this channel is shared with others
262
  missing_channels.append(channel)
263
- input[impute_channel].used = True
264
 
265
  second2 = time.time()
266
  print('Finish at stage 2 ! (',second2 - second1,'s)')
267
  #print('new_idx:', new_idx)
268
  #print('new_idx_name:', new_idx_name) # tmp
269
  print('missing_channels:', missing_channels)
270
- reorder_data(input_file, new_idx, fill_mode) # & save data to mapped.csv
271
 
272
  return
273
 
 
55
  #template_montage.plot()
56
  #input_montage.plot()
57
 
58
+ template_dict = {}
59
+ input_dict = {}
60
 
61
  # convert all channel names to uppercase
62
  for i in range(30):
 
64
  template_montage.rename_channels({channel: str.upper(channel)})
65
 
66
  channel = str.upper(channel)
67
+ template_dict[channel] = Channel(index=i, name=channel)
68
 
69
  for i in range(len(input_montage.ch_names)):
70
  channel = input_montage.ch_names[i]
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
 
95
 
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
 
102
+ if channel not in input_dict and channel in alias:
103
+ if alias[channel] in input_dict:
104
  template_montage.rename_channels({channel: alias[channel]})
105
+ template_dict[alias[channel]] = template_dict.pop(channel)
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:
117
  second2 = time.time()
 
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
  return
124
 
125
 
 
143
 
144
  ver = 'front' if i<3 else 'center' if i==3 else 'back'
145
  hor = 'left' if j<2 else 'center' if j==2 else 'right'
146
+ template_dict[channel].topo_index = [i, j]
147
+ template_dict[channel].topo_location = [ver, hor]
148
 
149
  if i > 1 and j in [0, 4]:
150
  temporal_channels.append(channel)
 
153
 
154
 
155
  # ensure that CZ is found or imputed by another channel
156
+ if 'CZ' not in input_dict and fill_mode=='adjacent':
157
  min_dist = 1e5
158
  for channel in input_montage.ch_names:
159
+ curr_x, curr_y, curr_z = input_dict[channel].coord.round(6)
 
160
  if curr_x**2 + curr_y**2 < min_dist:
161
  nearest_channel = channel
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)
169
 
170
 
 
174
 
175
  channel = template_montage.ch_names[i]
176
 
177
+ curr_prefix = template_dict[channel].prefix()
178
+ curr_suffix = template_dict[channel].suffix()
179
 
180
+ curr_row = template_dict[channel].topo_index[0]
181
+ curr_col = template_dict[channel].topo_index[1]
182
+ curr_ver = template_dict[channel].topo_location[0]
183
+ curr_hor = template_dict[channel].topo_location[1]
184
 
185
  impute_channel = ''
186
 
 
192
  if fill_mode == 'zero':
193
 
194
  impute_channel = curr_prefix+str(1) if curr_hor=='center' else curr_prefix+str(curr_suffix-2)
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(channel)
199
  continue
200
 
201
  elif fill_mode == 'adjacent':
202
+
203
+ ver_dir = 1 if curr_ver == 'front' else -1
204
+
205
+ if curr_hor == 'center': # FZ, FPZ...
206
 
207
+ if curr_prefix+str(1) in input_dict: # ex: FZ<-F1
 
 
208
  impute_channel = curr_prefix + str(1)
 
 
209
 
210
+ elif template_topo_pos[curr_row+ver_dir][curr_col] in input_dict: # ex: front:FZ<-FCZ,
211
+ impute_channel = template_topo_pos[curr_row+ver_dir][curr_col]
212
 
213
+ elif curr_prefix+str(3) in input_dict: # ex: FZ<-F3
214
  impute_channel = curr_prefix + str(3)
215
 
216
  else:
 
219
  elif curr_hor == 'left' or curr_hor == 'right':
220
 
221
  ver_ctrl = 1 if curr_ver=='front' else 2 if curr_ver=='back' else 3 # bit0: row+1, bit1: row-1
222
+ #ver_dir = 1 if curr_ver == 'front' else -1
223
 
224
  # search horizontally
225
  cnt = 0
226
  tmp_suffix = curr_suffix
227
  while tmp_suffix > 0: # ex: F7<-F5/F3/F1
228
  tmp_suffix = curr_suffix - 2*cnt
229
+ if curr_prefix+str(tmp_suffix) in input_dict:
230
  impute_channel = curr_prefix + str(tmp_suffix)
231
  break
232
 
233
  if cnt == 2:
234
  # check row+1/row-1
235
+ if ver_ctrl&1 and template_topo_pos[curr_row+1][curr_col] in input_dict:
236
  impute_channel = template_topo_pos[curr_row+1][curr_col]
237
  break
238
+ if ver_ctrl&2 and template_topo_pos[curr_row-1][curr_col] in input_dict:
239
  impute_channel = template_topo_pos[curr_row-1][curr_col]
240
  break
241
  cnt += 1
 
245
  cnt = 0
246
  tmp_row = curr_row + ver_dir
247
  while tmp_row-ver_dir != 3: # terminate if the last channel is a middle one
248
+ if template_topo_pos[tmp_row][curr_col] in input_dict:
249
  impute_channel = template_topo_pos[tmp_row][curr_col]
250
  break
251
  tmp_row += ver_dir
 
254
  if impute_channel == '':
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(channel)
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('new_idx_name:', new_idx_name) # tmp
267
  print('missing_channels:', missing_channels)
268
+ reorder_data(input_file, new_idx, fill_mode)
269
 
270
  return
271