Skip to content

Commit 92654c9

Browse files
authored
Fixes for grid square and foil hole registration (#459)
1 parent 79b3b3a commit 92654c9

File tree

5 files changed

+103
-82
lines changed

5 files changed

+103
-82
lines changed

src/murfey/client/contexts/spa.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,7 @@ def _position_analysis(
456456
foil_hole = foil_hole_from_file(transferred_file)
457457
if foil_hole not in self._foil_holes[grid_square]:
458458
fh_url = f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/grid_square/{grid_square}/foil_hole"
459-
if (
460-
grid_square_metadata_file.is_file()
461-
and environment.murfey_session is not None
462-
):
459+
if environment.murfey_session is not None:
463460
fh = foil_hole_data(
464461
grid_square_metadata_file,
465462
foil_hole,

src/murfey/client/contexts/spa_metadata.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,6 @@ def _foil_hole_positions(xml_path: Path, grid_square: int) -> Dict[str, FoilHole
2121
for_parsing = xml.read()
2222
data = xmltodict.parse(for_parsing)
2323
data = data["GridSquareXml"]
24-
readout_area = data["MicroscopeImage"]["microscopeData"]["acquisition"]["camera"][
25-
"ReadoutArea"
26-
]
27-
pixel_size = data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
28-
"numericValue"
29-
]
30-
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
3124
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][
3225
"a:m_serializationArray"
3326
]
@@ -58,11 +51,6 @@ def _foil_hole_positions(xml_path: Path, grid_square: int) -> Dict[str, FoilHole
5851
y_location=int(float(pix_loc["c:y"])),
5952
x_stage_position=float(stage["c:X"]),
6053
y_stage_position=float(stage["c:Y"]),
61-
readout_area_x=full_size[0] if image_path else None,
62-
readout_area_y=full_size[1] if image_path else None,
63-
thumbnail_size_x=None,
64-
thumbnail_size_y=None,
65-
pixel_size=float(pixel_size) if image_path else None,
6654
image=str(image_path),
6755
diameter=int(float(diameter)),
6856
)
@@ -128,17 +116,21 @@ def post_transfer(
128116
source_visit_dir = source.parent
129117

130118
logger.info(
131-
f"Looking for atlas XML file in metadata directory {str((source_visit_dir / partial_path).parent)}"
119+
f"Looking for atlas XML file in metadata directory {str((source_visit_dir / environment.visit / partial_path).parent)}"
132120
)
133121
atlas_xml_path = list(
134-
(source_visit_dir / partial_path).parent.glob("Atlas_*.xml")
122+
(source_visit_dir / environment.visit / partial_path).parent.glob(
123+
"Atlas_*.xml"
124+
)
135125
)[0]
136126
logger.info(f"Atlas XML path {str(atlas_xml_path)} found")
137127
with open(atlas_xml_path, "rb") as atlas_xml:
138128
atlas_xml_data = xmltodict.parse(atlas_xml)
139-
atlas_original_pixel_size = atlas_xml_data["MicroscopeImage"][
140-
"SpatialScale"
141-
]["pixelSize"]["x"]["numericValue"]
129+
atlas_original_pixel_size = float(
130+
atlas_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
131+
"numericValue"
132+
]
133+
)
142134

143135
# need to calculate the pixel size of the downscaled image
144136
atlas_pixel_size = atlas_original_pixel_size * 7.8
@@ -190,7 +182,7 @@ def post_transfer(
190182
)
191183

192184
elif transferred_file.suffix == ".dm" and environment:
193-
gs_name = transferred_file.name.split("_")[1]
185+
gs_name = transferred_file.stem.split("_")[1]
194186
fh_positions = _foil_hole_positions(transferred_file, int(gs_name))
195187
source = _get_source(transferred_file, environment=environment)
196188
visitless_source = str(source).replace(f"/{environment.visit}", "")

src/murfey/server/ispyb.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def do_update_grid_square(
253253
def do_insert_foil_hole(
254254
self,
255255
grid_square_id: int,
256-
scale_factor: float,
256+
scale_factor: Optional[float],
257257
foil_hole_parameters: FoilHoleParameters,
258258
):
259259
if (
@@ -265,21 +265,22 @@ def do_insert_foil_hole(
265265
foil_hole_parameters.readout_area_x
266266
/ foil_hole_parameters.thumbnail_size_x
267267
)
268-
foil_hole_parameters.diameter = (
269-
int(foil_hole_parameters.diameter * scale_factor)
270-
if foil_hole_parameters.diameter
271-
else None
272-
)
273-
foil_hole_parameters.x_location = (
274-
int(foil_hole_parameters.x_location * scale_factor)
275-
if foil_hole_parameters.x_location
276-
else None
277-
)
278-
foil_hole_parameters.y_location = (
279-
int(foil_hole_parameters.y_location * scale_factor)
280-
if foil_hole_parameters.y_location
281-
else None
282-
)
268+
if scale_factor:
269+
foil_hole_parameters.diameter = (
270+
int(foil_hole_parameters.diameter * scale_factor)
271+
if foil_hole_parameters.diameter
272+
else None
273+
)
274+
foil_hole_parameters.x_location = (
275+
int(foil_hole_parameters.x_location * scale_factor)
276+
if foil_hole_parameters.x_location
277+
else None
278+
)
279+
foil_hole_parameters.y_location = (
280+
int(foil_hole_parameters.y_location * scale_factor)
281+
if foil_hole_parameters.y_location
282+
else None
283+
)
283284
record = FoilHole(
284285
gridSquareId=grid_square_id,
285286
foilHoleLabel=foil_hole_parameters.name,

src/murfey/util/spa_metadata.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,36 +148,37 @@ def grid_square_data(xml_path: Path, grid_square: int) -> GridSquareInfo:
148148

149149

150150
def foil_hole_data(xml_path: Path, foil_hole: int, grid_square: int) -> FoilHoleInfo:
151-
with open(xml_path, "r") as xml:
152-
for_parsing = xml.read()
153-
data = xmltodict.parse(for_parsing)
154-
data = data["GridSquareXml"]
155-
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][
156-
"a:m_serializationArray"
157-
]
158151
required_key = ""
159-
for key in serialization_array.keys():
160-
if key.startswith("b:KeyValuePairOfintTargetLocation"):
161-
required_key = key
162-
break
163-
if required_key:
164-
image_paths = list(
165-
(xml_path.parent.parent).glob(
166-
f"Images-Disc*/GridSquare_{grid_square}/FoilHoles/FoilHole_{foil_hole}_*.jpg"
167-
)
152+
if xml_path.is_file():
153+
with open(xml_path, "r") as xml:
154+
for_parsing = xml.read()
155+
data = xmltodict.parse(for_parsing)
156+
data = data["GridSquareXml"]
157+
serialization_array = data["TargetLocations"]["TargetLocationsEfficient"][
158+
"a:m_serializationArray"
159+
]
160+
for key in serialization_array.keys():
161+
if key.startswith("b:KeyValuePairOfintTargetLocation"):
162+
required_key = key
163+
break
164+
image_paths = list(
165+
(xml_path.parent.parent).glob(
166+
f"Images-Disc*/GridSquare_{grid_square}/FoilHoles/FoilHole_{foil_hole}_*.jpg"
168167
)
169-
image_paths.sort(key=lambda x: x.stat().st_ctime)
170-
image_path: Union[Path, str] = image_paths[-1] if image_paths else ""
171-
if image_path:
172-
with open(Path(image_path).with_suffix(".xml")) as fh_xml:
173-
fh_xml_data = xmltodict.parse(fh_xml.read())
174-
readout_area = fh_xml_data["MicroscopeImage"]["microscopeData"][
175-
"acquisition"
176-
]["camera"]["ReadoutArea"]
177-
pixel_size = fh_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"][
178-
"x"
179-
]["numericValue"]
180-
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
168+
)
169+
image_paths.sort(key=lambda x: x.stat().st_ctime)
170+
image_path: Union[Path, str] = image_paths[-1] if image_paths else ""
171+
if image_path:
172+
with open(Path(image_path).with_suffix(".xml")) as fh_xml:
173+
fh_xml_data = xmltodict.parse(fh_xml.read())
174+
readout_area = fh_xml_data["MicroscopeImage"]["microscopeData"]["acquisition"][
175+
"camera"
176+
]["ReadoutArea"]
177+
pixel_size = fh_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
178+
"numericValue"
179+
]
180+
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
181+
if required_key:
181182
for fh_block in serialization_array[required_key]:
182183
pix = fh_block["b:value"]["PixelCenter"]
183184
stage = fh_block["b:value"]["StagePosition"]
@@ -198,6 +199,17 @@ def foil_hole_data(xml_path: Path, foil_hole: int, grid_square: int) -> FoilHole
198199
image=str(image_path),
199200
diameter=diameter,
200201
)
202+
elif image_path:
203+
return FoilHoleInfo(
204+
id=foil_hole,
205+
grid_square_id=grid_square,
206+
readout_area_x=full_size[0] if image_path else None,
207+
readout_area_y=full_size[1] if image_path else None,
208+
thumbnail_size_x=None,
209+
thumbnail_size_y=None,
210+
pixel_size=float(pixel_size) if image_path else None,
211+
image=str(image_path),
212+
)
201213
logger.warning(
202214
f"Foil hole positions could not be determined from metadata file {xml_path} for foil hole {foil_hole}"
203215
)

src/murfey/workflows/spa/flush_spa_preprocess.py

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ def register_grid_square(
3939
.where(GridSquare.tag == grid_square_params.tag)
4040
.where(GridSquare.session_id == session_id)
4141
).one()
42-
grid_square.x_location = grid_square_params.x_location
43-
grid_square.y_location = grid_square_params.y_location
44-
grid_square.x_stage_position = grid_square_params.x_stage_position
45-
grid_square.y_stage_position = grid_square_params.y_stage_position
42+
grid_square.x_location = grid_square_params.x_location or grid_square.x_location
43+
grid_square.y_location = grid_square_params.y_location or grid_square.y_location
44+
grid_square.x_stage_position = (
45+
grid_square_params.x_stage_position or grid_square.x_stage_position
46+
)
47+
grid_square.y_stage_position = (
48+
grid_square_params.y_stage_position or grid_square.y_stage_position
49+
)
4650
if _transport_object:
4751
_transport_object.do_update_grid_square(grid_square.id, grid_square_params)
4852
except Exception:
@@ -122,23 +126,37 @@ def register_foil_hole(
122126
.where(FoilHole.grid_square_id == gsid)
123127
.where(FoilHole.session_id == session_id)
124128
).one()
125-
foil_hole.x_location = foil_hole_params.x_location
126-
foil_hole.y_location = foil_hole_params.y_location
127-
foil_hole.x_stage_position = foil_hole_params.x_stage_position
128-
foil_hole.y_stage_position = foil_hole_params.y_stage_position
129-
foil_hole.readout_area_x = foil_hole_params.readout_area_x
130-
foil_hole.readout_area_y = foil_hole_params.readout_area_y
131-
foil_hole.thumbnail_size_x = foil_hole_params.thumbnail_size_x or jpeg_size[0]
132-
foil_hole.thumbnail_size_y = foil_hole_params.thumbnail_size_y or jpeg_size[1]
133-
foil_hole.pixel_size = foil_hole_params.pixel_size
134-
if _transport_object:
129+
foil_hole.x_location = foil_hole_params.x_location or foil_hole.x_location
130+
foil_hole.y_location = foil_hole_params.y_location or foil_hole.y_location
131+
foil_hole.x_stage_position = (
132+
foil_hole_params.x_stage_position or foil_hole.x_stage_position
133+
)
134+
foil_hole.y_stage_position = (
135+
foil_hole_params.y_stage_position or foil_hole.y_stage_position
136+
)
137+
foil_hole.readout_area_x = (
138+
foil_hole_params.readout_area_x or foil_hole.readout_area_x
139+
)
140+
foil_hole.readout_area_y = (
141+
foil_hole_params.readout_area_y or foil_hole.readout_area_y
142+
)
143+
foil_hole.thumbnail_size_x = (
144+
foil_hole_params.thumbnail_size_x or foil_hole.thumbnail_size_x
145+
) or jpeg_size[0]
146+
foil_hole.thumbnail_size_y = (
147+
foil_hole_params.thumbnail_size_y or foil_hole.thumbnail_size_y
148+
) or jpeg_size[1]
149+
foil_hole.pixel_size = foil_hole_params.pixel_size or foil_hole.pixel_size
150+
if _transport_object and gs.readout_area_x:
135151
_transport_object.do_update_foil_hole(
136152
foil_hole.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
137153
)
138154
except Exception:
139155
if _transport_object:
140156
fh_ispyb_response = _transport_object.do_insert_foil_hole(
141-
gs.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
157+
gs.id,
158+
gs.thumbnail_size_x / gs.readout_area_x if gs.readout_area_x else None,
159+
foil_hole_params,
142160
)
143161
else:
144162
fh_ispyb_response = {"success": False, "return_value": None}
@@ -334,7 +352,8 @@ def flush_spa_preprocessing(message: dict, db: Session, demo: bool = False):
334352
)
335353
except Exception as e:
336354
logger.error(
337-
f"Flushing position analysis for {f.file_path} caused exception {e}", exc_info=True
355+
f"Flushing position analysis for {f.file_path} caused exception {e}",
356+
exc_info=True,
338357
)
339358
foil_hole_id = None
340359

0 commit comments

Comments
 (0)