fix: flak8 & pytest

This commit is contained in:
ppetrak 2022-12-19 14:03:45 +01:00
parent 500c5234cf
commit 3d6ec7b397
3 changed files with 54 additions and 59 deletions

BIN
examples/my_db.sqlite Normal file

Binary file not shown.

View file

@ -457,7 +457,6 @@ from .obs import *
from .correlators import *
from .fits import *
from .misc import *
from . import combined_fits
from . import dirac
from . import input
from . import linalg

View file

@ -70,7 +70,6 @@ class Fit_result(Sequence):
def least_squares(x, y, func, priors=None, silent=False, **kwargs):
r'''Performs a non-linear fit to y = func(x).
```
Parameters
@ -161,7 +160,7 @@ def least_squares(x, y, func, priors=None, silent=False, **kwargs):
if priors is not None:
return _prior_fit(x, y, func, priors, silent=silent, **kwargs)
elif (type(x)==dict and type(y)==dict and type(func)==dict):
elif (type(x) == dict and type(y) == dict and type(func) == dict):
return _combined_fit(x, y, func, silent=silent, **kwargs)
else:
@ -688,7 +687,8 @@ def _standard_fit(x, y, func, silent=False, **kwargs):
return output
def _combined_fit(x,y,func,silent=False,**kwargs):
def _combined_fit(x, y, func, silent=False, **kwargs):
if kwargs.get('correlated_fit') is True:
raise Exception("Correlated fit has not been implemented yet")
@ -706,8 +706,8 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
x_all = []
y_all = []
for key in x.keys():
x_all+=x[key]
y_all+=y[key]
x_all += x[key]
y_all += y[key]
x_all = np.asarray(x_all)
@ -718,9 +718,9 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
n_parms_ls = []
for key in func.keys():
if not callable(func[key]):
raise TypeError('func (key='+ key + ') is not a function.')
raise TypeError('func (key=' + key + ') is not a function.')
if len(x[key]) != len(y[key]):
raise Exception('x and y input (key='+ key + ') do not have the same length')
raise Exception('x and y input (key=' + key + ') do not have the same length')
for i in range(42):
try:
func[key](np.arange(i), x_all.T[0])
@ -731,7 +731,7 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
else:
break
else:
raise RuntimeError("Fit function (key="+ key + ") is not valid.")
raise RuntimeError("Fit function (key=" + key + ") is not valid.")
n_parms = i
n_parms_ls.append(n_parms)
n_parms = max(n_parms_ls)
@ -753,12 +753,12 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
chisq = 0.0
for key in func.keys():
x_array = np.asarray(x[key])
model = anp.array(func[key](p,x_array))
model = anp.array(func[key](p, x_array))
y_obs = y[key]
y_f = [o.value for o in y_obs]
dy_f = [o.dvalue for o in y_obs]
C_inv = np.diag(np.diag(np.ones((len(x_array),len(x_array)))))/dy_f/dy_f
chisq += anp.sum((y_f - model)@ C_inv @(y_f - model))
C_inv = np.diag(np.diag(np.ones((len(x_array), len(x_array))))) / dy_f / dy_f
chisq += anp.sum((y_f - model) @ C_inv @ (y_f - model))
return chisq
if output.method == 'migrad':
@ -783,15 +783,15 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
if x_all.shape[-1] - n_parms > 0:
output.chisquare = chisqfunc(fit_result.x)
output.dof = x_all.shape[-1] - n_parms
output.chisquare_by_dof = output.chisquare/output.dof
output.chisquare_by_dof = output.chisquare / output.dof
output.p_value = 1 - scipy.stats.chi2.cdf(output.chisquare, output.dof)
else:
output.chisquare_by_dof = float('nan')
if not silent:
print(fit_result.message)
print('chisquare/d.o.f.:', output.chisquare_by_dof )
print('fit parameters',fit_result.x)
print('chisquare/d.o.f.:', output.chisquare_by_dof)
print('fit parameters', fit_result.x)
def chisqfunc_compact(d):
chisq = 0.0
@ -800,14 +800,13 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
c2 = 0
for key in func.keys():
x_array = np.asarray(x[key])
c2+=len(x_array)
model = anp.array(func[key](d[:n_parms],x_array))
c2 += len(x_array)
model = anp.array(func[key](d[:n_parms], x_array))
y_obs = y[key]
y_f = [o.value for o in y_obs]
dy_f = [o.dvalue for o in y_obs]
C_inv = np.diag(np.diag(np.ones((len(x_array),len(x_array)))))/dy_f/dy_f
list_tmp.append(anp.sum((d[n_parms+c1:n_parms+c2]- model)@ C_inv @(d[n_parms+c1:n_parms+c2]- model)))
c1+=len(x_array)
C_inv = np.diag(np.diag(np.ones((len(x_array), len(x_array))))) / dy_f / dy_f
list_tmp.append(anp.sum((d[n_parms + c1:n_parms + c2] - model) @ C_inv @ (d[n_parms + c1:n_parms + c2] - model)))
c1 += len(x_array)
chisq = anp.sum(list_tmp)
return chisq
@ -815,7 +814,7 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
hat_vector = []
for key in func.keys():
x_array = np.asarray(x[key])
if (len(x_array)!= 0):
if (len(x_array) != 0):
hat_vector.append(jacobian(func[key])(fit_result.x, x_array))
hat_vector = [item for sublist in hat_vector for item in sublist]
return hat_vector
@ -840,20 +839,18 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
except np.linalg.LinAlgError:
raise Exception("Cannot invert hessian matrix.")
if kwargs.get('expected_chisquare') is True:
if kwargs.get('correlated_fit') is not True:
W = np.diag(1 / np.asarray(dy_f))
cov = covariance(y_all)
hat_vector = prepare_hat_matrix()
A = W @ hat_vector #hat_vector = 'jacobian(func)(fit_result.x, x)'
A = W @ hat_vector # hat_vector = 'jacobian(func)(fit_result.x, x)'
P_phi = A @ np.linalg.pinv(A.T @ A) @ A.T
expected_chisquare = np.trace((np.identity(x_all.shape[-1]) - P_phi) @ W @ cov @ W)
output.chisquare_by_expected_chisquare = chisquare / expected_chisquare
if not silent:
print('chisquare/expected_chisquare:', output.chisquare_by_expected_chisquare)
result = []
for i in range(n_parms):
result.append(derived_observable(lambda x_all, **kwargs: (x_all[0] + np.finfo(np.float64).eps) / (y_all[0].value + np.finfo(np.float64).eps) * fitp[i], list(y_all), man_grad=list(deriv_y[i])))
@ -863,7 +860,6 @@ def _combined_fit(x,y,func,silent=False,**kwargs):
return output
def fit_lin(x, y, **kwargs):
"""Performs a linear fit to y = n + m * x and returns two Obs n, m.