|
4 | 4 |
|
5 | 5 | import xmltodict |
6 | 6 |
|
7 | | -from murfey.client.context import Context |
| 7 | +from murfey.client.context import Context, ensure_dcg_exists |
8 | 8 | from murfey.client.contexts.spa import _file_transferred_to, _get_source |
9 | | -from murfey.client.instance_environment import MurfeyInstanceEnvironment, SampleInfo |
10 | | -from murfey.util.client import capture_post, get_machine_config_client |
| 9 | +from murfey.client.instance_environment import MurfeyInstanceEnvironment |
| 10 | +from murfey.util.client import capture_post |
11 | 11 | from murfey.util.spa_metadata import ( |
12 | 12 | FoilHoleInfo, |
13 | 13 | get_grid_square_atlas_positions, |
@@ -69,29 +69,6 @@ def _foil_hole_positions(xml_path: Path, grid_square: int) -> Dict[str, FoilHole |
69 | 69 | return foil_holes |
70 | 70 |
|
71 | 71 |
|
72 | | -def _atlas_destination( |
73 | | - environment: MurfeyInstanceEnvironment, source: Path, file_path: Path, token: str |
74 | | -) -> Path: |
75 | | - machine_config = get_machine_config_client( |
76 | | - str(environment.url.geturl()), |
77 | | - token, |
78 | | - instrument_name=environment.instrument_name, |
79 | | - demo=environment.demo, |
80 | | - ) |
81 | | - for i, destination_part in enumerate( |
82 | | - Path(environment.default_destinations[source]).parts |
83 | | - ): |
84 | | - if destination_part == environment.visit: |
85 | | - return Path(machine_config.get("rsync_basepath", "")) / "/".join( |
86 | | - Path(environment.default_destinations[source]).parent.parts[: i + 1] |
87 | | - ) |
88 | | - return ( |
89 | | - Path(machine_config.get("rsync_basepath", "")) |
90 | | - / Path(environment.default_destinations[source]).parent |
91 | | - / environment.visit |
92 | | - ) |
93 | | - |
94 | | - |
95 | 72 | class SPAMetadataContext(Context): |
96 | 73 | def __init__(self, acquisition_software: str, basepath: Path, token: str): |
97 | 74 | super().__init__("SPA_metadata", acquisition_software, token) |
@@ -124,82 +101,19 @@ def post_transfer( |
124 | 101 | source = _get_source(transferred_file, environment) |
125 | 102 | if not source: |
126 | 103 | logger.warning( |
127 | | - f"Source could not be indentified for {str(transferred_file)}" |
| 104 | + f"Source could not be identified for {str(transferred_file)}" |
128 | 105 | ) |
129 | 106 | return |
130 | 107 |
|
131 | | - source_visit_dir = source.parent |
132 | | - |
133 | | - logger.info( |
134 | | - f"Looking for atlas XML file in metadata directory {str((source_visit_dir / partial_path).parent)}" |
135 | | - ) |
136 | | - atlas_xml_path = list( |
137 | | - (source_visit_dir / partial_path).parent.glob("Atlas_*.xml") |
138 | | - )[0] |
139 | | - logger.info(f"Atlas XML path {str(atlas_xml_path)} found") |
140 | | - with open(atlas_xml_path, "rb") as atlas_xml: |
141 | | - atlas_xml_data = xmltodict.parse(atlas_xml) |
142 | | - atlas_original_pixel_size = float( |
143 | | - atlas_xml_data["MicroscopeImage"]["SpatialScale"]["pixelSize"]["x"][ |
144 | | - "numericValue" |
145 | | - ] |
146 | | - ) |
147 | | - |
148 | | - # need to calculate the pixel size of the downscaled image |
149 | | - atlas_pixel_size = atlas_original_pixel_size * 7.8 |
150 | | - logger.info(f"Atlas image pixel size determined to be {atlas_pixel_size}") |
151 | | - |
152 | | - for p in partial_path.split("/"): |
153 | | - if p.startswith("Sample"): |
154 | | - sample = int(p.replace("Sample", "")) |
155 | | - break |
156 | | - else: |
157 | | - logger.warning(f"Sample could not be identified for {transferred_file}") |
158 | | - return |
159 | 108 | if source: |
160 | | - environment.samples[source] = SampleInfo( |
161 | | - atlas=Path(partial_path), sample=sample |
162 | | - ) |
163 | | - dcg_search_dir = "/".join( |
164 | | - p for p in transferred_file.parent.parts if p != environment.visit |
165 | | - ) |
166 | | - dcg_search_dir = ( |
167 | | - dcg_search_dir[1:] |
168 | | - if dcg_search_dir.startswith("//") |
169 | | - else dcg_search_dir |
170 | | - ) |
171 | | - dcg_images_dirs = sorted( |
172 | | - Path(dcg_search_dir).glob("Images-Disc*"), |
173 | | - key=lambda x: x.stat().st_ctime, |
174 | | - ) |
175 | | - if not dcg_images_dirs: |
176 | | - logger.warning(f"Cannot find Images-Disc* in {dcg_search_dir}") |
177 | | - return |
178 | | - dcg_tag = str(dcg_images_dirs[-1]) |
179 | | - dcg_data = { |
180 | | - "experiment_type_id": 37, # Single particle |
181 | | - "tag": dcg_tag, |
182 | | - "atlas": str( |
183 | | - _atlas_destination( |
184 | | - environment, source, transferred_file, self._token |
185 | | - ) |
186 | | - / environment.samples[source].atlas.parent |
187 | | - / atlas_xml_path.with_suffix(".jpg").name |
188 | | - ), |
189 | | - "sample": environment.samples[source].sample, |
190 | | - "atlas_pixel_size": atlas_pixel_size, |
191 | | - } |
192 | | - capture_post( |
193 | | - base_url=str(environment.url.geturl()), |
194 | | - router_name="workflow.router", |
195 | | - function_name="register_dc_group", |
| 109 | + dcg_tag = ensure_dcg_exists( |
| 110 | + collection_type="spa", |
| 111 | + metadata_source=source, |
| 112 | + environment=environment, |
196 | 113 | token=self._token, |
197 | | - visit_name=environment.visit, |
198 | | - session_id=environment.murfey_session, |
199 | | - data=dcg_data, |
200 | 114 | ) |
201 | 115 | gs_pix_positions = get_grid_square_atlas_positions( |
202 | | - source_visit_dir / partial_path |
| 116 | + source.parent / partial_path |
203 | 117 | ) |
204 | 118 | for gs, pos_data in gs_pix_positions.items(): |
205 | 119 | if pos_data: |
@@ -228,46 +142,21 @@ def post_transfer( |
228 | 142 | and environment |
229 | 143 | ): |
230 | 144 | # Make sure we have a data collection group before trying to register grid square |
231 | | - dcg_search_dir = "/".join( |
232 | | - p |
233 | | - for p in transferred_file.parent.parent.parts |
234 | | - if p != environment.visit |
235 | | - ) |
236 | | - dcg_search_dir = ( |
237 | | - dcg_search_dir[1:] |
238 | | - if dcg_search_dir.startswith("//") |
239 | | - else dcg_search_dir |
240 | | - ) |
241 | | - dcg_images_dirs = sorted( |
242 | | - Path(dcg_search_dir).glob("Images-Disc*"), |
243 | | - key=lambda x: x.stat().st_ctime, |
244 | | - ) |
245 | | - if not dcg_images_dirs: |
246 | | - logger.warning(f"Cannot find Images-Disc* in {dcg_search_dir}") |
247 | | - return |
248 | | - dcg_tag = str(dcg_images_dirs[-1]) |
249 | | - dcg_data = { |
250 | | - "experiment_type_id": 37, # Single particle |
251 | | - "tag": dcg_tag, |
252 | | - } |
253 | | - capture_post( |
254 | | - base_url=str(environment.url.geturl()), |
255 | | - router_name="workflow.router", |
256 | | - function_name="register_dc_group", |
| 145 | + source = _get_source(transferred_file, environment=environment) |
| 146 | + if source is None: |
| 147 | + return None |
| 148 | + ensure_dcg_exists( |
| 149 | + collection_type="spa", |
| 150 | + metadata_source=source, |
| 151 | + environment=environment, |
257 | 152 | token=self._token, |
258 | | - visit_name=environment.visit, |
259 | | - session_id=environment.murfey_session, |
260 | | - data=dcg_data, |
261 | 153 | ) |
262 | 154 |
|
263 | 155 | gs_name = int(transferred_file.stem.split("_")[1]) |
264 | 156 | logger.info( |
265 | 157 | f"Collecting foil hole positions for {str(transferred_file)} and grid square {gs_name}" |
266 | 158 | ) |
267 | 159 | fh_positions = _foil_hole_positions(transferred_file, gs_name) |
268 | | - source = _get_source(transferred_file, environment=environment) |
269 | | - if source is None: |
270 | | - return None |
271 | 160 | visitless_source_search_dir = str(source).replace( |
272 | 161 | f"/{environment.visit}", "" |
273 | 162 | ) |
|
0 commit comments