audrey06100 commited on
Commit
a2a070a
·
1 Parent(s): 4463626
Files changed (2) hide show
  1. app.py +129 -84
  2. channel_mapping.py +12 -11
app.py CHANGED
@@ -20,7 +20,7 @@ Upload your data's channel locations in `.loc` format, which can be obtained usi
20
  >If you cannot obtain it, we recommend you to download the standard montage <a href="">here</a>. If the channels in those files doesn't match yours, you can use **EEGLAB** to modify them to your needed montage.
21
 
22
  ## Mapping
23
- ...
24
 
25
  ### Step1: Mapping result
26
  After clicking the mapping button, the **template montage** and the **input montage** will be displayed, with the unmatched input channels highlighted in red.
@@ -41,7 +41,7 @@ There're two methods you can choose from:
41
  After filling all the template channels, you can then proceed to **run the model**.
42
 
43
  ## Run model
44
- ...
45
 
46
  """
47
 
@@ -67,7 +67,7 @@ init_js = """
67
  attribute = "name";
68
  }else return;
69
 
70
- // add figure of in_montage
71
  document.querySelector(selector).style.cssText = `
72
  position: relative;
73
  width: 560px;
@@ -99,8 +99,8 @@ init_js = """
99
  left = channel_info.templateDict[channel].css_position[0];
100
  bottom = channel_info.templateDict[channel].css_position[1];
101
 
102
- let rule = `
103
- ${selector}::after{
104
  content: '';
105
  position: absolute;
106
  background-color: red;
@@ -111,18 +111,32 @@ init_js = """
111
  bottom: ${bottom};
112
  }
113
  `;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  // check if indicator already exist
115
- let exist = 0;
116
  const styleSheet = document.styleSheets[0];
117
  for(let i=0; i<styleSheet.cssRules.length; i++){
118
- if(styleSheet.cssRules[i].selectorText == selector+"::after"){
119
- exist = 1;
120
- //console.log('exist!');
121
  styleSheet.deleteRule(i);
122
- styleSheet.insertRule(rule, styleSheet.cssRules.length);
123
  }
124
  }
125
- if(exist == 0) styleSheet.insertRule(rule, styleSheet.cssRules.length);
 
126
  }
127
  """
128
 
@@ -162,9 +176,9 @@ update_js = """
162
  left = channel_info.templateDict[channel].css_position[0];
163
  bottom = channel_info.templateDict[channel].css_position[1];
164
 
165
- let rule = `
166
- ${selector}::after{
167
- content: '';
168
  position: absolute;
169
  background-color: red;
170
  width: 10px;
@@ -174,18 +188,33 @@ update_js = """
174
  bottom: ${bottom};
175
  }
