Skip to content

Commit dd8f649

Browse files
Get the longest source, and log failed Images-Disc searches (#480)
The globs for `Images-Disc*` were not working, and this attempts to fix them again. If they fail, it logs the directory being searched. This also includes a few fixes to the database inserts for grid squares and foil holes
1 parent 3f11363 commit dd8f649

File tree

4 files changed

+80
-70
lines changed

4 files changed

+80
-70
lines changed

src/murfey/client/contexts/spa.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,19 @@ def _grid_square_metadata_file(
7979

8080

8181
def _get_source(file_path: Path, environment: MurfeyInstanceEnvironment) -> Path | None:
82+
possible_sources = []
8283
for s in environment.sources:
8384
if file_path.is_relative_to(s):
84-
return s
85-
return None
85+
possible_sources.append(s)
86+
if not possible_sources:
87+
return None
88+
elif len(possible_sources) == 1:
89+
return possible_sources[0]
90+
source = possible_sources[0]
91+
for extra_source in possible_sources[1:]:
92+
if extra_source.is_relative_to(source):
93+
source = extra_source
94+
return source
8695

8796

8897
def _get_xml_list_index(key: str, xml_list: list) -> int:

src/murfey/client/contexts/spa_metadata.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ def _atlas_destination(
6666
instrument_name=environment.instrument_name,
6767
demo=environment.demo,
6868
)
69-
if environment.visit in environment.default_destinations[source]:
70-
return (
71-
Path(machine_config.get("rsync_basepath", ""))
72-
/ Path(environment.default_destinations[source]).parent
73-
)
69+
for i, destination_part in enumerate(
70+
Path(environment.default_destinations[source]).parts
71+
):
72+
if destination_part == environment.visit:
73+
return Path(machine_config.get("rsync_basepath", "")) / "/".join(
74+
Path(environment.default_destinations[source]).parent.parts[: i + 1]
75+
)
7476
return (
7577
Path(machine_config.get("rsync_basepath", ""))
7678
/ Path(environment.default_destinations[source]).parent
@@ -117,12 +119,10 @@ def post_transfer(
117119
source_visit_dir = source.parent
118120

119121
logger.info(
120-
f"Looking for atlas XML file in metadata directory {str((source_visit_dir / environment.visit / partial_path).parent)}"
122+
f"Looking for atlas XML file in metadata directory {str((source_visit_dir / partial_path).parent)}"
121123
)
122124
atlas_xml_path = list(
123-
(source_visit_dir / environment.visit / partial_path).parent.glob(
124-
"Atlas_*.xml"
125-
)
125+
(source_visit_dir / partial_path).parent.glob("Atlas_*.xml")
126126
)[0]
127127
logger.info(f"Atlas XML path {str(atlas_xml_path)} found")
128128
with open(atlas_xml_path, "rb") as atlas_xml:
@@ -137,7 +137,6 @@ def post_transfer(
137137
atlas_pixel_size = atlas_original_pixel_size * 7.8
138138
logger.info(f"Atlas image pixel size determined to be {atlas_pixel_size}")
139139

140-
sample = None
141140
for p in partial_path.split("/"):
142141
if p.startswith("Sample"):
143142
sample = int(p.replace("Sample", ""))
@@ -150,31 +149,34 @@ def post_transfer(
150149
atlas=Path(partial_path), sample=sample
151150
)
152151
url = f"{str(environment.url.geturl())}/visits/{environment.visit}/{environment.murfey_session}/register_data_collection_group"
153-
dcg_search_dir = "/".join(
152+
dcg_search_dir = "/" + "/".join(
154153
p
155154
for p in transferred_file.parent.parts[1:]
156155
if p != environment.visit
157156
)
158-
dcg_tag = str(
159-
sorted(
160-
Path(dcg_search_dir).glob("Images-Disc*"),
161-
key=lambda x: x.stat().st_ctime,
162-
)[-1]
157+
dcg_images_dirs = sorted(
158+
Path(dcg_search_dir).glob("Images-Disc*"),
159+
key=lambda x: x.stat().st_ctime,
163160
)
161+
if not dcg_images_dirs:
162+
logger.warning(f"Cannot find Images-Disc* in {dcg_search_dir}")
163+
return
164+
dcg_tag = str(dcg_images_dirs[-1])
164165
dcg_data = {
165166
"experiment_type": "single particle",
166167
"experiment_type_id": 37,
167168
"tag": dcg_tag,
168169
"atlas": str(
169170
_atlas_destination(environment, source, transferred_file)
170-
/ environment.samples[source].atlas
171+
/ environment.samples[source].atlas.parent
172+
/ atlas_xml_path.with_suffix(".jpg").name
171173
),
172174
"sample": environment.samples[source].sample,
173175
"atlas_pixel_size": atlas_pixel_size,
174176
}
175177
capture_post(url, json=dcg_data)
176178
gs_pix_positions = get_grid_square_atlas_positions(
177-
source_visit_dir / environment.visit / partial_path
179+
source_visit_dir / partial_path
178180
)
179181
for gs, pos_data in gs_pix_positions.items():
180182
if pos_data:
@@ -206,12 +208,16 @@ def post_transfer(
206208
visitless_source_search_dir = str(source).replace(
207209
f"/{environment.visit}", ""
208210
)
209-
visitless_source = str(
210-
sorted(
211-
Path(visitless_source_search_dir).glob("Images-Disc*"),
212-
key=lambda x: x.stat().st_ctime,
213-
)[-1]
211+
visitless_source_images_dirs = sorted(
212+
Path(visitless_source_search_dir).glob("Images-Disc*"),
213+
key=lambda x: x.stat().st_ctime,
214214
)
215+
if not visitless_source_images_dirs:
216+
logger.warning(
217+
f"Cannot find Images-Disc* in {visitless_source_search_dir}"
218+
)
219+
return
220+
visitless_source = str(visitless_source_images_dirs[-1])
215221
for fh, fh_data in fh_positions.items():
216222
capture_post(
217223
f"{str(environment.url.geturl())}/sessions/{environment.murfey_session}/grid_square/{gs_name}/foil_hole",

src/murfey/server/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,6 +2566,7 @@ def feedback_callback(header: dict, message: dict) -> None:
25662566
message["atlas_pixel_size"],
25672567
message["sample"],
25682568
)
2569+
_transport_object.transport.ack(header)
25692570
return None
25702571
elif message["register"] == "data_collection":
25712572
murfey_session_id = message["session_id"]

src/murfey/server/ispyb.py

Lines changed: 39 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -213,32 +213,28 @@ def do_update_grid_square(
213213
grid_square_parameters.readout_area_x
214214
/ grid_square_parameters.thumbnail_size_x
215215
)
216-
grid_square.gridSquareImage = grid_square_parameters.image
217-
grid_square.pixelLocationX = (
218-
int(grid_square_parameters.x_location / 7.8)
219-
if grid_square_parameters.x_location
220-
else None
221-
)
222-
grid_square.pixelLocationY = (
223-
int(grid_square_parameters.y_location / 7.8)
224-
if grid_square_parameters.y_location
225-
else None
226-
)
227-
grid_square.pixelLocationY = grid_square_parameters.y_location
228-
grid_square.height = (
229-
int(grid_square_parameters.height / 7.8)
230-
if grid_square_parameters.height is not None
231-
else None
232-
)
233-
grid_square.width = (
234-
int(grid_square_parameters.width / 7.8)
235-
if grid_square_parameters.width is not None
236-
else None
237-
)
238-
grid_square.angle = grid_square_parameters.angle
239-
grid_square.stageLocationX = grid_square_parameters.x_stage_position
240-
grid_square.stageLocationY = grid_square_parameters.y_stage_position
241-
grid_square.pixelSize = grid_square_parameters.pixel_size
216+
if grid_square_parameters.image:
217+
grid_square.gridSquareImage = grid_square_parameters.image
218+
if grid_square_parameters.x_location:
219+
grid_square.pixelLocationX = int(
220+
grid_square_parameters.x_location / 7.8
221+
)
222+
if grid_square_parameters.y_location:
223+
grid_square.pixelLocationY = int(
224+
grid_square_parameters.y_location / 7.8
225+
)
226+
if grid_square_parameters.height is not None:
227+
grid_square.height = int(grid_square_parameters.height / 7.8)
228+
if grid_square_parameters.width is not None:
229+
grid_square.width = int(grid_square_parameters.width / 7.8)
230+
if grid_square_parameters.angle:
231+
grid_square.angle = grid_square_parameters.angle
232+
if grid_square_parameters.x_stage_position:
233+
grid_square.stageLocationX = grid_square_parameters.x_stage_position
234+
if grid_square_parameters.y_stage_position:
235+
grid_square.stageLocationY = grid_square_parameters.y_stage_position
236+
if grid_square_parameters.pixel_size:
237+
grid_square.pixelSize = grid_square_parameters.pixel_size
242238
db.add(grid_square)
243239
db.commit()
244240
return {"success": True, "return_value": grid_square.gridSquareId}
@@ -296,7 +292,7 @@ def do_insert_foil_hole(
296292
with Session() as db:
297293
db.add(record)
298294
db.commit()
299-
log.info(f"Created FoilHole {record.gridSquareId}")
295+
log.info(f"Created FoilHole {record.foilHoleId}")
300296
return {"success": True, "return_value": record.foilHoleId}
301297
except ispyb.ISPyBException as e:
302298
log.error(
@@ -317,24 +313,22 @@ def do_update_foil_hole(
317313
foil_hole = (
318314
db.query(FoilHole).filter(FoilHole.foilHoleId == foil_hole_id).one()
319315
)
320-
foil_hole.foilHoleImage = foil_hole_parameters.image
321-
foil_hole.pixelLocationX = (
322-
int(foil_hole_parameters.x_location * scale_factor)
323-
if foil_hole_parameters.x_location
324-
else None
325-
)
326-
foil_hole.pixelLocationY = (
327-
int(foil_hole_parameters.y_location * scale_factor)
328-
if foil_hole_parameters.y_location
329-
else None
330-
)
331-
foil_hole.diameter = (
332-
foil_hole_parameters.diameter * scale_factor
333-
if foil_hole_parameters.diameter is not None
334-
else None
335-
)
336-
foil_hole.stageLocationX = foil_hole_parameters.x_stage_position
337-
foil_hole.stageLocationY = foil_hole_parameters.y_stage_position
316+
if foil_hole_parameters.image:
317+
foil_hole.foilHoleImage = foil_hole_parameters.image
318+
if foil_hole_parameters.x_location:
319+
foil_hole.pixelLocationX = int(
320+
foil_hole_parameters.x_location * scale_factor
321+
)
322+
if foil_hole_parameters.y_location:
323+
foil_hole.pixelLocationY = int(
324+
foil_hole_parameters.y_location * scale_factor
325+
)
326+
if foil_hole_parameters.diameter is not None:
327+
foil_hole.diameter = foil_hole_parameters.diameter * scale_factor
328+
if foil_hole_parameters.x_stage_position:
329+
foil_hole.stageLocationX = foil_hole_parameters.x_stage_position
330+
if foil_hole_parameters.y_stage_position:
331+
foil_hole.stageLocationY = foil_hole_parameters.y_stage_position
338332
if (
339333
foil_hole_parameters.readout_area_x is not None
340334
and foil_hole_parameters.thumbnail_size_x is not None

0 commit comments

Comments
 (0)