From bf890c14f45deffcbe65512456b17bfb3d60b1c3 Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Sun, 6 Sep 2026 20:27:02 +0100 Subject: [PATCH 1/4] - Fixed `find_arc_lines` to accept any of the Astropy's uncertainty types. --- CHANGES.rst | 10 +++++ specreduce/line_matching.py | 16 +++++++- specreduce/tests/test_line_matching.py | 54 +++++++++++++++++++++++++- 3 files changed, 77 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 67809767..26c238b5 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -38,6 +38,16 @@ API Changes on instantiation, and its removal has been rescheduled from v2.0 to v1.11. Use ``specreduce.wavecal1d.WavelengthCalibration1D`` instead. [#316] +Bug Fixes +^^^^^^^^^ + +- ``line_matching.find_arc_lines`` now accepts spectra with any of the Astropy + uncertainty types (``StdDevUncertainty``, ``VarianceUncertainty``, or + ``InverseVariance``). Non-standard-deviation uncertainties are converted to + ``StdDevUncertainty`` on a copy of the spectrum before the line finding, which + previously failed with a unit conversion error for variance-type uncertainties. + [#XXX] + Other changes ^^^^^^^^^^^^^ diff --git a/specreduce/line_matching.py b/specreduce/line_matching.py index f8f1b807..423f7497 100644 --- a/specreduce/line_matching.py +++ b/specreduce/line_matching.py @@ -29,8 +29,13 @@ def find_arc_lines( Parameters ---------- - spectrum : The extracted arc spectrum to search for lines. It should be background-subtracted - and must have an "uncertainty" attribute. + spectrum + The extracted arc spectrum to search for lines. It should be background-subtracted. + The uncertainty can be any of the Astropy uncertainty types + (`~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, the square root of + the absolute flux is used as an estimate. fwhm Estimated full-width half-maximum of the lines in pixels. @@ -55,9 +60,16 @@ def find_arc_lines( if fwhm.unit != spectrum.spectral_axis.unit: raise ValueError("fwhm must have the same units as spectrum.spectral_axis.") + # The line finding and fitting are always done using standard deviation uncertainties. + # If the spectrum has no uncertainty, estimate it as the square root of the flux. + # If it has a variance or inverse variance uncertainty, convert it to a standard + # deviation. Either way, work on a copy so that the input spectrum is left untouched. if spectrum.uncertainty is None: spectrum = deepcopy(spectrum) spectrum.uncertainty = StdDevUncertainty(np.sqrt(np.abs(spectrum.flux.value))) + elif not isinstance(spectrum.uncertainty, StdDevUncertainty): + spectrum = deepcopy(spectrum) + spectrum.uncertainty = spectrum.uncertainty.represent_as(StdDevUncertainty) detected_lines = find_lines_threshold(spectrum, noise_factor=noise_factor) detected_lines = detected_lines[detected_lines["line_type"] == "emission"] diff --git a/specreduce/tests/test_line_matching.py b/specreduce/tests/test_line_matching.py index a96d0675..9c229fcc 100644 --- a/specreduce/tests/test_line_matching.py +++ b/specreduce/tests/test_line_matching.py @@ -4,7 +4,7 @@ from astropy.wcs import WCS from astropy.modeling import models -from astropy.nddata import StdDevUncertainty +from astropy.nddata import StdDevUncertainty, VarianceUncertainty, InverseVariance from specutils.fitting import fit_generic_continuum from specreduce.calibration_data import load_pypeit_calibration_lines @@ -100,6 +100,58 @@ def test_find_arc_lines(mk_test_data): assert len(lines) > 1 +@pytest.fixture +def synthetic_arc(): + """ + A small synthetic arc spectrum with Gaussian emission lines at known pixel positions + and a constant standard deviation of 3 DN per pixel. + """ + rng = np.random.default_rng(42) + x = np.arange(500.0) + centers = [50.0, 120.0, 210.0, 330.0, 410.0] + flux = np.zeros_like(x) + for c, a in zip(centers, [200.0, 80.0, 500.0, 60.0, 150.0]): + flux += a * np.exp(-0.5 * ((x - c) / 2.1) ** 2) + sigma = 3.0 + flux += rng.normal(0.0, sigma, x.size) + return x * u.pix, flux * u.DN, np.full(x.size, sigma) + + +@pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") +@pytest.mark.filterwarnings("ignore:Spectrum is not below the threshold") +@pytest.mark.parametrize( + "uncertainty_cls, transform", + [ + (StdDevUncertainty, lambda s: s), + (VarianceUncertainty, lambda s: s**2), + (InverseVariance, lambda s: 1.0 / s**2), + ], +) +def test_find_arc_lines_uncertainty_types(synthetic_arc, uncertainty_cls, transform): + """ + find_arc_lines must accept any of the three astropy uncertainty types and produce + the same lines as it does for an equivalent StdDevUncertainty. + """ + spectral_axis, flux, sigma = synthetic_arc + reference = Spectrum( + flux=flux, spectral_axis=spectral_axis, uncertainty=StdDevUncertainty(sigma) + ) + spectrum = Spectrum( + flux=flux, spectral_axis=spectral_axis, uncertainty=uncertainty_cls(transform(sigma)) + ) + + expected = find_arc_lines(reference, fwhm=5, window=3, noise_factor=5) + lines = find_arc_lines(spectrum, fwhm=5, window=3, noise_factor=5) + + assert len(expected) >= 5 + assert len(lines) == len(expected) + np.testing.assert_allclose(lines["centroid"].value, expected["centroid"].value) + np.testing.assert_allclose(lines["fwhm"].value, expected["fwhm"].value) + np.testing.assert_allclose(lines["amplitude"].value, expected["amplitude"].value) + # The input spectrum must not be modified in place. + assert isinstance(spectrum.uncertainty, uncertainty_cls) + + @pytest.mark.remote_data @pytest.mark.filterwarnings("ignore:No observer defined on WCS") @pytest.mark.filterwarnings("ignore:Model is linear in parameters") From 538ca06c39a33c57f13dd48c00299a24d4bcb027 Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Sun, 6 Sep 2026 21:24:38 +0100 Subject: [PATCH 2/4] =?UTF-8?q?-=20Added=20a=20=C2=B4specreduce.utils.util?= =?UTF-8?q?s.measure=5Fnoise=C2=B4=20function=20to=20robustly=20estimate?= =?UTF-8?q?=20noise=20in=20a=202D=20spectrum.=20This=20is=20for=20now=20us?= =?UTF-8?q?ed=20only=20in=20`specreduce.line=5Fmatching.find=5Farc=5Flines?= =?UTF-8?q?`=20as=20a=20fallback=20noise=20estimate=20if=20the=20spectrum?= =?UTF-8?q?=20doesn't=20contain=20uncertainties,=20but=20may=20be=20useful?= =?UTF-8?q?=20elsewhere=20as=20well.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGES.rst | 13 ++++ specreduce/line_matching.py | 16 +++-- specreduce/tests/test_line_matching.py | 21 ++++++- specreduce/tests/test_utils.py | 56 ++++++++++++++++- specreduce/utils/utils.py | 83 +++++++++++++++++++++++++- 5 files changed, 179 insertions(+), 10 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 26c238b5..74b4bc87 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -30,6 +30,12 @@ New Features (``AWAV-GRA``) and vacuum (``WAVE-GRI``) spectral axis types in FITS WCS export. [#316] +- Added ``specreduce.utils.measure_noise``, a robust estimator of the per-pixel + noise standard deviation of a 1D spectrum or of each row of a 2D spectral + image. It measures the sigma-clipped ``mad_std`` of the second difference of + the flux along the dispersion axis, so it is insensitive to a smooth continuum + or residual background and to emission lines. [#XXX] + API Changes ^^^^^^^^^^^ @@ -48,6 +54,13 @@ Bug Fixes previously failed with a unit conversion error for variance-type uncertainties. [#XXX] +- ``line_matching.find_arc_lines`` now estimates the noise of a spectrum without + an uncertainty using ``measure_noise`` instead of the square root of the absolute + flux. The old fallback amounted to a fixed detection threshold of + ``noise_factor**2`` flux units regardless of the actual noise, which missed + all lines in faint spectra and reported noise spikes as lines in noisy or + background-subtracted ones. [#XXX] + Other changes ^^^^^^^^^^^^^ diff --git a/specreduce/line_matching.py b/specreduce/line_matching.py index 423f7497..e774a60e 100644 --- a/specreduce/line_matching.py +++ b/specreduce/line_matching.py @@ -14,6 +14,8 @@ from specutils import Spectrum +from specreduce.utils.utils import measure_noise + __all__ = ["find_arc_lines", "match_lines_wcs"] @@ -34,8 +36,8 @@ def find_arc_lines( The uncertainty can be any of the Astropy uncertainty types (`~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, the square root of - the absolute flux is used as an estimate. + 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`. fwhm Estimated full-width half-maximum of the lines in pixels. @@ -61,12 +63,14 @@ def find_arc_lines( raise ValueError("fwhm must have the same units as spectrum.spectral_axis.") # The line finding and fitting are always done using standard deviation uncertainties. - # If the spectrum has no uncertainty, estimate it as the square root of the flux. - # If it has a variance or inverse variance uncertainty, convert it to a standard - # deviation. Either way, work on a copy so that the input spectrum is left untouched. + # If the spectrum has no uncertainty, estimate a constant per-pixel noise from the + # scatter in the data itself. If it has a variance or inverse variance uncertainty, + # convert it to a standard deviation. Either way, work on a copy so that the input + # spectrum is left untouched. if spectrum.uncertainty is None: spectrum = deepcopy(spectrum) - spectrum.uncertainty = StdDevUncertainty(np.sqrt(np.abs(spectrum.flux.value))) + noise = measure_noise(spectrum.flux.value) + spectrum.uncertainty = StdDevUncertainty(np.full(spectrum.flux.shape, noise)) elif not isinstance(spectrum.uncertainty, StdDevUncertainty): spectrum = deepcopy(spectrum) spectrum.uncertainty = spectrum.uncertainty.represent_as(StdDevUncertainty) diff --git a/specreduce/tests/test_line_matching.py b/specreduce/tests/test_line_matching.py index 9c229fcc..37e64c93 100644 --- a/specreduce/tests/test_line_matching.py +++ b/specreduce/tests/test_line_matching.py @@ -114,7 +114,7 @@ def synthetic_arc(): flux += a * np.exp(-0.5 * ((x - c) / 2.1) ** 2) sigma = 3.0 flux += rng.normal(0.0, sigma, x.size) - return x * u.pix, flux * u.DN, np.full(x.size, sigma) + return x * u.pix, flux * u.DN, np.full(x.size, sigma), np.array(centers) @pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") @@ -132,7 +132,7 @@ def test_find_arc_lines_uncertainty_types(synthetic_arc, uncertainty_cls, transf find_arc_lines must accept any of the three astropy uncertainty types and produce the same lines as it does for an equivalent StdDevUncertainty. """ - spectral_axis, flux, sigma = synthetic_arc + spectral_axis, flux, sigma, _ = synthetic_arc reference = Spectrum( flux=flux, spectral_axis=spectral_axis, uncertainty=StdDevUncertainty(sigma) ) @@ -152,6 +152,23 @@ def test_find_arc_lines_uncertainty_types(synthetic_arc, uncertainty_cls, transf assert isinstance(spectrum.uncertainty, uncertainty_cls) +@pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") +@pytest.mark.filterwarnings("ignore:Spectrum is not below the threshold") +@pytest.mark.parametrize("scale", [1.0 / 30.0, 10.0 / 3.0], ids=["faint", "noisy"]) +def test_find_arc_lines_estimates_noise_without_uncertainty(synthetic_arc, scale): + """ + Without an uncertainty, find_arc_lines must estimate the noise from the data so that + the detection threshold follows the actual noise level, independent of the flux scale. + """ + spectral_axis, flux, _, centers = synthetic_arc + spectrum = Spectrum(flux=flux * scale, spectral_axis=spectral_axis) + + lines = find_arc_lines(spectrum, fwhm=5, window=3, noise_factor=5) + + assert len(lines) == len(centers) + np.testing.assert_allclose(np.sort(lines["centroid"].value), centers, atol=0.5) + + @pytest.mark.remote_data @pytest.mark.filterwarnings("ignore:No observer defined on WCS") @pytest.mark.filterwarnings("ignore:Model is linear in parameters") diff --git a/specreduce/tests/test_utils.py b/specreduce/tests/test_utils.py index 43408603..7cbbed6e 100644 --- a/specreduce/tests/test_utils.py +++ b/specreduce/tests/test_utils.py @@ -6,7 +6,7 @@ from specutils import Spectrum from specreduce.tracing import FitTrace -from specreduce.utils.utils import measure_cross_dispersion_profile +from specreduce.utils.utils import measure_cross_dispersion_profile, measure_noise def mk_gaussian_img(nrows=20, ncols=16, mean=10, stddev=4): @@ -169,3 +169,57 @@ def test_errors_warnings(self): 'or None to use all ' 'cross-dispersion pixels.'): measure_cross_dispersion_profile(img, width='.') + + +class TestMeasureNoise: + """Tests for the robust second-difference noise estimator.""" + + @staticmethod + def _arc(rng, x, centers, amplitudes, fwhm=4.0): + sigma = fwhm / 2.3548 + flux = np.zeros_like(x) + for c, a in zip(centers, amplitudes): + flux += a * np.exp(-0.5 * ((x - c) / sigma) ** 2) + return flux + + def test_white_noise(self): + rng = np.random.default_rng(1) + flux = rng.normal(0.0, 3.0, 2000) + assert measure_noise(flux) == pytest.approx(3.0, rel=0.05) + + def test_lines_and_residual_background(self): + rng = np.random.default_rng(2) + x = np.arange(2000.0) + flux = self._arc(rng, x, rng.uniform(0, 2000, 40), 10 ** rng.uniform(1, 3.3, 40)) + flux += 120.0 * (x / 2000.0 - 0.5) ** 2 + flux += rng.normal(0.0, 3.0, x.size) + noise = measure_noise(flux) + assert 0.9 * 3.0 < noise < 1.3 * 3.0 + + def test_2d_per_row_along_dispersion_axis(self): + rng = np.random.default_rng(3) + sigmas = np.array([1.0, 2.0, 4.0, 8.0]) + flux = rng.normal(0.0, sigmas[:, None], (4, 1000)) + noise = measure_noise(flux, axis=-1) + assert noise.shape == (4,) + np.testing.assert_allclose(noise, sigmas, rtol=0.1) + noise_t = measure_noise(flux.T, axis=0) + np.testing.assert_allclose(noise_t, noise) + + def test_quantity_input_keeps_unit(self): + rng = np.random.default_rng(4) + flux = rng.normal(0.0, 3.0, 2000) * u.DN + noise = measure_noise(flux) + assert isinstance(noise, u.Quantity) + assert noise.unit == u.DN + assert noise.value == pytest.approx(3.0, rel=0.05) + + def test_nan_pixels_are_ignored(self): + rng = np.random.default_rng(5) + flux = rng.normal(0.0, 3.0, 2000) + flux[100:110] = np.nan + assert measure_noise(flux) == pytest.approx(3.0, rel=0.05) + + def test_too_short_input_raises(self): + with pytest.raises(ValueError, match="at least 5"): + measure_noise(np.zeros(4)) diff --git a/specreduce/utils/utils.py b/specreduce/utils/utils.py index 3886f90a..513074e0 100644 --- a/specreduce/utils/utils.py +++ b/specreduce/utils/utils.py @@ -1,10 +1,91 @@ import numpy as np +from astropy import units as u +from astropy.stats import mad_std, sigma_clipped_stats from specreduce.core import parse_image from specreduce.tracing import Trace, FlatTrace from specreduce.extract import _ap_weight_image, _align_along_trace -__all__ = ['measure_cross_dispersion_profile', '_align_along_trace'] +__all__ = ['measure_cross_dispersion_profile', 'measure_noise', '_align_along_trace'] + + +def measure_noise( + data: np.ndarray | u.Quantity, + axis: int = -1, + sigma: float = 3.0, + maxiters: int | None = 10, +) -> float | np.ndarray | u.Quantity: + """ + Estimate the per-pixel noise standard deviation of a spectrum from the data itself. + + The estimate is the sigma-clipped median absolute deviation of the second difference + of the flux along the dispersion axis, ``2 f[i] - f[i-2] - f[i+2]``, scaled to the + standard deviation of a single pixel. Differencing removes any smooth continuum or + residual background, so the estimate does not depend on the spectrum being + background-subtracted, while the iterative sigma clipping removes the pixels + dominated by emission or absorption lines before the scatter is measured. + + The second difference of white noise has a variance of six times the per-pixel + variance, so the clipped ``mad_std`` of the differences is divided by the square + root of six. This is the same differencing used by the DER_SNR algorithm + (Stoehr et al. 2008), with the plain median replaced by a sigma-clipped robust + standard deviation to reduce the bias from dense line lists. + + Parameters + ---------- + data + The flux array. Can be 1D or N-dimensional; for a 2D spectral image the noise + is estimated separately along ``axis`` for each row (or column). Non-finite + values and masked elements of a masked array are ignored. + axis + The dispersion axis along which the differences are taken. + sigma + The clipping threshold in units of the robust standard deviation. + maxiters + The maximum number of clipping iterations, or `None` to iterate until + convergence. + + Returns + ------- + float, ndarray, or Quantity + The estimated noise standard deviation. A scalar for 1D input, otherwise an + array with ``axis`` removed. If ``data`` is a `~astropy.units.Quantity`, the + result carries the same unit. + + Notes + ----- + The estimator assumes that the noise is uncorrelated between pixels two apart. + For spectra that have been smoothed or resampled onto a finer grid, the + differencing suppresses part of the correlated noise and the result is biased + low by up to a few tens of percent. + """ + unit = None + if isinstance(data, u.Quantity): + unit = data.unit + data = data.value + if np.ma.isMaskedArray(data): + data = data.astype(float).filled(np.nan) + values = np.asarray(data, dtype=float) + + if values.ndim == 0 or values.shape[axis] < 5: + raise ValueError("measure_noise requires at least 5 pixels along the dispersion axis.") + + values = np.moveaxis(values, axis, -1) + diff2 = 2.0 * values[..., 2:-2] - values[..., :-4] - values[..., 4:] + + # Non-finite differences (from NaN or masked pixels) are excluded through an explicit + # mask. The placeholder value is never used, but must be finite to keep astropy from + # warning about invalid input. + invalid = ~np.isfinite(diff2) + diff2 = np.where(invalid, 0.0, diff2) + _, _, clipped_std = sigma_clipped_stats( + diff2, mask=invalid, sigma=sigma, maxiters=maxiters, stdfunc=mad_std, axis=-1 + ) + noise = clipped_std / np.sqrt(6.0) + + if unit is not None: + noise = noise * unit + return noise def measure_cross_dispersion_profile(image, trace=None, crossdisp_axis=0, From 9cc79ebc0f6889f9046429d3dfe98c2d7bc1751b Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Wed, 18 Nov 2026 09:43:07 +0000 Subject: [PATCH 3/4] Fixed `find_arc_lines` detecting spurious lines without an uncertainty. 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. --- CHANGES.rst | 13 ++-- docs/api.rst | 3 + specreduce/line_matching.py | 18 +++++- specreduce/tests/test_line_matching.py | 47 ++++++++++++++ specreduce/tests/test_utils.py | 52 ++++++++++++++++ specreduce/utils/utils.py | 86 +++++++++++++++++--------- 6 files changed, 183 insertions(+), 36 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 74b4bc87..7c3c8243 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -32,9 +32,10 @@ New Features - Added ``specreduce.utils.measure_noise``, a robust estimator of the per-pixel noise standard deviation of a 1D spectrum or of each row of a 2D spectral - image. It measures the sigma-clipped ``mad_std`` of the second difference of - the flux along the dispersion axis, so it is insensitive to a smooth continuum - or residual background and to emission lines. [#XXX] + image. It measures the sigma-clipped standard deviation of the second difference + of the flux along the dispersion axis, so it is insensitive to a smooth continuum + or residual background and to emission lines. Non-finite and masked pixels are + ignored. [#321] API Changes ^^^^^^^^^^^ @@ -52,14 +53,16 @@ Bug Fixes ``InverseVariance``). Non-standard-deviation uncertainties are converted to ``StdDevUncertainty`` on a copy of the spectrum before the line finding, which previously failed with a unit conversion error for variance-type uncertainties. - [#XXX] + [#321] - ``line_matching.find_arc_lines`` now estimates the noise of a spectrum without an uncertainty using ``measure_noise`` instead of the square root of the absolute flux. The old fallback amounted to a fixed detection threshold of ``noise_factor**2`` flux units regardless of the actual noise, which missed all lines in faint spectra and reported noise spikes as lines in noisy or - background-subtracted ones. [#XXX] + background-subtracted ones. Pixels flagged in the spectrum mask are excluded from + the estimate, and a ``ValueError`` is raised if the noise cannot be estimated + (constant spectrum, or no finite unmasked pixels). [#321] Other changes ^^^^^^^^^^^^^ diff --git a/docs/api.rst b/docs/api.rst index f42b5183..c70fce23 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -9,6 +9,9 @@ API Reference .. automodapi:: specreduce.core :no-inheritance-diagram: +.. automodapi:: specreduce.utils + :no-inheritance-diagram: + .. automodapi:: specreduce.utils.synth_data :no-inheritance-diagram: diff --git a/specreduce/line_matching.py b/specreduce/line_matching.py index e774a60e..2938f7ac 100644 --- a/specreduce/line_matching.py +++ b/specreduce/line_matching.py @@ -37,7 +37,7 @@ def find_arc_lines( (`~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`. + noise is estimated from the unmasked pixels with `~specreduce.utils.measure_noise`. fwhm Estimated full-width half-maximum of the lines in pixels. @@ -53,6 +53,12 @@ def find_arc_lines( ------- QTable A table of detected arc lines and their properties: centroid, fwhm, and amplitude. + + Raises + ------ + ValueError + If the spectrum has no uncertainty and its noise cannot be estimated from the + flux, as for a constant spectrum or one without finite unmasked pixels. """ # If fwhm is a float, convert it to a Quantity with the same unit as the spectral axis # of the input spectrum. @@ -68,8 +74,16 @@ def find_arc_lines( # convert it to a standard deviation. Either way, work on a copy so that the input # spectrum is left untouched. if spectrum.uncertainty is None: + flux = spectrum.flux.value + if spectrum.mask is not None: + flux = np.ma.masked_array(flux, mask=spectrum.mask) + noise = measure_noise(flux) + if not (np.isfinite(noise) and noise > 0): + raise ValueError( + "Could not estimate the noise of the spectrum from its flux " + f"(estimate: {noise}). Give the spectrum an uncertainty." + ) spectrum = deepcopy(spectrum) - noise = measure_noise(spectrum.flux.value) spectrum.uncertainty = StdDevUncertainty(np.full(spectrum.flux.shape, noise)) elif not isinstance(spectrum.uncertainty, StdDevUncertainty): spectrum = deepcopy(spectrum) diff --git a/specreduce/tests/test_line_matching.py b/specreduce/tests/test_line_matching.py index 37e64c93..04ef891e 100644 --- a/specreduce/tests/test_line_matching.py +++ b/specreduce/tests/test_line_matching.py @@ -10,6 +10,7 @@ from specreduce.calibration_data import load_pypeit_calibration_lines from specutils import Spectrum from specreduce.extract import BoxcarExtract +from specreduce import line_matching from specreduce.line_matching import match_lines_wcs, find_arc_lines from specreduce.tracing import FlatTrace from specreduce.utils.synth_data import SynthImage @@ -169,6 +170,52 @@ def test_find_arc_lines_estimates_noise_without_uncertainty(synthetic_arc, scale np.testing.assert_allclose(np.sort(lines["centroid"].value), centers, atol=0.5) +@pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") +@pytest.mark.filterwarnings("ignore:Spectrum is not below the threshold") +def test_find_arc_lines_noise_estimate_ignores_masked_pixels(synthetic_arc, mocker): + """The noise must be estimated from the unmasked pixels of the spectrum only.""" + spectral_axis, flux, _, _ = synthetic_arc + mask = np.zeros(flux.size, dtype=bool) + mask[250:300] = True + spy = mocker.spy(line_matching, "measure_noise") + + find_arc_lines(Spectrum(flux=flux, spectral_axis=spectral_axis, mask=mask), fwhm=5) + + data = spy.call_args.args[0] + assert np.ma.isMaskedArray(data) + np.testing.assert_array_equal(data.mask, mask) + + +@pytest.mark.filterwarnings("ignore:The fit may be unsuccessful") +@pytest.mark.filterwarnings("ignore:Spectrum is not below the threshold") +@pytest.mark.filterwarnings("ignore:invalid value encountered in divide") +def test_find_arc_lines_low_count_spectrum_without_uncertainty(): + """ + Integer data with less than a count per pixel has a zero median absolute deviation; + the noise estimate must not vanish and turn every positive pixel into a line. + """ + # The specutils continuum check divides the zero median by the zero MAD of the flux. + rng = np.random.default_rng(3) + x = np.arange(1000.0) + centers = np.array([150.0, 400.0, 620.0, 850.0]) + expected = 0.2 + sum(40.0 * np.exp(-0.5 * ((x - c) / 2.0) ** 2) for c in centers) + spectrum = Spectrum(flux=rng.poisson(expected).astype(float) * u.ct, spectral_axis=x * u.pix) + + lines = find_arc_lines(spectrum, fwhm=4.7) + + # A noisy peak can be detected twice, so compare the sets of positions. + distances = np.abs(lines["centroid"].value[:, None] - centers) + assert (distances.min(axis=1) < 0.5).all() + assert (distances.min(axis=0) < 0.5).all() + + +@pytest.mark.parametrize("value", [np.nan, 100.0], ids=["all_nan", "constant"]) +def test_find_arc_lines_raises_if_noise_cannot_be_estimated(value): + spectrum = Spectrum(flux=np.full(200, value) * u.ct, spectral_axis=np.arange(200) * u.pix) + with pytest.raises(ValueError, match="Could not estimate the noise"): + find_arc_lines(spectrum, fwhm=5) + + @pytest.mark.remote_data @pytest.mark.filterwarnings("ignore:No observer defined on WCS") @pytest.mark.filterwarnings("ignore:Model is linear in parameters") diff --git a/specreduce/tests/test_utils.py b/specreduce/tests/test_utils.py index 7cbbed6e..a9567685 100644 --- a/specreduce/tests/test_utils.py +++ b/specreduce/tests/test_utils.py @@ -220,6 +220,58 @@ def test_nan_pixels_are_ignored(self): flux[100:110] = np.nan assert measure_noise(flux) == pytest.approx(3.0, rel=0.05) + @pytest.mark.parametrize("invalid", ["nan", "masked"]) + def test_invalid_pixels_do_not_disable_line_clipping(self, invalid): + # A NaN or masked pixel must not switch off the clipping of the strong lines. + rng = np.random.default_rng(6) + x = np.arange(1000.0) + flux = self._arc(rng, x, [150.0, 400.0, 620.0, 850.0], [500.0] * 4) + flux += rng.normal(0.0, 2.0, x.size) + reference = measure_noise(flux) + assert reference == pytest.approx(2.0, rel=0.1) + if invalid == "nan": + flux[300] = np.nan + else: + flux = np.ma.masked_array(flux, mask=np.zeros(x.size, dtype=bool)) + flux.mask[300:303] = True + assert measure_noise(flux) == pytest.approx(reference, rel=0.02) + + def test_masked_outliers_are_excluded(self): + rng = np.random.default_rng(7) + flux = rng.normal(0.0, 3.0, 2000) + mask = np.zeros(flux.size, dtype=bool) + mask[:1800] = True + flux[mask] = rng.normal(0.0, 300.0, 1800) + assert measure_noise(flux) > 100.0 + assert measure_noise(np.ma.masked_array(flux, mask=mask)) == pytest.approx(3.0, rel=0.1) + + def test_low_count_integer_data(self): + # More than half of the second differences are zero, so the MAD is zero. + rng = np.random.default_rng(8) + x = np.arange(1000.0) + lines = self._arc(rng, x, [150.0, 400.0, 620.0, 850.0], [40.0] * 4) + flux = rng.poisson(0.2 + lines).astype(float) + noise = measure_noise(flux) + assert np.sqrt(0.2) < noise < 3 * np.sqrt(0.2) + + def test_zero_padded_spectrum(self): + rng = np.random.default_rng(9) + flux = rng.normal(0.0, 2.0, 1000) + flux[400:] = 0.0 + assert 0.5 * 2.0 < measure_noise(flux) < 2.0 * 2.0 + + def test_degenerate_rows(self): + rng = np.random.default_rng(10) + flux = np.zeros((3, 100)) + flux[0] = np.nan + flux[1] = rng.normal(0.0, 2.0, 100) + flux[2] = 7.0 + noise = measure_noise(flux) + assert np.isnan(noise[0]) + assert noise[1] == pytest.approx(2.0, rel=0.3) + assert noise[2] == 0.0 + assert np.isnan(measure_noise(np.full(100, np.nan))) + def test_too_short_input_raises(self): with pytest.raises(ValueError, match="at least 5"): measure_noise(np.zeros(4)) diff --git a/specreduce/utils/utils.py b/specreduce/utils/utils.py index 513074e0..4f34f14a 100644 --- a/specreduce/utils/utils.py +++ b/specreduce/utils/utils.py @@ -1,6 +1,6 @@ import numpy as np from astropy import units as u -from astropy.stats import mad_std, sigma_clipped_stats +from astropy.stats import sigma_clipped_stats from specreduce.core import parse_image from specreduce.tracing import Trace, FlatTrace @@ -18,18 +18,19 @@ def measure_noise( """ Estimate the per-pixel noise standard deviation of a spectrum from the data itself. - The estimate is the sigma-clipped median absolute deviation of the second difference - of the flux along the dispersion axis, ``2 f[i] - f[i-2] - f[i+2]``, scaled to the - standard deviation of a single pixel. Differencing removes any smooth continuum or + The estimate is the sigma-clipped standard deviation of the second difference of the + flux along the dispersion axis, ``2 f[i] - f[i-2] - f[i+2]``, scaled to the standard + deviation of a single pixel. Differencing removes any smooth continuum or residual background, so the estimate does not depend on the spectrum being background-subtracted, while the iterative sigma clipping removes the pixels dominated by emission or absorption lines before the scatter is measured. The second difference of white noise has a variance of six times the per-pixel - variance, so the clipped ``mad_std`` of the differences is divided by the square - root of six. This is the same differencing used by the DER_SNR algorithm - (Stoehr et al. 2008), with the plain median replaced by a sigma-clipped robust - standard deviation to reduce the bias from dense line lists. + variance, so the clipped standard deviation of the differences is divided by the + square root of six. This is the same differencing used by the DER_SNR algorithm + (Stoehr et al. 2008), with the plain median replaced by a standard deviation that is + sigma-clipped around the median using the median absolute deviation, to reduce the + bias from dense line lists. Parameters ---------- @@ -50,7 +51,8 @@ def measure_noise( float, ndarray, or Quantity The estimated noise standard deviation. A scalar for 1D input, otherwise an array with ``axis`` removed. If ``data`` is a `~astropy.units.Quantity`, the - result carries the same unit. + result carries the same unit. The estimate is NaN where there are no valid + differences, and zero where the valid differences are all equal. Notes ----- @@ -58,6 +60,13 @@ def measure_noise( For spectra that have been smoothed or resampled onto a finer grid, the differencing suppresses part of the correlated noise and the result is biased low by up to a few tens of percent. + + The median absolute deviation is zero when more than half of the differences are + identical, as for integer data with less than about one count per pixel or for + spectra padded with a constant. The estimate then falls back to the standard + deviation after a single clipping pass, which is biased high by the lines that + survive the clipping. Padded or otherwise invalid pixels should be masked rather + than left to this fallback. """ unit = None if isinstance(data, u.Quantity): @@ -78,10 +87,29 @@ def measure_noise( # warning about invalid input. invalid = ~np.isfinite(diff2) diff2 = np.where(invalid, 0.0, diff2) - _, _, clipped_std = sigma_clipped_stats( - diff2, mask=invalid, sigma=sigma, maxiters=maxiters, stdfunc=mad_std, axis=-1 - ) - noise = clipped_std / np.sqrt(6.0) + + def clipped_std(stdfunc: str, maxiters: int | None) -> np.ndarray: + # The standard deviation function is given by name: with a callable, astropy + # does not clip masked input along an axis. A row without any valid + # differences yields a masked value, which is returned as NaN. + std = sigma_clipped_stats( + diff2, mask=invalid, sigma=sigma, maxiters=maxiters, stdfunc=stdfunc, axis=-1 + )[2] + return np.asarray(np.ma.filled(std, np.nan), dtype=float) + + noise = clipped_std("mad_std", maxiters) + + # The median absolute deviation is zero when more than half of the differences are + # identical (low-count integer data, zero-padded spectra). Iterating the clipping + # would then converge on the repeated value, so use a single clipping pass around + # the plain standard deviation instead. + degenerate = noise == 0.0 + if degenerate.any(): + noise = np.where(degenerate, clipped_std("std", 1), noise) + + noise = noise / np.sqrt(6.0) + if noise.ndim == 0: + noise = float(noise) if unit is not None: noise = noise * unit @@ -97,26 +125,26 @@ def measure_cross_dispersion_profile(image, trace=None, crossdisp_axis=0, or the average profile across several pixel values (or range of pixel values) along the dispersion axis. - If a single number is specified for `pixel`, then the profile at that pixel + If a single number is specified for ``pixel``, then the profile at that pixel (i.e wavelength) will be returned. If several pixels are specified in a list or - array, then they will be averaged (median or average, set by `statistic` which - defaults to median). Alternatively, `pixel_range` can be specified as a tuple + array, then they will be averaged (median or average, set by ``statistic`` which + defaults to median). Alternatively, ``pixel_range`` can be specified as a tuple of integers specifying the minimum and maximum pixel range to average the - profile. `pixel` and `pixel_range` cannot be set simultaneously, and the default - is `pixel_range`=(min_image, max_image) to return an average profile across the + profile. ``pixel`` and ``pixel_range`` cannot be set simultaneously, and the default + is ``pixel_range=(min_image, max_image)`` to return an average profile across the entire wavelength range of the image. The window in the cross dispersion axis for measuring the profile at - the pixel(s) specified is determined by `width` and `trace`. If a trace is + the pixel(s) specified is determined by ``width`` and ``trace``. If a trace is provided (either as a Trace object, or as a number specifying the location on the cross-dispersion axis of a FlatTrace object) that will determine the - center of the profile on the cross-dispersion axis. Otherwise, if `trace` + center of the profile on the cross-dispersion axis. Otherwise, if ``trace`` is None, the center of the image will be the center of the returned profile (i.e., the center row assuming a horizontal trace). - If `width` is none, a window size of half the image in the cross-dispersion + If ``width`` is none, a window size of half the image in the cross-dispersion axis will be used to measure the cross dispersion profile. Otherwise, an - integer value can be set for `width` which will determine the size of the + integer value can be set for ``width`` which will determine the size of the window around the trace used to measure the profile (this window moves with the trace if trace is not flat). @@ -138,19 +166,19 @@ def measure_cross_dispersion_profile(image, trace=None, crossdisp_axis=0, cross-dispersion axis will be used. [default: None] pixel: int, list of int, or None Pixel value(s) along the dispersion axis to return cross-dispersion profile. - If several specified in list, then the average (method set by `statistic`) - profile will be calculated. If None, and `pixel_range` is set, then - `pixel_range` will be used. [default: None] + If several specified in list, then the average (method set by ``statistic``) + profile will be calculated. If None, and ``pixel_range`` is set, then + ``pixel_range`` will be used. [default: None] pixel_range: tuple of int or None Tuple of (min, max) defining the pixel range (along dispersion axis) to calculate average cross-dispersion profile, up to and not inclusive of max. - If None, and `pixel` is not None, `pixel` will be used. If None and `pixel` + If None, and ``pixel`` is not None, ``pixel`` will be used. If None and ``pixel`` is also None, this will be interpreted as using the entire dispersion axis to generate an average profile for the whole image. [default: None] statistic: 'median' or 'average' - If `pixel` specifies multiple pixels, or `pixel_range` is specified, an - average profile will be returned. This can be either `median` (default) - or `average` (mean). This is ignored if only one pixel is specified. + If ``pixel`` specifies multiple pixels, or ``pixel_range`` is specified, an + average profile will be returned. This can be either ``median`` (default) + or ``average`` (mean). This is ignored if only one pixel is specified. [default: median] align_along_trace: bool Relevant only for non-flat traces. If True, "roll" each column so that From 6bef7834ffa2752eba59d98d542948f7d7ead718 Mon Sep 17 00:00:00 2001 From: Hannu Parviainen Date: Wed, 18 Nov 2026 10:37:33 +0000 Subject: [PATCH 4/4] Fixed `measure_noise` for rows without any valid differences. 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. --- specreduce/utils/utils.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/specreduce/utils/utils.py b/specreduce/utils/utils.py index 4f34f14a..be32c652 100644 --- a/specreduce/utils/utils.py +++ b/specreduce/utils/utils.py @@ -82,32 +82,42 @@ def measure_noise( values = np.moveaxis(values, axis, -1) diff2 = 2.0 * values[..., 2:-2] - values[..., :-4] - values[..., 4:] - # Non-finite differences (from NaN or masked pixels) are excluded through an explicit - # mask. The placeholder value is never used, but must be finite to keep astropy from - # warning about invalid input. + # Work on a 2D stack of rows. Non-finite differences (from NaN or masked pixels) are + # excluded through an explicit mask; the placeholder value is never used, but must + # be finite to keep astropy from warning about invalid input. + shape = diff2.shape[:-1] + diff2 = diff2.reshape(-1, diff2.shape[-1]) invalid = ~np.isfinite(diff2) diff2 = np.where(invalid, 0.0, diff2) - def clipped_std(stdfunc: str, maxiters: int | None) -> np.ndarray: + def clipped_std(rows: np.ndarray, stdfunc: str, maxiters: int | None) -> np.ndarray: # The standard deviation function is given by name: with a callable, astropy - # does not clip masked input along an axis. A row without any valid - # differences yields a masked value, which is returned as NaN. - std = sigma_clipped_stats( - diff2, mask=invalid, sigma=sigma, maxiters=maxiters, stdfunc=stdfunc, axis=-1 + # does not clip masked input along an axis. + return sigma_clipped_stats( + diff2[rows], + mask=invalid[rows], + sigma=sigma, + maxiters=maxiters, + stdfunc=stdfunc, + axis=-1, )[2] - return np.asarray(np.ma.filled(std, np.nan), dtype=float) - noise = clipped_std("mad_std", maxiters) + # Rows without any valid difference are left as NaN and never passed to astropy, + # whose behaviour for fully masked rows depends on the version. + noise = np.full(diff2.shape[0], np.nan) + rows = np.flatnonzero(~invalid.all(axis=-1)) + if rows.size > 0: + noise[rows] = clipped_std(rows, "mad_std", maxiters) # The median absolute deviation is zero when more than half of the differences are # identical (low-count integer data, zero-padded spectra). Iterating the clipping # would then converge on the repeated value, so use a single clipping pass around # the plain standard deviation instead. - degenerate = noise == 0.0 - if degenerate.any(): - noise = np.where(degenerate, clipped_std("std", 1), noise) + rows = np.flatnonzero(noise == 0.0) + if rows.size > 0: + noise[rows] = clipped_std(rows, "std", 1) - noise = noise / np.sqrt(6.0) + noise = noise.reshape(shape) / np.sqrt(6.0) if noise.ndim == 0: noise = float(noise)