176
  `;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  // check if indicator already exist
178
- let exist = 0;
179
  const styleSheet = document.styleSheets[0];
180
  for(let i=0; i<styleSheet.cssRules.length; i++){
181
- if(styleSheet.cssRules[i].selectorText == selector+"::after"){
182
- exist = 1;
183
- //console.log('exist!');
184
  styleSheet.deleteRule(i);
185
- styleSheet.insertRule(rule, styleSheet.cssRules.length);
186
  }
187
  }
188
- if(exist == 0) styleSheet.insertRule(rule, styleSheet.cssRules.length);
 
189
  }
190
  """
191
 
@@ -198,7 +227,7 @@ with gr.Blocks() as demo:
198
  with gr.Row():
199
  gr.Markdown(
200
  """
201
- <p style="text-align: center;">...</p>
202
  """
203
  )
204
  with gr.Row():
@@ -209,9 +238,9 @@ with gr.Blocks() as demo:
209
  with gr.Row():
210
  in_raw_data = gr.File(label="Raw data (.csv)", file_types=[".csv"])
211
  in_raw_loc = gr.File(label="Channel locations (.loc, .locs)", file_types=[".loc", "locs"])
212
- with gr.Column(): #min_width=100
213
- in_samplerate = gr.Textbox(label="Sampling rate (Hz)")
214
- map_btn = gr.Button("Mapping")
215
 
216
  # ------------------------mapping------------------------
217
  # description for stage1-123
@@ -220,11 +249,10 @@ with gr.Blocks() as demo:
220
  # stage1-1 : mapping result
221
  with gr.Row():
222
  tpl_montage = gr.Image("./template_montage.png", label="Template montage", visible=False)
223
- in_montage = gr.Image(elem_id="input-montage", label="Input channels", visible=False)
224
 
225
  # stage1-2 : assign unmatched input channels to empty template channels
226
- radio = gr.Radio(elem_id="radio", visible=False) #, label=""
227
- step2_btn = gr.Button("Next", visible=False) #, interactive=False
228
 
229
  # stage1-3 : select a way to fill the empty template channels
230
  with gr.Row():
@@ -234,11 +262,13 @@ with gr.Blocks() as demo:
234
  visible=False,
235
  scale=2)
236
  fillmode_btn = gr.Button("OK", visible=False, scale=1)
 
237
 
238
- chkbox_group = gr.CheckboxGroup(elem_id="chkbox-group", label="", visible=False)
239
- step3_btn = gr.Button("Next", visible=False)
240
- next_btn = gr.Button("Next step", visible=False)
241
-
 
242
  # -------------------------------------------------------
243
 
244
  with gr.Column():
@@ -249,7 +279,9 @@ with gr.Blocks() as demo:
249
  ("ART", "EEGART"),
250
  ("IC-U-Net", "ICUNet"),
251
  ("IC-U-Net++", "UNetpp"),
252
- ("IC-U-Net-Attn", "AttUnet")],
 
 
253
  value="EEGART",
254
  label="Model",
255
  scale=2)
@@ -279,7 +311,7 @@ with gr.Blocks() as demo:
279
 
280
  #demo.load(js=js)
281
 
282
- # stage1
283
  def reset_all(raw_data, raw_loc, samplerate):
284
  # verify that all required inputs have been provided
285
  if raw_data == None or raw_loc == None:
@@ -313,23 +345,25 @@ with gr.Blocks() as demo:
313
  # reset layout
314
  return {app_state_json : app_state,
315
  channel_info_json : channel_info,
316
- # ------------------Stage1-----------------------
317
  desc_md : gr.Markdown("### Step1: Mapping result", visible=False),
318
  tpl_montage : gr.Image(visible=False),
319
- in_montage : gr.Image(value=None, visible=False),
320
  radio : gr.Radio(choices=[], value=[], label="", visible=False),
321
  in_fill_mode : gr.Dropdown(value="mean", visible=False),
322
  chkbox_group : gr.CheckboxGroup(choices=[], value=[], label="", visible=False),
323
- fillmode_btn : gr.Button("OK", visible=False),
324
- step2_btn : gr.Button("Next", visible=False),
325
- step3_btn : gr.Button("Next", visible=False),
326
- next_btn : gr.Button("Next step", visible=False),
327
- # ------------------Stage2-----------------------
 
328
  run_btn : gr.Button(interactive=False),
329
  batch_md : gr.Markdown(visible=False),
330
  out_denoised_data : gr.File(visible=False)}
331
 
332
- # stage1-1
 
333
  def mapping_result(app_state, channel_info, raw_loc):
334
  filepath = app_state["filepath"]
335
  filename1 = filepath+"raw_montage_"+str(random.randint(1,10000))+".png"
@@ -358,7 +392,7 @@ with gr.Blocks() as demo:
358
  matched_num = 30 - len(app_state["missingTemplates"])
359
 
360
  # if the input channels(>=30) has all the 30 template channels
361
- # -> Stage2.decode data
362
  if matched_num == 30:
363
  app_state["stage1State"] = "finished"
364
  gr.Info('The mapping process has been finished.')
@@ -366,7 +400,7 @@ with gr.Blocks() as demo:
366
  return {app_state_json : app_state,
367
  desc_md : gr.Markdown("### Mapping result", visible=True),
368
  tpl_montage : gr.Image(visible=True),
369
- in_montage : gr.Image(value=filename2, visible=True),
370
  run_btn : gr.Button(interactive=True)}
371
 
372
  # if matched channels < 30, and there're still some unmatched input channels
@@ -376,20 +410,20 @@ with gr.Blocks() as demo:
376
 
377
  # if input channels < 30, but all of them can match to some template channels
378
  # -> directly use fill_mode to fill the remaining channels
379
- if in_num == matched_num:
380
  app_state["stage1State"] = "step3-initializing"
381
 
382
  return {app_state_json : app_state,
383
  desc_md : gr.Markdown("### Step1: Mapping result", visible=True),
384
  tpl_montage : gr.Image(visible=True),
385
- in_montage : gr.Image(value=filename2, visible=True),
386
  next_btn : gr.Button("Next step", visible=True)}
387
 
388
  map_btn.click(
389
  fn = reset_all,
390
  inputs = [in_raw_data, in_raw_loc, in_samplerate],
391
- outputs = [app_state_json, channel_info_json, desc_md, tpl_montage, in_montage, radio, in_fill_mode,
392
- chkbox_group, fillmode_btn, step2_btn, step3_btn, next_btn, run_btn, batch_md, out_denoised_data]
393
  ).success(
394
  fn = mapping_stage1,
395
  inputs = [app_state_json, channel_info_json, in_raw_loc],
@@ -398,7 +432,7 @@ with gr.Blocks() as demo:
398
  ).success(
399
  fn = mapping_result,
400
  inputs = [app_state_json, channel_info_json, in_raw_loc],
401
- outputs = [app_state_json, desc_md, tpl_montage, in_montage, next_btn, run_btn]
402
  )
403
 
404
 
@@ -423,16 +457,18 @@ with gr.Blocks() as demo:
423
  channel_info_json : channel_info,
424
  desc_md : gr.Markdown("### Step2: Assign unmatched input channels"),
425
  tpl_montage : gr.Image(visible=False),
426
- in_montage : gr.Image(visible=False),
427
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
 
428
  next_btn : gr.Button("Next step")}
429
  else:
430
  return {app_state_json : app_state,
431
  channel_info_json : channel_info,
432
  desc_md : gr.Markdown("### Step2: Assign unmatched input channels"),
433
  tpl_montage : gr.Image(visible=False),
434
- in_montage : gr.Image(visible=False),
435
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
 
436
  step2_btn : gr.Button(visible=True),
437
  next_btn : gr.Button(visible=False)}
438
 
@@ -450,7 +486,7 @@ with gr.Blocks() as demo:
450
  channel_info_json : channel_info,
451
  desc_md : gr.Markdown("### Step3: Fill the remaining template channels"),
452
  tpl_montage : gr.Image(visible=False),
453
- in_montage : gr.Image(visible=False),
454
  in_fill_mode : gr.Dropdown(visible=True),
455
  fillmode_btn : gr.Button(visible=True),
456
  next_btn : gr.Button(visible=False)}
@@ -481,7 +517,7 @@ with gr.Blocks() as demo:
481
  # if all the unmatched template channels were filled by input channels
482
  # -> stage2
483
  if len(app_state["missingTemplates"]) == 0:
484
- print('step2 -> Stage2')
485
  gr.Info('The mapping process has been finished.')
486
  app_state["stage1State"] = "finished"
487
 
@@ -489,6 +525,7 @@ with gr.Blocks() as demo:
489
  channel_info_json : channel_info,
490
  desc_md : gr.Markdown(visible=False),
491
  radio : gr.Radio(visible=False),
 
492
  next_btn : gr.Button(visible=False),
493
  run_btn : gr.Button(interactive=True)}
494
 
@@ -506,6 +543,7 @@ with gr.Blocks() as demo:
506
  radio : gr.Radio(visible=False),
507
  in_fill_mode : gr.Dropdown(visible=True),
508
  fillmode_btn : gr.Button(visible=True),
 
509
  next_btn : gr.Button(visible=False)}
510
 
511
  # stage1-3 -> stage2
@@ -523,7 +561,7 @@ with gr.Blocks() as demo:
523
 
524
  gr.Info('The mapping process has been finished.')
525
  app_state["stage1State"] = "finished"
526
- print('step3 -> Stage2')
527
 
528
  app_state["missingTemplates"] = [channel for channel in channel_info["templateOrder"]
529
  if channel_info["templateDict"][channel]["matched"]==False]
@@ -537,8 +575,8 @@ with gr.Blocks() as demo:
537
  next_btn.click(
538
  fn = init_next_step,
539
  inputs = [app_state_json, channel_info_json, radio, chkbox_group],
540
- outputs = [app_state_json, channel_info_json, desc_md, tpl_montage, in_montage, radio, in_fill_mode,
541
- chkbox_group, fillmode_btn, step2_btn, next_btn, run_btn]
542
  ).success(
543
  fn = None,
544
  js = init_js,
@@ -546,8 +584,7 @@ with gr.Blocks() as demo:
546
  outputs = []
547
  )
548
 
549
- # stage1-2
550
- # def update_selection()
551
  def update_radio(app_state, channel_info, selected):
552
 
553
  # save info before clicking on next_btn
@@ -594,7 +631,14 @@ with gr.Blocks() as demo:
594
  outputs = []
595
  )
596
 
597
- # stage1-3
 
 
 
 
 
 
 
598
  def fill_value(app_state, channel_info, fill_mode):
599
 
600
  if fill_mode == 'zero':
@@ -609,40 +653,31 @@ with gr.Blocks() as demo:
609
 
610
  elif fill_mode == 'mean':
611
  app_state["stage1State"] = "step3-selecting"
612
- app_state = find_neighbors(app_state, channel_info, fill_mode)
613
 
614
- name = app_state["missingTemplates"][0]
615
- idx = channel_info["templateDict"][name]["index"]
616
- value = app_state["stage1NewOrder"][idx]
617
- value = [channel_info["inputOrder"][i] for i in value]
618
- label = name+' (1/'+str(app_state["totalFillingNum"])+')'
 
 
619
 
620
  if app_state["totalFillingNum"] == 1:
621
  return {app_state_json : app_state,
622
  in_fill_mode : gr.Dropdown(visible=False),
623
  fillmode_btn : gr.Button(visible=False),
624
  chkbox_group : gr.CheckboxGroup(choices=channel_info["inputOrder"],
625
- value=value, label=label, visible=True),
626
  next_btn : gr.Button(visible=True)}
627
  else:
628
  return {app_state_json : app_state,
629
  in_fill_mode : gr.Dropdown(visible=False),
630
  fillmode_btn : gr.Button(visible=False),
631
  chkbox_group : gr.CheckboxGroup(choices=channel_info["inputOrder"],
632
- value=value, label=label, visible=True),
633
  step3_btn : gr.Button(visible=True)}
634
 
635
- fillmode_btn.click(
636
- fn = fill_value,
637
- inputs = [app_state_json, channel_info_json, in_fill_mode],
638
- outputs = [app_state_json, desc_md, in_fill_mode, fillmode_btn, chkbox_group, step3_btn, next_btn, run_btn]
639
- ).success(
640
- fn = None,
641
- js = init_js,
642
- inputs = [app_state_json, channel_info_json],
643
- outputs = []
644
- )
645
-
646
  def update_chkbox(app_state, channel_info, selected):
647
 
648
  # save info before clicking on next_btn
@@ -674,6 +709,17 @@ with gr.Blocks() as demo:
674
  return {app_state_json : app_state,
675
  chkbox_group : gr.CheckboxGroup(value=chkbox_value, label=chkbox_label)}
676
 
 
 
 
 
 
 
 
 
 
 
 
677
  step3_btn.click(
678
  fn = update_chkbox,
679
  inputs = [app_state_json, channel_info_json, chkbox_group],
@@ -686,17 +732,16 @@ with gr.Blocks() as demo:
686
  outputs = []
687
  )
688
 
689
-
690
  def delete_file(filename):
691
  try:
692
  os.remove(filename)
693
  except OSError as e:
694
  print(e)
695
 
696
- # stage2
697
  def reset_run(app_state, channel_info, raw_data, model_name):
698
 
699
- # reset in.assigned back to the state after Stage1
700
  for channel in app_state["stage1UnassignedInputs"]:
701
  channel_info["inputDict"][channel]["assigned"] = False
702
 
@@ -725,7 +770,7 @@ with gr.Blocks() as demo:
725
  batch_md : gr.Markdown(visible=False),
726
  out_denoised_data : gr.File(visible=False)}
727
 
728
- def run_model(app_state, channel_info, raw_data, model_name, fill_mode):
729
  filepath = app_state["filepath"]
730
  samplerate = app_state["sampleRate"]
731
  new_filename = app_state["filenames"]["denoised"]
@@ -738,7 +783,7 @@ with gr.Blocks() as demo:
738
  yield {batch_md : gr.Markdown(md, visible=True)}
739
 
740
  if app_state["batchCount"] > 1:
741
- app_state, channel_info = mapping_stage2(app_state, channel_info, fill_mode)
742
  if app_state["runningState"] == "finished":
743
  #yield {batch_md : gr.Markdown("error", visible=True)}
744
  break
@@ -748,7 +793,7 @@ with gr.Blocks() as demo:
748
  total_file_num = utils.preprocessing(filepath, 'mapped.csv', samplerate)
749
  # step2: Signal reconstruction
750
  utils.reconstruct(model_name, total_file_num, filepath, 'denoised.csv', samplerate)
751
- reorder_to_origin(app_state, channel_info, filepath+'denoised.csv', new_filename)
752
 
753
  #if model_name == "(mapped data)":
754
  #return {out_denoised_data : filepath + 'mapped.csv'}
@@ -770,7 +815,7 @@ with gr.Blocks() as demo:
770
 
771
  ).success(
772
  fn = run_model,
773
- inputs = [app_state_json, channel_info_json, in_raw_data, in_model_name, in_fill_mode],
774
  outputs = [run_btn, batch_md, out_denoised_data]
775
  )
776
 
 
20
  >If you cannot obtain it, we recommend you to download the standard montage <a href="">here</a>. If the channels in those files doesn't match yours, you can use **EEGLAB** to modify them to your needed montage.
21
 
22
  ## Mapping
23
+ (...)
24
 
25
  ### Step1: Mapping result
26
  After clicking the mapping button, the **template montage** and the **input montage** will be displayed, with the unmatched input channels highlighted in red.
 
41
  After filling all the template channels, you can then proceed to **run the model**.
42
 
43
  ## Run model
44
+ (...)
45
 
46
  """
47
 
 
67
  attribute = "name";
68
  }else return;
69
 
70
+ // add figure of mapped_montage
71
  document.querySelector(selector).style.cssText = `
72
  position: relative;
73
  width: 560px;
 
99
  left = channel_info.templateDict[channel].css_position[0];
100
  bottom = channel_info.templateDict[channel].css_position[1];
101
 
102
+ let dot_rule = `
103
+ ${selector}::before{
104
  content: '';
105
  position: absolute;
106
  background-color: red;
 
111
  bottom: ${bottom};
112
  }
113
  `;
114
+
115
+ left = parseFloat(left.slice(0, -1))+2.5
116
+ left = left.toString()+"%"
117
+ bottom = parseFloat(bottom.slice(0, -1))-1
118
+ bottom = bottom.toString()+"%"
119
+ let txt_rule = `
120
+ ${selector}::after{
121
+ content: "${channel}";
122
+ position: absolute;
123
+ color: red;
124
+ left: ${left};
125
+ bottom: ${bottom};
126
+ }
127
+ `;
128
+
129
  // check if indicator already exist
 
130
  const styleSheet = document.styleSheets[0];
131
  for(let i=0; i<styleSheet.cssRules.length; i++){
132
+ let tmp = styleSheet.cssRules[i].selectorText;
133
+ if(tmp==selector+"::before" || tmp==selector+"::after"){
 
134
  styleSheet.deleteRule(i);
135
+ i--;
136
  }
137
  }
