Skip to content

Commit 79cf3fa

Browse files
doc: update io.rst for awkward array (#457)
* Update io.rst As raised in #436 * Update documentation for `awkward-array`-related io functions. * Update the doc string and change the order of the functions to reflect their intended use case. remove trailing blanks * Use double backticks
1 parent e3118da commit 79cf3fa

File tree

2 files changed

+75
-52
lines changed

2 files changed

+75
-52
lines changed

docs/api_reference/io.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ These methods require `scipy <https://scipy.org/>`_ to be installed.
4949

5050
.. autofunction:: graphblas.io.mmwrite
5151

52+
Awkward Array
53+
~~~~~~~~~~~~~
54+
55+
`Awkward Array <https://awkward-array.org/doc/main/>`_ is a library for nested,
56+
variable-sized data, including arbitrary-length lists, records, mixed types,
57+
and missing data, using NumPy-like idioms. Note that the intended use of the
58+
``awkward-array``-related ``io`` functions is to convert ``graphblas`` objects to awkward,
59+
perform necessary computations/transformations and, if required, convert the
60+
awkward array back to ``graphblas`` format. To facilitate this conversion process,
61+
``graphblas.io.to_awkward`` adds top-level attribute ``format``, describing the
62+
format of the ``graphblas`` object (this attributed is used by the
63+
``graphblas.io.from_awkward`` function to reconstruct the ``graphblas`` object).
64+
65+
.. autofunction:: graphblas.io.to_awkward
66+
67+
.. autofunction:: graphblas.io.from_awkward
68+
5269
Visualization
5370
~~~~~~~~~~~~~
5471

graphblas/io/_awkward.py

Lines changed: 58 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,58 +7,6 @@
77
_AwkwardDoublyCompressedMatrix = None
88

99

10-
def from_awkward(A, *, name=None):
11-
"""Create a Matrix or Vector from an Awkward Array.
12-
13-
The Awkward Array must have top-level parameters: format, shape
14-
15-
The Awkward Array must have top-level attributes based on format:
16-
- vec/csr/csc: values, indices
17-
- hypercsr/hypercsc: values, indices, offset_labels
18-
19-
Parameters
20-
----------
21-
A : awkward.Array
22-
Awkward Array with values and indices
23-
name : str, optional
24-
Name of resulting Matrix or Vector
25-
26-
Returns
27-
-------
28-
Vector or Matrix
29-
"""
30-
params = A.layout.parameters
31-
if missing := {"format", "shape"} - params.keys():
32-
raise ValueError(f"Missing parameters: {missing}")
33-
format = params["format"]
34-
shape = params["shape"]
35-
36-
if len(shape) == 1:
37-
if format != "vec":
38-
raise ValueError(f"Invalid format for Vector: {format}")
39-
return Vector.from_coo(
40-
A.indices.layout.data, A.values.layout.data, size=shape[0], name=name
41-
)
42-
nrows, ncols = shape
43-
values = A.values.layout.content.data
44-
indptr = A.values.layout.offsets.data
45-
if format == "csr":
46-
cols = A.indices.layout.content.data
47-
return Matrix.from_csr(indptr, cols, values, ncols=ncols, name=name)
48-
if format == "csc":
49-
rows = A.indices.layout.content.data
50-
return Matrix.from_csc(indptr, rows, values, nrows=nrows, name=name)
51-
if format == "hypercsr":
52-
rows = A.offset_labels.layout.data
53-
cols = A.indices.layout.content.data
54-
return Matrix.from_dcsr(rows, indptr, cols, values, nrows=nrows, ncols=ncols, name=name)
55-
if format == "hypercsc":
56-
cols = A.offset_labels.layout.data
57-
rows = A.indices.layout.content.data
58-
return Matrix.from_dcsc(cols, indptr, rows, values, nrows=nrows, ncols=ncols, name=name)
59-
raise ValueError(f"Invalid format for Matrix: {format}")
60-
61-
6210
def to_awkward(A, format=None):
6311
"""Create an Awkward Array from a GraphBLAS Matrix.
6412
@@ -179,3 +127,61 @@ def indices(self): # pragma: no branch (???)
179127
if classname:
180128
ret = ak.with_name(ret, classname)
181129
return ret
130+
131+
132+
def from_awkward(A, *, name=None):
133+
"""Create a Matrix or Vector from an Awkward Array.
134+
135+
The Awkward Array must have top-level parameters: format, shape
136+
137+
The Awkward Array must have top-level attributes based on format:
138+
- vec/csr/csc: values, indices
139+
- hypercsr/hypercsc: values, indices, offset_labels
140+
141+
Parameters
142+
----------
143+
A : awkward.Array
144+
Awkward Array with values and indices
145+
name : str, optional
146+
Name of resulting Matrix or Vector
147+
148+
Returns
149+
-------
150+
Vector or Matrix
151+
152+
Note: the intended purpose of this function is to facilitate
153+
conversion of an `awkward-array` that was created via `to_awkward`
154+
function. If attempting to convert an arbitrary `awkward-array`,
155+
make sure that the top-level attributes and parameters contain
156+
the expected values.
157+
"""
158+
params = A.layout.parameters
159+
if missing := {"format", "shape"} - params.keys():
160+
raise ValueError(f"Missing parameters: {missing}")
161+
format = params["format"]
162+
shape = params["shape"]
163+
164+
if len(shape) == 1:
165+
if format != "vec":
166+
raise ValueError(f"Invalid format for Vector: {format}")
167+
return Vector.from_coo(
168+
A.indices.layout.data, A.values.layout.data, size=shape[0], name=name
169+
)
170+
nrows, ncols = shape
171+
values = A.values.layout.content.data
172+
indptr = A.values.layout.offsets.data
173+
if format == "csr":
174+
cols = A.indices.layout.content.data
175+
return Matrix.from_csr(indptr, cols, values, ncols=ncols, name=name)
176+
if format == "csc":
177+
rows = A.indices.layout.content.data
178+
return Matrix.from_csc(indptr, rows, values, nrows=nrows, name=name)
179+
if format == "hypercsr":
180+
rows = A.offset_labels.layout.data
181+
cols = A.indices.layout.content.data
182+
return Matrix.from_dcsr(rows, indptr, cols, values, nrows=nrows, ncols=ncols, name=name)
183+
if format == "hypercsc":
184+
cols = A.offset_labels.layout.data
185+
rows = A.indices.layout.content.data
186+
return Matrix.from_dcsc(cols, indptr, rows, values, nrows=nrows, ncols=ncols, name=name)
187+
raise ValueError(f"Invalid format for Matrix: {format}")

0 commit comments

Comments
 (0)