@@ -1930,6 +1930,100 @@ def _register_initial_model(message: dict, _db=murfey_db, demo: bool = False):
19301930 _db .close ()
19311931
19321932
1933+ from murfey .client .contexts .spa import (
1934+ _foil_hole_data ,
1935+ _foil_hole_from_file ,
1936+ _get_grid_square_atlas_positions ,
1937+ _grid_square_data ,
1938+ _grid_square_from_file ,
1939+ )
1940+ from murfey .server .spa .spa_metadata import register_foil_hole , register_grid_square
1941+ from murfey .util .models import FoilHoleParameters , GridSquareParameters
1942+
1943+
1944+ def _grid_square_metadata_file (f : Path , grid_square : int ) -> Path :
1945+ raw_dir = f .parent .parent .parent
1946+ metadata_dirs = raw_dir .glob ("metadata*" )
1947+ for md_dir in metadata_dirs :
1948+ gs_path = md_dir / f"Metadata/GridSquare_{ grid_square } .dm"
1949+ if gs_path .is_file ():
1950+ return gs_path
1951+ raise ValueError (f"Could not determine grid square metadata path for { f } " )
1952+
1953+
1954+ def _flush_position_analysis (movie_path : Path , dcg_id : int , session_id : int ) -> int :
1955+ grid_square = _grid_square_from_file (movie_path )
1956+ grid_square_metadata_file = _grid_square_metadata_file (movie_path , grid_square )
1957+ gs = _grid_square_data (grid_square_metadata_file , grid_square )
1958+
1959+ data_collection_group = murfey_db .exec (
1960+ select (db .DataCollectionGroup ).where (db .DataCollectionGroup .id == dcg_id )
1961+ ).one ()
1962+ if data_collection_group .atlas :
1963+ gs_pix_position = _get_grid_square_atlas_positions (
1964+ data_collection_group .atlas ,
1965+ grid_square = str (grid_square ),
1966+ )[str (grid_square )]
1967+ grid_square_parameters = GridSquareParameters (
1968+ tag = data_collection_group .tag ,
1969+ x_location = gs_pix_position [0 ],
1970+ y_location = gs_pix_position [1 ],
1971+ x_stage_position = gs_pix_position [2 ],
1972+ y_stage_position = gs_pix_position [3 ],
1973+ readout_area_x = gs .readout_area_x ,
1974+ readout_area_y = gs .readout_area_y ,
1975+ thumbnail_size_x = gs .thumbnail_size_x ,
1976+ thumbnail_size_y = gs .thumbnail_size_y ,
1977+ height = gs_pix_position [5 ],
1978+ width = gs_pix_position [4 ],
1979+ pixel_size = gs .pixel_size ,
1980+ image = gs .image ,
1981+ angle = gs_pix_position [6 ],
1982+ )
1983+ else :
1984+ grid_square_parameters = GridSquareParameters (
1985+ tag = data_collection_group .tag ,
1986+ readout_area_x = gs .readout_area_x ,
1987+ readout_area_y = gs .readout_area_y ,
1988+ thumbnail_size_x = gs .thumbnail_size_x ,
1989+ thumbnail_size_y = gs .thumbnail_size_y ,
1990+ pixel_size = gs .pixel_size ,
1991+ image = gs .image ,
1992+ )
1993+
1994+ register_grid_square (session_id , gs .id , grid_square_parameters , murfey_db )
1995+
1996+ foil_hole = _foil_hole_from_file (movie_path )
1997+ if grid_square_metadata_file .is_file ():
1998+ fh = _foil_hole_data (
1999+ grid_square_metadata_file ,
2000+ foil_hole ,
2001+ grid_square ,
2002+ )
2003+ foil_hole_parameters = FoilHoleParameters (
2004+ tag = data_collection_group .tag ,
2005+ name = foil_hole ,
2006+ x_location = fh .x_location ,
2007+ y_location = fh .y_location ,
2008+ x_stage_position = fh .x_stage_position ,
2009+ y_stage_position = fh .y_stage_position ,
2010+ readout_area_x = fh .readout_area_x ,
2011+ readout_area_y = fh .readout_area_y ,
2012+ thumbnail_size_x = fh .thumbnail_size_x ,
2013+ thumbnail_size_y = fh .thumbnail_size_y ,
2014+ pixel_size = fh .pixel_size ,
2015+ image = fh .image ,
2016+ diameter = fh .diameter ,
2017+ )
2018+ else :
2019+ foil_hole_parameters = FoilHoleParameters (
2020+ tag = data_collection_group .tag ,
2021+ name = foil_hole ,
2022+ )
2023+ register_foil_hole (session_id , gs .id , foil_hole_parameters , murfey_db )
2024+ return foil_hole
2025+
2026+
19332027def _flush_spa_preprocessing (message : dict ):
19342028 session_id = message ["session_id" ]
19352029 stashed_files = murfey_db .exec (
@@ -1988,6 +2082,15 @@ def _flush_spa_preprocessing(message: dict):
19882082 murfey_db .add (feedback_params )
19892083
19902084 for i , f in enumerate (stashed_files ):
2085+ if f .foil_hole_id :
2086+ foil_hole_id = f .foil_hole_id
2087+ else :
2088+ foil_hole_id = _flush_position_analysis (
2089+ movie_path = f .file_path ,
2090+ dcg_id = collected_ids [0 ].id ,
2091+ session_id = session_id ,
2092+ )
2093+
19912094 mrcp = Path (f .mrc_out )
19922095 ppath = Path (f .file_path )
19932096 if not mrcp .parent .exists ():
@@ -1997,7 +2100,7 @@ def _flush_spa_preprocessing(message: dict):
19972100 path = f .file_path ,
19982101 image_number = f .image_number ,
19992102 tag = f .tag ,
2000- foil_hole_id = f . foil_hole_id ,
2103+ foil_hole_id = foil_hole_id ,
20012104 )
20022105 murfey_db .add (movie )
20032106 zocalo_message : dict = {
@@ -2021,7 +2124,7 @@ def _flush_spa_preprocessing(message: dict):
20212124 "particle_diameter" : proc_params .particle_diameter or 0 ,
20222125 "fm_int_file" : f .eer_fractionation_file ,
20232126 "do_icebreaker_jobs" : default_spa_parameters .do_icebreaker_jobs ,
2024- "foil_hole_id" : f . foil_hole_id ,
2127+ "foil_hole_id" : foil_hole_id ,
20252128 },
20262129 }
20272130 if _transport_object :
0 commit comments