Skip to content

Commit 0e6300b

Browse files
committed
Raise an error if bin_edges is called for unevenly spaced centers
1 parent 57dd2b2 commit 0e6300b

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
@@ -48,6 +48,11 @@ def _edges_from_centers(centers, unit):
4848
centers, with the two outer edges based on extrapolated centers added
4949
to the beginning and end of the spectral axis.
5050
"""
51+
diffs = centers[1:] - centers[:-1]
52+
if not np.all(diffs == diffs[0]):
53+
raise ValueError("Cannot calculate consistent bin edges from"
54+
" unevenly spaced bin centers")
55+
5156
a = np.insert(centers, 0, 2*centers[0] - centers[1])
5257
b = np.append(centers, 2*centers[-1] - centers[-2])
5358
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)