Skip to content

Can't create COO object if shape parameter is ndarray #541

@arzdou

Description

@arzdou

When creating a new sparse COO object there is an error if the shape is an ndarray instead of a list.

The error can be encountered as follows:

import numpy as np
import sparse

coords = [[0, 1, 2, 3, 4],

          [0, 1, 2, 3, 4]]

data = [10, 20, 30, 40, 50]

s = sparse.COO(coords, data, shape=np.array((5, 5)))

Which results in the following propt

File "<stdin>", line 1, in <module>
File "path\to\directory\.venv\lib\site-packages\sparse\_coo\core.py", line 244, in __init__
    if shape and not self.coords.size:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The error is produced when shape is casted into a bool, if it is a list the result will be False if it's empty and True in any other situation. For an ndarray the casting is done element by element and thus cannot be evaluated in an if statement.

The solution is just to cast the value of shape into a list before operating with it

System

  • Windows 10
  • sparse version 0.13.0
  • NumPy version 1.21.5
  • Numba version 0.55.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementIndicates new feature requests

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions