Skip to content

Commit 7c2a7d9

Browse files
Ensure foil hole id is in the database before inserting movie (#470)
1 parent a8645e9 commit 7c2a7d9

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/murfey/workflows/spa/flush_spa_preprocess.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def register_foil_hole(
110110
gs_name: int,
111111
foil_hole_params: FoilHoleParameters,
112112
murfey_db: Session,
113-
):
113+
) -> Optional[int]:
114114
try:
115115
gs = murfey_db.exec(
116116
select(GridSquare)
@@ -120,10 +120,10 @@ def register_foil_hole(
120120
).one()
121121
gsid = gs.id
122122
except NoResultFound:
123-
logger.debug(
123+
logger.warning(
124124
f"Foil hole {sanitise(str(foil_hole_params.name))} could not be registered as grid square {sanitise(str(gs_name))} was not found"
125125
)
126-
return
126+
return None
127127
secured_foil_hole_image_path = secure_filename(foil_hole_params.image)
128128
if foil_hole_params.image and Path(secured_foil_hole_image_path).is_file():
129129
jpeg_size = Image.open(secured_foil_hole_image_path).size
@@ -193,6 +193,7 @@ def register_foil_hole(
193193
murfey_db.add(foil_hole)
194194
murfey_db.commit()
195195
murfey_db.close()
196+
return foil_hole.id
196197

197198

198199
def _grid_square_metadata_file(f: Path, grid_square: int) -> Optional[Path]:
@@ -287,8 +288,7 @@ def _flush_position_analysis(
287288
name=foil_hole,
288289
)
289290
# Insert or update this foil hole in the database
290-
register_foil_hole(session_id, gs.id, foil_hole_parameters, murfey_db)
291-
return foil_hole
291+
return register_foil_hole(session_id, gs.id, foil_hole_parameters, murfey_db)
292292

293293

294294
def flush_spa_preprocess(message: dict, murfey_db: Session, demo: bool = False) -> bool:
@@ -349,23 +349,29 @@ def flush_spa_preprocess(message: dict, murfey_db: Session, demo: bool = False)
349349
murfey_db.add(feedback_params)
350350

351351
for i, f in enumerate(stashed_files):
352-
if f.foil_hole_id:
353-
foil_hole_id = f.foil_hole_id
354-
else:
355-
# Register grid square and foil hole if not present
356-
try:
352+
try:
353+
foil_hole_id = None
354+
if f.foil_hole_id:
355+
# Check if the foil hole id has been registered in the database
356+
db_foil_hole = murfey_db.exec(
357+
select(FoilHole).where(FoilHole.id == f.foil_hole_id)
358+
).all()
359+
if db_foil_hole:
360+
foil_hole_id = f.foil_hole_id
361+
if not foil_hole_id:
362+
# Register grid square and foil hole if not present
357363
foil_hole_id = _flush_position_analysis(
358364
movie_path=Path(f.file_path),
359365
dcg_id=collected_ids[0].id,
360366
session_id=session_id,
361367
murfey_db=murfey_db,
362368
)
363-
except Exception as e:
364-
logger.error(
365-
f"Flushing position analysis for {f.file_path} caused exception {e}",
366-
exc_info=True,
367-
)
368-
foil_hole_id = None
369+
except Exception as e:
370+
logger.error(
371+
f"Flushing position analysis for {f.file_path} caused exception {e}",
372+
exc_info=True,
373+
)
374+
foil_hole_id = None
369375

370376
mrcp = Path(f.mrc_out)
371377
ppath = Path(f.file_path)

0 commit comments

Comments
 (0)