audrey06100 commited on
Commit
40b4459
·
1 Parent(s): b62670c
Files changed (2) hide show
  1. app.py +48 -47
  2. channel_mapping.py +9 -9
app.py CHANGED
@@ -52,10 +52,10 @@ init_js = """
52
  let selector, attribute;
53
  let channel, left, bottom;
54
 
55
- if(app_state.state == "step2-selecting"){
56
  selector = "#radio> div:nth-of-type(2)";
57
  attribute = "value";
58
- }else if(app_state.state == "step3-selecting"){
59
  selector = "#chkbox-group> div:nth-of-type(2)";
60
  attribute = "name";
61
  }else return;
@@ -129,7 +129,7 @@ update_js = """
129
  let selector;
130
  let channel, left, bottom;
131
 
132
- if(app_state.state == "step2-selecting"){
133
  selector = "#radio> div:nth-of-type(2)";
134
 
135
  // update the radios
@@ -148,7 +148,7 @@ update_js = """
148
  item.className = "";
149
  item.querySelector(":scope> span").innerText = "";
150
  });
151
- }else if(app_state.state == "step3-selecting"){
152
  selector = "#chkbox-group> div:nth-of-type(2)";
153
  }else return;
154
 
@@ -211,19 +211,19 @@ with gr.Blocks() as demo:
211
  map_btn = gr.Button("Mapping")
212
 
213
  # ------------------------mapping------------------------
214
- # description for step123
215
- desc_md = gr.Markdown("### Mapping result:", visible=False) # """??? # test
216
 
217
- # step1 : mapping result
218
  with gr.Row():
219
  tpl_montage = gr.Image("./template_montage.png", label="Template montage", visible=False)
220
  map_montage = gr.Image(label="Input channels", visible=False)
221
 
222
- # step2 : assign unmatched input channels to empty template channels
223
  radio = gr.Radio(elem_id="radio", visible=False) #, label=""
224
  step2_btn = gr.Button("Next", visible=False) #, interactive=False
225
 
226
- # step3 : select a way to fill the empty template channels
227
  with gr.Row():
228
  in_fill_mode = gr.Dropdown(choices=["mean", "zero"],
229
  value="mean",
@@ -278,7 +278,7 @@ with gr.Blocks() as demo:
278
 
279
  #demo.load(js=js)
280
 
281
- # click on mapping button
282
  def reset_all(raw_data, raw_loc, samplerate):
283
  # verify that all required inputs have been provided
284
  if raw_data == None or raw_loc == None:
@@ -303,7 +303,7 @@ with gr.Blocks() as demo:
303
  "filepath": filepath+"/temp_data/",
304
  "filenames": {},
305
  "sampleRate": int(samplerate),
306
- "state" : "step1"
307
  }
308
  channel_info = {
309
  #"dataShape" : data.shape
@@ -313,7 +313,7 @@ with gr.Blocks() as demo:
313
  return {app_state_json : app_state,
314
  channel_info_json : channel_info,
315
  # ------------------Stage1-----------------------
316
- desc_md : gr.Markdown(visible=False),# res_md
317
  tpl_montage : gr.Image(visible=False),
318
  map_montage : gr.Image(value=None, visible=False),
319
  radio : gr.Radio(choices=[], value=[], label="", visible=False),
@@ -328,7 +328,7 @@ with gr.Blocks() as demo:
328
  batch_md : gr.Markdown(visible=False),
329
  out_denoised_data : gr.File(visible=False)}
330
 
331
- # step1
332
  def mapping_result(app_state, channel_info, raw_loc):
333
  filepath = app_state["filepath"]
334
  filename = filepath+"raw_montage_"+str(random.randint(1,10000))+".png"
@@ -347,11 +347,11 @@ with gr.Blocks() as demo:
347
  # if the input channels(>=30) has all the 30 template channels
348
  # -> Stage2.decode data
349
  if matched_num == 30:
350
- app_state["state"] = "finished"
351
  gr.Info('The mapping process is finished!')
352
 
353
  return {app_state_json : app_state,
354
- desc_md : gr.Markdown("### Mapping result", visible=True),
355
  tpl_montage : gr.Image(visible=True),
356
  map_montage : gr.Image(value=filename, visible=True),
357
  run_btn : gr.Button(interactive=True)}
@@ -359,15 +359,15 @@ with gr.Blocks() as demo:
359
  # if matched channels < 30, and there're still some unmatched input channels
360
  # -> assign these input channels to nearby unmatched/empty template channels
361
  if in_num > matched_num:
362
- app_state["state"] = "step2-initializing"
363
 
364
  # if input channels < 30, but all of them can match to some template channels
365
  # -> directly use fill_mode to fill the remaining channels
366
  if in_num == matched_num:
367
- app_state["state"] = "step3-initializing"
368
 
369
  return {app_state_json : app_state,
370
- desc_md : gr.Markdown("### Mapping result", visible=True),
371
  tpl_montage : gr.Image(visible=True),
372
  map_montage : gr.Image(value=filename, visible=True),
373
  next_btn : gr.Button("Next step", visible=True)}
@@ -391,13 +391,13 @@ with gr.Blocks() as demo:
391
 
392
  def init_next_step(app_state, channel_info, selected_radio, selected_chkbox):
393
 
394
- # step1 -> step2
395
- if app_state["state"] == "step2-initializing":
396
  print('step1 -> step2')
397
  app_state["missingTemplates"] = [channel for channel in channel_info["templateByIndex"]
398
  if channel_info["templateByName"][channel]["matched"]==False]
399
  app_state.update({
400
- "state" : "step2-selecting",
401
  "fillingCount" : 1,
402
  "totalFillingNum" : len(app_state["missingTemplates"])
403
  })
@@ -408,7 +408,7 @@ with gr.Blocks() as demo:
408
  if len(app_state["stage1UnassignedInputs"])==1 or app_state["totalFillingNum"]==1:
409
  return {app_state_json : app_state,
410
  channel_info_json : channel_info,
411
- desc_md : gr.Markdown("### step2"),
412
  tpl_montage : gr.Image(visible=False),
413
  map_montage : gr.Image(visible=False),
414
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
@@ -416,34 +416,34 @@ with gr.Blocks() as demo:
416
  else:
417
  return {app_state_json : app_state,
418
  channel_info_json : channel_info,
419
- desc_md : gr.Markdown("### step2"),
420
  tpl_montage : gr.Image(visible=False),
421
  map_montage : gr.Image(visible=False),
422
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
423
  step2_btn : gr.Button(visible=True),
424
  next_btn : gr.Button(visible=False)}
425
 
426
- # step1 -> step3
427
- elif app_state["state"] == "step3-initializing":
428
  print('step1 -> step3')
429
  app_state["missingTemplates"] = [channel for channel in channel_info["templateByIndex"]
430
  if channel_info["templateByName"][channel]["matched"]==False]
431
  app_state.update({
432
- "state" : "step3-initializing",
433
  "fillingCount" : 1,
434
  "totalFillingNum" : len(app_state["missingTemplates"])
435
  })
436
  return {app_state_json : app_state,
437
  channel_info_json : channel_info,
438
- desc_md : gr.Markdown("### step3"),
439
  tpl_montage : gr.Image(visible=False),
440
  map_montage : gr.Image(visible=False),
441
  in_fill_mode : gr.Dropdown(visible=True),
442
  fillmode_btn : gr.Button(visible=True),
443
  next_btn : gr.Button(visible=False)}
444
 
445
- # step2 -> step3/Stage2.decode data
446
- elif app_state["state"] == "step2-selecting":
447
 
448
  # save info before clicking on next_btn
449
  prev_target_name = app_state["missingTemplates"][app_state["fillingCount"]-1]
@@ -466,11 +466,11 @@ with gr.Blocks() as demo:
466
  })
467
 
468
  # if all the unmatched template channels were filled by input channels
469
- # -> Stage2
470
  if len(app_state["missingTemplates"]) == 0:
471
  print('step2 -> Stage2')
472
  gr.Info('The mapping process is finished!')
473
- app_state["state"] = "finished"
474
 
475
  return {app_state_json : app_state,
476
  channel_info_json : channel_info,
@@ -479,24 +479,24 @@ with gr.Blocks() as demo:
479
  next_btn : gr.Button(visible=False),
480
  run_btn : gr.Button(interactive=True)}
481
 
482
- # -> step3
483
  else:
484
  print('step2 -> step3')
485
  app_state.update({
486
- "state" : "step3-initializing",
487
  "fillingCount" : 1,
488
  "totalFillingNum" : len(app_state["missingTemplates"])
489
  })
490
  return {app_state_json : app_state,
491
  channel_info_json : channel_info,
492
- desc_md : gr.Markdown("### step3"),
493
  radio : gr.Radio(visible=False),
494
  in_fill_mode : gr.Dropdown(visible=True),
495
  fillmode_btn : gr.Button(visible=True),
496
  next_btn : gr.Button(visible=False)}
497
 
498
- # step3 -> Stage2.decode data
499
- elif app_state["state"] == "step3-selecting":
500
 
501
  # save info before clicking on next_btn
502
  prev_target_name = app_state["missingTemplates"][app_state["fillingCount"]-1]
@@ -509,7 +509,7 @@ with gr.Blocks() as demo:
509
  #print(f'{prev_target_name}({prev_target_idx}): {selected_chkbox}')
510
 
511
  gr.Info('The mapping process is finished!')
512
- app_state["state"] = "finished"
513
  print('step3 -> Stage2')
514
 
515
  app_state["missingTemplates"] = [channel for channel in channel_info["templateByIndex"]
@@ -533,7 +533,7 @@ with gr.Blocks() as demo:
533
  outputs = []
534
  )
535
 
536
- # step2
537
  # def update_selection()
538
  def update_radio(app_state, channel_info, selected):
539
 
@@ -581,11 +581,11 @@ with gr.Blocks() as demo:
581
  outputs = []
582
  )
