[AstroPy] astropy.modeling.fitting Using Weights in 2d Fits

Nadia Dencheva dencheva at stsci.edu
Wed Jan 25 10:18:03 EST 2017


Hi Natan,

This looks like a bug. Could you file an issue on github

https://github.com/astropy/astropy/issueshttps://github.com/astropy/astropy/issues

Thanks,
Nadia


________________________________
From: AstroPy [astropy-bounces at scipy.org] on behalf of Nathan De Lee [deleenm at nku.edu]
Sent: Tuesday, January 24, 2017 12:45 PM
To: astropy at scipy.org
Subject: [AstroPy] astropy.modeling.fitting Using Weights in 2d Fits

Hi Everyone,

I am trying to fit a 2d Gaussian with weights for each data point using the astropy.modeling.fitting in astropy version 1.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. Am I doing something wrong, or is there a bug that I am running into?

Thanks for any help you can give,

Nathan

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)



--
Dr. Nathan De Lee
Assistant Professor of Astronomy and Physics
Northern Kentucky University
151 Science Center
1 Nunn Drive
Highland Heights, KY 41099
Tel: 859-572-5492
Fax: 859-572-6092
www.nku.edu/~deleen1<http://www.nku.edu/~deleen1>
deleenm at nku.edu<mailto:deleenm at nku.edu>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/astropy/attachments/20170125/73adab14/attachment.html>


More information about the AstroPy mailing list