Skip to content

Commit

Permalink
Use natural sorting in imread(...) when globbing multiple files (#265)
Browse files Browse the repository at this point in the history
* Add sortfunc argument to imread to customize glob order

* Fix flake8 complaints

* Refine docstring

* Remove custom sortfunc, default to natural sort

Co-authored-by: Volker Hilsenstein <volker.hilsenstein@embl.de>
  • Loading branch information
VolkerH and Volker Hilsenstein committed Jul 25, 2022
1 parent 902fc1b commit 5cf77bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
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]]))

0 comments on commit 5cf77bf

Please sign in to comment.