From 86b5b71f352b4d3914529a8f2eb8bca241f624bc Mon Sep 17 00:00:00 2001 From: Matt Einhorn Date: Tue, 9 Apr 2024 23:41:37 -0400 Subject: [PATCH 1/3] Support single z-stack tif file for input. --- cellfinder/core/tools/IO.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/cellfinder/core/tools/IO.py b/cellfinder/core/tools/IO.py index 84eafdd6..6f96d4fb 100644 --- a/cellfinder/core/tools/IO.py +++ b/cellfinder/core/tools/IO.py @@ -24,6 +24,39 @@ def get_tiff_meta( lazy_imread = delayed(imread) # lazy reader +def read_z_stack(path): + """ + Reads z-stack, lazily, if possible. + + If it's a text file or folder with 2D tiff files use dask to read lazily, + otherwise it's a single file tiff stack and is read into memory. + + :param path: Filename of text file listing 2D tiffs, folder of 2D tiffs, + or single file tiff z-stack. + :return: The data as a dask/numpy array. + """ + if path.endswith(".tiff") or path.endswith(".tif"): + tiff = TiffFile(path) + if not len(tiff.series): + raise ValueError( + f"Attempted to load {path} but couldn't read a z-stack" + ) + if len(tiff.series) != 1: + raise ValueError( + f"Attempted to load {path} but found multiple stacks" + ) + + axes = tiff.series[0].axes.lower() + if set(axes) != {"x", "y", "z"} or axes[0].lower() != "z": + raise ValueError( + f"Attempted to load {path} but didn't find a zyx or " + f"zxy stack. Found {axes} axes") + + return imread(path) + + return read_with_dask(path) + + def read_with_dask(path): """ Based on https://github.com/tlambert03/napari-ndtiffs From 97b8bd73519515a96e1d4ada715d2150bc7cedb1 Mon Sep 17 00:00:00 2001 From: Matt Einhorn Date: Tue, 9 Apr 2024 23:42:25 -0400 Subject: [PATCH 2/3] Fix commit hook. --- cellfinder/core/tools/IO.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cellfinder/core/tools/IO.py b/cellfinder/core/tools/IO.py index 6f96d4fb..3975d5a5 100644 --- a/cellfinder/core/tools/IO.py +++ b/cellfinder/core/tools/IO.py @@ -50,7 +50,8 @@ def read_z_stack(path): if set(axes) != {"x", "y", "z"} or axes[0].lower() != "z": raise ValueError( f"Attempted to load {path} but didn't find a zyx or " - f"zxy stack. Found {axes} axes") + f"zxy stack. Found {axes} axes" + ) return imread(path) From ab033e1bd701127fb5f2bcbd8e41795d6ac476f8 Mon Sep 17 00:00:00 2001 From: Matt Einhorn Date: Mon, 22 Apr 2024 01:53:15 -0400 Subject: [PATCH 3/3] Apply review suggestions. --- cellfinder/core/tools/IO.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/cellfinder/core/tools/IO.py b/cellfinder/core/tools/IO.py index 3975d5a5..359a6da5 100644 --- a/cellfinder/core/tools/IO.py +++ b/cellfinder/core/tools/IO.py @@ -36,22 +36,22 @@ def read_z_stack(path): :return: The data as a dask/numpy array. """ if path.endswith(".tiff") or path.endswith(".tif"): - tiff = TiffFile(path) - if not len(tiff.series): - raise ValueError( - f"Attempted to load {path} but couldn't read a z-stack" - ) - if len(tiff.series) != 1: - raise ValueError( - f"Attempted to load {path} but found multiple stacks" - ) - - axes = tiff.series[0].axes.lower() - if set(axes) != {"x", "y", "z"} or axes[0].lower() != "z": - raise ValueError( - f"Attempted to load {path} but didn't find a zyx or " - f"zxy stack. Found {axes} axes" - ) + with TiffFile(path) as tiff: + if not len(tiff.series): + raise ValueError( + f"Attempted to load {path} but couldn't read a z-stack" + ) + if len(tiff.series) != 1: + raise ValueError( + f"Attempted to load {path} but found multiple stacks" + ) + + axes = tiff.series[0].axes.lower() + if set(axes) != {"x", "y", "z"} or axes[0] != "z": + raise ValueError( + f"Attempted to load {path} but didn't find a zyx or " + f"zxy stack. Found {axes} axes" + ) return imread(path)