Skip to content

Utility to turn block-ID into equivalent slice  #545

@jhamman

Description

@jhamman

Suppose I have a array block id (e.g. "0.0.0") and I want its equivalent slice on the full array. Does Zarr currently have a function to do this? I'm looking for something like this:

In [1]: block_id = '0.0.0'

In [2]: block_size = (10, 20, 30)

In [3]: def slice_axis(key, chunk_size):
   ...:     return slice(key * chunk_size, (key + 1) * chunk_size)
   ...:
   ...:
   ...: def get_indexers(key, chunks):
   ...:     ikeys = key.split(".")
   ...:     return tuple(slice_axis(int(i), c) for i, c in zip(ikeys, chunks))
   ...:

In [4]: get_indexers(block_id, block_size)
Out[4]: (slice(0, 10, None), slice(0, 20, None), slice(0, 30, None))

In [5]: import numpy as np

In [6]: data = np.random.random((100, 100, 100))

In [7]: slices = get_indexers(block_id, block_size)

In [8]: data[slices]

I recognize this is actually the inverse operation that Zarr takes when slicing a zarr array (usually we go from slice -> block_id).

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew features or improvements

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions