Skip to content

Commit

Permalink
nested parameterize mm test
Browse files Browse the repository at this point in the history
  • Loading branch information
cshanahan1 committed Nov 20, 2024
1 parent e3a1b26 commit 88dd54a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
2 changes: 1 addition & 1 deletion jdaviz/configs/cubeviz/plugins/moment_maps/moment_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def calculate_moment(self, add_data=True):
# only flux<> flux conversions will occur, so we only
# need the spectral_density equivalency. should I just
# be using the mean wavelength here?
eqv = u.spectral_density(np.mean(slab.spectral_axis))
eqv = u.spectral_density(np.nanmean(slab.spectral_axis))

self.moment = flux_conversion_general(self.moment.value,
u.Unit(self.moment.unit) / x_unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,15 +355,23 @@ def test_correct_output_spectral_y_units(cubeviz_helper, spectrum1d_cube_custom_


@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
@pytest.mark.parametrize("new_flux_unit", [u.Unit(x)for x in SPEC_PHOTON_FLUX_DENSITY_UNITS])
def test_moment_zero_unit_flux_conversions(cubeviz_helper,
spectrum1d_cube_custom_fluxunit,
flux_angle_unit):
flux_angle_unit,
new_flux_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
flux_unit_str = flux_unit.to_string()
new_flux_unit_str = new_flux_unit.to_string()

if flux_unit == new_flux_unit: # would result in no conversion, skip
return

cube_unit = flux_unit / angle_unit

sb_cube = spectrum1d_cube_custom_fluxunit(fluxunit=cube_unit)
Expand All @@ -381,46 +389,44 @@ def test_moment_zero_unit_flux_conversions(cubeviz_helper,
flux_viewer = cubeviz_helper.app.get_viewer(cubeviz_helper._default_flux_viewer_reference_name)
label_mouseover = cubeviz_helper.app.session.application._tools['g-coords-info']

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()
# 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_str

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

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

# calculate moment with new output label and plot in flux viewer
mm.add_results.label = new_flux_unit
mm.add_results.viewer.selected = cubeviz_helper._default_flux_viewer_reference_name
mm.calculate_moment()
# calculate moment with new output label and plot in flux viewer
mm.add_results.label = new_flux_unit_str
mm.add_results.viewer.selected = cubeviz_helper._default_flux_viewer_reference_name
mm.calculate_moment()

assert mm.moment.unit == new_mm_unit

# make sure mouseover info in flux unit is new moment map unit
# which should be flux/sb unit times spectral axis unit (e.g. MJy m / sr)
label_mouseover._viewer_mouse_event(flux_viewer,
{'event': 'mousemove',
'domain': {'x': 0, 'y': 0}})
m_orig = label_mouseover.as_text()[0]
assert (u.Unit(new_flux_unit / angle_unit) * u.m).to_string() in m_orig

# 'jiggle' mouse so we can move it back
label_mouseover._viewer_mouse_event(flux_viewer,
{'event': 'mousemove',
'domain': {'x': 1, 'y': 1}})

# when flux unit is changed, the mouseover unit conversion should be
# skipped so that the plotted moment map remains in its original
# unit. setting back to the original flux unit also ensures that
# each iteration begins on the same unit so that every comparison
# is tested
uc.flux_unit.selected = new_flux_unit
label_mouseover._viewer_mouse_event(flux_viewer,
{'event': 'mousemove',
'domain': {'x': 0, 'y': 0}})
assert m_orig == label_mouseover.as_text()[0]
assert mm.moment.unit == new_mm_unit

# make sure mouseover info in flux unit is new moment map unit
# which should be flux/sb unit times spectral axis unit (e.g. MJy m / sr)
label_mouseover._viewer_mouse_event(flux_viewer,
{'event': 'mousemove',
'domain': {'x': 0, 'y': 0}})
m_orig = label_mouseover.as_text()[0]
assert ((new_flux_unit / angle_unit) * u.m).to_string() in m_orig

# 'jiggle' mouse so we can move it back
label_mouseover._viewer_mouse_event(flux_viewer,
{'event': 'mousemove',
'domain': {'x': 1, 'y': 1}})

# when flux unit is changed, the mouseover unit conversion should be
# skipped so that the plotted moment map remains in its original
# unit. setting back to the original flux unit also ensures that
# each iteration begins on the same unit so that every comparison
# is tested
uc.flux_unit.selected = new_flux_unit_str
label_mouseover._viewer_mouse_event(flux_viewer,
{'event': 'mousemove',
'domain': {'x': 0, 'y': 0}})
assert m_orig == label_mouseover.as_text()[0]

0 comments on commit 88dd54a

Please sign in to comment.