Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support single z-stack tif file for input #397

Merged
merged 4 commits into from
May 1, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions cellfinder/core/tools/IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@ 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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use with TiffFile(path) as tiff: for this section? This will ensure the connection is closed correctly, as they require in their docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added change!

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
Expand Down
Loading