Debian Codegraph @

dask_image/imread/__init__.py

raw
1 # -*- coding: utf-8 -*-
2 import glob
3 import numbers
4 import warnings
5
6 import dask.array as da
7 import numpy as np
8 import pims
9 from tifffile import natural_sorted
10
11
12 def imread(fname, nframes=1, *, arraytype="numpy"):
13 """
14 Read image data into a Dask Array.
15
16 Provides a simple, fast mechanism to ingest image data into a
17 Dask Array. This uses the `pims` package to open images.
18
19 Parameters
20 ----------
21 fname : str or pathlib.Path
22 A glob like string that may match one or multiple filenames.
23 Where multiple filenames match, they are sorted using
24 natural (as opposed to alphabetical) sort.
25 nframes : int, optional
26 Number of the frames to include in each chunk (default: 1).
27 arraytype : str, optional
28 Array type for dask chunks. Available options: "numpy", "cupy".
29
30 Returns
31 -------
32 array : dask.array.Array
33 A Dask Array representing the contents of all image files.
34
35 Warnings
36 --------
37 There are several known issues with this function, and users are
38 recommended to use `dask.array.image.imread` or `bioio` instead.
39 """
40
41 sfname = str(fname)
42 if not isinstance(nframes, numbers.Integral):
43 raise ValueError("`nframes` must be an integer.")
44 if (nframes != -1) and not (nframes > 0):
45 raise ValueError("`nframes` must be greater than zero.")
46
47 if arraytype == "numpy":
48 arrayfunc = np.asanyarray
49 elif arraytype == "cupy": # pragma: no cover
50 import cupy
51 arrayfunc = cupy.asanyarray
52
53 with pims.open(sfname) as imgs:
54 shape = (len(imgs),) + imgs.frame_shape
55 dtype = np.dtype(imgs.pixel_type)
56
57 if nframes == -1:
58 nframes = shape[0]
59
60 if nframes > shape[0]:
61 warnings.warn(
62 "`nframes` larger than number of frames in file."
63 " Will truncate to number of frames in file.",
64 RuntimeWarning
65 )
66 elif shape[0] % nframes != 0:
67 warnings.warn(
68 "`nframes` does not nicely divide number of frames in file."
69 " Last chunk will contain the remainder.",
70 RuntimeWarning
71 )
72
73 # place source filenames into dask array after sorting
74 filenames = natural_sorted(glob.glob(sfname))
75 if len(filenames) > 1:
76 ar = da.from_array(filenames, chunks=(nframes,))
77 multiple_files = True
78 else:
79 ar = da.from_array(filenames * shape[0], chunks=(nframes,))
80 multiple_files = False
81
82 # read in data using encoded filenames
83 a = ar.map_blocks(
84 _map_read_frame,
85 chunks=da.core.normalize_chunks(
86 (nframes,) + shape[1:], shape),
87 multiple_files=multiple_files,
88 new_axis=list(range(1, len(shape))),
89 arrayfunc=arrayfunc,
90 meta=arrayfunc([]).astype(dtype), # meta overwrites `dtype` argument
91 )
92 return a
93
94
95 def _map_read_frame(x, multiple_files, block_info=None, **kwargs):
96
97 fn = x[0] # get filename from input chunk
98
99 if multiple_files:
100 i, j = 0, 1
101 else:
102 i, j = block_info[None]['array-location'][0]
103
104 return _read_frame(fn=fn, i=slice(i, j), **kwargs)
105
106
107 def _read_frame(fn, i, *, arrayfunc=np.asanyarray):
108 with pims.open(fn) as imgs:
109 return arrayfunc(imgs[i])