|
1 | 1 | import warnings |
2 | 2 |
|
3 | 3 | import numpy as np |
4 | | -from packaging.version import Version |
5 | | - |
6 | | -from .pycompat import dask_version |
7 | 4 |
|
8 | 5 | try: |
9 | 6 | import dask.array as da |
@@ -57,127 +54,7 @@ def pad(array, pad_width, mode="constant", **kwargs): |
57 | 54 | return padded |
58 | 55 |
|
59 | 56 |
|
60 | | -if dask_version > Version("2.30.0"): |
61 | | - ensure_minimum_chunksize = da.overlap.ensure_minimum_chunksize |
62 | | -else: |
63 | | - |
64 | | - # copied from dask |
65 | | - def ensure_minimum_chunksize(size, chunks): |
66 | | - """Determine new chunks to ensure that every chunk >= size |
67 | | -
|
68 | | - Parameters |
69 | | - ---------- |
70 | | - size : int |
71 | | - The maximum size of any chunk. |
72 | | - chunks : tuple |
73 | | - Chunks along one axis, e.g. ``(3, 3, 2)`` |
74 | | -
|
75 | | - Examples |
76 | | - -------- |
77 | | - >>> ensure_minimum_chunksize(10, (20, 20, 1)) |
78 | | - (20, 11, 10) |
79 | | - >>> ensure_minimum_chunksize(3, (1, 1, 3)) |
80 | | - (5,) |
81 | | -
|
82 | | - See Also |
83 | | - -------- |
84 | | - overlap |
85 | | - """ |
86 | | - if size <= min(chunks): |
87 | | - return chunks |
88 | | - |
89 | | - # add too-small chunks to chunks before them |
90 | | - output = [] |
91 | | - new = 0 |
92 | | - for c in chunks: |
93 | | - if c < size: |
94 | | - if new > size + (size - c): |
95 | | - output.append(new - (size - c)) |
96 | | - new = size |
97 | | - else: |
98 | | - new += c |
99 | | - if new >= size: |
100 | | - output.append(new) |
101 | | - new = 0 |
102 | | - if c >= size: |
103 | | - new += c |
104 | | - if new >= size: |
105 | | - output.append(new) |
106 | | - elif len(output) >= 1: |
107 | | - output[-1] += new |
108 | | - else: |
109 | | - raise ValueError( |
110 | | - f"The overlapping depth {size} is larger than your " |
111 | | - f"array {sum(chunks)}." |
112 | | - ) |
113 | | - |
114 | | - return tuple(output) |
115 | | - |
116 | | - |
117 | | -if dask_version > Version("2021.03.0"): |
| 57 | +if da is not None: |
118 | 58 | sliding_window_view = da.lib.stride_tricks.sliding_window_view |
119 | 59 | else: |
120 | | - |
121 | | - def sliding_window_view(x, window_shape, axis=None): |
122 | | - from dask.array.overlap import map_overlap |
123 | | - from numpy.core.numeric import normalize_axis_tuple |
124 | | - |
125 | | - from .npcompat import sliding_window_view as _np_sliding_window_view |
126 | | - |
127 | | - window_shape = ( |
128 | | - tuple(window_shape) if np.iterable(window_shape) else (window_shape,) |
129 | | - ) |
130 | | - |
131 | | - window_shape_array = np.array(window_shape) |
132 | | - if np.any(window_shape_array <= 0): |
133 | | - raise ValueError("`window_shape` must contain positive values") |
134 | | - |
135 | | - if axis is None: |
136 | | - axis = tuple(range(x.ndim)) |
137 | | - if len(window_shape) != len(axis): |
138 | | - raise ValueError( |
139 | | - f"Since axis is `None`, must provide " |
140 | | - f"window_shape for all dimensions of `x`; " |
141 | | - f"got {len(window_shape)} window_shape elements " |
142 | | - f"and `x.ndim` is {x.ndim}." |
143 | | - ) |
144 | | - else: |
145 | | - axis = normalize_axis_tuple(axis, x.ndim, allow_duplicate=True) |
146 | | - if len(window_shape) != len(axis): |
147 | | - raise ValueError( |
148 | | - f"Must provide matching length window_shape and " |
149 | | - f"axis; got {len(window_shape)} window_shape " |
150 | | - f"elements and {len(axis)} axes elements." |
151 | | - ) |
152 | | - |
153 | | - depths = [0] * x.ndim |
154 | | - for ax, window in zip(axis, window_shape): |
155 | | - depths[ax] += window - 1 |
156 | | - |
157 | | - # Ensure that each chunk is big enough to leave at least a size-1 chunk |
158 | | - # after windowing (this is only really necessary for the last chunk). |
159 | | - safe_chunks = tuple( |
160 | | - ensure_minimum_chunksize(d + 1, c) for d, c in zip(depths, x.chunks) |
161 | | - ) |
162 | | - x = x.rechunk(safe_chunks) |
163 | | - |
164 | | - # result.shape = x_shape_trimmed + window_shape, |
165 | | - # where x_shape_trimmed is x.shape with every entry |
166 | | - # reduced by one less than the corresponding window size. |
167 | | - # trim chunks to match x_shape_trimmed |
168 | | - newchunks = tuple( |
169 | | - c[:-1] + (c[-1] - d,) for d, c in zip(depths, x.chunks) |
170 | | - ) + tuple((window,) for window in window_shape) |
171 | | - |
172 | | - kwargs = dict( |
173 | | - depth=tuple((0, d) for d in depths), # Overlap on +ve side only |
174 | | - boundary="none", |
175 | | - meta=x._meta, |
176 | | - new_axis=range(x.ndim, x.ndim + len(axis)), |
177 | | - chunks=newchunks, |
178 | | - trim=False, |
179 | | - window_shape=window_shape, |
180 | | - axis=axis, |
181 | | - ) |
182 | | - |
183 | | - return map_overlap(_np_sliding_window_view, x, align_arrays=False, **kwargs) |
| 60 | + sliding_window_view = None |
0 commit comments