138
+ styleSheet.insertRule(dot_rule, styleSheet.cssRules.length);
139
+ styleSheet.insertRule(txt_rule, styleSheet.cssRules.length);
140
  }
141
  """
142
 
 
176
  left = channel_info.templateDict[channel].css_position[0];
177
  bottom = channel_info.templateDict[channel].css_position[1];
178
 
179
+ let dot_rule = `
180
+ ${selector}::before{
181
+ content: "";
182
  position: absolute;
183
  background-color: red;
184
  width: 10px;
 
188
  bottom: ${bottom};
189
  }
190
  `;
191
+
192
+ left = parseFloat(left.slice(0, -1))+2.5
193
+ left = left.toString()+"%"
194
+ bottom = parseFloat(bottom.slice(0, -1))-1
195
+ bottom = bottom.toString()+"%"
196
+ let txt_rule = `
197
+ ${selector}::after{
198
+ content: "${channel}";
199
+ position: absolute;
200
+ color: red;
201
+ left: ${left};
202
+ bottom: ${bottom};
203
+ }
204
+ `;
205
+
206
  // check if indicator already exist
 
207
  const styleSheet = document.styleSheets[0];
208
  for(let i=0; i<styleSheet.cssRules.length; i++){
209
+ let tmp = styleSheet.cssRules[i].selectorText;
210
+ if(tmp==selector+"::before" || tmp==selector+"::after"){
211
+ console.log('exist!!', tmp);
212
  styleSheet.deleteRule(i);
213
+ i--;
214
  }
215
  }
216
+ styleSheet.insertRule(dot_rule, styleSheet.cssRules.length);
217
+ styleSheet.insertRule(txt_rule, styleSheet.cssRules.length);
218
  }
219
  """
220
 
 
227
  with gr.Row():
228
  gr.Markdown(
229
  """
230
+ <p style="text-align: center;">(...)</p>
231
  """
232
  )
233
  with gr.Row():
 
238
  with gr.Row():
239
  in_raw_data = gr.File(label="Raw data (.csv)", file_types=[".csv"])
240
  in_raw_loc = gr.File(label="Channel locations (.loc, .locs)", file_types=[".loc", "locs"])
241
+ with gr.Row():
242
+ in_samplerate = gr.Textbox(label="Sampling rate (Hz)", scale=2)
243
+ map_btn = gr.Button("Mapping", scale=1)
244
 
245
  # ------------------------mapping------------------------
246
  # description for stage1-123
 
249
  # stage1-1 : mapping result
250
  with gr.Row():
251
  tpl_montage = gr.Image("./template_montage.png", label="Template montage", visible=False)
252
+ mapped_montage = gr.Image(elem_id="input-montage", label="Input channels", visible=False)
253
 
254
  # stage1-2 : assign unmatched input channels to empty template channels
255
+ radio = gr.Radio(elem_id="radio", visible=False)
 
256
 
257
  # stage1-3 : select a way to fill the empty template channels
258
  with gr.Row():
 
262
  visible=False,
263
  scale=2)
264
  fillmode_btn = gr.Button("OK", visible=False, scale=1)
265
+ chkbox_group = gr.CheckboxGroup(elem_id="chkbox-group", visible=False)
266
 
267
+ with gr.Row():
268
+ clear_btn = gr.Button("Clear", visible=False) #, interactive=False
269
+ step2_btn = gr.Button("Next", visible=False)
270
+ step3_btn = gr.Button("Next", visible=False)
271
+ next_btn = gr.Button("Next step", visible=False)
272
  # -------------------------------------------------------
273
 
274
  with gr.Column():
 
279
  ("ART", "EEGART"),
280
  ("IC-U-Net", "ICUNet"),
281
  ("IC-U-Net++", "UNetpp"),
282
+ ("IC-U-Net-Attn", "AttUnet"),
283
+ "(mapped data)",
284
+ "(denoised data)"],
285
  value="EEGART",
286
  label="Model",
287
  scale=2)
 
311
 
312
  #demo.load(js=js)
313
 
314
+ # -------------------------stage1: channel mapping-------------------------------
315
  def reset_all(raw_data, raw_loc, samplerate):
316
  # verify that all required inputs have been provided
317
  if raw_data == None or raw_loc == None:
 
345
  # reset layout
346
  return {app_state_json : app_state,
347
  channel_info_json : channel_info,
348
+ # ------------------stage1-----------------------
349
  desc_md : gr.Markdown("### Step1: Mapping result", visible=False),
350
  tpl_montage : gr.Image(visible=False),
351
+ mapped_montage : gr.Image(value=None, visible=False),
352
  radio : gr.Radio(choices=[], value=[], label="", visible=False),
353
  in_fill_mode : gr.Dropdown(value="mean", visible=False),
354
  chkbox_group : gr.CheckboxGroup(choices=[], value=[], label="", visible=False),
355
+ fillmode_btn : gr.Button(visible=False),
356
+ clear_btn : gr.Button(visible=False),
357
+ step2_btn : gr.Button(visible=False),
358
+ step3_btn : gr.Button(visible=False),
359
+ next_btn : gr.Button(visible=False),
360
+ # ------------------stage2-----------------------
361
  run_btn : gr.Button(interactive=False),
362
  batch_md : gr.Markdown(visible=False),
363
  out_denoised_data : gr.File(visible=False)}
364
 
365
+
366
+ # ---------------------------stage1-1-------------------------------
367
  def mapping_result(app_state, channel_info, raw_loc):
368
  filepath = app_state["filepath"]
369
  filename1 = filepath+"raw_montage_"+str(random.randint(1,10000))+".png"
 
392
  matched_num = 30 - len(app_state["missingTemplates"])
393
 
394
  # if the input channels(>=30) has all the 30 template channels
395
+ # -> stage2
396
  if matched_num == 30:
397
  app_state["stage1State"] = "finished"
398
  gr.Info('The mapping process has been finished.')
 
400
  return {app_state_json : app_state,
401
  desc_md : gr.Markdown("### Mapping result", visible=True),
402
  tpl_montage : gr.Image(visible=True),
403
+ mapped_montage : gr.Image(value=filename2, visible=True),
404
  run_btn : gr.Button(interactive=True)}
405
 
406
  # if matched channels < 30, and there're still some unmatched input channels
 
410
 
411
  # if input channels < 30, but all of them can match to some template channels
412
  # -> directly use fill_mode to fill the remaining channels
