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 all 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 @@
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"):
with TiffFile(path) as tiff:
if not len(tiff.series):
raise ValueError(

Check warning on line 41 in cellfinder/core/tools/IO.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/core/tools/IO.py#L38-L41

Added lines #L38 - L41 were not covered by tests
f"Attempted to load {path} but couldn't read a z-stack"
)
if len(tiff.series) != 1:
raise ValueError(

Check warning on line 45 in cellfinder/core/tools/IO.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/core/tools/IO.py#L44-L45

Added lines #L44 - L45 were not covered by tests
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(

Check warning on line 51 in cellfinder/core/tools/IO.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/core/tools/IO.py#L49-L51

Added lines #L49 - L51 were not covered by tests
f"Attempted to load {path} but didn't find a zyx or "
f"zxy stack. Found {axes} axes"
)

return imread(path)

Check warning on line 56 in cellfinder/core/tools/IO.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/core/tools/IO.py#L56

Added line #L56 was not covered by tests

return read_with_dask(path)

Check warning on line 58 in cellfinder/core/tools/IO.py

View check run for this annotation

Codecov / codecov/patch

cellfinder/core/tools/IO.py#L58

Added line #L58 was not covered by tests


def read_with_dask(path):
"""
Based on https://github.com/tlambert03/napari-ndtiffs
Expand Down
Loading