Determine automatic chunks This takes in a chunks value that contains ``"auto"`` values in certain dimensions and replaces those values with concrete dimension sizes that try to get chunks to be of a certain size in bytes, provided by the ``limit=`` keyword. If multiple dimensions
(chunks, shape, limit, dtype, previous_chunks=None)
| 3255 | |
| 3256 | |
| 3257 | def auto_chunks(chunks, shape, limit, dtype, previous_chunks=None): |
| 3258 | """Determine automatic chunks |
| 3259 | |
| 3260 | This takes in a chunks value that contains ``"auto"`` values in certain |
| 3261 | dimensions and replaces those values with concrete dimension sizes that try |
| 3262 | to get chunks to be of a certain size in bytes, provided by the ``limit=`` |
| 3263 | keyword. If multiple dimensions are marked as ``"auto"`` then they will |
| 3264 | all respond to meet the desired byte limit, trying to respect the aspect |
| 3265 | ratio of their dimensions in ``previous_chunks=``, if given. |
| 3266 | |
| 3267 | Parameters |
| 3268 | ---------- |
| 3269 | chunks: Tuple |
| 3270 | A tuple of either dimensions or tuples of explicit chunk dimensions |
| 3271 | Some entries should be "auto" |
| 3272 | shape: Tuple[int] |
| 3273 | limit: int, str |
| 3274 | The maximum allowable size of a chunk in bytes |
| 3275 | previous_chunks: Tuple[Tuple[int]] |
| 3276 | |
| 3277 | See also |
| 3278 | -------- |
| 3279 | normalize_chunks: for full docstring and parameters |
| 3280 | """ |
| 3281 | if previous_chunks is not None: |
| 3282 | # rioxarray is passing ((1, ), (x,)) for shapes like (100, 5x), |
| 3283 | # so add this compat code for now |
| 3284 | # https://github.com/corteva/rioxarray/pull/820 |
| 3285 | previous_chunks = ( |
| 3286 | c[0] if isinstance(c, tuple) and len(c) == 1 else c for c in previous_chunks |
| 3287 | ) |
| 3288 | previous_chunks = _convert_int_chunk_to_tuple(shape, previous_chunks) |
| 3289 | chunks = list(chunks) |
| 3290 | |
| 3291 | autos = {i for i, c in enumerate(chunks) if c == "auto"} |
| 3292 | if not autos: |
| 3293 | return tuple(chunks) |
| 3294 | |
| 3295 | if limit is None: |
| 3296 | limit = config.get("array.chunk-size") |
| 3297 | if isinstance(limit, str): |
| 3298 | limit = parse_bytes(limit) |
| 3299 | |
| 3300 | if dtype is None: |
| 3301 | raise TypeError("dtype must be known for auto-chunking") |
| 3302 | |
| 3303 | if dtype.hasobject: |
| 3304 | raise NotImplementedError( |
| 3305 | "Can not use auto rechunking with object dtype. " |
| 3306 | "We are unable to estimate the size in bytes of object data" |
| 3307 | ) |
| 3308 | |
| 3309 | for x in tuple(chunks) + tuple(shape): |
| 3310 | if ( |
| 3311 | isinstance(x, Number) |
| 3312 | and np.isnan(x) |
| 3313 | or isinstance(x, tuple) |
| 3314 | and np.isnan(x).any() |
no test coverage detected
searching dependent graphs…