Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Sep 6, 2024
1 parent 8b3f3a1 commit ca3e40e
Show file tree
Hide file tree
Showing 19 changed files with 630 additions and 217 deletions.
11 changes: 10 additions & 1 deletion jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@ def equivalent_units(self, data, cid, units):
'erg / (s sr cm2)', 'erg / (Hz s sr cm2)',
'erg / (Angstrom s sr cm2)',
'ph / (Angstrom s sr cm2)', 'ph / (Hz s sr cm2)'
])
]
+ [
'Jy / pix2', 'mJy / pix2', 'uJy / pix2', 'MJy / pix2',
'W / (Hz m2 pix2)', 'eV / (Hz s m2 pix2)',
'erg / (s cm2 pix2)', 'erg / (Hz s cm2 pix2)',
'erg / (Angstrom s cm2 pix2)',
'ph / (Angstrom s cm2 pix2)', 'ph / (Hz s cm2 pix2)'
]

)
else: # spectral axis
# prefer Hz over Bq and um over micron
exclude = {'Bq', 'micron'}
Expand Down
60 changes: 15 additions & 45 deletions jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
SpectralContinuumMixin,
skip_if_no_updates_since_last_active,
with_spinner)
from jdaviz.core.validunits import check_if_unit_is_per_solid_angle
from jdaviz.core.user_api import PluginUserApi

__all__ = ['MomentMap']
Expand Down Expand Up @@ -111,8 +110,10 @@ def __init__(self, *args, **kwargs):
self.output_unit = SelectPluginComponent(self,
items='output_unit_items',
selected='output_unit_selected',
manual_options=['Flux', 'Spectral Unit',
'Velocity', 'Velocity^N'])
manual_options=['Surface Brightness',
'Spectral Unit',
'Velocity',
'Velocity^N'])

self.dataset.add_filter('is_cube')
self.add_results.viewer.filters = ['is_image_viewer']
Expand Down Expand Up @@ -174,16 +175,11 @@ def _set_data_units(self, event={}):
self.output_unit_selected = moment_unit_options[unit_options_index][0]
self.send_state("output_unit_selected")

# either 'Flux' or 'Surface Brightness'
orig_flux_or_sb = self.output_unit_items[0]['label']

