1111from importlib .resources import files
1212from pathlib import Path
1313from threading import Thread
14- from typing import Any , Dict , List , Literal , NamedTuple , Tuple
14+ from typing import Any , Dict , List , Literal , NamedTuple , Optional , Tuple
1515
1616import graypy
1717import mrcfile
6060)
6161from murfey .util .models import FoilHoleParameters , GridSquareParameters
6262from murfey .util .processing_params import default_spa_parameters
63- from murfey .util .spa .spa_metadata import (
63+ from murfey .util .spa_metadata import (
64+ GridSquare ,
6465 _foil_hole_data ,
6566 _foil_hole_from_file ,
6667 _get_grid_square_atlas_positions ,
@@ -1939,25 +1940,35 @@ def _register_initial_model(message: dict, _db=murfey_db, demo: bool = False):
19391940 _db .close ()
19401941
19411942
1942- def _grid_square_metadata_file (f : Path , grid_square : int ) -> Path :
1943+ def _grid_square_metadata_file (f : Path , grid_square : int ) -> Optional [Path ]:
1944+ """Search through metadata directories to find the required grid square dm"""
19431945 raw_dir = f .parent .parent .parent
19441946 metadata_dirs = raw_dir .glob ("metadata*" )
19451947 for md_dir in metadata_dirs :
19461948 gs_path = md_dir / f"Metadata/GridSquare_{ grid_square } .dm"
19471949 if gs_path .is_file ():
19481950 return gs_path
1949- raise ValueError (f"Could not determine grid square metadata path for { f } " )
1951+ logger .error (f"Could not determine grid square metadata path for { f } " )
1952+ return None
19501953
19511954
1952- def _flush_position_analysis (movie_path : Path , dcg_id : int , session_id : int ) -> int :
1955+ def _flush_position_analysis (
1956+ movie_path : Path , dcg_id : int , session_id : int
1957+ ) -> Optional [int ]:
1958+ """Register a grid square and foil hole in the database"""
1959+ # Work out the grid square and associated metadata file
19531960 grid_square = _grid_square_from_file (movie_path )
19541961 grid_square_metadata_file = _grid_square_metadata_file (movie_path , grid_square )
1955- gs = _grid_square_data (grid_square_metadata_file , grid_square )
1962+ if grid_square_metadata_file :
1963+ gs = _grid_square_data (grid_square_metadata_file , grid_square )
1964+ else :
1965+ gs = GridSquare (id = grid_square )
19561966
19571967 data_collection_group = murfey_db .exec (
19581968 select (db .DataCollectionGroup ).where (db .DataCollectionGroup .id == dcg_id )
19591969 ).one ()
19601970 if data_collection_group .atlas :
1971+ # If an atlas if present, work out where this grid square is on it
19611972 gs_pix_position = _get_grid_square_atlas_positions (
19621973 data_collection_group .atlas ,
19631974 grid_square = str (grid_square ),
@@ -1979,6 +1990,7 @@ def _flush_position_analysis(movie_path: Path, dcg_id: int, session_id: int) ->
19791990 angle = gs_pix_position [6 ],
19801991 )
19811992 else :
1993+ # Skip location analysis if no atlas
19821994 grid_square_parameters = GridSquareParameters (
19831995 tag = data_collection_group .tag ,
19841996 readout_area_x = gs .readout_area_x ,
@@ -1988,11 +2000,11 @@ def _flush_position_analysis(movie_path: Path, dcg_id: int, session_id: int) ->
19882000 pixel_size = gs .pixel_size ,
19892001 image = gs .image ,
19902002 )
1991-
19922003 register_grid_square (session_id , gs .id , grid_square_parameters , murfey_db )
19932004
2005+ # Find the foil hole info and register it
19942006 foil_hole = _foil_hole_from_file (movie_path )
1995- if grid_square_metadata_file . is_file () :
2007+ if grid_square_metadata_file :
19962008 fh = _foil_hole_data (
19972009 grid_square_metadata_file ,
19982010 foil_hole ,
@@ -2083,6 +2095,7 @@ def _flush_spa_preprocessing(message: dict):
20832095 if f .foil_hole_id :
20842096 foil_hole_id = f .foil_hole_id
20852097 else :
2098+ # Register grid square and foil hole if not present
20862099 foil_hole_id = _flush_position_analysis (
20872100 movie_path = f .file_path ,
20882101 dcg_id = collected_ids [0 ].id ,
0 commit comments