Skip to content

Commit aedfa37

Browse files
authored
Standardise contexts (#844)
* Switched FIBContext to using the shared '_get_source' and '_file_transferred_to' functions * Switched CLEMContext to using the shared '_get_source' and '_file_transferred_to' functions * Streamlined conditionals in the CLEMContext * Switched CLEMContext 'post_transfer' function to return 'None' * Added tests for the CLEMContext
1 parent f97ffec commit aedfa37

4 files changed

Lines changed: 447 additions & 131 deletions

File tree

src/murfey/client/contexts/clem.py

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,14 @@
1010

1111
from defusedxml.ElementTree import parse
1212

13-
from murfey.client.context import Context
13+
from murfey.client.context import Context, _file_transferred_to, _get_source
1414
from murfey.client.instance_environment import MurfeyInstanceEnvironment
1515
from murfey.util.client import capture_post
1616

1717
# Create logger object
1818
logger = logging.getLogger("murfey.client.contexts.clem")
1919

2020

21-
def _file_transferred_to(
22-
environment: MurfeyInstanceEnvironment,
23-
source: Path,
24-
file_path: Path,
25-
rsync_basepath: Path,
26-
):
27-
"""
28-
Returns the Path of the transferred file on the DLS file system.
29-
"""
30-
# Construct destination path
31-
base_destination = rsync_basepath / Path(environment.default_destinations[source])
32-
# Add visit number to the path if it's not present in default destination
33-
if environment.visit not in environment.default_destinations[source]:
34-
base_destination = base_destination / environment.visit
35-
destination = base_destination / file_path.relative_to(source)
36-
return destination
37-
38-
39-
def _get_source(file_path: Path, environment: MurfeyInstanceEnvironment):
40-
"""
41-
Returns the Path of the file on the client PC.
42-
"""
43-
for s in environment.sources:
44-
if file_path.is_relative_to(s):
45-
return s
46-
return None
47-
48-
4921
def _get_image_elements(root: ET.Element) -> list[ET.Element]:
5022
"""
5123
Searches the XML metadata recursively to find the nodes tagged as "Element" that
@@ -100,25 +72,23 @@ def post_transfer(
10072
transferred_file: Path,
10173
environment: MurfeyInstanceEnvironment | None = None,
10274
**kwargs,
103-
) -> bool:
75+
):
10476
super().post_transfer(transferred_file, environment=environment, **kwargs)
10577

78+
# Early exit if environment was not set
79+
if not environment:
80+
logger.warning("No environment was set")
81+
return None
82+
10683
# Process files generated by "auto-save" acquisition mode
10784
# These include TIF/TIFF and XLIF files
10885
if transferred_file.suffix in (".tif", ".tiff", ".xlif"):
10986
logger.debug(f"File extension {transferred_file.suffix!r} detected")
110-
111-
# Type checking to satisfy MyPy
112-
if not environment:
113-
logger.warning("No environment passed in")
114-
return False
115-
11687
# Location of the file on the client PC
11788
source = _get_source(transferred_file, environment)
118-
# Type checking to satisfy MyPy
11989
if not source:
12090
logger.warning(f"No source found for file {transferred_file}")
121-
return False
91+
return None
12292

12393
# Get the file Path at the destination
12494
destination_file = _file_transferred_to(
@@ -127,18 +97,13 @@ def post_transfer(
12797
file_path=transferred_file,
12898
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
12999
)
130-
if not destination_file:
131-
logger.warning(
132-
f"File {transferred_file.name!r} not found on the storage system"
133-
)
134-
return False
135100

136101
# Skip processing of binned "_pmd" image series
137102
if "_pmd_" in transferred_file.stem:
138103
logger.debug(
139104
f"File {transferred_file.name!r} belongs to the '_pmd_' series of binned images; skipping processing"
140105
)
141-
return True
106+
return None
142107

143108
# Process TIF/TIFF files
144109
if transferred_file.suffix in (".tif", ".tiff"):
@@ -150,7 +115,7 @@ def post_transfer(
150115
logger.warning(
151116
f"File {transferred_file.name!r} is likely not part of the CLEM workflow"
152117
)
153-
return False
118+
return None
154119
logger.debug(
155120
f"File {transferred_file.name!r} is part of a TIFF image series"
156121
)
@@ -179,21 +144,21 @@ def post_transfer(
179144
)
180145

181146
# Process XLIF files
182-
if transferred_file.suffix == ".xlif":
147+
elif transferred_file.suffix == ".xlif":
183148
# Skip processing of "_histo" histogram XLIF files
184149
if transferred_file.stem.endswith("_histo"):
185150
logger.debug(
186151
f"File {transferred_file.name!r} contains histogram metadata; skipping processing"
187152
)
188-
return True
153+
return None
189154

190155
# Skip processing of "IOManagerConfiguation.xlif" files
191156
# YES, the 'Configuation' typo IS part of the file name
192157
if "IOManagerConfiguation" in transferred_file.stem:
193158
logger.debug(
194159
f"File {transferred_file.name!r} is a Leica configuration file; skipping processing"
195160
)
196-
return True
161+
return None
197162

198163
logger.debug(
199164
f"File {transferred_file.name!r} contains metadata for an image series"
@@ -242,12 +207,10 @@ def post_transfer(
242207
# .get(series_name, 0) returns 0 if no associated key is found
243208
if not len(self._tiff_series.get(series_name, [])):
244209
logger.debug(f"TIFF series {series_name!r} not yet loaded")
245-
return True
246210
elif self._files_in_series.get(series_name, 0) == 0:
247211
logger.debug(
248212
f"Metadata for TIFF series {series_name!r} not yet processed"
249213
)
250-
return True
251214
elif len(
252215
self._tiff_series.get(series_name, [])
253216
) >= self._files_in_series.get(series_name, 0):
@@ -261,10 +224,8 @@ def post_transfer(
261224
"tiff_files": self._tiff_series[series_name][0:1],
262225
"series_metadata": self._series_metadata[series_name],
263226
}
264-
post_result = self.process_tiff_series(tiff_dataset, environment)
265-
if post_result is False:
266-
return False
267-
logger.info(f"Started preprocessing of TIFF series {series_name!r}")
227+
if self.process_tiff_series(tiff_dataset, environment):
228+
logger.info(f"Started preprocessing of TIFF series {series_name!r}")
268229

269230
# Clean up memory after posting
270231
del self._tiff_series[series_name]
@@ -274,18 +235,12 @@ def post_transfer(
274235
logger.debug(f"TIFF series {series_name!r} is still being processed")
275236

276237
# Process LIF files
277-
if transferred_file.suffix == ".lif":
278-
# Type checking to satisfy MyPy
279-
if not environment:
280-
logger.warning("No environment passed in")
281-
return False
282-
238+
elif transferred_file.suffix == ".lif":
283239
# Location of the file on the client PC
284240
source = _get_source(transferred_file, environment)
285-
# Type checking to satisfy MyPy
286241
if not source:
287242
logger.warning(f"No source found for file {transferred_file}")
288-
return False
243+
return None
289244

290245
logger.debug(
291246
f"File {transferred_file.name!r} is a valid LIF file; starting processing"
@@ -298,20 +253,13 @@ def post_transfer(
298253
file_path=transferred_file,
299254
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
300255
)
301-
if not destination_file:
302-
logger.warning(
303-
f"File {transferred_file.name!r} not found on the storage system"
304-
)
305-
return False
306256

307257
# Post URL to trigger job and convert LIF file into image stacks
308-
post_result = self.process_lif_file(destination_file, environment)
309-
if post_result is False:
310-
return False
311-
logger.info(f"Started preprocessing of {destination_file.name!r}")
258+
if self.process_lif_file(destination_file, environment):
259+
logger.info(f"Started preprocessing of {destination_file.name!r}")
312260

313261
# Function has completed as expected
314-
return True
262+
return None
315263

316264
def process_lif_file(
317265
self,

src/murfey/client/contexts/fib.py

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing import Callable, Type, TypeVar, cast
99

10-
from murfey.client.context import Context
10+
from murfey.client.context import Context, _file_transferred_to, _get_source
1111
from murfey.client.instance_environment import MurfeyInstanceEnvironment
1212
from murfey.util.client import capture_post
1313
from murfey.util.fib import get_slot_number, number_from_name
@@ -200,34 +200,6 @@ def _get_project_name(file_path: Path):
200200
return None
201201

202202

203-
def _get_source(file_path: Path, environment: MurfeyInstanceEnvironment) -> Path | None:
204-
"""
205-
Returns the Path of the file on the client PC.
206-
"""
207-
for s in environment.sources:
208-
if file_path.is_relative_to(s):
209-
return s
210-
return None
211-
212-
213-
def _file_transferred_to(
214-
environment: MurfeyInstanceEnvironment,
215-
source: Path,
216-
file_path: Path,
217-
rsync_basepath: Path,
218-
) -> Path | None:
219-
"""
220-
Returns the Path of the transferred file on the DLS file system.
221-
"""
222-
# Construct destination path
223-
base_destination = rsync_basepath / Path(environment.default_destinations[source])
224-
# Add visit number to the path if it's not present in default destination
225-
if environment.visit not in environment.default_destinations[source]:
226-
base_destination = base_destination / environment.visit
227-
destination = base_destination / file_path.relative_to(source)
228-
return destination
229-
230-
231203
@dataclass
232204
class FIBImage:
233205
images: list[Path] = field(default_factory=list)
@@ -338,11 +310,6 @@ def post_transfer(
338310
file_path=transferred_file,
339311
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
340312
)
341-
if destination_file is None:
342-
logger.warning(
343-
f"Could not find destination file path for {transferred_file.name!r}"
344-
)
345-
return None
346313

347314
# Register image in database
348315
self._register_atlas(destination_file, environment)
@@ -599,11 +566,6 @@ def _make_drift_correction_gif(
599566
file_path=file,
600567
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
601568
)
602-
if destination_file is None:
603-
logger.warning(
604-
f"Could not find destination file path for {file.name!r}"
605-
)
606-
return
607569
else:
608570
destination_file = file
609571

0 commit comments

Comments
 (0)