zarr-developers / zarr-python

An implementation of chunked, compressed, N-dimensional arrays for Python.
https://zarr.readthedocs.io
MIT License
1.5k stars 278 forks source link

Empty array & chunk definition #303

Open thomascoquet opened 6 years ago

thomascoquet commented 6 years ago
import zarr

store = zarr.DirectoryStore('data')
r = zarr.open_group(store=store)

z1 = r.full('V1', None,
            shape=(0,),
            chunks=None,
            dtype='i4',
            compressor=None)

print(z1[:])

z2 = r.full('V2', None,
            shape=(1,),
            chunks=(None,),
            dtype='i4',
            compressor=None)

print(z2[:])

z3 = r.full('V3', None,
            shape=(0,),
            chunks=(None,),
            dtype='i4',
            compressor=None)

print(z3[:])

Problem description

An exception is raised when array has a length of 0 & chunk is specified using (None,) . Result of above code:


[]
[897988541]
Traceback (most recent call last):
  File "/home/tom/PycharmProjects/starling-hcs/forest-penrose-zcube/examples/example_raster.py", line 31, in <module>
    print(z3[:])
  File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/core.py", line 559, in __getitem__
    return self.get_basic_selection(selection, fields=fields)
  File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/core.py", line 685, in get_basic_selection
    fields=fields)
  File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/core.py", line 725, in _get_basic_selection_nd
    indexer = BasicIndexer(selection, self)
  File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/indexing.py", line 279, in __init__
    dim_indexer = SliceDimIndexer(dim_sel, dim_len, dim_chunk_len)
  File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/indexing.py", line 113, in __init__
    self.nchunks = ceildiv(self.dim_len, self.dim_chunk_len)
  File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/indexing.py", line 97, in ceildiv
    return int(np.ceil(a / b))
ZeroDivisionError: division by zero

Version and installation information

python 3.6 zarr 2.2.0 numcodecs 0.5.5 Ubuntu

alimanfoo commented 6 years ago

Thanks Thomas. I'm not sure if there's anything better we could do here. Using None as a chunk length for a dimension is mean to indicate use the total dimension size for the chunk length, i.e., no chunking along the given dimension, IIRC. If dimension length is zero then chunk length will also be zero for a dimension Ending up with zero for the chunk length along any dimension is never going to be a good thing, it will always mean infinite chunks. Maybe a better error message to catch cases like this, and check we never allow chunk dimensiom length of zero under any circumstances?

On Mon, 24 Sep 2018, 16:55 Thomas Coquet, notifications@github.com wrote:

import zarr

store = zarr.DirectoryStore('data') r = zarr.open_group(store=store)

z1 = r.full('V1', None, shape=(0,), chunks=None, dtype='i4', compressor=None)

print(z1[:])

z2 = r.full('V2', None, shape=(1,), chunks=(None,), dtype='i4', compressor=None)

print(z2[:])

z3 = r.full('V3', None, shape=(0,), chunks=(None,), dtype='i4', compressor=None)

print(z3[:])

Problem description

An exception is raised when array has a length of 0 & chunk is specified using (None,) . Result of above code:

[] [897988541] Traceback (most recent call last): File "/home/tom/PycharmProjects/starling-hcs/forest-penrose-zcube/examples/example_raster.py", line 31, in print(z3[:]) File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/core.py", line 559, in getitem return self.get_basic_selection(selection, fields=fields) File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/core.py", line 685, in get_basic_selection fields=fields) File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/core.py", line 725, in _get_basic_selection_nd indexer = BasicIndexer(selection, self) File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/indexing.py", line 279, in init dim_indexer = SliceDimIndexer(dim_sel, dim_len, dim_chunk_len) File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/indexing.py", line 113, in init self.nchunks = ceildiv(self.dim_len, self.dim_chunk_len) File "/home/tom/anaconda3/lib/python3.6/site-packages/zarr/indexing.py", line 97, in ceildiv return int(np.ceil(a / b)) ZeroDivisionError: division by zero

Version and installation information

python 3.6 zarr 2.2.0 numcodecs 0.5.5 Ubuntu

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zarr-developers/zarr/issues/303, or mute the thread https://github.com/notifications/unsubscribe-auth/AAq8QoXq9ByvQa2ALlrUa0NZjJrWo5gJks5uePJUgaJpZM4W20_U .