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

View File

@ -56,7 +56,7 @@ class Lorentzian:
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