413
+ elif in_num == matched_num:
414
  app_state["stage1State"] = "step3-initializing"
415
 
416
  return {app_state_json : app_state,
417
  desc_md : gr.Markdown("### Step1: Mapping result", visible=True),
418
  tpl_montage : gr.Image(visible=True),
419
+ mapped_montage : gr.Image(value=filename2, visible=True),
420
  next_btn : gr.Button("Next step", visible=True)}
421
 
422
  map_btn.click(
423
  fn = reset_all,
424
  inputs = [in_raw_data, in_raw_loc, in_samplerate],
425
+ outputs = [app_state_json, channel_info_json, desc_md, tpl_montage, mapped_montage, radio, in_fill_mode, chkbox_group,
426
+ fillmode_btn, clear_btn, step2_btn, step3_btn, next_btn, run_btn, batch_md, out_denoised_data]
427
  ).success(
428
  fn = mapping_stage1,
429
  inputs = [app_state_json, channel_info_json, in_raw_loc],
 
432
  ).success(
433
  fn = mapping_result,
434
  inputs = [app_state_json, channel_info_json, in_raw_loc],
435
+ outputs = [app_state_json, desc_md, tpl_montage, mapped_montage, next_btn, run_btn]
436
  )
437
 
438
 
 
457
  channel_info_json : channel_info,
458
  desc_md : gr.Markdown("### Step2: Assign unmatched input channels"),
459
  tpl_montage : gr.Image(visible=False),
460
+ mapped_montage : gr.Image(visible=False),
461
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
462
+ clear_btn : gr.Button(visible=True),
463
  next_btn : gr.Button("Next step")}
464
  else:
465
  return {app_state_json : app_state,
466
  channel_info_json : channel_info,
467
  desc_md : gr.Markdown("### Step2: Assign unmatched input channels"),
468
  tpl_montage : gr.Image(visible=False),
469
+ mapped_montage : gr.Image(visible=False),
470
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
471
+ clear_btn : gr.Button(visible=True),
472
  step2_btn : gr.Button(visible=True),
473
  next_btn : gr.Button(visible=False)}
474
 
 
486
  channel_info_json : channel_info,
487
  desc_md : gr.Markdown("### Step3: Fill the remaining template channels"),
488
  tpl_montage : gr.Image(visible=False),
489
+ mapped_montage : gr.Image(visible=False),
490
  in_fill_mode : gr.Dropdown(visible=True),
491
  fillmode_btn : gr.Button(visible=True),
492
  next_btn : gr.Button(visible=False)}
 
517
  # if all the unmatched template channels were filled by input channels
518
  # -> stage2
519
  if len(app_state["missingTemplates"]) == 0:
520
+ print('step2 -> stage2')
521
  gr.Info('The mapping process has been finished.')
522
  app_state["stage1State"] = "finished"
523
 
 
525
  channel_info_json : channel_info,
526
  desc_md : gr.Markdown(visible=False),
527
  radio : gr.Radio(visible=False),
528
+ clear_btn : gr.Button(visible=False),
529
  next_btn : gr.Button(visible=False),
530
  run_btn : gr.Button(interactive=True)}
531
 
 
543
  radio : gr.Radio(visible=False),
544
  in_fill_mode : gr.Dropdown(visible=True),
545
  fillmode_btn : gr.Button(visible=True),
546
+ clear_btn : gr.Button(visible=False),
547
  next_btn : gr.Button(visible=False)}
548
 
549
  # stage1-3 -> stage2
 
561
 
562
  gr.Info('The mapping process has been finished.')
563
  app_state["stage1State"] = "finished"
564
+ print('step3 -> stage2')
565
 
566
  app_state["missingTemplates"] = [channel for channel in channel_info["templateOrder"]
567
  if channel_info["templateDict"][channel]["matched"]==False]
 
575
  next_btn.click(
576
  fn = init_next_step,
577
  inputs = [app_state_json, channel_info_json, radio, chkbox_group],
578
+ outputs = [app_state_json, channel_info_json, desc_md, tpl_montage, mapped_montage, radio, in_fill_mode,
579
+ chkbox_group, fillmode_btn, clear_btn, step2_btn, next_btn, run_btn]
580
  ).success(
581
  fn = None,
582
  js = init_js,
 
584
  outputs = []
585
  )
586
 
587
+ # ---------------------------stage1-2-------------------------------
 
588
  def update_radio(app_state, channel_info, selected):
589
 
590
  # save info before clicking on next_btn
 
631
  outputs = []
632
  )
633
 
634
+ clear_btn.click(
635
+ fn = lambda : gr.Radio(value=[]),
636
+ inputs = [],
637
+ outputs = radio
638
+ )
639
+
640
+
641
+ # ---------------------------stage1-3-------------------------------
642
  def fill_value(app_state, channel_info, fill_mode):
643
 
644
  if fill_mode == 'zero':
 
653
 
654
  elif fill_mode == 'mean':
655
  app_state["stage1State"] = "step3-selecting"
656
+ app_state = find_neighbors(app_state, channel_info)
657
 
658
+ # init stage1-3-selecting
659
+ target_name = app_state["missingTemplates"][0]
660
+ target_idx = channel_info["templateDict"][target_name]["index"]
661
+
662
+ chkbox_value = app_state["stage1NewOrder"][target_idx]
663
+ chkbox_value = [channel_info["inputOrder"][i] for i in chkbox_value]
664
+ chkbox_label = target_name+' (1/'+str(app_state["totalFillingNum"])+')'
665
 
