-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathispyb.py
More file actions
983 lines (936 loc) · 37.3 KB
/
Copy pathispyb.py
File metadata and controls
983 lines (936 loc) · 37.3 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
from __future__ import annotations
import datetime
import logging
from typing import Callable, Generator, List, Literal, Optional
import ispyb
import workflows.transport
from fastapi import Depends
from ispyb.sqlalchemy import (
Atlas,
AutoProcProgram,
BLSample,
BLSampleGroup,
BLSampleGroupHasBLSample,
BLSampleImage,
BLSession,
BLSubSample,
DataCollection,
DataCollectionGroup,
FoilHole,
GridSquare,
MillingStep,
MillingStepName,
ProcessingJob,
ProcessingJobParameter,
Proposal,
ZcZocaloBuffer,
url,
)
from pydantic import BaseModel
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
from murfey.util import sanitise
from murfey.util.config import get_security_config
from murfey.util.models import (
FoilHoleParameters,
GridSquareParameters,
SearchMapParameters,
)
log = logging.getLogger("murfey.server.ispyb")
security_config = get_security_config()
try:
ISPyBSession = sessionmaker(
bind=create_engine(
url(credentials=security_config.ispyb_credentials),
connect_args={"use_pure": True},
pool_recycle=250,
)
)
log.info("Loaded ISPyB database session")
# Catch all errors associated with loading ISPyB database
except Exception:
log.error("Error loading ISPyB session", exc_info=True)
ISPyBSession = lambda: None
class Visit(BaseModel):
start: datetime.datetime
end: datetime.datetime
session_id: int
name: str
beamline: str
proposal_title: str
def __repr__(self) -> str:
return (
"Visit("
f"start='{self.start:%Y-%m-%d %H:%M}', "
f"end='{self.end:%Y-%m-%d %H:%M}', "
f"session_id='{self.session_id!r}',"
f"name={self.name!r}, "
f"beamline={self.beamline!r}, "
f"proposal_title={self.proposal_title!r}"
")"
)
def _send_using_new_connection(transport_type: str, queue: str, message: dict) -> None:
transport = workflows.transport.lookup(transport_type)()
transport.connect()
send_call = transport.send(queue, message)
# send_call may be a concurrent.futures.Future object
if send_call:
send_call.result()
transport.disconnect()
return None
class TransportManager:
def __init__(self, transport_type: Literal["PikaTransport"]):
self._transport_type = transport_type
self.transport = workflows.transport.lookup(transport_type)()
self.transport.connect()
self.feedback_queue = ""
try:
# Attempt to connect to ISPyB if credentials files provided
self.ispyb = (
ispyb.open(credentials=security_config.ispyb_credentials)
if security_config.ispyb_credentials
else None
)
except Exception:
# Log error and return None if the connection fails
log.error("Error encountered connecting to ISPyB server", exc_info=True)
self.ispyb = None
self._connection_callback: Callable | None = None
def reconnect(self):
try:
self.transport.disconnect()
except Exception:
log.warning(
"Disconnection of old transport failed when reconnecting",
exc_info=True,
)
self.transport = workflows.transport.lookup(self._transport_type)()
self.transport.connect()
def do_insert_data_collection_group(
self,
record: DataCollectionGroup,
message=None,
**kwargs,
):
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created DataCollectionGroup {record.dataCollectionGroupId}")
return {"success": True, "return_value": record.dataCollectionGroupId}
except ispyb.ISPyBException as e:
log.error(
"Inserting Data Collection Group entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_data_collection_group(
self,
record: DataCollectionGroup,
message=None,
**kwargs,
):
try:
with ISPyBSession() as db:
dcg = (
db.query(DataCollectionGroup)
.filter(
DataCollectionGroup.dataCollectionGroupId
== record.dataCollectionGroupId
)
.one()
)
dcg.experimentTypeId = record.experimentTypeId
db.add(dcg)
db.commit()
return {"success": True, "return_value": record.dataCollectionGroupId}
except ispyb.ISPyBException as e:
log.error(
"Updating Data Collection Group entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_atlas(self, record: Atlas):
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created Atlas {record.atlasId}")
return {"success": True, "return_value": record.atlasId}
except ispyb.ISPyBException as e:
log.error(
"Inserting Atlas entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_atlas(
self,
atlas_id: int,
atlas_image: str,
pixel_size: float,
slot: int | None,
collection_mode: str | None = None,
color_flags: dict[str, str | int] | None = None,
):
color_flags = color_flags or {}
try:
with ISPyBSession() as db:
atlas = db.query(Atlas).filter(Atlas.atlasId == atlas_id).one()
atlas.atlasImage = atlas_image or atlas.atlasImage
atlas.pixelSize = pixel_size or atlas.pixelSize
atlas.cassetteSlot = slot or atlas.cassetteSlot
atlas.mode = collection_mode or atlas.mode
# Optionally insert colour flags if present
if color_flags:
for col_name, value in color_flags.items():
setattr(atlas, col_name, value)
db.add(atlas)
db.commit()
return {"success": True, "return_value": atlas.atlasId}
except ispyb.ISPyBException as e:
log.error(
"Updating Atlas entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_grid_square(
self,
atlas_id: int,
grid_square_id: int,
grid_square_parameters: GridSquareParameters,
color_flags: dict[str, int] | None = None,
):
color_flags = color_flags or {}
# most of this is for mypy
if (
grid_square_parameters.pixel_size is not None
and grid_square_parameters.thumbnail_size_x is not None
and grid_square_parameters.readout_area_x is not None
):
# currently hard coding the scale factor because of difficulties with
# guaranteeing we have the atlas jpg and mrc sizes
grid_square_parameters.pixel_size *= (
grid_square_parameters.readout_area_x
/ grid_square_parameters.thumbnail_size_x
)
record = GridSquare(
atlasId=atlas_id,
gridSquareLabel=grid_square_id,
gridSquareImage=grid_square_parameters.image,
pixelLocationX=grid_square_parameters.x_location_scaled,
pixelLocationY=grid_square_parameters.y_location_scaled,
height=grid_square_parameters.height_scaled,
width=grid_square_parameters.width_scaled,
angle=grid_square_parameters.angle,
stageLocationX=grid_square_parameters.x_stage_position,
stageLocationY=grid_square_parameters.y_stage_position,
pixelSize=grid_square_parameters.pixel_size,
mode=grid_square_parameters.collection_mode,
)
# Optionally insert colour flags
if color_flags:
for col_name, value in color_flags.items():
setattr(record, col_name, value)
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created GridSquare {record.gridSquareId}")
return {"success": True, "return_value": record.gridSquareId}
except ispyb.ISPyBException as e:
log.error(
"Inserting GridSquare entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_grid_square(
self,
grid_square_id: int,
grid_square_parameters: GridSquareParameters,
color_flags: dict[str, int] | None = None,
):
color_flags = color_flags or {}
try:
with ISPyBSession() as db:
grid_square: GridSquare = (
db.query(GridSquare)
.filter(GridSquare.gridSquareId == grid_square_id)
.one()
)
if (
grid_square_parameters.pixel_size is not None
and grid_square_parameters.readout_area_x is not None
and grid_square_parameters.thumbnail_size_x is not None
):
grid_square_parameters.pixel_size *= (
grid_square_parameters.readout_area_x
/ grid_square_parameters.thumbnail_size_x
)
if grid_square_parameters.image:
grid_square.gridSquareImage = grid_square_parameters.image
if grid_square_parameters.x_location_scaled:
grid_square.pixelLocationX = (
grid_square_parameters.x_location_scaled
)
if grid_square_parameters.y_location_scaled:
grid_square.pixelLocationY = (
grid_square_parameters.y_location_scaled
)
if grid_square_parameters.height_scaled is not None:
grid_square.height = grid_square_parameters.height_scaled
if grid_square_parameters.width_scaled is not None:
grid_square.width = grid_square_parameters.width_scaled
if grid_square_parameters.angle:
grid_square.angle = grid_square_parameters.angle
if grid_square_parameters.x_stage_position:
grid_square.stageLocationX = grid_square_parameters.x_stage_position
if grid_square_parameters.y_stage_position:
grid_square.stageLocationY = grid_square_parameters.y_stage_position
if grid_square_parameters.pixel_size:
grid_square.pixelSize = grid_square_parameters.pixel_size
if grid_square_parameters.collection_mode:
grid_square.mode = grid_square_parameters.collection_mode
# Optionally insert colour flags
if color_flags:
for col_name, value in color_flags.items():
setattr(grid_square, col_name, value)
db.add(grid_square)
db.commit()
return {"success": True, "return_value": grid_square.gridSquareId}
except ispyb.ISPyBException as e:
log.error(
"Updating GridSquare entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_foil_hole(
self,
grid_square_id: int,
scale_factor: Optional[float],
foil_hole_parameters: FoilHoleParameters,
):
if (
foil_hole_parameters.thumbnail_size_x is not None
and foil_hole_parameters.readout_area_x is not None
and foil_hole_parameters.pixel_size is not None
):
foil_hole_parameters.pixel_size *= (
foil_hole_parameters.readout_area_x
/ foil_hole_parameters.thumbnail_size_x
)
if scale_factor:
foil_hole_parameters.diameter = (
int(foil_hole_parameters.diameter * scale_factor)
if foil_hole_parameters.diameter
else None
)
foil_hole_parameters.x_location = (
int(foil_hole_parameters.x_location * scale_factor)
if foil_hole_parameters.x_location
else None
)
foil_hole_parameters.y_location = (
int(foil_hole_parameters.y_location * scale_factor)
if foil_hole_parameters.y_location
else None
)
record = FoilHole(
gridSquareId=grid_square_id,
foilHoleLabel=foil_hole_parameters.name,
foilHoleImage=foil_hole_parameters.image,
pixelLocationX=foil_hole_parameters.x_location,
pixelLocationY=foil_hole_parameters.y_location,
diameter=foil_hole_parameters.diameter,
stageLocationX=foil_hole_parameters.x_stage_position,
stageLocationY=foil_hole_parameters.y_stage_position,
pixelSize=foil_hole_parameters.pixel_size,
)
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created FoilHole {record.foilHoleId}")
return {"success": True, "return_value": record.foilHoleId}
except ispyb.ISPyBException as e:
log.error(
"Inserting FoilHole entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_foil_hole(
self,
foil_hole_id: int,
scale_factor: float,
foil_hole_parameters: FoilHoleParameters,
):
try:
with ISPyBSession() as db:
foil_hole = (
db.query(FoilHole).filter(FoilHole.foilHoleId == foil_hole_id).one()
)
if foil_hole_parameters.image:
foil_hole.foilHoleImage = foil_hole_parameters.image
if foil_hole_parameters.x_location:
foil_hole.pixelLocationX = int(
foil_hole_parameters.x_location * scale_factor
)
if foil_hole_parameters.y_location:
foil_hole.pixelLocationY = int(
foil_hole_parameters.y_location * scale_factor
)
if foil_hole_parameters.diameter is not None:
foil_hole.diameter = foil_hole_parameters.diameter * scale_factor
if foil_hole_parameters.x_stage_position:
foil_hole.stageLocationX = foil_hole_parameters.x_stage_position
if foil_hole_parameters.y_stage_position:
foil_hole.stageLocationY = foil_hole_parameters.y_stage_position
if (
foil_hole_parameters.readout_area_x is not None
and foil_hole_parameters.thumbnail_size_x is not None
and foil_hole_parameters.pixel_size is not None
):
foil_hole.pixelSize = foil_hole_parameters.pixel_size * (
foil_hole_parameters.readout_area_x
/ foil_hole_parameters.thumbnail_size_x
)
db.add(foil_hole)
db.commit()
return {"success": True, "return_value": foil_hole.foilHoleId}
except ispyb.ISPyBException as e:
log.error(
"Updating FoilHole entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_search_map(
self,
atlas_id: int,
search_map_parameters: SearchMapParameters,
):
if (
search_map_parameters.pixel_size
and search_map_parameters.height
and search_map_parameters.height_on_atlas
):
search_map_parameters.pixel_size *= (
search_map_parameters.height / search_map_parameters.height_on_atlas
)
search_map_parameters.height = (
int(search_map_parameters.height / 7.8)
if search_map_parameters.height
else None
)
search_map_parameters.width = (
int(search_map_parameters.width / 7.8)
if search_map_parameters.width
else None
)
search_map_parameters.x_location = (
int(search_map_parameters.x_location / 7.8)
if search_map_parameters.x_location
else None
)
search_map_parameters.y_location = (
int(search_map_parameters.y_location / 7.8)
if search_map_parameters.y_location
else None
)
record = GridSquare(
atlasId=atlas_id,
gridSquareImage=search_map_parameters.image,
pixelLocationX=search_map_parameters.x_location,
pixelLocationY=search_map_parameters.y_location,
height=search_map_parameters.height_on_atlas,
width=search_map_parameters.width_on_atlas,
angle=0,
stageLocationX=search_map_parameters.x_stage_position,
stageLocationY=search_map_parameters.y_stage_position,
pixelSize=search_map_parameters.pixel_size,
)
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created SearchMap (GridSquare) {record.gridSquareId}")
return {"success": True, "return_value": record.gridSquareId}
except ispyb.ISPyBException as e:
log.error(
"Inserting SearchMap (GridSquare) entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_search_map(
self, search_map_id, search_map_parameters: SearchMapParameters
):
try:
with ISPyBSession() as db:
grid_square = (
db.query(GridSquare)
.filter(GridSquare.gridSquareId == search_map_id)
.one()
)
if (
search_map_parameters.pixel_size
and search_map_parameters.height
and search_map_parameters.height_on_atlas
):
search_map_parameters.pixel_size *= (
search_map_parameters.height
/ search_map_parameters.height_on_atlas
)
if search_map_parameters.image:
grid_square.gridSquareImage = search_map_parameters.image
if search_map_parameters.x_location:
grid_square.pixelLocationX = int(
search_map_parameters.x_location / 7.8
)
if search_map_parameters.y_location:
grid_square.pixelLocationY = int(
search_map_parameters.y_location / 7.8
)
if search_map_parameters.height_on_atlas:
grid_square.height = int(
search_map_parameters.height_on_atlas / 7.8
)
if search_map_parameters.width_on_atlas:
grid_square.width = int(search_map_parameters.width_on_atlas / 7.8)
if search_map_parameters.x_stage_position:
grid_square.stageLocationX = search_map_parameters.x_stage_position
if search_map_parameters.y_stage_position:
grid_square.stageLocationY = search_map_parameters.y_stage_position
if search_map_parameters.pixel_size:
grid_square.pixelSize = search_map_parameters.pixel_size
db.add(grid_square)
db.commit()
return {"success": True, "return_value": grid_square.gridSquareId}
except ispyb.ISPyBException as e:
log.error(
"Updating SearchMap (GridSquare) entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def send(self, queue: str, message: dict, new_connection: bool = False):
if self.transport:
if not self.transport.is_connected():
self.reconnect()
if self._connection_callback:
self._connection_callback()
if new_connection:
_send_using_new_connection(self._transport_type, queue, message)
else:
self.transport.send(queue, message)
def do_insert_data_collection(self, record: DataCollection, message=None, **kwargs):
comment = (
f"Tilt series: {kwargs['tag']}"
if kwargs.get("tag")
else "Created for Murfey"
)
try:
with ISPyBSession() as db:
record.comments = comment
db.add(record)
db.commit()
log.info(f"Created DataCollection {record.dataCollectionId}")
return {"success": True, "return_value": record.dataCollectionId}
except ispyb.ISPyBException as e:
log.error(
"Inserting Data Collection entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_sample_group(self, record: BLSampleGroup) -> dict:
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created BLSampleGroup {record.blSampleGroupId}")
return {"success": True, "return_value": record.blSampleGroupId}
except ispyb.ISPyBException as e:
log.error(
"Inserting Sample Group entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_sample(self, record: BLSample, sample_group_id: int) -> dict:
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created BLSample {record.blSampleId}")
linking_record = BLSampleGroupHasBLSample(
blSampleId=record.blSampleId, blSampleGroupId=sample_group_id
)
db.add(linking_record)
db.commit()
log.info(
f"Linked BLSample {record.blSampleId} to BLSampleGroup {sample_group_id}"
)
return {"success": True, "return_value": record.blSampleId}
except ispyb.ISPyBException as e:
log.error(
"Inserting Sample entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_subsample(self, record: BLSubSample) -> dict:
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created BLSubSample {record.blSubSampleId}")
return {"success": True, "return_value": record.blSubSampleId}
except ispyb.ISPyBException as e:
log.error(
"Inserting SubSample entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_sample_image(self, record: BLSampleImage) -> dict:
try:
with ISPyBSession() as db:
db.add(record)
db.commit()
log.info(f"Created BLSampleImage {record.blSampleImageId}")
return {"success": True, "return_value": record.blSampleImageId}
except ispyb.ISPyBException as e:
log.error(
"Inserting Sample Image entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_create_ispyb_job(
self,
record: ProcessingJob,
params: List[ProcessingJobParameter] | None = None,
rw=None,
**kwargs,
):
params = params or []
dcid = record.dataCollectionId
if not dcid:
log.error("Can not create job: DCID not specified")
return {"success": False, "return_value": None}
jp = self.ispyb.mx_processing.get_job_params()
jp["automatic"] = record.automatic
jp["comments"] = record.comments
jp["datacollectionid"] = dcid
jp["display_name"] = record.displayName
jp["recipe"] = record.recipe
log.info("Creating database entries...")
try:
jobid = self.ispyb.mx_processing.upsert_job(list(jp.values()))
for p in params:
pp = self.ispyb.mx_processing.get_job_parameter_params()
pp["job_id"] = jobid
pp["parameter_key"] = p.parameterKey
pp["parameter_value"] = p.parameterValue
self.ispyb.mx_processing.upsert_job_parameter(list(pp.values()))
log.info(f"All done. Processing job {jobid} created")
return {"success": True, "return_value": jobid}
except ispyb.ISPyBException as e:
log.error(
"Inserting Processing Job entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_processing_status(self, record: AutoProcProgram, **kwargs):
ppid = record.autoProcProgramId
message = record.processingMessage
status = (
"success"
if record.processingStatus
else ("none" if record.processingStatus is None else "failure")
)
try:
result = self.ispyb.mx_processing.upsert_program_ex(
program_id=ppid,
status={"success": 1, "failure": 0, "none": None}.get(status),
time_start=record.processingStartTime,
time_update=record.processingEndTime,
message=message,
job_id=record.processingJobId,
)
log.info(
f"Updating program {result} with status {status!r}",
)
return {"success": True, "return_value": result}
except ispyb.ISPyBException as e:
log.error(
"Updating program %s status: '%s' caused exception '%s'.",
ppid,
message,
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_insert_milling_step(
self,
# MillingStepName identifiers
recipe_name: str,
activity_name: str,
grid_square_id: int,
# Values
is_enabled: bool | None = None,
status: str | None = None,
execution_time: float | None = None,
stage_x: float | None = None,
stage_y: float | None = None,
stage_z: float | None = None,
rotation: float | None = None,
tilt_alpha: float | None = None,
beam_type: str | None = None,
beam_voltage: float | None = None,
beam_current: float | None = None,
milling_angle: float | None = None,
depth_correction: float | None = None,
lamella_offset: float | None = None,
trench_height_front: float | None = None,
trench_height_rear: float | None = None,
width_overlap_front_left: float | None = None,
width_overlap_front_right: float | None = None,
width_overlap_rear_left: float | None = None,
width_overlap_rear_right: float | None = None,
):
try:
with ISPyBSession() as db:
# Find the ID of this MillingStep
milling_step_name = (
db.query(MillingStepName)
.filter(
MillingStepName.recipe == recipe_name,
MillingStepName.step == activity_name,
)
.one()
)
record = MillingStep(
# IDs
millingStepNameId=milling_step_name.millingStepNameId,
gridSquareId=grid_square_id,
# Values
isEnabled=is_enabled,
status=status,
executionTime=execution_time,
stageX=stage_x,
stageY=stage_y,
stageZ=stage_z,
rotation=rotation,
alphaTilt=tilt_alpha,
beamType=beam_type,
beamVoltage=beam_voltage,
beamCurrent=beam_current,
millingAngle=milling_angle,
depthCorrection=depth_correction,
lamellaOffset=lamella_offset,
trenchHeightFront=trench_height_front,
trenchHeightRear=trench_height_rear,
widthOverlapFrontLeft=width_overlap_front_left,
widthOverlapFrontRight=width_overlap_front_right,
widthOverlapRearLeft=width_overlap_rear_left,
widthOverlapRearRight=width_overlap_rear_right,
)
db.add(record)
db.commit()
log.info(f"Created MillingStep {record.millingStepId}")
return {"success": True, "return_value": record.millingStepId}
except ispyb.ISPyBException as e:
log.error(
"Insert MillingStep entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_update_milling_step(
self,
milling_step_id: int,
is_enabled: bool | None = None,
status: str | None = None,
execution_time: float | None = None,
stage_x: float | None = None,
stage_y: float | None = None,
stage_z: float | None = None,
rotation: float | None = None,
tilt_alpha: float | None = None,
beam_type: str | None = None,
beam_voltage: float | None = None,
beam_current: float | None = None,
milling_angle: float | None = None,
depth_correction: float | None = None,
lamella_offset: float | None = None,
trench_height_front: float | None = None,
trench_height_rear: float | None = None,
width_overlap_front_left: float | None = None,
width_overlap_front_right: float | None = None,
width_overlap_rear_left: float | None = None,
width_overlap_rear_right: float | None = None,
):
try:
with ISPyBSession() as db:
milling_step = (
db.query(MillingStep)
.filter(MillingStep.millingStepId == milling_step_id)
.one()
)
milling_step.isEnabled = is_enabled or milling_step.isEnabled
milling_step.status = status or milling_step.status
milling_step.executionTime = (
execution_time or milling_step.executionTime
)
milling_step.stageX = stage_x or milling_step.stageX
milling_step.stageY = stage_y or milling_step.stageY
milling_step.stageZ = stage_z or milling_step.stageZ
milling_step.rotation = rotation or milling_step.rotation
milling_step.alphaTilt = tilt_alpha or milling_step.alphaTilt
milling_step.beamType = beam_type or milling_step.beamType
milling_step.beamVoltage = beam_voltage or milling_step.beamVoltage
milling_step.beamCurrent = beam_current or milling_step.beamCurrent
milling_step.millingAngle = milling_angle or milling_step.millingAngle
milling_step.depthCorrection = (
depth_correction or milling_step.depthCorrection
)
milling_step.lamellaOffset = (
lamella_offset or milling_step.lamellaOffset
)
milling_step.trenchHeightFront = (
trench_height_front or milling_step.trenchHeightFront
)
milling_step.trenchHeightRear = (
trench_height_rear or milling_step.trenchHeightRear
)
milling_step.widthOverlapFrontLeft = (
width_overlap_front_left or milling_step.widthOverlapFrontLeft
)
milling_step.widthOverlapFrontRight = (
width_overlap_front_right or milling_step.widthOverlapFrontRight
)
milling_step.widthOverlapRearLeft = (
width_overlap_rear_left or milling_step.widthOverlapRearLeft
)
milling_step.widthOverlapRearRight = (
width_overlap_rear_right or milling_step.widthOverlapRearRight
)
db.add(milling_step)
db.commit()
return {"success": True, "return_value": milling_step.millingStepId}
except ispyb.ISPyBException as e:
log.error(
"Updating MillingStep entry caused exception '%s'.",
e,
exc_info=True,
)
return {"success": False, "return_value": None}
def do_buffer_lookup(self, app_id: int, uuid: int) -> Optional[int]:
with ISPyBSession() as db:
buffer_objects = (
db.query(ZcZocaloBuffer)
.filter_by(AutoProcProgramID=app_id, UUID=uuid)
.all()
)
reference = buffer_objects[0].Reference if buffer_objects else None
log.info(f"Buffer lookup {uuid} returned {reference}")
return reference
def _get_session() -> Generator[Optional[Session], None, None]:
db = ISPyBSession()
if db is None:
yield None
return
try:
yield db
finally:
db.close()
# Shortcut to access the database in a FastAPI endpoint
DB = Depends(_get_session)
def get_session_id(
microscope: str,
proposal_code: str,
proposal_number: str,
visit_number: str,
db: Session | None,
) -> int | None:
# Log received lookup parameters
log.debug(
"Looking up ISPyB BLSession ID using the following values:\n"
f"microscope: {sanitise(microscope)}\n"
f"proposal_code: {sanitise(proposal_code)}\n"
f"proposal_number: {sanitise(str(proposal_number))}\n"
f"visit_number: {sanitise(str(visit_number))}\n"
)
# Lookup BLSession ID
if db is None:
return None
query = (
db.query(BLSession)
.join(Proposal)
.filter(
BLSession.proposalId == Proposal.proposalId,
BLSession.beamLineName == microscope,
Proposal.proposalCode == proposal_code,
Proposal.proposalNumber == proposal_number,
BLSession.visit_number == visit_number,
)
.add_columns(BLSession.sessionId)
.all()
)
res = query[0][1]
db.close()
return res
def get_proposal_id(proposal_code: str, proposal_number: str, db: Session) -> int:
query = (
db.query(Proposal)
.filter(
Proposal.proposalCode == proposal_code,
Proposal.proposalNumber == proposal_number,
)
.all()
)
return query[0].proposalId
def get_all_ongoing_visits(microscope: str, db: Session | None) -> list[Visit]:
if db is None:
print("No database found")
return []
query = (
db.query(BLSession)
.join(Proposal)
.filter(
BLSession.proposalId == Proposal.proposalId,
BLSession.beamLineName == microscope,
BLSession.endDate > datetime.datetime.now(),
BLSession.startDate < datetime.datetime.now(),
)
.add_columns(
BLSession.startDate,
BLSession.endDate,
BLSession.sessionId,
Proposal.proposalCode,
Proposal.proposalNumber,
BLSession.visit_number,
Proposal.title,
)
.all()
)
return [
Visit(
start=row.startDate,
end=row.endDate,
session_id=row.sessionId,
name=f"{row.proposalCode}{row.proposalNumber}-{row.visit_number}",
proposal_title=row.title,
beamline=microscope,
)
for row in query
]