583
 
584
- # step3
585
  def fill_value(app_state, channel_info, fill_mode):
586
 
587
  if fill_mode == 'zero':
588
- app_state["state"] = "finished"
589
  gr.Info('The mapping process is finished!')
590
 
591
  return {app_state_json : app_state,
@@ -595,7 +595,7 @@ with gr.Blocks() as demo:
595
  run_btn : gr.Button(interactive=True)}
596
 
597
  elif fill_mode == 'mean':
598
- app_state["state"] = "step3-selecting"
599
  app_state = find_neighbors(app_state, channel_info, fill_mode)
600
 
601
  name = app_state["missingTemplates"][0]
@@ -673,13 +673,14 @@ with gr.Blocks() as demo:
673
  outputs = []
674
  )
675
 
676
-
677
  def delete_file(filename):
678
  try:
679
  os.remove(filename)
680
  except OSError as e:
681
  print(e)
682
 
 
683
  def reset_run(app_state, channel_info, raw_data, model_name):
684
  filepath = app_state["filepath"]
685
  delete_file(filepath+'mapped.csv')
@@ -695,7 +696,7 @@ with gr.Blocks() as demo:
695
 
696
  app_state["filenames"]["denoised"] = filepath + output_name
697
  app_state.update({
698
- "runnigState" : "stage1",
699
  "batchCount" : 1,
700
  "totalBatchNum" : batch_num,
701
  "stage2UnassignedInputs" : app_state["stage1UnassignedInputs"],
@@ -717,16 +718,16 @@ with gr.Blocks() as demo:
717
  samplerate = app_state["sampleRate"]
718
  new_filename = app_state["filenames"]["denoised"]
719
 
720
- while app_state["runnigState"] != "finished":
721
  #if app_state["batchCount"] > app_state["totalBatchNum"]:
722
- #app_state["runnigState"] = "finished"
723
  #break
724
  md = 'Running model('+str(app_state["batchCount"])+'/'+str(app_state["totalBatchNum"])+')...'
725
  yield {batch_md : gr.Markdown(md, visible=True)}
726
 
727
  if app_state["batchCount"] > 1:
728
  app_state, channel_info = mapping_stage2(app_state, channel_info, fill_mode)
729
- if app_state["runnigState"] == "finished":
730
  #yield {batch_md : gr.Markdown("error", visible=True)}
731
  break
732
 
@@ -762,4 +763,4 @@ with gr.Blocks() as demo:
762
  )
763
 
764
  if __name__ == "__main__":
765
- demo.launch()
 
52
  let selector, attribute;
53
  let channel, left, bottom;
54
 
55
+ if(app_state.stage1State == "step2-selecting"){
56
  selector = "#radio> div:nth-of-type(2)";
57
  attribute = "value";
58
+ }else if(app_state.stage1State == "step3-selecting"){
59
  selector = "#chkbox-group> div:nth-of-type(2)";
60
  attribute = "name";
61
  }else return;
 
129
  let selector;
130
  let channel, left, bottom;
131
 
132
+ if(app_state.stage1State == "step2-selecting"){
133
  selector = "#radio> div:nth-of-type(2)";
134
 
135
  // update the radios
 
148
  item.className = "";
149
  item.querySelector(":scope> span").innerText = "";
150
  });
