Skip to content

Commit 833e9b6

Browse files
Safe inserts of search maps into database (#754)
We currently run a .one() on search maps, grid squares and foil holes in the database. If there is already one, but the ispyb insert fails we trigger the except clause and end up inserting a second one, which then keeps crashing and inserting more as the .one() fails. The except on these inserts has been removed in this PR and instead an .all() is run on the values and the first one taken. If the ispyb update fails this should then fall through to the outer try-except in the feedback.py
1 parent 26e2f89 commit 833e9b6

2 files changed

Lines changed: 32 additions & 25 deletions

File tree

src/murfey/workflows/spa/flush_spa_preprocess.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,15 @@ def register_grid_square(
5353
if grid_square_params.width is not None:
5454
grid_square_params.width_scaled = int(grid_square_params.width / 7.8)
5555

56-
try:
57-
grid_square = murfey_db.exec(
58-
select(GridSquare)
59-
.where(GridSquare.name == gsid)
60-
.where(GridSquare.tag == grid_square_params.tag)
61-
.where(GridSquare.session_id == session_id)
62-
).one()
56+
grid_square_query = murfey_db.exec(
57+
select(GridSquare)
58+
.where(GridSquare.name == gsid)
59+
.where(GridSquare.tag == grid_square_params.tag)
60+
.where(GridSquare.session_id == session_id)
61+
).all()
62+
if grid_square_query:
63+
# Grid square already exists in the murfey database
64+
grid_square = grid_square_query[0]
6365
grid_square.x_location = grid_square_params.x_location or grid_square.x_location
6466
grid_square.y_location = grid_square_params.y_location or grid_square.y_location
6567
grid_square.x_stage_position = (
@@ -84,7 +86,8 @@ def register_grid_square(
8486
grid_square.image = grid_square_params.image or grid_square.image
8587
if _transport_object:
8688
_transport_object.do_update_grid_square(grid_square.id, grid_square_params)
87-
except Exception:
89+
else:
90+
# No existing grid square in the murfey database
8891
if _transport_object:
8992
dcg = murfey_db.exec(
9093
select(DataCollectionGroup)
@@ -151,13 +154,15 @@ def register_foil_hole(
151154
jpeg_size = Image.open(secured_foil_hole_image_path).size
152155
else:
153156
jpeg_size = (0, 0)
154-
try:
155-
foil_hole = murfey_db.exec(
156-
select(FoilHole)
157-
.where(FoilHole.name == foil_hole_params.name)
158-
.where(FoilHole.grid_square_id == gsid)
159-
.where(FoilHole.session_id == session_id)
160-
).one()
157+
foil_hole_query = murfey_db.exec(
158+
select(FoilHole)
159+
.where(FoilHole.name == foil_hole_params.name)
160+
.where(FoilHole.grid_square_id == gsid)
161+
.where(FoilHole.session_id == session_id)
162+
).all()
163+
if foil_hole_query:
164+
# Foil hole already exists in the murfey database
165+
foil_hole = foil_hole_query[0]
161166
foil_hole.x_location = foil_hole_params.x_location or foil_hole.x_location
162167
foil_hole.y_location = foil_hole_params.y_location or foil_hole.y_location
163168
foil_hole.x_stage_position = (
@@ -183,7 +188,8 @@ def register_foil_hole(
183188
_transport_object.do_update_foil_hole(
184189
foil_hole.id, gs.thumbnail_size_x / gs.readout_area_x, foil_hole_params
185190
)
186-
except Exception:
191+
else:
192+
# No existing foil hole in the murfey database
187193
if _transport_object:
188194
fh_ispyb_response = _transport_object.do_insert_foil_hole(
189195
gs.id,

src/murfey/workflows/tomo/tomo_metadata.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@ def register_search_map_in_database(
3131
.where(DataCollectionGroup.session_id == session_id)
3232
.where(DataCollectionGroup.tag == search_map_params.tag)
3333
).one()
34-
try:
34+
search_map_query = murfey_db.exec(
35+
select(SearchMap)
36+
.where(SearchMap.name == search_map_name)
37+
.where(SearchMap.tag == search_map_params.tag)
38+
.where(SearchMap.session_id == session_id)
39+
).all()
40+
if search_map_query:
3541
# See if there is already a search map with this name and update if so
36-
search_map = murfey_db.exec(
37-
select(SearchMap)
38-
.where(SearchMap.name == search_map_name)
39-
.where(SearchMap.tag == search_map_params.tag)
40-
.where(SearchMap.session_id == session_id)
41-
).one()
42+
search_map = search_map_query[0]
4243
search_map.x_stage_position = (
4344
search_map_params.x_stage_position or search_map.x_stage_position
4445
)
@@ -100,8 +101,8 @@ def register_search_map_in_database(
100101
search_map.width = search_map_params.width or search_map.width
101102
if _transport_object:
102103
_transport_object.do_update_search_map(search_map.id, search_map_params)
103-
except Exception as e:
104-
logger.info(f"Registering new search map due to {e}", exc_info=True)
104+
else:
105+
logger.info(f"Registering new search map {sanitise(search_map_name)}")
105106
if _transport_object:
106107
sm_ispyb_response = _transport_object.do_insert_search_map(
107108
dcg.atlas_id, search_map_params

0 commit comments

Comments
 (0)