|
7 | 7 | _AwkwardDoublyCompressedMatrix = None
|
8 | 8 |
|
9 | 9 |
|
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 |
| - |
62 | 10 | def to_awkward(A, format=None):
|
63 | 11 | """Create an Awkward Array from a GraphBLAS Matrix.
|
64 | 12 |
|
@@ -179,3 +127,61 @@ def indices(self): # pragma: no branch (???)
|
179 | 127 | if classname:
|
180 | 128 | ret = ak.with_name(ret, classname)
|
181 | 129 | 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