tBuLi / symfit

Symbolic Fitting; fitting as it should be.
http://symfit.readthedocs.org
MIT License
232 stars 17 forks source link

AttributeError: 'Model' object has no attribute '_cached_numerical_components' #372

Open bartrex13 opened 7 months ago

bartrex13 commented 7 months ago

I have a problem while solving my own problem. I tried to fit connected equations with shared parameters, but I get an error, so then I just try to get the result passing random numbers to the model and get the same error. Following error appears even if I change the model function some other ones (e.g. I tried a simple sum of all params and vars).

My simplified code:

from symfit import variables, parameters, Fit, Model
from sympy import cos, sin, tan, sqrt

x, y, z1, z2 = variables('x, y, z1, z2')
y0, gamma, h, r0, alpha = parameters('y0, gamma, h, r0, alpha')

model_dict = {
    z1: (x / r0 * h * sqrt(1 + ((y-y0)/r0)**2) / (cos(gamma) - (y-y0)/r0*sin(gamma)))*cos(alpha),
    # z2: (h * sqrt(1 + (x/r0)**2) * (((y-y0)/r0 + tan(gamma))/(1 - (y-y0)/r0*tan(gamma)) - tan(gamma)))*sin(alpha),
}
model = Model(model_dict)
# print(model)

zfit = model(x=34, y=46, y0=-70, gamma=1, h=34, r0=2346, alpha=0.865).z1
print(zfit)

The error I got:

Traceback (most recent call last):

  File C:\Miniconda3\lib\site-packages\symfit\core\support.py:264 in __get__
    return getattr(obj, self.cache_attr)

AttributeError: 'Model' object has no attribute '_cached_numerical_components'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

  File C:\Miniconda3\lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec
    exec(code, globals, locals)

  File c:\...\error.py:14
    zfit = model(x=34, y=46, y0=-70, gamma=1, h=34, r0=2346, alpha=0.865).z1

  File C:\Miniconda3\lib\site-packages\symfit\core\models.py:706 in __call__
    return ModelOutput(self.keys(), self.eval_components(*args, **kwargs))

  File C:\Miniconda3\lib\site-packages\symfit\core\models.py:647 in eval_components
    components = dict(zip(self, self.numerical_components))

  File C:\Miniconda3\lib\site-packages\symfit\core\support.py:267 in __get__
    setattr(obj, self.cache_attr, self.fget(obj))

  File C:\Miniconda3\lib\site-packages\symfit\core\models.py:852 in numerical_components
    components.append(sympy_to_py(expr, ordered))

  File C:\Miniconda3\lib\site-packages\symfit\core\support.py:105 in sympy_to_py
    lambdafunc = lambdify(args, func, dummify=False)

  File C:\Miniconda3\lib\site-packages\sympy\utilities\lambdify.py:878 in lambdify
    funcstr = funcprinter.doprint(funcname, iterable_args, _expr, cses=cses)

  File C:\Miniconda3\lib\site-packages\sympy\utilities\lambdify.py:1141 in doprint
    argstrs, expr = self._preprocess(args, expr)

  File C:\Miniconda3\lib\site-packages\sympy\utilities\lambdify.py:1209 in _preprocess
    s = self._argrepr(arg)

  File C:\Miniconda3\lib\site-packages\sympy\printing\codeprinter.py:163 in doprint
    lines = self._print(expr).splitlines()

  File C:\Miniconda3\lib\site-packages\sympy\printing\printer.py:309 in _print
    return getattr(expr, self.printmethod)(self, **kwargs)

  File C:\Miniconda3\lib\site-packages\symfit\core\argument.py:76 in _sympystr
    return printer.doprint(self.name)

  File C:\Miniconda3\lib\site-packages\sympy\printing\codeprinter.py:156 in doprint
    expr = self._handle_UnevaluatedExpr(expr)

  File C:\Miniconda3\lib\site-packages\sympy\printing\codeprinter.py:116 in _handle_UnevaluatedExpr
    return expr.replace(re, lambda arg: arg if isinstance(

TypeError: Basic.replace() missing 1 required positional argument: 'value'

As you see I use anaconda. python==3.10.13 symfit==0.5.6 sympy==1.12 numpy==1.26.0 scipy==1.11.3

I checked could my code working on the oldest version and I found out that it works with symfit==0.4.6 (sympy==1.1.1) but when I add second equation to the model another error occurs.