From 6ec31057af1498856982bd05bb22b5e032820b0e Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Tue, 28 Jan 2025 09:21:12 +0000 Subject: [PATCH 1/3] Ensure foil hole id is in the database before inserting movie --- .../workflows/spa/flush_spa_preprocess.py | 28 +++++++++++-------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/murfey/workflows/spa/flush_spa_preprocess.py b/src/murfey/workflows/spa/flush_spa_preprocess.py index d0c220b5a..3394298eb 100644 --- a/src/murfey/workflows/spa/flush_spa_preprocess.py +++ b/src/murfey/workflows/spa/flush_spa_preprocess.py @@ -349,23 +349,29 @@ def flush_spa_preprocess(message: dict, murfey_db: Session, demo: bool = False) murfey_db.add(feedback_params) for i, f in enumerate(stashed_files): - if f.foil_hole_id: - foil_hole_id = f.foil_hole_id - else: - # Register grid square and foil hole if not present - try: + try: + foil_hole_id = None + if f.foil_hole_id: + # Check if the foil hole id has been registered in the database + db_foil_hole = murfey_db.exec( + select(FoilHole).where(FoilHole.id == f.foil_hole_id) + ).all() + if db_foil_hole: + foil_hole_id = f.foil_hole_id + if not foil_hole_id: + # Register grid square and foil hole if not present foil_hole_id = _flush_position_analysis( movie_path=Path(f.file_path), dcg_id=collected_ids[0].id, session_id=session_id, murfey_db=murfey_db, ) - except Exception as e: - logger.error( - f"Flushing position analysis for {f.file_path} caused exception {e}", - exc_info=True, - ) - foil_hole_id = None + except Exception as e: + logger.error( + f"Flushing position analysis for {f.file_path} caused exception {e}", + exc_info=True, + ) + foil_hole_id = None mrcp = Path(f.mrc_out) ppath = Path(f.file_path) From 76f7136055e6ad6b59bbdb318d25dcb311da5e13 Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Tue, 28 Jan 2025 09:27:02 +0000 Subject: [PATCH 2/3] Need to ensure hole is present before returning it --- src/murfey/workflows/spa/flush_spa_preprocess.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/murfey/workflows/spa/flush_spa_preprocess.py b/src/murfey/workflows/spa/flush_spa_preprocess.py index 3394298eb..cb8de11e6 100644 --- a/src/murfey/workflows/spa/flush_spa_preprocess.py +++ b/src/murfey/workflows/spa/flush_spa_preprocess.py @@ -110,7 +110,7 @@ def register_foil_hole( gs_name: int, foil_hole_params: FoilHoleParameters, murfey_db: Session, -): +) -> bool: try: gs = murfey_db.exec( select(GridSquare) @@ -120,10 +120,10 @@ def register_foil_hole( ).one() gsid = gs.id except NoResultFound: - logger.debug( + logger.warning( f"Foil hole {sanitise(str(foil_hole_params.name))} could not be registered as grid square {sanitise(str(gs_name))} was not found" ) - return + return False secured_foil_hole_image_path = secure_filename(foil_hole_params.image) if foil_hole_params.image and Path(secured_foil_hole_image_path).is_file(): jpeg_size = Image.open(secured_foil_hole_image_path).size @@ -193,6 +193,7 @@ def register_foil_hole( murfey_db.add(foil_hole) murfey_db.commit() murfey_db.close() + return True def _grid_square_metadata_file(f: Path, grid_square: int) -> Optional[Path]: @@ -287,8 +288,10 @@ def _flush_position_analysis( name=foil_hole, ) # Insert or update this foil hole in the database - register_foil_hole(session_id, gs.id, foil_hole_parameters, murfey_db) - return foil_hole + registered_hole = register_foil_hole( + session_id, gs.id, foil_hole_parameters, murfey_db + ) + return foil_hole if registered_hole else None def flush_spa_preprocess(message: dict, murfey_db: Session, demo: bool = False) -> bool: From b45ec535532ccf7f54d4a787639a845e0a8cc224 Mon Sep 17 00:00:00 2001 From: yxd92326 Date: Tue, 28 Jan 2025 09:49:18 +0000 Subject: [PATCH 3/3] Wrong id probably being returned --- src/murfey/workflows/spa/flush_spa_preprocess.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/murfey/workflows/spa/flush_spa_preprocess.py b/src/murfey/workflows/spa/flush_spa_preprocess.py index cb8de11e6..d8ab71d83 100644 --- a/src/murfey/workflows/spa/flush_spa_preprocess.py +++ b/src/murfey/workflows/spa/flush_spa_preprocess.py @@ -110,7 +110,7 @@ def register_foil_hole( gs_name: int, foil_hole_params: FoilHoleParameters, murfey_db: Session, -) -> bool: +) -> Optional[int]: try: gs = murfey_db.exec( select(GridSquare) @@ -123,7 +123,7 @@ def register_foil_hole( logger.warning( f"Foil hole {sanitise(str(foil_hole_params.name))} could not be registered as grid square {sanitise(str(gs_name))} was not found" ) - return False + return None secured_foil_hole_image_path = secure_filename(foil_hole_params.image) if foil_hole_params.image and Path(secured_foil_hole_image_path).is_file(): jpeg_size = Image.open(secured_foil_hole_image_path).size @@ -193,7 +193,7 @@ def register_foil_hole( murfey_db.add(foil_hole) murfey_db.commit() murfey_db.close() - return True + return foil_hole.id def _grid_square_metadata_file(f: Path, grid_square: int) -> Optional[Path]: @@ -288,10 +288,7 @@ def _flush_position_analysis( name=foil_hole, ) # Insert or update this foil hole in the database - registered_hole = register_foil_hole( - session_id, gs.id, foil_hole_parameters, murfey_db - ) - return foil_hole if registered_hole else None + return register_foil_hole(session_id, gs.id, foil_hole_parameters, murfey_db) def flush_spa_preprocess(message: dict, murfey_db: Session, demo: bool = False) -> bool: