-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_peakindexing.py
More file actions
1238 lines (1112 loc) · 58 KB
/
Copy pathcreate_peakindexing.py
File metadata and controls
1238 lines (1112 loc) · 58 KB
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 datetime
import glob
import logging
import os
import urllib.parse
import dash
import dash_bootstrap_components as dbc
from dash import html, dcc, Input, Output, set_props, State
from dash.exceptions import PreventUpdate
from sqlalchemy.orm import Session
import laue_portal.database.db_utils as db_utils
import laue_portal.database.db_schema as db_schema
import laue_portal.components.navbar as navbar
from laue_portal.database.db_utils import get_catalog_data, remove_root_path_prefix, parse_parameter
from laue_portal.components.peakindex_form import peakindex_form, set_peakindex_form_props
from laue_portal.processing.redis_utils import enqueue_peakindexing, STATUS_REVERSE_MAPPING
from laue_portal.config import DEFAULT_VARIABLES
from srange import srange
import laue_portal.database.session_utils as session_utils
import re
from difflib import SequenceMatcher
from itertools import combinations
logger = logging.getLogger(__name__)
JOB_DEFAULTS = {
"computer_name": 'example_computer',
"status": 0, #pending, running, finished, stopped
"priority": 0,
"submit_time": datetime.datetime.now(),
"start_time": datetime.datetime.now(),
"finish_time": datetime.datetime.now(),
}
PEAKINDEX_DEFAULTS = {
"scanNumber": 276994,
# "peakProgram": "peaksearch",
"threshold": "", #250
"thresholdRatio": "",
"maxRfactor": 0.5, #0.5
"boxsize": 18, #18
"max_number": 300,
"min_separation": 20, #40
"peakShape": "L", #"Lorentzian"
# "scanPointStart": 1,
# "scanPointEnd": 2,
"scanPoints": "",#"1-2", # String field for srange parsing
"depthRange": "", # Empty string for no depth range
"detectorCropX1": 0,
"detectorCropX2": 2047,
"detectorCropY1": 0,
"detectorCropY2": 2047,
"min_size": 3, #1.13
"max_peaks": 50,
"smooth": False, #0
"maskFile": "None",
"indexKeVmaxCalc": 17.2, #17.2
"indexKeVmaxTest": 35.0, #30.0
"indexAngleTolerance": 0.1, #0.1
"indexH": 0, #1
"indexK": 0, #1
"indexL": 1,
"indexCone": 72.0,
"energyUnit": "keV",
"exposureUnit": "sec",
"cosmicFilter": True, # Assuming the last occurrence in YAML is the one to use
"recipLatticeUnit": "1/nm",
"latticeParametersUnit": "nm",
# "peaksearchPath": None,
# "p2qPath": None,
# "indexingPath": None,
"outputFolder": "analysis/scan_%d/index_%d",# "analysis/scan_%d/rec_%d/index_%d",
# "filefolder": "tests/data/gdata",
# "filenamePrefix": "HAs_long_laue1_",
"geoFile": "tests/data/geo/geoN_2022-03-29_14-15-05.xml",
"crystFile": "tests/data/crystal/Al.xtal",
"depth": float('nan'), # Representing YAML nan
"beamline": "34ID-E"
}
CATALOG_DEFAULTS = {
"filefolder": "tests/data/gdata",
"filenamePrefix": "HAs_long_laue1_",
}
# DEFAULT_VARIABLES = {
# "author": "",
# "notes": "",
# }
dash.register_page(__name__)
layout = dbc.Container(
[html.Div([
navbar.navbar,
dcc.Location(id='url-create-peakindexing', refresh=False),
dbc.Alert(
"Hello! I am an alert",
id="alert-upload",
dismissable=True,
is_open=False,
),
dbc.Alert(
"Hello! I am an alert",
id="alert-submit",
dismissable=True,
is_open=False,
),
dbc.Alert(
"Scan data loaded successfully",
id="alert-scan-loaded",
dismissable=True,
is_open=False,
color="success",
),
html.Hr(),
dbc.Row(
[
dbc.Col(
html.H3(id="peakindex-title", children="New peak indexing"),
width="auto", # shrink to content
),
dbc.Col(
dbc.Button(
"Set from ...",
id="upload-peakindexing-config",
color="secondary",
style={"minWidth": 150, "maxWidth": "150px", "width": "100%"},
),
width="auto",
className="ms-3", # small gap from title
),
dbc.Col(
dbc.Button(
"Validate",
id="validate-btn",
color="secondary",
style={"minWidth": 150, "maxWidth": "150px", "width": "100%"},
),
width="auto",
className="ms-3", # small gap from title
),
dbc.Col(
dbc.Button(
"Submit",
id="submit_peakindexing",
color="primary",
style={"minWidth": 150, "maxWidth": "150px", "width": "100%"},
),
width="auto",
className="ms-2",
),
],
className="g-2", # gutter between cols
justify="center", # CENTER horizontally
align="center", # CENTER vertically
),
html.Hr(),
dbc.Row([
dbc.Col(
dbc.Alert([
html.H4("Validate status: Warning !", className="alert-heading"),
html.Hr(),
html.P("Press button Validate! or Some parameter (which one) seems to be too big! Better check it! ", className="mb-0"),
],
color="warning", # <- set the color here
),
),
],
className="g-0", # g-0 removes row gutters
align="center",
),
######## Below are other examples of the Alert
# this is just example but it should be changable to something like that if Error
dbc.Row([
dbc.Col(
dbc.Alert([
html.H4("Validate status: Error !", className="alert-heading"),
html.Hr(),
html.P("Check your file inputs! or check Peak Search parameters! or Check Index Parameters!", className="mb-0"),
],
color="danger", # <- set the color here
),
),
],
className="g-0", # g-0 removes row gutters
align="center",
),
# this is just example but it should be changable to something like that if Success
dbc.Row([
dbc.Col(
dbc.Alert([
html.H4("Validate status: Success !", className="alert-heading"),
html.Hr(),
html.P("You can Submit!", className="mb-0"),
],
color="success", # <- set the color here
),
),
],
className="g-0", # g-0 removes row gutters
align="center",
),
html.Hr(),
dbc.Row([
dbc.Col(
dbc.InputGroup([
dbc.InputGroupText("Author"),
dbc.Input(
type="text",
id="author-input",
placeholder="Required! Enter author or Tag for the reconstruction",
),
],
className="w-100 mb-0",
),
md=12, xs=12, # full row on small, wide on medium+
width = "auto",
style={"minWidth": "300px"},
),
],
justify="center",
className="mb-3",
align="center",
),
peakindex_form,
# dcc.Store(id="next-peakindex-id"),
dcc.Store(id="peakindex-data-loaded-trigger"),
],
)
],
className='dbc',
fluid=True
)
"""
=======================
Callbacks
=======================
"""
@dash.callback(
Input('submit_peakindexing', 'n_clicks'),
State('scanNumber', 'value'),
State('author', 'value'),
State('notes', 'value'),
State('recon_id', 'value'),
State('wirerecon_id', 'value'),
# State('peakProgram', 'value'),
State('threshold', 'value'),
State('thresholdRatio', 'value'),
State('maxRfactor', 'value'),
State('boxsize', 'value'),
State('max_number', 'value'),
State('min_separation', 'value'),
State('peakShape', 'value'),
# State('scanPointStart', 'value'),
# State('scanPointEnd', 'value'),
# State('depthRangeStart', 'value'),
# State('depthRangeEnd', 'value'),
State('scanPoints', 'value'),
State('depthRange', 'value'),
State('detectorCropX1', 'value'),
State('detectorCropX2', 'value'),
State('detectorCropY1', 'value'),
State('detectorCropY2', 'value'),
State('min_size', 'value'),
State('max_peaks', 'value'),
State('smooth', 'value'),
State('maskFile', 'value'),
State('indexKeVmaxCalc', 'value'),
State('indexKeVmaxTest', 'value'),
State('indexAngleTolerance', 'value'),
State('indexHKL', 'value'),
# State('indexH', 'value'),
# State('indexK', 'value'),
# State('indexL', 'value'),
State('indexCone', 'value'),
State('energyUnit', 'value'),
State('exposureUnit', 'value'),
State('cosmicFilter', 'value'),
State('recipLatticeUnit', 'value'),
State('latticeParametersUnit', 'value'),
# State('peaksearchPath', 'value'),
# State('p2qPath', 'value'),
# State('indexingPath', 'value'),
State('data_path', 'value'),
# State('filefolder', 'value'),
State('filenamePrefix', 'value'),
State('outputFolder', 'value'),
State('geoFile', 'value'),
State('crystFile', 'value'),
State('depth', 'value'),
State('beamline', 'value'),
prevent_initial_call=True,
)
def submit_parameters(n,
scanNumber,
author,
notes,
recon_id,
wirerecon_id,
# peakProgram,
threshold,
thresholdRatio,
maxRfactor,
boxsize,
max_number,
min_separation,
peakShape,
# scanPointStart,
# scanPointEnd,
# depthRangeStart,
# depthRangeEnd,
scanPoints,
depthRange,
detectorCropX1,
detectorCropX2,
detectorCropY1,
detectorCropY2,
min_size,
max_peaks,
smooth,
maskFile,
indexKeVmaxCalc,
indexKeVmaxTest,
indexAngleTolerance,
indexHKL,
# indexH,
# indexK,
# indexL,
indexCone,
energyUnit,
exposureUnit,
cosmicFilter,
recipLatticeUnit,
latticeParametersUnit,
# peaksearchPath,
# p2qPath,
# indexingPath,
data_path,
# filefolder,
filenamePrefix,
outputFolder,
geometry_file,
crystal_file,
depth,
beamline,
):
# TODO: Input validation and response
"""
Submit parameters for peak indexing job(s).
Handles both single scan and pooled scan submissions.
"""
# Parse data_path first to get the number of inputs
data_path_list = parse_parameter(data_path)
num_inputs = len(data_path_list)
# Parse all other parameters with num_inputs
try:
scanNumber_list = parse_parameter(scanNumber, num_inputs)
author_list = parse_parameter(author, num_inputs)
notes_list = parse_parameter(notes, num_inputs)
recon_id_list = parse_parameter(recon_id, num_inputs)
wirerecon_id_list = parse_parameter(wirerecon_id, num_inputs)
threshold_list = parse_parameter(threshold, num_inputs)
thresholdRatio_list = parse_parameter(thresholdRatio, num_inputs)
maxRfactor_list = parse_parameter(maxRfactor, num_inputs)
boxsize_list = parse_parameter(boxsize, num_inputs)
max_number_list = parse_parameter(max_number, num_inputs)
min_separation_list = parse_parameter(min_separation, num_inputs)
peakShape_list = parse_parameter(peakShape, num_inputs)
scanPoints_list = parse_parameter(scanPoints, num_inputs)
depthRange_list = parse_parameter(depthRange, num_inputs)
detectorCropX1_list = parse_parameter(detectorCropX1, num_inputs)
detectorCropX2_list = parse_parameter(detectorCropX2, num_inputs)
detectorCropY1_list = parse_parameter(detectorCropY1, num_inputs)
detectorCropY2_list = parse_parameter(detectorCropY2, num_inputs)
min_size_list = parse_parameter(min_size, num_inputs)
max_peaks_list = parse_parameter(max_peaks, num_inputs)
smooth_list = parse_parameter(smooth, num_inputs)
maskFile_list = parse_parameter(maskFile, num_inputs)
indexKeVmaxCalc_list = parse_parameter(indexKeVmaxCalc, num_inputs)
indexKeVmaxTest_list = parse_parameter(indexKeVmaxTest, num_inputs)
indexAngleTolerance_list = parse_parameter(indexAngleTolerance, num_inputs)
indexHKL_list = parse_parameter(indexHKL, num_inputs)
indexCone_list = parse_parameter(indexCone, num_inputs)
energyUnit_list = parse_parameter(energyUnit, num_inputs)
exposureUnit_list = parse_parameter(exposureUnit, num_inputs)
cosmicFilter_list = parse_parameter(cosmicFilter, num_inputs)
recipLatticeUnit_list = parse_parameter(recipLatticeUnit, num_inputs)
latticeParametersUnit_list = parse_parameter(latticeParametersUnit, num_inputs)
data_path_list = parse_parameter(data_path, num_inputs)
filenamePrefix_list = parse_parameter(filenamePrefix, num_inputs)
outputFolder_list = parse_parameter(outputFolder, num_inputs)
geoFile_list = parse_parameter(geometry_file, num_inputs)
crystFile_list = parse_parameter(crystal_file, num_inputs)
depth_list = parse_parameter(depth, num_inputs)
beamline_list = parse_parameter(beamline, num_inputs)
except ValueError as e:
# Error: mismatched lengths
set_props("alert-submit", {
'is_open': True,
'children': str(e),
'color': 'danger'
})
return
root_path = DEFAULT_VARIABLES["root_path"]
peakindexes_to_enqueue = []
# First loop: Create all database entries for each listed scanNumber
with Session(session_utils.get_engine()) as session:
try:
for i in range(num_inputs):
# Extract values for this scan
current_scanNumber = scanNumber_list[i]
current_recon_id = recon_id_list[i]
current_wirerecon_id = wirerecon_id_list[i]
current_output_folder = outputFolder_list[i]
current_geo_file = geoFile_list[i]
current_crystal_file = crystFile_list[i]
current_scanPoints = scanPoints_list[i]
current_depthRange = depthRange_list[i]
# Convert relative paths to full paths
full_geometry_file = os.path.join(root_path, current_geo_file.lstrip('/'))
full_crystal_file = os.path.join(root_path, current_crystal_file.lstrip('/'))
next_peakindex_id = db_utils.get_next_id(session, db_schema.PeakIndex)
# Now that we have the ID, format the output folder path
try:
if '%d' in current_output_folder:
if current_wirerecon_id:
formatted_output_folder = current_output_folder % (current_scanNumber, current_wirerecon_id, next_peakindex_id)
elif current_recon_id:
formatted_output_folder = current_output_folder % (current_scanNumber, current_recon_id, next_peakindex_id)
else:
formatted_output_folder = current_output_folder % (current_scanNumber, next_peakindex_id)
else:
formatted_output_folder = current_output_folder
except TypeError:
formatted_output_folder = current_output_folder # Fallback if formatting fails
full_output_folder = os.path.join(root_path, formatted_output_folder.lstrip('/'))
# Create output directory if it doesn't exist
try:
os.makedirs(full_output_folder, exist_ok=True)
logger.info(f"Output directory: {full_output_folder}")
except Exception as e:
logger.error(f"Failed to create output directory {full_output_folder}: {e}")
set_props("alert-submit", {'is_open': True,
'children': f'Failed to create output directory: {str(e)}',
'color': 'danger'})
continue
JOB_DEFAULTS.update({'submit_time':datetime.datetime.now()})
JOB_DEFAULTS.update({'start_time':datetime.datetime.now()})
JOB_DEFAULTS.update({'finish_time':datetime.datetime.now()})
job = db_schema.Job(
computer_name=JOB_DEFAULTS['computer_name'],
status=JOB_DEFAULTS['status'],
priority=JOB_DEFAULTS['priority'],
submit_time=JOB_DEFAULTS['submit_time'],
start_time=JOB_DEFAULTS['start_time'],
finish_time=JOB_DEFAULTS['finish_time'],
)
session.add(job)
session.flush() # Get job_id without committing
job_id = job.job_id
# Create subjobs for parallel processing
# Parse scanPoints using srange
scanPoints_srange = srange(current_scanPoints)
scanPoint_nums = scanPoints_srange.list()
# Parse depthRange if provided using srange
if current_depthRange and current_depthRange.strip():
depthRange_srange = srange(current_depthRange)
depthRange_nums = depthRange_srange.list()
else:
depthRange_srange = srange('')
depthRange_nums = [None] # No reconstruction indices
# Create subjobs for each combination of scan point and depth
subjob_count = 0
for scanPoint_num in scanPoint_nums:
for depthRange_num in depthRange_nums:
subjob = db_schema.SubJob(
job_id=job_id,
computer_name=JOB_DEFAULTS['computer_name'],
status=STATUS_REVERSE_MAPPING["Queued"],
priority=JOB_DEFAULTS['priority']
)
session.add(subjob)
subjob_count += 1
# Extract HKL values from indexHKL parameter
current_indexHKL = str(indexHKL_list[i])
# Get filefolder and filenamePrefix
current_data_path = data_path_list[i]
current_filename_prefix_str = filenamePrefix_list[i]
current_filename_prefix = [s.strip() for s in current_filename_prefix_str.split(',')] if current_filename_prefix_str else []
# Build full path
current_full_data_path=os.path.join(root_path, current_data_path.lstrip('/'))
peakindex = db_schema.PeakIndex(
scanNumber = current_scanNumber,
job_id = job_id,
author = author_list[i],
notes = notes_list[i],
recon_id = current_recon_id,
wirerecon_id = current_wirerecon_id,
filefolder=current_full_data_path,
filenamePrefix=current_filename_prefix,
# peakProgram=peakProgram,
threshold=threshold_list[i],
thresholdRatio=thresholdRatio_list[i],
maxRfactor=maxRfactor_list[i],
boxsize=boxsize_list[i],
max_number=max_number_list[i],
min_separation=min_separation_list[i],
peakShape=peakShape_list[i],
# scanPointStart=scanPointStart,
# scanPointEnd=scanPointEnd,
# depthRangeStart=depthRangeStart,
# depthRangeEnd=depthRangeEnd,
scanPoints=current_scanPoints,
scanPointslen=scanPoints_srange.len(),
depthRange=current_depthRange,
depthRangelen=depthRange_srange.len(),
detectorCropX1=detectorCropX1_list[i],
detectorCropX2=detectorCropX2_list[i],
detectorCropY1=detectorCropY1_list[i],
detectorCropY2=detectorCropY2_list[i],
min_size=min_size_list[i],
max_peaks=max_peaks_list[i],
smooth=smooth_list[i],
maskFile=maskFile_list[i],
indexKeVmaxCalc=indexKeVmaxCalc_list[i],
indexKeVmaxTest=indexKeVmaxTest_list[i],
indexAngleTolerance=indexAngleTolerance_list[i],
indexH=int(current_indexHKL[0]),
indexK=int(current_indexHKL[1]),
indexL=int(current_indexHKL[2]),
indexCone=indexCone_list[i],
energyUnit=energyUnit_list[i],
exposureUnit=exposureUnit_list[i],
cosmicFilter=cosmicFilter_list[i],
recipLatticeUnit=recipLatticeUnit_list[i],
latticeParametersUnit=latticeParametersUnit_list[i],
# peaksearchPath=peaksearchPath,
# p2qPath=p2qPath,
# indexingPath=indexingPath,
outputFolder=full_output_folder, # Store full path in database
geoFile=full_geometry_file, # Store full path in database
crystFile=full_crystal_file, # Store full path in database
depth=depth_list[i],
beamline=beamline_list[i],
)
session.add(peakindex)
peakindexes_to_enqueue.append({
"job_id": job_id,
"scanNumber": current_scanNumber,
"filefolder": current_full_data_path,
"filenamePrefix": current_filename_prefix,
"outputFolder": full_output_folder,
"geoFile": full_geometry_file,
"crystFile": full_crystal_file,
"scanPoints": current_scanPoints,
"depthRange": current_depthRange,
"boxsize": boxsize_list[i],
"maxRfactor": maxRfactor_list[i],
"min_size": min_size_list[i],
"min_separation": min_separation_list[i],
"threshold": threshold_list[i],
"peakShape": peakShape_list[i],
"max_peaks": max_peaks_list[i],
"smooth": smooth_list[i],
"indexKeVmaxCalc": indexKeVmaxCalc_list[i],
"indexKeVmaxTest": indexKeVmaxTest_list[i],
"indexAngleTolerance": indexAngleTolerance_list[i],
"indexCone": indexCone_list[i],
"indexH": int(current_indexHKL[0]),
"indexK": int(current_indexHKL[1]),
"indexL": int(current_indexHKL[2]),
})
session.commit()
set_props("alert-submit", {'is_open': True,
'children': 'Entries Added to Database',
'color': 'success'})
except Exception as e:
session.rollback()
logger.error(f"Failed to create database entries: {e}")
set_props("alert-submit", {'is_open': True,
'children': f'Failed to create database entries: {str(e)}',
'color': 'danger'})
return
# Second loop: Enqueue jobs to Redis
for i, spec in enumerate(peakindexes_to_enqueue):
try:
# Extract values for this scan
full_data_path = spec["filefolder"]
current_filename_prefix = spec["filenamePrefix"]
scanPoints_srange = srange(spec["scanPoints"])
scanPoint_nums = scanPoints_srange.list()
if spec["depthRange"] and str(spec["depthRange"]).strip():
depthRange_srange = srange(spec["depthRange"])
depthRange_nums = depthRange_srange.list()
else:
depthRange_nums = [None]
# Prepare lists of input and output files for all subjobs
input_files = []
for current_filename_prefix_i in current_filename_prefix:
for scanPoint_num in scanPoint_nums:
for depthRange_num in depthRange_nums:
# Apply %d formatting with scanPoint_num if prefix contains %d placeholder
file_str = current_filename_prefix_i % scanPoint_num if '%d' in current_filename_prefix_i else current_filename_prefix_i
# Add depth index if processing reconstruction data
if depthRange_num is not None:
file_str += f"_{depthRange_num}"
input_file_pattern = os.path.join(full_data_path, file_str)
# Use glob to find matching files
matched_files = glob.glob(input_file_pattern)
if not matched_files:
raise ValueError(f"No files found matching pattern: {input_file_pattern}")
input_files.extend(matched_files)
# Create output directory list matching input_files length
output_dirs = [spec["outputFolder"] for _ in input_files]
# Enqueue the batch job with all files
rq_job_id = enqueue_peakindexing(
job_id=spec["job_id"],
input_files=input_files,
output_files=output_dirs,
geometry_file=spec["geoFile"],
crystal_file=spec["crystFile"],
boxsize=spec["boxsize"],
max_rfactor=spec["maxRfactor"],
min_size=spec["min_size"],
min_separation=spec["min_separation"],
threshold=spec["threshold"],
peak_shape=spec["peakShape"],
max_peaks=spec["max_peaks"],
smooth=spec["smooth"],
index_kev_max_calc=spec["indexKeVmaxCalc"],
index_kev_max_test=spec["indexKeVmaxTest"],
index_angle_tolerance=spec["indexAngleTolerance"],
index_cone=spec["indexCone"],
index_h=spec["indexH"],
index_k=spec["indexK"],
index_l=spec["indexL"]
)
logger.info(f"Peakindexing batch job {spec['job_id']} enqueued with RQ ID: {rq_job_id} for {len(input_files)} files")
set_props("alert-submit", {'is_open': True,
'children': f'Job {spec["job_id"]} submitted to queue with {len(input_files)} file(s)',
'color': 'info'})
except Exception as e:
# Provide more context to help diagnose format issues and input parsing
logger.error(
f"Failed to enqueue job {spec['job_id']}: {e}. "
f"filenamePrefix='{current_filename_prefix_str}', "
f"parsed prefixes={current_filename_prefix}, "
f"scanPoints='{spec['scanPoints']}', depthRange='{spec['depthRange']}', data_path='{current_data_path}'"
)
set_props("alert-submit", {'is_open': True,
'children': f'Failed to queue job {spec["job_id"]}: {str(e)}. '
f'filenamePrefix={current_filename_prefix_str}, '
f'scanPoints={spec["scanPoints"]}, depthRange={spec["depthRange"]}',
'color': 'danger'})
@dash.callback(
Output('peakindex-filename-templates', 'children', allow_duplicate=True),
Input('peakindex-check-filenames-btn', 'n_clicks'),
Input('peakindex-update-path-fields-btn', 'n_clicks'),
Input('peakindex-data-loaded-trigger', 'data'),
# Files
State('data_path', 'value'),
prevent_initial_call=True,
)
def check_filenames(n_check, n_update, data_loaded_trigger,
# Files
data_path,
num_indices=2,
):
"""
Scan directory and suggest common filename patterns.
Replaces numeric sequences with %d to find templates and shows index ranges.
For peak indexing, this captures TWO rightmost numbers:
- First number: scanPoint (e.g., 7 in file_7_150.h5)
- Second number: depthRange (e.g., 150 in file_7_150.h5)
Parameters:
-----------
n : int
Number of clicks (callback trigger)
data_path : str
Path to the data directory
num_indices : int, optional
Number of rightmost numeric indices to capture (default=2 for peak indexing)
"""
if not data_path:
return [html.Option(value="", label="No data path provided")]
# Parse data_path first to get the number of inputs
data_path_list = parse_parameter(data_path)
num_inputs = len(data_path_list)
root_path = DEFAULT_VARIABLES["root_path"]
# Dictionary to store pattern -> list of indices
pattern_files = {}
for i in range(num_inputs):
# Get filefolder
current_data_path = data_path_list[i]
# Build full path
current_full_data_path=os.path.join(root_path, current_data_path.lstrip('/'))
# Check if directory exists
if not os.path.exists(current_full_data_path):
logger.warning(f"Directory does not exist: {current_full_data_path}")
continue
# List all files in directory
try:
files = [f for f in os.listdir(current_full_data_path) if os.path.isfile(os.path.join(current_full_data_path, f))]
except Exception as e:
logger.error(f"Error reading directory {current_full_data_path}: {e}")
continue
# Extract patterns and indices
for filename in files:
base_name, extension = os.path.splitext(filename)
# Build regex pattern to capture N rightmost numbers
# For num_indices=1: (\d+)(?!.*\d)
# For num_indices=2: (\d+)_(\d+)(?!.*\d)
if num_indices == 1:
regex_pattern = r'(\d+)(?!.*\d)'
else:
# Capture N groups of digits separated by underscores from the right
regex_pattern = r'_'.join([r'(\d+)'] * num_indices) + r'(?!.*\d)'
match = re.search(regex_pattern, base_name)
if match:
# Extract all captured groups as integers
indices = [int(match.group(i)) for i in range(1, num_indices + 1)]
# Create pattern with appropriate number of %d placeholders
pattern_placeholder = '_'.join(['%d'] * num_indices)
pattern = base_name[:match.start()] + pattern_placeholder + base_name[match.end():] + extension
pattern_files.setdefault(pattern, []).append(indices)
else:
# No numeric pattern found
pattern_files.setdefault(filename, []).append([])
if not pattern_files:
return [html.Option(value="", label="No files found in specified path(s)")]
# Sort by file count and create options for top 10 patterns
sorted_patterns = sorted(pattern_files.items(), key=lambda x: len(x[1]), reverse=True)[:10]
pattern_options = []
for pattern, indices_list in sorted_patterns:
if indices_list and indices_list[0]:
if num_indices == 1:
# Single index: show simple range
label = f"{pattern} (files {str(srange(sorted(set(idx[0] for idx in indices_list))))})"
else:
# Multiple indices: show ranges for each dimension
range_labels = []
dim_names = ['scanPoints', 'depths'] if num_indices == 2 else [f"dim{i+1}" for i in range(num_indices)]
for dim in range(num_indices):
dim_values = sorted(set(idx[dim] for idx in indices_list if len(idx) > dim))
if dim_values:
range_labels.append(f"{dim_names[dim]}: {str(srange(dim_values))}")
if range_labels:
label = f"{pattern} ({', '.join(range_labels)})"
else:
label = f"{pattern} ({len(indices_list)} file{'s' if len(indices_list) != 1 else ''})"
else:
label = f"{pattern} ({len(indices_list)} file{'s' if len(indices_list) != 1 else ''})"
pattern_options.append(html.Option(value=pattern, label=label))
# Generate combined wildcard patterns for similar patterns
if len(sorted_patterns) > 1:
seen_wildcards = set()
for (pattern1, indices1), (pattern2, indices2) in combinations(sorted_patterns, 2):
# Find matching and differing sections
matcher = SequenceMatcher(None, pattern1, pattern2)
wildcard_parts = []
last_pos = 0
for match_start1, match_start2, match_length in matcher.get_matching_blocks():
# Handle the gap before this match (differences)
if match_start1 > last_pos:
diff1 = pattern1[last_pos:match_start1]
diff2 = pattern2[last_pos:match_start2]
# Skip if differences contain %d
if '%d' in diff1 or '%d' in diff2:
break
wildcard_parts.append('*')
# Add the matching section
if match_length > 0:
wildcard_parts.append(pattern1[match_start1:match_start1 + match_length])
last_pos = match_start1 + match_length
else:
# Only create wildcard if pattern contains wildcards and hasn't been seen before
wildcard_pattern = ''.join(wildcard_parts)
if '*' in wildcard_pattern and wildcard_pattern not in seen_wildcards:
seen_wildcards.add(wildcard_pattern)
# Combine indices from both patterns
combined_indices = sorted(set(idx[0] for idx in indices1 + indices2 if idx))
label = f"{wildcard_pattern} (files {str(srange(combined_indices))})"
pattern_options.append(html.Option(value=wildcard_pattern, label=label))
return pattern_options
# @dash.callback(
# Input('url-create-peakindexing','pathname'),
# prevent_initial_call=True,
# )
# def get_peakindexings(path):
# root_path = DEFAULT_VARIABLES["root_path"]
# if path == '/create-peakindexing':
# # Create a PeakIndex object with form defaults (not for database insertion)
# peakindex_form_data = db_schema.PeakIndex(
# scanNumber=PEAKINDEX_DEFAULTS.get("scanNumber", 0),
# # User text
# author=DEFAULT_VARIABLES["author"],
# notes=DEFAULT_VARIABLES["notes"],
# # Processing parameters
# # peakProgram=PEAKINDEX_DEFAULTS["peakProgram"],
# threshold=PEAKINDEX_DEFAULTS["threshold"],
# thresholdRatio=PEAKINDEX_DEFAULTS["thresholdRatio"],
# maxRfactor=PEAKINDEX_DEFAULTS["maxRfactor"],
# boxsize=PEAKINDEX_DEFAULTS["boxsize"],
# max_number=PEAKINDEX_DEFAULTS["max_peaks"],
# min_separation=PEAKINDEX_DEFAULTS["min_separation"],
# peakShape=PEAKINDEX_DEFAULTS["peakShape"],
# # scanPointStart=PEAKINDEX_DEFAULTS["scanPointStart"],
# # scanPointEnd=PEAKINDEX_DEFAULTS["scanPointEnd"],
# # depthRangeStart=PEAKINDEX_DEFAULTS.get("depthRangeStart"),
# # depthRangeEnd=PEAKINDEX_DEFAULTS.get("depthRangeEnd"),
# scanPoints=PEAKINDEX_DEFAULTS["scanPoints"],
# scanPointslen=srange(PEAKINDEX_DEFAULTS["scanPoints"]).len(),
# depthRange=PEAKINDEX_DEFAULTS["depthRange"],
# depthRangelen=srange(PEAKINDEX_DEFAULTS["depthRange"]).len(),
# detectorCropX1=PEAKINDEX_DEFAULTS["detectorCropX1"],
# detectorCropX2=PEAKINDEX_DEFAULTS["detectorCropX2"],
# detectorCropY1=PEAKINDEX_DEFAULTS["detectorCropY1"],
# detectorCropY2=PEAKINDEX_DEFAULTS["detectorCropY2"],
# min_size=PEAKINDEX_DEFAULTS["min_size"],
# max_peaks=PEAKINDEX_DEFAULTS["max_peaks"],
# smooth=PEAKINDEX_DEFAULTS["smooth"],
# maskFile=PEAKINDEX_DEFAULTS["maskFile"],
# indexKeVmaxCalc=PEAKINDEX_DEFAULTS["indexKeVmaxCalc"],
# indexKeVmaxTest=PEAKINDEX_DEFAULTS["indexKeVmaxTest"],
# indexAngleTolerance=PEAKINDEX_DEFAULTS["indexAngleTolerance"],
# indexH=PEAKINDEX_DEFAULTS["indexH"],
# indexK=PEAKINDEX_DEFAULTS["indexK"],
# indexL=PEAKINDEX_DEFAULTS["indexL"],
# indexCone=PEAKINDEX_DEFAULTS["indexCone"],
# energyUnit=PEAKINDEX_DEFAULTS["energyUnit"],
# exposureUnit=PEAKINDEX_DEFAULTS["exposureUnit"],
# cosmicFilter=PEAKINDEX_DEFAULTS["cosmicFilter"],
# recipLatticeUnit=PEAKINDEX_DEFAULTS["recipLatticeUnit"],
# latticeParametersUnit=PEAKINDEX_DEFAULTS["latticeParametersUnit"],
# # peaksearchPath=PEAKINDEX_DEFAULTS["peaksearchPath"],
# # p2qPath=PEAKINDEX_DEFAULTS["p2qPath"],
# # indexingPath=PEAKINDEX_DEFAULTS["indexingPath"],
# # File paths
# outputFolder=PEAKINDEX_DEFAULTS["outputFolder"],
# # filefolder=CATALOG_DEFAULTS["filefolder"],
# geoFile=PEAKINDEX_DEFAULTS["geoFile"],
# crystFile=PEAKINDEX_DEFAULTS["crystFile"],
# # Other fields
# depth=PEAKINDEX_DEFAULTS["depth"],
# beamline=PEAKINDEX_DEFAULTS["beamline"],
# )
# # Add root_path from DEFAULT_VARIABLES
# peakindex_form_data.root_path = root_path
# with Session(session_utils.get_engine()) as session:
# # Get next peakindex_id
# next_peakindex_id = db_utils.get_next_id(session, db_schema.PeakIndex)
# # Store next_peakindex_id and update title
# set_props('next-peakindex-id', {'value': next_peakindex_id})
# set_props('peakindex-title', {'children': f"New peak indexing {next_peakindex_id}"})
# # Retrieve data_path and filenamePrefix from catalog data
# catalog_data = get_catalog_data(session, PEAKINDEX_DEFAULTS["scanNumber"], root_path, CATALOG_DEFAULTS)
# peakindex_form_data.data_path = catalog_data["data_path"]
# peakindex_form_data.filenamePrefix = catalog_data["filenamePrefix"]
# # Populate the form with the defaults
# set_peakindex_form_props(peakindex_form_data)
# else:
# raise PreventUpdate
@dash.callback(
Output('peakindex-data-loaded-trigger', 'data'),
Input('url-create-peakindexing', 'href'),
prevent_initial_call=True,
)
def load_scan_data_from_url(href):
"""
Load scan data and optionally existing recon and peakindex data when provided in URL query parameters
URL format: /create-peakindexing?scan_id={scan_id}
Pooled URL format: /create-peakindexing?scan_id={scan_ids}
With recon_id: /create-peakindexing?scan_id={scan_id}&recon_id={recon_id}
With wirerecon_id: /create-peakindexing?scan_id={scan_id}&wirerecon_id={wirerecon_id}
With peakindex_id: /create-peakindexing?scan_id={scan_id}&peakindex_id={peakindex_id}
With both recon_id and peakindex_id: /create-peakindexing?scan_id={scan_id}&recon_id={recon_id}&peakindex_id={peakindex_id}
"""
if not href:
raise PreventUpdate
parsed_url = urllib.parse.urlparse(href)
query_params = urllib.parse.parse_qs(parsed_url.query)
scan_id_str = query_params.get('scan_id', [None])[0]
recon_id_str = query_params.get('recon_id', [None])[0]
wirerecon_id_str = query_params.get('wirerecon_id', [None])[0]
peakindex_id_str = query_params.get('peakindex_id', [None])[0]
root_path = DEFAULT_VARIABLES.get("root_path", "")
if scan_id_str:
with Session(session_utils.get_engine()) as session:
# # Get next peakindex_id
# next_peakindex_id = db_utils.get_next_id(session, db_schema.PeakIndex)
# # Store next_peakindex_id and update title
# set_props('next-peakindex-id', {'value': next_peakindex_id})
# set_props('peakindex-title', {'children': f"New peak indexing {next_peakindex_id}"})
try:
# This section handles both single and multiple/pooled scan numbers
scan_ids = [int(sid) if sid and sid.lower() != 'none' else None for sid in (scan_id_str.split(',') if scan_id_str else [])]
# Handle pooled reconstruction IDs
wirerecon_ids = [int(wid) if wid and wid.lower() != 'none' else None for wid in (wirerecon_id_str.split(',') if wirerecon_id_str else [])]
recon_ids = [int(rid) if rid and rid.lower() != 'none' else None for rid in (recon_id_str.split(',') if recon_id_str else [])]
peakindex_ids = [int(pid) if pid and pid.lower() != 'none' else None for pid in (peakindex_id_str.split(',') if peakindex_id_str else [])]
# Validate that lists have matching lengths
if wirerecon_ids and len(wirerecon_ids) != len(scan_ids): raise ValueError(f"Mismatch: {len(scan_ids)} scan IDs but {len(wirerecon_ids)} wirerecon IDs")
if recon_ids and len(recon_ids) != len(scan_ids): raise ValueError(f"Mismatch: {len(scan_ids)} scan IDs but {len(recon_ids)} recon IDs")
if peakindex_ids and len(peakindex_ids) != len(scan_ids): raise ValueError(f"Mismatch: {len(scan_ids)} scan IDs but {len(peakindex_ids)} peakindex IDs")
# If no reconstruction IDs provided, fill with None