Skip to content

Commit

Permalink
Raise an error if bin_edges is called for unevenly spaced centers
Browse files Browse the repository at this point in the history
  • Loading branch information
rosteen committed Apr 28, 2021
1 parent 57dd2b2 commit 0e6300b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions specutils/spectra/spectral_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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
Expand Down
6 changes: 6 additions & 0 deletions specutils/tests/test_spectral_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0e6300b

Please sign in to comment.