Skip to content
Merged
92 changes: 20 additions & 72 deletions src/murfey/client/contexts/clem.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,14 @@

from defusedxml.ElementTree import parse

from murfey.client.context import Context
from murfey.client.context import Context, _file_transferred_to, _get_source
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.client import capture_post

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


def _file_transferred_to(
environment: MurfeyInstanceEnvironment,
source: Path,
file_path: Path,
rsync_basepath: Path,
):
"""
Returns the Path of the transferred file on the DLS file system.
"""
# Construct destination path
base_destination = rsync_basepath / Path(environment.default_destinations[source])
# Add visit number to the path if it's not present in default destination
if environment.visit not in environment.default_destinations[source]:
base_destination = base_destination / environment.visit
destination = base_destination / file_path.relative_to(source)
return destination


def _get_source(file_path: Path, environment: MurfeyInstanceEnvironment):
"""
Returns the Path of the file on the client PC.
"""
for s in environment.sources:
if file_path.is_relative_to(s):
return s
return None


def _get_image_elements(root: ET.Element) -> list[ET.Element]:
"""
Searches the XML metadata recursively to find the nodes tagged as "Element" that
Expand Down Expand Up @@ -100,25 +72,23 @@ def post_transfer(
transferred_file: Path,
environment: MurfeyInstanceEnvironment | None = None,
**kwargs,
) -> bool:
):
super().post_transfer(transferred_file, environment=environment, **kwargs)

# Early exit if environment was not set
if not environment:
logger.warning("No environment was set")
return None

# Process files generated by "auto-save" acquisition mode
# These include TIF/TIFF and XLIF files
if transferred_file.suffix in (".tif", ".tiff", ".xlif"):
logger.debug(f"File extension {transferred_file.suffix!r} detected")

# Type checking to satisfy MyPy
if not environment:
logger.warning("No environment passed in")
return False

# Location of the file on the client PC
source = _get_source(transferred_file, environment)
# Type checking to satisfy MyPy
if not source:
logger.warning(f"No source found for file {transferred_file}")
return False
return None

# Get the file Path at the destination
destination_file = _file_transferred_to(
Expand All @@ -127,18 +97,13 @@ def post_transfer(
file_path=transferred_file,
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
)
if not destination_file:
logger.warning(
f"File {transferred_file.name!r} not found on the storage system"
)
return False

# Skip processing of binned "_pmd" image series
if "_pmd_" in transferred_file.stem:
logger.debug(
f"File {transferred_file.name!r} belongs to the '_pmd_' series of binned images; skipping processing"
)
return True
return None

# Process TIF/TIFF files
if transferred_file.suffix in (".tif", ".tiff"):
Expand All @@ -150,7 +115,7 @@ def post_transfer(
logger.warning(
f"File {transferred_file.name!r} is likely not part of the CLEM workflow"
)
return False
return None
logger.debug(
f"File {transferred_file.name!r} is part of a TIFF image series"
)
Expand Down Expand Up @@ -179,21 +144,21 @@ def post_transfer(
)

# Process XLIF files
if transferred_file.suffix == ".xlif":
elif transferred_file.suffix == ".xlif":
# Skip processing of "_histo" histogram XLIF files
if transferred_file.stem.endswith("_histo"):
logger.debug(
f"File {transferred_file.name!r} contains histogram metadata; skipping processing"
)
return True
return None

# Skip processing of "IOManagerConfiguation.xlif" files
# YES, the 'Configuation' typo IS part of the file name
if "IOManagerConfiguation" in transferred_file.stem:
logger.debug(
f"File {transferred_file.name!r} is a Leica configuration file; skipping processing"
)
return True
return None

logger.debug(
f"File {transferred_file.name!r} contains metadata for an image series"
Expand Down Expand Up @@ -242,12 +207,10 @@ def post_transfer(
# .get(series_name, 0) returns 0 if no associated key is found
if not len(self._tiff_series.get(series_name, [])):
logger.debug(f"TIFF series {series_name!r} not yet loaded")
return True
elif self._files_in_series.get(series_name, 0) == 0:
logger.debug(
f"Metadata for TIFF series {series_name!r} not yet processed"
)
return True
elif len(
self._tiff_series.get(series_name, [])
) >= self._files_in_series.get(series_name, 0):
Expand All @@ -261,10 +224,8 @@ def post_transfer(
"tiff_files": self._tiff_series[series_name][0:1],
"series_metadata": self._series_metadata[series_name],
}
post_result = self.process_tiff_series(tiff_dataset, environment)
if post_result is False:
return False
logger.info(f"Started preprocessing of TIFF series {series_name!r}")
if self.process_tiff_series(tiff_dataset, environment):
logger.info(f"Started preprocessing of TIFF series {series_name!r}")

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

# Process LIF files
if transferred_file.suffix == ".lif":
# Type checking to satisfy MyPy
if not environment:
logger.warning("No environment passed in")
return False

elif transferred_file.suffix == ".lif":
# Location of the file on the client PC
source = _get_source(transferred_file, environment)
# Type checking to satisfy MyPy
if not source:
logger.warning(f"No source found for file {transferred_file}")
return False
return None

logger.debug(
f"File {transferred_file.name!r} is a valid LIF file; starting processing"
Expand All @@ -298,20 +253,13 @@ def post_transfer(
file_path=transferred_file,
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
)
if not destination_file:
logger.warning(
f"File {transferred_file.name!r} not found on the storage system"
)
return False

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

# Function has completed as expected
return True
return None

def process_lif_file(
self,
Expand Down
40 changes: 1 addition & 39 deletions src/murfey/client/contexts/fib.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pathlib import Path
from typing import Callable, Type, TypeVar, cast

from murfey.client.context import Context
from murfey.client.context import Context, _file_transferred_to, _get_source
from murfey.client.instance_environment import MurfeyInstanceEnvironment
from murfey.util.client import capture_post
from murfey.util.fib import get_slot_number, number_from_name
Expand Down Expand Up @@ -200,34 +200,6 @@ def _get_project_name(file_path: Path):
return None


def _get_source(file_path: Path, environment: MurfeyInstanceEnvironment) -> Path | None:
"""
Returns the Path of the file on the client PC.
"""
for s in environment.sources:
if file_path.is_relative_to(s):
return s
return None


def _file_transferred_to(
environment: MurfeyInstanceEnvironment,
source: Path,
file_path: Path,
rsync_basepath: Path,
) -> Path | None:
"""
Returns the Path of the transferred file on the DLS file system.
"""
# Construct destination path
base_destination = rsync_basepath / Path(environment.default_destinations[source])
# Add visit number to the path if it's not present in default destination
if environment.visit not in environment.default_destinations[source]:
base_destination = base_destination / environment.visit
destination = base_destination / file_path.relative_to(source)
return destination


@dataclass
class FIBImage:
images: list[Path] = field(default_factory=list)
Expand Down Expand Up @@ -338,11 +310,6 @@ def post_transfer(
file_path=transferred_file,
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
)
if destination_file is None:
logger.warning(
f"Could not find destination file path for {transferred_file.name!r}"
)
return None

# Register image in database
self._register_atlas(destination_file, environment)
Expand Down Expand Up @@ -599,11 +566,6 @@ def _make_drift_correction_gif(
file_path=file,
rsync_basepath=Path(self._machine_config.get("rsync_basepath", "")),
)
if destination_file is None:
logger.warning(
f"Could not find destination file path for {file.name!r}"
)
return
else:
destination_file = file

Expand Down
Loading