151
+ }else if(app_state.stage1State == "step3-selecting"){
152
  selector = "#chkbox-group> div:nth-of-type(2)";
153
  }else return;
154
 
 
211
  map_btn = gr.Button("Mapping")
212
 
213
  # ------------------------mapping------------------------
214
+ # description for stage1-123
215
+ desc_md = gr.Markdown("### Step1: Mapping result", visible=False) # """??? # test
216
 
217
+ # stage1-1 : mapping result
218
  with gr.Row():
219
  tpl_montage = gr.Image("./template_montage.png", label="Template montage", visible=False)
220
  map_montage = gr.Image(label="Input channels", visible=False)
221
 
222
+ # stage1-2 : assign unmatched input channels to empty template channels
223
  radio = gr.Radio(elem_id="radio", visible=False) #, label=""
224
  step2_btn = gr.Button("Next", visible=False) #, interactive=False
225
 
226
+ # stage1-3 : select a way to fill the empty template channels
227
  with gr.Row():
228
  in_fill_mode = gr.Dropdown(choices=["mean", "zero"],
229
  value="mean",
 
278
 
279
  #demo.load(js=js)
280
 
281
+ # stage1
282
  def reset_all(raw_data, raw_loc, samplerate):
283
  # verify that all required inputs have been provided
284
  if raw_data == None or raw_loc == None:
 
303
  "filepath": filepath+"/temp_data/",
304
  "filenames": {},
305
  "sampleRate": int(samplerate),
306
+ "stage1State" : "step1"
307
  }
