Skip to content

Commit

Permalink
moving changes
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Nov 1, 2024
1 parent de00973 commit 12c5b13
Show file tree
Hide file tree
Showing 12 changed files with 497 additions and 163 deletions.
13 changes: 12 additions & 1 deletion jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
skip_if_no_updates_since_last_active,
with_spinner)
from jdaviz.core.user_api import PluginUserApi
from jdaviz.utils import flux_conversion_general

__all__ = ['MomentMap']

Expand Down Expand Up @@ -321,7 +322,17 @@ def calculate_moment(self, add_data=True):
# convert units for moment 0, which is the only currently supported
# moment for using converted units.
if n_moment == 0:
self.moment = self.moment.to(self.moment_zero_unit)

# multiply out spectral axis unit so we are converting between
# surface brightnesses only
x_unit = u.Unit(self.spectrum_viewer.state.x_display_unit)
eqv = u.spectral_density(self.reference_wavelength * x_unit)

self.moment = flux_conversion_general(self.moment.value,
u.Unit(self.moment.unit) / x_unit,
u.Unit(self.moment_zero_unit) / x_unit,
eqv, with_unit=True)
self.moment *= x_unit

# Reattach the WCS so we can load the result
self.moment = CCDData(self.moment, wcs=data_wcs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from numpy.testing import assert_allclose
from specutils import SpectralRegion

from jdaviz.core.custom_units import PIX2, SPEC_PHOTON_FLUX_DENSITY_UNITS


@pytest.mark.parametrize("cube_type", ["Surface Brightness", "Flux"])
def test_user_api(cubeviz_helper, spectrum1d_cube, spectrum1d_cube_sb_unit, cube_type):
Expand Down Expand Up @@ -350,3 +352,43 @@ def test_correct_output_spectral_y_units(cubeviz_helper, spectrum1d_cube_custom_

mm.calculate_moment()
assert mm.moment.unit == moment_unit.replace('m', 'um')

@pytest.mark.parametrize("flux_angle_unit", [(u.Unit(x), u.sr) for x in SPEC_PHOTON_FLUX_DENSITY_UNITS] + [(u.Unit(x), PIX2) for x in SPEC_PHOTON_FLUX_DENSITY_UNITS]) # noqa
def test_moment_zero_unit_flux_conversions(cubeviz_helper,
spectrum1d_cube_custom_fluxunit,
flux_angle_unit):
"""
Test the calculation of the 0th moment and all possible flux unit conversions.
Tests all units in SPEC_PHOTON_FLUX_DENSITY_UNITS against one another, since
they are all valid selections in the unit conversion plugin.
"""
flux_unit, angle_unit = flux_angle_unit
cube_unit = flux_unit / angle_unit

sb_cube = spectrum1d_cube_custom_fluxunit(fluxunit=cube_unit)

# load surface brigtness cube
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="No observer defined on WCS.*")
cubeviz_helper.load_data(sb_cube, data_label='test')

# get plugins
uc = cubeviz_helper.plugins["Unit Conversion"]
mm = cubeviz_helper.plugins['Moment Maps']._obj

for new_flux_unit in SPEC_PHOTON_FLUX_DENSITY_UNITS:
if new_flux_unit != flux_unit: # dont compare same units
# first set back to original flux unit, so we're not converting from
# the unit set during the last iteration
uc.flux_unit.selected = flux_unit.to_string()

# then convert to new flux unit
uc.flux_unit.selected = new_flux_unit

output_unit_moment_0 = mm.output_unit_items[0]
new_mm_unit = (u.Unit(new_flux_unit) * u.m / u.Unit(angle_unit)).to_string()
assert output_unit_moment_0['label'] == 'Surface Brightness'
assert output_unit_moment_0['unit_str'] == new_mm_unit

mm.calculate_moment()
assert mm.moment.unit == new_mm_unit
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from jdaviz.core.validunits import check_if_unit_is_per_solid_angle
from jdaviz.configs.cubeviz.plugins.parsers import _return_spectrum_with_correct_units
from jdaviz.configs.cubeviz.plugins.viewers import WithSliceIndicator
from jdaviz.utils import _eqv_pixar_sr
from jdaviz.utils import flux_conversion_general, _eqv_pixar_sr, _eqv_flux_to_sb_pixel


__all__ = ['SpectralExtraction']
Expand Down Expand Up @@ -559,9 +559,19 @@ def _preview_x_from_extracted(self, extracted):
return extracted.spectral_axis

def _preview_y_from_extracted(self, extracted):

# TODO: use extracted.meta.get('PIXAR_SR') once populated
return extracted.flux.to(self.spectrum_y_units,
equivalencies=_eqv_pixar_sr(self.dataset.selected_obj.meta.get('PIXAR_SR', 1.0))) # noqa:
# may need flux<>sb equivalencies, as well as spectral_density
eqv =_eqv_pixar_sr(self.dataset.selected_obj.meta.get('PIXAR_SR', 1.0)) # for conversions between flux <> sb per sr
eqv += _eqv_flux_to_sb_pixel() # for conversions between flux <> sb per pix2
eqv += u.spectral_density(self.dataset.selected_obj.spectral_axis)

# call this rather than unit.to because this function handles special cases where
# the solid angle needs to be multiplied out before conversion
return flux_conversion_general(extracted.flux.value, extracted.flux.unit,
self.spectrum_y_units,
eqv)


@with_spinner()
def extract(self, return_bg=False, add_data=True, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
from specutils import Spectrum1D
from specutils.manipulation import FluxConservingResampler

from jdaviz.core.custom_units import PIX2, SPEC_PHOTON_FLUX_DENSITY_UNITS

calspec_url = "https://archive.stsci.edu/hlsps/reference-atlases/cdbs/current_calspec/"


Expand Down Expand Up @@ -547,24 +549,42 @@ def test_default_spectral_extraction(cubeviz_helper, spectrum1d_cube_fluxunit_jy
extracted_spectra[0].flux, extracted_spectra[1].flux
)

@pytest.mark.parametrize("flux_angle_unit", [(u.Unit(x), u.sr) for x in SPEC_PHOTON_FLUX_DENSITY_UNITS] + [(u.Unit(x), PIX2) for x in SPEC_PHOTON_FLUX_DENSITY_UNITS]) # noqa
def test_spectral_extraction_unit_conv_one_spec(cubeviz_helper,
spectrum1d_cube_custom_fluxunit,
flux_angle_unit):

flux_unit, angle_unit = flux_angle_unit
flux_unit_str = flux_unit.to_string()

sb_cube = spectrum1d_cube_custom_fluxunit(fluxunit=flux_unit / angle_unit)
cubeviz_helper.load_data(sb_cube)

def test_spectral_extraction_unit_conv_one_spec(
cubeviz_helper, spectrum1d_cube_fluxunit_jy_per_steradian
):
cubeviz_helper.load_data(spectrum1d_cube_fluxunit_jy_per_steradian)
spectrum_viewer = cubeviz_helper.app.get_viewer(
cubeviz_helper._default_spectrum_viewer_reference_name)

uc = cubeviz_helper.plugins["Unit Conversion"]
assert uc.flux_unit == "Jy"
uc.flux_unit.selected = "MJy"
assert spectrum_viewer.state.y_display_unit == "MJy"
spec_extr_plugin = cubeviz_helper.plugins['Spectral Extraction']
# Overwrite the one and only default extraction.
collapsed = spec_extr_plugin.extract()
# Actual values not in display unit but should not affect display unit.
assert collapsed.flux.unit == u.Jy
assert uc.flux_unit.selected == "MJy"
assert spectrum_viewer.state.y_display_unit == "MJy"
se = cubeviz_helper.plugins['Spectral Extraction']

for new_flux_unit in SPEC_PHOTON_FLUX_DENSITY_UNITS:
if new_flux_unit != flux_unit: # dont compare same units

# first set back to original flux unit, so we're not converting from
# the unit set during the last iteration
uc.flux_unit.selected = flux_unit.to_string()

# set to new unit
uc.flux_unit.selected = new_flux_unit

assert spectrum_viewer.state.y_display_unit == new_flux_unit

# Overwrite the one and only default extraction.

collapsed = se.extract()
# Actual values not in display unit but should not affect display unit.
assert collapsed.flux.unit == flux_unit
assert uc.flux_unit.selected == new_flux_unit
assert spectrum_viewer.state.y_display_unit == new_flux_unit


@pytest.mark.usefixtures('_jail')
Expand Down
Loading

0 comments on commit 12c5b13

Please # to comment.