Skip to content

Commit 2d25c41

Browse files
committed
Moved the conditional checks for grayscale images or image stacks into the helper functions; added additional conditions to skip certain image processing steps if they are not applicable to the image type
1 parent 2775c3d commit 2d25c41

1 file changed

Lines changed: 62 additions & 51 deletions

File tree

src/cryoemservices/wrappers/clem_align_and_merge.py

Lines changed: 62 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
align_image_to_self,
2929
convert_to_rgb,
3030
flatten_image,
31+
is_grayscale_image,
32+
is_image_stack,
3133
merge_images,
3234
stretch_image_contrast,
3335
)
@@ -181,13 +183,7 @@ def align_and_merge_stacks(
181183
array = tiff_file.series[0].pages.asarray()
182184

183185
# Crop array to middle n frames if selected and a stack is provided
184-
if (
185-
not len(array.shape) < 3 # 2D grayscale image
186-
or not (
187-
len(array.shape) == 3 # 2D RGB/RGBA image
188-
and array.shape[-1] in (3, 4)
189-
)
190-
) and isinstance(crop_to_n_frames, int):
186+
if is_image_stack(array) and isinstance(crop_to_n_frames, int):
191187
m = len(array) // 2
192188
n1 = crop_to_n_frames // 2
193189
n2 = n1 + 1 if crop_to_n_frames % 2 == 1 else n1
@@ -267,54 +263,66 @@ def align_and_merge_stacks(
267263

268264
# Align frames within each image stack
269265
if align_self:
270-
if print_messages is True:
271-
print(" Correcting for drift in images...")
272-
logger.info("Correcting for drift in images")
273-
with Pool(len(arrays)) as pool:
274-
arrays = pool.starmap(
275-
align_image_to_self,
276-
[(arr, "middle") for arr in arrays],
266+
# Perform drift correction only if they are image stacks
267+
if all(is_image_stack(array) for array in arrays):
268+
if print_messages is True:
269+
print(" Correcting for drift in images...")
270+
logger.info("Correcting for drift in images")
271+
with Pool(len(arrays)) as pool:
272+
arrays = pool.starmap(
273+
align_image_to_self,
274+
[(arr, "middle") for arr in arrays],
275+
)
276+
if print_messages is True:
277+
print(" Done")
278+
else:
279+
logger.info(
280+
"Skipping drift correction step as no image stacks were provided"
277281
)
278-
if print_messages is True:
279-
print(" Done")
280282

281283
# Flatten images if the option is selected
282284
if flatten:
283-
if print_messages is True:
284-
print("Flattening image stacks...")
285-
logger.info("Flattening image stacks")
286-
with Pool(len(arrays)) as pool:
287-
arrays = pool.starmap(
288-
flatten_image,
289-
[(arr, flatten) for arr in arrays],
290-
)
291-
# Validate that image flattening was done correctly
292-
if len({arr.shape for arr in arrays}) > 1:
293-
logger.error("The flattened arrays do not have the same shape")
294-
raise ValueError
295-
if print_messages is True:
296-
print(" Done")
285+
# Only flatten if they are image stacks
286+
if all(is_image_stack(array) for array in arrays):
287+
if print_messages is True:
288+
print("Flattening image stacks...")
289+
logger.info("Flattening image stacks")
290+
with Pool(len(arrays)) as pool:
291+
arrays = pool.starmap(
292+
flatten_image,
293+
[(arr, flatten) for arr in arrays],
294+
)
295+
# Validate that image flattening was done correctly
296+
if len({arr.shape for arr in arrays}) > 1:
297+
logger.error("The flattened arrays do not have the same shape")
298+
raise ValueError
299+
if print_messages is True:
300+
print(" Done")
297301

298-
# # Debug
299-
if debug and print_messages:
300-
print(
302+
# # Debug
303+
if debug and print_messages:
304+
print(
305+
"Properties of array after flattening: \n",
306+
f"Shape: {arrays[0].shape} \n",
307+
f"dtype: {arrays[0].dtype} \n",
308+
f"Min: {np.min(arrays)} \n",
309+
f"Max: {np.max(arrays)} \n",
310+
)
311+
logger.debug(
301312
"Properties of array after flattening: \n",
302313
f"Shape: {arrays[0].shape} \n",
303314
f"dtype: {arrays[0].dtype} \n",
304315
f"Min: {np.min(arrays)} \n",
305316
f"Max: {np.max(arrays)} \n",
306317
)
307-
logger.debug(
308-
"Properties of array after flattening: \n",
309-
f"Shape: {arrays[0].shape} \n",
310-
f"dtype: {arrays[0].dtype} \n",
311-
f"Min: {np.min(arrays)} \n",
312-
f"Max: {np.max(arrays)} \n",
313-
)
318+
else:
319+
logger.info(
320+
"Skipping image flattening step as no image stacks were provided"
321+
)
314322

315323
# Align other stacks to reference stack
316324
if align_across:
317-
if len(arrays) >= 2:
325+
if len(arrays) > 1:
318326
if print_messages is True:
319327
print("Aligning images to reference image...")
320328
reference = arrays[0] # First image in list is the reference image
@@ -332,21 +340,24 @@ def align_and_merge_stacks(
332340
print(" Done")
333341
elif len(arrays) == 1:
334342
if print_messages is True:
335-
print("Skipping step as there is only one image")
343+
logger.info("Skipping image alignment step as there is only one image")
336344
else:
337345
if print_messages is True:
338346
print("No image arrays are present")
339347

340348
# Colourise images
341-
if print_messages is True:
342-
print("Converting images from grayscale to RGB...")
343-
logger.info("Converting images from grayscale to RGB")
344-
with Pool(len(arrays)) as pool:
345-
arrays = pool.starmap(
346-
convert_to_rgb, [(arrays[c], colors[c]) for c in range(len(colors))]
347-
)
348-
if print_messages is True:
349-
print(" Done")
349+
if all(is_grayscale_image(array) for array in arrays):
350+
if print_messages is True:
351+
print("Converting images from grayscale to RGB...")
352+
logger.info("Converting images from grayscale to RGB")
353+
with Pool(len(arrays)) as pool:
354+
arrays = pool.starmap(
355+
convert_to_rgb, [(arrays[c], colors[c]) for c in range(len(colors))]
356+
)
357+
if print_messages is True:
358+
print(" Done")
359+
else:
360+
logger.info("Skipping image colorisation step as they are not grayscale")
350361

351362
# Debug
352363
if debug and print_messages:

0 commit comments

Comments
 (0)