-
Notifications
You must be signed in to change notification settings - Fork 11
/
AbstractMultiCollect.py
1012 lines (814 loc) · 41.4 KB
/
AbstractMultiCollect.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 os
import sys
import types
import logging
import time
import errno
import abc
import collections
import gevent
import autoprocessing
import gevent
from HardwareRepository.TaskUtils import *
BeamlineControl = collections.namedtuple('BeamlineControl',
['diffractometer',
'sample_changer',
'lims',
'safety_shutter',
'machine_current',
'cryo_stream',
'energy',
'resolution',
'detector_distance',
'transmission',
'undulators',
'flux',
'detector',
'beam_info'])
BeamlineConfig = collections.namedtuple('BeamlineConfig',
['synchrotron_name',
'directory_prefix',
'default_exposure_time',
'minimum_exposure_time',
'detector_fileext',
'detector_type',
'detector_manufacturer',
'detector_model',
'detector_px',
'detector_py',
'undulators',
'focusing_optic',
'monochromator_type',
'beam_divergence_vertical',
'beam_divergence_horizontal',
'polarisation',
'input_files_server'])
class AbstractMultiCollect(object):
__metaclass__ = abc.ABCMeta
def __init__(self):
self.bl_control = BeamlineControl(*[None]*14)
self.bl_config = BeamlineConfig(*[None]*17)
self.data_collect_task = None
self.oscillations_history = []
self.current_lims_sample = None
self.__safety_shutter_close_task = None
self.run_without_loop = None
self.run_autoprocessing = None
#wait for the 1st image from detector for 30 seconds by default
self.first_image_timeout = 30
self.mesh = None
self.mesh_num_lines = None
self.mesh_total_nb_frames = None
self.mesh_range = None
self.mesh_center = None
def setControlObjects(self, **control_objects):
self.bl_control = BeamlineControl(**control_objects)
def setBeamlineConfiguration(self, **configuration_parameters):
self.bl_config = BeamlineConfig(**configuration_parameters)
@abc.abstractmethod
@task
def data_collection_hook(self, data_collect_parameters):
pass
@task
def data_collection_end_hook(self, data_collect_parameters):
pass
@task
def set_detector_mode(self, detector_mode):
"""
Descript. :
"""
if self.bl_control.detector is not None:
self.bl_control.detector.set_detector_mode(detector_mode)
@abc.abstractmethod
@task
def set_transmission(self, transmission_percent):
pass
@abc.abstractmethod
@task
def set_wavelength(self, wavelength):
pass
@abc.abstractmethod
@task
def set_resolution(self, new_resolution):
pass
@abc.abstractmethod
@task
def set_energy(self, energy):
pass
@abc.abstractmethod
@task
def close_fast_shutter(self):
pass
@abc.abstractmethod
@task
def move_detector(self, distance):
pass
@abc.abstractmethod
@task
def move_motors(self, motor_position_dict):
return
@abc.abstractmethod
@task
def open_safety_shutter(self):
pass
def safety_shutter_opened(self):
return False
@abc.abstractmethod
@task
def close_safety_shutter(self):
pass
@abc.abstractmethod
@task
def prepare_intensity_monitors(self):
pass
@abc.abstractmethod
@task
def prepare_oscillation(self, start, osc_range, exptime, npass):
"""Should return osc_start and osc_end positions -
gonio should be ready for data collection after this ;
Remember to check for still image if range is too small !
"""
pass
@abc.abstractmethod
@task
def do_oscillation(self, start, end, exptime, npass):
pass
@abc.abstractmethod
@task
def set_detector_filenames(self, frame_number, start, filename, jpeg_full_path, jpeg_thumbnail_full_path):
pass
@abc.abstractmethod
def last_image_saved(self):
pass
@abc.abstractmethod
@task
def prepare_acquisition(self, take_dark, start, osc_range, exptime, npass, number_of_images, comment):
pass
@abc.abstractmethod
@task
def start_acquisition(self, exptime, npass, first_frame):
pass
@abc.abstractmethod
@task
def stop_acquisition(self):
# detector's readout
pass
@abc.abstractmethod
@task
def write_image(self, last_frame):
pass
@abc.abstractmethod
@task
def reset_detector(self):
pass
@abc.abstractmethod
@task
def data_collection_cleanup(self):
pass
@abc.abstractmethod
def get_wavelength(self):
pass
@abc.abstractmethod
def get_detector_distance(self):
pass
@abc.abstractmethod
def get_resolution(self):
pass
@abc.abstractmethod
def get_transmission(self):
pass
@abc.abstractmethod
def get_undulators_gaps(self):
pass
@abc.abstractmethod
def get_resolution_at_corner(self):
pass
@abc.abstractmethod
def get_beam_size(self):
pass
@abc.abstractmethod
def get_slit_gaps(self):
pass
@abc.abstractmethod
def get_beam_shape(self):
pass
@abc.abstractmethod
def get_beam_centre(self):
pass
@abc.abstractmethod
def get_measured_intensity(self):
pass
@abc.abstractmethod
def get_machine_current(self):
pass
@abc.abstractmethod
def get_machine_fill_mode(self):
pass
@abc.abstractmethod
def get_machine_message(self):
pass
@abc.abstractmethod
def get_cryo_temperature(self):
pass
@abc.abstractmethod
def get_flux(self):
"""Return flux in photons/second"""
pass
@abc.abstractmethod
def store_image_in_lims(self, frame, first_frame, last_frame):
pass
@abc.abstractmethod
@task
def generate_image_jpeg(self, filename, jpeg_path, jpeg_thumbnail_path):
pass
def get_sample_info_from_parameters(self, parameters):
"""Returns sample_id, sample_location and sample_code from data collection parameters"""
sample_info = parameters.get("sample_reference")
try:
sample_id = int(sample_info["blSampleId"])
except:
sample_id = None
try:
sample_code = sample_info["code"]
except:
sample_code = None
sample_location = None
try:
sample_container_number = int(sample_info['container_reference'])
except:
pass
else:
try:
vial_number = int(sample_info["sample_location"])
except:
pass
else:
sample_location = (sample_container_number, vial_number)
return sample_id, sample_location, sample_code
def create_directories(self, *args):
for directory in args:
try:
os.makedirs(directory)
except os.error as e:
if e.errno != errno.EEXIST:
raise
def _take_crystal_snapshots(self, number_of_snapshots):
if isinstance(number_of_snapshots, bool):
# backward compatibility, if number_of_snapshots is True|False
if number_of_snapshots:
return self.take_crystal_snapshots(4)
else:
return
if number_of_snapshots:
return self.take_crystal_snapshots(number_of_snapshots)
@abc.abstractmethod
@task
def take_crystal_snapshots(self, number_of_snapshots):
pass
@abc.abstractmethod
def set_helical(self, helical_on):
pass
@abc.abstractmethod
def set_helical_pos(self, helical_pos):
pass
def prepare_wedges_to_collect(self, start, nframes, osc_range, subwedge_size=1, overlap=0):
if overlap == 0:
wedge_sizes_list = [nframes//subwedge_size]*subwedge_size
else:
wedge_sizes_list = [subwedge_size]*(nframes//subwedge_size)
remaining_frames = nframes % subwedge_size
if remaining_frames:
wedge_sizes_list.append(remaining_frames)
wedges_to_collect = []
for wedge_size in wedge_sizes_list:
orig_start = start
wedges_to_collect.append((start, wedge_size))
start += wedge_size*osc_range - overlap
return wedges_to_collect
def update_oscillations_history(self, data_collect_parameters):
sample_id, sample_code, sample_location = self.get_sample_info_from_parameters(data_collect_parameters)
self.oscillations_history.append((sample_id, sample_code, sample_location, data_collect_parameters))
return len(self.oscillations_history), sample_id, sample_code, sample_location
@abc.abstractmethod
def get_archive_directory(self, directory):
pass
@abc.abstractmethod
def prepare_input_files(self, files_directory, prefix, run_number, process_directory):
"""Return XDS input file directory"""
pass
@abc.abstractmethod
@task
def write_input_files(self, collection_id):
pass
def execute_collect_without_loop(self, data_collect_parameters):
return
def do_collect(self, owner, data_collect_parameters):
if self.__safety_shutter_close_task is not None:
self.__safety_shutter_close_task.kill()
logging.getLogger("user_level_log").info("Closing fast shutter")
self.close_fast_shutter()
# reset collection id on each data collect
self.collection_id = None
# Preparing directory path for images and processing files
# creating image file template and jpegs files templates
file_parameters = data_collect_parameters["fileinfo"]
file_parameters["suffix"] = self.bl_config.detector_fileext
image_file_template = "%(prefix)s_%(run_number)s_%%04d.%(suffix)s" % file_parameters
file_parameters["template"] = image_file_template
archive_directory = self.get_archive_directory(file_parameters["directory"])
data_collect_parameters["archive_dir"] = archive_directory
if archive_directory:
jpeg_filename="%s.jpeg" % os.path.splitext(image_file_template)[0]
thumb_filename="%s.thumb.jpeg" % os.path.splitext(image_file_template)[0]
jpeg_file_template = os.path.join(archive_directory, jpeg_filename)
jpeg_thumbnail_file_template = os.path.join(archive_directory, thumb_filename)
else:
jpeg_file_template = None
jpeg_thumbnail_file_template = None
# database filling
if self.bl_control.lims:
data_collect_parameters["collection_start_time"] = time.strftime("%Y-%m-%d %H:%M:%S")
if self.bl_control.machine_current is not None:
logging.getLogger("user_level_log").info("Getting synchrotron filling mode")
data_collect_parameters["synchrotronMode"] = self.get_machine_fill_mode()
data_collect_parameters["status"] = "failed"
logging.getLogger("user_level_log").info("Storing data collection in LIMS")
(self.collection_id, detector_id) = \
self.bl_control.lims.store_data_collection(data_collect_parameters, self.bl_config)
data_collect_parameters['collection_id'] = self.collection_id
if detector_id:
data_collect_parameters['detector_id'] = detector_id
# Creating the directory for images and processing information
logging.getLogger("user_level_log").info("Creating directory for images and processing")
self.create_directories(file_parameters['directory'], file_parameters['process_directory'])
self.xds_directory, self.mosflm_directory, self.hkl2000_directory = self.prepare_input_files(file_parameters["directory"], file_parameters["prefix"], file_parameters["run_number"], file_parameters['process_directory'])
data_collect_parameters['xds_dir'] = self.xds_directory
logging.getLogger("user_level_log").info("Getting sample info from parameters")
sample_id, sample_location, sample_code = self.get_sample_info_from_parameters(data_collect_parameters)
data_collect_parameters['blSampleId'] = sample_id
if self.bl_control.sample_changer is not None:
try:
data_collect_parameters["actualSampleBarcode"] = \
self.bl_control.sample_changer.getLoadedSample().getID()
data_collect_parameters["actualContainerBarcode"] = \
self.bl_control.sample_changer.getLoadedSample().getContainer().getID()
logging.getLogger("user_level_log").info("Getting loaded sample coords")
basket, vial = self.bl_control.sample_changer.getLoadedSample().getCoords()
data_collect_parameters["actualSampleSlotInContainer"] = vial
data_collect_parameters["actualContainerSlotInSC"] = basket
except:
data_collect_parameters["actualSampleBarcode"] = None
data_collect_parameters["actualContainerBarcode"] = None
else:
data_collect_parameters["actualSampleBarcode"] = None
data_collect_parameters["actualContainerBarcode"] = None
centring_info = {}
try:
logging.getLogger("user_level_log").info("Getting centring status")
centring_status = self.diffractometer().getCentringStatus()
except:
pass
else:
centring_info = dict(centring_status)
#Save sample centring positions
positions_str = ""
motors = centring_info.get("motors", {}) #.update(centring_info.get("extraMotors", {}))
motors_to_move_before_collect = data_collect_parameters.setdefault("motors", {})
for motor, pos in motors.iteritems():
if motor in motors_to_move_before_collect:
continue
motors_to_move_before_collect[motor]=pos
current_diffractometer_position = self.diffractometer().getPositions()
for motor in motors_to_move_before_collect.keys():
if motors_to_move_before_collect[motor] is None:
del motors_to_move_before_collect[motor]
try:
if current_diffractometer_position[motor] is not None:
positions_str += "%s=%f " % (motor, current_diffractometer_position[motor])
except:
pass
# this is for the LIMS
positions_str += " ".join([motor+("=%f" % pos) for motor, pos in motors_to_move_before_collect.iteritems()])
data_collect_parameters['actualCenteringPosition'] = positions_str
self.move_motors(motors_to_move_before_collect)
# take snapshots, then assign centring status (which contains images) to centring_info variable
take_snapshots = data_collect_parameters.get("take_snapshots", False)
if take_snapshots:
logging.getLogger("user_level_log").info("Taking sample snapshosts")
self._take_crystal_snapshots(take_snapshots)
centring_info = self.bl_control.diffractometer.getCentringStatus()
# move *again* motors, since taking snapshots may change positions
logging.getLogger("user_level_log").info("Moving motors: %r", motors_to_move_before_collect)
self.move_motors(motors_to_move_before_collect)
if self.bl_control.lims:
try:
if self.current_lims_sample:
self.current_lims_sample['lastKnownCentringPosition'] = positions_str
logging.getLogger("user_level_log").info("Updating sample information in LIMS")
self.bl_control.lims.update_bl_sample(self.current_lims_sample)
except:
logging.getLogger("HWR").exception("Could not update sample information in LIMS")
if centring_info.get('images'):
# Save snapshots
snapshot_directory = self.get_archive_directory(file_parameters["directory"])
try:
logging.getLogger("user_level_log").info("Creating snapshosts directory: %r", snapshot_directory)
self.create_directories(snapshot_directory)
except:
logging.getLogger("HWR").exception("Error creating snapshot directory")
else:
snapshot_i = 1
snapshots = []
for img in centring_info["images"]:
img_phi_pos = img[0]
img_data = img[1]
snapshot_filename = "%s_%s_%s.snapshot.jpeg" % (file_parameters["prefix"],
file_parameters["run_number"],
snapshot_i)
full_snapshot = os.path.join(snapshot_directory,
snapshot_filename)
try:
f = open(full_snapshot, "w")
logging.getLogger("user_level_log").info("Saving snapshot %d", snapshot_i)
f.write(img_data)
except:
logging.getLogger("HWR").exception("Could not save snapshot!")
try:
f.close()
except:
pass
data_collect_parameters['xtalSnapshotFullPath%i' % snapshot_i] = full_snapshot
snapshots.append(full_snapshot)
snapshot_i+=1
try:
data_collect_parameters["centeringMethod"] = centring_info['method']
except:
data_collect_parameters["centeringMethod"] = None
if self.bl_control.lims:
try:
logging.getLogger("user_level_log").info("Updating data collection in LIMS")
if 'kappa' in data_collect_parameters['actualCenteringPosition']:
data_collect_parameters['oscillation_sequence'][0]['kappaStart'] = current_diffractometer_position['kappa']
data_collect_parameters['oscillation_sequence'][0]['phiStart'] = current_diffractometer_position['kappa_phi']
self.bl_control.lims.update_data_collection(data_collect_parameters)
except:
logging.getLogger("HWR").exception("Could not update data collection in LIMS")
oscillation_parameters = data_collect_parameters["oscillation_sequence"][0]
sample_id = data_collect_parameters['blSampleId']
subwedge_size = oscillation_parameters.get("reference_interval", 1)
#if data_collect_parameters["shutterless"]:
# subwedge_size = 1
#else:
# subwedge_size = oscillation_parameters["number_of_images"]
wedges_to_collect = self.prepare_wedges_to_collect(oscillation_parameters["start"],
oscillation_parameters["number_of_images"],
oscillation_parameters["range"],
subwedge_size,
oscillation_parameters["overlap"])
nframes = sum([wedge_size for _, wedge_size in wedges_to_collect])
#Added exposure time for ProgressBarBrick.
#Extra time for each collection needs to be added (in this case 0.04)
self.emit("collectNumberOfFrames", nframes, oscillation_parameters["exposure_time"] + 0.04)
start_image_number = oscillation_parameters["start_image_number"]
last_frame = start_image_number + nframes - 1
if data_collect_parameters["skip_images"]:
for start, wedge_size in wedges_to_collect[:]:
filename = image_file_template % start_image_number
file_location = file_parameters["directory"]
file_path = os.path.join(file_location, filename)
if os.path.isfile(file_path):
logging.info("Skipping existing image %s", file_path)
del wedges_to_collect[0]
start_image_number += wedge_size
nframes -= wedge_size
else:
# images have to be consecutive
break
if nframes == 0:
return
# data collection
self.first_image_timeout = 30+oscillation_parameters["exposure_time"]
self.data_collection_hook(data_collect_parameters)
if 'transmission' in data_collect_parameters:
logging.getLogger("user_level_log").info("Setting transmission to %f", data_collect_parameters["transmission"])
self.set_transmission(data_collect_parameters["transmission"])
if 'wavelength' in data_collect_parameters:
logging.getLogger("user_level_log").info("Setting wavelength to %f", data_collect_parameters["wavelength"])
self.set_wavelength(data_collect_parameters["wavelength"])
elif 'energy' in data_collect_parameters:
logging.getLogger("user_level_log").info("Setting energy to %f", data_collect_parameters["energy"])
self.set_energy(data_collect_parameters["energy"])
if 'resolution' in data_collect_parameters:
resolution = data_collect_parameters["resolution"]["upper"]
logging.getLogger("user_level_log").info("Setting resolution to %f", resolution)
self.set_resolution(resolution)
elif 'detdistance' in oscillation_parameters:
logging.getLogger("user_level_log").info("Moving detector to %f", data_collect_parameters["detdistance"])
self.move_detector(oscillation_parameters["detdistance"])
# 0: software binned, 1: unbinned, 2:hw binned
self.set_detector_mode(data_collect_parameters["detector_mode"])
with cleanup(self.data_collection_cleanup):
if not self.safety_shutter_opened():
logging.getLogger("user_level_log").info("Opening safety shutter")
self.open_safety_shutter(timeout=10)
logging.getLogger("user_level_log").info("Preparing intensity monitors")
self.prepare_intensity_monitors()
frame = start_image_number
osc_range = oscillation_parameters["range"]
exptime = oscillation_parameters["exposure_time"]
npass = oscillation_parameters["number_of_passes"]
# update LIMS
if self.bl_control.lims:
try:
logging.getLogger("user_level_log").info("Gathering data for LIMS update")
data_collect_parameters["flux"] = self.get_flux()
data_collect_parameters["flux_end"] = data_collect_parameters["flux"]
data_collect_parameters["wavelength"]= self.get_wavelength()
data_collect_parameters["detectorDistance"] = self.get_detector_distance()
data_collect_parameters["resolution"] = self.get_resolution()
data_collect_parameters["transmission"] = self.get_transmission()
beam_centre_x, beam_centre_y = self.get_beam_centre()
data_collect_parameters["xBeam"] = beam_centre_x
data_collect_parameters["yBeam"] = beam_centre_y
und = self.get_undulators_gaps()
i = 1
for jj in self.bl_config.undulators:
key = jj.type
if und.has_key(key):
data_collect_parameters["undulatorGap%d" % (i)] = und[key]
i += 1
data_collect_parameters["resolutionAtCorner"] = self.get_resolution_at_corner()
beam_size_x, beam_size_y = self.get_beam_size()
data_collect_parameters["beamSizeAtSampleX"] = beam_size_x
data_collect_parameters["beamSizeAtSampleY"] = beam_size_y
data_collect_parameters["beamShape"] = self.get_beam_shape()
hor_gap, vert_gap = self.get_slit_gaps()
data_collect_parameters["slitGapHorizontal"] = hor_gap
data_collect_parameters["slitGapVertical"] = vert_gap
logging.getLogger("user_level_log").info("Updating data collection in LIMS")
self.bl_control.lims.update_data_collection(data_collect_parameters, wait=True)
logging.getLogger("user_level_log").info("Done updating data collection in LIMS")
except:
logging.getLogger("HWR").exception("Could not store data collection into LIMS")
if self.bl_control.lims and self.bl_config.input_files_server:
logging.getLogger("user_level_log").info("Asking for input files writing")
self.write_input_files(self.collection_id, wait=False)
# at this point input files should have been written
# TODO aggree what parameters will be sent to this function
if data_collect_parameters.get("processing", False)=="True":
self.trigger_auto_processing("before",
self.xds_directory,
data_collect_parameters["EDNA_files_dir"],
data_collect_parameters["anomalous"],
data_collect_parameters["residues"],
data_collect_parameters["do_inducedraddam"],
data_collect_parameters.get("sample_reference", {}).get("spacegroup", ""),
data_collect_parameters.get("sample_reference", {}).get("cell", ""))
if self.run_without_loop:
self.execute_collect_without_loop(data_collect_parameters)
else:
for start, wedge_size in wedges_to_collect:
logging.getLogger("user_level_log").info("Preparing acquisition, start=%f, wedge size=%d", start, wedge_size)
self.prepare_acquisition(1 if data_collect_parameters.get("dark", 0) else 0,
start,
osc_range,
exptime,
npass,
wedge_size,
data_collect_parameters["comment"])
data_collect_parameters["dark"] = 0
i = 0
j = wedge_size
while j > 0:
frame_start = start+i*osc_range
i+=1
filename = image_file_template % frame
try:
jpeg_full_path = jpeg_file_template % frame
jpeg_thumbnail_full_path = jpeg_thumbnail_file_template % frame
except:
jpeg_full_path = None
jpeg_thumbnail_full_path = None
file_location = file_parameters["directory"]
file_path = os.path.join(file_location, filename)
self.set_detector_filenames(frame, frame_start, str(file_path), str(jpeg_full_path), str(jpeg_thumbnail_full_path))
osc_start, osc_end = self.prepare_oscillation(frame_start, osc_range, exptime, npass)
with error_cleanup(self.reset_detector):
self.start_acquisition(exptime, npass, j == wedge_size)
self.do_oscillation(osc_start, osc_end, exptime, npass)
self.stop_acquisition()
self.write_image(j == 1)
# Store image in lims
if self.bl_control.lims:
if self.store_image_in_lims(frame, j == wedge_size, j == 1):
lims_image={'dataCollectionId': self.collection_id,
'fileName': filename,
'fileLocation': file_location,
'imageNumber': frame,
'measuredIntensity': self.get_measured_intensity(),
'synchrotronCurrent': self.get_machine_current(),
'machineMessage': self.get_machine_message(),
'temperature': self.get_cryo_temperature()}
if archive_directory:
lims_image['jpegFileFullPath'] = jpeg_full_path
lims_image['jpegThumbnailFileFullPath'] = jpeg_thumbnail_full_path
try:
self.bl_control.lims.store_image(lims_image)
except:
logging.getLogger("HWR").exception("Could not store store image in LIMS")
self.generate_image_jpeg(str(file_path), str(jpeg_full_path), str(jpeg_thumbnail_full_path),wait=False)
if data_collect_parameters.get("processing", False)=="True":
self.trigger_auto_processing("image",
self.xds_directory,
data_collect_parameters["EDNA_files_dir"],
data_collect_parameters["anomalous"],
data_collect_parameters["residues"],
data_collect_parameters["do_inducedraddam"],
data_collect_parameters.get("sample_reference", {}).get("spacegroup", ""),
data_collect_parameters.get("sample_reference", {}).get("cell", ""))
if data_collect_parameters.get("shutterless"):
with gevent.Timeout(self.first_image_timeout, RuntimeError("Timeout waiting for detector trigger, no image taken")):
while self.last_image_saved() == 0:
time.sleep(exptime)
last_image_saved = self.last_image_saved()
if last_image_saved < wedge_size:
time.sleep(exptime*wedge_size/100.0)
last_image_saved = self.last_image_saved()
frame = max(start_image_number+1, start_image_number+last_image_saved-1)
self.emit("collectImageTaken", frame)
j = wedge_size - last_image_saved
else:
j -= 1
self.emit("collectImageTaken", frame)
frame += 1
if j == 0:
break
# data collection done
self.data_collection_end_hook(data_collect_parameters)
@task
def loop(self, owner, data_collect_parameters_list):
failed_msg = "Data collection failed!"
failed = True
collections_analyse_params = []
try:
self.emit("collectReady", (False, ))
self.emit("collectStarted", (owner, 1))
for data_collect_parameters in data_collect_parameters_list:
logging.debug("collect parameters = %r", data_collect_parameters)
failed = False
try:
# emit signals to make bricks happy
osc_id, sample_id, sample_code, sample_location = self.update_oscillations_history(data_collect_parameters)
self.emit('collectOscillationStarted', (owner, sample_id, sample_code, sample_location, data_collect_parameters, osc_id))
data_collect_parameters["status"]='Running'
# now really start collect sequence
self.do_collect(owner, data_collect_parameters)
except:
failed = True
exc_type, exc_value, exc_tb = sys.exc_info()
logging.exception("Data collection failed")
data_collect_parameters["status"] = 'Data collection failed!' #Message to be stored in LIMS
failed_msg = 'Data collection failed!\n%s' % exc_value
self.emit("collectOscillationFailed", (owner, False, failed_msg, self.collection_id, osc_id))
else:
data_collect_parameters["status"]='Data collection successful'
try:
if data_collect_parameters.get("processing", False)=="True":
self.trigger_auto_processing("after",
self.xds_directory,
data_collect_parameters["EDNA_files_dir"],
data_collect_parameters["anomalous"],
data_collect_parameters["residues"],
data_collect_parameters["do_inducedraddam"],
data_collect_parameters.get("sample_reference", {}).get("spacegroup", ""),
data_collect_parameters.get("sample_reference", {}).get("cell", ""))
except:
pass
else:
collections_analyse_params.append((self.collection_id,
self.xds_directory,
data_collect_parameters["EDNA_files_dir"],
data_collect_parameters["anomalous"],
data_collect_parameters["residues"],
"reference_interval" in data_collect_parameters["oscillation_sequence"][0],
data_collect_parameters["do_inducedraddam"]))
if self.bl_control.lims:
data_collect_parameters["flux_end"]=self.get_flux()
try:
self.bl_control.lims.update_data_collection(data_collect_parameters)
except:
logging.getLogger("HWR").exception("Could not store data collection into LIMS")
if failed:
# if one dc fails, stop the whole loop
break
else:
self.emit("collectOscillationFinished", (owner, True, data_collect_parameters["status"], self.collection_id, osc_id, data_collect_parameters))
try:
self.__safety_shutter_close_task = gevent.spawn_later(10*60, self.close_safety_shutter, timeout=10)
except:
logging.exception("Could not close safety shutter")
finally:
self.emit("collectEnded", owner, not failed, failed_msg if failed else "Data collection successful")
self.emit("collectReady", (True, ))
def collect_without_loop(self, data_collect_parameters_list):
in_multicollect = len(data_collect_parameters_list) > 1
collections_analyse_params = []
self.emit("collectReady", (False, ))
#self.emit("collectStarted", (self.owner, 1))
total_frames = 0
total_time_sec = 0
# in principle data_collect_parameters_list always has one element
# Two options: rename this module to AbstractCollect
# or fully implement multicollect
for data_collect_parameters in data_collect_parameters_list:
total_frames = total_frames + data_collect_parameters['oscillation_sequence'][0]['number_of_images']
total_time_sec = total_time_sec + total_frames + 2* \
(data_collect_parameters['oscillation_sequence'][0]['exposure_time'] + 0.02)
self.emit("collectOverallFramesTime", (total_frames, int(total_time_sec)))
for data_collect_parameters in data_collect_parameters_list:
self.actual_data_collect_parameters = data_collect_parameters
self.osc_id, sample_id, sample_code, sample_location = \
self.update_oscillations_history(self.actual_data_collect_parameters)
self.emit('collectOscillationStarted', (self.owner, sample_id, \
sample_code, sample_location, self.actual_data_collect_parameters, self.osc_id))
self.actual_data_collect_parameters["status"]='Running'
self.do_collect(self.owner, self.actual_data_collect_parameters)
def collect(self, owner, data_collect_parameters_list): #, finished_callback=None):
if self.run_without_loop:
self.owner = owner
self.ready_event.clear()
self.data_collect_task = gevent.spawn(self.collect_without_loop,
data_collect_parameters_list)
self.ready_event.wait()
self.ready_event.clear()
else:
self.data_collect_task = self.loop(owner, data_collect_parameters_list, wait = False)
return self.data_collect_task
#TODO: rename to stop_collect
def stopCollect(self, owner):
if self.data_collect_task is not None:
self.data_collect_task.kill(block = False)
"""
processDataScripts
Description : executes a script after the data collection has finished
Type : method
"""
def trigger_auto_processing(self, process_event, xds_dir, EDNA_files_dir=None, anomalous=None, residues=200, do_inducedraddam=False, spacegroup=None, cell=None):
# quick fix for anomalous, do_inducedraddam... passed as a string!!!
# (comes from the queue)
if type(anomalous) == types.StringType:
anomalous = anomalous == "True"
if type(do_inducedraddam) == types.StringType:
do_inducedraddam = do_inducedraddam == "True"
if type(residues) == types.StringType:
try:
residues = int(residues)
except:
residues = 200
# residues = zero should be interpreted as if no value was provided
# use default of 200
if residues == 0:
residues = 200
processAnalyseParams = {}
processAnalyseParams['EDNA_files_dir'] = EDNA_files_dir
try:
if type(xds_dir) == types.ListType:
processAnalyseParams["collections_params"] = xds_dir
else:
processAnalyseParams['datacollect_id'] = self.collection_id
processAnalyseParams['xds_dir'] = xds_dir
processAnalyseParams['anomalous'] = anomalous
processAnalyseParams['residues'] = residues
processAnalyseParams["spacegroup"]=spacegroup
processAnalyseParams["cell"]=cell
except Exception as msg:
logging.getLogger().exception("DataCollect:processing: %r" % str(msg))
else:
#logging.info("AUTO PROCESSING: %s, %s, %s, %s, %s, %s, %r, %r", process_event, EDNA_files_dir, anomalous, residues, do_inducedraddam, spacegroup, cell)
try:
autoprocessing.start(self["auto_processing"], process_event, processAnalyseParams)
except:
logging.getLogger().exception("Error starting processing")
if process_event=="after" and do_inducedraddam:
try:
autoprocessing.startInducedRadDam(processAnalyseParams)
except:
logging.exception("Error starting induced rad.dam")
def set_run_autoprocessing(self, status):
pass
# specifies the next scan will be a mesh scan
def set_mesh(self, mesh_on):
self.mesh = mesh_on
def set_mesh_scan_parameters(self, num_lines, total_nb_frames, mesh_center_param, mesh_range_param):