Skip to content

Commit 59e47a0

Browse files
committed
Update with suggestions from @pearu.
1 parent 280ee10 commit 59e47a0

File tree

4 files changed

+16
-41
lines changed

4 files changed

+16
-41
lines changed

sparse/numba_backend/_compressed/compressed.py

+5-13
Original file line numberDiff line numberDiff line change
@@ -847,11 +847,8 @@ def isnan(self):
847847
# `GCXS` is a reshaped/transposed `CSR`, but it can't (usually)
848848
# be expressed in the `binsparse` 0.1 language.
849849
# We are missing index maps.
850-
def __binsparse_descriptor__(self) -> dict:
851-
return super().__binsparse_descriptor__()
852-
853-
def __binsparse_dlpack__(self) -> dict[str, np.ndarray]:
854-
return super().__binsparse_dlpack__()
850+
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
851+
return super().__binsparse__()
855852

856853

857854
class _Compressed2d(GCXS):
@@ -892,13 +889,13 @@ def from_numpy(cls, x, fill_value=0, idx_dtype=None):
892889
coo = COO.from_numpy(x, fill_value=fill_value, idx_dtype=idx_dtype)
893890
return cls.from_coo(coo, cls.class_compressed_axes, idx_dtype)
894891

895-
def __binsparse_descriptor__(self) -> dict:
892+
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
896893
from sparse._version import __version__
897894

898895
data_dt = str(self.data.dtype)
899896
if np.issubdtype(data_dt, np.complexfloating):
900897
data_dt = f"complex[float{self.data.dtype.itemsize // 2}]"
901-
return {
898+
descriptor = {
902899
"binsparse": {
903900
"version": "0.1",
904901
"format": self.format.upper(),
@@ -913,12 +910,7 @@ def __binsparse_descriptor__(self) -> dict:
913910
"original_source": f"`sparse`, version {__version__}",
914911
}
915912

916-
def __binsparse_dlpack__(self) -> dict[str, np.ndarray]:
917-
return {
918-
"pointers_to_1": self.indices,
919-
"indices_1": self.indptr,
920-
"values": self.data,
921-
}
913+
return descriptor, [self.indices, self.indptr, self.data]
922914

923915

924916
class CSR(_Compressed2d):

sparse/numba_backend/_coo/core.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1537,13 +1537,13 @@ def isnan(self):
15371537
prune=True,
15381538
)
15391539

1540-
def __binsparse_descriptor__(self) -> dict:
1540+
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
15411541
from sparse._version import __version__
15421542

15431543
data_dt = str(self.data.dtype)
15441544
if np.issubdtype(data_dt, np.complexfloating):
15451545
data_dt = f"complex[float{self.data.dtype.itemsize // 2}]"
1546-
return {
1546+
descriptor = {
15471547
"binsparse": {
15481548
"version": "0.1",
15491549
"format": {
@@ -1568,12 +1568,7 @@ def __binsparse_descriptor__(self) -> dict:
15681568
"original_source": f"`sparse`, version {__version__}",
15691569
}
15701570

1571-
def __binsparse_dlpack__(self) -> dict[str, np.ndarray]:
1572-
return {
1573-
"pointers_to_1": np.array([0, self.nnz], dtype=np.uint8),
1574-
"indices_1": self.coords,
1575-
"values": self.data,
1576-
}
1571+
return descriptor, [np.array([0, self.nnz], dtype=np.uint8), self.coords, self.data]
15771572

15781573

15791574
def as_coo(x, shape=None, fill_value=None, idx_dtype=None):

sparse/numba_backend/_dok.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,8 @@ def reshape(self, shape, order="C"):
548548

549549
return DOK.from_coo(self.to_coo().reshape(shape))
550550

551-
def __binsparse_descriptor__(self) -> dict:
552-
raise RuntimeError("`DOK` doesn't support the `__binsparse_descriptor__` protocol.")
553-
554-
def __binsparse_dlpack__(self) -> dict[str, np.ndarray]:
555-
raise RuntimeError("`DOK` doesn't support the `__binsparse_dlpack__` protocol.")
551+
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
552+
raise RuntimeError("`DOK` doesn't support the `__binsparse__` protocol.")
556553

557554

558555
def to_slice(k):

sparse/numba_backend/_sparse_array.py

+6-15
Original file line numberDiff line numberDiff line change
@@ -219,27 +219,18 @@ def _str_impl(self, summary):
219219
return summary
220220

221221
@abstractmethod
222-
def __binsparse_descriptor__(self) -> dict:
223-
"""Return a `dict` equivalent to a parsed JSON [`binsparse` descriptor](https://graphblas.org/binsparse-specification/#descriptor)
222+
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
223+
"""Return a 2-tuple:
224+
* First element is a `dict` equivalent to a parsed JSON [`binsparse` descriptor](https://graphblas.org/binsparse-specification/#descriptor)
224225
of this array.
226+
* Second element is a `list[np.ndarray]` of the constituent arrays.
225227
226228
Returns
227229
-------
228230
dict
229231
Parsed `binsparse` descriptor.
230-
"""
231-
raise NotImplementedError
232-
233-
@abstractmethod
234-
def __binsparse_dlpack__(self) -> dict[str, np.ndarray]:
235-
"""A `dict` containing the constituent arrays of this sparse array. The keys are compatible with the
236-
[`binsparse`](https://graphblas.org/binsparse-specification/) scheme, and the values are [`__dlpack__`](https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack__.html)
237-
compatible objects.
238-
239-
Returns
240-
-------
241-
dict[str, np.ndarray]
242-
The constituent arrays.
232+
list[np.ndarray]
233+
The constituent arrays
243234
"""
244235
raise NotImplementedError
245236

0 commit comments

Comments
 (0)