Skip to content

Commit d1b2478

Browse files
committed
improve rng; tighthen rtol for scipy
1 parent 60fd30a commit d1b2478

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

tests/test_odr_fit.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
from odrpack import OdrStop, odr_fit
1010
from odrpack.__odrpack import loc_rwork
1111

12-
SEED = 1234567890
12+
RNG = np.random.default_rng(seed=1234567890)
1313

1414

15-
def add_noise(array, noise, seed=SEED):
15+
def add_noise(array, noise):
1616
"""Adds random noise to an array."""
17-
rng = np.random.default_rng(seed)
18-
return array*(1 + noise*rng.uniform(-1, 1, size=array.shape))
17+
return array*(1 + noise*RNG.uniform(-1, 1, size=array.shape))
1918

2019

2120
def flipargs(f):
@@ -318,20 +317,20 @@ def test_delta0_related(case1, case3):
318317
def test_weight_x(case1, case3):
319318

320319
# weight_x scalar
321-
sol = odr_fit(**case1, weight_x=1e10)
320+
sol = odr_fit(**case1, weight_x=1e100)
322321
assert np.allclose(sol.delta, np.zeros_like(sol.delta))
323322

324323
# weight_x (n,) and m=1
325324
weight_x = np.ones_like(case1['xdata'])
326325
fix = (4, 7)
327-
weight_x[fix,] = 1e10
326+
weight_x[fix,] = 1e100
328327
sol = odr_fit(**case1, weight_x=weight_x)
329328
assert np.allclose(sol.delta[fix,], np.zeros_like(sol.delta[fix,]))
330329

331330
# weight_x (m, n)
332331
weight_x = np.ones_like(case3['xdata'])
333332
fix = (4, 13)
334-
weight_x[:, fix,] = 1e10
333+
weight_x[:, fix,] = 1e100
335334
sol = odr_fit(**case3, weight_x=weight_x)
336335
sol1 = deepcopy(sol)
337336
assert np.allclose(sol.delta[:, fix,], np.zeros((sol.delta.shape[0], len(fix))))
@@ -345,7 +344,7 @@ def test_weight_x(case1, case3):
345344
# weight_x (m,)
346345
weight_x = np.ones(case3['xdata'].shape[0])
347346
fix = (1,)
348-
weight_x[fix,] = 1e10
347+
weight_x[fix,] = 1e100
349348
sol = odr_fit(**case3, weight_x=weight_x)
350349
sol1 = deepcopy(sol)
351350
assert np.allclose(sol.delta[fix, :], np.zeros((len(fix), sol.delta.shape[-1])))
@@ -361,7 +360,7 @@ def test_weight_x(case1, case3):
361360
weight_x = np.zeros((m, m))
362361
np.fill_diagonal(weight_x, 1.)
363362
fix = (1,)
364-
weight_x[fix, fix] = 1e10
363+
weight_x[fix, fix] = 1e100
365364
sol = odr_fit(**case3, weight_x=weight_x)
366365
sol1 = deepcopy(sol)
367366
assert np.allclose(sol.delta[fix, :], np.zeros((len(fix), sol.delta.shape[-1])))
@@ -393,20 +392,20 @@ def test_weight_y(case1, case3):
393392
ATOL = 1e-6
394393

395394
# weight_y scalar
396-
sol = odr_fit(**case1, weight_y=1e10)
395+
sol = odr_fit(**case1, weight_y=1e100)
397396
assert np.allclose(sol.eps, np.zeros_like(sol.eps))
398397

399398
# weight_y (n,) and q==1
400399
weight_y = np.ones_like(case1['ydata'])
401400
fix = (4, 7)
402-
weight_y[fix,] = 1e10
401+
weight_y[fix,] = 1e100
403402
sol = odr_fit(**case1, weight_y=weight_y)
404403
assert np.allclose(sol.eps[fix,], np.zeros_like(sol.eps[fix,]), atol=ATOL)
405404

