multiple single fits working

This commit is contained in:
Dominik Demuth 2023-08-15 18:44:08 +02:00
parent 7762758958
commit 88a32ea7fd
4 changed files with 15 additions and 20 deletions

View File

@ -136,6 +136,8 @@ class FitRoutine(object):
linked_sender[repl].add(par)
linked_sender[par].add(repl)
print(_found_models)
for mm, m_data in _found_models.items():
if mm.global_parameter:
for dd in m_data:
@ -235,7 +237,7 @@ class FitRoutine(object):
var = []
data_pars = []
# loopyloop over data that belong to one fit (linked or global)
# loopy-loop over data that belong to one fit (linked or global)
for data in data_group:
actual_pars = []
for i, (p_k, v_k) in enumerate(data.parameter.items()):
@ -273,10 +275,10 @@ class FitRoutine(object):
# COST FUNCTIONS: f(x) - y (least_square, minimize), and f(x) (ODR)
def __cost_scipy(self, p, data, varpars, used_pars):
for keys, values in zip(varpars, p):
self.parameter[keys].scaled_value = values
self.parameter[keys].namespace[keys] = self.parameter[keys].value
data.parameter[keys].scaled_value = values
data.parameter[keys].namespace[keys] = data.parameter[keys].value
actual_parameters = [self.parameter[keys].value for keys in used_pars]
actual_parameters = [data.parameter[keys].value for keys in used_pars]
return data.cost(actual_parameters)
def __cost_odr(self, p, data, varpars, used_pars):
@ -438,15 +440,17 @@ class FitRoutine(object):
# update parameter values
for keys, p_value, err_value in zip(var_pars, p, err):
self.parameter[keys].scaled_value = p_value
self.parameter[keys].scaled_error = err_value
data.parameter[keys].scaled_value = p_value
data.parameter[keys].scaled_error = err_value
data.parameter[keys].namespace[keys] = data.parameter[keys].value
combinations = list(product(var_pars, var_pars))
actual_parameters = []
corr_idx = []
for i, p_i in enumerate(used_pars):
actual_parameters.append(self.parameter[p_i])
actual_parameters.append(data.parameter[p_i])
for j, p_j in enumerate(used_pars):
try:
# find the position of the parameter combinations

View File

@ -80,23 +80,15 @@ class Model(object):
self.fun_kwargs = {k: v.default for k, v in inspect.signature(model.func).parameters.items()
if v.default is not inspect.Parameter.empty}
def set_global_parameter(self, idx, p, var=None, lb=None, ub=None, default_bounds=False):
if idx is None:
self.parameter = Parameters()
self.global_parameter = {}
return
def set_global_parameter(self, key, value, var=None, lb=None, ub=None, default_bounds=False):
idx = [self.params.index(key)]
if default_bounds:
if lb is None:
lb = [self.lb[i] for i in idx]
if ub is None:
ub = [self.lb[i] for i in idx]
gp = self.parameter.add_parameter(p, var=var, lb=lb, ub=ub)
for k, v in zip(idx, gp):
self.global_parameter[k] = v
return gp
self.parameter.add(key, value, var=var, lb=lb, ub=ub)
@staticmethod
def _prep(param_len, val):

View File

@ -60,7 +60,7 @@ class Parameters(dict):
else:
key = f'_p{new_idx}'
new_keys.append(key)
self[key] = Parameter(param[i], var=var[i], lb=lb[i], ub=ub[i])
self[key] = Parameter(key, param[i], var=var[i], lb=lb[i], ub=ub[i])
return new_keys

View File

@ -56,7 +56,6 @@ class Lorentzian:
off (float): baseline
"""
# print('Lorentzian', a, mu, sigma, off)
return (a/np.pi) * 2*sigma / (4*(x-mu)**2 + sigma**2) + off