From 88a32ea7fd3a5130f55af0d04d6f12f9b02c953e Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Tue, 15 Aug 2023 18:44:08 +0200 Subject: [PATCH] multiple single fits working --- src/nmreval/fit/minimizer.py | 18 +++++++++++------- src/nmreval/fit/model.py | 14 +++----------- src/nmreval/fit/parameter.py | 2 +- src/nmreval/models/spectrum.py | 1 - 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/nmreval/fit/minimizer.py b/src/nmreval/fit/minimizer.py index 9a5ede5..26f45c5 100644 --- a/src/nmreval/fit/minimizer.py +++ b/src/nmreval/fit/minimizer.py @@ -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 diff --git a/src/nmreval/fit/model.py b/src/nmreval/fit/model.py index c0121da..bfca558 100644 --- a/src/nmreval/fit/model.py +++ b/src/nmreval/fit/model.py @@ -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): diff --git a/src/nmreval/fit/parameter.py b/src/nmreval/fit/parameter.py index 879b22e..4b5b821 100644 --- a/src/nmreval/fit/parameter.py +++ b/src/nmreval/fit/parameter.py @@ -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 diff --git a/src/nmreval/models/spectrum.py b/src/nmreval/models/spectrum.py index 1c78de2..30b6917 100644 --- a/src/nmreval/models/spectrum.py +++ b/src/nmreval/models/spectrum.py @@ -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