406405
# weight_y (q, n)
407406
weight_y = np.ones_like(case3['ydata'])
408407
fix = (4, 13)
409-
weight_y[:, fix,] = 1e10
408+
weight_y[:, fix,] = 1e100
410409
sol = odr_fit(**case3, weight_y=weight_y)
411410
sol1 = deepcopy(sol)
412411
assert np.allclose(sol.eps[:, fix,], np.zeros((sol.eps.shape[0], len(fix))),
@@ -421,7 +420,7 @@ def test_weight_y(case1, case3):
421420
# weight_y (q,)
422421
weight_y = np.ones(case3['ydata'].shape[0])
423422
fix = (1,)
424-
weight_y[fix,] = 1e10
423+
weight_y[fix,] = 1e100
425424
sol = odr_fit(**case3, weight_y=weight_y)
426425
sol1 = deepcopy(sol)
427426
assert np.allclose(sol.eps[fix, :], np.zeros((len(fix), sol.eps.shape[-1])),
@@ -438,7 +437,7 @@ def test_weight_y(case1, case3):
438437
weight_y = np.zeros((q, q))
439438
np.fill_diagonal(weight_y, 1.)
440439
fix = (1,)
441-
weight_y[fix, fix] = 1e10
440+
weight_y[fix, fix] = 1e100
442441
sol = odr_fit(**case3, weight_y=weight_y)
443442
sol1 = deepcopy(sol)
444443
assert np.allclose(sol.eps[fix, :], np.zeros((len(fix), sol.eps.shape[-1])),
@@ -627,19 +626,17 @@ def test_compare_scipy(case1, case2, case3, example2):
627626
sol1 = odr_fit(**case1, task='OLS')
628627
sol2 = curve_fit(lambda x, *b: case1['f'](x, np.array(b)),
629628
case1['xdata'], case1['ydata'], case1['beta0'])
630-
assert np.allclose(sol1.beta, sol2[0])
629+
assert np.allclose(sol1.beta, sol2[0], rtol=1e-4)
631630

632631
# case1,2,3 // scipy.odr.odr
633-
for case in [case3]:
632+
for case in [case1, case2, case3]:
634633
for subcase in range(2):
635634
if subcase == 0:
636-
wd = np.random.uniform(0.1, 1.0)
637-
we = np.random.uniform(0.1, 1.0)
638-
rtol = 1e-4
635+
wd = RNG.uniform(0.1, 1.0)
636+
we = RNG.uniform(0.1, 1.0)
639637
else:
640-
wd = np.random.rand(*case['xdata'].shape)
641-
we = np.random.rand(*case['ydata'].shape)
642-
rtol = 1e-3 # very tough problem!
638+
wd = RNG.uniform(0.1, 1.0, size=case['xdata'].shape)
639+
we = RNG.uniform(0.1, 1.0, size=case['ydata'].shape)
643640

644641
sol1 = odr_fit(**case, weight_x=wd, weight_y=we)
645642
sol2 = odrscipy(flipargs(case['f']),
@@ -649,7 +646,7 @@ def test_compare_scipy(case1, case2, case3, example2):
649646
assert np.allclose(sol1.beta, sol2[0], rtol=1e-5)
650647

651648
assert np.all(np.max(we*abs(sol1.eps - sol2[3]['eps']), -1) /
652-
(np.max(case['ydata'], -1) - np.min(case['ydata'], -1)) < rtol)
649+
(np.max(case['ydata'], -1) - np.min(case['ydata'], -1)) < 1e-7)
653650

654651
assert np.all(np.max(wd*abs(sol1.delta - sol2[3]['delta']), -1) /
655-
(np.max(case['xdata'], -1) - np.min(case['xdata'], -1)) < rtol)
652+
(np.max(case['xdata'], -1) - np.min(case['xdata'], -1)) < 1e-5)

0 commit comments

Comments
 (0)