Skip to content

Commit b59003e

Browse files
authored
Ensure arithmetic results preserve spectral axis shifts (#1158)
* Ensure arithmetic results preserve spectral axis shifts * Changelog * Codestyle * Clearer changelog
1 parent c006678 commit b59003e

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ New Features
77
Bug Fixes
88
^^^^^^^^^
99

10+
- Arithmetic operations on ``Spectrum1D`` objects now preserve spectral axis values that
11+
were updated by setting redshift or radial velocity. [#1158]
12+
1013
Other Changes and Additions
1114
^^^^^^^^^^^^^^^^^^^^^^^^^^^
1215

specutils/spectra/spectrum1d.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,18 @@ def redshift(self, val):
680680
def radial_velocity(self, val):
681681
self.shift_spectrum_to(radial_velocity=val)
682682

683+
def _return_with_redshift(self, result):
684+
result.shift_spectrum_to(redshift=self.redshift)
685+
return result
686+
683687
def __add__(self, other):
684688
if not isinstance(other, (NDCube, u.Quantity)):
685689
try:
686690
other = u.Quantity(other, unit=self.unit)
687691
except TypeError:
688692
return NotImplemented
689693

690-
return self.add(other)
694+
return self._return_with_redshift(self.add(other))
691695

692696
def __sub__(self, other):
693697
if not isinstance(other, NDCube):
@@ -696,25 +700,25 @@ def __sub__(self, other):
696700
except TypeError:
697701
return NotImplemented
698702

699-
return self.subtract(other)
703+
return self._return_with_redshift(self.subtract(other))
700704

701705
def __mul__(self, other):
702706
if not isinstance(other, NDCube):
703707
other = u.Quantity(other)
704708

705-
return self.multiply(other)
709+
return self._return_with_redshift(self.multiply(other))
706710

707711
def __div__(self, other):
708712
if not isinstance(other, NDCube):
709713
other = u.Quantity(other)
710714

711-
return self.divide(other)
715+
return self._return_with_redshift(self.divide(other))
712716

713717
def __truediv__(self, other):
714718
if not isinstance(other, NDCube):
715719
other = u.Quantity(other)
716720

717-
return self.divide(other)
721+
return self._return_with_redshift(self.divide(other))
718722

719723
__radd__ = __add__
720724

specutils/tests/test_arithmetic.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,12 @@ def test_with_constants(simulated_spectra):
131131
r_sub_result = 2 - spec
132132
l_sub_result = -1 * (spec - 2)
133133
assert_quantity_allclose(r_sub_result.flux, l_sub_result.flux)
134+
135+
136+
def test_arithmetic_after_shift(simulated_spectra):
137+
spec = simulated_spectra.s1_um_mJy_e1
138+
spec.shift_spectrum_to(redshift = 1)
139+
140+
# Test that doing arithmetic preserves the shifted spectral axis
141+
spec *= 2
142+
assert_quantity_allclose(spec.spectral_axis, 2*np.linspace(0.4, 1.05, 100)*u.um)

0 commit comments

Comments
 (0)