308
  channel_info = {
309
  #"dataShape" : data.shape
 
313
  return {app_state_json : app_state,
314
  channel_info_json : channel_info,
315
  # ------------------Stage1-----------------------
316
+ desc_md : gr.Markdown("### Step1: Mapping result", visible=False),
317
  tpl_montage : gr.Image(visible=False),
318
  map_montage : gr.Image(value=None, visible=False),
319
  radio : gr.Radio(choices=[], value=[], label="", visible=False),
 
328
  batch_md : gr.Markdown(visible=False),
329
  out_denoised_data : gr.File(visible=False)}
330
 
331
+ # stage1-1
332
  def mapping_result(app_state, channel_info, raw_loc):
333
  filepath = app_state["filepath"]
334
  filename = filepath+"raw_montage_"+str(random.randint(1,10000))+".png"
 
347
  # if the input channels(>=30) has all the 30 template channels
348
  # -> Stage2.decode data
349
  if matched_num == 30:
350
+ app_state["stage1State"] = "finished"
351
  gr.Info('The mapping process is finished!')
352
 
353
  return {app_state_json : app_state,
354
+ desc_md : gr.Markdown("### Step1: Mapping result", visible=True),
355
  tpl_montage : gr.Image(visible=True),
356
  map_montage : gr.Image(value=filename, visible=True),
357
  run_btn : gr.Button(interactive=True)}
 
359
  # if matched channels < 30, and there're still some unmatched input channels
360
  # -> assign these input channels to nearby unmatched/empty template channels
361
  if in_num > matched_num:
362
+ app_state["stage1State"] = "step2-initializing"
363
 
364
  # if input channels < 30, but all of them can match to some template channels
365
  # -> directly use fill_mode to fill the remaining channels
366
  if in_num == matched_num:
367
+ app_state["stage1State"] = "step3-initializing"
368
 
369
  return {app_state_json : app_state,
370
+ desc_md : gr.Markdown("### Step1: Mapping result", visible=True),
371
  tpl_montage : gr.Image(visible=True),
372
  map_montage : gr.Image(value=filename, visible=True),
373
  next_btn : gr.Button("Next step", visible=True)}
 
391
 
392
  def init_next_step(app_state, channel_info, selected_radio, selected_chkbox):
393
 
394
+ # stage1-1 -> stage1-2
395
+ if app_state["stage1State"] == "step2-initializing":
396
  print('step1 -> step2')
397
  app_state["missingTemplates"] = [channel for channel in channel_info["templateByIndex"]
398
  if channel_info["templateByName"][channel]["matched"]==False]
399
  app_state.update({
400
+ "stage1State" : "step2-selecting",
401
  "fillingCount" : 1,
402
  "totalFillingNum" : len(app_state["missingTemplates"])
403
  })
 
408
  if len(app_state["stage1UnassignedInputs"])==1 or app_state["totalFillingNum"]==1:
409
  return {app_state_json : app_state,
410
  channel_info_json : channel_info,
411
+ desc_md : gr.Markdown("### Step2"),
412
  tpl_montage : gr.Image(visible=False),
413
  map_montage : gr.Image(visible=False),
414
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
 
416
  else:
417
  return {app_state_json : app_state,
418
  channel_info_json : channel_info,
419
+ desc_md : gr.Markdown("### Step2"),
420
  tpl_montage : gr.Image(visible=False),
421
  map_montage : gr.Image(visible=False),
422
  radio : gr.Radio(choices=app_state["stage1UnassignedInputs"], value=[], label=label, visible=True),
423
  step2_btn : gr.Button(visible=True),
424
  next_btn : gr.Button(visible=False)}
425
 
426
+ # stage1-1 -> stage1-3
427
+ elif app_state["stage1State"] == "step3-initializing":
428
  print('step1 -> step3')
429
  app_state["missingTemplates"] = [channel for channel in channel_info["templateByIndex"]
430
  if channel_info["templateByName"][channel]["matched"]==False]
431
  app_state.update({
432
+ "stage1State" : "step3-initializing",
433
  "fillingCount" : 1,
434
  "totalFillingNum" : len(app_state["missingTemplates"])
435
  })
436
  return {app_state_json : app_state,
437
  channel_info_json : channel_info,
438
+ desc_md : gr.Markdown("### Step3"),
439
  tpl_montage : gr.Image(visible=False),
440
  map_montage : gr.Image(visible=False),
441
  in_fill_mode : gr.Dropdown(visible=True),
442
  fillmode_btn : gr.Button(visible=True),
443
  next_btn : gr.Button(visible=False)}
444
 
445
+ # stage1-2 -> stage1-3 or stage2
446
+ elif app_state["stage1State"] == "step2-selecting":
447
 
448
  # save info before clicking on next_btn
449
  prev_target_name = app_state["missingTemplates"][app_state["fillingCount"]-1]
 
466
  })
