You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I started using astropy.models in my own code, but have run in to some issues. First off, we need to sprint hard on the documentation -- no one will use this if we don't have good documentation explaining all of the nice features in here, which are currently buried in code!
I'm having trouble getting bounded / tied parameters to work with the Gaussian2DModel. Haven't tried this with others, and haven't yet tried to dig in to the code, but here's an example of what I'm trying to do -- fit a 2D Gaussian to some image data:
@adrn Could you fetch upstream and try your example again. When given in the initializer the constraints were not propagated. This should be fixed by #1075.
I agree about the documentation. May be this can become an example of constraint fitting?
I am not closing this until I hear it works.
Oh! Oops, didn't notice the new import stuff -- hmm, I wonder why the code above worked...must have a phantom old version of astropy installed alongside the dev version...
With the new astropy.modeling import, the above kind of works...
@nden@eteq Here's some code that generates simulated data, then uses astropy and scipy.optimize to fit the data. For some reason, astropy.modeling is doing much worse:
@adrn Note that currently the astropy Gaussian2DModeleval gives incorrect results (see point 1 in #1077).
Care to fix Gaussian2DModel and add a unit test for eval (and gderiv if possible)?
@adrn In the gist the initial values p0 are compared with the output of optimize. If you change the true output to the output of the NonLinearLSQFitter the example works.
print("true", ["{0:.3f}".format(x) for x in gauss.parameters])
I will add this to the tests unless you want to add it.
Were the original problems with bounds fixed?
It seems like the bounds work now, but when I run it with bounds the two stddev's get slammed to the upper bound (4.). If I delete the bounds, the y_stddev value goes to ~271 (true value is 2.).
@adrn I don't think this test compares apples to apples. The gaussian2d function does not evaluate to the same thing as models.Gaussian2DModel. Here's a test which I think does what you intended but let me know if I misunderstood something (I copied the eval method from Gaussian2DModel):
from astropy.modeling import models, fitting
import numpy as np
from scipy.optimize import leastsq
def test_gaussian2d_fitting():
def gaussian2d(p, x, y):
a = 0.5 * ((np.cos(p[5]) / p[3]) ** 2 +
(np.sin(p[5]) / p[4]) ** 2)
b = 0.5 * (np.cos(p[5]) * np.sin(p[5]) *
(1. / p[3] ** 2 - 1. / p[4] ** 2))
c = 0.5 * ((np.sin(p[5]) / p[3]) ** 2 +
(np.cos(p[5]) / p[4]) ** 2)
return p[0] * np.exp(-(a * (x - p[1]) ** 2 +
b * (x - p[1]) * (y - p[2]) +
c * (y - p[2]) ** 2))
def error_function(p, x, y, z):
return z - gaussian2d(p, x, y)
X,Y = np.meshgrid(np.arange(11),np.arange(11))
# generate fake data
p0 = [137., 5.1, 5.4, 1.5, 2., np.pi/4]
data = gaussian2dm(p0, X.ravel(), Y.ravel()).reshape(X.shape)
rsn=np.random.RandomState(1234567890)
n=rsn.randn(data.size)
n.shape = data.shape
data += n
gauss = models.Gaussian2DModel(amplitude=10., x_mean=5., y_mean=5.,
x_stddev=4., y_stddev=4., theta=0.5)
gauss_fit = fitting.NonLinearLSQFitter(gauss)
gauss_fit(X, Y, data)
p_opt, success = leastsq(error_function,
[10., 5., 5., 4., 4., 0.5],
args=(X.ravel(),Y.ravel(),data.ravel()),
maxfev=10000)
print("true", ["{0:.3f}".format(x) for x in p0])
print("leastsq", ["{0:.3f}".format(x) for x in p_opt])
print("modeling", ["{0:.3f}".format(x) for x in gauss.parameters])
@adrn The Gaussian2DModel and contraints code has been updated in the meantime and is not covered by unit tests. Could you try again and let us know if you still find any issues?
@nden Running the example from your latest comment I don't get identical results for leastsq and modeling!?
Activity
nden commentedon May 9, 2013
@adrn Could you fetch upstream and try your example again. When given in the initializer the constraints were not propagated. This should be fixed by #1075.
I agree about the documentation. May be this can become an example of constraint fitting?
I am not closing this until I hear it works.
nden commentedon May 9, 2013
@adrn actually I missed one commit, so part of this won't work
Address problems with constraints reported in astropy#1060 and astrop…
Merge pull request #1107 from nden/constraints_issues
eteq commentedon May 18, 2013
@adrn - was this fixed by #1107 ?
adrn commentedon May 19, 2013
@eteq I don't think so. I fetched from master, but the bounds still don't work -- run this:
I get negative mean values and huge standard deviations.
adrn commentedon May 19, 2013
Oh! Oops, didn't notice the new import stuff -- hmm, I wonder why the code above worked...must have a phantom old version of astropy installed alongside the dev version...
With the new
astropy.modelingimport, the above kind of works...adrn commentedon May 19, 2013
Spoke too soon -- looks like the bounds work for the
stddev's but not the mean? strange.adrn commentedon May 19, 2013
@nden @eteq Here's some code that generates simulated data, then uses
astropyandscipy.optimizeto fit the data. For some reason,astropy.modelingis doing much worse:https://gist.github.com/adrn/5606738
cdeil commentedon May 19, 2013
@adrn Note that currently the astropy
Gaussian2DModelevalgives incorrect results (see point 1 in #1077).Care to fix
Gaussian2DModeland add a unit test foreval(andgderivif possible)?cdeil commentedon May 19, 2013
I just saw that @astrofrog already fixed the astropy Gaussian models in #1106 .
adrn commentedon May 19, 2013
Hm, there is some deeper issue here. I tried out #1106 on the gist code I posted above, still doesn't work. I'm going to take a look at this...
Address problems with constraints reported in astropy#1060 and astrop…
nden commentedon May 22, 2013
@adrn In the gist the initial values
p0are compared with the output ofoptimize. If you change thetrueoutput to the output of theNonLinearLSQFitterthe example works.I will add this to the tests unless you want to add it.
Were the original problems with bounds fixed?
adrn commentedon May 22, 2013
@nden "true" are the parameters I used to generate the data, not meant to be the
NonLinearLSQFitterparameters. I updated the gist code to also print those values, though...still getting weirdness. Here's the code again:https://gist.github.com/adrn/5606738
It seems like the bounds work now, but when I run it with bounds the two stddev's get slammed to the upper bound (4.). If I delete the bounds, the
y_stddevvalue goes to ~271 (true value is 2.).adrn commentedon May 22, 2013
Here are the results from removing the bounds on the stddev's:
true ['137.000', '5.100', '5.400', '1.500', '2.000', '0.785']
leastsq ['137.135', '5.094', '5.410', '1.498', '2.000', '0.786']
astropy.modeling ['132.801', '5.094', '5.409', '-0.877', '271.430', '0.644']
Not sure why it's doing so much worse than leastsq...
nden commentedon May 22, 2013
@adrn I don't think this test compares apples to apples. The
gaussian2dfunction does not evaluate to the same thing asmodels.Gaussian2DModel. Here's a test which I think does what you intended but let me know if I misunderstood something (I copied theevalmethod fromGaussian2DModel):The result of this is:
cdeil commentedon Aug 8, 2013
@adrn The
Gaussian2DModeland contraints code has been updated in the meantime and is not covered by unit tests. Could you try again and let us know if you still find any issues?@nden Running the example from your latest comment I don't get identical results for
leastsqandmodeling!?(I had to change the line
to
to make it run.)
adrn commentedon Oct 8, 2013
@nden what's the status of this? I'll check with some tests of my own but want to hear from you too..
nden commentedon Oct 8, 2013
@adrn After merging #1534 it will be ready to test.
nden commentedon Oct 11, 2013
@adrn #1534 was merged and I think this is ready to be closed. Can you confirm it works for you?
nden commentedon Oct 18, 2013
@adrn I am closing this as it was included as a test in
test_models. Please open a different issue if you think something is wrong.