Skip to content

Commit 3cc8410

Browse files
committed
Give examples of the binsparse descriptor.
1 parent c8e4d60 commit 3cc8410

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

spec/draft/extensions/sparse_interchange.rst

+57-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Sparse interchange
44
==================
55

6-
Array API specification for sparse interchange functions.
6+
Array API specification for sparse interchange functions using `binsparse <https://graphblas.org/binsparse-specification/>`_.
77

88
Extension name and usage
99
------------------------
@@ -14,11 +14,66 @@ If implemented, this extension must be retrievable via::
1414
>>> if hasattr(xp, 'sparse'):
1515
>>> # Use the extension
1616

17-
.. currentmodule:: array_api
17+
To convert an object from another library supporting also supporting the sparse interchange extension::
18+
19+
>>> xp1 = xp1.sparse.from_binsparse(xp2_array) # Convert with the same formats
20+
>>> xp1 = xp1.sparse.from_binsparse(xp2_array, descriptor=binsparse_descriptor)
21+
22+
.. _binsparse_descriptor_examples:
23+
24+
Examples of binsparse descriptors
25+
---------------------------------
26+
27+
While the `binsparse specification <https://graphblas.org/binsparse-specification/>`_ uses JSON for its descriptor,
28+
we will work with equivalent Python objects instead. Here are some examples of binsparse descriptors::
29+
30+
>>> coo_2d_descriptor = {
31+
"binsparse": {
32+
"version": "0.1",
33+
"format": "COOR",
34+
"shape": [10, 12],
35+
"number_of_stored_values": 20,
36+
"data_types": {
37+
"indices_0": "uint64",
38+
"indices_1": "uint64",
39+
"values": "float32",
40+
},
41+
},
42+
"original_source": f"{library_name!s}, version {library_version!s}",
43+
}
44+
>>> csr_2d_descriptor = {
45+
"binsparse": {
46+
"version": "0.1",
47+
"format": "CSR",
48+
"shape": [20, 24],
49+
"number_of_stored_values": 20,
50+
"data_types": {
51+
"pointers_to_1": "uint64",
52+
"indices_1": "uint64",
53+
"values": "float32",
54+
},
55+
},
56+
"original_source": f"{library_name!s}, version {library_version!s}",
57+
}
58+
>>> compressed_vector_descriptor = {
59+
"binsparse": {
60+
"version": "0.1",
61+
"format": "CVEC",
62+
"shape": [30],
63+
"number_of_stored_values": 3,
64+
"data_types": {
65+
"indices_0": "uint64",
66+
"values": "float32",
67+
},
68+
},
69+
"original_source": f"{library_name!s}, version {library_version!s}",
70+
}
1871

1972
Objects in API
2073
--------------
2174

75+
.. currentmodule:: array_api
76+
2277
A conforming implementation of this extension must provide and support the following
2378
functions/methods. In addition, the ``asarray`` method must also be able to convert
2479
objects with supported formats which implement the protocol.

src/array_api_stubs/_draft/array_object.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ def __binsparse__(
12721272
self: array
12731273
array instance.
12741274
descriptor: Optional[dict]
1275-
If ``descriptor`` is not ``None``, the data returned must be in the format specified by it. If the format is unsupported, a ``TypeError`` must be raised.
1275+
If ``descriptor`` is not ``None``, the data returned must be in the format specified by it.
12761276
12771277
Returns
12781278
-------
@@ -1283,6 +1283,14 @@ def __binsparse__(
12831283
------
12841284
TypeError
12851285
If ``descriptor`` is not ``None``, and the array library does not support converting to a format specified by it.
1286+
ValueError
1287+
If ``descriptor`` is not a valid binsparse descriptor.
1288+
1289+
Notes
1290+
-----
1291+
1292+
- ``x.__binsparse_descriptor__()["binsparse"]["data_types"].keys() == x.__binsparse__().keys()`` must hold.
1293+
- ``descriptor["binsparse"]["data_types"].keys() == x.__binsparse__(descriptor=descriptor).keys()`` must hold.
12861294
"""
12871295

12881296

src/array_api_stubs/_draft/sparse.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def from_binsparse(
6767
on the input array. This may happen for libraries that are never able
6868
to export their data with binsparse.
6969
ValueError
70-
If data exchange is possible via an explicit copy but ``copy`` is set to ``False``.
70+
If data exchange is possible via an explicit copy but ``copy`` is set to ``False``, or if the specified
71+
descriptor is not valid.
7172
TypeError
7273
If ``descriptor`` is ``None``, the data received from the source library is not guaranteed to
7374
be in a format that the target array library supports. In this case, a ``TypeError`` must be raised.

0 commit comments

Comments
 (0)