467
 
468
  # if all the unmatched template channels were filled by input channels
469
+ # -> stage2
470
  if len(app_state["missingTemplates"]) == 0:
471
  print('step2 -> Stage2')
472
  gr.Info('The mapping process is finished!')
473
+ app_state["stage1State"] = "finished"
474
 
475
  return {app_state_json : app_state,
476
  channel_info_json : channel_info,
 
479
  next_btn : gr.Button(visible=False),
480
  run_btn : gr.Button(interactive=True)}
481
 
482
+ # -> stage1-3
483
  else:
484
  print('step2 -> step3')
485
  app_state.update({
486
+ "stage1State" : "step3-initializing",
487
  "fillingCount" : 1,
488
  "totalFillingNum" : len(app_state["missingTemplates"])
489
  })
490
  return {app_state_json : app_state,
491
  channel_info_json : channel_info,
492
+ desc_md : gr.Markdown("### Step3"),
493
  radio : gr.Radio(visible=False),
494
  in_fill_mode : gr.Dropdown(visible=True),
495
  fillmode_btn : gr.Button(visible=True),
496
  next_btn : gr.Button(visible=False)}
497
 
498
+ # stage1-3 -> stage2
499
+ elif app_state["stage1State"] == "step3-selecting":
500
 
501
  # save info before clicking on next_btn
502
  prev_target_name = app_state["missingTemplates"][app_state["fillingCount"]-1]
 
509
  #print(f'{prev_target_name}({prev_target_idx}): {selected_chkbox}')
510
 
511
  gr.Info('The mapping process is finished!')
512
+ app_state["stage1State"] = "finished"
513
  print('step3 -> Stage2')
514
 
