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

Use natural sorting in imread(...) when globbing multiple files #265

Merged
merged 4 commits into from
Jul 25, 2022
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
8 changes: 5 additions & 3 deletions dask_image/imread/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dask.array as da
import numpy as np
import pims
from tifffile import natural_sorted

from . import _utils

Expand All @@ -21,6 +22,8 @@ def imread(fname, nframes=1, *, arraytype="numpy"):
----------
fname : str or pathlib.Path
A glob like string that may match one or multiple filenames.
Where multiple filenames match, they are sorted using
natural (as opposed to alphabetical) sort.
nframes : int, optional
Number of the frames to include in each chunk (default: 1).
arraytype : str, optional
Expand Down Expand Up @@ -64,8 +67,8 @@ def imread(fname, nframes=1, *, arraytype="numpy"):
RuntimeWarning
)

# place source filenames into dask array
filenames = sorted(glob.glob(sfname)) # pims also does this
# place source filenames into dask array after sorting
filenames = natural_sorted(glob.glob(sfname))
if len(filenames) > 1:
ar = da.from_array(filenames, chunks=(nframes,))
multiple_files = True
Expand All @@ -83,7 +86,6 @@ def imread(fname, nframes=1, *, arraytype="numpy"):
arrayfunc=arrayfunc,
meta=arrayfunc([]).astype(dtype), # meta overwrites `dtype` argument
)

return a


Expand Down
8 changes: 8 additions & 0 deletions tests/test_dask_image/test_imread/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,11 @@ def test_tiff_imread(tmpdir, seed, nframes, shape, runtime_warning, dtype, is_pa
assert (shape[0] % nframes) == d.chunks[0][-1]

da.utils.assert_eq(a, d)


def test_tiff_imread_glob_natural_sort(tmpdir):
dirpth = tmpdir.mkdir("test_imread")
tifffile.imwrite(dirpth.join("10.tif"), np.array([10]))
tifffile.imwrite(dirpth.join("9.tif"), np.array([9]))
actual = np.array(dask_image.imread.imread(dirpth.join("*.tif")))
assert np.all(actual == np.array([[9], [10]]))