666
  if app_state["totalFillingNum"] == 1:
667
  return {app_state_json : app_state,
668
  in_fill_mode : gr.Dropdown(visible=False),
669
  fillmode_btn : gr.Button(visible=False),
670
  chkbox_group : gr.CheckboxGroup(choices=channel_info["inputOrder"],
671
+ value=chkbox_value, label=chkbox_label, visible=True),
672
  next_btn : gr.Button(visible=True)}
673
  else:
674
  return {app_state_json : app_state,
675
  in_fill_mode : gr.Dropdown(visible=False),
676
  fillmode_btn : gr.Button(visible=False),
677
  chkbox_group : gr.CheckboxGroup(choices=channel_info["inputOrder"],
678
+ value=chkbox_value, label=chkbox_label, visible=True),
679
  step3_btn : gr.Button(visible=True)}
680
 
 
 
 
 
 
 
 
 
 
 
 
681
  def update_chkbox(app_state, channel_info, selected):
682
 
683
  # save info before clicking on next_btn
 
709
  return {app_state_json : app_state,
710
  chkbox_group : gr.CheckboxGroup(value=chkbox_value, label=chkbox_label)}
711
 
712
+ fillmode_btn.click(
713
+ fn = fill_value,
714
+ inputs = [app_state_json, channel_info_json, in_fill_mode],
715
+ outputs = [app_state_json, desc_md, in_fill_mode, fillmode_btn, chkbox_group, step3_btn, next_btn, run_btn]
716
+ ).success(
717
+ fn = None,
718
+ js = init_js,
719
+ inputs = [app_state_json, channel_info_json],
720
+ outputs = []
721
+ )
722
+
723
  step3_btn.click(
724
  fn = update_chkbox,
725
  inputs = [app_state_json, channel_info_json, chkbox_group],
 
732
  outputs = []
733
  )
734
 
735
+ # -------------------------stage2: decode data-------------------------------
736
  def delete_file(filename):
737
  try:
738
  os.remove(filename)
739
  except OSError as e:
740
  print(e)
741
 
 
742
  def reset_run(app_state, channel_info, raw_data, model_name):
743
 
744
+ # reset in.assigned back to the state after stage1
745
  for channel in app_state["stage1UnassignedInputs"]:
746
  channel_info["inputDict"][channel]["assigned"] = False
747
 
 
770
  batch_md : gr.Markdown(visible=False),
771
  out_denoised_data : gr.File(visible=False)}
772
 
773
+ def run_model(app_state, channel_info, raw_data, model_name):
774
  filepath = app_state["filepath"]
775
  samplerate = app_state["sampleRate"]
776
  new_filename = app_state["filenames"]["denoised"]
 
783
  yield {batch_md : gr.Markdown(md, visible=True)}
784
 
785
  if app_state["batchCount"] > 1:
786
+ app_state, channel_info = mapping_stage2(app_state, channel_info)
787
  if app_state["runningState"] == "finished":
788
  #yield {batch_md : gr.Markdown("error", visible=True)}
789
  break
 
793
  total_file_num = utils.preprocessing(filepath, 'mapped.csv', samplerate)
794
  # step2: Signal reconstruction
795
  utils.reconstruct(model_name, total_file_num, filepath, 'denoised.csv', samplerate)
796
+ reorder_to_origin(app_state, channel_info, new_filename)
797
 
798
  #if model_name == "(mapped data)":
799
  #return {out_denoised_data : filepath + 'mapped.csv'}
 
815
 
816
  ).success(
817
  fn = run_model,
818
+ inputs = [app_state_json, channel_info_json, in_raw_data, in_model_name],
819
  outputs = [run_btn, batch_md, out_denoised_data]
820
  )
821
 
channel_mapping.py CHANGED
@@ -22,20 +22,21 @@ def reorder_to_template(app_state, filename):
22
  old_data = np.concatenate((old_data, zero_arr), axis=0)
23
 
24
  for i in range(30):
25
- curr_idx_set = old_idx[i]
26
- #print("channel_{}'s index set: {}".format(i, curr_idx_set))
27
 
28
- if curr_idx_set == []:
29
  new_data[i, :] = zero_arr
30
  else:
31
- tmp_data = [old_data[j, :] for j in curr_idx_set]
32
  new_data[i, :] = np.mean(tmp_data, axis=0)
33
 
34
  print('old.shape, new.shape: ', old_data.shape, new_data.shape)
35
  utils.save_data(new_data, new_filename)
36
  return
37
 
38
- def reorder_to_origin(app_state, channel_info, filename, new_filename):
 
39
  old_idx = app_state["stage1NewOrder"] if app_state["runningState"]=="stage1" else app_state["stage2NewOrder"]
40
  old_data = utils.read_train_data(filename) # denoised data
41
  template_order = channel_info["templateOrder"]
@@ -48,7 +49,7 @@ def reorder_to_origin(app_state, channel_info, filename, new_filename):
48
  for i, channel in enumerate(template_order):
49
  idx_set = old_idx[i]
50
 
51
- # ignore if this channel doesn't exist
52
  if len(idx_set)==1 and channel_info["templateDict"][channel]["matched"]==True:
