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

Remove LevMarLSQFitter #1899

Merged
merged 14 commits into from
Sep 27, 2024
Merged

Remove LevMarLSQFitter #1899

merged 14 commits into from
Sep 27, 2024

Conversation

larrybradley
Copy link
Member

This PR changes the default Astropy fitter for PSFPhotometry, IterativePSFPhotometry, and EPSFFitter from LevMarLSQFitter to TRFLSQFitter.

LevMarLSQFitter uses the Levenberg-Marquardt algorithm via the SciPy legacy function scipy.optimize.leastsq, which is no longer recommended. This fitter supports parameter bounds using an unsophisticated min/max condition where parameters that are out of bounds are simply reset to the min or max of the bounds during each step. This can cause parameters to stick to one of the bounds during the fitting process if the parameter gets close to the bound. If needed, this fitter can still be used by explicitly setting the fitter in the PSFPhotometry, IterativePSFPhotometry, and EPSFFitter classes.

The fitter used in RadialProfile to fit the profile with a Gaussian was also changed from LevMarLSQFitter to TRFLSQFitter.

The fitter used in centroid_1dg and centroid_2dg was changed from LevMarLSQFitter to LMLSQFitter.

See astropy/astropy#16983 for more information.

Closes: #1895

@larrybradley larrybradley added this to the 2.0.0 milestone Sep 27, 2024
@larrybradley larrybradley merged commit f447424 into astropy:main Sep 27, 2024
27 of 28 checks passed
@larrybradley larrybradley deleted the default-fitter branch September 27, 2024 02:33
@@ -105,11 +105,13 @@ def centroid_1dg(data, error=None, mask=None):

xy_data = [np.ma.sum(data, axis=i).data for i in (0, 1)]

# would need to use a different fitter if parameter bounds are used
fitter = LMLSQFitter()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larrybradley - gaussians have bounds enabled by default (on stddev) so if you are fitting a Gaussian I think it would be best to use TRFLSQFitter or another one, not LMLSQFitter

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@astrofrog Are you sure about that? Where are the bounds defined? I don't see any bounds defined in functional_models? Do you mean constrained to be postive? I still get negative stddev's sometimes.....

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also noticed that TRFLSQFitter is about 4-5% slower than LevMarLSQFitter even without any bounds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@larrybradley yes the fact the stddev is required to be positive - it's formally treated as a bound.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you see negative stddev values with LMLSQ or TRFLSQ or both?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also noticed that TRFLSQFitter is about 4-5% slower than LevMarLSQFitter even without any bounds.

Luckily the fitters will be significantly faster with astropy 7.0.0 so should be more than enough to compensate for this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see that's only for Gaussian1D. I was seeing negative stddev when fitting Gaussian2D. Why is Gaussian1D stddev bounded, but not Gaussian2D? Should I open a PR?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DOC: Replace LevMarLSQFitter with something else?
2 participants