Skip to content

Commit

Permalink
Merge pull request #2603 from pllim/to-value
Browse files Browse the repository at this point in the history
MNT: Unit conversion code clean-up
  • Loading branch information
pllim authored Dec 8, 2023
2 parents ecfee83 + d39466b commit 6e7f4a1
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ def test_subset_masks(cubeviz_helper, spectrum1d_cube_larger):
sv.toolbar_active_subset.selected = []

# Now create the new spectral subset:
sv.apply_roi(XRangeROI(
min=min_wavelength.to(u.m).value,
max=max_wavelength.to(u.m).value
))
sv.apply_roi(XRangeROI(min=min_wavelength.to_value(u.m), max=max_wavelength.to_value(u.m)))
assert "Subset 2" in p.spectral_subset.choices

# Select the spectral subset
Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/specviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def _set_scale(self, scale, axis, min_val=None, max_val=None):
# If user's value has a unit, convert it to the current axis' units
elif isinstance(min_val, u.Quantity):
# Convert user's value to axis' units
min_val = min_val.to(axis.unit).value
min_val = min_val.to_value(axis.unit)
# If auto, set to min axis wavelength value
elif min_val == "auto":
min_val = min(axis).value
Expand All @@ -231,7 +231,7 @@ def _set_scale(self, scale, axis, min_val=None, max_val=None):
# If user's value has a unit, convert it to the current axis' units
if isinstance(max_val, u.Quantity):
# Convert user's value to axis' units
max_val = max_val.to(axis.unit).value
max_val = max_val.to_value(axis.unit)
# If auto, set to max axis wavelength value
elif max_val == "auto":
max_val = max(axis).value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import numpy as np
from numpy.testing import assert_allclose
import pytest
import numpy as np
from astropy import units as u
from astropy.table import QTable
from astropy.tests.helper import assert_quantity_allclose
from glue.core.roi import XRangeROI
from glue.core.edit_subset_mode import NewMode
from numpy.testing import assert_allclose
from regions import RectanglePixelRegion, PixCoord
from specutils import Spectrum1D

Expand Down Expand Up @@ -175,11 +176,10 @@ def test_line_identify(specviz_helper, spectrum1d):
def test_coerce_unit():
q_input = 1 * u.Unit('1E-20 erg m / (Angstrom cm**2 s)')
q_input.uncertainty = 0.1 * u.Unit('1E-20 erg m / (Angstrom cm**2 s)')
q_unit = u.Unit('erg / (cm**2 s)')
q_coerced = _coerce_unit(q_input)
assert q_coerced.unit == u.Unit('erg / (cm**2 s)')
assert np.allclose(q_coerced.value, 1e-20 * u.m.to(u.Angstrom))
assert q_coerced.uncertainty.unit == u.Unit('erg / (cm**2 s)')
assert np.allclose(q_coerced.uncertainty.value, 0.1 * 1e-20 * u.m.to(u.Angstrom))
assert_quantity_allclose(q_coerced, 1e-10 * q_unit)
assert_quantity_allclose(q_coerced.uncertainty, 1e-11 * q_unit)
q_input.uncertainty = None
q_coerced = _coerce_unit(q_input)
assert not hasattr(q_coerced, 'uncertainty')
Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/specviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def plot_spectral_line(self, line, plot_units=None, **kwargs):
plot_units = self.data()[0].spectral_axis.unit

line_mark = SpectralLine(self,
line['rest'].to(plot_units).value,
line['rest'].to_value(plot_units),
self.redshift,
name=line["linename"],
table_index=line["name_rest"],
Expand Down Expand Up @@ -306,7 +306,7 @@ def plot_spectral_lines(self, colors=["blue"], **kwargs):
if not line["show"]:
continue
line = SpectralLine(self,
line['rest'].to(plot_units).value,
line['rest'].to_value(plot_units),
redshift=self.redshift,
name=line["linename"],
table_index=line["name_rest"],
Expand Down
4 changes: 2 additions & 2 deletions jdaviz/core/tests/test_template_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_spectralsubsetselect(specviz_helper, spectrum1d):

# check selected subset mask available via API:
expected_mask_with_spectral_subset = (
(spectrum1d.wavelength.to(u.AA).value < 6500) |
(spectrum1d.wavelength.to(u.AA).value > 7400)
(spectrum1d.wavelength.to_value(u.AA) < 6500) |
(spectrum1d.wavelength.to_value(u.AA) > 7400)
)
assert np.all(
expected_mask_with_spectral_subset == p.spectral_subset.selected_subset_mask
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/models/physical_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class BlackBody(Fittable1DModel):
with quantity_support():
plt.figure()
plt.semilogx(wav, flux)
plt.axvline(bb.nu_max.to(u.AA, equivalencies=u.spectral()).value, ls='--')
plt.axvline(bb.nu_max.to_value(u.AA, equivalencies=u.spectral()), ls='--')
plt.show()
"""

Expand Down

0 comments on commit 6e7f4a1

Please sign in to comment.