Skip to content

Commit

Permalink
Update function name to with_flux unit and update docstring+tests (#1124
Browse files Browse the repository at this point in the history
)
  • Loading branch information
rosteen authored Feb 20, 2024
1 parent 2fe4fa2 commit 98dfcfd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 6 additions & 2 deletions specutils/spectra/spectrum_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ def flux(self):
"""
return u.Quantity(self.data, unit=self.unit, copy=False)

@deprecated('v1.13', alternative="with_flux_unit")
def new_flux_unit(self, unit, equivalencies=None, suppress_conversion=False):
return self.with_flux_unit(unit, equivalencies=equivalencies,
suppress_conversion=suppress_conversion)

def with_flux_unit(self, unit, equivalencies=None, suppress_conversion=False):
"""
Converts the flux data to the specified unit. This is an in-place
change to the object.
Returns a new spectrum with a different flux unit
Parameters
----------
Expand Down
8 changes: 4 additions & 4 deletions specutils/tests/test_spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,25 +277,25 @@ def test_flux_unit_conversion():

# Simple Unit Conversion
s = Spectrum1D(flux=np.array([26.0, 44.5]) * u.Jy, spectral_axis=np.array([400, 500])*u.nm)
converted_spec = s.new_flux_unit(unit=u.uJy)
converted_spec = s.with_flux_unit(unit=u.uJy)
assert ((26.0 * u.Jy).to(u.uJy) == converted_spec.flux[0])

# Make sure incompatible units raise UnitConversionError
with pytest.raises(u.UnitConversionError):
s.new_flux_unit(unit=u.m)
s.with_flux_unit(unit=u.m)

# Pass custom equivalencies
s = Spectrum1D(flux=np.array([26.0, 44.5]) * u.Jy,
spectral_axis=np.array([400, 500]) * u.nm)
eq = [[u.Jy, u.m,
lambda x: np.full_like(np.array(x), 1000.0, dtype=np.double),
lambda x: np.full_like(np.array(x), 0.001, dtype=np.double)]]
converted_spec = s.new_flux_unit(unit=u.m, equivalencies=eq)
converted_spec = s.with_flux_unit(unit=u.m, equivalencies=eq)
assert 1000.0 * u.m == converted_spec.flux[0]

# Check if suppressing the unit conversion works
s = Spectrum1D(flux=np.array([26.0, 44.5]) * u.Jy, spectral_axis=np.array([400, 500]) * u.nm)
new_spec = s.new_flux_unit("uJy", suppress_conversion=True)
new_spec = s.with_flux_unit("uJy", suppress_conversion=True)
assert new_spec.flux[0] == 26.0 * u.uJy


Expand Down

0 comments on commit 98dfcfd

Please sign in to comment.