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