Skip to content

Commit 9c7b775

Browse files
authored
Optimised conditionals and updated comments for CLEM functions (#301)
1 parent 76b0cf2 commit 9c7b775

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

src/murfey/client/contexts/clem.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def _file_transferred_to(
2727
environment: MurfeyInstanceEnvironment, source: Path, file_path: Path
2828
) -> Optional[Path]:
2929
"""
30-
Returns the Path of the transferred file on the DLS file system
30+
Returns the Path of the transferred file on the DLS file system.
3131
"""
3232
machine_config = get_machine_config(
3333
str(environment.url.geturl()), demo=environment.demo
@@ -45,7 +45,7 @@ def _get_source(
4545
file_path: Path, environment: MurfeyInstanceEnvironment
4646
) -> Optional[Path]:
4747
"""
48-
Returns the Path of the file on the client PC
48+
Returns the Path of the file on the client PC.
4949
"""
5050
for s in environment.sources:
5151
if file_path.is_relative_to(s):
@@ -54,7 +54,6 @@ def _get_source(
5454

5555

5656
# WORK IN PROGRESS
57-
# Will need to add context for TIFF files associated with CLEM
5857
class CLEMContext(Context):
5958
def __init__(self, acquisition_software: str, basepath: Path):
6059
super().__init__("CLEM", acquisition_software)
@@ -107,13 +106,13 @@ def post_transfer(
107106
return False
108107

109108
# Process TIF/TIFF files
110-
if any(transferred_file.suffix == s for s in [".tif", ".tiff"]):
109+
if transferred_file.suffix in (".tif", ".tiff"):
111110
# Files should be named "PositionX--ZXX--CXX.tif" by default
112111
if not len(transferred_file.stem.split("--")) == 3:
113112
logger.warning(
114113
"This TIFF file is likely not part of the CLEM workflow"
115114
)
116-
return False # Not sure if None, False, or True is most appropriate
115+
return False
117116

118117
# Get series name from file name
119118
series_name = "/".join(

src/murfey/util/clem/images.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def rescale_across_channel(
6666
"""
6767

6868
# Check that bit depth is valid before processing even begins
69-
if not any(bit_depth == b for b in [8, 16, 32, 64]):
69+
if bit_depth not in (8, 16, 32, 64):
7070
raise_BitDepthError(bit_depth)
7171

7272
# Use shorter variable names
@@ -114,7 +114,7 @@ def rescale_to_bit_depth(
114114
bit_final = target_bit_depth
115115

116116
# Check that target bit depth is allowed
117-
if not any(bit_final == b for b in [8, 16, 32, 64]):
117+
if bit_final not in (8, 16, 32, 64):
118118
raise_BitDepthError(bit_final)
119119

120120
# Rescale (DIVIDE BEFORE MULTIPLY)
@@ -142,7 +142,7 @@ def process_img_stk(
142142
bdi = initial_bit_depth
143143
bdt = target_bit_depth
144144

145-
if not any(bdi == b for b in [8, 16, 32, 64]):
145+
if bdi not in (8, 16, 32, 64):
146146
logger.info(f"{bdi}-bit is not supported by NumPy; converting to 16-bit")
147147
arr = (
148148
rescale_to_bit_depth(array=arr, initial_bit_depth=bdi, target_bit_depth=16)
@@ -201,6 +201,10 @@ def write_to_tiff(
201201
axes: str,
202202
image_labels: List[str],
203203
):
204+
"""
205+
Writes the NumPy array as a calibrated greyscale TIFF image stack.
206+
"""
207+
204208
# Use shorter aliases and calculate what is needed
205209
arr = array
206210
z_size = (1 / z_res) if z_res > 0 else float(0)
@@ -212,7 +216,7 @@ def write_to_tiff(
212216
save_name,
213217
arr,
214218
imagej=True, # ImageJ compatible
215-
photometric="minisblack", # Grayscale image
219+
photometric="minisblack", # Greyscale image
216220
shape=np.shape(arr),
217221
dtype=arr.dtype,
218222
resolution=(x_res * 10**6 / 10**6, y_res * 10**6 / 10**6),

src/murfey/util/clem/lif.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def process_lif_file(
4545
# Create save dirs for TIFF files and their metadata
4646
img_dir = save_dir / img_name
4747
img_xml_dir = img_dir / "metadata"
48-
for folder in [img_dir, img_xml_dir]:
48+
for folder in (img_dir, img_xml_dir):
4949
if not folder.exists():
5050
folder.mkdir(parents=True)
5151
logger.info(f"Created {folder}")
@@ -203,7 +203,7 @@ def convert_lif_to_tiff(
203203
# Create folders if not already present
204204
processed_dir = Path("/".join(path_parts)).parent / file_name # Processed images
205205
raw_xml_dir = file.parent / "metadata" # Raw metadata
206-
for folder in [processed_dir, raw_xml_dir]:
206+
for folder in (processed_dir, raw_xml_dir):
207207
if not folder.exists():
208208
folder.mkdir(parents=True)
209209
logger.info(f"Created {sanitise(str(folder))}")

src/murfey/util/clem/xml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def get_lif_xml_metadata(
2525
save_xml: Optional[Path] = None,
2626
) -> ET.Element:
2727
"""
28-
Extracts and returns the file metadata as a formatted XML Element. Provides option
29-
to save it as an XML file to the specified file path
28+
Extracts and returns the metadata from the LIF file as a formatted XML Element.
29+
It can be optionally saved as an XML file to the specified file path.
3030
"""
3131

3232
# Use readlif function to get XML metadata

0 commit comments

Comments
 (0)