-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoniometer.py
executable file
·1312 lines (1042 loc) · 51.8 KB
/
goniometer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import gevent
import numpy as np
from math import sin, cos, atan2, radians, sqrt
import logging
import traceback
import time
import datetime
import copy
from scipy.optimize import minimize
try:
import lmfit
from lmfit import fit_report
except ImportError:
logging.warning(
"Could not lmfit minimization library, "
+ "refractive model centring will not work."
)
try:
if sys.version_info.major == 3:
import tango
else:
import PyTango as tango
except ImportError:
print('goniometer could not import tango')
from md2_mockup import md2_mockup
def get_voxel_calibration(vertical_step, horizontal_step):
calibration = np.ones((3,))
calibration[0] = horizontal_step
calibration[1:] = vertical_step
return calibration
def get_origin(parameters, position_key='reference_position'):
p = parameters[position_key]
o = np.array([p['CentringX'], p['CentringY'], p['AlignmentY'], p['AlignmentZ']])
return o
def get_points_in_goniometer_frame(points_px, calibration, origin, center=np.array([160, 256, 256]), directions=np.array([-1,-1,1]), order=[1,2,0]):
points_mm = ((points_px-center)*calibration*directions)[:, order] + origin
return points_mm
def get_points_in_camera_frame(points_mm, calibration, origin, center=np.array([160, 256, 256]), directions=np.array([-1, -1, 1]), order=[1,2,0]):
mm = points_mm - origin
mm = mm[:, order[::-1]]
mm *= directions
mm /= calibration
points_px = mm + center
return points_px
def add_shift(position, shift, keys=['CentringX', 'CentringY', 'AlignmentY', 'AlignmentZ']):
shifted_position = {}
for k, key in enumerate(keys):
shifted_position[key] = position[key] + shift[k]
return shifted_position
def get_shift(position, reference, keys=['CentringX', 'CentringY', 'AlignmentY', 'AlignmentZ']):
p = get_vector_from_position(position, keys=keys)
r = get_vector_from_position(reference, keys=keys)
shift = p - r
return shift
def get_position_from_vector(v, keys=['CentringX', 'CentringY', 'AlignmentY', 'AlignmentZ', 'AlignmentX', 'Kappa', 'Phi']):
return dict([(key, value) for key, value in zip(keys, v)])
def get_vector_from_position(p, keys=['CentringX', 'CentringY', 'AlignmentY', 'AlignmentZ', 'AlignmentX', 'Kappa', 'Phi']):
return np.array([p[key] for key in keys if key in p])
def get_distance(p1, p2, keys=['CentringX', 'CentringY']):
return np.linalg.norm(get_vector_from_position(p1, keys=keys) - get_vector_from_position(p2, keys=keys))
def get_reduced_point(p, keys=['CentringX', 'CentringY']):
return dict([(key, value) for key, value in p.items() if key in keys])
def copy_position(p):
new_position = {}
for key in p:
new_position[key] = p[key]
return position
def get_point_between(p1, p2, keys=['CentringX', 'CentringY', 'AlignmentY', 'AlignmentZ']):
v1 = get_vector_from_position(p1, keys=keys)
v2 = get_vector_from_position(p2, keys=keys)
v = v1 + (v2-v1)/2.
p = get_position_from_vector(v, keys=keys)
return p
# minikappa translational offsets
def circle_model(angle, center, amplitude, phase):
return center + amplitude*np.cos(angle - phase)
def line_and_circle_model(kappa, intercept, growth, amplitude, phase):
return intercept + kappa*growth + amplitude * np.sin(np.radians(kappa) - phase)
def amplitude_y_model(kappa, amplitude, amplitude_residual, amplitude_residual2):
return amplitude * np.sin(0.5*kappa) + amplitude_residual * np.sin(kappa) + amplitude_residual2 * np.sin(2*kappa)
def get_alignmentz_offset(kappa, phi):
return 0
def get_alignmenty_offset(kappa, phi,
center_center=-2.2465475,
center_amplitude=0.3278655,
center_phase=np.radians(269.3882546),
amplitude_amplitude=0.47039019,
amplitude_amplitude_residual=0.01182333,
amplitude_amplitude_residual2=0.00581796,
phase_intercept=-4.7510392,
phase_growth=0.5056157,
phase_amplitude=-2.6508604,
phase_phase=np.radians(14.9266433)):
center = circle_model(kappa, center_center, center_amplitude, center_phase)
amplitude = amplitude_y_model(kappa, amplitude_amplitude, amplitude_amplitude_residual, amplitude_amplitude_residual2)
phase = line_and_circle_model(kappa, phase_intercept, phase_growth, phase_amplitude, phase_phase)
phase = np.mod(phase, 180)
alignmenty_offset = circle_model(phi, center, amplitude, np.radians(phase))
return alignmenty_offset
def amplitude_cx_model(kappa, *params):
kappa = np.mod(kappa, 2*np.pi)
powers = []
if type(params) == tuple and len(params) < 2:
params = params[0]
else:
params = np.array(params)
if len(params.shape) > 1:
params = params[0]
params = np.array(params)
params = params[:-2]
amplitude_residual, phase_residual = params[-2:]
kappa = np.array(kappa)
for k in range(len(params)):
powers.append(kappa**k)
powers = np.array(powers)
return np.dot(powers.T, params) + amplitude_residual * np.sin(2*kappa - phase_residual)
def amplitude_cx_residual_model(kappa, amplitude, phase):
return amplitude * np.sin(2*kappa - phase)
def phase_error_model(kappa, amplitude, phase, frequency):
return amplitude * np.sin(frequency*np.radians(kappa) - phase)
def get_centringx_offset(kappa, phi,
center_center=0.5955864,
center_amplitude=0.7738802,
center_phase=np.radians(222.1041400),
amplitude_p1=0.63682813,
amplitude_p2=0.02332819,
amplitude_p3=-0.02999456,
amplitude_p4=0.00366993,
amplitude_residual=0.00592784,
amplitude_phase_residual=1.82492612,
phase_intercept=25.8526552,
phase_growth=1.0919045,
phase_amplitude=-12.4088622,
phase_phase=np.radians(96.7545812),
phase_error_amplitude=1.23428124,
phase_error_phase=0.83821785,
phase_error_frequency=2.74178863,
amplitude_error_amplitude=0.00918566,
amplitude_error_phase=4.33422268):
amplitude_params = [amplitude_p1, amplitude_p2, amplitude_p3, amplitude_p4, amplitude_residual, amplitude_phase_residual]
center = circle_model(kappa, center_center, center_amplitude, center_phase)
amplitude = amplitude_cx_model(kappa, *amplitude_params)
amplitude_error = amplitude_cx_residual_model(kappa, amplitude_error_amplitude, amplitude_error_phase)
amplitude -= amplitude_error
phase = line_and_circle_model(kappa, phase_intercept, phase_growth, phase_amplitude, phase_phase)
phase_error = phase_error_model(kappa, phase_error_amplitude, phase_error_phase, phase_error_frequency)
phase -= phase_error
phase = np.mod(phase, 180)
centringx_offset = circle_model(phi, center, amplitude, np.radians(phase))
return centringx_offset
def amplitude_cy_model(kappa, center, amplitude, phase, amplitude_residual, phase_residual):
return center + amplitude*np.sin(kappa - phase) + amplitude_residual*np.sin(kappa*2 - phase_residual)
def get_centringy_offset(kappa, phi,
center_center=0.5383092,
center_amplitude=-0.7701891,
center_phase=np.radians(137.6146006),
amplitude_center=0.56306051,
amplitude_amplitude=-0.06911649,
amplitude_phase=0.77841959,
amplitude_amplitude_residual=0.03132799,
amplitude_phase_residual=-0.12249943,
phase_intercept=146.9185176,
phase_growth=0.8985232,
phase_amplitude=-17.5015172,
phase_phase=-409.1764969,
phase_error_amplitude=1.18820494,
phase_error_phase=4.12663751,
phase_error_frequency=3.11751387):
center = circle_model(kappa, center_center, center_amplitude, center_phase) #3
amplitude = amplitude_cy_model(kappa, amplitude_center, amplitude_amplitude, amplitude_phase, amplitude_amplitude_residual, amplitude_phase_residual) #5
phase = line_and_circle_model(kappa, phase_intercept, phase_growth, phase_amplitude, phase_phase) #4
phase_error = phase_error_model(kappa, phase_error_amplitude, phase_error_phase, phase_error_frequency) #3
phase -= phase_error
phase = np.mod(phase, 180)
centringy_offset = circle_model(phi, center, amplitude, np.radians(phase))
return centringy_offset
class goniometer(object):
motorsNames = ['AlignmentX',
'AlignmentY',
'AlignmentZ',
'CentringX',
'CentringY']
motorShortNames = ['PhiX', 'PhiY', 'PhiZ', 'SamX', 'SamY']
mxcubeShortNames = ['phix', 'phiy', 'phiz', 'sampx', 'sampy']
shortFull = dict(list(zip(motorShortNames, motorsNames)))
phiy_direction=-1.
phiz_direction=1.
motor_name_mapping = [
("AlignmentX", "phix"),
("AlignmentY", "phiy"),
("AlignmentZ", "phiz"),
("CentringX", "sampx"),
("CentringY", "sampy"),
("Omega", "phi"),
("Kappa", "kappa"),
("Phi", "kappa_phi"),
("beam_x", "beam_x"),
("beam_y", "beam_y"),
]
#2019
#kappa_direction=[0.29636375, 0.29377944, -0.90992064],
#kappa_position=[-0.30655466, -0.3570731, 0.52893628],
#phi_direction=[0.03149443, 0.03216924, -0.99469729],
#phi_position=[-0.01467116, -0.08069945, 0.46818622],
# 2023-11
#kappa_direction=[0.2857927293557859, 0.29825779103438044, -0.9106980618759559],
#kappa_position=[0.05983227148328028, -0.17418159369049926, -0.3931170045291165],
#phi_direction=[0.05080273994228953, -0.006002697566495966, -1.0134508568340999],
#phi_position=[0.21993931768594516, -0.03147698225694694, -2.0073121331928205],
# 2023-12
#kappa_direction=[0.2699214563788776, 0.2838993348700071, -1.0018455551762098],
#kappa_position=[0.7765041345864001, 0.5561954243441296, -2.6141365642906167],
#phi_direction=[0.017981988387908307, 0.04904500982612193, -1.1478709640779396],
#phi_position=[0.2813575203934254, -0.030698833411830308, -2.584128291492474],
def __init__(self, monitor_sleep_time=0.05,
kappa_direction=[0.2857927293557859, 0.29825779103438044, -0.9106980618759559],
kappa_position=[0.05983227148328028, -0.17418159369049926, -0.3931170045291165],
phi_direction=[0.05080273994228953, -0.006002697566495966, -1.0134508568340999],
phi_position=[0.21993931768594516, -0.03147698225694694, -2.0073121331928205],
align_direction=[0, 0, -1]):
try:
self.md2 = tango.DeviceProxy('i11-ma-cx1/ex/md2')
except:
from md2_mockup import md2_mockup
self.md2 = md2_mockup()
self.monitor_sleep_time = monitor_sleep_time
self.observe = None
self.kappa_axis = self.get_axis(kappa_direction, kappa_position)
self.phi_axis = self.get_axis(phi_direction, phi_position)
self.align_direction = align_direction
#self.redis = redis.StrictRedis()
self.observation_fields = ['chronos', 'Omega']
self.observations = []
self.centringx_direction = -1.
self.centringy_direction = +1.
self.alignmenty_direction = -1.
self.alignmentz_direction = +1.
self.md2_to_mxcube = dict(
[(key, value) for key, value in self.motor_name_mapping]
)
self.mxcube_to_md2 = dict(
[(value, key) for key, value in self.motor_name_mapping]
)
def set_scan_start_angle(self, scan_start_angle):
self.md2.scanstartangle = scan_start_angle
def get_scan_start_angle(self):
return self.md2.scanstartangle
def set_scan_range(self, scan_range):
self.md2.scanrange = scan_range
def get_scan_range(self):
return self.md2.scanrange
def set_scan_exposure_time(self, scan_exposure_time):
self.md2.scanexposuretime = scan_exposure_time
def get_scan_exposure_time(self):
return self.md2.scanexposuretime
def set_scan_number_of_frames(self, scan_number_of_frames):
try:
if self.get_scan_number_of_frames() != scan_number_of_frames:
self.md2.scannumberofframes = scan_number_of_frames
except:
logging.info(traceback.format_exc())
def get_scan_number_of_frames(self):
return self.md2.scannumberofframes
def set_collect_phase(self):
return self.set_data_collection_phase()
def abort(self):
return self.md2.abort()
def start_scan(self, number_of_attempts=3, wait=False):
tried = 0
while tried < number_of_attempts:
tried += 1
try:
self.task_id = self.md2.startscan()
break
except:
self.wait()
if wait:
self.wait_for_task_to_finish(self.task_id)
return self.task_id
def omega_scan(self, start_angle, scan_range, exposure_time, frame_number=1, number_of_passes=1, number_of_attempts=7, wait=True):
start_angle = '%6.4f' % start_angle
scan_range = '%6.4f' % scan_range
exposure_time = '%6.4f' % exposure_time
frame_number = '%d' % frame_number
number_of_passes = '%d' % number_of_passes
parameters = [frame_number, start_angle, scan_range, exposure_time, number_of_passes]
tried = 0
self.wait()
while tried < number_of_attempts:
tried += 1
try:
self.task_id = self.md2.startscanex(parameters)
break
except:
self.wait()
if wait:
self.wait_for_task_to_finish(self.task_id)
return self.task_id
def vertical_helical_scan(self, vertical_scan_length, position, scan_start_angle, scan_range, scan_exposure_time, wait=True):
start = {}
stop = {}
for motor in position:
if motor == 'AlignmentZ':
start[motor] = position[motor] + vertical_scan_length/2.
stop[motor] = position[motor] - vertical_scan_length/2.
else:
start[motor] = position[motor]
stop[motor] = position[motor]
return self.helical_scan(start, stop, scan_start_angle, scan_range, scan_exposure_time, wait=wait)
def helical_scan(self, start, stop, scan_start_angle, scan_range, scan_exposure_time, number_of_attempts=7, wait=True, sleeptime=0.5):
scan_start_angle = '%6.4f' % (scan_start_angle % 360., )
scan_range = '%6.4f' % scan_range
scan_exposure_time = '%6.4f' % scan_exposure_time
start_z = '%6.4f' % start['AlignmentZ']
start_y = '%6.4f' % start['AlignmentY']
stop_z = '%6.4f' % stop['AlignmentZ']
stop_y = '%6.4f' % stop['AlignmentY']
start_cx = '%6.4f' % start['CentringX']
start_cy = '%6.4f' % start['CentringY']
stop_cx = '%6.4f' % stop['CentringX']
stop_cy = '%6.4f' % stop['CentringY']
parameters = [scan_start_angle, scan_range, scan_exposure_time, start_y, start_z, start_cx, start_cy, stop_y, stop_z, stop_cx, stop_cy]
tried = 0
while tried < number_of_attempts:
tried += 1
try:
self.task_id = self.start_scan_4d_ex(parameters)
break
except:
gevent.sleep(sleeptime)
if wait:
self.wait_for_task_to_finish(self.task_id)
return self.task_id
def start_helical_scan(self):
return self.md2.startscan4d()
def start_scan_4d_ex(self, parameters):
return self.md2.startScan4DEx(parameters)
def set_helical_start(self):
return self.md2.setstartscan4d()
def set_helical_stop(self):
return self.md2.setstopscan4d()
def start_raster_scan(self, vertical_range, horizontal_range, number_of_rows, number_of_columns, direction_inversion):
return self.md2.startRasterScan([vertical_range, horizontal_range, number_of_rows, number_of_columns, direction_inversion])
def get_motor_state(self, motor_name):
if isinstance(self.md2, md2_mockup):
return 'STANDBY'
else:
return self.md2.getMotorState(motor_name).name
def get_status(self):
try:
return self.md2.read_attribute('Status').value
except:
return 'Unknown'
def get_state(self):
# This solution takes approximately 2.4 ms on average
try:
return self.md2.read_attribute('State').value.name
except:
return 'UNKNOWN'
# This solution takes approximately 15.5 ms on average
#motors = ['Omega', 'AlignmentX', 'AlignmentY', 'AlignmentZ', 'CentringX', 'CentringY', 'ApertureHorizontal', 'ApertureVertical', 'CapillaryHorizontal', 'CapillaryVertical', 'ScintillatorHorizontal', 'ScintillatorVertical', 'Zoom']
#state = set([self.get_motor_state(m) for m in motors])
#if len(state) == 1 and 'STANDBY' in state:
#return 'STANDBY'
#else:
#return 'MOVING'
def wait(self, device=None, timeout=7):
green_light = False
last_state = None
last_status = None
_start = time.time()
while green_light is False and (time.time()-_start)<timeout:
state = self.get_state()
status = self.get_status()
try:
if device is None:
if state.lower() in ['moving', 'running', 'unknown']:
if state != last_state:
logging.info("MD2 wait" )
last_state = state
elif status.lower() in ['running', 'unknown', 'setting beamlocation phase', 'setting transfer phase', 'setting centring phase', 'setting data collection phase']:
if status != last_status:
logging.info("MD2 wait" )
last_status = status
else:
green_light = True
return
else:
if device.state().name not in ['STANDBY']:
logging.info("Device %s wait" % device)
else:
green_light = True
return
except:
traceback.print_exc()
logging.info('Problem occured in wait %s ' % device)
logging.info(traceback.print_exc())
gevent.sleep(.1)
def move_to_position(self, position={}, epsilon = 0.0002):
if position != {}:
for motor in position:
while abs(self.md2.read_attribute('%sPosition' % self.shortFull[motor]).value - position[motor]) > epsilon:
self.wait()
gevent.sleep(0.5)
self.md2.write_attribute('%sPosition' % self.shortFull[motor], position[motor])
self.wait()
self.wait()
return
def get_head_type(self):
return self.md2.headtype
def has_kappa(self):
return self.get_head_type() == 'MiniKappa'
def set_position(self, position, number_of_attempts=3, wait=True, collect_auxiliary_images=False, ignored_motors=['beam_x', 'beam_y', 'kappa', 'kappa_phi']):
if not self.has_kappa():
ignored_motors += ['Phi', 'Kappa']
motor_name_value_list = ['%s=%6.4f' % (motor, position[motor]) for motor in position if position[motor] != None and (motor not in ignored_motors)]
command_string = ','.join(motor_name_value_list)
k=0
task_id = None
success = False
self.auxiliary_images = []
while k < number_of_attempts and success == False:
k+=1
try:
task_id = self.md2.startSimultaneousMoveMotors(command_string)
success = True
except:
gevent.sleep(1)
if wait == True and task_id != None:
self.wait_for_task_to_finish(task_id)
return task_id
def save_aperture_and_capillary_beam_positions(self):
self.md2.saveaperturebeamposition()
self.md2.savecapillarybeamposition()
def get_omega_position(self):
return self.get_position()['Omega']
def get_kappa_position(self):
return self.get_position()['Kappa']
def set_kappa_position(self, kappa_position):
current_position = self.get_aligned_position()
current_kappa = current_position['Kappa']
current_phi = current_position['Phi']
x = self.get_x()
shift = self.get_shift(current_kappa, current_phi, x, kappa_position, current_phi)
destination = copy.deepcopy(current_position)
destination['CentringX'] = shift[0]
destination['CentringY'] = shift[1]
destination['AlignmentY'] = shift[2]
#destination['AlignmentZ'] += (az_destination_offset - az_current_offset)
destination['Kappa'] = kappa_position
self.set_position(destination)
def get_phi_position(self):
return self.get_position()['Phi']
def set_phi_position(self, phi_position):
current_position = self.get_aligned_position()
current_kappa = current_position['Kappa']
current_phi = current_position['Phi']
x = self.get_x()
shift = self.get_shift(current_kappa, current_phi, x, current_kappa, phi_position)
destination = copy.deepcopy(current_position)
#destination['CentringX'] = shift[0]
#destination['CentringY'] = shift[1]
#destination['AlignmentY'] = shift[2]
#destination['AlignmentZ'] += (az_destination_offset - az_current_offset)
destination['Phi'] = phi_position
self.set_position(destination)
def set_kappa_phi_position(self, kappa_position, phi_position):
current_position = self.get_aligned_position()
current_kappa = current_position['Kappa']
current_phi = current_position['Phi']
x = self.get_x()
shift = self.get_shift(current_kappa, current_phi, x, kappa_position, phi_position)
destination = copy.deepcopy(current_position)
destination['CentringX'] = shift[0]
destination['CentringY'] = shift[1]
destination['AlignmentY'] = shift[2]
#destination['AlignmentZ'] += (az_destination_offset - az_current_offset)
destination['Kappa'] = kappa_position
destination['Phi'] = phi_position
self.set_position(destination)
def get_chi_position(self):
return self.get_position()['Chi']
def get_x(self):
current_position = self.get_aligned_position()
return [current_position[motor] for motor in ['CentringX', 'CentringY', 'AlignmentY']]
def get_centringx_position(self):
return self.get_position()['CentringX']
def get_centringy_position(self):
return self.get_position()['CentringY']
def get_alignmentx_position(self):
return self.get_position()['AlignmentX']
def get_alignmenty_position(self):
return self.get_position()['AlignmentY']
def get_alignmentz_position(self):
return self.get_position()['AlignmentZ']
def get_centringtablevertical_position(self):
return self.get_position()['CentringTableVertical']
def get_centringtablefocus_position(self):
return self.get_position()['CentringTableFocus']
def get_zoom_position(self):
return self.get_position()['Zoom']
def get_beam_x_position(self):
return 0
def get_beam_y_position(self):
return 0.
def get_centring_x_y_tabledisplacement(self):
x = self.get_centringx_position()
y = self.get_centringy_position()
return x, y, sqrt(x**2 + y**2)
def get_omega_alpha_and_centringtabledisplacement(self):
omega = radians(self.get_omega_position())
x, y, centringtabledisplacement = self.get_centring_x_y_tabledisplacement()
alpha = atan2(y, -x)
return omega, alpha, centringtabledisplacement
def get_centringtablevertical_position_abinitio(self):
omega, alpha, centringtabledisplacement = self.get_omega_alpha_and_centringtabledisplacement()
return sin(omega + alpha) * centringtabledisplacement
def get_centringtablefocus_position_abinitio(self):
omega, alpha, centringtabledisplacement = self.get_omega_alpha_and_centringtabledisplacement()
return cos(omega + alpha) * centringtabledisplacement
def get_centringtable_vertical_position_from_hypothetical_centringx_centringy_and_omega(self, x, y, omega):
d = sqrt(x**2 + y**2)
alpha = atan2(y, -x)
omega = radians(omega)
return sin(omega + alpha) * d
def get_centringtable_focus_position_from_hypothetical_centringx_centringy_and_omega(self, x, y, omega):
d = sqrt(x**2 + y**2)
alpha = atan2(y, -x)
omega = radians(omega)
return cos(omega + alpha) * d
def get_focus_and_vertical_from_position(self, position=None):
if position is None:
position = self.get_aligned_position()
x = position['CentringX']
y = position['CentringY']
omega = position['Omega']
focus, vertical = self.get_focus_and_vertical(x, y, omega)
return focus, vertical
def get_aligned_position_from_reference_position_and_shift(self, reference_position, horizontal_shift, vertical_shift, AlignmentZ_reference=0.0944, epsilon=1e-3):
alignmentz_shift = reference_position['AlignmentZ'] - AlignmentZ_reference
if abs(alignmentz_shift) < epsilon:
alignmentz_shift = 0
vertical_shift += alignmentz_shift
#centringx_shift, centringy_shift = self.goniometer.get_x_and_y(0, vertical_shift, reference_position['Omega']) : changed by Elke
centringx_shift, centringy_shift = self.get_x_and_y(0, vertical_shift, reference_position['Omega'])
aligned_position = copy.deepcopy(reference_position)
aligned_position['AlignmentZ'] -= alignmentz_shift
aligned_position['AlignmentY'] -= horizontal_shift
aligned_position['CentringX'] += centringx_shift
aligned_position['CentringY'] += centringy_shift
return aligned_position
def get_aligned_position_from_reference_position_and_x_and_y(self, reference_position, x, y, AlignmentZ_reference=0.0944):
horizontal_shift = x - reference_position['AlignmentY']
vertical_shift = y - reference_position['AlignmentZ']
return self.get_aligned_position_from_reference_position_and_shift(reference_position, horizontal_shift, vertical_shift, AlignmentZ_reference=AlignmentZ_reference)
def get_x_and_y(self, focus, vertical, omega):
omega = -radians(omega)
R = np.array([[cos(omega), -sin(omega)], [sin(omega), cos(omega)]])
R = np.linalg.pinv(R)
return np.dot(R, [-focus, vertical])
def get_focus_and_vertical(self, x, y, omega):
omega = radians(omega)
R = np.array([[cos(omega), -sin(omega)], [sin(omega), cos(omega)]])
return np.dot(R, [-x, y])
def get_centring_x_y_for_given_omega_and_vertical_position(self, omega, vertical_position, focus_position, C=1., l=1., nruns=10):
from scipy.optimize import minimize
import random
def vertical_position_model(x, y, omega):
d = sqrt(x**2 + y**2)
alpha = atan2(y, -x)
omega = radians(omega)
return sin(omega + alpha) * d
def focus_position_model(x, y, omega):
d = sqrt(x**2 + y**2)
alpha = atan2(y, -x)
omega = radians(omega)
return cos(omega + alpha) * d
def error(varse, omega, truth_vertical, truth_focus, C=C, l=l):
x, y = varse
model_vertical = vertical_position_model(x, y, omega)
model_focus = focus_position_model(x, y, omega)
return C*(abs(truth_vertical-model_vertical) + abs(truth_focus-model_focus)) + l*(x**2 + y**2)
def fit(nruns=nruns):
results = []
for run in range(int(nruns)):
initial_parameters = [random.random(), random.random()]
fit_results = minimize(error, initial_parameters, method='nelder-mead', args=(omega, vertical_position, focus_position))
results.append(fit_results.x)
results = np.array(results)
return np.median(results, axis=0)
x, y = fit(nruns=nruns)
return x, y
def get_analytical_centring_x_y_for_given_omega_and_vertical_position(self, omega, vertical_position, focus_position):
omega = radians(omega)
alpha = atan2(vertical, focus) - omega
y_over_x = tan(alpha)
def get_position(self):
return dict([(m.split('=')[0], float(m.split('=')[1])) for m in self.md2.motorpositions])
def get_aligned_position(self, motor_names=['AlignmentX', 'AlignmentY', 'AlignmentZ', 'CentringY', 'CentringX', 'Kappa', 'Phi', 'Omega']):
return dict([(m.split('=')[0], float(m.split('=')[1])) for m in self.md2.motorpositions if m.split('=')[0] in motor_names and m.split('=')[1] != 'NaN'])
def get_state_vector(self, motor_names=['Omega', 'Kappa', 'Phi', 'CentringX', 'CentringY', 'AlignmentX', 'AlignmentY', 'AlignmentZ', 'ScintillatorVertical', 'Zoom']):
motor_positions_dictionary = self.get_motor_positions_dictionary()
return [motor_positions_dictionary[motor_name] for motor_name in motor_names]
#return [m.split('=')[1] for m in motor_positions if m.split('=')[0] in motor_names]
def get_motor_positions_dictionary(self, motor_names=['Omega', 'Kappa', 'Phi', 'Chi', 'CentringX', 'CentringY', 'AlignmentX', 'AlignmentY', 'AlignmentZ', 'ApertureVertical', 'ApertureHorizontal', 'CapillaryVertical', 'CapillaryHorizontal', 'ScintillatorVertical', 'ScintillatorHorizontal', 'BeamstopX', 'BeamstopY', 'BeamstopZ', 'Zoom', 'PlateTranslation', 'CentringTableVertical', 'CentringTableFocus', 'BeamstopDistance'], logfile='/nfs/data2/log/md2_motor_positions_problem.log'):
try:
motor_positions_dictionary = dict([item.split('=') for item in self.md2.motorpositions])
except:
message = 'failure to read md2.motorpositions attribute'
logging.info(message)
os.system('echo "{now:s} {message:s}" >> {logfile:s}'.format(logfile=logfile, message=message, now=str(datetime.datetime.now())))
motor_positions_dictionary = dict((motor_name, np.nan) for motor_name in motor_names)
return motor_positions_dictionary
def sample_is_loaded(self, sample_size=7, sleeptime=0.01, timeout=3, logfile='/nfs/data2/log/md2_sample_detection_problem.log'):
_start = time.time()
sample_is_coherent = False
all_answers = []
while not sample_is_coherent and (time.time()-_start<timeout):
is_loaded_sample = []
for k in range(sample_size):
is_loaded_sample.append(int(self.md2.SampleIsLoaded))
gevent.sleep(np.random.random()*sleeptime)
median = np.median(is_loaded_sample)
mean = np.mean(is_loaded_sample)
sample_is_coherent = median == mean
if not sample_is_coherent:
message = 'gonio sample detection is not coherent, you may want to check ...'
logging.info(message)
print(message)
os.system('echo "{now:s} {is_loaded_sample:s}" >> {logfile:s}'.format(logfile=logfile, is_loaded_sample=str(is_loaded_sample), now=str(datetime.datetime.now())))
all_answers += is_loaded_sample
if not sample_is_coherent:
median = np.median(all_answers)
return bool(median)
def insert_backlight(self, sleeptime=0.1, timeout=7):
_start = time.time()
self.wait()
while not self.backlight_is_on() and (time.time()-_start) < timeout:
try:
self.md2.backlightison = True
except:
gevent.sleep(sleeptime)
def insert_frontlight(self, sleeptime=0.1, timeout=7):
_start = time.time()
print('inserting frontlight')
self.wait()
while not self.frontlight_is_on() and (time.time()-_start) < timeout:
try:
self.md2.frontlightison = True
except:
gevent.sleep(sleeptime)
print('success? %s' % self.frontlight_is_on())
def extract_backlight(self):
self.remove_backlight()
def remove_backlight(self, sleeptime=0.1, timeout=7):
_start = time.time()
while self.backlight_is_on() and (time.time()-_start) < timeout:
try:
self.md2.backlightison = False
except:
gevent.sleep(sleeptime)
def extract_frontlight(self, sleeptime=0.1, timeout=7):
_start = time.time()
print('extracting frontlight')
while self.frontlight_is_on() and (time.time()-_start) < timeout:
try:
self.md2.frontlightison = False
except:
gevent.sleep(sleeptime)
print('success? %s' % (not self.frontlight_is_on()))
def backlight_is_on(self):
return self.md2.backlightison
def frontlight_is_on(self):
return self.md2.frontlightison
def get_backlightlevel(self):
return self.md2.backlightlevel
def set_backlightlevel(self, level=10, number_of_attempts=7, sleeptime=0.5):
n = 0
while self.md2.backlightlevel != level and n <= number_of_attempts:
n += 1
try:
self.md2.backlightlevel = level
except:
gevent.sleep(sleeptime)
def get_frontlightlevel(self):
return self.md2.frontlightlevel
def set_frontlightlevel(self, level=55, number_of_attempts=7, sleeptime=0.5):
n = 0
while self.md2.frontlightlevel != level and n <= number_of_attempts:
n += 1
try:
self.md2.frontlightlevel = level
success = True
except:
gevent.sleep(sleeptime)
def insert_fluorescence_detector(self):
self.md2.fluodetectorisback = False
def extract_fluorescence_detector(self):
self.md2.fluodetectorisback = True
def insert_cryostream(self):
self.md2.cryoisback = False
def extract_cryostream(self):
self.md2.cryoisback = True
def is_task_running(self, task_id):
return self.md2.istaskrunning(task_id)
def get_last_task_info(self):
return self.md2.lasttaskinfo
def get_time_from_string(self, timestring, format='%Y-%m-%d %H:%M:%S.%f'):
micros = float(timestring[timestring.find('.'):])
return time.mktime(time.strptime(timestring, format)) + micros
def get_last_task_start(self):
lasttaskinfo = self.md2.lasttaskinfo
start = lasttaskinfo[2]
return self.get_time_from_string(start)
def get_last_task_end(self):
lasttaskinfo = self.md2.lasttaskinfo
end = lasttaskinfo[3]
if end == 'null':
return None
return self.get_time_from_string(end)
def get_last_task_duration(self):
start = self.get_last_task_start()
end = self.get_last_task_end()
if end == None:
return time.time() - start
return end - start
def get_task_info(self, task_id):
return self.md2.gettaskinfo(task_id)
def set_detector_gate_pulse_enabled(self, value=True):
self.md2.DetectorGatePulseEnabled = value
def set_goniometer_phase(self, phase, wait=False, number_of_attempts=7, sleeptime=0.5):
self.wait()
k = 0
while k < number_of_attempts:
try:
self.task_id = self.md2.startsetphase(phase)
break
except:
gevent.sleep(sleeptime)
k += 1
if wait:
self.wait_for_task_to_finish(self.task_id)
else:
return self.task_id
def set_data_collection_phase(self, wait=False):
self.save_position()
self.set_goniometer_phase('DataCollection', wait=wait)
def set_transfer_phase(self, transfer_position={'AlignmentZ': 0.1017, 'AlignmentY': -1.35, 'AlignmentX': -0.0157, 'CentringX': 0.431, 'CentringY': 0.210, 'ApertureVertical': 83, 'CapillaryVertical': 0., 'Zoom': 33524.0, 'Omega': 0}, phase=False, wait=False): #, 'Kappa': 0, 'Phi': 0
#transfer_position={'AlignmentX': -0.42292751002956075, 'AlignmentY': -1.5267995679700732, 'AlignmentZ': -0.049934926625844867, 'ApertureVertical': 82.99996634818412, 'CapillaryHorizontal': -0.7518915227941564, 'CapillaryVertical': -0.00012483891752579357, 'CentringX': 1.644736842105263e-05, 'CentringY': 1.644736842105263e-05, 'Kappa': 0.0015625125024403275, 'Phi': 0.004218750006591797, 'Zoom': 34448.0}
self.set_position(transfer_position, wait=wait)
if phase:
self.set_goniometer_phase('Transfer', wait=wait)
def set_beam_location_phase(self, wait=False):
if self.get_current_phase() != 'BeamLocation':
self.save_position()
self.set_goniometer_phase('BeamLocation', wait=wait)
def set_centring_phase(self, wait=False):
self.set_goniometer_phase('Centring', wait=wait)
def get_current_phase(self):
return self.md2.currentphase
def save_position(self, number_of_attempts=15, sleeptime=0.2):
success = False
k = 0
while success == False and k < number_of_attempts:
k+=1
self.wait()
try:
self.md2.savecentringpositions()
success = True
except:
gevent.sleep(sleeptime)
def wait_for_task_to_finish(self, task_id, collect_auxiliary_images=False, sleeptime=0.1):
k = 0
self.auxiliary_images = []
while self.is_task_running(task_id):
if k == 0:
logging.info('waiting for task %d to finish' % task_id)
gevent.sleep(sleeptime)
k += 1
def set_omega_relative_position(self, step):
current_position = self.get_omega_position()
return self.set_omega_position(self, current_position+step)
def set_omega_position(self, omega_position, number_of_attempts=3, wait=True):
return self.set_position({'Omega': omega_position}, wait=wait)
def set_zoom(self, zoom, wait=True):
self.md2.coaxialcamerazoomvalue = zoom
if wait:
self.wait()
def get_orientation(self):
return self.get_omega_position()
def set_orientation(self, orientation):
self.set_omega_position(orientation)
def check_position(self, candidate_position):
if isinstance(candidate_position, str):
candidate_position = eval(candidate_position)