Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
6d05e1b
Added uncertainty propagation to `Background` module. Implemented tes…
hpparvi Jan 13, 2026
3180978
Improved variance estimation in `Background` when no uncertainty is p…
hpparvi Jan 13, 2026
646b821
Added tests for `StdDevUncertainty` and `InverseVariance` handling in…
hpparvi Jan 25, 2026
d4c190a
Corrected trace position calculation in `Background` by referencing `…
hpparvi Jan 25, 2026
caaf96e
Fixed `mask_treatment` docstrings in `Background`.
hpparvi Jan 25, 2026
6733ba1
Fixed incorrect axis reference in test for `Background` trace positio…
hpparvi Jan 25, 2026
04e3a80
Added sigma clipping to `Background` for outlier rejection, updated v…
hpparvi Jan 25, 2026
f712fdc
Updated the changelog.
hpparvi Jan 25, 2026
2d60ca0
Documented uncertainty propagation and outlier rejection in `Backgrou…
hpparvi Jan 25, 2026
db417b0
Trying to fix an RTD Sphinx issue.
hpparvi Jan 26, 2026
28f6156
Trying to fix an RTD Sphinx issue.
hpparvi Jan 26, 2026
bc08949
Continuing to try to fix an RTD Sphinx issue. Homogenized the docstri…
hpparvi Jan 26, 2026
9e342bc
Still continuing to try to fix an RTD Sphinx issue.
hpparvi Jan 26, 2026
bbbf020
Updated Python version to 3.14 in RTD configuration in the hope of fi…
hpparvi Jan 27, 2026
0fa17bb
Disabled `fail_on_warning` in RTD config to debug ongoing Sphinx buil…
hpparvi Jan 27, 2026
9de05de
Enabled `fail_on_warning` in RTD config and added `nitpick_ignore_reg…
hpparvi Jan 27, 2026
c0950ff
Added missing Sphinx extensions to `environment-dev.yml`.
hpparvi Jan 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .readthedocs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build:
apt_packages:
- graphviz
tools:
python: "3.13"
python: "3.14"

sphinx:
builder: html
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ New Features

- Added uncertainty propagation to ``specreduce.extract.BoxcarExtract`` and
``specreduce.extract.HorneExtract``. The extracted spectra have now proper uncertainties.
- Added uncertainty propagation to ``specreduce.background.Background``. The
``bkg_image()``, ``bkg_spectrum()``, ``sub_image()``, and ``sub_spectrum()`` methods
now return spectra with proper uncertainties. When input image has uncertainty, it is
propagated using variance formulas appropriate for the chosen statistic. When no
uncertainty is provided, it is estimated from the flux values in the background region.
- Added optional ``sigma`` parameter to ``specreduce.background.Background`` for sigma
clipping outlier rejection in background estimation. Default is 5.0; set to ``None``
to disable.

Other changes
^^^^^^^^^^^^^
Expand Down
39 changes: 38 additions & 1 deletion docs/background.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,41 @@ and the background-subtracted image via `~specreduce.background.Background.sub_i
(or ``image - bg``).

The background and trace steps can be done iteratively, to refine an automated
trace using the background-subtracted image as input.
trace using the background-subtracted image as input.

Outlier rejection
-----------------

The background estimation supports sigma clipping for outlier rejection, which is useful
for removing cosmic rays and other artifacts from the background region. The ``sigma``
parameter controls the number of standard deviations used for clipping (default is 5.0).
Set ``sigma=None`` to disable sigma clipping.

.. code-block:: python

# Use tighter sigma clipping for aggressive outlier rejection
bg = Background.two_sided(image, trace, separation=5, width=4, sigma=3.0)

# Disable sigma clipping
bg = Background.two_sided(image, trace, separation=5, width=4, sigma=None)

Uncertainty propagation
-----------------------

The `~specreduce.background.Background` class propagates uncertainties through
background subtraction. When the input image is an `~astropy.nddata.NDData` object
with ``image.uncertainty`` provided, the uncertainties are propagated using variance
formulas appropriate for the chosen statistic (``average`` or ``median``). When no
uncertainty is provided, it is estimated from the variance of flux values in the
background region.

The background image, background spectrum, and background-subtracted outputs all
include proper uncertainties:

.. code-block:: python

bg = Background.two_sided(image, trace, separation=5, width=4)

# Access background with uncertainty
bkg_spec = bg.bkg_spectrum()
print(bkg_spec.uncertainty)
11 changes: 11 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@
# ArrayLike from numpy.typing is not properly resolved by intersphinx
nitpick_ignore = [
("py:class", "ArrayLike"),
("py:class", "numpy._typing.ArrayLike"),
("py:class", "specutils.spectra.spectrum1d.Spectrum1D"),
Copy link
Contributor

Choose a reason for hiding this comment

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

is this because we're still referencing Spectrum1D or is it on the specutils end? either way, i think ignoring is the right approach for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fixes an issue I ran into while testing with specutils < 2. Then again, I believe we can finally pin specutils >= 2 for the next release. I'll clean up this and the remaining compatibility workarounds in a separate PR.

]

# Ignore complex type annotations that can't be cross-referenced
# These come from autodoc processing dataclass fields with Literal, Union, and generic types
nitpick_ignore_regex = [
(r"py:obj", r"typing\.Literal\[.*"), # Literal types with string values
(r"py:class", r"tuple\[.*"), # Generic tuple types
(r"py:class", r"None \| dict\[.*"), # Union types with dict
(r"py:class", r"dict\[.*"), # Generic dict types
]

# -- Options for linkcheck output -------------------------------------------
Expand Down
27 changes: 27 additions & 0 deletions docs/extraction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,33 @@ flux is scaled according to the effective number of unmasked pixels. When using
options (``filter`` or ``omit``), the non-finite values may be propagated or treated
differently as documented in the API.

Uncertainty propagation
-----------------------

Both `~specreduce.extract.BoxcarExtract` and `~specreduce.extract.HorneExtract`
propagate uncertainties from the input image to the extracted 1D spectrum.

For the input image, uncertainties can be provided in two ways:

1. As part of an `~astropy.nddata.NDData` object via ``image.uncertainty``
2. Via the ``variance`` parameter (for HorneExtract)

The extracted spectrum includes the propagated uncertainty, which can be accessed via
the ``uncertainty`` attribute:

.. code-block:: python

extract = BoxcarExtract(image, trace, width=5)
spectrum = extract.spectrum
print(spectrum.uncertainty)

For `~specreduce.extract.BoxcarExtract`, the uncertainty is propagated through the
weighted sum over the extraction aperture. For `~specreduce.extract.HorneExtract`,
the optimal extraction algorithm naturally produces properly weighted uncertainties.

Calling the extraction methods
------------------------------

The previous examples in this section show how to initialize the BoxcarExtract
or HorneExtract objects with their required parameters. To extract the 1D
spectrum
Expand Down
4 changes: 4 additions & 0 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ dependencies:
- photutils
- tox
- sphinx-astropy
- sphinx-copybutton
- sphinx-design
- nbsphinx
- pydata-sphinx-theme
- synphot
Loading