Skip to content

find_arc_lines uncertainty handling fix - #321

Open
hpparvi wants to merge 4 commits into
astropy:mainfrom
hpparvi:v110_find_lines_uncertainty_fix
Open

hpparvi wants to merge 4 commits into
astropy:mainfrom
hpparvi:v110_find_lines_uncertainty_fix

Conversation

@hpparvi

@hpparvi hpparvi commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

This PR fixes two issues in how the find_arc_lines handles uncertainties:

  1. the find_arc_lines function now allows uncertainties to be any of Astropy's uncertainty types (it allowed previously only StdDevUncertainty)

  2. If the spectrum doesn't include uncertainties, the uncertainties are estimated using a robust approach based on the DER_SNR algorithm by Stoerh et al. (2008). (with a small difference that we're using Astropy's sigma-clipped median instead of plain median).

I've also added the noise estimation function as specreduce.utils.utils.measure_noise.

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.

@hpparvi
hpparvi requested review from cshanahan1, kbwestfall, kecnry and tepickering and removed request for cshanahan1 September 7, 2026 14:11
@codecov

codecov Bot commented Sep 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.68%. Comparing base (333fb7b) to head (6bef783).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #321      +/-   ##
==========================================
+ Coverage   92.54%   92.68%   +0.13%     
==========================================
  Files          18       18              
  Lines        2348     2391      +43     
==========================================
+ Hits         2173     2216      +43     
  Misses        175      175              

☔ 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

python 3.11 still in CI when 3.12 is minimum required.

@hpparvi hpparvi added this to the v1.10 milestone Sep 17, 2026
@hpparvi
hpparvi force-pushed the v110_find_lines_uncertainty_fix branch from fa7adef to c9bc1b9 Compare September 23, 2026 09:07
@tepickering

Copy link
Copy Markdown
Contributor

Shouldn't specreduce.utils.utils.measure_noise be specreduce.utils.measure_noise? It is in the CHANGES.rst...

@tepickering tepickering left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The points I raised in specreduce/utils/utils.py are pretty significant and should be dealt with, though it may be an upstream issue as well. Definitely needs a deeper look. Once that's sorted, measure_noise should operate on the masked data. The docs issue is minor, but worth resolving.

As a heads-up: this PR and #320 both rewrite the "no uncertainty" fallback block in find_arc_lines, so whichever merges second will need a rebase. They complement each other: the second-difference noise estimate here doesn't depend on the baseline, which suits the baseline subtraction in #320. Do you have a preferred merge order?

