-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathxtc_process.py
More file actions
1403 lines (1260 loc) · 60.8 KB
/
xtc_process.py
File metadata and controls
1403 lines (1260 loc) · 60.8 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
from __future__ import absolute_import, division, print_function
from six.moves import range
from six.moves import zip
# -*- Mode: Python; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 8 -*-
#
# LIBTBX_SET_DISPATCHER_NAME cctbx.xfel.xtc_process
#
try:
import psana
except ImportError:
pass # for running at home without psdm build
import errno
from xfel.cftbx.detector import cspad_cbf_tbx
from xfel.cxi.cspad_ana import cspad_tbx, rayonix_tbx
import pycbf, os, sys, copy, socket
import libtbx.load_env
from libtbx.utils import Sorry, Usage
from dials.util import show_mail_on_error
from dials.util.options import ArgumentParser
from libtbx.phil import parse
from dxtbx.model.experiment_list import ExperimentListFactory
from dials.array_family import flex
from libtbx import easy_pickle
from dxtbx.model.experiment_list import ExperimentList
import io # fix buffering py2/3
# check version of psana
from xfel.cftbx.detector.cspad_cbf_tbx import PSANA2_VERSION
xtc_phil_str = '''
dispatch {
max_events = None
.type = int
.help = If not specified, process all events. Otherwise, only process this many
process_percent = None
.type = int(value_min=1, value_max=100)
.help = Percent of events to process
estimate_gain_only = False
.type = bool
.help = Use to print estimated gain parameters for each event, then exit without attempting \
further processing.
find_spots = True
.type = bool
.help = Whether to do spotfinding. Needed for indexing/integration
datasource = None
.type = str
.expert_level = 2
.help = This is to specify which datasource should be used for processing data at LCLS \
Format is exp=<experiment_name>:run=<run_number>:<mode> \
eg. exp=mfxo1916:run=20:xtc \
More info at https://confluence.slac.stanford.edu/display/PSDM/Manual#Manual-Datasetspecification
hit_finder{
enable = True
.type = bool
.help = Whether to do hitfinding. hit_finder=False: process all images
minimum_number_of_reflections = 16
.type = int
.help = If the number of strong reflections on an image is less than this, and \
the hitfinder is enabled, discard this image.
maximum_number_of_reflections = None
.type = int
.help = If specified, ignores images with more than this many number of reflections
}
index = True
.type = bool
.help = Attempt to index images
refine = False
.type = bool
.help = If True, after indexing, refine the experimental models
integrate = True
.type = bool
.help = Integrated indexed images. Ignored if index=False
coset = False
.expert_level = 2
.type = bool
.help = Within the integrate dispatcher, integrate a sublattice coset intended to represent \
negative control spots with no Bragg diffraction.
dump_strong = False
.type = bool
.help = Save strongly diffracting images to cbf format
dump_indexed = True
.type = bool
.help = Save indexed images to cbf format
dump_all = False
.type = bool
.help = All frames will be saved to cbf format if set to True
reindex_strong = False
.type = bool
.help = If true, after indexing and refinement, re-index the strong reflections with \
no outlier rejection
}
debug
.help = Use these flags to track down problematic events that cause unhandled exceptions. \
Here, a bad event means it caused an unhandled exception, not that the image \
failed to index. \
Examples: \
Process only unprocessed events (excluding bad events): \
skip_processed_events=True, skip_unprocessed_events=False skip_bad_events=True \
Process only bad events (for debugging): \
skip_processed_events=True, skip_unprocessed_events=True skip_bad_events=False \
Note, due to how MPI works, if an unhandled exception occurrs, some bad events \
will be marked as bad that were simply in process when the program terminated \
due to a bad event. Try processing only bad events single process to find the \
culprit and alert the program authors.
{
skip_processed_events = False
.type = bool
.help = If True, will look for diagnostic files in the output directory and use \
them to skip events that had already been processed (succesfully or not)
skip_unprocessed_events = False
.type = bool
.help = If True, will look for diagnostic files in the output directory and use \
them to skip events that had haven't been processed
skip_bad_events = False
.type = bool
.help = If True, will look for diagnostic files in the output directory and use \
them to skip events that had caused unhandled exceptions previously
event_timestamp = None
.type = str
.multiple = True
.help = List of timestamps. If set, will only process the events that match them
}
input {
cfg = None
.type = str
.help = Path to psana config file. Genearlly not needed for CBFs. For image pickles, \
the psana config file should have a mod_image_dict module.
experiment = None
.type = str
.help = Experiment identifier, e.g. cxi84914
run_num = None
.type = int
.help = Run number or run range to process
address = None
.type = str
.help = Detector address, e.g. CxiDs2.0:Cspad.0, or detector alias, e.g. Ds1CsPad
stream = None
.type = ints
.expert_level = 2
.help = Stream number to read from. Usually not necessary as psana will read the data \
from all streams by default
override_spotfinding_trusted_max = None
.type = int
.help = During spot finding, override the saturation value for this data. \
Overloads will not be integrated, but they can assist with indexing.
override_spotfinding_trusted_min = None
.type = int
.help = During spot finding, override the minimum pixel value \
for this data. This does not affect integration.
override_integration_trusted_max = None
.type = int
.help = During integration, override the saturation value for this data.
override_integration_trusted_min = None
.type = int
.help = During integration, override the minimum pixel value \
for this data.
use_ffb = False
.type = bool
.help = Run on the ffb if possible. Only for active users!
xtc_dir = None
.type = str
.help = Optional path to data directory if it's non-standard. Only needed if xtc \
streams are not in the standard location for your PSDM installation.
calib_dir = None
.type = str
.help = Optional path to calib directory if it's non-standard. Only needed if calib \
data are not in the standard location for your PSDM installation.
trial = None
.type = int
.help = Optional. Trial number for this run.
rungroup = None
.type = int
.help = Optional. Useful for organizing runs with similar parameters into logical \
groupings.
known_orientations_folder = None
.type = str
.expert_level = 2
.help = Folder with previous processing results including crystal orientations. \
If specified, images will not be re-indexed, but instead the known \
orientations will be used.
ignore_gain_mismatch = False
.type = bool
.expert_level = 3
.help = Detector gain should be set on the detector models loaded from the images or in the \
processing parameters, not both. Override the check that this is true with this flag. \
}
format {
file_format = *cbf pickle
.type = choice
.help = Output file format, 64 tile segmented CBF or image pickle
pickle {
out_key = cctbx.xfel.image_dict
.type = str
.help = Key name that mod_image_dict uses to put image data in each psana event
}
cbf {
detz_offset = None
.type = float
.help = Distance from back of detector rail to sample interaction region (CXI) \
or actual detector distance (XPP/MFX)
override_energy = None
.type = float
.help = If not None, use the input energy for every event instead of the energy \
from the XTC stream
override_distance = None
.type = float
.help = If not None, use the input distance for every event instead of the distance \
from the XTC stream
invalid_pixel_mask = None
.type = str
.help = Path to invalid pixel mask, in the dials.generate_mask format. If not set, use the \
psana computed invalid pixel mask. Regardless, pixels outside of the trusted range \
for each image will also be masked out. See cxi.make_dials_mask.
mode = *cspad rayonix
.type = choice
.help = CBFs output in the designated mode
cspad {
mask_nonbonded_pixels = False
.type = bool
.help = If true, try to get non-bonded pixels from psana calibrations and apply them. Includes \
the 4 pixels on each side of each pixel. Only used if a custom invalid_pixel_mask is \
provided (otherwise the psana calibration will mask these out automatically).
gain_mask_value = None
.type = float
.help = If not None, use the gain mask for the run to multiply the low-gain pixels by this number
per_pixel_gain = False
.type = bool
.help = If True, use a per pixel gain from the run's calib folder, if available
additional_gain_factor = None
.type = float
.help = If set, pixels counts are divided by this number after all other corrections.
common_mode {
algorithm = default custom
.type = choice
.help = Choice of SLAC's common mode correction algorithms. If not specified, use no common \
mode correction, only dark pedestal subtraction. Default: use the default common_mode \
correction. Custom, see \
https://confluence.slac.stanford.edu/display/PSDM/Common+mode+correction+algorithms
custom_parameterization = None
.type = ints
.help = Parameters to control SLAC's common mode correction algorithms. Should be None if \
common_mode.algorithm is default or None. See \
https://confluence.slac.stanford.edu/display/PSDM/Common+mode+correction+algorithms
}
}
rayonix {
bin_size = 2
.type = int
.help = Detector binning mode
override_beam_x = None
.type = float
.help = If set, override the beam X position
override_beam_y = None
.type = float
.help = If set, override the beam Y position
}
}
per_pixel_absorption_correction
.multiple = True {
apply = False
.type = bool
algorithm = *fuller_kapton
.type = choice
fuller_kapton {
xtal_height_above_kapton_mm {
value = 0.02
.type = float
.help = height of the beam (or the irradiated crystal) above the kapton tape
}
rotation_angle_deg {
value = 1.15
.type = float
.help = angle of the tape from vertical
}
kapton_half_width_mm {
value = 1.5875
.type = float
.help = forward distance from irradiated crystal to edge of tape nearest detector
}
kapton_thickness_mm {
value = 0.05
.type = float
.help = tape thickness
}
}
}
}
output {
output_dir = .
.type = str
.help = Directory output files will be placed
composite_output = True
.type = bool
.help = If True, save one set of json/pickle files per process, where each is a \
concatenated list of all the successful events examined by that process. \
If False, output a separate json/pickle file per image (generates a \
lot of files).
delete_integration_shoeboxes = True
.type = bool
.help = Delete integration shoeboxes when finished with each image.
logging_dir = None
.type = str
.help = Directory output log files will be placed
experiments_filename = %s.expt
.type = str
.help = The filename for output experiment list
strong_filename = %s_strong.refl
.type = str
.help = The filename for strong reflections from spot finder output.
indexed_filename = %s_indexed.refl
.type = str
.help = The filename for indexed reflections.
refined_experiments_filename = %s_refined.expt
.type = str
.help = The filename for saving refined experimental models
integrated_filename = %s_integrated.refl
.type = str
.help = The filename for final experimental modls
integrated_experiments_filename = %s_integrated.expt
.type = str
.help = The filename for final integrated reflections.
coset_filename = %s_coset%d.refl
.type = str
.help = The filename for final coset reflections.
coset_experiments_filename = %s_coset%d.expt
.type = str
.help = The filename for saving final coset experimental models.
profile_filename = None
.type = str
.help = The filename for output reflection profile parameters
integration_pickle = int-%d-%s.pickle
.type = str
.help = Filename for cctbx.xfel-style integration pickle files
reindexedstrong_filename = %s_reindexedstrong.refl
.type = str
.help = The file name for re-indexed strong reflections
tmp_output_dir = "(NONE)"
.type = str
.help = Directory for CBFlib temporary output files
}
mp {
method = *mpi sge
.type = choice
.help = Muliprocessing method
mpi {
method = *client_server striping
.type = choice
.help = Method of serving data to child processes in MPI. client_server: \
use one process as a server that sends timestamps to each process. \
All processes will stay busy at all times at the cost of MPI send/ \
recieve overhead. striping: each process uses its rank to determine \
which events to process. Some processes will finish early and go \
idle, but no MPI overhead is incurred.
}
composite_stride = None
.type = int
.help = For MPI, if using composite mode, specify how many ranks to \
aggregate data from. For example, if you have 100 processes, \
composite mode will output N*100 files, where N is the number \
of file types (json, pickle, etc). If you specify stride = 25, \
then each group of 25 process will send their results to 4 \
processes and only N*4 files will be created. Ideally, match \
stride to the number of processors per node.
}
'''
from dials.command_line.stills_process import dials_phil_str, program_defaults_phil_str
extra_dials_phil_str = '''
border_mask {
include scope dials.util.masking.phil_scope
}
joint_reintegration {
enable = False
.type = bool
.help = If enabled, after processing the data, do a joint refinement and \
re-integration
minimum_results = 30
.type = int
.help = Minimum number of integration results needed for joint reintegration
maximum_results_per_chunk = 500
.type = int
include scope dials.algorithms.refinement.refiner.phil_scope
include scope dials.algorithms.integration.integrator.phil_scope
}
'''
def filter(evt):
return True
def run_psana2(ims, params, comm):
"""" Begins psana2
This setup a DataSource psana2 style. The parallelization is determined within
the generation of the DataSource.
ims: InMemScript (cctbx driver class)
params: input parameters
comm: mpi comm for broadcasting per run calibration files"""
ds = psana.DataSource(exp=params.input.experiment, run=params.input.run_num, \
dir=params.input.xtc_dir, max_events=params.dispatch.max_events, \
det_name=params.input.address)
for run in ds.runs():
det = run.Detector(ds.det_name)
# broadcast cctbx per run calibration
if comm.Get_rank() == 0:
PS_CALIB_DIR = os.environ.get('PS_CALIB_DIR')
assert PS_CALIB_DIR
dials_mask = easy_pickle.load(params.format.cbf.invalid_pixel_mask)
else:
dials_mask = None
dials_mask = comm.bcast(dials_mask, root=0)
for evt in run.events():
ims.base_dxtbx = cspad_cbf_tbx.env_dxtbx_from_slac_metrology(run, params.input.address)
ims.dials_mask = dials_mask
ims.spotfinder_mask = None
ims.integration_mask = None
ims.psana_det = det
if params.format.file_format == 'cbf':
if params.format.cbf.cspad.common_mode.algorithm == "custom":
ims.common_mode = params.format.cbf.cspad.common_mode.custom_parameterization
assert ims.common_mode is not None
else:
ims.common_mode = params.format.cbf.cspad.common_mode.algorithm # could be None or default
ims.process_event(run, evt)
ims.finalize()
class EventOffsetSerializer(object):
""" Pickles python object """
def __init__(self,psanaOffset):
self.filenames = psanaOffset.filenames()
self.offsets = psanaOffset.offsets()
self.lastBeginCalibCycleDgram = psanaOffset.lastBeginCalibCycleDgram()
from xfel.ui import db_phil_str
from xfel.command_line.xfel_process import radial_average_phil_str
phil_scope = parse(xtc_phil_str + dials_phil_str + extra_dials_phil_str + db_phil_str + radial_average_phil_str, process_includes=True).fetch(parse(program_defaults_phil_str))
from xfel.command_line.xfel_process import Script as DialsProcessScript
from xfel.ui.db.frame_logging import DialsProcessorWithLogging
from xfel.ui.db.dxtbx_db import dxtbx_xfel_db_application
class InMemScript(DialsProcessScript, DialsProcessorWithLogging):
""" Script to process XFEL data at LCLS """
def __init__(self):
""" Set up the option parser. Arguments come from the command line or a phil file """
self.usage = """
%s input.experiment=experimentname input.run_num=N input.address=address
format.file_format=cbf format.cbf.detz_offset=N
%s input.experiment=experimentname input.run_num=N input.address=address
format.file_format=pickle input.cfg=filename
"""%(libtbx.env.dispatcher_name, libtbx.env.dispatcher_name)
self.parser = ArgumentParser(
usage = self.usage,
phil = phil_scope)
self.debug_file_path = None
self.debug_str = None
self.mpi_log_file_path = None
self.reference_detector = None
self.composite_tag = None
self.all_imported_experiments = None
self.all_strong_reflections = None
self.all_indexed_experiments = None
self.all_indexed_reflections = None
self.all_integrated_experiments = None
self.all_integrated_reflections = None
self.all_int_pickle_filenames = []
self.all_int_pickles = []
self.cached_ranges = None
self.tt_low = None
self.tt_high = None
self.db_app = None
def debug_start(self, ts):
self.debug_str = "%s,%s"%(socket.gethostname(), ts)
self.debug_str += ",%s,%s,%s\n"
self.debug_write("start")
def debug_write(self, string, state = None):
ts = cspad_tbx.evt_timestamp() # Now
debug_file_handle = open(self.debug_file_path, 'a')
if string == "":
debug_file_handle.write("\n")
else:
if state is None:
state = " "
debug_file_handle.write(self.debug_str%(ts, state, string))
debug_file_handle.close()
def mpi_log_write(self, string):
print(string)
mpi_log_file_handle = open(self.mpi_log_file_path, 'a')
mpi_log_file_handle.write(string)
mpi_log_file_handle.close()
def psana_mask_to_dials_mask(self, psana_mask):
if psana_mask.dtype == bool:
psana_mask = flex.bool(psana_mask)
else:
psana_mask = flex.bool(psana_mask == 1)
assert psana_mask.focus() == (32, 185, 388)
dials_mask = []
for i in range(32):
dials_mask.append(psana_mask[i:i+1,:,:194])
dials_mask[-1].reshape(flex.grid(185,194))
dials_mask.append(psana_mask[i:i+1,:,194:])
dials_mask[-1].reshape(flex.grid(185,194))
return dials_mask
def run(self):
""" Process all images assigned to this thread """
try:
params, options = self.parser.parse_args(
show_diff_phil=True, quick_parse=True)
except Exception as e:
if "Unknown command line parameter definition" in str(e) or \
"The following definitions were not recognised" in str(e):
deprecated_params = ['mask_nonbonded_pixels','gain_mask_value','algorithm','custom_parameterization']
deprecated_strs = ['%s','%s','common_mode.%s','common_mode.%s']
for i in range(len(deprecated_params)):
if deprecated_params[i] in str(e):
print("format.cbf.%s"%(deprecated_strs[i]%deprecated_params[i]), "has changed to format.cbf.cspad.%s"%(deprecated_strs[i]%deprecated_params[i]))
raise
if params.dispatch.coset:
self.all_coset_experiments = ExperimentList()
self.all_coset_reflections = flex.reflection_table()
# Check inputs
if params.input.experiment is None or \
params.input.run_num is None or \
(params.input.address is None and params.format.file_format != 'pickle'):
raise Usage(self.usage)
if params.format.file_format == "cbf":
if params.format.cbf.detz_offset is None:
raise Usage(self.usage)
elif params.format.file_format == "pickle":
if params.input.cfg is None:
raise Usage(self.usage)
else:
raise Usage(self.usage)
if not os.path.exists(params.output.output_dir):
raise Sorry("Output path not found:" + params.output.output_dir)
if params.format.file_format == "cbf":
if params.output.tmp_output_dir == "(NONE)":
tmp_dir = params.output.tmp_output_dir
else:
#Environment variable redirect for CBFLib temporary CBF_TMP_XYZ file output
if params.output.tmp_output_dir is None:
tmp_dir = os.path.join(params.output.output_dir, '.tmp')
else:
tmp_dir = os.path.join(params.output.tmp_output_dir, '.tmp')
if not os.path.exists(tmp_dir):
with show_mail_on_error():
try:
os.makedirs(tmp_dir)
# Can fail if running multiprocessed - that's OK if the folder was created
except OSError as e: # In Python 2, a FileExistsError is just an OSError
if e.errno != errno.EEXIST: # If this OSError is not a FileExistsError
raise
os.environ['CBF_TMP_DIR'] = tmp_dir
for abs_params in params.integration.absorption_correction:
if abs_params.apply and abs_params.algorithm == "fuller_kapton":
if not (params.integration.debug.output and not params.integration.debug.separate_files):
raise Sorry('Shoeboxes must be saved to integration intermediates to apply an absorption correction. '\
+'Set integration.debug.output=True and integration.debug.separate_files=False to save shoeboxes.')
self.params = params
self.load_reference_geometry()
if params.output.composite_output:
self.all_imported_experiments = ExperimentList()
self.all_strong_reflections = flex.reflection_table()
self.all_indexed_experiments = ExperimentList()
self.all_indexed_reflections = flex.reflection_table()
self.all_integrated_experiments = ExperimentList()
self.all_integrated_reflections = flex.reflection_table()
else:
# The convention is to put %s in the phil parameter to add a time stamp to
# each output datafile. Save the initial templates here.
self.strong_filename_template = params.output.strong_filename
self.indexed_filename_template = params.output.indexed_filename
self.refined_experiments_filename_template = params.output.refined_experiments_filename
self.integrated_filename_template = params.output.integrated_filename
self.integrated_experiments_filename_template = params.output.integrated_experiments_filename
self.coset_filename_template = params.output.coset_filename
self.coset_experiments_filename_template = params.output.coset_experiments_filename
self.reindexedstrong_filename_template = params.output.reindexedstrong_filename
# Don't allow the strong reflections to be written unless there are enough to
# process
params.output.strong_filename = None
# Save the paramters
self.params_cache = copy.deepcopy(params)
self.options = options
if params.mp.method == "mpi":
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank() # each process in MPI has a unique id, 0-indexed
size = comm.Get_size() # size: number of processes running in this job
elif params.mp.method == "sge" and \
'SGE_TASK_ID' in os.environ and \
'SGE_TASK_FIRST' in os.environ and \
'SGE_TASK_LAST' in os.environ:
if 'SGE_STEP_SIZE' in os.environ:
assert int(os.environ['SGE_STEP_SIZE']) == 1
if os.environ['SGE_TASK_ID'] == 'undefined' or os.environ['SGE_TASK_ID'] == 'undefined' or os.environ['SGE_TASK_ID'] == 'undefined':
rank = 0
size = 1
else:
rank = int(os.environ['SGE_TASK_ID']) - int(os.environ['SGE_TASK_FIRST'])
size = int(os.environ['SGE_TASK_LAST']) - int(os.environ['SGE_TASK_FIRST']) + 1
else:
rank = 0
size = 1
self.composite_tag = "%04d"%rank
# Configure the logging
if params.output.logging_dir is None:
logfile = ''
elif params.output.logging_dir == 'DevNull':
print("Redirecting stdout, stderr and other DIALS logfiles to /dev/null")
logfile = os.devnull
try:
# Python 3
sys.stdout = io.TextIOWrapper(open(os.devnull,'wb', 0), write_through=True)
sys.stderr = io.TextIOWrapper(open(os.devnull,'wb', 0), write_through=True)
except TypeError:
# Python 2
sys.stdout = open(os.devnull,'w', buffering=0)
sys.stderr = open(os.devnull,'w',buffering=0)
info_path = os.devnull
debug_path = os.devnull
else:
log_path = os.path.join(params.output.logging_dir, "log_rank%04d.out"%rank)
error_path = os.path.join(params.output.logging_dir, "error_rank%04d.out"%rank)
print("Redirecting stdout to %s"%log_path)
print("Redirecting stderr to %s"%error_path)
try:
# Python 3
sys.stdout = io.TextIOWrapper(open(log_path,'ab', 0), write_through=True)
sys.stderr = io.TextIOWrapper(open(error_path,'ab', 0), write_through=True)
except TypeError:
# Python 2
sys.stdout = open(log_path,'a', buffering=0)
sys.stderr = open(error_path,'a',buffering=0)
print("Should be redirected now")
logfile = os.path.join(params.output.logging_dir, "info_rank%04d.out"%rank)
from dials.util import log
log.config(options.verbose, logfile=logfile)
debug_dir = os.path.join(params.output.output_dir, "debug")
if not os.path.exists(debug_dir):
try:
os.makedirs(debug_dir)
except OSError as e:
pass # due to multiprocessing, makedirs can sometimes fail
assert os.path.exists(debug_dir)
if params.debug.skip_processed_events or params.debug.skip_unprocessed_events or params.debug.skip_bad_events:
print("Reading debug files...")
self.known_events = {}
for filename in os.listdir(debug_dir):
# format: hostname,timestamp_event,timestamp_now,status,detail
for line in open(os.path.join(debug_dir, filename)):
vals = line.strip().split(',')
if len(vals) != 5:
continue
_, ts, _, status, detail = vals
if status in ["done", "stop", "fail"]:
self.known_events[ts] = status
else:
self.known_events[ts] = "unknown"
self.debug_file_path = os.path.join(debug_dir, "debug_%d.txt"%rank)
write_newline = os.path.exists(self.debug_file_path)
if write_newline: # needed if the there was a crash
self.debug_write("")
if params.mp.method != 'mpi' or params.mp.mpi.method == 'client_server':
if rank == 0:
self.mpi_log_file_path = os.path.join(debug_dir, "mpilog.out")
write_newline = os.path.exists(self.mpi_log_file_path)
if write_newline: # needed if the there was a crash
self.mpi_log_write("\n")
# FIXME MONA: psana 2 has pedestals and geometry hardcoded for cxid9114.
# We can remove after return code when all interfaces are ready.
if PSANA2_VERSION:
print(("PSANA2_VERSION", PSANA2_VERSION))
run_psana2(self, params, comm)
return
# set up psana
if params.input.cfg is not None:
psana.setConfigFile(params.input.cfg)
# all cores in stripe mode and the master in client-server mode read smd
if params.dispatch.datasource is None:
datasource = "exp=%s:run=%s:%s"%(params.input.experiment,params.input.run_num,'smd')
if params.input.xtc_dir is not None:
if params.input.use_ffb:
raise Sorry("Cannot specify the xtc_dir and use SLAC's ffb system")
datasource += ":dir=%s"%params.input.xtc_dir
elif params.input.use_ffb:
# as ffb is only at SLAC, ok to hardcode /reg/d here
datasource += ":dir=/reg/d/ffb/%s/%s/xtc"%(params.input.experiment[0:3],params.input.experiment)
if params.input.stream is not None and len(params.input.stream) > 0:
datasource += ":stream=%s"%(",".join(["%d"%stream for stream in params.input.stream]))
if params.input.calib_dir is not None:
psana.setOption('psana.calib-dir',params.input.calib_dir)
if params.mp.method == "mpi" and params.mp.mpi.method == 'client_server' and size > 2:
dataset_name_client = datasource.replace(":smd",":rax")
# for client-server, master reads smd - clients read rax
if rank == 0:
ds = psana.DataSource(datasource)
else:
ds = psana.DataSource(dataset_name_client)
else:
# for stripe, all cores read smd
ds = psana.DataSource(datasource)
else:
datasource = params.dispatch.datasource
ds = psana.DataSource(datasource)
if params.format.file_format == "cbf":
self.psana_det = psana.Detector(params.input.address, ds.env())
# set this to sys.maxint to analyze all events
if params.dispatch.max_events is None:
max_events = sys.maxsize
else:
max_events = params.dispatch.max_events
# Set up db connection if being used
if self.params.experiment_tag is not None:
self.db_app = dxtbx_xfel_db_application(params)
for run in ds.runs():
if params.format.file_format == "cbf":
if params.format.cbf.mode == "cspad":
# load a header only cspad cbf from the slac metrology
try:
self.base_dxtbx = cspad_cbf_tbx.env_dxtbx_from_slac_metrology(run, params.input.address)
except Exception as e:
raise Sorry("Couldn't load calibration file for run %d, %s"%(run.run(), str(e)))
elif params.format.cbf.mode == "rayonix":
# load a header only rayonix cbf from the input parameters
detector_size = rayonix_tbx.get_rayonix_detector_dimensions(ds.env())
self.base_dxtbx = rayonix_tbx.get_dxtbx_from_params(params.format.cbf.rayonix, detector_size)
if self.base_dxtbx is None:
raise Sorry("Couldn't load calibration file for run %d"%run.run())
if params.format.file_format == 'cbf':
if params.format.cbf.cspad.common_mode.algorithm == "custom":
self.common_mode = params.format.cbf.cspad.common_mode.custom_parameterization
assert self.common_mode is not None
else:
self.common_mode = params.format.cbf.cspad.common_mode.algorithm # could be None or default
if params.format.cbf.invalid_pixel_mask is not None:
self.dials_mask = easy_pickle.load(params.format.cbf.invalid_pixel_mask)
if params.format.cbf.mode == "cspad":
assert len(self.dials_mask) == 64
if self.params.format.cbf.cspad.mask_nonbonded_pixels:
psana_mask = self.psana_det.mask(run.run(),calib=False,status=False,edges=False,central=False,unbond=True,unbondnbrs=True)
dials_mask = self.psana_mask_to_dials_mask(psana_mask)
self.dials_mask = [self.dials_mask[i] & dials_mask[i] for i in range(len(dials_mask))]
else:
if params.format.cbf.mode == "cspad":
psana_mask = self.psana_det.mask(run.run(),calib=True,status=True,edges=True,central=True,unbond=True,unbondnbrs=True)
self.dials_mask = self.psana_mask_to_dials_mask(psana_mask)
else:
self.dials_mask = None
if self.params.spotfinder.lookup.mask is not None:
self.spotfinder_mask = easy_pickle.load(self.params.spotfinder.lookup.mask)
else:
self.spotfinder_mask = None
if self.params.integration.lookup.mask is not None:
self.integration_mask = easy_pickle.load(self.params.integration.lookup.mask)
else:
self.integration_mask = None
# prepare fractions of process_percent, if given
process_fractions = None
if params.dispatch.process_percent:
import fractions
percent = params.dispatch.process_percent / 100
process_fractions = fractions.Fraction(percent).limit_denominator(100)
# list of all events
# only cycle through times in client_server mode
if params.mp.method == "mpi" and params.mp.mpi.method == 'client_server' and size > 2:
# process fractions only works in idx-striping mode
if params.dispatch.process_percent:
raise Sorry("Process percent only works in striping mode.")
print("Using MPI client server in rax mode")
# use a client/server approach to be sure every process is busy as much as possible
# only do this if there are more than 2 processes, as one process will be a server
try:
if rank == 0:
# server process
self.mpi_log_write("MPI START\n")
for nevt, evt in enumerate(run.events()):
if nevt == max_events: break
self.mpi_log_write("Getting next available process\n")
offset = evt.get(psana.EventOffset)
rankreq = comm.recv(source=MPI.ANY_SOURCE)
t = evt.get(psana.EventId).time()
ts = cspad_tbx.evt_timestamp((t[0],t[1]/1e6))
self.mpi_log_write("Process %s is ready, sending ts %s\n"%(rankreq, ts))
comm.send(EventOffsetSerializer(offset),dest=rankreq)
# send a stop command to each process
self.mpi_log_write("MPI DONE, sending stops\n")
for rankreq in range(size-1):
self.mpi_log_write("Getting next available process\n")
rankreq = comm.recv(source=MPI.ANY_SOURCE)
self.mpi_log_write("Sending stop to %d\n"%rankreq)
comm.send('endrun',dest=rankreq)
self.mpi_log_write("All stops sent.")
else:
# client process
while True:
# inform the server this process is ready for an event
print("Rank %d getting next task"%rank)
comm.send(rank,dest=0)
print("Rank %d waiting for response"%rank)
offset = comm.recv(source=0)
if offset == 'endrun':
print("Rank %d recieved endrun"%rank)
break
evt = ds.jump(offset.filenames, offset.offsets, offset.lastBeginCalibCycleDgram)
print("Rank %d beginning processing"%rank)
try:
self.process_event(run, evt)
except Exception as e:
print("Rank %d unhandled exception processing event"%rank, str(e))
print("Rank %d event processed"%rank)
except Exception as e:
print("Error caught in main loop")
print(str(e))
print("Synchronizing rank %d"%rank)
comm.Barrier()
print("Rank %d done with main loop"%rank)
else:
import resource
# chop the list into pieces, depending on rank. This assigns each process
# events such that the get every Nth event where N is the number of processes
print("Striping events")
nevent = mem = first = last = 0
if process_fractions:
def process_this_event(nevent):
# nevent modulo the denominator gives us which fraction we're in
n_mod_denom = nevent % process_fractions.denominator
# compare the 0-indexed modulo against the 1-indexed numerator (intentionally not <=)
n_accept = n_mod_denom < process_fractions.numerator
return n_accept
for nevent, evt in enumerate(run.events()):
if nevent%size != rank: continue
if nevent >= max_events: break
if process_fractions and not process_this_event(nevent): continue
self.process_event(run, evt)
mem = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
if nevent < 50:
#print "Mem test rank %03d"%rank, i, mem
continue
#print "Mem test rank %03d"%rank, 'Cycle %6d total %7dkB increase %4dkB' % (i, mem, mem - last)
if not first:
first = mem
last = mem
print('Total memory leaked in %d cycles: %dkB' % (nevent+1-50, mem - first))
print("Rank %d finalizing"%rank)
try:
self.finalize()
except Exception as e:
print("Rank %d, exception caught in finalize"%rank)
print(str(e))
if params.format.file_format == "cbf" and params.output.tmp_output_dir == "(NONE)":
try:
os.rmdir(tmp_dir)
except Exception as e:
pass
if params.joint_reintegration.enable:
if params.output.composite_output:
raise NotImplementedError("Joint reintegration not implemented for composite output yet")
assert self.params.dispatch.dump_indexed, "Cannot do joint reintegration unless indexed files were dumped"
if rank == 0:
reint_dir = os.path.join(params.output.output_dir, "reint")
if not os.path.exists(reint_dir):
os.makedirs(reint_dir)
images = []
experiment_jsons = []
indexed_tables = []
for filename in os.listdir(params.output.output_dir):
if not filename.endswith("_indexed.refl"):
continue
experiment_jsons.append(os.path.join(params.output.output_dir, filename.split("_indexed.refl")[0] + "_refined.expt"))
indexed_tables.append(os.path.join(params.output.output_dir, filename))
if params.format.file_format == "cbf":
images.append(os.path.join(params.output.output_dir, filename.split("_indexed.refl")[0] + ".cbf"))
elif params.format.file_format == "pickle":
images.append(os.path.join(params.output.output_dir, filename.split("_indexed.refl")[0] + ".pickle"))
if len(images) < params.joint_reintegration.minimum_results:
pass # print and return
# TODO: maximum_results_per_chunk = 500
combo_input = os.path.join(reint_dir, "input.phil")
f = open(combo_input, 'w')
for json, indexed in zip(experiment_jsons, indexed_tables):
f.write("input {\n")
f.write(" experiments = %s\n"%json)
f.write(" reflections = %s\n"%indexed)
f.write("}\n")
f.close()
combined_experiments_file = os.path.join(reint_dir, "combined.expt")
combined_reflections_file = os.path.join(reint_dir, "combined.refl")
command = "dials.combine_experiments reference_from_experiment.average_detector=True %s output.reflections=%s output.experiments=%s"% \
(combo_input, combined_reflections_file, combined_experiments_file)
print(command)
from libtbx import easy_run
easy_run.fully_buffered(command).raise_if_errors().show_stdout()
from dxtbx.model.experiment_list import ExperimentListFactory
combined_experiments = ExperimentListFactory.from_json_file(combined_experiments_file, check_format=False)
combined_reflections = flex.reflection_table.from_file(combined_reflections_file)
from dials.algorithms.refinement import RefinerFactory
refiner = RefinerFactory.from_parameters_data_experiments(
params.joint_reintegration, combined_reflections, combined_experiments)
refiner.run()
experiments = refiner.get_experiments()
reflections = combined_reflections.select(refiner.selection_used_for_refinement())
experiments.as_file(os.path.join(reint_dir, "refined.expt"))
reflections.as_pickle(os.path.join(reint_dir, "refined.refl"))
for expt_id, (expt, img_file) in enumerate(zip(experiments, images)):
try:
refls = reflections.select(reflections['id'] == expt_id)
refls['id'] = flex.int(len(refls), 0)
base_name = os.path.splitext(os.path.basename(img_file))[0]
self.params.output.integrated_filename = os.path.join(reint_dir, base_name + "_integrated.refl")
expts = ExperimentList([expt])
self.integrate(expts, refls)
expts.as_file(os.path.join(reint_dir, base_name + "_refined.expt"))
except Exception as e:
print("Couldn't reintegrate", img_file, str(e))
print("Rank %d signing off"%rank)
def get_run_and_timestamp(self, obj):
# Used by database logger
return self.run.run(), self.timestamp
def process_event(self, run, evt):
"""
Process a single event from a run
@param run psana run object
@param timestamp psana timestamp object
"""
if PSANA2_VERSION:
sec = evt._seconds
nsec = evt._nanoseconds
else: