Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility to turn block-ID into equivalent slice #545

Open
jhamman opened this issue Mar 9, 2020 · 1 comment
Open

Utility to turn block-ID into equivalent slice #545

jhamman opened this issue Mar 9, 2020 · 1 comment

Comments

@jhamman
Copy link
Member

jhamman commented Mar 9, 2020

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).

@jakirkham
Copy link
Member

Similar to issue ( #543 ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants