Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict u.spectral equivalency to contexts where it is required #673

Merged
merged 3 commits into from
May 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions specutils/spectra/spectral_region.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ def __delitem__(self, item):
def _valid(self):

# Lower bound < Upper bound for all sub regions in length physical type
sub_regions = [(x[0].to('m'), x[1].to('m'))
if x[0].unit.is_equivalent(u.m) else x
for x in self._subregions]
with u.set_enabled_equivalencies(u.spectral()):
sub_regions = [(x[0].to('m'), x[1].to('m'))
if x[0].unit.is_equivalent(u.m) else x
for x in self._subregions]

if any(x[0] >= x[1] for x in sub_regions):
raise ValueError('Lower bound must be strictly less than the upper bound')
Expand Down
5 changes: 1 addition & 4 deletions specutils/spectra/spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

__doctest_skip__ = ['Spectrum1D.spectral_resolution']

u.set_enabled_equivalencies(u.spectral())


class Spectrum1D(OneDSpectrumMixin, NDDataRef):
"""
Expand Down Expand Up @@ -111,8 +109,7 @@ def __init__(self, flux=None, spectral_axis=None, wcs=None,
"Assuming units of spectral axis ('%s').",
spectral_axis.unit)
rest_value = u.Quantity(rest_value, spectral_axis.unit)
elif not rest_value.unit.is_equivalent(u.AA) \
and not rest_value.unit.is_equivalent(u.Hz):
elif not rest_value.unit.is_equivalent(u.AA, equivalencies=u.spectral()):
raise u.UnitsError("Rest value must be "
"energy/wavelength/frequency equivalent.")

Expand Down
6 changes: 3 additions & 3 deletions specutils/tests/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_equivalent_width():

result = equivalent_width(spectrum)

assert result.unit.is_equivalent(spectrum.wcs.unit)
assert result.unit.is_equivalent(spectrum.wcs.unit, equivalencies=u.spectral())

# Since this is an emission line, we expect the equivalent width value to
# be negative
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_equivalent_width_continuum(continuum):

result = equivalent_width(spectrum, continuum=continuum)

assert result.unit.is_equivalent(spectrum.wcs.unit)
assert result.unit.is_equivalent(spectrum.wcs.unit, equivalencies=u.spectral())

# Since this is an emission line, we expect the equivalent width value to
# be negative
Expand All @@ -116,7 +116,7 @@ def test_equivalent_width_absorption():

result = equivalent_width(spectrum)

assert result.unit.is_equivalent(spectrum.wcs.unit)
assert result.unit.is_equivalent(spectrum.wcs.unit, equivalencies=u.spectral())

expected = amplitude*np.sqrt(2*np.pi) * u.GHz

Expand Down
22 changes: 22 additions & 0 deletions specutils/tests/test_spectrum1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ def test_spectral_axis_conversions():
new_spec = spec.with_spectral_unit(u.GHz)


@pytest.mark.parametrize('unit', ['micron', 'GHz', 'cm**-1', 'eV'])
def test_spectral_axis_equivalencies(unit):
"""Test that `u.spectral` equivalencies are enabled for `spectral_axis`."""

spectral_axis=np.array([3400, 5000, 6660]) * u.AA
spec = Spectrum1D(flux=np.array([26.0, 30.0, 44.5]) * u.Jy, spectral_axis=spectral_axis)

new_axis = spectral_axis.to(unit, equivalencies=u.spectral())
assert u.allclose(spec.spectral_axis.to(unit), new_axis)


def test_redshift():
spec = Spectrum1D(flux=np.array([26.0, 30., 44.5]) * u.Jy,
spectral_axis=np.array([4000, 6000, 8000]) * u.AA,
Expand Down Expand Up @@ -387,3 +398,14 @@ def test_str():
"""Spectrum1D (length=1)
flux: [ 1.0 Jy ], mean=1.0 Jy
spectral axis: [ 0.0 nm ], mean=0.0 nm"""


def test_equivalencies():
"""
Test that after import `u.spectral` equivalencies are not enabled in the global namespace.
"""
assert u.micron.is_equivalent(u.cm**-1) is False
assert u.micron.is_equivalent(u.Hz) is False
assert u.micron.is_equivalent(u.eV) is False
assert u.Hz.is_equivalent(u.cm**-1) is False
assert u.Hz.is_equivalent(u.eV) is False