-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathispyb.py
More file actions
682 lines (639 loc) · 24.6 KB
/
Copy pathispyb.py
File metadata and controls
682 lines (639 loc) · 24.6 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
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,
ProcessingJob,
ProcessingJobParameter,
Proposal,
ZcZocaloBuffer,
url,
)
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, Sample, Visit
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},
)
)
log.info("Loaded ISPyB database session")
except AttributeError:
log.error("Error loading ISPyB session", exc_info=True)
ISPyBSession = lambda: None
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 = ""
self.ispyb = (
ispyb.open(credentials=security_config.ispyb_credentials)
if security_config.ispyb_credentials
else None
)
if self.ispyb is not None:
print("Loaded ISPyB databse")
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_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
):
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
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,
):
# 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
)
grid_square_parameters.height = (
int(grid_square_parameters.height / 7.8)
if grid_square_parameters.height
else None
)
grid_square_parameters.width = (
int(grid_square_parameters.width / 7.8)
if grid_square_parameters.width
else None
)
grid_square_parameters.x_location = (
int(grid_square_parameters.x_location / 7.8)
if grid_square_parameters.x_location
else None
)
grid_square_parameters.y_location = (
int(grid_square_parameters.y_location / 7.8)
if grid_square_parameters.y_location
else None
)
record = GridSquare(
atlasId=atlas_id,
gridSquareLabel=grid_square_id,
gridSquareImage=grid_square_parameters.image,
pixelLocationX=grid_square_parameters.x_location,
pixelLocationY=grid_square_parameters.y_location,
height=grid_square_parameters.height,
width=grid_square_parameters.width,
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,
)
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
):
try:
with ISPyBSession() as db:
grid_square = (
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:
grid_square.pixelLocationX = int(
grid_square_parameters.x_location / 7.8
)
if grid_square_parameters.y_location:
grid_square.pixelLocationY = int(
grid_square_parameters.y_location / 7.8
)
if grid_square_parameters.height is not None:
grid_square.height = int(grid_square_parameters.height / 7.8)
if grid_square_parameters.width is not None:
grid_square.width = int(grid_square_parameters.width / 7.8)
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
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 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 False
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_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()
DB = Depends(_get_session)
# Shortcut to access the database in a FastAPI endpoint
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_sub_samples_from_visit(visit: str, db: Session) -> List[Sample]:
proposal_id = get_proposal_id(visit[:2], visit.split("-")[0][2:], db)
samples = (
db.query(BLSampleGroup, BLSampleGroupHasBLSample, BLSample, BLSubSample)
.join(BLSample, BLSample.blSampleId == BLSampleGroupHasBLSample.blSampleId)
.join(
BLSampleGroup,
BLSampleGroup.blSampleGroupId == BLSampleGroupHasBLSample.blSampleGroupId,
)
.join(BLSubSample, BLSubSample.blSampleId == BLSample.blSampleId)
.filter(BLSampleGroup.proposalId == proposal_id)
.all()
)
res = [
Sample(
sample_group_id=s[1].blSampleGroupId,
sample_id=s[2].blSampleId,
subsample_id=s[3].blSubSampleId,
image_path=s[3].imgFilePath,
)
for s in samples
]
return res
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
]
def get_data_collection_group_ids(session_id):
query = (
ISPyBSession()
.query(DataCollectionGroup)
.filter(
DataCollectionGroup.sessionId == session_id,
)
.all()
)
dcgids = [row.dataCollectionGroupId for row in query]
return dcgids