From 9e134808aaf67bfc35ca5980cdc61976dcbf1f56 Mon Sep 17 00:00:00 2001 From: Ricky O'Steen Date: Wed, 28 Apr 2021 15:31:41 -0400 Subject: [PATCH] Raise an error if bin_edges is called for unevenly spaced centers --- specutils/spectra/spectral_axis.py | 5 +++++ specutils/tests/test_spectral_axis.py | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/specutils/spectra/spectral_axis.py b/specutils/spectra/spectral_axis.py index 165b0d7d4..20616db66 100644 --- a/specutils/spectra/spectral_axis.py +++ b/specutils/spectra/spectral_axis.py @@ -46,6 +46,11 @@ def _edges_from_centers(centers, unit): centers, with the two outer edges based on extrapolated centers added to the beginning and end of the spectral axis. """ + diffs = centers[1:] - centers[:-1] + if not np.all(diffs == diffs[0]): + raise ValueError("Cannot calculate consistent bin edges from" + " unevenly spaced bin centers") + a = np.insert(centers, 0, 2*centers[0] - centers[1]) b = np.append(centers, 2*centers[-1] - centers[-2]) edges = (a + b) / 2 diff --git a/specutils/tests/test_spectral_axis.py b/specutils/tests/test_spectral_axis.py index 0d5aff743..a60ce6e6c 100644 --- a/specutils/tests/test_spectral_axis.py +++ b/specutils/tests/test_spectral_axis.py @@ -51,6 +51,12 @@ def test_create_with_bin_edges(): assert np.all(spectral_axis.bin_edges == wavelengths) assert np.all(spectral_axis == [505., 530., 555., 575.]*u.AA) +def test_uneven_centers(): + + wavelengths = [10,15,25,28]*u.AA + + with pytest.raises(ValueError): + spectral_axis.bin_edges # GENERAL TESTS