Skip to content

Commit c314d08

Browse files
committed
Update to latest agreed-upon binsparse protocol version.
1 parent 0dfa265 commit c314d08

File tree

6 files changed

+38
-26
lines changed

6 files changed

+38
-26
lines changed

pixi.toml

-5
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@ pytest-cov = "*"
3636
pytest-xdist = "*"
3737
pytest-codspeed = "*"
3838

39-
<<<<<<< HEAD
4039
[feature.notebooks.pypi-dependencies]
4140
ipykernel = "*"
42-
=======
43-
[feature.notebooks.dependencies]
44-
ipython = "*"
45-
>>>>>>> 54b7c1d (Fix `notebooks` feature and add it to development environments.)
4641
nbmake = "*"
4742
matplotlib = "*"
4843
networkx = "*"

sparse/numba_backend/_compressed/compressed.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,12 @@ def isnan(self):
816816
# `GCXS` is a reshaped/transposed `CSR`, but it can't (usually)
817817
# be expressed in the `binsparse` 0.1 language.
818818
# We are missing index maps.
819-
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
819+
def __binsparse__(self) -> dict:
820820
return super().__binsparse__()
821821

822+
def __binsparse_descriptor__(self) -> dict[str, np.ndarray]:
823+
return super().__binsparse_descriptor__()
824+
822825

823826
class _Compressed2d(GCXS):
824827
class_compressed_axes: tuple[int]
@@ -858,13 +861,13 @@ def from_numpy(cls, x, fill_value=0, idx_dtype=None):
858861
coo = COO.from_numpy(x, fill_value=fill_value, idx_dtype=idx_dtype)
859862
return cls.from_coo(coo, cls.class_compressed_axes, idx_dtype)
860863

861-
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
864+
def __binsparse_descriptor__(self) -> dict:
862865
from sparse._version import __version__
863866

