audrey06100 commited on
Commit
a90ec89
·
1 Parent(s): fba3596
Files changed (1) hide show
  1. app_utils.py +105 -105
app_utils.py CHANGED
@@ -47,6 +47,111 @@ def read_montage_data(loc_file):
47
  }
48
  return tpl_montage, in_montage, tpl_dict, in_dict
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  def save_figures(channel_info, tpl_montage, filename1, filename2):
51
  tpl_names = channel_info["templateNames"]
52
  in_names = channel_info["inputNames"]
@@ -117,63 +222,6 @@ def save_figures(channel_info, tpl_montage, filename1, filename2):
117
  })
118
  return channel_info
119
 
120
- def align_coords(channel_info, tpl_montage, in_montage):
121
- tpl_names = channel_info["templateNames"]
122
- in_names = channel_info["inputNames"]
123
- tpl_dict = channel_info["templateDict"]
124
- in_dict = channel_info["inputDict"]
125
- matched_names = get_matched(tpl_names, tpl_dict)
126
-
127
- # 2D alignment (for visualization purposes)
128
- fig = [tpl_montage.plot(), in_montage.plot()]
129
- ax = [fig[0].axes[0], fig[1].axes[0]]
130
-
131
- # extract the displayed 2D coordinates
132
- all_tpl = ax[0].collections[0].get_offsets().data
133
- all_in= ax[1].collections[0].get_offsets().data
134
- matched_tpl = np.array([all_tpl[tpl_dict[name]["index"]] for name in matched_names])
135
- matched_in = np.array([all_in[in_dict[name]["index"]] for name in matched_names])
136
- plt.close('all')
137
-
138
- # apply TPS to transform in_channels to align with tpl_channels positions
139
- rbf_x = Rbf(matched_in[:,0], matched_in[:,1], matched_tpl[:,0], function='thin_plate')
140
- rbf_y = Rbf(matched_in[:,0], matched_in[:,1], matched_tpl[:,1], function='thin_plate')
141
-
142
- # apply the transformation to all in_channels
143
- transformed_in_x = rbf_x(all_in[:,0], all_in[:,1])
144
- transformed_in_y = rbf_y(all_in[:,0], all_in[:,1])
145
- transformed_in = np.vstack((transformed_in_x, transformed_in_y)).T
146
-
147
- for i, name in enumerate(tpl_names):
148
- tpl_dict[name]["coord_2d"] = all_tpl[i]
149
- for i, name in enumerate(in_names):
150
- in_dict[name]["coord_2d"] = transformed_in[i].tolist()
151
-
152
-
153
- # 3D alignment
154
- all_tpl = np.array([tpl_dict[name]["coord_3d"].tolist() for name in tpl_names])
155
- all_in = np.array([in_dict[name]["coord_3d"].tolist() for name in in_names])
156
- matched_tpl = np.array([all_tpl[tpl_dict[name]["index"]] for name in matched_names])
157
- matched_in = np.array([all_in[in_dict[name]["index"]] for name in matched_names])
158
-
159
- rbf_x = Rbf(matched_in[:,0], matched_in[:,1], matched_in[:,2], matched_tpl[:,0], function='thin_plate')
160
- rbf_y = Rbf(matched_in[:,0], matched_in[:,1], matched_in[:,2], matched_tpl[:,1], function='thin_plate')
161
- rbf_z = Rbf(matched_in[:,0], matched_in[:,1], matched_in[:,2], matched_tpl[:,2], function='thin_plate')
162
-
163
- transformed_in_x = rbf_x(all_in[:,0], all_in[:,1], all_in[:,2])
164
- transformed_in_y = rbf_y(all_in[:,0], all_in[:,1], all_in[:,2])
165
- transformed_in_z = rbf_z(all_in[:,0], all_in[:,1], all_in[:,2])
166
- transformed_in = np.vstack((transformed_in_x, transformed_in_y, transformed_in_z)).T
167
-
168
- for i, name in enumerate(in_names):
169
- in_dict[name]["coord_3d"] = transformed_in[i].tolist()
170
-
171
- channel_info.update({
172
- "templateDict" : tpl_dict,
173
- "inputDict" : in_dict
174
- })
175
- return channel_info
176
-
177
  def find_neighbors(channel_info, empty_tpl_names, old_idx):
178
  in_names = channel_info["inputNames"]
