Skip to content

Commit 9e13480

Browse files
committed
Raise an error if bin_edges is called for unevenly spaced centers
1 parent dca5e7c commit 9e13480

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

specutils/spectra/spectral_axis.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ def _edges_from_centers(centers, unit):
4646
centers, with the two outer edges based on extrapolated centers added
4747
to the beginning and end of the spectral axis.
4848
"""
49+
diffs = centers[1:] - centers[:-1]
50+
if not np.all(diffs == diffs[0]):
51+
raise ValueError("Cannot calculate consistent bin edges from"
52+
" unevenly spaced bin centers")
53+
4954
a = np.insert(centers, 0, 2*centers[0] - centers[1])
5055
b = np.append(centers, 2*centers[-1] - centers[-2])
5156
edges = (a + b) / 2

specutils/tests/test_spectral_axis.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ def test_create_with_bin_edges():
5151
assert np.all(spectral_axis.bin_edges == wavelengths)
5252
assert np.all(spectral_axis == [505., 530., 555., 575.]*u.AA)
5353

54+
def test_uneven_centers():
55+
56+
wavelengths = [10,15,25,28]*u.AA
57+
58+
with pytest.raises(ValueError):
59+
spectral_axis.bin_edges
5460

5561
# GENERAL TESTS
5662

0 commit comments

Comments
 (0)