diff --git a/dask_image/imread/__init__.py b/dask_image/imread/__init__.py index 9cd5aaba..3649f147 100644 --- a/dask_image/imread/__init__.py +++ b/dask_image/imread/__init__.py @@ -6,6 +6,7 @@ import dask.array as da import numpy as np import pims +from tifffile import natural_sorted from . import _utils @@ -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 @@ -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 @@ -83,7 +86,6 @@ def imread(fname, nframes=1, *, arraytype="numpy"): arrayfunc=arrayfunc, meta=arrayfunc([]).astype(dtype), # meta overwrites `dtype` argument ) - return a diff --git a/tests/test_dask_image/test_imread/test_core.py b/tests/test_dask_image/test_imread/test_core.py index 07f8d26f..ddebc70c 100644 --- a/tests/test_dask_image/test_imread/test_core.py +++ b/tests/test_dask_image/test_imread/test_core.py @@ -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]]))