Repository navigation
Expand file tree
/
Copy pathsaps_sparse.py
More file actions
314 lines (263 loc) · 10.1 KB
/
Copy pathsaps_sparse.py
File metadata and controls
314 lines (263 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import numpy as np
import scipy.sparse as sps
import scipy.sparse.linalg as spla
import array_api_compat
import array_api_compat.numpy as compat_np
import sparse as sp
from binsparse import (
CustomTensor,
DenseLevel,
DMATCMatrix,
DMATRMatrix,
DVECVector,
ElementLevel,
)
from binsparse.conversions import from_numpy, from_sparse, to_numpy, to_sparse
from saps_framework import Framework, einsum
class PyDataSparseLinalg:
@staticmethod
def _dense(array):
if hasattr(array, "todense"):
return np.asarray(array.todense())
if hasattr(array, "toarray"):
return np.asarray(array.toarray())
return np.asarray(array)
@staticmethod
def _scipy_sparse(array):
if hasattr(array, "to_scipy_sparse"):
if array.ndim == 2:
return array.to_scipy_sparse()
if array.ndim == 1:
return array.reshape((array.shape[0], 1)).to_scipy_sparse()
raise ValueError(
"SciPy sparse linalg only supports one- or two-dimensional arrays."
)
array = np.asarray(array)
if array.ndim == 1:
return sps.coo_matrix(array.reshape((-1, 1)))
if array.ndim == 2:
return sps.coo_matrix(array)
raise ValueError(
"SciPy sparse linalg only supports one- or two-dimensional arrays."
)
@staticmethod
def _wrap_result(result):
if sps.issparse(result):
return sp.asarray(result)
if isinstance(result, np.ndarray):
return sp.asarray(result)
if isinstance(result, tuple):
return tuple(PyDataSparseLinalg._wrap_result(item) for item in result)
return result
@staticmethod
def solve(A, b):
A = PyDataSparseLinalg._scipy_sparse(A).tocsc()
b = PyDataSparseLinalg._scipy_sparse(b)
x = spla.spsolve(A, b)
return PyDataSparseLinalg._wrap_result(x)
@staticmethod
def pinv(A, **kwargs):
return np.linalg.pinv(PyDataSparseLinalg._dense(A), **kwargs)
@staticmethod
def lstsq(A, b, **kwargs):
return np.linalg.lstsq(
PyDataSparseLinalg._dense(A), PyDataSparseLinalg._dense(b), **kwargs
)
@staticmethod
def svd(A, full_matrices=False, k=None, **kwargs):
if not isinstance(A, sp.SparseArray):
U, S, Vt = np.linalg.svd(
np.asarray(A), full_matrices=full_matrices, **kwargs
)
if k is not None:
U = U[:, :k]
S = S[:k]
Vt = Vt[:k, :]
return U, S, Vt
if full_matrices:
raise ValueError("Sparse SVD does not support full_matrices=True.")
A = PyDataSparseLinalg._scipy_sparse(A)
min_dim = min(A.shape)
if min_dim <= 1:
raise ValueError("Sparse SVD requires min(A.shape) > 1.")
if k is None:
k = min_dim - 1
U, S, Vt = spla.svds(A, k=k, **kwargs)
order = np.argsort(S)[::-1]
return (
sp.asarray(U[:, order]),
sp.asarray(S[order]),
sp.asarray(Vt[order, :]),
)
def __getattr__(self, name):
attr = getattr(spla, name)
def wrapped(*args, **kwargs):
args = tuple(
self._scipy_sparse(arg) if hasattr(arg, "ndim") else arg for arg in args
)
kwargs = {
key: self._scipy_sparse(value) if hasattr(value, "ndim") else value
for key, value in kwargs.items()
}
return self._wrap_result(attr(*args, **kwargs))
return wrapped
class PyDataSparseFramework(Framework):
_sparse_first: set[str] = set()
_dtype_attrs = {
"bool",
"float32",
"float64",
"int8",
"int16",
"int32",
"int64",
"uint8",
"uint16",
"uint32",
"uint64",
}
def __init__(self):
self._modules = [sp, compat_np, np]
@staticmethod
def _has_sparse_arg(*args, **kwargs):
return any(isinstance(arg, sp.SparseArray) for arg in args) or any(
isinstance(value, sp.SparseArray) for value in kwargs.values()
)
@staticmethod
def _array_namespace(*arrays):
return array_api_compat.array_namespace(*arrays, use_compat=True)
@staticmethod
def _fill_value_is_zero(array):
fill_value = getattr(array, "fill_value", 0)
return np.all(np.asarray(fill_value) == 0)
@staticmethod
def _dense(array):
if hasattr(array, "todense"):
return np.asarray(array.todense())
if hasattr(array, "toarray"):
return np.asarray(array.toarray())
return np.asarray(array)
@staticmethod
def _sparse_compatible_arg(arg):
if isinstance(arg, np.ndarray):
return sp.asarray(arg)
return arg
def from_binsparse(self, array):
match array:
case DVECVector() | DMATRMatrix() | DMATCMatrix():
return to_numpy(array)
case CustomTensor(shape=(), transpose=None, level=ElementLevel()):
return to_numpy(array)
case CustomTensor(
shape=shape,
transpose=None,
level=DenseLevel(rank=rank, level=ElementLevel()),
) if rank == len(shape):
return to_numpy(array)
case _:
return to_sparse(array)
def to_binsparse(self, array):
if isinstance(array, sp.COO):
if array.ndim == 0 or not self._fill_value_is_zero(array):
return from_numpy(self._dense(array))
return from_sparse(array)
if isinstance(array, sp.SparseArray):
if array.ndim == 0 or not self._fill_value_is_zero(array):
return from_numpy(self._dense(array))
return self.to_binsparse(array.tocoo())
if isinstance(array, np.ndarray):
return from_numpy(array)
if np.isscalar(array):
return from_numpy(np.asarray(array))
raise ValueError("Unsupported array type: " + str(type(array)))
def lazy(self, array):
return array
def compute(self, array):
return array
def einsum(self, prgm, **kwargs):
if all(not isinstance(value, sp.SparseArray) for value in kwargs.values()):
xp = self._array_namespace(*kwargs.values())
return einsum(xp, prgm, **kwargs)
return einsum(sp, prgm, **kwargs)
def diagonal(self, a, *args, **kwargs):
if isinstance(a, sp.SparseArray):
return sp.diagonal(a, *args, **kwargs)
xp = self._array_namespace(a)
return xp.diagonal(a, *args, **kwargs)
def matmul(self, x1, x2, /, **kwargs):
if isinstance(x1, sp.SparseArray) or isinstance(x2, sp.SparseArray):
return x1 @ x2
xp = self._array_namespace(x1, x2)
return xp.matmul(x1, x2, **kwargs)
def zeros(self, shape, *args, **kwargs):
return compat_np.zeros(shape, *args, **kwargs)
def arange(self, *args, **kwargs):
return compat_np.arange(*args, **kwargs)
def asarray(self, obj, *args, **kwargs):
if isinstance(obj, sp.SparseArray):
return sp.asarray(obj, *args, **kwargs)
return compat_np.asarray(obj, *args, **kwargs)
def array(self, obj, *args, **kwargs):
if isinstance(obj, sp.SparseArray):
return sp.asarray(obj, *args, **kwargs)
return np.array(obj, *args, **kwargs)
def eye(self, *args, **kwargs):
return compat_np.eye(*args, **kwargs)
def ones(self, *args, **kwargs):
return compat_np.ones(*args, **kwargs)
def expand_dims(self, a, axis):
if isinstance(a, sp.SparseArray):
return sp.expand_dims(a, axis=axis)
xp = self._array_namespace(a)
return xp.expand_dims(a, axis=axis)
def stack(self, arrays, *, axis=0):
if any(isinstance(array, sp.SparseArray) for array in arrays):
return sp.stack([sp.asarray(array) for array in arrays], axis=axis)
return compat_np.stack(arrays, axis=axis)
def take(self, x, indices, /, *args, **kwargs):
if isinstance(indices, sp.SparseArray):
indices = self._dense(indices)
if isinstance(x, sp.SparseArray):
return sp.take(x, indices, *args, **kwargs)
xp = self._array_namespace(x)
return xp.take(x, indices, *args, **kwargs)
def item(self, array):
if isinstance(array, sp.SparseArray):
return self._dense(array).item()
return array.item()
def with_fill_value(self, array, value):
if isinstance(array, sp.SparseArray):
res = array.copy(deep=False)
res.fill_value = array.dtype.type(value)
return res
return array
@property
def linalg(self):
return PyDataSparseLinalg()
def __getattr__(self, name):
sparse_attr = getattr(sp, name, None)
compat_attr = getattr(compat_np, name, None)
if name in self._dtype_attrs and compat_attr is not None:
return compat_attr
if name in self._sparse_first and sparse_attr is not None:
return sparse_attr
if callable(sparse_attr) and callable(compat_attr):
def wrapped(*args, **kwargs):
if self._has_sparse_arg(*args, **kwargs):
args = tuple(self._sparse_compatible_arg(arg) for arg in args)
kwargs = {
key: self._sparse_compatible_arg(value)
for key, value in kwargs.items()
}
attr = sparse_attr
else:
attr = compat_attr
return attr(*args, **kwargs)
return wrapped
if sparse_attr is not None and compat_attr is not None:
return compat_attr
for attr in (sparse_attr, compat_attr, getattr(np, name, None)):
if attr is not None:
return attr
raise AttributeError(f"'{self.__class__.__name__}' has no attribute '{name}'")
xp = PyDataSparseFramework()