Skip to content

Commit 0ee25a0

Browse files
authored
Merge pull request #678 from mrava87/test-preallocatesolver
test: added tests to solver with preallocate=True
2 parents 1db6169 + 94abd92 commit 0ee25a0

3 files changed

Lines changed: 292 additions & 222 deletions

File tree

pytests/test_leastsquares.py

Lines changed: 135 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -101,57 +101,58 @@ def test_NormalEquationsInversion(par):
101101
)
102102
y = Gop * x
103103

104-
# normal equations with regularization
105-
xinv = normal_equations_inversion(
106-
Gop,
107-
y,
108-
[Reg],
109-
epsI=1e-5,
110-
epsRs=[1e-8],
111-
x0=x0,
112-
engine="pylops",
113-
**dict(niter=200, tol=1e-10)
114-
)[0]
115-
assert_array_almost_equal(x, xinv, decimal=3)
116-
# normal equations with weight
117-
xinv = normal_equations_inversion(
118-
Gop,
119-
y,
120-
None,
121-
Weight=Weigth,
122-
epsI=1e-5,
123-
x0=x0,
124-
engine="pylops",
125-
**dict(niter=200, tol=1e-10)
126-
)[0]
127-
assert_array_almost_equal(x, xinv, decimal=3)
128-
# normal equations with weight and small regularization
129-
xinv = normal_equations_inversion(
130-
Gop,
131-
y,
132-
[Reg],
133-
Weight=Weigth,
134-
epsI=1e-5,
135-
epsRs=[1e-8],
136-
x0=x0,
137-
engine="pylops",
138-
**dict(niter=200, tol=1e-10)
139-
)[0]
140-
assert_array_almost_equal(x, xinv, decimal=3)
141-
# normal equations with weight and small normal regularization
142-
xinv = normal_equations_inversion(
143-
Gop,
144-
y,
145-
[],
146-
NRegs=[NReg],
147-
Weight=Weigth,
148-
epsI=1e-5,
149-
epsNRs=[1e-8],
150-
x0=x0,
151-
engine="pylops",
152-
**dict(niter=200, tol=1e-10)
153-
)[0]
154-
assert_array_almost_equal(x, xinv, decimal=3)
104+
for preallocate in [False, True]:
105+
# normal equations with regularization
106+
xinv = normal_equations_inversion(
107+
Gop,
108+
y,
109+
[Reg],
110+
epsI=1e-5,
111+
epsRs=[1e-8],
112+
x0=x0,
113+
engine="pylops",
114+
**dict(niter=200, tol=1e-10, preallocate=preallocate)
115+
)[0]
116+
assert_array_almost_equal(x, xinv, decimal=3)
117+
# normal equations with weight
118+
xinv = normal_equations_inversion(
119+
Gop,
120+
y,
121+
None,
122+
Weight=Weigth,
123+
epsI=1e-5,
124+
x0=x0,
125+
engine="pylops",
126+
**dict(niter=200, tol=1e-10, preallocate=preallocate)
127+
)[0]
128+
assert_array_almost_equal(x, xinv, decimal=3)
129+
# normal equations with weight and small regularization
130+
xinv = normal_equations_inversion(
131+
Gop,
132+
y,
133+
[Reg],
134+
Weight=Weigth,
135+
epsI=1e-5,
136+
epsRs=[1e-8],
137+
x0=x0,
138+
engine="pylops",
139+
**dict(niter=200, tol=1e-10, preallocate=preallocate)
140+
)[0]
141+
assert_array_almost_equal(x, xinv, decimal=3)
142+
# normal equations with weight and small normal regularization
143+
xinv = normal_equations_inversion(
144+
Gop,
145+
y,
146+
[],
147+
NRegs=[NReg],
148+
Weight=Weigth,
149+
epsI=1e-5,
150+
epsNRs=[1e-8],
151+
x0=x0,
152+
engine="pylops",
153+
**dict(niter=200, tol=1e-10, preallocate=preallocate)
154+
)[0]
155+
assert_array_almost_equal(x, xinv, decimal=3)
155156

156157

157158
@pytest.mark.parametrize(
@@ -175,40 +176,41 @@ def test_RegularizedInversion(par):
175176
)
176177
y = Gop * x
177178

