Skip to content

ValueError when creating NDArray with shape=1 and large blocks/chunks #577

@Jacc4224

Description

@Jacc4224

Describe the bug
Cannot create a blosc2.NDArray with shape=1 if I specify chunks or blocks larger than 1. The error ValueError: blocks cannot be greater than shape if it is 1 is raised.

To Reproduce
Steps to reproduce the behavior:

import blosc2
import numpy as np

# Define dtype
dt = np.int64

# Create an array (shape=1) causes crash
expected_size = 1_000_000
c, b = compute_chunks_blocks((expected_size,))
my_array = blosc2.zeros(shape=1, dtype = dt , chunks=c, blocks=b)

Output error:

Traceback (most recent call last):
  File "/home/jorge/PycharmProjects/python-blosc2/src/ctable_pruebas_2.py", line 693, in <module>
    tabla = CTable(RowModel)
            ^^^^^^^^^^^^^^^^
  File "/home/jorge/PycharmProjects/python-blosc2/src/ctable_pruebas_2.py", line 94, in __init__
    self._valid_rows = blosc2.zeros(shape=1, dtype = np.bool_ , chunks=c, blocks=b)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jorge/PycharmProjects/python-blosc2/src/blosc2/ndarray.py", line 5015, in zeros
    chunks, blocks = compute_chunks_blocks(shape, chunks, blocks, dtype, **kwargs)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/jorge/PycharmProjects/python-blosc2/src/blosc2/core.py", line 1565, in compute_chunks_blocks
    raise ValueError("blocks cannot be greater than shape if it is 1")
ValueError: blocks cannot be greater than shape if it is 1

Process finished with exit code 1

Expected behavior
Should be able to create an array with shape=1.

Desktop (please complete the following information):

  • Python 3.12.4
  • Blosc2 version: 4.0.0-b2.dev0
  • OS: Ubuntu 24.04.3 LTS

Additional context
This seems to be an inconsistent restriction specific to shape=1.
If the shape changes from shape=1 to shape=2 in the exact same code above, it works perfectly fine even though the blocks are still much larger than the shape.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions