mirror of
https://github.com/fjosw/pyerrors.git
synced 2025-05-14 19:43:41 +02:00
fix: detection of invalid fit functions extended.
This commit is contained in:
parent
338bf8906a
commit
3e29cf9ca8
2 changed files with 36 additions and 5 deletions
|
@ -177,13 +177,15 @@ def total_least_squares(x, y, func, silent=False, **kwargs):
|
||||||
if not callable(func):
|
if not callable(func):
|
||||||
raise TypeError('func has to be a function.')
|
raise TypeError('func has to be a function.')
|
||||||
|
|
||||||
for i in range(25):
|
for i in range(42):
|
||||||
try:
|
try:
|
||||||
func(np.arange(i), x.T[0])
|
func(np.arange(i), x.T[0])
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
continue
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Fit function is not valid.")
|
||||||
|
|
||||||
n_parms = i
|
n_parms = i
|
||||||
if not silent:
|
if not silent:
|
||||||
|
@ -321,9 +323,11 @@ def _prior_fit(x, y, func, priors, silent=False, **kwargs):
|
||||||
try:
|
try:
|
||||||
func(np.arange(i), 0)
|
func(np.arange(i), 0)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
continue
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Fit function is not valid.")
|
||||||
|
|
||||||
n_parms = i
|
n_parms = i
|
||||||
|
|
||||||
|
@ -442,13 +446,15 @@ def _standard_fit(x, y, func, silent=False, **kwargs):
|
||||||
if not callable(func):
|
if not callable(func):
|
||||||
raise TypeError('func has to be a function.')
|
raise TypeError('func has to be a function.')
|
||||||
|
|
||||||
for i in range(25):
|
for i in range(42):
|
||||||
try:
|
try:
|
||||||
func(np.arange(i), x.T[0])
|
func(np.arange(i), x.T[0])
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
continue
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
raise RuntimeError("Fit function is not valid.")
|
||||||
|
|
||||||
n_parms = i
|
n_parms = i
|
||||||
|
|
||||||
|
|
|
@ -495,6 +495,31 @@ def test_fit_no_autograd():
|
||||||
pe.total_least_squares(oy, oy, func)
|
pe.total_least_squares(oy, oy, func)
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_fit_function():
|
||||||
|
def func1(a, x):
|
||||||
|
return a[0] + a[1] * x + a[2] * anp.sinh(x) + a[199]
|
||||||
|
|
||||||
|
def func2(a, x, y):
|
||||||
|
return a[0] + a[1] * x
|
||||||
|
|
||||||
|
def func3(x):
|
||||||
|
return x
|
||||||
|
|
||||||
|
xvals =[]
|
||||||
|
yvals =[]
|
||||||
|
err = 0.1
|
||||||
|
|
||||||
|
for x in range(1, 8, 2):
|
||||||
|
xvals.append(x)
|
||||||
|
yvals.append(pe.pseudo_Obs(x + np.random.normal(0.0, err), err, 'test1') + pe.pseudo_Obs(0, err / 100, 'test2', samples=87))
|
||||||
|
[o.gamma_method() for o in yvals]
|
||||||
|
for func in [func1, func2, func3]:
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
pe.least_squares(xvals, yvals, func)
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
pe.total_least_squares(yvals, yvals, func)
|
||||||
|
|
||||||
|
|
||||||
def test_singular_correlated_fit():
|
def test_singular_correlated_fit():
|
||||||
obs1 = pe.pseudo_Obs(1.0, 0.1, 'test')
|
obs1 = pe.pseudo_Obs(1.0, 0.1, 'test')
|
||||||
with pytest.raises(Exception):
|
with pytest.raises(Exception):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue