forked from theislab/cellrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_linear_solver.py
323 lines (258 loc) · 9.09 KB
/
test_linear_solver.py
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
315
316
317
318
319
320
321
322
323
import pytest
from cellrank._utils._linear_solver import (
_solve_lin_system,
_petsc_direct_solve,
_create_petsc_matrix,
)
import numpy as np
from scipy.sparse import eye as speye
from scipy.sparse import random, csr_matrix
def _petsc_not_installed() -> bool:
try:
import petsc4py
import slepc4py
return False
except ImportError:
return True
petsc_slepc_skip = pytest.mark.skipif(
_petsc_not_installed(), reason="PETSc or SLEPc is not installed."
)
def _create_a_b_matrices(seed: int, sparse: bool):
np.random.seed(seed)
if sparse:
A = random(
20, 20, density=0.8, random_state=np.random.randint(0, 100), format="csr"
)
B = random(
20, 10, density=1, random_state=np.random.randint(0, 100), format="csr"
)
else:
A = np.random.normal(size=(20, 20))
B = np.random.normal(size=(20, 10))
return A, B
class TestScipyLinearSolver:
def test_invalid_solver(self):
np.random.seed(42)
A = np.random.normal(size=(20, 10))
B = np.random.normal(size=(20, 10))
with pytest.raises(ValueError):
_solve_lin_system(A, B, solver="foobar", use_petsc=False)
@pytest.mark.parametrize("seed,sparse", zip(range(10), [False] * 5 + [True] * 5))
def test_gmres(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
sol = _solve_lin_system(
A,
B,
solver="gmres",
use_petsc=False,
use_eye=False,
show_progress_bar=False,
tol=1e-6,
)
assert sol.ndim == 2
if sparse:
A = A.A
B = B.A
np.testing.assert_allclose(A @ sol, B, rtol=1e-6, atol=1e-10)
@pytest.mark.parametrize(
"seed,sparse", zip(range(10, 20), [False] * 5 + [True] * 5)
)
def test_direct_solver_dense(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
sol = _solve_lin_system(
A,
B,
solver="direct",
use_petsc=False,
use_eye=False,
show_progress_bar=False,
tol=1e-6,
)
assert sol.ndim == 2
if sparse:
A = A.A
B = B.A
np.testing.assert_allclose(A @ sol, B, rtol=1e-6, atol=1e-10)
@pytest.mark.parametrize(
"seed,sparse", zip(range(30, 40), [False] * 5 + [True] * 5)
)
def test_eye(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
sol = _solve_lin_system(
A,
B,
solver="gmres",
use_petsc=False,
use_eye=True,
show_progress_bar=False,
tol=1e-6,
)
assert sol.ndim == 2
if sparse:
A = A.A
B = B.A
A = np.eye(20, 20) - A
np.testing.assert_allclose(A @ sol, B, rtol=1e-6, atol=1e-10)
@petsc_slepc_skip
class TestLinearSolverPETSc:
def test_create_petsc_matrix_no_a_matrix(self):
with pytest.raises(TypeError):
_create_petsc_matrix(np.empty((100,)))
def test_create_petsc_matrix_from_dense(self):
x = np.random.normal(size=(10, 2))
res = _create_petsc_matrix(x)
assert res.assembled
assert res.getType() == "seqdense"
np.testing.assert_array_equal(res.getDenseArray(), x)
def test_create_petsc_matrix_from_sparse_as_dense(self):
x = random(100, 10, format="csr", density=0.1)
res = _create_petsc_matrix(x, as_dense=True)
assert res.assembled
assert res.getType() == "seqdense"
np.testing.assert_array_equal(res.getDenseArray(), x.A)
def test_create_petsc_matrix_from_sparse_as_not_dense(self):
x = random(100, 10, format="csr", density=0.1)
res = _create_petsc_matrix(x, as_dense=False)
assert res.assembled
assert res.getType() == "seqaij"
np.testing.assert_array_equal(csr_matrix(res.getValuesCSR()[::-1]).A, x.A)
def test_create_petsc_matrix_from_sparse_not_csr(self):
x = random(100, 10, format="coo", density=0.1)
res = _create_petsc_matrix(x, as_dense=False)
assert res.assembled
assert res.getType() == "seqaij"
np.testing.assert_array_equal(csr_matrix(res.getValuesCSR()[::-1]).A, x.A)
def test_create_solver_invalid_solver(self):
from petsc4py.PETSc import Error
A = np.random.normal(size=(20, 20))
B = np.random.normal(size=(20, 10))
with pytest.raises(Error):
_solve_lin_system(A, B, solver="foobar", use_petsc=True)
def test_create_solver_invalid_preconditioner(self):
from petsc4py.PETSc import Error
A = np.random.normal(size=(20, 20))
B = np.random.normal(size=(20, 10))
with pytest.raises(Error):
_solve_lin_system(A, B, preconditioner="foobar", use_petsc=True)
def test_solve_invalid_dimension(self):
from petsc4py.PETSc import Error
A = np.random.normal(size=(20, 10))
B = np.random.normal(size=(20, 10))
with pytest.raises(Error):
_solve_lin_system(A, B, use_petsc=True)
@pytest.mark.parametrize(
"seed, solver, sparse",
zip(
range(42, 62),
["direct"] * 5 + ["gmres"] * 5 + ["direct"] * 5 + ["gmres"] * 5,
[False] * 10 + [True] * 10,
),
)
def test_petsc_scipy_matches(self, seed: int, solver: str, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
sol_petsc = _solve_lin_system(
A,
B,
solver=solver,
use_petsc=True,
use_eye=True,
show_progress_bar=False,
preconditioner="lu",
tol=1e-6,
)
sol_scipy = _solve_lin_system(
A,
B,
solver=solver,
use_petsc=False,
use_eye=True,
show_progress_bar=False,
tol=1e-6,
)
assert sol_petsc.ndim == 2
assert sol_scipy.ndim == 2
if sparse:
A = A.A
B = B.A
A = np.eye(A.shape[0]) - A
np.testing.assert_allclose(A @ sol_scipy, B, rtol=1e-6, atol=1e-8)
np.testing.assert_allclose(A @ sol_petsc, B, rtol=1e-6, atol=1e-8)
np.testing.assert_allclose(sol_petsc, sol_scipy, rtol=1e-6, atol=1e-8)
@pytest.mark.parametrize("seed,sparse", zip(range(10), [False] * 5 + [True] * 5))
def test_gmres(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
sol = _solve_lin_system(
A,
B,
solver="gmres",
use_petsc=True,
use_eye=sparse,
show_progress_bar=False,
preconditioner="lu",
tol=1e-6,
)
assert sol.ndim == 2
if sparse:
A = A.A
A = np.eye(A.shape[0]) - A
B = B.A
np.testing.assert_allclose(A @ sol, B, rtol=1e-6, atol=1e-8)
@pytest.mark.parametrize(
"seed,sparse", zip(range(10, 20), [False] * 5 + [True] * 5)
)
def test_direct_solver(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
sol = _solve_lin_system(
A,
B,
solver="direct",
use_petsc=True,
use_eye=True,
show_progress_bar=False,
tol=1e-6,
)
assert sol.ndim == 2
if sparse:
A = A.A
B = B.A
A = np.eye(A.shape[0]) - A
np.testing.assert_allclose(A @ sol, B, rtol=1e-6, atol=1e-10)
@pytest.mark.parametrize(
"seed,sparse", zip(range(10, 20), [False] * 5 + [True] * 5)
)
def test_mat_solve(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
A = (speye(*A.shape) if sparse else np.eye(*A.shape)) - A
X = _petsc_direct_solve(A, B, tol=1e-8)
assert X.ndim == 2
if sparse:
A = A.A
B = B.A
np.testing.assert_allclose(A @ X, B, rtol=1e-6)
@pytest.mark.parametrize(
"seed,sparse", zip(range(10, 20), [False] * 5 + [True] * 5)
)
def test_mat_solve_1_dim_b(self, seed: int, sparse: bool):
A, B = _create_a_b_matrices(seed, sparse)
A = (speye(*A.shape) if sparse else np.eye(*A.shape)) - A
B = B[:, 0]
X = _petsc_direct_solve(A, B, tol=1e-8)
assert X.ndim == 2
assert X.shape[1] == 1
if sparse:
A = A.A
B = B.A
np.testing.assert_allclose(
((A @ X).squeeze()), B.squeeze(), rtol=1e-6, atol=1e-6
)
@pytest.mark.parametrize(
"seed,sparse", zip(range(10, 20), [False] * 5 + [True] * 5)
)
def test_mat_solve_inversion(self, seed: int, sparse: bool):
A, _ = _create_a_b_matrices(seed, sparse)
A = (speye(*A.shape) if sparse else np.eye(*A.shape)) - A
X = _petsc_direct_solve(A, tol=1e-8)
assert X.ndim == 2
if sparse:
A = A.A
np.testing.assert_allclose(A @ X, np.eye(*A.shape), rtol=1e-6, atol=1e-6)