Helper to build old_chunks to new_chunks. Handles missing values, as long as the dimension with the missing chunk values is unchanged. Notes ----- This function expects that the arguments have been pre-processed by :func:`dask.array.core.normalize_chunks`. In particular any
(old_chunks, new_chunks)
| 157 | |
| 158 | |
| 159 | def old_to_new(old_chunks, new_chunks): |
| 160 | """Helper to build old_chunks to new_chunks. |
| 161 | |
| 162 | Handles missing values, as long as the dimension with the missing chunk values |
| 163 | is unchanged. |
| 164 | |
| 165 | Notes |
| 166 | ----- |
| 167 | This function expects that the arguments have been pre-processed by |
| 168 | :func:`dask.array.core.normalize_chunks`. In particular any ``nan`` values should |
| 169 | have been replaced (and are so by :func:`dask.array.core.normalize_chunks`) |
| 170 | by the canonical ``np.nan``. It also expects that the arguments have been validated |
| 171 | with `_validate_rechunk` and rechunking is thus possible. |
| 172 | |
| 173 | Examples |
| 174 | -------- |
| 175 | >>> old = ((10, 10, 10, 10, 10), ) |
| 176 | >>> new = ((25, 5, 20), ) |
| 177 | >>> old_to_new(old, new) # doctest: +NORMALIZE_WHITESPACE |
| 178 | [[[(0, slice(0, 10, None)), (1, slice(0, 10, None)), (2, slice(0, 5, None))], |
| 179 | [(2, slice(5, 10, None))], |
| 180 | [(3, slice(0, 10, None)), (4, slice(0, 10, None))]]] |
| 181 | """ |
| 182 | |
| 183 | def is_unknown(dim): |
| 184 | return any(math.isnan(chunk) for chunk in dim) |
| 185 | |
| 186 | dims_unknown = [is_unknown(dim) for dim in old_chunks] |
| 187 | |
| 188 | known_indices = [] |
| 189 | unknown_indices = [] |
| 190 | for i, unknown in enumerate(dims_unknown): |
| 191 | if unknown: |
| 192 | unknown_indices.append(i) |
| 193 | else: |
| 194 | known_indices.append(i) |
| 195 | |
| 196 | old_known = [old_chunks[i] for i in known_indices] |
| 197 | new_known = [new_chunks[i] for i in known_indices] |
| 198 | |
| 199 | cmos = cumdims_label(old_known, "o") |
| 200 | cmns = cumdims_label(new_known, "n") |
| 201 | |
| 202 | sliced = [None] * len(old_chunks) |
| 203 | for i, cmo, cmn in zip(known_indices, cmos, cmns): |
| 204 | sliced[i] = _intersect_1d(_breakpoints(cmo, cmn)) |
| 205 | |
| 206 | for i in unknown_indices: |
| 207 | dim = old_chunks[i] |
| 208 | # Unknown dimensions are always unchanged, so old -> new is everything |
| 209 | extra = [ |
| 210 | [(j, slice(0, size if not math.isnan(size) else None))] |
| 211 | for j, size in enumerate(dim) |
| 212 | ] |
| 213 | sliced[i] = extra |
| 214 | assert all(x is not None for x in sliced) |
| 215 | return sliced |
| 216 |
searching dependent graphs…