unit_dict = {orig_flux_or_sb: "",
unit_dict = {"Surface Brightness": "",
"Spectral Unit": "",
"Velocity": "km/s",
"Velocity^N": f"km{self.n_moment}/s{self.n_moment}"}

sb_or_flux_label = None

if self.dataset_selected != "":
# Spectral axis is first in this list
data = self.app.data_collection[self.dataset_selected]
Expand All @@ -197,36 +193,11 @@ def _set_data_units(self, event={}):
self.dataset_spectral_unit = sunit
unit_dict["Spectral Unit"] = sunit

# get flux/SB units
if self.spectrum_viewer and hasattr(self.spectrum_viewer.state, 'y_display_unit'):
if self.spectrum_viewer.state.y_display_unit is not None:
unit_dict[orig_flux_or_sb] = self.spectrum_viewer.state.y_display_unit
else:
# spectrum_viewer.state will only have x/y_display_unit if unit conversion has
# been done if not, get default flux units which should be the units displayed
unit_dict[orig_flux_or_sb] = data.get_component('flux').units
else:
# spectrum_viewer.state will only have x/y_display_unit if unit conversion has
# been done if not, get default flux units which should be the units displayed
unit_dict[orig_flux_or_sb] = data.get_component('flux').units

# figure out if label should say 'Flux' or 'Surface Brightness'
sb_or_flux_label = "Flux"
is_unit_solid_angle = check_if_unit_is_per_solid_angle(unit_dict[orig_flux_or_sb])
if is_unit_solid_angle is True:
sb_or_flux_label = "Surface Brightness"
unit_dict["Surface Brightness"] = self.app._get_display_unit('sb')

# Update units in selection item dictionary
for item in self.output_unit_items:
item["unit_str"] = unit_dict[item["label"]]
if item["label"] in ["Flux", "Surface Brightness"] and sb_or_flux_label:
# change unit label to reflect if unit is flux or SB
item["label"] = sb_or_flux_label

if self.dataset_selected != "":
# output_unit_selected might not match (potentially) changed flux/SB label
if self.output_unit_selected in ['Flux', 'Surface Brightness']:
self.output_unit_selected = sb_or_flux_label

# Filter what we want based on n_moment
if self.n_moment == 0:
Expand Down Expand Up @@ -343,21 +314,20 @@ 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:
# get flux/SB units
if self.spectrum_viewer and hasattr(self.spectrum_viewer.state, 'y_display_unit'):
if self.spectrum_viewer.state.y_display_unit is not None:
flux_sb_unit = self.spectrum_viewer.state.y_display_unit
else:
flux_sb_unit = data.get_component('flux').units
else:
flux_sb_unit = data.get_component('flux').units

# convert unit string to u.Unit so moment map data can be converted
flux_or_sb_display_unit = u.Unit(flux_sb_unit)
# get display units for moment 0 based on unit conversion plugin selection
moment_0_display_unit = self.output_unit_items[0]['unit_str']

# convert unit string to Unit so moment map data can be converted
flux_or_sb_display_unit = u.Unit(moment_0_display_unit)

# account for extra wavelength factor, depending on specutils version
if SPECUTILS_LT_1_15_1:
moment_new_unit = flux_or_sb_display_unit
else:
moment_new_unit = flux_or_sb_display_unit * self.spectrum_viewer.state.x_display_unit # noqa: E501

print(self.moment.unit, moment_new_unit)
self.moment = self.moment.to(moment_new_unit)

# Reattach the WCS so we can load the result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@
from jdaviz.configs.cubeviz.plugins.moment_maps.moment_maps import SPECUTILS_LT_1_15_1


def test_user_api(cubeviz_helper, spectrum1d_cube):
@pytest.mark.parametrize("cube_type", ["Surface Brightness", "Flux"])
def test_user_api(cubeviz_helper, spectrum1d_cube, spectrum1d_cube_sb_unit, cube_type):

# test is parameterize to test a cube that is in Jy / sr (Surface Brightness)
# as well as Jy (Flux), to test that flux cubes, which are converted in the
# parser to flux / pix^2 surface brightness cubes, both work correctly.

if cube_type == 'Surface Brightness':
cube = spectrum1d_cube_sb_unit
elif cube_type == 'Flux':
cube = spectrum1d_cube

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

mm = cubeviz_helper.plugins['Moment Maps']
assert not mm._obj.continuum_marks['center'].visible
Expand Down Expand Up @@ -45,23 +56,37 @@ def test_user_api(cubeviz_helper, spectrum1d_cube):
mm.n_moment = 1
with pytest.raises(ValueError, match="not one of"):
mm._obj.output_unit_selected = "Bad Unit"
mm._obj.output_unit_selected = "Flux"
mm._obj.output_unit_selected = "Surface Brightness"
with pytest.raises(ValueError, match="units must be in"):
mm.calculate_moment()


def test_moment_calculation(cubeviz_helper, spectrum1d_cube, tmp_path):
@pytest.mark.parametrize("cube_type", ["Surface Brightness", "Flux"])
def test_moment_calculation(cubeviz_helper, spectrum1d_cube,
spectrum1d_cube_sb_unit, cube_type, tmp_path):

if SPECUTILS_LT_1_15_1:
moment_unit = "Jy"
moment_value_str = "+8.00000e+00"
else:
moment_unit = "Jy m"
moment_value_str = "+6.40166e-10"

if cube_type == 'Surface Brightness':
moment_unit += " / sr"
cube = spectrum1d_cube_sb_unit
cube_unit = cube.unit.to_string()

elif cube_type == 'Flux':
moment_unit += " / pix2"
cube = spectrum1d_cube
cube_unit = moment_unit # cube in Jy will become cube in Jy / pix2

dc = cubeviz_helper.app.data_collection
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="No observer defined on WCS.*")
cubeviz_helper.load_data(spectrum1d_cube, data_label='test')
cubeviz_helper.load_data(cube, data_label='test')

flux_viewer = cubeviz_helper.app.get_viewer(cubeviz_helper._default_flux_viewer_reference_name)

# Since we are not really displaying, need this to trigger GUI stuff.
Expand All @@ -73,7 +98,9 @@ def test_moment_calculation(cubeviz_helper, spectrum1d_cube, tmp_path):

mm.n_moment = 0 # Collapsed sum, will get back 2D spatial image
assert mm._obj.results_label == 'moment 0'
assert mm.output_unit == "Flux"

# result is always a SB unit - if flux cube loaded, per pix2
assert mm.output_unit == 'Surface Brightness'

mm._obj.add_results.viewer.selected = cubeviz_helper._default_uncert_viewer_reference_name
mm._obj.vue_calculate_moment()
Expand All @@ -93,13 +120,13 @@ def test_moment_calculation(cubeviz_helper, spectrum1d_cube, tmp_path):
assert mm._obj.results_label == 'moment 0'
assert mm._obj.results_label_overwrite is True

# Make sure coordinate display works
# Make sure coordinate display works in flux viewer (loaded data, not the moment map)
label_mouseover = cubeviz_helper.app.session.application._tools['g-coords-info']
label_mouseover._viewer_mouse_event(flux_viewer, {'event': 'mousemove',
'domain': {'x': 0, 'y': 0}})
assert flux_viewer.state.slices == (0, 0, 1)
# Slice 0 has 8 pixels, this is Slice 1
assert label_mouseover.as_text() == ("Pixel x=00.0 y=00.0 Value +8.00000e+00 Jy",
assert label_mouseover.as_text() == (f"Pixel x=00.0 y=00.0 Value +8.00000e+00 {cube_unit}",
"World 13h39m59.9731s +27d00m00.3600s (ICRS)",
"204.9998877673 27.0001000000 (deg)")

Expand Down
Loading

0 comments on commit ca3e40e

Please sign in to comment.