Skip to content

Commit f060982

Browse files
committed
Add ISPyB inserts for grid squares
1 parent 0828d28 commit f060982

File tree

5 files changed

+84
-6
lines changed

5 files changed

+84
-6
lines changed

src/murfey/client/contexts/spa.py

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,18 @@ class GridSquare(NamedTuple):
5959
tag: str = ""
6060

6161

62-
def _get_grid_square_atlas_positions(
63-
xml_path: Path, grid_square: str = ""
64-
) -> Dict[str, Tuple[Optional[int], Optional[int], Optional[float], Optional[float]]]:
62+
def _get_grid_square_atlas_positions(xml_path: Path, grid_square: str = "") -> Dict[
63+
str,
64+
Tuple[
65+
Optional[int],
66+
Optional[int],
67+
Optional[float],
68+
Optional[float],
69+
Optional[int],
70+
Optional[int],
71+
Optional[float],
72+
],
73+
]:
6574
with open(
6675
xml_path,
6776
"r",
@@ -71,7 +80,16 @@ def _get_grid_square_atlas_positions(
7180
"TileXml"
7281
]
7382
gs_pix_positions: Dict[
74-
str, Tuple[Optional[int], Optional[int], Optional[float], Optional[float]]
83+
str,
84+
Tuple[
85+
Optional[int],
86+
Optional[int],
87+
Optional[float],
88+
Optional[float],
89+
Optional[int],
90+
Optional[int],
91+
Optional[float],
92+
],
7593
] = {}
7694
for ti in tile_info:
7795
try:
@@ -96,6 +114,13 @@ def _get_grid_square_atlas_positions(
96114
* 1e9,
97115
float(gs["value"]["b:PositionOnTheAtlas"]["c:Physical"]["d:y"])
98116
* 1e9,
117+
int(
118+
float(gs["value"]["b:PositionOnTheAtlas"]["c:Size"]["d:width"])
119+
),
120+
int(
121+
float(gs["value"]["b:PositionOnTheAtlas"]["c:Size"]["d:height"])
122+
),
123+
float(gs["value"]["b:PositionOnTheAtlas"]["c:Rotation"]),
99124
)
100125
if grid_square:
101126
break
@@ -557,7 +582,10 @@ def _position_analysis(
557582
Optional[int],
558583
Optional[float],
559584
Optional[float],
560-
] = (None, None, None, None)
585+
Optional[int],
586+
Optional[int],
587+
Optional[float],
588+
] = (None, None, None, None, None, None, None)
561589
data_collection_group = (
562590
requests.get(
563591
f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/data_collection_groups"
@@ -611,6 +639,9 @@ def _position_analysis(
611639
"y_location": gs_pix_position[1],
612640
"x_stage_position": gs_pix_position[2],
613641
"y_stage_position": gs_pix_position[3],
642+
"width": gs_pix_position[4],
643+
"height": gs_pix_position[5],
644+
"angle": gs_pix_position[6],
614645
},
615646
)
616647
foil_hole = _foil_hole_from_file(transferred_file)

src/murfey/client/contexts/spa_metadata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,8 @@ def post_transfer(
155155
"y_location": pos_data[1],
156156
"x_stage_position": pos_data[2],
157157
"y_stage_position": pos_data[3],
158+
"width": pos_data[4],
159+
"height": pos_data[5],
160+
"angle": pos_data[6],
158161
},
159162
)

src/murfey/server/api/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,13 @@ def register_grid_square(
491491
grid_square_params: GridSquareParameters,
492492
db=murfey_db,
493493
):
494+
if _transport_object:
495+
dcg = db.exec(
496+
select(DataCollectionGroup)
497+
.where(DataCollectionGroup.session_id == session_id)
498+
.where(DataCollectionGroup.tag == grid_square_params.tag)
499+
).one()
500+
_transport_object.do_insert_grid_square(dcg.atlas_id, gsid, grid_square_params)
494501
try:
495502
grid_square = db.exec(
496503
select(GridSquare)

src/murfey/server/ispyb.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@
2222
BLSubSample,
2323
DataCollection,
2424
DataCollectionGroup,
25+
GridSquare,
2526
ProcessingJob,
2627
ProcessingJobParameter,
2728
Proposal,
2829
ZcZocaloBuffer,
2930
url,
3031
)
3132

32-
from murfey.util.models import Sample, Visit
33+
from murfey.util.models import GridSquareParameters, Sample, Visit
3334

3435
log = logging.getLogger("murfey.server.ispyb")
3536

@@ -127,6 +128,39 @@ def do_update_atlas(
127128
)
128129
return {"success": False, "return_value": None}
129130

131+
def do_insert_grid_square(
132+
self,
133+
atlas_id: int,
134+
grid_square_id: int,
135+
grid_square_parameters: GridSquareParameters,
136+
):
137+
record = GridSquare(
138+
atlasId=atlas_id,
139+
gridSquareLabel=grid_square_id,
140+
gridSquareImage=grid_square_parameters.image,
141+
pixelLocationX=grid_square_parameters.x_location,
142+
pixelLocationY=grid_square_parameters.y_location,
143+
height=grid_square_parameters.height,
144+
weight=grid_square_parameters.width,
145+
angle=grid_square_parameters.angle,
146+
stageLocationX=grid_square_parameters.x_stage_position,
147+
stageLocationY=grid_square_parameters.y_stage_position,
148+
pixelSize=grid_square_parameters.pixel_size,
149+
)
150+
try:
151+
with Session() as db:
152+
db.add(record)
153+
db.commit()
154+
log.info(f"Created GridSquare {record.gridSquareId}")
155+
return {"success": True, "return_value": record.gridSquareId}
156+
except ispyb.ISPyBException as e:
157+
log.error(
158+
"Inserting GridSquare entry caused exception '%s'.",
159+
e,
160+
exc_info=True,
161+
)
162+
return {"success": False, "return_value": None}
163+
130164
def send(self, queue: str, message: dict, new_connection: bool = False):
131165
if self.transport:
132166
if not self.transport.is_connected():

src/murfey/util/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,11 @@ class GridSquareParameters(BaseModel):
273273
readout_area_y: Optional[int] = None
274274
thumbnail_size_x: Optional[int] = None
275275
thumbnail_size_y: Optional[int] = None
276+
height: Optional[int] = None
277+
width: Optional[int] = None
276278
pixel_size: Optional[float] = None
277279
image: str = ""
280+
angle: Optional[float] = None
278281

279282

280283
class FoilHoleParameters(BaseModel):

0 commit comments

Comments
 (0)