spacetelescope / jwst

Python library for science observations from the James Webb Space Telescope
https://jwst-pipeline.readthedocs.io/en/latest/
Other
560 stars 167 forks source link

Refactor/rewrite of extract1d #8802

Open stscijgbot-jp opened 1 week ago

stscijgbot-jp commented 1 week ago

Issue JP-3753 was created on JIRA by Timothy Brandt:

The extract1d software is slow due to a reliance on lists and explicit for loops, it can be difficult to read, and it does not perform optimal extraction.  The refactoring described in this ticket will use efficient numpy arrays and linear algebra to preserve the current behavior, improve background fitting and error estimates, and provide functionality for optimal and PSF-based extraction if the appropriate templates are supplied as arrays.  

Extract1d will, by default, operate on the same reference file information as it does currently and will produce similar, though not identical, behavior (mostly in the treatment of backgrounds).  In the future PSF-based and optimal extraction approaches are planned to be offered (see https://jira.stsci.edu/browse/JP-251).  

stscijgbot-jp commented 6 days ago

Comment by Timothy Brandt on JIRA:

I made progress on this during my weekend travel.  I am attaching a notebook with an initial implementation of a new extract1d approach with most functionality implemented.  I would like anyone interested to take a look at this for general comments.  There are a couple of things I would like to discuss:

-With a weighted or optimal-type extraction, it is no longer straightforward to keep read noise and photon noise separate.  I would like to discuss what to do.-  [edit] T{}his is incorrect: it is relatively straightforward to do so.{}

There is a bias in optimal or weighted extractions that it would be very nice to avoid.  The notebook includes a demonstration of this bias at the end with a sketch of a suggested approach to avoid it.

More generally, I would like comments on whether this includes all of the basic desired functionality, and whether the code structure is generally satisfactory.[^extract1d_framework.ipynb]

stscijgbot-jp commented 5 days ago

Comment by Melanie Clarke on JIRA:

Thanks for the update Timothy Brandt - I'll start taking a look at what you have so far.

stscijgbot-jp commented 3 days ago

Comment by Timothy Brandt on JIRA:

Updated notebook to reflect distinction between read noise, photon noise, flat field noise.  Updated background noise calculations.[^extract1d_framework 9-27.ipynb]

stscijgbot-jp commented 6 hours ago

Comment by Timothy Brandt on JIRA:

One more update to implement de-biasing approach to inverse variance weighting.

[^extract1d_framework 9-29.ipynb]

stscijgbot-jp commented 57 minutes ago

Comment by Melanie Clarke on JIRA:

Thanks again, Timothy Brandt . I think I now mostly understand your approach. 

It looks like what would be needed, in addition to this engine, is a method to make the 2D spatial profiles, either from a model for PSF-based fitting, or from input parameters/references specifying the box/background location(s).  I think Jane is working on the PSF end of that in JP-251, but we may need some additional effort to support the current use case, as well as integration with the step's front end.  The format you’re expecting for the input profiles looks fine to me, anyway.

A few comments and questions...

 

For data handling and formatting:

 

Questions about your assumptions:

 

For the debiasing approach for inverse variance weighting… I’m a little skeptical about how well your proposal will work. It seems error prone, given the multiple levels of fitting to the same input data, and given that read noise variance is not necessarily independent of the input flux either. But I agree we should discuss more – it might be worth trying.

stscijgbot-jp commented 12 minutes ago

Comment by Timothy Brandt on JIRA:

Thank you, Melanie Clarke.  Your suggestions all seem reasonable and straightforward to implement.  And yes, the (difficult) task is to actually derive these spatial profiles.  I can do this for the current case of polynomial interval boundaries once we are happy with this end of the implementation, since that it what should play nicely with future use cases.  For your questions:

Multiple sources with box extraction will be fine and unproblematic as long as they are well-separated.  It will be straightforward to generalize that step.  We could add a check that they are indeed non-overlapping (np.prod(np.array(profiles_2d), axis=0) == 0), with either a warning or an error if they do overlap.

The matrix inversions will be fine, since the matrices to invert have a size corresponding to the number of parameters to fit.  For a given column, that should never be more than a few.  The more expensive part is in the matrix multiplications to create the design matrix.  Even here, it scales linearly with the size of the region to extract.  If you are fitting a 2048x2048 matrix row-by-row, the total cost should be no more than a couple of seconds.  You can check this cost by increasing npixels_crossdisperse; it won't be problematic.  

And for the debiasing, I agree that my approach is a bit too simplistic.  I think it's probably much better than just using inverse-variance weighting (as is often implied in a PSF-based approach), but is far from perfect.  I think that the best approach, which would be very nearly correct, would probably rely on the ramp fitting itself, finding the photon and read noise that would have been reported if the count rate matched the model count rate.  I would favor leaving in something like I have now as a placeholder for doing things properly.