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

Be permissive about tiff axis metadata #75

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions brainglobe_utils/IO/image/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,12 @@ def read_z_stack(path):
)

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"
if set(axes) != {"x", "y", "z"}:
# log that metadata does not specify expected axes
logging.debug(
f"Axis metadata is {axes}, "
"which is not the expected set of x,y,z in any order. "
"Assume z,y,x"
)

return tifffile.imread(path)
Expand Down
34 changes: 34 additions & 0 deletions tests/tests/test_IO/test_image_io.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import random
from collections import namedtuple
from unittest import mock

import numpy as np
import psutil
import pytest
import tifffile

from brainglobe_utils.IO.image import load, save, utils

Expand Down Expand Up @@ -68,6 +70,16 @@ def shuffled_txt_path(txt_path):
return txt_path


@pytest.fixture
def array3d_as_tiff_stack_with_missing_metadata(array_3d, tmp_path):
tiff_path = tmp_path / "test_missing_metadata.tif"
metadata = {"axes": ""}
tifffile.imwrite(
tiff_path, array_3d, photometric="minisblack", metadata=metadata
)
return tiff_path


@pytest.mark.parametrize("use_path", [True, False], ids=["Path", "String"])
def test_tiff_io(tmp_path, array_3d, use_path):
"""
Expand Down Expand Up @@ -406,3 +418,25 @@ def test_read_with_dask_glob_txt_equal(array_3D_as_2d_tiffs_path, txt_path):
glob_stack = load.read_with_dask(array_3D_as_2d_tiffs_path)
txt_stack = load.read_with_dask(txt_path)
np.testing.assert_array_equal(glob_stack, txt_stack)


def test_read_z_stack_with_missing_metadata(
array3d_as_tiff_stack_with_missing_metadata,
):
with mock.patch(
"brainglobe_utils.IO.image.load.logging.debug"
) as mock_debug:
load.read_z_stack(str(array3d_as_tiff_stack_with_missing_metadata))
mock_debug.assert_called_once()


def test_get_size_image_with_missing_metadata(
array3d_as_tiff_stack_with_missing_metadata,
):
with mock.patch(
"brainglobe_utils.IO.image.load.logging.debug"
) as mock_debug:
load.get_size_image_from_file_paths(
str(array3d_as_tiff_stack_with_missing_metadata)
)
mock_debug.assert_called_once()
Loading