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 #67

Merged
merged 6 commits into from
May 3, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix commit hook.
  • Loading branch information
matham committed Apr 10, 2024

Verified

This commit was signed with the committer’s verified signature.
matham Matt Einhorn
commit 6aa5e395f450579d770e75c498d50c6e12a46bd0
3 changes: 2 additions & 1 deletion brainglobe_utils/image_io/load.py
Original file line number Diff line number Diff line change
@@ -686,27 +686,28 @@
file_path = Path(file_path)
if file_path.name.endswith(".tif") or file_path.name.endswith(".tiff"):
matham marked this conversation as resolved.
Show resolved Hide resolved
# read just the metadata
tiff = tifffile.TiffFile(file_path)
Copy link
Contributor

@K-Meech K-Meech Apr 15, 2024

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(file_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.

Applied the change.

if not len(tiff.series):
raise ValueError(

Check warning on line 691 in brainglobe_utils/image_io/load.py

Codecov / codecov/patch

brainglobe_utils/image_io/load.py#L689-L691

Added lines #L689 - L691 were not covered by tests
Copy link
Contributor

Choose a reason for hiding this comment

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

Ideally these would use ImageIOLoadException rather than ValueError. This would need some restructuring of ImageIOLoadException though - so happy to leave as-is for now, and I'll look into this once it's all merged.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I left it as is then.

f"Attempted to load {file_path} but couldn't read a z-stack"
)
if len(tiff.series) != 1:
raise ValueError(

Check warning on line 695 in brainglobe_utils/image_io/load.py

Codecov / codecov/patch

brainglobe_utils/image_io/load.py#L694-L695

Added lines #L694 - L695 were not covered by tests
f"Attempted to load {file_path} but found multiple stacks"
)

shape = tiff.series[0].shape
axes = tiff.series[0].axes.lower()

Check warning on line 700 in brainglobe_utils/image_io/load.py

Codecov / codecov/patch

brainglobe_utils/image_io/load.py#L699-L700

Added lines #L699 - L700 were not covered by tests
# axes is e.g. "ZXY"
indices = {ax: i for i, ax in enumerate(axes)}
if set(axes) != {"x", "y", "z"}:
raise ValueError(

Check warning on line 704 in brainglobe_utils/image_io/load.py

Codecov / codecov/patch

brainglobe_utils/image_io/load.py#L702-L704

Added lines #L702 - L704 were not covered by tests
f"Attempted to load {file_path} but didn't find a xyz-stack. "
f"Found {axes} axes with shape {shape}")
f"Found {axes} axes with shape {shape}"
)

image_shape = {name: shape[i] for name, i in indices.items()}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you could remove the indices = {ax: i for i, ax in enumerate(axes)} line above, and do this in one line with something like: image_shape = {ax: sh for ax, sh in zip(axes, shape)}

Copy link
Contributor Author

@matham matham Apr 22, 2024

Choose a reason for hiding this comment

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

Good catch! I applied the change.

return image_shape

Check warning on line 710 in brainglobe_utils/image_io/load.py

Codecov / codecov/patch

brainglobe_utils/image_io/load.py#L709-L710

Added lines #L709 - L710 were not covered by tests

img_paths = get_sorted_file_paths(file_path, file_extension=file_extension)
z_shape = len(img_paths)
Loading