53
  new_data[idx_set[0], :] = old_data[i, :]
54
 
@@ -96,7 +97,7 @@ def align_coords(channel_info, template_montage, input_montage):
96
  input_order = channel_info["inputOrder"]
97
  matched = [channel for channel in input_order if input_dict[channel]["matched"]==True]
98
 
99
- # 2-d (for the indication of missing template channel's position when fill_mode:'mean_manual')
100
  fig = [template_montage.plot(), input_montage.plot()]
101
  fig[0].set_size_inches(5.6, 5.6)
102
  fig[1].set_size_inches(5.6, 5.6)
@@ -161,7 +162,7 @@ def align_coords(channel_info, template_montage, input_montage):
161
  })
162
  return channel_info
163
 
164
- def find_neighbors(app_state, channel_info, fill_mode):
165
  new_idx = app_state["stage1NewOrder"] if app_state["runningState"]=="stage1" else app_state["stage2NewOrder"]
166
  template_dict = channel_info["templateDict"]
167
  input_dict = channel_info["inputDict"]
@@ -170,7 +171,7 @@ def find_neighbors(app_state, channel_info, fill_mode):
170
  #z_row_idx = channel_info["dataShape"][0]
171
  missing_channels = app_state["missingTemplates"]
172
  if missing_channels == []:
173
- return app_state # change mothing
174
 
175
 
176
  in_coords = [input_dict[channel]["coord"] for channel in input_order]
@@ -254,7 +255,7 @@ def mapping_stage1(app_state, channel_info, loc_file):
254
  print('Mapping (stage1) finished in',second2 - second1,'s.')
255
  yield app_state, channel_info, gr.Markdown("", visible=False)
256
 
257
- def mapping_stage2(app_state, channel_info, fill_mode):
258
  second1 = time.time()
259
 
260
  template_dict = channel_info["templateDict"]
@@ -312,7 +313,7 @@ def mapping_stage2(app_state, channel_info, fill_mode):
312
  })
313
 
314
  # fill the missing_channels channels
315
- app_state = find_neighbors(app_state, channel_info, fill_mode)
316
 
317
  second2 = time.time()
318
  print(f'Mapping (stage2-{app_state["batchCount"]-1}) finished in {second2 - second1}s.')
 
22
  old_data = np.concatenate((old_data, zero_arr), axis=0)
23
 
24
  for i in range(30):
25
+ idx_set = old_idx[i]
26
+ #print("channel_{}'s index set: {}".format(i, idx_set))
27
 
28
+ if idx_set == []:
29
  new_data[i, :] = zero_arr
30
  else:
31
+ tmp_data = [old_data[j, :] for j in idx_set]
32
  new_data[i, :] = np.mean(tmp_data, axis=0)
33
 
34
  print('old.shape, new.shape: ', old_data.shape, new_data.shape)
35
  utils.save_data(new_data, new_filename)
36
  return
37
 
38
+ def reorder_to_origin(app_state, channel_info, new_filename):
39
+ filename = app_state["filepath"]+'denoised.csv'
40
  old_idx = app_state["stage1NewOrder"] if app_state["runningState"]=="stage1" else app_state["stage2NewOrder"]
41
  old_data = utils.read_train_data(filename) # denoised data
42
  template_order = channel_info["templateOrder"]
 
49
  for i, channel in enumerate(template_order):
50
  idx_set = old_idx[i]
51
 
52
+ # ignore if this channel was filled with fill_mode ('mean' or 'zero')
53
  if len(idx_set)==1 and channel_info["templateDict"][channel]["matched"]==True:
54
  new_data[idx_set[0], :] = old_data[i, :]
55
 
 
97
  input_order = channel_info["inputOrder"]
98
  matched = [channel for channel in input_order if input_dict[channel]["matched"]==True]
99
 
100
+ # 2-d (for the indication of missing template channel's position when fill_mode:'mean')
101
  fig = [template_montage.plot(), input_montage.plot()]
102
  fig[0].set_size_inches(5.6, 5.6)
103
  fig[1].set_size_inches(5.6, 5.6)
 
162
  })
163
  return channel_info
164
 
165
+ def find_neighbors(app_state, channel_info):
166
  new_idx = app_state["stage1NewOrder"] if app_state["runningState"]=="stage1" else app_state["stage2NewOrder"]
167
  template_dict = channel_info["templateDict"]
168
  input_dict = channel_info["inputDict"]
 
171
  #z_row_idx = channel_info["dataShape"][0]
172
  missing_channels = app_state["missingTemplates"]
173
  if missing_channels == []:
174
+ return app_state # change nothing
175
 
176
 
177
  in_coords = [input_dict[channel]["coord"] for channel in input_order]
 
255
  print('Mapping (stage1) finished in',second2 - second1,'s.')
256
  yield app_state, channel_info, gr.Markdown("", visible=False)
257
 
258
+ def mapping_stage2(app_state, channel_info):
259
  second1 = time.time()
260
 
261
  template_dict = channel_info["templateDict"]
 
313
  })
314
 
315
  # fill the missing_channels channels
316
+ app_state = find_neighbors(app_state, channel_info)
317
 
318
  second2 = time.time()
319
  print(f'Mapping (stage2-{app_state["batchCount"]-1}) finished in {second2 - second1}s.')