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[repl].add(par)
linked_sender[par].add(repl) linked_sender[par].add(repl)
print(_found_models)
for mm, m_data in _found_models.items(): for mm, m_data in _found_models.items():
if mm.global_parameter: if mm.global_parameter:
for dd in m_data: for dd in m_data:
@ -235,7 +237,7 @@ class FitRoutine(object):
var = [] var = []
data_pars = [] 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: for data in data_group:
actual_pars = [] actual_pars = []
for i, (p_k, v_k) in enumerate(data.parameter.items()): 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) # COST FUNCTIONS: f(x) - y (least_square, minimize), and f(x) (ODR)
def __cost_scipy(self, p, data, varpars, used_pars): def __cost_scipy(self, p, data, varpars, used_pars):
for keys, values in zip(varpars, p): for keys, values in zip(varpars, p):
self.parameter[keys].scaled_value = values data.parameter[keys].scaled_value = values
self.parameter[keys].namespace[keys] = self.parameter[keys].value 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) return data.cost(actual_parameters)
def __cost_odr(self, p, data, varpars, used_pars): def __cost_odr(self, p, data, varpars, used_pars):
@ -438,15 +440,17 @@ class FitRoutine(object):
# update parameter values # update parameter values
for keys, p_value, err_value in zip(var_pars, p, err): for keys, p_value, err_value in zip(var_pars, p, err):
self.parameter[keys].scaled_value = p_value data.parameter[keys].scaled_value = p_value
self.parameter[keys].scaled_error = err_value data.parameter[keys].scaled_error = err_value
data.parameter[keys].namespace[keys] = data.parameter[keys].value
combinations = list(product(var_pars, var_pars)) combinations = list(product(var_pars, var_pars))
actual_parameters = [] actual_parameters = []
corr_idx = [] corr_idx = []
for i, p_i in enumerate(used_pars): 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): for j, p_j in enumerate(used_pars):
try: try:
# find the position of the parameter combinations # 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() self.fun_kwargs = {k: v.default for k, v in inspect.signature(model.func).parameters.items()
if v.default is not inspect.Parameter.empty} if v.default is not inspect.Parameter.empty}
def set_global_parameter(self, idx, p, var=None, lb=None, ub=None, default_bounds=False): def set_global_parameter(self, key, value, var=None, lb=None, ub=None, default_bounds=False):
if idx is None: idx = [self.params.index(key)]
self.parameter = Parameters()
self.global_parameter = {}
return
if default_bounds: if default_bounds:
if lb is None: if lb is None:
lb = [self.lb[i] for i in idx] lb = [self.lb[i] for i in idx]
if ub is None: if ub is None:
ub = [self.lb[i] for i in idx] ub = [self.lb[i] for i in idx]
gp = self.parameter.add_parameter(p, var=var, lb=lb, ub=ub) self.parameter.add(key, value, var=var, lb=lb, ub=ub)
for k, v in zip(idx, gp):
self.global_parameter[k] = v
return gp
@staticmethod @staticmethod
def _prep(param_len, val): def _prep(param_len, val):

View File

@ -60,7 +60,7 @@ class Parameters(dict):
else: else:
key = f'_p{new_idx}' key = f'_p{new_idx}'
new_keys.append(key) 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 return new_keys

View File

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