tBuLi / symfit

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

ValueError: could not broadcast input array from shape (11,11) into shape (11) #149

Closed Jhsmit closed 6 years ago

Jhsmit commented 6 years ago

testarr.txt I have the following code to fit a 2D gaussian:

from symfit import Variable, Parameter, Fit, exp
import numpy as np

arr = np.genfromtxt('testarr.txt')

a = Parameter(name='a', value=8000)
b = Parameter(name='b', value=3000, min=100)
sig_x = Parameter(name='sig_x', value=2)
sig_y = Parameter(name='sig_y', value=1.5)
mu_x = Parameter(name='mu_x', value=0.5)
mu_y = Parameter(name='mu_y', value=0.5)
x_var = Variable(name='x_var')
y_var = Variable(name='y_var')
z_var = Variable(name='z_var')

model = {z_var: ((a*exp(-(((x_var-mu_x)**2/(2*sig_x**2)) + ((y_var-mu_y)**2 / (2*sig_y**2))))))+b}
shape = arr.shape

xv, yv = np.meshgrid(*[np.arange(1 - np.floor(x/2), x - np.floor(x/2) + 1) for x in shape])
fit = Fit(model, z_var=arr, y_var=yv, x_var=xv)
res = fit.execute()

print(res)

Which gives this error:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\pydev_run_in_console.py", line 53, in run_file
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm 2018.1\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Smit/_processed_data/2018/20180419_psf/fitting.py", line 29, in <module>
    res = fit.execute()
  File "C:\Miniconda3\envs\py_main\lib\site-packages\symfit\core\fit.py", line 1364, in execute
    cov_matrix = self.covariance_matrix(dict(zip(self.model.params, minimizer_ans._popt)))
  File "C:\Miniconda3\envs\py_main\lib\site-packages\symfit\core\fit.py", line 850, in covariance_matrix
    return self._cov_mat_equal_lenghts(best_fit_params=best_fit_params)
  File "C:\Miniconda3\envs\py_main\lib\site-packages\symfit\core\fit.py", line 914, in _cov_mat_equal_lenghts
    jac = np.array(self.model.eval_jacobian(**kwargs))
  File "C:\Miniconda3\envs\py_main\lib\site-packages\symfit\core\fit.py", line 474, in eval_jacobian
    jac[idx] = np.array(jac[idx], dtype=float)
ValueError: could not broadcast input array from shape (11,11) into shape (11)

When I remove the constant b from the model, it does run.

 model = {z_var: ((a*exp(-(((x_var-mu_x)**2/(2*sig_x**2)) + ((y_var-mu_y)**2 / (2*sig_y**2))))))}

However, I'd very much like to have this b there. I'm aware I have a surplus of brackets, but for brackets goes, the more the better.

I'm using symfit 0.4.0 and numpy 1.13.1, which is the latest version distributed by conda. With numpy 1.14.2 the bug persists.

tBuLi commented 6 years ago

Thanks for reporting this issue. It turned out to be caused by the fact that the derivative wrt to b is just int 1, which was then incorrectly changed to a shape 11 vector containing ones instead of an (11, 11) matrix. Fixed it, thanks for reporting.