178-
# regularized inversion with regularization
179-
xinv = regularized_inversion(
180-
Gop,
181-
y,
182-
[Reg],
183-
epsRs=[1e-8],
184-
x0=x0,
185-
engine="pylops",
186-
**dict(damp=0, niter=200, show=0)
187-
)[0]
188-
assert_array_almost_equal(x, xinv, decimal=3)
189-
# regularized inversion with weight
190-
xinv = regularized_inversion(
191-
Gop,
192-
y,
193-
None,
194-
Weight=Weigth,
195-
x0=x0,
196-
engine="pylops",
197-
**dict(damp=0, niter=200, show=0)
198-
)[0]
199-
assert_array_almost_equal(x, xinv, decimal=3)
200-
# regularized inversion with regularization
201-
xinv = regularized_inversion(
202-
Gop,
203-
y,
204-
[Reg],
205-
Weight=Weigth,
206-
epsRs=[1e-8],
207-
x0=x0,
208-
engine="pylops",
209-
**dict(damp=0, niter=200, show=0)
210-
)[0]
211-
assert_array_almost_equal(x, xinv, decimal=3)
179+
for preallocate in [False, True]:
180+
# regularized inversion with regularization
181+
xinv = regularized_inversion(
182+
Gop,
183+
y,
184+
[Reg],
185+
epsRs=[1e-8],
186+
x0=x0,
187+
engine="pylops",
188+
**dict(damp=0, niter=200, preallocate=preallocate)
189+
)[0]
190+
assert_array_almost_equal(x, xinv, decimal=3)
191+
# regularized inversion with weight
192+
xinv = regularized_inversion(
193+
Gop,
194+
y,
195+
None,
196+
Weight=Weigth,
197+
x0=x0,
198+
engine="pylops",
199+
**dict(damp=0, niter=200, preallocate=preallocate)
200+
)[0]
201+
assert_array_almost_equal(x, xinv, decimal=3)
202+
# regularized inversion with regularization
203+
xinv = regularized_inversion(
204+
Gop,
205+
y,
206+
[Reg],
207+
Weight=Weigth,
208+
epsRs=[1e-8],
209+
x0=x0,
210+
engine="pylops",
211+
**dict(damp=0, niter=200, preallocate=preallocate)
212+
)[0]
213+
assert_array_almost_equal(x, xinv, decimal=3)
212214

213215

214216
@pytest.mark.parametrize(
@@ -230,13 +232,24 @@ def test_WeightedInversion(par):
230232
x = np.ones(par["nx"]) + par["imag"] * np.ones(par["nx"])
231233
y = Gop * x
232234

233-
xne = normal_equations_inversion(
234-
Gop, y, None, Weight=Weigth, engine="pylops", **dict(niter=5, tol=1e-10)
235-
)[0]
236-
xreg = regularized_inversion(
237-
Gop, y, None, Weight=Weigth1, engine="pylops", **dict(damp=0, niter=5, show=0)
238-
)[0]
239-
assert_array_almost_equal(xne, xreg, decimal=3)
235+
for preallocate in [False, True]:
236+
xne = normal_equations_inversion(
237+
Gop,
238+
y,
239+
None,
240+
Weight=Weigth,
241+
engine="pylops",
242+
**dict(niter=5, tol=1e-10, preallocate=preallocate)
243+
)[0]
244+
xreg = regularized_inversion(
245+
Gop,
246+
y,
247+
None,
248+
Weight=Weigth1,
249+
engine="pylops",
250+
**dict(damp=0, niter=5, preallocate=preallocate)
251+
)[0]
252+
assert_array_almost_equal(xne, xreg, decimal=3)
240253

241254

242255
@pytest.mark.parametrize(
@@ -260,10 +273,17 @@ def test_PreconditionedInversion(par):
260273
else None
261274
)
262275
y = Gop * x
263-
xinv = preconditioned_inversion(
264-
Gop, y, Pre, x0=x0, engine="pylops", **dict(damp=0, niter=800, show=0)
265-
)[0]
266-
assert_array_almost_equal(x, xinv, decimal=2)
276+
277+
for preallocate in [False, True]:
278+
xinv = preconditioned_inversion(
279+
Gop,
280+
y,
281+
Pre,
282+
x0=x0,
283+
engine="pylops",
284+
**dict(damp=0, niter=800, preallocate=preallocate)
285+
)[0]
286+
assert_array_almost_equal(x, xinv, decimal=2)
267287

268288

269289
@pytest.mark.parametrize("par", [(par1)])
@@ -279,8 +299,23 @@ def test_skinnyregularization(par):
279299
x = np.arange(par["nx"] - 1)
280300
y = Dop * x
281301

282-
xinv = normal_equations_inversion(Dop, y, [Regop], epsRs=[1e-4], engine="pylops")[0]
283-
assert_array_almost_equal(x, xinv, decimal=2)
302+
for preallocate in [False, True]:
303+
xinv = normal_equations_inversion(
304+
Dop,
305+
y,
306+
[Regop],
307+
epsRs=[1e-4],
308+
engine="pylops",
309+
**dict(preallocate=preallocate)
310+
)[0]
311+
assert_array_almost_equal(x, xinv, decimal=2)
284312

285-
xinv = regularized_inversion(Dop, y, [Regop], epsRs=[1e-4], engine="pylops")[0]
286-
assert_array_almost_equal(x, xinv, decimal=2)
313+
xinv = regularized_inversion(
314+
Dop,
315+
y,
316+
[Regop],
317+
epsRs=[1e-4],
318+
engine="pylops",
319+
**dict(preallocate=preallocate)
320+
)[0]
321+
assert_array_almost_equal(x, xinv, decimal=2)

pytests/test_solver.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,10 @@ def test_cg(par):
9696
x0 = None
9797

9898
y = Aop * x
99-
xinv = cg(Aop, y, x0=x0, niter=par["nx"], tol=1e-5, show=True)[0]
100-
assert_array_almost_equal(x, xinv, decimal=4)
99+
100+
for preallocate in [False, True]:
101+
xinv = cg(Aop, y, x0=x0, niter=par["nx"], tol=1e-5, preallocate=preallocate)[0]
102+
assert_array_almost_equal(x, xinv, decimal=4)
101103

102104

103105
@pytest.mark.parametrize(
@@ -124,9 +126,11 @@ def test_cg_ndarray(par):
124126
x0 = None
125127

126128
y = Aop * x
127-
xinv = cg(Aop, y, x0=x0, niter=2 * x.size, tol=0, show=True)[0]
128-
assert xinv.shape == x.shape
129-
assert_array_almost_equal(x, xinv, decimal=4)
129+
130+
for preallocate in [False, True]:
131+
xinv = cg(Aop, y, x0=x0, niter=2 * x.size, tol=0, preallocate=preallocate)[0]
132+
assert xinv.shape == x.shape
133+
assert_array_almost_equal(x, xinv, decimal=4)
130134

131135

132136
@pytest.mark.parametrize(
@@ -153,9 +157,11 @@ def test_cg_forceflat(par):
153157
x0 = None
154158

155159
y = Aop * x
156-
xinv = cg(Aop, y, x0=x0, niter=2 * x.size, tol=0, show=True)[0]
157-
assert xinv.shape == x.ravel().shape
158-
assert_array_almost_equal(x.ravel(), xinv, decimal=4)
160+
161+
for preallocate in [False, True]:
162+
xinv = cg(Aop, y, x0=x0, niter=2 * x.size, tol=0, preallocate=preallocate)[0]
163+
assert xinv.shape == x.ravel().shape
164+
assert_array_almost_equal(x.ravel(), xinv, decimal=4)
159165

160166

161167
@pytest.mark.parametrize(
@@ -179,8 +185,12 @@ def test_cgls(par):
179185
x0 = None
180186

181187
y = Aop * x
182-
xinv = cgls(Aop, y, x0=x0, niter=par["nx"], tol=1e-5, show=True)[0]
183-
assert_array_almost_equal(x, xinv, decimal=4)
188+
189+
for preallocate in [False, True]:
190+
xinv = cgls(Aop, y, x0=x0, niter=par["nx"], tol=1e-5, preallocate=preallocate)[
191+
0
192+
]
193+
assert_array_almost_equal(x, xinv, decimal=4)
184194

185195

186196
@pytest.mark.skipif(
@@ -210,10 +220,13 @@ def test_lsqr(par):
210220
y_sp = y - Aop * x0
211221
else:
212222
y_sp = y.copy()
213-
xinv = lsqr(Aop, y, x0, niter=par["nx"])[0]
223+
214224
xinv_sp = sp_lsqr(Aop, y_sp, iter_lim=par["nx"])[0]
215225
if par["x0"]:
216226
xinv_sp += x0
217227

218-
assert_array_almost_equal(xinv, x, decimal=4)
219-
assert_array_almost_equal(xinv_sp, x, decimal=4)
228+
for preallocate in [False, True]:
229+
xinv = lsqr(Aop, y, x0, niter=par["nx"], preallocate=preallocate)[0]
230+
231+
assert_array_almost_equal(xinv, x, decimal=4)
232+
assert_array_almost_equal(xinv_sp, x, decimal=4)

0 commit comments

Comments
 (0)