179
  tpl_dict = channel_info["templateDict"]
@@ -193,54 +241,6 @@ def find_neighbors(channel_info, empty_tpl_names, old_idx):
193
 
194
  return old_idx
195
 
196
- def match_names(stage1_info):
197
- # read the location file
198
- loc_file = stage1_info["fileNames"]["inputLocation"]
199
- tpl_montage, in_montage, tpl_dict, in_dict = read_montage_data(loc_file)
200
- tpl_names = tpl_montage.ch_names
201
- in_names = in_montage.ch_names
202
- old_idx = [[None]]*30 # store the indices of the in_channels in the order of tpl_channels
203
- orig_flags = [False]*30
204
-
205
- alias_dict = {
206
- 'T3': 'T7',
207
- 'T4': 'T8',
208
- 'T5': 'P7',
209
- 'T6': 'P8'
210
- }
211
- for i, name in enumerate(tpl_names):
212
- if name in alias_dict and alias_dict[name] in in_dict:
213
- tpl_montage.rename_channels({name: alias_dict[name]})
214
- tpl_dict[alias_dict[name]] = tpl_dict.pop(name)
215
- name = alias_dict[name]
216
-
217
- if name in in_dict:
218
- old_idx[i] = [in_dict[name]["index"]]
219
- orig_flags[i] = True
220
- tpl_dict[name]["matched"] = True
221
- in_dict[name]["assigned"] = True
222
-
223
- # update the names
224
- tpl_names = tpl_montage.ch_names
225
-
226
- stage1_info.update({
227
- "unassignedInputs" : get_unassigned_inputs(in_names, in_dict),
228
- "emptyTemplates" : get_empty_templates(tpl_names, tpl_dict),
229
- "mappingResult" : [
230
- {
231
- "index" : old_idx,
232
- "isOriginalData" : orig_flags
233
- }
234
- ]
235
- })
236
- channel_info = {
237
- "templateNames" : tpl_names,
238
- "inputNames" : in_names,
239
- "templateDict" : tpl_dict,
240
- "inputDict" : in_dict
241
- }
242
- return stage1_info, channel_info, tpl_montage, in_montage
243
-
244
  def optimal_mapping(channel_info):
245
  tpl_names = channel_info["templateNames"]
246
  in_names = channel_info["inputNames"]
 
47
  }
48
  return tpl_montage, in_montage, tpl_dict, in_dict
49
 
