1
0
forked from IPKM/nmreval

fit with expression works with single fit

This commit is contained in:
Dominik Demuth 2023-07-30 19:34:59 +02:00
parent ca130eaa14
commit c601e77cec
2 changed files with 11 additions and 4 deletions

View File

@ -2,6 +2,7 @@ from __future__ import annotations
from numbers import Number from numbers import Number
from itertools import count from itertools import count
from re import sub
import numpy as np import numpy as np
@ -69,6 +70,9 @@ class Parameters(dict):
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)
name = _prepare_namespace_string(name)
self[name] = par self[name] = par
par.eval_allowed = False par.eval_allowed = False
self._namespace[name] = par.value self._namespace[name] = par.value
@ -114,6 +118,7 @@ class Parameter:
self._expr = None self._expr = None
if isinstance(value, str): if isinstance(value, str):
value = _prepare_namespace_string(value)
self._expr = value self._expr = value
self.var = False self.var = False
@ -171,9 +176,7 @@ 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) return eval(self._expr, {}, self.namespace)
print(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
@ -216,3 +219,7 @@ class Parameter:
para_copy = Parameter(name=self.name, value=val, var=self.var, lb=self.lb, ub=self.ub) para_copy = Parameter(name=self.name, value=val, var=self.var, lb=self.lb, ub=self.ub)
return para_copy return para_copy
def _prepare_namespace_string(expr: str) -> str:
return sub('[\(\)\{.\}\\\]', '_', expr)

View File

@ -56,7 +56,7 @@ class Lorentzian:
off (float): baseline off (float): baseline
""" """
print('Lorentzian', a, mu, sigma, off) # 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