Skip to content

Commit 2b5d793

Browse files
committed
Cover the case where the .dm file is seen before the grid square XML so that the foil hole positions can't be determined until the XML is seen, at which point there needs to be an update
1 parent 331a2d2 commit 2b5d793

File tree

4 files changed

+86
-60
lines changed

4 files changed

+86
-60
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/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: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -148,36 +148,38 @@ 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+
required_key = ""
161+
for key in serialization_array.keys():
162+
if key.startswith("b:KeyValuePairOfintTargetLocation"):
163+
required_key = key
164+
break
165+
image_paths = list(
166+
(xml_path.parent.parent).glob(
167+
f"Images-Disc*/GridSquare_{grid_square}/FoilHoles/FoilHole_{foil_hole}_*.jpg"
168168
)
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"]))
169+
)
170+
image_paths.sort(key=lambda x: x.stat().st_ctime)
171+
image_path: Union[Path, str] = image_paths[-1] if image_paths else ""
172+
if image_path:
173+
with open(Path(image_path).with_suffix(".xml")) as fh_xml:
174+
fh_xml_data = xmltodict.parse(fh_xml.read())
175+
readout_area = fh_xml_data["MicroscopeImage"]["microscopeData"]["acquisition"][
176+
"camera"
177+
]["ReadoutArea"]
178+
pixel_size = fh_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][
179+
"numericValue"
180+
]
181+
full_size = (int(readout_area["a:width"]), int(readout_area["a:height"]))
182+
if required_key:
181183
for fh_block in serialization_array[required_key]:
182184
pix = fh_block["b:value"]["PixelCenter"]
183185
stage = fh_block["b:value"]["StagePosition"]
@@ -198,6 +200,17 @@ def foil_hole_data(xml_path: Path, foil_hole: int, grid_square: int) -> FoilHole
198200
image=str(image_path),
199201
diameter=diameter,
200202
)
203+
elif image_path:
204+
return FoilHoleInfo(
205+
id=foil_hole,
206+
grid_square_id=grid_square,
207+
readout_area_x=full_size[0] if image_path else None,
208+
readout_area_y=full_size[1] if image_path else None,
209+
thumbnail_size_x=None,
210+
thumbnail_size_y=None,
211+
pixel_size=float(pixel_size) if image_path else None,
212+
image=str(image_path),
213+
)
201214
logger.warning(
202215
f"Foil hole positions could not be determined from metadata file {xml_path} for foil hole {foil_hole}"
203216
)

src/murfey/workflows/spa/flush_spa_preprocess.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,37 @@ def register_foil_hole(
122122
.where(FoilHole.grid_square_id == gsid)
123123
.where(FoilHole.session_id == session_id)
124124
).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:
125+
foil_hole.x_location = foil_hole_params.x_location or foil_hole.x_location
126+
foil_hole.y_location = foil_hole_params.y_location or foil_hole.y_location
127+
foil_hole.x_stage_position = (
128+
foil_hole_params.x_stage_position or foil_hole.x_stage_position
129+
)
130+
foil_hole.y_stage_position = (
131+
foil_hole_params.y_stage_position or foil_hole.y_stage_position
132+
)
133+
foil_hole.readout_area_x = (
134+
foil_hole_params.readout_area_x or foil_hole.readout_area_x
135+
)
136+
foil_hole.readout_area_y = (
137+
foil_hole_params.readout_area_y or foil_hole.readout_area_y
138+
)
139+
foil_hole.thumbnail_size_x = (
140+
foil_hole_params.thumbnail_size_x or foil_hole.thumbnail_size_x
141+
) or jpeg_size[0]
142+
foil_hole.thumbnail_size_y = (
143+
foil_hole_params.thumbnail_size_y or foil_hole.thumbnail_size_y
144+
) or jpeg_size[1]
145+
foil_hole.pixel_size = foil_hole_params.pixel_size or foil_hole.pixel_size
146+
if _transport_object and gs.readout_area_x:
135147
_transport_object.do_update_foil_hole(
136148
foil_hole.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
137149
)
138150
except Exception:
139151
if _transport_object:
140152
fh_ispyb_response = _transport_object.do_insert_foil_hole(
141-
gs.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
153+
gs.id,
154+
gs.thumbnail_size_x / gs.readout_area_x if gs.readout_area_x else None,
155+
foil_hole_params,
142156
)
143157
else:
144158
fh_ispyb_response = {"success": False, "return_value": None}
@@ -334,7 +348,8 @@ def flush_spa_preprocessing(message: dict, db: Session, demo: bool = False):
334348
)
335349
except Exception as e:
336350
logger.error(
337-
f"Flushing position analysis for {f.file_path} caused exception {e}", exc_info=True
351+
f"Flushing position analysis for {f.file_path} caused exception {e}",
352+
exc_info=True,
338353
)
339354
foil_hole_id = None
340355

0 commit comments

Comments
 (0)