Comment thread specreduce/utils/utils.py Outdated
Comment on lines +81 to +83
_, _, clipped_std = sigma_clipped_stats(
diff2, mask=invalid, sigma=sigma, maxiters=maxiters, stdfunc=mad_std, axis=-1
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's an issue here with NaN or masked input. With axis=-1 and stdfunc=mad_std, sigma_clipped_stats (astropy 8.0.1) returns the plain unclipped np.nanstd as soon as anything is masked or non-finite. A synthetic arc with 4 Gaussian lines plus σ = 2 noise:

input measure_noise
clean 2.12
one pixel set to NaN 25.9
three pixels masked (masked array) 26.1

The same data with axis=None gives 2.10, so it's specific to the axis path. test_nan_pixels_are_ignored doesn't catch it, I think because it's line-free noise, where clipping makes no difference. In find_arc_lines, a single NaN pixel in a spectrum without uncertainties would raise the detection threshold about 12×.

A per-row loop over the finite values avoids it, something like (untested):

rows = diff2.reshape(-1, diff2.shape[-1])
noise = np.full(rows.shape[0], np.nan)
for i, row in enumerate(rows):
    row = row[np.isfinite(row)]
    if row.size > 0:
        noise[i] = sigma_clipped_stats(
            row, sigma=sigma, maxiters=maxiters, stdfunc=mad_std
        )[2]
noise = noise.reshape(diff2.shape[:-1]) / np.sqrt(6.0)

It would also be good for the NaN test to include a few strong lines so it exercises the clipping. This might be worth reporting upstream to astropy as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed, the cause was the callable mad_std combined with axis and a mask. The string form stdfunc="mad_std" uses astropy's NaN-aware path and clips correctly.

Comment thread specreduce/utils/utils.py Outdated
_, _, clipped_std = sigma_clipped_stats(
diff2, mask=invalid, sigma=sigma, maxiters=maxiters, stdfunc=mad_std, axis=-1
)
noise = clipped_std / np.sqrt(6.0)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The MAD collapses to 0 whenever more than half of the second differences are identical, and then every positive pixel passes the detection threshold. Some cases I tried through find_arc_lines (no input uncertainty):

  • Low-count integer Poisson arc (true σ ≈ 0.45): 51% of the differences are exactly 0, so the noise is 0 and the result is 73 "lines", some with centroids outside the frame (e.g. −24.3).
  • Spectrum zero-padded over 60% of its length: noise 0, 47 junk lines.
  • Noiseless synthetic arc on a 100-count pedestal: noise 0, 1 line found out of 4.
  • All-NaN spectrum: noise nan, 0 lines, and no error.

The raw-count case is a regression compared with the old sqrt(|flux|) fallback. Maybe fall back to the clipped standard deviation when the MAD is 0 (e.g. rerun with the default stdfunc="std" for those rows), and have find_arc_lines raise a clear error if the estimate is still non-finite or ≤ 0?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed. I tried the iterated clipped standard deviation, but it also converges to 0 for sparse counts, so the fallback is the standard deviation after one clipping pass, which is biased high rather than low. find_arc_lines now raises a ValueError if the estimate is non-finite or ≤ 0.

Comment thread specreduce/line_matching.py Outdated
if spectrum.uncertainty is None:
spectrum = deepcopy(spectrum)
spectrum.uncertainty = StdDevUncertainty(np.sqrt(np.abs(spectrum.flux.value)))
noise = measure_noise(spectrum.flux.value)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This ignores spectrum.mask, so masked pixels (cosmic rays, bad columns) still go into the noise estimate. measure_noise already handles masked arrays, so this could be:

flux = spectrum.flux.value
if spectrum.mask is not None:
    flux = np.ma.masked_array(flux, mask=spectrum.mask)
noise = measure_noise(flux)

This only helps once the masked-input issue in measure_noise (comment on utils.py) is fixed; at the moment, adding a mask makes the estimate worse.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, the noise is now estimated from the unmasked pixels.

Comment thread specreduce/line_matching.py Outdated
(`~astropy.nddata.StdDevUncertainty`, `~astropy.nddata.VarianceUncertainty`, or
`~astropy.nddata.InverseVariance`); it is converted to a standard deviation
before the line finding. If the spectrum has no uncertainty, a constant per-pixel
noise is estimated from the data with `~specreduce.utils.utils.measure_noise`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

specreduce.utils.utils isn't in docs/api.rst (only specreduce.utils.synth_data is), so this cross-reference won't resolve. With nitpicky = True that gives a docs warning, and measure_noise won't appear in the API docs at all. Could you add an automodapi entry for it?

.. automodapi:: specreduce.utils.utils
    :no-inheritance-diagram:

The module's __all__ also exposes the private _align_along_trace, so you might want :include: measure_noise, measure_cross_dispersion_profile (or similar) to keep that out of the docs.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Documented as specreduce.utils in api.rst, and the references now use specreduce.utils.measure_noise consistently. This required fixing the backticks in the measure_cross_dispersion_profile docstring.

… estimate noise in a 2D spectrum. This is for now used only in `specreduce.line_matching.find_arc_lines` as a fallback noise estimate if the spectrum doesn't contain uncertainties, but may be useful elsewhere as well.
For a spectrum given without an uncertainty, the assumed Poisson
noise vanishes for the pixels of a background-subtracted spectrum
that lie close to zero, so they passed any detection threshold. The
noise is now limited from below by the sigma-clipped scatter of the
unmasked flux.

Added a test for a background-subtracted spectrum without an
uncertainty, and added a changelog entry.

Relaxed the running-baseline check in the tilt correction test to
require 80% of the reference lines within the tolerance, because a
blended or marginal line can be fitted differently than with the
global baseline.
@hpparvi
hpparvi force-pushed the v110_find_lines_uncertainty_fix branch from c9bc1b9 to 9cc79eb Compare November 18, 2026 10:15
@hpparvi

hpparvi commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @tepickering! Feel free to take a look again anytime you have a moment.

@hpparvi
hpparvi requested a review from tepickering November 18, 2026 10:31
Rows whose second differences are all non-finite or masked are now
left as NaN and never passed to sigma_clipped_stats, whose
behaviour for fully masked rows depends on the astropy version.

The differences are flattened into a 2D stack of rows, and the
clipped scatter is computed only for the rows that need it, also in
the fallback for a zero median absolute deviation.
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