50
+ def match_names(stage1_info):
51
+ # read the location file
52
+ loc_file = stage1_info["fileNames"]["inputLocation"]
53
+ tpl_montage, in_montage, tpl_dict, in_dict = read_montage_data(loc_file)
54
+ tpl_names = tpl_montage.ch_names
55
+ in_names = in_montage.ch_names
56
+ old_idx = [[None]]*30 # store the indices of the in_channels in the order of tpl_channels
57
+ orig_flags = [False]*30
58
+
59
+ alias_dict = {
60
+ 'T3': 'T7',
61
+ 'T4': 'T8',
62
+ 'T5': 'P7',
63
+ 'T6': 'P8'
64
+ }
65
+ for i, name in enumerate(tpl_names):
66
+ if name in alias_dict and alias_dict[name] in in_dict:
67
+ tpl_montage.rename_channels({name: alias_dict[name]})
68
+ tpl_dict[alias_dict[name]] = tpl_dict.pop(name)
69
+ name = alias_dict[name]
70
+
71
+ if name in in_dict:
72
+ old_idx[i] = [in_dict[name]["index"]]
73
+ orig_flags[i] = True
74
+ tpl_dict[name]["matched"] = True
75
+ in_dict[name]["assigned"] = True
76
+
77
+ # update the names
78
+ tpl_names = tpl_montage.ch_names
79
+
80
+ stage1_info.update({
81
+ "unassignedInputs" : get_unassigned_inputs(in_names, in_dict),
82
+ "emptyTemplates" : get_empty_templates(tpl_names, tpl_dict),
83
+ "mappingResult" : [
84
+ {
85
+ "index" : old_idx,
86
+ "isOriginalData" : orig_flags
87
+ }
88
+ ]
89
+ })
90
+ channel_info = {
91
+ "templateNames" : tpl_names,
92
+ "inputNames" : in_names,
93
+ "templateDict" : tpl_dict,
94
+ "inputDict" : in_dict
95
+ }
96
+ return stage1_info, channel_info, tpl_montage, in_montage
97
+
98
+ def align_coords(channel_info, tpl_montage, in_montage):
99
+ tpl_names = channel_info["templateNames"]
100
+ in_names = channel_info["inputNames"]
101
+ tpl_dict = channel_info["templateDict"]
102
+ in_dict = channel_info["inputDict"]
103
+ matched_names = get_matched(tpl_names, tpl_dict)
104
+
105
+ # 2D alignment (for visualization purposes)
106
+ fig = [tpl_montage.plot(), in_montage.plot()]
107
+ ax = [fig[0].axes[0], fig[1].axes[0]]
108
+
109
+ # extract the displayed 2D coordinates
110
+ all_tpl = ax[0].collections[0].get_offsets().data
111
+ all_in= ax[1].collections[0].get_offsets().data
112
+ matched_tpl = np.array([all_tpl[tpl_dict[name]["index"]] for name in matched_names])
113
+ matched_in = np.array([all_in[in_dict[name]["index"]] for name in matched_names])
114
+ plt.close('all')
115
+
116
+ # apply TPS to transform in_channels to align with tpl_channels positions
117
+ rbf_x = Rbf(matched_in[:,0], matched_in[:,1], matched_tpl[:,0], function='thin_plate')
118
+ rbf_y = Rbf(matched_in[:,0], matched_in[:,1], matched_tpl[:,1], function='thin_plate')
119
+
120
+ # apply the transformation to all in_channels
121
+ transformed_in_x = rbf_x(all_in[:,0], all_in[:,1])
122
+ transformed_in_y = rbf_y(all_in[:,0], all_in[:,1])
123
+ transformed_in = np.vstack((transformed_in_x, transformed_in_y)).T
124
+
125
+ for i, name in enumerate(tpl_names):
126
+ tpl_dict[name]["coord_2d"] = all_tpl[i]
127
+ for i, name in enumerate(in_names):
128
+ in_dict[name]["coord_2d"] = transformed_in[i].tolist()
129
+
130
+
131
+ # 3D alignment
132
+ all_tpl = np.array([tpl_dict[name]["coord_3d"].tolist() for name in tpl_names])
133
+ all_in = np.array([in_dict[name]["coord_3d"].tolist() for name in in_names])
134
+ matched_tpl = np.array([all_tpl[tpl_dict[name]["index"]] for name in matched_names])
135
+ matched_in = np.array([all_in[in_dict[name]["index"]] for name in matched_names])
136
+
137
+ rbf_x = Rbf(matched_in[:,0], matched_in[:,1], matched_in[:,2], matched_tpl[:,0], function='thin_plate')
138
+ rbf_y = Rbf(matched_in[:,0], matched_in[:,1], matched_in[:,2], matched_tpl[:,1], function='thin_plate')
139
+ rbf_z = Rbf(matched_in[:,0], matched_in[:,1], matched_in[:,2], matched_tpl[:,2], function='thin_plate')
140
+
141
+ transformed_in_x = rbf_x(all_in[:,0], all_in[:,1], all_in[:,2])
142
+ transformed_in_y = rbf_y(all_in[:,0], all_in[:,1], all_in[:,2])
143
+ transformed_in_z = rbf_z(all_in[:,0], all_in[:,1], all_in[:,2])
144
+ transformed_in = np.vstack((transformed_in_x, transformed_in_y, transformed_in_z)).T
145
+
146
+ for i, name in enumerate(in_names):
147
+ in_dict[name]["coord_3d"] = transformed_in[i].tolist()
148
+
149
+ channel_info.update({
150
+ "templateDict" : tpl_dict,
151
+ "inputDict" : in_dict
152
+ })
153
+ return channel_info
154
+
155
  def save_figures(channel_info, tpl_montage, filename1, filename2):
156
  tpl_names = channel_info["templateNames"]
157
  in_names = channel_info["inputNames"]
 
222
  })
223
  return channel_info
224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
  def find_neighbors(channel_info, empty_tpl_names, old_idx):
226
  in_names = channel_info["inputNames"]
227
  tpl_dict = channel_info["templateDict"]
 
241
 
242
  return old_idx
243
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  def optimal_mapping(channel_info):
245
  tpl_names = channel_info["templateNames"]
246
  in_names = channel_info["inputNames"]