eval parameter

This commit is contained in:
Dominik Demuth 2023-07-27 18:58:22 +02:00
parent 7a50028202
commit ca130eaa14
3 changed files with 15 additions and 14 deletions

View File

@ -103,7 +103,7 @@ class FitRoutine(object):
linked_sender[v] = set() linked_sender[v] = set()
self.parameter.update(v.parameter.copy()) self.parameter.update(v.parameter.copy())
# set temporaray model # set temporary model
if v.model is None: if v.model is None:
v.model = self.fit_model v.model = self.fit_model
self._no_own_model.append(v) self._no_own_model.append(v)
@ -111,9 +111,9 @@ class FitRoutine(object):
# register model # register model
if v.model not in _found_models: if v.model not in _found_models:
_found_models[v.model] = [] _found_models[v.model] = []
m_param = v.model.parameter.copy() # m_param = v.model.parameter.copy()
self.parameter.update(m_param) # self.parameter.update(m_param)
#
_found_models[v.model].append(v) _found_models[v.model].append(v)
if v.model not in linked_sender: if v.model not in linked_sender:
@ -208,6 +208,7 @@ class FitRoutine(object):
return self.result return self.result
def _prep_data(self, data): def _prep_data(self, data):
if data.get_model() is None: if data.get_model() is None:
data._model = self.fit_model data._model = self.fit_model
self._no_own_model.append(data) self._no_own_model.append(data)
@ -273,12 +274,9 @@ class FitRoutine(object):
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 self.parameter[keys].scaled_value = values
self.parameter._namespace[keys] = self.parameter[keys].scaled_value self.parameter[keys].namespace[keys] = self.parameter[keys].value
print(id(self.parameter._namespace), id(self.parameter[keys].namespace))
actual_parameters = [self.parameter[keys].value for keys in used_pars] actual_parameters = [self.parameter[keys].value for keys in used_pars]
print(actual_parameters)
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):

View File

@ -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): def add(self, name: str | Parameter, value: str | float | int = None, *, var=True, lb=-np.inf, ub=np.inf):
if isinstance(name, Parameter): if isinstance(name, Parameter):
par = name par = name
name = par.name
else: else:
par = Parameter(name=name, value=value, var=var, lb=lb, ub=ub) par = Parameter(name=name, value=value, var=var, lb=lb, ub=ub)
self[name] = par self[name] = par
@ -75,7 +76,6 @@ class Parameters(dict):
par.eval_allowed = True par.eval_allowed = True
def copy(self) -> Parameters: def copy(self) -> Parameters:
print('huhuuhuh')
new_para_dict = Parameters() new_para_dict = Parameters()
for k, v in self.items(): for k, v in self.items():
new_para = v.copy() new_para = v.copy()
@ -171,6 +171,8 @@ class Parameter:
@property @property
def value(self) -> float: def value(self) -> float:
if self._expr is not None and self.eval_allowed: 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) return eval(self._expr, self.namespace)
elif self._value is not None: elif self._value is not None:
return self._value return self._value

View File

@ -35,8 +35,8 @@ class Gaussian:
class Lorentzian: class Lorentzian:
type = 'Spectrum' type = 'Spectrum'
name = 'Lorentzian' name = 'Lorentzian'
equation = 'A (2/\pi)w/[4*(x-\mu)^{2} + w^{2}] + A_{0}' equation = r'A (2/\pi)w/[4*(x-\mu)^{2} + w^{2}] + A_{0}'
params = ['A', '\mu', 'w', 'A_{0}'] params = ['A', r'\mu', 'w', 'A_{0}']
ext_params = None ext_params = None
bounds = [(0, None), (None, None), (0, None), (None, None)] bounds = [(0, None), (None, None), (0, None), (None, None)]
@ -56,15 +56,16 @@ 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
class PseudoVoigt: class PseudoVoigt:
type = 'Spectrum' type = 'Spectrum'
name = 'Pseudo Voigt' name = 'Pseudo Voigt'
equation = 'A [R*2/\pi*w/[4*(x-\mu)^{2} + w^{2}] + ' \ equation = r'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}' r'(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}'] params = ['A', 'R', r'\mu', 'w', 'A_{0}']
ext_params = None ext_params = None
bounds = [(0, None), (0, 1), (None, None), (0, None)] bounds = [(0, None), (0, 1), (None, None), (0, None)]