From 5cf77bfc57fde60e14c1aa59566bb2eb4404bdb4 Mon Sep 17 00:00:00 2001 From: Volker Hilsenstein Date: Mon, 25 Jul 2022 03:01:03 +0200 Subject: [PATCH] Use natural sorting in `imread(...)` when globbing multiple files (#265) * 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 --- dask_image/imread/__init__.py | 8 +++++--- tests/test_dask_image/test_imread/test_core.py | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) 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]]))