Skip to content

Commit

Permalink
Be permissive about tiff axis metadata (#75)
Browse files Browse the repository at this point in the history
* add tests for tiffs with missing metadata

* be permissive around tiff axes - just log
  • Loading branch information
alessandrofelder authored May 15, 2024
1 parent 085a14c commit f7bd231
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
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()

0 comments on commit f7bd231

Please sign in to comment.