515
  app_state["missingTemplates"] = [channel for channel in channel_info["templateByIndex"]
 
533
  outputs = []
534
  )
535
 
536
+ # stage1-2
537
  # def update_selection()
538
  def update_radio(app_state, channel_info, selected):
539
 
 
581
  outputs = []
582
  )
583
 
584
+ # stage1-3
585
  def fill_value(app_state, channel_info, fill_mode):
586
 
587
  if fill_mode == 'zero':
588
+ app_state["stage1State"] = "finished"
589
  gr.Info('The mapping process is finished!')
590
 
591
  return {app_state_json : app_state,
 
595
  run_btn : gr.Button(interactive=True)}
596
 
597
  elif fill_mode == 'mean':
598
+ app_state["stage1State"] = "step3-selecting"
599
  app_state = find_neighbors(app_state, channel_info, fill_mode)
600
 
601
  name = app_state["missingTemplates"][0]
 
673
  outputs = []
674
  )
675
 
676
+
677
  def delete_file(filename):
678
  try:
679
  os.remove(filename)
680
  except OSError as e:
681
  print(e)
682
 
683
+ # stage2
684
  def reset_run(app_state, channel_info, raw_data, model_name):
685
  filepath = app_state["filepath"]
686
  delete_file(filepath+'mapped.csv')
 
696
 
697
  app_state["filenames"]["denoised"] = filepath + output_name
698
  app_state.update({
699
+ "runningState" : "stage1",
700
  "batchCount" : 1,
701
  "totalBatchNum" : batch_num,
702
  "stage2UnassignedInputs" : app_state["stage1UnassignedInputs"],
 
718
  samplerate = app_state["sampleRate"]
719
  new_filename = app_state["filenames"]["denoised"]
720
 
721
+ while app_state["runningState"] != "finished":
722
  #if app_state["batchCount"] > app_state["totalBatchNum"]:
723
+ #app_state["runningState"] = "finished"
724
  #break
725
  md = 'Running model('+str(app_state["batchCount"])+'/'+str(app_state["totalBatchNum"])+')...'
726
  yield {batch_md : gr.Markdown(md, visible=True)}
727
 
728
  if app_state["batchCount"] > 1:
729
  app_state, channel_info = mapping_stage2(app_state, channel_info, fill_mode)
730
+ if app_state["runningState"] == "finished":
731
  #yield {batch_md : gr.Markdown("error", visible=True)}
732
  break
733
 
 
763
  )
764
 
765
  if __name__ == "__main__":
766
+ demo.launch(server_name="0.0.0.0", server_port=7860)
channel_mapping.py CHANGED
@@ -11,7 +11,7 @@ from scipy.optimize import linear_sum_assignment
11
  from sklearn.neighbors import NearestNeighbors
12
 
13
  def reorder_to_template(app_state, filename):
14
- old_idx = app_state["stage1NewOrder"] if app_state["runnigState"]=="stage1" else app_state["stage2NewOrder"]
15
  old_data = utils.read_train_data(filename) # original raw data
16
  new_data = np.zeros((30, old_data.shape[1])) # reordered raw data
17
  new_filename = app_state["filepath"]+'mapped.csv'
@@ -36,11 +36,11 @@ def reorder_to_template(app_state, 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["runnigState"]=="stage1" else app_state["stage2NewOrder"]
40
  old_data = utils.read_train_data(filename) # denoised data
41
  template_order = channel_info["templateByIndex"]
42
 
43
- if app_state["runnigState"] == "stage1":
44
  new_data = np.zeros((len(channel_info["inputByIndex"]), old_data.shape[1]))
45
  else:
46
  new_data = utils.read_train_data(new_filename)
@@ -162,7 +162,7 @@ def align_coords(channel_info, template_montage, input_montage):
162
  return channel_info
163
 
164
  def find_neighbors(app_state, channel_info, fill_mode):
165
- new_idx = app_state["stage1NewOrder"] if app_state["runnigState"]=="stage1" else app_state["stage2NewOrder"]
166
  template_dict = channel_info["templateByName"]
167
  input_dict = channel_info["inputByName"]
168
  template_order = channel_info["templateByIndex"]
@@ -184,12 +184,12 @@ def find_neighbors(app_state, channel_info, fill_mode):
184
  for channel in missing_channels:
185
  distances, indices = knn.kneighbors(np.array(template_dict[channel]["coord"]).reshape(1,-1))
186
  selected = [input_order[i] for i in indices[0]]
187
- #print(channel, ':', selected)
188
 
189
  idx = template_dict[channel]["index"]
190
  new_idx[idx] = indices[0].tolist()
191
 
192
- if app_state["runnigState"] == "stage1":
193
  app_state["stage1NewOrder"] = new_idx
194
  else:
195
  app_state["stage2NewOrder"] = new_idx
@@ -240,7 +240,7 @@ def mapping_stage1(app_state, channel_info, loc_file):
240
  })
241
  app_state.update({
242
  "stage1NewOrder" : new_idx,
243
- "runnigState" : "stage1",
244
  "stage1UnassignedInputs" : [channel for channel in input_order if input_dict[channel].assigned==False],
245
  "missingTemplates" : [channel for channel in template_order if template_dict[channel].matched==False]
246
  })
@@ -263,7 +263,7 @@ def mapping_stage2(app_state, channel_info, fill_mode):
263
  input_order = channel_info["inputByIndex"]
264
  unassigned = app_state["stage2UnassignedInputs"]
265
  if unassigned == []:
266
- app_state["runnigState"] = "finished"
267
  return app_state, channel_info
268
 
269
  tpl_coords = np.array([template_dict[channel]["coord"] for channel in template_order])
@@ -306,7 +306,7 @@ def mapping_stage2(app_state, channel_info, fill_mode):
306
  })
307
  app_state.update({
308
  "stage2NewOrder" : new_idx,
309
- "runnigState" : "stage2",
310
  "stage2UnassignedInputs" : [channel for channel in input_order if input_dict[channel]["assigned"]==False],
311
  "missingTemplates" : [channel for channel in template_order if template_dict[channel]["matched"]==False]
312
  })
 
11
  from sklearn.neighbors import NearestNeighbors
12
 
13
  def reorder_to_template(app_state, filename):
14
+ old_idx = app_state["stage1NewOrder"] if app_state["runningState"]=="stage1" else app_state["stage2NewOrder"]
15
  old_data = utils.read_train_data(filename) # original raw data
16
  new_data = np.zeros((30, old_data.shape[1])) # reordered raw data
17
  new_filename = app_state["filepath"]+'mapped.csv'
 
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["templateByIndex"]
42
 
43
+ if app_state["runningState"] == "stage1":
44
  new_data = np.zeros((len(channel_info["inputByIndex"]), old_data.shape[1]))
45
  else:
46
  new_data = utils.read_train_data(new_filename)
 
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["templateByName"]
167
  input_dict = channel_info["inputByName"]
168
  template_order = channel_info["templateByIndex"]
 
184
  for channel in missing_channels:
185
  distances, indices = knn.kneighbors(np.array(template_dict[channel]["coord"]).reshape(1,-1))
186
  selected = [input_order[i] for i in indices[0]]
187
+ print(channel, ':', selected)
188
 
189
  idx = template_dict[channel]["index"]
190
  new_idx[idx] = indices[0].tolist()
191
 
192
+ if app_state["runningState"] == "stage1":
193
  app_state["stage1NewOrder"] = new_idx
194
  else:
195
  app_state["stage2NewOrder"] = new_idx
 
240
  })
241
  app_state.update({
242
  "stage1NewOrder" : new_idx,
243
+ "runningState" : "stage1",
244
  "stage1UnassignedInputs" : [channel for channel in input_order if input_dict[channel].assigned==False],
245
  "missingTemplates" : [channel for channel in template_order if template_dict[channel].matched==False]
246
  })
 
263
  input_order = channel_info["inputByIndex"]
264
  unassigned = app_state["stage2UnassignedInputs"]
265
  if unassigned == []:
266
+ app_state["runningState"] = "finished"
267
  return app_state, channel_info
268
 
269
  tpl_coords = np.array([template_dict[channel]["coord"] for channel in template_order])
 
306
  })
307
  app_state.update({
308
  "stage2NewOrder" : new_idx,
309
+ "runningState" : "stage2",
310
  "stage2UnassignedInputs" : [channel for channel in input_order if input_dict[channel]["assigned"]==False],
311
  "missingTemplates" : [channel for channel in template_order if template_dict[channel]["matched"]==False]
312
  })