-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Add fittable 1D spline models #11634
Add fittable 1D spline models #11634
Conversation
👋 Thank you for your draft pull request! Do you know that you can use |
I can't tell if you already put 43 commits into this PR or you have unrelated commits... |
The first 22 of them are from the PR #11548 merge. The rest are the development of this. |
83a8709
to
465dc51
Compare
It has now been rebased and squashed. So it should be way easier to review now! |
The astropy models do not require that x be sorted into increasing order. For uniformity should that requirement be dropped here by sorting the input arrays before sending them to the scipy routine? |
@chris-simpson I think if I re-work this to use |
I amend this statement. It certainly implies that the data should be sorted. It assumes that the independent variables Its a simple enough step to run a sort on |
8483540
to
634b604
Compare
We also noticed a bit of work being done on spline models & fitters in astropy/specutils#291 (which has likewise attracted some input from STScI, though it was last updated a year ago), if you wanted to co-ordinate a bit? It would certainly be nice to get splines into |
Sure, I would be happy to co-ordinate so that everyone gets what they need! |
Hello @WilliamJamieson 👋! It looks like you've made some changes in your pull request, so I've checked the code again for style. There are no PEP8 style issues with this pull request - thanks! 🎉 Comment last updated at 2021-10-30 00:07:24 UTC |
d5990ec
to
dd2918d
Compare
a34e5e6
to
2b97737
Compare
May I suggest some tidying around the edges?
The warning isn't particularly useful if an exception gets raised immediately. I'm also not sure that the warning is worded correctly, since the "fit degree" isn't something the user ever defines. Splines obviously differ from polynomials because they can only be evaluated after the fitting is performed. If this hasn't happened then I think either a more specific exception should be raised, or the spline should evaluate to 0.
but
Maybe I'm doing something wrong here? |
Agreed, the warning being raised is intended for a different case (user accessing spline information prior to a fit) then what it is being raised for here. Instead it is probably better for it to raise a proper error instead.
This seems reasonable, although I think it would be better to have the spline simply raise a much more useful error as it could be misleading. I am open to debate about this.
The This points me to the conclusion that
Agreed, I overlooked testing for this. This should be an easy fix.
This is a product of combining the interpolation and least squares fitting into a single interface. Essentially, what is happening is that least squares fitting is using Note that I think the conclusion is that I should create a |
I'm confused by your nomenclature, and also by your trouble with knots. Let me set up some data:
Here are three ways to make a least-squares spline:
and they're all the same (there are negligible floating-point differences between the first two and the third). For This is one type of spline, where the user specifies |
I should have been more clear when it came to the knots. The original desired application, required an "interpolating" spline of the data using a predefined set of knots (generated by an independent calculation, which I was not provided). Note that interpolation requires that the for a data point I totally missed the |
111dae6
to
2c53a6e
Compare
0b69835
to
1402e9c
Compare
After offline discussion with @nden and @perrygreenfield, we decided to move the spline fitters to |
2dafa92
to
ef3e5b3
Compare
@chris-simpson @jehturner DO you have any other comments on this? |
I haven't been keeping up very well but I'll pull this and kick it around very quickly. Thanks for working on it. If everything looks good I imagine we'll replace our spline fitting in fit_1D with this at some point. We do have several tests for that code, which should help to confirm that the results are as expected. |
One change I'd suggest is removing the classmethods and moving the instance methods under the fitters. For example, move Spline.interpolated_data Also the degree setter should probably be removed. |
Hmm. I see @nden's point that there are too many different ways to do the fitting. However, I'm not sure if I like the suggestion above because I was thinking :
So maybe that argues for removing Another option is to do the most idiomatic of the three ways (which I think is @nden's suggestion above?) now, but have a follow-on PR for 5.1 that adds the above back in, since that avoids the problem of API lock-in. |
I think the current CI failures in this PR are from #12324, not this PR itself. |
I'm not spotting any mention of splines in the narrative docs -- just an automodapi reference. Does some description still need adding there? |
The narrative docs will be updated next week. The feature freeze today is only for new features. |
4892206
to
84f654c
Compare
This is ready to be merged after the unrelated CI failures are resolved. |
84f654c
to
cd085ae
Compare
OK. It will be nice to see a summary/comparison of the different kinds of fitters in particular. I haven't pulled AstroPy for a while and ran into some problems with dependency versions, in between meetings etc. Superficially I would say this looks very promising though (I will look at it more when I can). Do the fitters handle model sets with masked data nicely? Has this been tested with |
No. However, a very similar iterative rejection fitting method (its not ready for merging into astropy yet) has been partially tested with these splines. So I feel like it should be possible to get it to work with |
Added initial spline More additions to spline Added spline model evaluate Filled in warnings Added reset for spline. Added bounding box error fix and evaluate derivative passthrough Added SplineFitter classes Added Spline2D support Fixes to some bugs in spline2D Setting up for tests Added fitting tests and fixed bounding box Added tests for optional argument hack Added warning tests for spline fitter Initial tests for Spline2D Initial fitter tests for Spline2D Finished adding Spline2D fit tests Added call test for Spline2D hack Added SplineFitter tests Added SplineLSQFitter Added some documentation. Refactored common code in bbox handling. Refactored to allow access to underlying spline Moved bounding box into abstract sub-class Abstracted optional input handling Made optional input parameters private variables Fully tested abstract base class Added changelog fragment. Added splines to common name spaces Fixed circular import error. Initial tck-refactor work Partial refactor to using tck instead of spline class in 1D Almost completed Spline1D refactor Refactored 1D spline is fully tested now Start of refactor of _Spline tests Added _Spline tests Refactored so that dimension was better accounted for in _Spline Spline2D transition to using tck tuple done except for fitting. Added Spline2D fitter Cleaned up comments Added Fitter for Spline1D Added 2D spline fitter Fixed codestyle errors Added updated the basic documentation. Added input sorting Fixed code style and pep8speaks warning (used walrus operator which is not supported in python 3.7) Fixed more walrus errors Fixed scipy testing error Removed scipy from _Spline test Fixed code coverage and added better tck protection Added derivative and antiderivative constructors for Spline1D Fixed test broken by rebase Made unfit spline default to 0 as requested Updates to insure 1D spline fitting can accept non-numpy array data. Added full out lsq fitter tests. Increase code coverage for spline. Import new 1D spline API Removing unnecessary changes to `modeling/utils.py` Added tests for uncovered lines Fixing code documentation Fixed missing docstrings and typos in docstrings. Added example to spline Changed fitting interface to use `weights` keyword instead of `w`. Added whatsnew entry for splines Update docs/whatsnew/5.0.rst Co-authored-by: Erik Tollerud <erik.tollerud@gmail.com> Incorporated eteq's suggestions for changing docstrings At eteq's suggestion moved from using lsq to using term exact_knots Moved spline fitters from `fitting.py` to `spline.py` Fixed doctest fail Copied spline fitting code into fitter. Removed bound constructors Removed interpolate_data method Removed `smoothing_fit` method Removed `exact_knots_fit` Removed `sprep_data` method Removed degree setter. Updated doc-string to reflect new interfaces.
cd085ae
to
4f9b443
Compare
This seems like a nice new feature to highlight in the 'What's new in 5.0?' page. @WilliamJamieson - could you consider opening a pull request to the v5.0.x branch to add an entry to this file: https://github.com/astropy/astropy/blob/v5.0.x/docs/whatsnew/5.0.rst to advertise this new feature? (including a plot if possible!) |
I see there is already an entry for this but adding a plot showing an example of a fitted spline would be a nice addition |
Description
This pull request introduces fittable spline models to
~astropy.modeling
based on thescipy.interpolate
spline models.Note that this only introduces 1D spline models; 2D spline models will be introduced in a later pull request.
Note that this pull request depends on pull request #11548.