864867
data_dt = str(self.data.dtype)
865868
if np.issubdtype(data_dt, np.complexfloating):
866869
data_dt = f"complex[float{self.data.dtype.itemsize * 4}]"
867-
descriptor = {
870+
return {
868871
"binsparse": {
869872
"version": "0.1",
870873
"format": self.format.upper(),
@@ -879,7 +882,8 @@ def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
879882
"original_source": f"`sparse`, version {__version__}",
880883
}
881884

882-
return descriptor, [self.indptr, self.indices, self.data]
885+
def __binsparse__(self) -> dict[str, np.ndarray]:
886+
return {"pointers_to_1": self.indptr, "indices_1": self.indices, "values": self.data}
883887

884888

885889
class CSR(_Compressed2d):

sparse/numba_backend/_coo/core.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1515,13 +1515,13 @@ def isnan(self):
15151515
prune=True,
15161516
)
15171517

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

15211521
data_dt = str(self.data.dtype)
15221522
if np.issubdtype(data_dt, np.complexfloating):
15231523
data_dt = f"complex[float{self.data.dtype.itemsize * 4}]"
1524-
descriptor = {
1524+
return {
15251525
"binsparse": {
15261526
"version": "0.1",
15271527
"format": {
@@ -1548,7 +1548,12 @@ def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
15481548
"original_source": f"`sparse`, version {__version__}",
15491549
}
15501550

1551-
return descriptor, [np.array([0, self.nnz], dtype=np.uint64), self.coords, self.data]
1551+
def __binsparse__(self) -> dict[str, np.ndarray]:
1552+
return {
1553+
"pointers_to_1": np.array([0, self.nnz], dtype=np.uint64),
1554+
"indices_1": self.coords,
1555+
"values": self.data,
1556+
}
15521557

15531558

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

sparse/numba_backend/_dok.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ def reshape(self, shape, order="C"):
527527
return DOK.from_coo(self.to_coo().reshape(shape))
528528

529529
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
530-
raise RuntimeError("`DOK` doesn't support the `__binsparse__` protocol.")
530+
raise TypeError("`DOK` doesn't support the `__binsparse__` protocol.")
531531

532532

533533
def to_slice(k):

sparse/numba_backend/_io.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ def load_npz(filename):
136136

137137
@_check_device
138138
def from_binsparse(arr, /, *, device=None, copy: bool | None = None) -> SparseArray:
139-
desc, arrs = arr.__binsparse__()
139+
desc = arr.__binsparse_descriptor__()
140+
arrs = arr.__binsparse__()
140141

141142
desc = desc["binsparse"]
142143
version_tuple: tuple[int, ...] = tuple(int(v) for v in desc["version"].split("."))
@@ -216,13 +217,13 @@ def from_binsparse(arr, /, *, device=None, copy: bool | None = None) -> SparseAr
216217
if transpose != list(range(ndim)):
217218
raise RuntimeError(format_err_str)
218219

219-
ptr_arr: np.ndarray = np.from_dlpack(arrs[0])
220+
ptr_arr: np.ndarray = np.from_dlpack(arrs["pointers_to_1"])
220221
start, end = ptr_arr
221222
if copy is False and not (start == 0 or end == nnz):
222223
raise RuntimeError(format_err_str)
223224

224-
coord_arr: np.ndarray = np.from_dlpack(arrs[1])
225-
value_arr: np.ndarray = np.from_dlpack(arrs[2])
225+
coord_arr: np.ndarray = np.from_dlpack(arrs["indices_1"])
226+
value_arr: np.ndarray = np.from_dlpack(arrs["values"])
226227

227228
_check_binsparse_dt(coord_arr, coords_dtype)
228229
_check_binsparse_dt(value_arr, value_dtype)
@@ -262,11 +263,11 @@ def from_binsparse(arr, /, *, device=None, copy: bool | None = None) -> SparseAr
262263
},
263264
**_kwargs,
264265
}:
265-
crd_arr = np.from_dlpack(arrs[0])
266+
crd_arr = np.from_dlpack(arrs["pointers_to_1"])
266267
_check_binsparse_dt(crd_arr, crd_dtype)
267-
ptr_arr = np.from_dlpack(arrs[1])
268+
ptr_arr = np.from_dlpack(arrs["indices_1"])
268269
_check_binsparse_dt(ptr_arr, ptr_dtype)
269-
val_arr = np.from_dlpack(arrs[2])
270+
val_arr = np.from_dlpack(arrs["values"])
270271
_check_binsparse_dt(val_arr, val_dtype)
271272

272273
match transpose:

sparse/numba_backend/_sparse_array.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,25 @@ def _str_impl(self, summary):
247247
return summary
248248

249249
@abstractmethod
250-
def __binsparse__(self) -> tuple[dict, list[np.ndarray]]:
251-
"""Return a 2-tuple:
252-
* First element is a `dict` equivalent to a parsed JSON [`binsparse` descriptor](https://graphblas.org/binsparse-specification/#descriptor)
250+
def __binsparse_descriptor__(self) -> dict:
251+
"""Return a `dict` equivalent to a parsed JSON [`binsparse` descriptor](https://graphblas.org/binsparse-specification/#descriptor)
253252
of this array.
254-
* Second element is a `list[np.ndarray]` of the constituent arrays.
255253
256254
Returns
257255
-------
258256
dict
259257
Parsed `binsparse` descriptor.
260-
list[np.ndarray]
261-
The constituent arrays
258+
"""
259+
raise NotImplementedError
260+
261+
@abstractmethod
262+
def __binsparse__(self) -> dict[str, np.ndarray]:
263+
"""Return a is a `dict[str, np.ndarray]` of the constituent arrays.
264+
265+
Returns
266+
-------
267+
dict
268+
Parsed `binsparse` descriptor.
262269
"""
263270
raise NotImplementedError
264271

0 commit comments

Comments
 (0)