forked from NSLS-II/ptycho_gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ptycho_gui.py
1437 lines (1196 loc) · 62.8 KB
/
ptycho_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import random
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog, QAction
from nsls2ptycho.ui import ui_ptycho
from nsls2ptycho.core.utils import clean_shared_memory, get_mpi_num_processes, parse_range
from nsls2ptycho.core.ptycho_param import Param
from nsls2ptycho.core.ptycho_recon import PtychoReconWorker, PtychoReconFakeWorker, HardWorker
from nsls2ptycho.core.ptycho_qt_utils import PtychoStream
from nsls2ptycho.core.widgets.list_widget import ListWidget
from nsls2ptycho.core.widgets.mplcanvas import load_image_pil
from nsls2ptycho.core.ptycho.utils import parse_config
from nsls2ptycho._version import __version__
# databroker related
from nsls2ptycho.core.databroker_api import db, load_metadata, get_single_image, get_detector_names, beamline_name
from nsls2ptycho.reconStep_gui import ReconStepWindow
from nsls2ptycho.roi_gui import RoiWindow
from nsls2ptycho.scan_pt import ScanWindow
import h5py
import numpy as np
from numpy import pi
import traceback
# for frontend-backend communication
from posix_ipc import SharedMemory, ExistentialError
import mmap
# set True for testing GUI changes
_TEST = False
# for shared memory
mm_list = []
shm_list = []
class MainWindow(QtWidgets.QMainWindow, ui_ptycho.Ui_MainWindow):
_mainwindow_signal = QtCore.pyqtSignal()
def __init__(self, parent=None, param:Param=None):
super().__init__(parent)
self.setupUi(self)
QtWidgets.QApplication.setStyle('Plastique')
# connect
self.btn_load_probe.clicked.connect(self.loadProbe)
self.btn_load_object.clicked.connect(self.loadObject)
self.ck_init_prb_flag.clicked.connect(self.resetProbeFlg)
self.ck_init_obj_flag.clicked.connect(self.resetObjectFlg)
self.btn_choose_cwd.clicked.connect(self.setWorkingDirectory)
self.cb_dataloader.currentTextChanged.connect(self.setLoadButton)
self.btn_load_scan.clicked.connect(self.loadExpParam)
self.btn_view_frame.clicked.connect(self.viewDataFrame)
self.ck_extra_scans_flag.clicked.connect(self.updateExtraScansFlg)
self.btn_set_extra_scans.clicked.connect(self.setExtraScans)
#self.le_scan_num.editingFinished.connect(self.forceLoad) # too sensitive, why?
self.le_scan_num.textChanged.connect(self.forceLoad)
self.cb_dataloader.currentTextChanged.connect(self.forceLoad)
self.cb_detectorkind.currentTextChanged.connect(self.forceLoad)
self.ck_mode_flag.clicked.connect(self.modeMultiSliceGuard)
self.ck_multislice_flag.clicked.connect(self.modeMultiSliceGuard)
self.ck_mask_obj_flag.clicked.connect(self.updateObjMaskFlg)
self.ck_gpu_flag.clicked.connect(self.updateGpuFlg)
self.ck_bragg_flag.clicked.connect(self.updateBraggFlg)
self.ck_pc_flag.clicked.connect(self.updatePcFlg)
self.ck_position_correction_flag.clicked.connect(self.updateCorrFlg)
self.ck_refine_data_flag.clicked.connect(self.updateRefineDataFlg)
self.ck_postprocessing_flag.clicked.connect(self.showNoPostProcessingWarning)
self.ck_batch_crop_flag.clicked.connect(self.updateBatchCropDataFlg)
self.cb_dataloader.currentTextChanged.connect(self.updateBatchCropDataFlg)
self.btn_recon_start.clicked.connect(self.start)
self.btn_recon_stop.clicked.connect(self.stop)
self.btn_recon_batch_start.clicked.connect(self.batchStart)
self.btn_recon_batch_stop.clicked.connect(self.batchStop)
self.ck_init_prb_batch_flag.stateChanged.connect(self.switchProbeBatch)
self.ck_init_obj_batch_flag.stateChanged.connect(self.switchObjectBatch)
self.menu_import_config.triggered.connect(self.importConfig)
self.menu_export_config.triggered.connect(self.exportConfig)
self.menu_clear_config_history.triggered.connect(self.removeConfigHistory)
self.menu_save_config_history.triggered.connect(self.saveConfigHistory)
self.actionClear_shared_memory.triggered.connect(self.clearSharedMemory)
self.btn_MPI_file.clicked.connect(self.setMPIfile)
self.le_gpus.textChanged.connect(self.resetMPIFlg)
# setup
self.sp_pha_max.setMaximum(pi)
self.sp_pha_max.setMinimum(-pi)
self.sp_pha_min.setMaximum(pi)
self.sp_pha_min.setMinimum(-pi)
# init.
if param is None:
self.param = Param() # default
else:
self.param = param
self._prb = None
self._obj = None
self._ptycho_gpu_thread = None
self._worker_thread = None
self._db = None # hold the Broker instance that contains the info of the given scan id
self._mds_table = None # hold a Pandas.dataframe instance
self._loaded = False # whether the user has loaded metadata or not (from either databroker or h5)
self._scan_numbers = None # a list of scan numbers for batch mode
self._scan_points = None # an array of shape (2, N) holding the scan coordinates
self._extra_scans_dialog = None
self._batch_prb_filename = None # probe's filename template for batch mode
self._batch_obj_filename = None # object's filename template for batch mode
self._config_path = os.path.expanduser("~") + "/.ptycho_gui/.ptycho_gui_config"
if not os.path.isdir(os.path.dirname(self._config_path)):
os.makedirs(os.path.dirname(self._config_path))
self.reconStepWindow = None
self.roiWindow = None
self.scanWindow = None
# temporary solutions
self.ck_ms_pie_flag.setEnabled(False)
self.ck_weak_obj_flag.setEnabled(False)
#self.cb_alg_flag. addItem("PIE")
#self.cb_alg2_flag.addItem("PIE")
# TODO: find a way to register the live windows so that they can be opened anytime
self.menuWindows.setEnabled(False)
#self.actionROI.setEnabled(False)
#self.actionMonitor.setEnabled(False)
#self.actionScan_points.setEnabled(False)
#if self.menu_save_config_history.isChecked(): # TODO: think of a better way...
self.retrieveConfigHistory()
self.update_gui_from_param()
self.updateExtraScansFlg()
self.updateModeFlg()
self.updateMultiSliceFlg()
self.updateObjMaskFlg()
self.updateBraggFlg()
self.updatePcFlg()
self.updateCorrFlg()
self.updateRefineDataFlg()
self.updateBatchCropDataFlg()
self.checkGpuAvail()
self.updateGpuFlg()
self.resetExperimentalParameters() # probably not necessary
self.setLoadButton()
# generate a unique string for shared memory
if sys.platform.startswith('darwin'): # OS X has a much shorter name limit
self.param.shm_name = os.getlogin()+'_'+str(os.getpid())+'_'+str(random.randrange(256))
else:
self.param.shm_name = 'ptycho_'+os.getlogin()+'_'+str(os.getpid())+'_'+str(random.randrange(256))
# TODO: delete param.shm_name read in from previous config so that we can reset the buttons earlier
self.resetButtons()
# display GUI version
self.setWindowTitle("NSLS-II Ptychography v" + __version__)
@property
def db(self):
# access the Broker instance; the name is probably not intuitive enough...?
return self._db
@db.setter
def db(self, scan_id:int):
# TODO: this should be configured based on selected beamline profile!
self._db = db
def resetButtons(self):
self.btn_recon_start.setEnabled(True)
self.btn_recon_stop.setEnabled(False)
self.btn_recon_batch_start.setEnabled(True)
self.btn_recon_batch_stop.setEnabled(False)
self.recon_bar.setValue(0)
# close the mmap arrays
# removing these arrays, can be changed later if needed
if self._prb is not None:
del self._prb
self._prb = None
if self._obj is not None:
del self._obj
self._obj = None
#if self._scan_points is not None:
# del self._scan_points
# self._scan_points = None
self.close_mmap()
# TODO: consider merging this function with importConfig()?
def retrieveConfigHistory(self):
if os.path.isfile(self._config_path):
try:
param = parse_config(self._config_path, Param())
self.menu_save_config_history.setChecked(param.save_config_history)
if param.save_config_history:
self.param = param
except Exception as ex:
self.exception_handler(ex)
def saveConfigHistory(self):
self.param.save_config_history = self.menu_save_config_history.isChecked()
def removeConfigHistory(self):
if os.path.isfile(self._config_path):
self.param = Param() # default
os.remove(self._config_path)
self.update_gui_from_param()
def update_param_from_gui(self):
p = self.param
# data group
p.scan_num = str(self.le_scan_num.text())
p.detectorkind = str(self.cb_detectorkind.currentText())
p.frame_num = int(self.sp_fram_num.value())
# p.working_directory set by setWorkingDirectory()
# Exp param group
p.xray_energy_kev = float(self.sp_xray_energy.value())
if self.sp_xray_energy.value() != 0.:
p.lambda_nm = 1.2398/self.sp_xray_energy.value()
p.z_m = float(self.sp_detector_distance.value())
p.nx = int(self.sp_x_arr_size.value()) # bookkeeping
p.dr_x = float(self.sp_x_step_size.value())
p.x_range = float(self.sp_x_scan_range.value())
p.ny = int(self.sp_y_arr_size.value()) # bookkeeping
p.dr_y = float(self.sp_y_step_size.value())
p.y_range = float(self.sp_y_scan_range.value())
#p.scan_type = str(self.cb_scan_type.currentText()) # do we need this one?
p.nz = int(self.sp_num_points.value()) # bookkeeping
# recon param group
p.n_iterations = int(self.sp_n_iterations.value())
p.alg_flag = str(self.cb_alg_flag.currentText())
p.alg2_flag = str(self.cb_alg2_flag.currentText())
p.alg_percentage = float(self.sp_alg_percentage.value())
p.sign = str(self.le_sign.text())
p.precision = self.cb_precision_flag.currentText()
p.init_prb_flag = self.ck_init_prb_flag.isChecked()
p.init_obj_flag = self.ck_init_obj_flag.isChecked()
# prb and obj path already set
p.mode_flag = self.ck_mode_flag.isChecked()
p.prb_mode_num = self.sp_prb_mode_num.value()
p.obj_mode_num = self.sp_obj_mode_num.value()
if p.mode_flag and "_mode" not in p.sign:
p.sign = p.sign + "_mode"
p.multislice_flag = self.ck_multislice_flag.isChecked()
p.slice_num = int(self.sp_slice_num.value())
p.slice_spacing_m = float(self.sp_slice_spacing_m.value() * 1e-6)
if p.multislice_flag and "_ms" not in p.sign:
p.sign = p.sign + "_ms"
p.amp_min = float(self.sp_amp_min.value())
p.amp_max = float(self.sp_amp_max.value())
p.pha_min = float(self.sp_pha_min.value())
p.pha_max = float(self.sp_pha_max.value())
p.gpu_flag = self.ck_gpu_flag.isChecked()
p.gpus = parse_range(self.le_gpus.text(), batch_processing=False)
p.gpu_batch_size = int(self.cb_gpu_batch_size.currentText())
# adv param group
p.ccd_pixel_um = float(self.sp_ccd_pixel_um.value())
p.distance = float(self.sp_distance.value())
p.angle_correction_flag = self.ck_angle_correction_flag.isChecked()
p.x_direction = float(self.sp_x_direction.value())
p.y_direction = float(self.sp_y_direction.value())
p.angle = self.sp_angle.value()
p.start_update_probe = self.sp_start_update_probe.value()
p.start_update_object = self.sp_start_update_object.value()
p.ml_mode = self.cb_ml_mode.currentText()
p.ml_weight = self.sp_ml_weight.value()
p.dm_version = self.sp_dm_version.value()
p.cal_scan_pattern_flag = self.ck_cal_scal_pattern_flag.isChecked()
p.nth = self.sp_nth.value()
p.start_ave = self.sp_start_ave.value()
p.processes = self.sp_processes.value()
p.bragg_flag = self.ck_bragg_flag.isChecked()
p.bragg_theta = self.sp_bragg_theta.value()
p.bragg_gamma = self.sp_bragg_gamma.value()
p.bragg_delta = self.sp_bragg_delta.value()
p.pc_flag = self.ck_pc_flag.isChecked()
p.pc_sigma = self.sp_pc_sigma.value()
p.pc_alg = self.cb_pc_alg.currentText()
p.pc_kernel_n = self.sp_pc_kernel_n.value()
p.position_correction_flag = self.ck_position_correction_flag.isChecked()
p.position_correction_start = self.sp_position_correction_start.value()
p.position_correction_step = self.sp_position_correction_step.value()
p.sigma2 = float(self.sp_sigma2.value())
p.beta = float(self.sp_beta.value())
p.display_interval = int(self.sp_display_interval.value())
p.preview_flag = self.ck_preview_flag.isChecked()
p.cal_error_flag = self.ck_cal_error_flag.isChecked()
p.prb_center_flag = self.ck_prb_center_flag.isChecked()
p.mask_obj_flag = self.ck_mask_obj_flag.isChecked()
p.norm_prb_amp_flag = self.ck_norm_prb_amp_flag.isChecked()
p.weak_obj_flag = self.ck_weak_obj_flag.isChecked()
p.ms_pie_flag = self.ck_ms_pie_flag.isChecked()
p.refine_data_flag = self.ck_refine_data_flag.isChecked()
p.refine_data_start_it = int(self.sp_refine_data_start_it.value())
p.refine_data_interval = int(self.sp_refine_data_interval.value())
p.refine_data_step = float(self.sp_refine_data_step.value())
p.profiler_flag = self.ck_profiler_flag.isChecked()
p.postprocessing_flag = self.ck_postprocessing_flag.isChecked()
p.use_NCCL = self.rb_nccl.isChecked()
p.use_CUDA_MPI = self.rb_cuda_mpi.isChecked()
# TODO: organize them
#self.ck_init_obj_dpc_flag.setChecked(p.init_obj_dpc_flag)
#self.ck_mask_prb_flag.setChecked(p.mask_prb_flag)
#self.ck_mesh_flag.setChecked(p.mesh_flag)
#self.ck_sf_flag.setChecked(p.sf_flag)
# batch param group, necessary?
def update_gui_from_param(self):
p = self.param
# Data group
self.le_scan_num.setText(p.scan_num)
self.le_working_directory.setText(str(p.working_directory or ''))
self.cb_detectorkind.setCurrentIndex(p.get_detector_kind_index())
self.sp_fram_num.setValue(int(p.frame_num))
# Exp param group
self.sp_xray_energy.setValue(1.2398/float(p.lambda_nm) if 'lambda_nm' in p.__dict__ else 0.)
self.sp_detector_distance.setValue(float(p.z_m) if 'z_m' in p.__dict__ else 0)
self.sp_x_arr_size.setValue(float(p.nx))
self.sp_x_step_size.setValue(float(p.dr_x))
self.sp_x_scan_range.setValue(float(p.x_range))
self.sp_y_arr_size.setValue(float(p.ny))
self.sp_y_step_size.setValue(float(p.dr_y))
self.sp_y_scan_range.setValue(float(p.y_range))
self.cb_scan_type.setCurrentIndex(p.get_scan_type_index())
self.sp_num_points.setValue(int(p.nz))
# recon param group
self.sp_n_iterations.setValue(int(p.n_iterations))
self.cb_alg_flag.setCurrentIndex(p.get_alg_flg_index())
self.cb_alg2_flag.setCurrentIndex(p.get_alg2_flg_index())
self.sp_alg_percentage.setValue(float(p.alg_percentage))
self.le_sign.setText(p.sign)
self.cb_precision_flag.setCurrentText(p.precision)
self.ck_init_prb_flag.setChecked(p.init_prb_flag)
self.le_prb_path.setText(str(p.prb_filename or ''))
self.ck_init_obj_flag.setChecked(p.init_obj_flag)
self.le_obj_path.setText(str(p.obj_filename or ''))
self.ck_mode_flag.setChecked(p.mode_flag)
self.sp_prb_mode_num.setValue(int(p.prb_mode_num))
self.sp_obj_mode_num.setValue(int(p.obj_mode_num))
self.ck_multislice_flag.setChecked(p.multislice_flag)
self.sp_slice_num.setValue(int(p.slice_num))
self.sp_slice_spacing_m.setValue(p.get_slice_spacing_m())
self.sp_amp_max.setValue(float(p.amp_max))
self.sp_amp_min.setValue(float(p.amp_min))
self.sp_pha_max.setValue(float(p.pha_max))
self.sp_pha_min.setValue(float(p.pha_min))
self.ck_gpu_flag.setChecked(p.gpu_flag)
gpu_str = ''
for i, dev_id in enumerate(p.gpus):
gpu_str += str(dev_id)
if i != len(p.gpus) - 1:
gpu_str += ', '
self.le_gpus.setText(gpu_str)
self.cb_gpu_batch_size.setCurrentIndex(p.get_gpu_batch_index())
# set MPI file path from param
if p.mpi_file_path != '':
mpi_filename = os.path.basename(p.mpi_file_path)
self.le_MPI_file_path.setText(mpi_filename)
# TODO: does this make sense?
self.le_gpus.setText('')
# adv param group
self.sp_ccd_pixel_um.setValue(p.ccd_pixel_um)
self.sp_distance.setValue(float(p.distance))
self.ck_angle_correction_flag.setChecked(p.angle_correction_flag)
self.sp_x_direction.setValue(p.x_direction)
self.sp_y_direction.setValue(p.y_direction)
self.sp_angle.setValue(p.angle)
self.sp_start_update_probe.setValue(p.start_update_probe)
self.sp_start_update_object.setValue(p.start_update_object)
self.cb_ml_mode.setCurrentText(p.ml_mode)
self.sp_ml_weight.setValue(p.ml_weight)
self.sp_dm_version.setValue(p.dm_version)
self.ck_cal_scal_pattern_flag.setChecked(p.cal_scan_pattern_flag)
self.sp_nth.setValue(p.nth)
self.sp_start_ave.setValue(p.start_ave)
self.sp_processes.setValue(p.processes)
self.ck_bragg_flag.setChecked(p.bragg_flag)
self.sp_bragg_theta.setValue(p.bragg_theta)
self.sp_bragg_gamma.setValue(p.bragg_gamma)
self.sp_bragg_delta.setValue(p.bragg_delta)
self.ck_pc_flag.setChecked(p.pc_flag)
self.sp_pc_sigma.setValue(p.pc_sigma)
self.cb_pc_alg.setCurrentText(p.pc_alg)
self.sp_pc_kernel_n.setValue(p.pc_kernel_n)
self.ck_position_correction_flag.setChecked(p.position_correction_flag)
self.sp_position_correction_start.setValue(p.position_correction_start)
self.sp_position_correction_step.setValue(p.position_correction_step)
self.sp_sigma2.setValue(p.sigma2)
self.sp_beta.setValue(p.beta)
self.sp_display_interval.setValue(p.display_interval)
self.ck_preview_flag.setChecked(p.preview_flag)
self.ck_cal_error_flag.setChecked(p.cal_error_flag)
self.ck_init_obj_dpc_flag.setChecked(p.init_obj_dpc_flag)
self.ck_prb_center_flag.setChecked(p.prb_center_flag)
self.ck_mask_prb_flag.setChecked(p.mask_prb_flag)
self.ck_mask_obj_flag.setChecked(p.mask_obj_flag)
self.ck_norm_prb_amp_flag.setChecked(p.norm_prb_amp_flag)
self.ck_weak_obj_flag.setChecked(p.weak_obj_flag)
self.ck_mesh_flag.setChecked(p.mesh_flag)
self.ck_ms_pie_flag.setChecked(p.ms_pie_flag)
self.ck_sf_flag.setChecked(p.sf_flag)
self.ck_refine_data_flag.setChecked(p.refine_data_flag)
self.sp_refine_data_start_it.setValue(p.refine_data_start_it)
self.sp_refine_data_interval.setValue(p.refine_data_interval)
self.sp_refine_data_step.setValue(p.refine_data_step)
self.ck_profiler_flag.setChecked(p.profiler_flag)
self.ck_postprocessing_flag.setChecked(p.postprocessing_flag)
self.rb_nccl.setChecked(p.use_NCCL)
self.rb_cuda_mpi.setChecked(p.use_CUDA_MPI)
# batch param group, necessary?
def start(self, batch_mode=False):
if self._ptycho_gpu_thread is not None and self._ptycho_gpu_thread.isFinished():
self._ptycho_gpu_thread = None
if self._ptycho_gpu_thread is None:
if not self._loaded:
print("[WARNING] Remember to click \"Load\" before proceeding!", file=sys.stderr)
return
self.update_param_from_gui() # this has to be done first, so all operations depending on param are correct
self.recon_bar.setValue(0)
self.recon_bar.setMaximum(self.param.n_iterations)
# at least one GPU needs to be selected
if self.param.gpu_flag and len(self.param.gpus) == 0 and self.param.mpi_file_path == '':
print("[WARNING] select at least one GPU!", file=sys.stderr)
return
# batch mode requires some additional changes to param
if batch_mode:
if self._batch_prb_filename is not None:
p = self.param
p.init_prb_flag = False
scan_num = str(self.param.scan_num)
sign = self._batch_prb_filename[1].split('probe')[0]
sign = sign.strip('_')
dirname = p.working_directory + "/recon_result/S" + scan_num + "/" + sign + "/recon_data/"
filename = scan_num.join(self._batch_prb_filename)
p.set_prb_path(dirname, filename)
print("[BATCH] will load " + dirname + filename + " as probe")
if self._batch_obj_filename is not None:
p = self.param
p.init_obj_flag = False
scan_num = str(self.param.scan_num)
sign = self._batch_obj_filename[1].split('object')[0]
sign = sign.strip('_')
dirname = p.working_directory + "/recon_result/S" + scan_num + "/" + sign + "/recon_data/"
filename = scan_num.join(self._batch_obj_filename)
p.set_obj_path(dirname, filename)
print("[BATCH] will load " + dirname + filename + " as object")
# this is needed because MPI processes need to know the working directory...
self._exportConfigHelper(self._config_path)
# init reconStepWindow
if self.ck_preview_flag.isChecked():
if self.param.mode_flag:
info = (self.param.obj_mode_num, self.param.prb_mode_num, 1)
elif self.param.multislice_flag:
info = (self.param.slice_num, 1, 1)
else:
info = (1, 1, 2)
if self.reconStepWindow is None:
self.reconStepWindow = ReconStepWindow(*info)
self.reconStepWindow.reset_window(*info, iterations=self.param.n_iterations,
slider_interval=self.param.display_interval)
self.reconStepWindow.show()
else:
if self.reconStepWindow is not None:
# TODO: maybe a thorough cleanup???
self.reconStepWindow.close()
if not _TEST:
thread = self._ptycho_gpu_thread = PtychoReconWorker(self.param, parent=self)
else:
thread = self._ptycho_gpu_thread = PtychoReconFakeWorker(self.param, parent=self)
thread.update_signal.connect(self.update_recon_step)
thread.finished.connect(self.resetButtons)
if batch_mode:
thread.finished.connect(self._batch_manager)
#thread.finished.connect(self.reconStepWindow.debug)
thread.start()
self.btn_recon_stop.setEnabled(True)
self.btn_recon_start.setEnabled(False)
# init scan window
# TODO: optimize and refactor this part
if self.ck_scan_pt_flag.isChecked():
if self.scanWindow is None:
self.scanWindow = ScanWindow()
self.scanWindow.reset_window()
self.scanWindow.show()
else:
if self.scanWindow is not None:
self.scanWindow.close()
self.scanWindow = None
return
if self._scan_points is None:
raise RuntimeError("Scan points were not read. This shouldn't happen. Abort.")
else:
self._scan_points[0] *= -1.*self.param.x_direction
self._scan_points[1] *= self.param.y_direction
# borrowed from nsls2ptycho/core/ptycho_recon.py
if self.param.mpi_file_path == '':
if self.param.gpu_flag:
num_processes = str(len(self.param.gpus))
else:
num_processes = str(self.param.processes) if self.param.processes > 1 else str(1)
else:
# regardless if GPU is used or not --- trust users to know this
num_processes = str(get_mpi_num_processes(self.param.mpi_file_path))
self.scanWindow.update_image(self._scan_points, int(num_processes))
def stop(self, batch_mode=False):
if self._ptycho_gpu_thread is not None:
if batch_mode:
self._ptycho_gpu_thread.finished.disconnect(self._batch_manager)
if self._ptycho_gpu_thread.isRunning():
self._ptycho_gpu_thread.kill() # first kill the mpi processes
self._ptycho_gpu_thread.quit() # then quit QThread gracefully
self._ptycho_gpu_thread = None
self.resetButtons()
if self.reconStepWindow is not None:
self.reconStepWindow.reset_window()
if self.scanWindow is not None:
self.scanWindow.reset_window()
def init_mmap(self):
p = self.param
datasize = 8 if p.precision == 'single' else 16
datatype = np.complex64 if p.precision == 'single' else np.complex128
global mm_list, shm_list
for i, name in enumerate(["/"+p.shm_name+"_obj_size", "/"+p.shm_name+"_prb", "/"+p.shm_name+"_obj"]):
shm_list.append(SharedMemory(name))
mm_list.append(mmap.mmap(shm_list[i].fd, shm_list[i].size))
nx_obj = int.from_bytes(mm_list[0].read(8), byteorder='big')
ny_obj = int.from_bytes(mm_list[0].read(8), byteorder='big') # the file position has been moved by 8 bytes when we get nx_obj
if p.mode_flag:
self._prb = np.ndarray(shape=(p.n_iterations, p.prb_mode_num, p.nx, p.ny), dtype=datatype, buffer=mm_list[1], order='C')
self._obj = np.ndarray(shape=(p.n_iterations, p.obj_mode_num, nx_obj, ny_obj), dtype=datatype, buffer=mm_list[2], order='C')
elif p.multislice_flag:
self._prb = np.ndarray(shape=(p.n_iterations, 1, p.nx, p.ny), dtype=datatype, buffer=mm_list[1], order='C')
self._obj = np.ndarray(shape=(p.n_iterations, p.slice_num, nx_obj, ny_obj), dtype=datatype, buffer=mm_list[2], order='C')
else:
self._prb = np.ndarray(shape=(p.n_iterations, 1, p.nx, p.ny), dtype=datatype, buffer=mm_list[1], order='C')
self._obj = np.ndarray(shape=(p.n_iterations, 1, nx_obj, ny_obj), dtype=datatype, buffer=mm_list[2], order='C')
def close_mmap(self):
# We close shared memory as long as the backend is terminated either normally or
# abnormally. The subtlety here is that the monitor should still be able to access
# the intermediate results after mmaps' are closed. A potential segfault is avoided
# by accessing the transformed results, which are buffered, not the original ones.
try:
global mm_list, shm_list
for mm, shm in zip(mm_list, shm_list):
mm.close()
shm.close_fd()
shm.unlink()
mm_list = []
shm_list = []
except NameError:
# either not using GUI, monitor is turned off, global variables are deleted or not yet created!
# need to examine the last case
try:
SharedMemory("/"+self.param.shm_name+"_obj_size").unlink()
SharedMemory("/"+self.param.shm_name+"_prb").unlink()
SharedMemory("/"+self.param.shm_name+"_obj").unlink()
except ExistentialError:
pass # nothing to clean up, we're done
def update_recon_step(self, it, data=None):
self.recon_bar.setValue(it)
if self.reconStepWindow is not None:
self.reconStepWindow.update_iter(it)
if not _TEST and self.ck_preview_flag.isChecked():
try:
if it == -1 and data == 'init_mmap':
try:
# the two npy are created by ptycho by this time
self.init_mmap()
except ExistentialError:
# user may kill the process prematurely
self.stop()
elif it == self.param.n_iterations+1:
# reserve it=n_iterations+1 as the working space
self.reconStepWindow.current_max_iters = self.param.n_iterations
p = self.param
if not p.postprocessing_flag:
return
work_dir = p.working_directory
scan_num = str(p.scan_num)
data_dir = work_dir+'/recon_result/S'+scan_num+'/'+p.sign+'/recon_data/'
data = {}
images = []
print("[SUCCESS] generated results are loaded in the preview window. ", end='', file=sys.stderr)
print("Slide to frame "+str(p.n_iterations+1)+" and select from drop-down menus.", file=sys.stderr)
if self.param.mode_flag:
# load data that has been averaged + orthonormalized + phase-ramp removed
for i in range(self.param.obj_mode_num):
data['obj_'+str(i)] = np.load(data_dir+'recon_'+scan_num+'_'+p.sign+'_' \
+'object_mode_orth_ave_rp_mode_'+str(i)+'.npy')
self.reconStepWindow.cb_image_object.addItem("Object "+str(i)+" (orth_ave_rp)")
# hard-wire the padding values here...
images.append( np.rot90(np.angle(data['obj_'+str(i)][(p.nx+30)//2:-(p.nx+30)//2, (p.ny+30)//2:-(p.ny+30)//2])) )
images.append( np.rot90(np.abs(data['obj_'+str(i)][(p.nx+30)//2:-(p.nx+30)//2, (p.ny+30)//2:-(p.ny+30)//2])) )
for i in range(self.param.prb_mode_num):
data['prb_'+str(i)] = np.load(data_dir+'recon_'+scan_num+'_'+p.sign+'_' \
+'probe_mode_orth_ave_rp_mode_'+str(i)+'.npy')
self.reconStepWindow.cb_image_probe.addItem("Probe "+str(i)+" (orth_ave_rp)")
images.append( np.rot90(np.abs(data['prb_'+str(i)])) )
images.append( np.rot90(np.angle(data['prb_'+str(i)])) )
elif self.param.multislice_flag:
# load data that has been averaged + phase-ramp removed
for i in range(self.param.slice_num):
data['obj_'+str(i)] = np.load(data_dir+'recon_'+scan_num+'_'+p.sign+'_' \
+'object_ave_rp_ms_'+str(i)+'.npy')
self.reconStepWindow.cb_image_object.addItem("Object "+str(i)+" (ave_rp)")
# hard-wire the padding values here...
images.append( np.rot90(np.angle(data['obj_'+str(i)][(p.nx+30)//2:-(p.nx+30)//2, (p.ny+30)//2:-(p.ny+30)//2])) )
images.append( np.rot90(np.abs(data['obj_'+str(i)][(p.nx+30)//2:-(p.nx+30)//2, (p.ny+30)//2:-(p.ny+30)//2])) )
for i in range(self.param.slice_num):
data['prb_'+str(i)] = np.load(data_dir+'recon_'+scan_num+'_'+p.sign+'_' \
+'probe_ave_rp_ms_'+str(i)+'.npy')
self.reconStepWindow.cb_image_probe.addItem("Probe "+str(i)+" (ave_rp)")
images.append( np.rot90(np.abs(data['prb_'+str(i)])) )
images.append( np.rot90(np.angle(data['prb_'+str(i)])) )
else:
# load data (ave & ave_rp)
for sol in ['ave', 'ave_rp']:
for tar, target in zip(['obj', 'prb'], ['object', 'probe']):
data[tar+'_'+sol] = np.load(data_dir+'recon_'+scan_num+'_'+p.sign+'_'+target+'_'+sol+'.npy')
# calculate images
for sol in ['ave', 'ave_rp']:
self.reconStepWindow.cb_image_object.addItem("Object ("+sol+")")
# hard-wire the padding values here...
images.append( np.rot90(np.angle(data['obj_'+sol][(p.nx+30)//2:-(p.nx+30)//2, (p.ny+30)//2:-(p.ny+30)//2])) )
images.append( np.rot90(np.abs(data['obj_'+sol][(p.nx+30)//2:-(p.nx+30)//2, (p.ny+30)//2:-(p.ny+30)//2])) )
for sol in ['ave', 'ave_rp']:
self.reconStepWindow.cb_image_probe.addItem("Probe ("+sol+")")
images.append( np.rot90(np.abs(data['prb_'+sol])) )
images.append( np.rot90(np.angle(data['prb_'+sol])) )
self.reconStepWindow.update_images(it, images)
elif (it-1) % self.param.display_interval == 0:
if self.param.mode_flag:
images = []
for i in range(self.param.obj_mode_num):
images.append(np.rot90(np.angle(self._obj[it-1, i])))
images.append(np.rot90(np.abs(self._obj[it-1, i])))
for i in range(self.param.prb_mode_num):
images.append(np.rot90(np.abs(self._prb[it-1, i])))
images.append(np.rot90(np.angle(self._prb[it-1, i])))
elif self.param.multislice_flag:
images = []
for i in range(self.param.slice_num):
images.append(np.rot90(np.angle(self._obj[it-1, i])))
images.append(np.rot90(np.abs(self._obj[it-1, i])))
#TODO: decide which probe we'd like to present
images.append(np.rot90(np.abs(self._prb[it-1, 0])))
images.append(np.rot90(np.angle(self._prb[it-1, 0])))
else:
images = [np.rot90(np.angle(self._obj[it-1, 0])),
np.rot90(np.abs(self._obj[it-1, 0] )),
np.rot90(np.abs(self._prb[it-1, 0] )),
np.rot90(np.angle(self._prb[it-1, 0]))]
self.reconStepWindow.update_images(it, images)
self.reconStepWindow.update_metric(it, data)
except TypeError as ex: # when MPI processes are terminated, _prb and _obj are deleted and so not subscriptable
pass
else:
# -------------------- Sungsoo version -------------------------------------
# a list of random images for test
# in the order of [object_amplitude, object_phase, probe_amplitude, probe_phase]
images = [np.random.random((128,128)) for _ in range(4)]
self.reconStepWindow.update_images(it, images)
self.reconStepWindow.update_metric(it, data)
def loadProbe(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open probe file', directory=self.param.working_directory, filter="(*.npy)")
if filename is not None and len(filename) > 0:
prb_filename = os.path.basename(filename)
prb_dir = filename[:(len(filename)-len(prb_filename))]
self.param.set_prb_path(prb_dir, prb_filename)
self.le_prb_path.setText(prb_filename)
self.ck_init_prb_flag.setChecked(False)
def resetProbeFlg(self):
# called when "estimate from data" is clicked
self.param.set_prb_path('', '')
self.le_prb_path.setText('')
self.ck_init_prb_flag.setChecked(True)
def loadObject(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open object file', directory=self.param.working_directory, filter="(*.npy)")
if filename is not None and len(filename) > 0:
obj_filename = os.path.basename(filename)
obj_dir = filename[:(len(filename)-len(obj_filename))]
self.param.set_obj_path(obj_dir, obj_filename)
self.le_obj_path.setText(obj_filename)
self.ck_init_obj_flag.setChecked(False)
def resetObjectFlg(self):
# called when "random start" is clicked
self.param.set_obj_path('', '')
self.le_obj_path.setText('')
self.ck_init_obj_flag.setChecked(True)
def setWorkingDirectory(self):
dirname = QFileDialog.getExistingDirectory(self, 'Choose working folder', directory=os.path.expanduser("~"))
if dirname is not None and len(dirname) > 0:
dirname = dirname + "/"
self.param.set_working_directory(dirname)
self.le_working_directory.setText(dirname)
def updateExtraScansFlg(self):
self.btn_set_extra_scans.setEnabled(self.ck_extra_scans_flag.isChecked())
def setExtraScans(self):
if self._extra_scans_dialog is None:
self._extra_scans_dialog = ListWidget()
self._extra_scans_dialog.setWindowTitle('Set associated scan numbers')
self._extra_scans_dialog.show()
def modeMultiSliceGuard(self):
'''
Currently our ptycho code does not support simultaneous mode + multi-slice reconstruction.
This function can be removed once the support is added.
'''
if self.ck_mode_flag.isChecked() and self.ck_multislice_flag.isChecked():
message = "Currently our ptycho code does not support simultaneous multi-mode + multi-slice reconstruction."
print("[WARNING] " + message, file=sys.stderr)
QtWidgets.QMessageBox.warning(self, "Warning", message)
self.ck_mode_flag.setChecked(False)
self.ck_multislice_flag.setChecked(False)
self.updateModeFlg()
self.updateMultiSliceFlg()
def updateModeFlg(self):
mode_flag = self.ck_mode_flag.isChecked()
self.sp_prb_mode_num.setEnabled(mode_flag)
self.sp_obj_mode_num.setEnabled(mode_flag)
self.param.mode_flag = mode_flag
def updateMultiSliceFlg(self):
flag = self.ck_multislice_flag.isChecked()
self.sp_slice_num.setEnabled(flag)
self.sp_slice_spacing_m.setEnabled(flag)
self.param.multislice_flag = flag
def updateObjMaskFlg(self):
mask_flag = self.ck_mask_obj_flag.isChecked()
self.sp_amp_min.setEnabled(mask_flag)
self.sp_amp_max.setEnabled(mask_flag)
self.sp_pha_min.setEnabled(mask_flag)
self.sp_pha_max.setEnabled(mask_flag)
self.param.mask_obj_flag = mask_flag
def checkGpuAvail(self):
try:
import cupy
except ImportError:
print('[!] Unable to import CuPy. GPU reconstruction is disabled.')
print('[!] (Either CuPy is not installed, or GPU is not available.)')
self.ck_gpu_flag.setChecked(False)
self.ck_gpu_flag.setEnabled(False)
self.param.gpu_flag = False
self.le_gpus.setText('')
self.le_gpus.setEnabled(False)
self.cb_gpu_batch_size.setEnabled(False)
else:
del cupy
def updateGpuFlg(self):
flag = self.ck_gpu_flag.isChecked()
self.le_gpus.setEnabled(flag)
self.rb_nccl.setEnabled(flag)
if not flag and self.rb_nccl.isChecked():
self.rb_mpi.setChecked(True)
def updateBraggFlg(self):
flag = self.ck_bragg_flag.isChecked()
self.sp_bragg_theta.setEnabled(flag)
self.sp_bragg_gamma.setEnabled(flag)
self.sp_bragg_delta.setEnabled(flag)
self.param.bragg_flag = flag
def updatePcFlg(self):
flag = self.ck_pc_flag.isChecked()
self.sp_pc_sigma.setEnabled(flag)
self.sp_pc_kernel_n.setEnabled(flag)
self.cb_pc_alg.setEnabled(flag)
self.param.pc_flag = flag
def updateCorrFlg(self):
flag = self.ck_position_correction_flag.isChecked()
self.sp_position_correction_start.setEnabled(flag)
self.sp_position_correction_step.setEnabled(flag)
self.param.position_correction_flag = flag
def updateRefineDataFlg(self):
flag = self.ck_refine_data_flag.isChecked()
self.sp_refine_data_start_it.setEnabled(flag)
self.sp_refine_data_interval.setEnabled(flag)
self.sp_refine_data_step.setEnabled(flag)
self.param.refine_data_flag = flag
def updateBatchCropDataFlg(self):
if self.cb_dataloader.currentText() != "Load from databroker":
flag = False
self.ck_batch_crop_flag.setChecked(flag)
self.ck_batch_crop_flag.setEnabled(flag)
else:
flag = self.ck_batch_crop_flag.isChecked()
self.ck_batch_crop_flag.setEnabled(True)
self.sp_batch_x0.setEnabled(flag)
self.sp_batch_y0.setEnabled(flag)
self.sp_batch_width.setEnabled(flag)
self.sp_batch_height.setEnabled(flag)
def showNoPostProcessingWarning(self):
if not self.ck_postprocessing_flag.isChecked():
print("[WARNING] Post-processing is turned off. No result will be written to disk!", file=sys.stderr)
def clearSharedMemory(self):
message = "Are you sure you want to clear the shared memory segments currently left in /dev/shm? "\
"The safest way to do so is to ensure you have only one window (that is, this one) opened on this machine."
ans = QtWidgets.QMessageBox.question(self, "Warning", message, QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
if ans == QtWidgets.QMessageBox.Yes:
clean_shared_memory()
def setMPIfile(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open MPI machine file', directory=self.param.working_directory)
if filename is not None and len(filename) > 0:
mpi_filename = os.path.basename(filename)
mpi_dir = filename[:(len(filename)-len(mpi_filename))]
#self.param.le_MPI_file_path(mpi_dir, mpi_filename)
self.param.mpi_file_path = filename
#print(filename)
self.le_MPI_file_path.setText(mpi_filename)
self.le_gpus.setText('')
def resetMPIFlg(self):
# called when any gpu button is clicked
self.param.mpi_file_path = ''
self.le_MPI_file_path.setText('')
def batchStart(self):
if not self.ck_batch_crop_flag.isChecked() and not self.ck_batch_run_flag.isChecked():
print("[WARNING] Choose least one action (Crop or Run). Stop.", file=sys.stderr)
return
if self.cb_dataloader.currentText() == "Load from databroker":
if not self.ck_batch_crop_flag.isChecked():
print("[WARNING] Batch mode with databroker is set, but \"Crop data\" is not.\n"
"[WARNING] Will attempt to load h5 from working directory", file=sys.stderr)
try:
self._scan_numbers = parse_range(self.le_batch_items.text(), self.sp_batch_step.value())
print(self._scan_numbers)
# TODO: is there a way to lock all widgets to prevent accidental parameter changes in the middle?
# fire up
self.le_scan_num.textChanged.disconnect(self.forceLoad)
if self.ck_init_prb_batch_flag.isChecked():
filename = self.le_prb_path_batch.text()
self._batch_prb_filename = filename.split("*")
if self.ck_init_obj_batch_flag.isChecked():
filename = self.le_obj_path_batch.text()
self._batch_obj_filename = filename.split("*")
self._batch_manager() # serve as linked list's head
except Exception as ex:
self.exception_handler(ex)
def batchStop(self):
'''
Brute-force abortion of the entire batch. No resumption is possible.
'''
self._scan_numbers = None
self.le_scan_num.textChanged.connect(self.forceLoad)
self.stop(True)
if self.roiWindow is not None:
if self.roiWindow._worker_thread is not None:
self.roiWindow._worker_thread.disconnect()
## thread.terminate() freezes the whole GUI -- why?
#if self.roiWindow._worker_thread.isRunning():
# self.roiWindow._worker_thread.terminate()
# self.roiWindow._worker_thread.wait()
self.roiWindow._worker_thread = None
self.roiWindow = None
self.resetButtons()
def _batch_manager(self):
'''
This is a "linked list" that utilizes Qt's signal mechanism to retrieve the next item in the list
when the current item is processed. We need this because most likely the users want to put all
available computing resources to process the batch item by item, and having more than one worker
is not helping.
'''