Skip to content

Commit

Permalink
Merge pull request #98 from brainglobe/ignore-ome
Browse files Browse the repository at this point in the history
improve read_with_dask docs, ignore OME metadata
  • Loading branch information
alessandrofelder authored Aug 15, 2024
2 parents 0d463f6 + f2a5f21 commit ea37575
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 19 additions & 4 deletions brainglobe_utils/IO/image/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -768,19 +768,34 @@ def read_z_stack(path):
def read_with_dask(path):
"""
Based on https://github.com/tlambert03/napari-ndtiffs
:param path:
:return:
Reads a folder of tiffs lazily.
Note that it will make tifffile.imread ignore OME metadata,
because this can cause issues with correct metadata reading.
See https://forum.image.sc/t/tifffile-opening-individual-ome-tiff-files-as-single-huge-array-even-when-isolated/77701
:param path: folder with tifs.
:return: dask array containing stack of tifs
"""
path = str(path)
if path.endswith(".txt"):
with open(path, "r") as f:
filenames = [line.rstrip() for line in f.readlines()]

else:
filenames = glob.glob(os.path.join(path, "*.tif"))
filenames = glob.glob(os.path.join(path, "*.tif")) or glob.glob(
os.path.join(path, "*.tiff")
)
if not filenames:
raise ValueError(
f"Folder {path} does not contain any .tif or .tiff files"
)

shape, dtype = get_tiff_meta(filenames[0])
lazy_arrays = [lazy_imread(fn) for fn in get_sorted_file_paths(filenames)]
lazy_arrays = [
lazy_imread(fn, is_ome=False)
for fn in get_sorted_file_paths(filenames)
]
dask_arrays = [
da.from_delayed(delayed_reader, shape=shape, dtype=dtype)
for delayed_reader in lazy_arrays
Expand Down
6 changes: 6 additions & 0 deletions tests/tests/test_IO/test_image_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,9 @@ def test_get_size_image_with_missing_metadata(
str(array3d_as_tiff_stack_with_missing_metadata)
)
mock_debug.assert_called_once()


def test_read_with_dask_raises(tmp_path):
with pytest.raises(ValueError) as e:
load.read_with_dask(tmp_path)
assert e.match("not contain any .tif or .tiff files")

0 comments on commit ea37575

Please sign in to comment.