Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in using astropy.modeling.fitting with weights in 2d fits #5737

Closed
deleenm opened this issue Jan 25, 2017 · 1 comment
Closed

Error in using astropy.modeling.fitting with weights in 2d fits #5737

deleenm opened this issue Jan 25, 2017 · 1 comment

Comments

@deleenm
Copy link

deleenm commented Jan 25, 2017

Description:

I am trying to fit a 2d Gaussian with weights for each data point using the astropy.modeling.fitting in astropy version 1.3, python 2.7.13, scipy 0.18.1, and numpy 1.11.3. When I try this using a 1d Gaussian model everything seems to work fine, but I am encountering a shape error when I try to do it in the 2d case. The weights have the same shape (21,21) as everything else. The code I used and the error is below.

Code:

from astropy.modeling import models,fitting
import numpy as np

def gaussian(height, center_x, center_y, width_x, width_y):
    """Returns a gaussian function with the given parameters"""
    width_x = float(width_x)
    width_y = float(width_y)
    return lambda x,y: height*np.exp(
                -(((center_x-x)/width_x)**2+((center_y-y)/width_y)**2)/2)

Xin, Yin = np.mgrid[0:21, 0:21]
y = gaussian(10, 10, 9, 2, 3)(Xin, Yin)

#Add gaussian noise to the function with a random sigma between 0.1 and 1.1
ysig = 1*np.random.rand(Xin.shape[0],Xin.shape[1]) + 0.1
y = y + np.random.normal(0., ysig, Xin.shape)
#Fit the data
gauss2d_init = models.Gaussian2D(amplitude=15, x_mean=5, y_mean=5, x_stddev=1, y_stddev=1, theta=0)
fit_gauss2d = fitting.LevMarLSQFitter()
gmod = fit_gauss2d(gauss2d_init, Xin, Yin, y,weights=1.0/ysig)

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-589f3eb378b7> in <module>()
     12 print(y.shape)
     13 fit_gauss2d = fitting.LevMarLSQFitter()
---> 14 gmod = fit_gauss2d(gauss2d_init, Xin, Yin, y,weights=1.0/ysig)
     15 print(fit_gauss2d.fit_info.keys())
     16 print("{} {} {} {} {} {}".format(*gmod.param_names))

/home/deleenm/anaconda2/lib/python2.7/site-packages/astropy/modeling/fitting.pyc in __call__(self, model, x, y, z, weights, maxiter, acc, epsilon, estimate_jacobian)
    558             self.objective_function, init_values, args=farg, Dfun=dfunc,
    559             col_deriv=model_copy.col_fit_deriv, maxfev=maxiter, epsfcn=epsilon,
--> 560             xtol=acc, full_output=True)
    561         _fitter_to_model_params(model_copy, fitparams)
    562         self.fit_info.update(dinfo)

/home/deleenm/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in leastsq(func, x0, args, Dfun, full_output, col_deriv, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)
    388     else:
    389         if col_deriv:
--> 390             _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (n, m))
    391         else:
    392             _check_func('leastsq', 'Dfun', Dfun, x0, args, n, (m, n))

/home/deleenm/anaconda2/lib/python2.7/site-packages/scipy/optimize/minpack.pyc in _check_func(checker, argname, thefunc, x0, args, numinputs, output_shape)
     24 def _check_func(checker, argname, thefunc, x0, args, numinputs,
     25                 output_shape=None):
---> 26     res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
     27     if (output_shape is not None) and (shape(res) != output_shape):
     28         if (output_shape[0] != 1):

/home/deleenm/anaconda2/lib/python2.7/site-packages/astropy/modeling/fitting.pyc in _wrap_deriv(params, model, weights, x, y, z)
    620                 return [np.ravel(_) for _ in np.ravel(weights) * np.array(model.fit_deriv(x, *params))]
    621             else:
--> 622                 return [np.ravel(_) for _ in (np.ravel(weights) * np.array(model.fit_deriv(x, y, *params)).T).T]
    623 
    624 

ValueError: operands could not be broadcast together with shapes (441,) (21,21,6) 
@nden
Copy link
Contributor

nden commented Feb 17, 2017

Fixed in #5788

@nden nden closed this as completed Feb 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants