From ca130eaa147513f12ed7b80bec55574744bef144 Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Thu, 27 Jul 2023 18:58:22 +0200 Subject: [PATCH] eval parameter --- src/nmreval/fit/minimizer.py | 14 ++++++-------- src/nmreval/fit/parameter.py | 4 +++- src/nmreval/models/spectrum.py | 11 ++++++----- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/nmreval/fit/minimizer.py b/src/nmreval/fit/minimizer.py index 5305a0a..9a5ede5 100644 --- a/src/nmreval/fit/minimizer.py +++ b/src/nmreval/fit/minimizer.py @@ -103,7 +103,7 @@ class FitRoutine(object): linked_sender[v] = set() self.parameter.update(v.parameter.copy()) - # set temporaray model + # set temporary model if v.model is None: v.model = self.fit_model self._no_own_model.append(v) @@ -111,9 +111,9 @@ class FitRoutine(object): # register model if v.model not in _found_models: _found_models[v.model] = [] - m_param = v.model.parameter.copy() - self.parameter.update(m_param) - + # m_param = v.model.parameter.copy() + # self.parameter.update(m_param) + # _found_models[v.model].append(v) if v.model not in linked_sender: @@ -208,6 +208,7 @@ class FitRoutine(object): return self.result def _prep_data(self, data): + if data.get_model() is None: data._model = self.fit_model self._no_own_model.append(data) @@ -273,12 +274,9 @@ class FitRoutine(object): def __cost_scipy(self, p, data, varpars, used_pars): for keys, values in zip(varpars, p): self.parameter[keys].scaled_value = values - self.parameter._namespace[keys] = self.parameter[keys].scaled_value - - print(id(self.parameter._namespace), id(self.parameter[keys].namespace)) + self.parameter[keys].namespace[keys] = self.parameter[keys].value actual_parameters = [self.parameter[keys].value for keys in used_pars] - print(actual_parameters) return data.cost(actual_parameters) def __cost_odr(self, p, data, varpars, used_pars): diff --git a/src/nmreval/fit/parameter.py b/src/nmreval/fit/parameter.py index 58dfe63..a7800a3 100644 --- a/src/nmreval/fit/parameter.py +++ b/src/nmreval/fit/parameter.py @@ -66,6 +66,7 @@ class Parameters(dict): def add(self, name: str | Parameter, value: str | float | int = None, *, var=True, lb=-np.inf, ub=np.inf): if isinstance(name, Parameter): par = name + name = par.name else: par = Parameter(name=name, value=value, var=var, lb=lb, ub=ub) self[name] = par @@ -75,7 +76,6 @@ class Parameters(dict): par.eval_allowed = True def copy(self) -> Parameters: - print('huhuuhuh') new_para_dict = Parameters() for k, v in self.items(): new_para = v.copy() @@ -171,6 +171,8 @@ class Parameter: @property def value(self) -> float: if self._expr is not None and self.eval_allowed: + print(self._expr, self.namespace) + print(eval(self._expr, self.namespace)) return eval(self._expr, self.namespace) elif self._value is not None: return self._value diff --git a/src/nmreval/models/spectrum.py b/src/nmreval/models/spectrum.py index 229cf66..2a59aee 100644 --- a/src/nmreval/models/spectrum.py +++ b/src/nmreval/models/spectrum.py @@ -35,8 +35,8 @@ class Gaussian: class Lorentzian: type = 'Spectrum' name = 'Lorentzian' - equation = 'A (2/\pi)w/[4*(x-\mu)^{2} + w^{2}] + A_{0}' - params = ['A', '\mu', 'w', 'A_{0}'] + equation = r'A (2/\pi)w/[4*(x-\mu)^{2} + w^{2}] + A_{0}' + params = ['A', r'\mu', 'w', 'A_{0}'] ext_params = None bounds = [(0, None), (None, None), (0, None), (None, None)] @@ -56,15 +56,16 @@ class Lorentzian: off (float): baseline """ + print('Lorentzian', a, mu, sigma, off) return (a/np.pi) * 2*sigma / (4*(x-mu)**2 + sigma**2) + off class PseudoVoigt: type = 'Spectrum' name = 'Pseudo Voigt' - equation = 'A [R*2/\pi*w/[4*(x-\mu)^{2} + w^{2}] + ' \ - '(1-R)*sqrt(4*ln(2)/pi)/w*exp(-4*ln(2)[(x-\mu)/w]^{2})] + A_{0}' - params = ['A', 'R', '\mu', 'w', 'A_{0}'] + equation = r'A [R*2/\pi*w/[4*(x-\mu)^{2} + w^{2}] + ' \ + r'(1-R)*sqrt(4*ln(2)/pi)/w*exp(-4*ln(2)[(x-\mu)/w]^{2})] + A_{0}' + params = ['A', 'R', r'\mu', 'w', 'A_{0}'] ext_params = None bounds = [(0, None), (0, 1), (None, None), (0, None)]