Skip to content

Fix and improve HorneExtract - #322

Open
hpparvi wants to merge 5 commits into
astropy:mainfrom
hpparvi:v110_horne_extraction_improvements
Open

hpparvi wants to merge 5 commits into
astropy:mainfrom
hpparvi:v110_horne_extraction_improvements

Conversation

@hpparvi

@hpparvi hpparvi commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

This PR fixes a number of accuracy issues in HorneExtract and makes it follow the trace at sub-pixel precision.

Bug fixes

  • Non-flat traces: the flux was rolled to align the trace with the central row, but the variance and mask arrays were not, so the extraction weights came from the wrong pixels. This biased the flux and its uncertainty when variances were non-uniform or when pixels were masked.

  • Low flux bias with data-derived variances: weighting by the Poisson variance of the observed counts underweights upward fluctuations and biases the flux low by roughly the inverse of the counts per pixel. Following Horne, the variances are now re-estimated from the extraction model before the final extraction. The new model_variance argument (default True) controls this.

  • Extraction kernel: the kernel is now the fitted Gaussian alone. The background model no longer leaks into the kernel and its normalisation, which could drive the flux towards zero far from the source.

  • bkgrd_prof=None now fits the profile without a background model instead of silently substituting the default polynomial.

Improvements

  • Sub-pixel trace following: the kernel is evaluated directly on the image grid at each pixel's offset from the trace instead of rolling the image by whole pixels. The profile is fit in trace-relative coordinates without resampling (Horne 1986, Sect. II.A).

  • window argument restricts the profile fit and extraction to pixels near the trace, keeping other sources on the slit out of the fit.

  • Vectorised the kernel evaluation, which makes the extraction about three times faster.

AI/LLM Disclaimer: The same as in #319. I've used Claude Code with Fable 5.1 to co-develop the PR, but I understand what the code does and how it works, and can explain it when needed.

@codecov

codecov Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.05882% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.61%. Comparing base (4e330bc) to head (3126944).

Files with missing lines Patch % Lines
specreduce/extract.py 97.05% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #322      +/-   ##
==========================================
+ Coverage   92.52%   92.61%   +0.08%     
==========================================
  Files          18       18              
  Lines        2341     2396      +55     
==========================================
+ Hits         2166     2219      +53     
- Misses        175      177       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tepickering

Copy link
Copy Markdown
Contributor

looks like CI is only passing under python 3.14.

@hpparvi

hpparvi commented Sep 15, 2026

Copy link
Copy Markdown
Contributor Author

Ohps, true... I'll take a look into this tomorrow morning.

@tepickering

Copy link
Copy Markdown
Contributor

also, see my other comments about 3.11 still in CI when 3.12 is now the minimum. should fix that in a separate PR and then rebase these once that's merged.

@hpparvi hpparvi added this to the v1.10 milestone Sep 17, 2026
…ask arrays were not aligned along with the trace correctly.
…ed from the model.

- Changed the Horne extraction kernel to be the fitted Gaussian alone.
- Changed the default behavior of ``HorneExtract(bkgrd_prof=None)``. This now fits the Gaussian profile without a background model.
…e kernel is now evaluated directly on the image grid at each pixel's offset from the trace instead of rolling the image by whole pixels.

- Changed the spatial profile fit to work in trace-relative coordinates without resampling the image (Horne 1986, Sect. II.A).

- Added a ``window`` argument to HorneExtract that restricts the profile fit and the extraction to pixels near the trace.

- Vectorised the kernel evaluation, making the extraction about three times faster.
@hpparvi
hpparvi force-pushed the v110_horne_extraction_improvements branch from 449c8d5 to eee2bd1 Compare September 23, 2026 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants