Skip to content

Commit

Permalink
add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen committed May 6, 2024
1 parent 7dfe830 commit 8387d03
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 21 deletions.
18 changes: 0 additions & 18 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,6 @@ def to_unit(self, data, cid, values, original_units, target_units):
# Flux -> Surface Brightness
eqv = u.spectral_density(spec.spectral_axis)

# below is the generalized version
'''
eqv = [(u.Unit(original_units),
u.Unit(target_units),
lambda x: (x * spec.meta['_pixel_scale_factor']),
lambda x: x)]
'''
# if spectrum data collection item is in Flux units
elif u.sr not in spec.unit.bases:
# Data item in data collection does not update from conversion/translation.
Expand All @@ -150,17 +143,6 @@ def to_unit(self, data, cid, values, original_units, target_units):
# Surface Brightness -> Flux
eqv = u.spectral_density(spec.spectral_axis)

# below is the generalized version
'''
eqv = [(u.Unit(original_units),
u.Unit(target_units),
lambda x: (x / spec.meta['_pixel_scale_factor']),
lambda x: x)]
'''

# flux cid, but neither Surface Brightness nor Flux units
else:
raise ValueError('flux cid, but not a flux nor surface brightness unit.')
elif len(values) == 2:
# Need this for setting the y-limits
spec_limits = [spec.spectral_axis[0].value, spec.spectral_axis[-1].value]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def test_unit_translation(cubeviz_helper):
cubeviz_helper.load_regions(CirclePixelRegion(center, radius=2.5))

uc_plg = cubeviz_helper.plugins['Unit Conversion']

# we can get rid of this after all spectra pass through
# spectral extraction plugin
extract_plg = cubeviz_helper.plugins['Spectral Extraction']

extract_plg.aperture = extract_plg.aperture.choices[-1]
Expand All @@ -160,13 +161,20 @@ def test_unit_translation(cubeviz_helper):
# When the dropdown is displayed, this ensures the loaded
# data collection item units will be used for translations.
uc_plg._obj.show_translator = True
assert uc_plg._obj.flux_or_sb_selected == 'Flux'

# to have access to display units
viewer_1d = cubeviz_helper.app.get_viewer(
cubeviz_helper._default_spectrum_viewer_reference_name)

# for testing _set_flux_or_sb()
uc_plg._obj.show_translator = False

# change global y-units from Flux -> Surface Brightness
uc_plg._obj.flux_or_sb_selected = 'Surface Brightness'

uc_plg._obj.show_translator = True
assert uc_plg._obj.flux_or_sb_selected == 'Surface Brightness'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

# check if units translated
Expand Down Expand Up @@ -198,6 +206,29 @@ def test_sb_unit_conversion(cubeviz_helper):
assert y_display_unit == u.Jy / u.sr

# Try a second conversion
uc_plg.flux_or_sb_unit = 'AB / sr'
uc_plg.flux_or_sb_unit = 'W / Hz sr m2'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)
assert y_display_unit == u.AB / u.sr
assert y_display_unit == u.Unit("W / (Hz sr m2)")

# really a translation test, test_unit_translation loads a Flux
# cube, this test load a Surface Brightness Cube, this ensures
# two-way translation
uc_plg.flux_or_sb_unit = 'MJy / sr'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

# we can get rid of this after all spectra pass through
# spectral extraction plugin
extract_plg = cubeviz_helper.plugins['Spectral Extraction']
extract_plg.aperture = extract_plg.aperture.choices[-1]
extract_plg.aperture_method.selected = 'Exact'
extract_plg.wavelength_dependent = True
extract_plg.function = 'Sum'
extract_plg.reference_spectral_value = 0.000001
extract_plg.collapse_to_spectrum()

uc_plg._obj.show_translator = True
uc_plg._obj.flux_or_sb_selected = 'Flux'
uc_plg.flux_or_sb_unit = 'MJy'
y_display_unit = u.Unit(viewer_1d.state.y_display_unit)

assert y_display_unit == u.MJy
42 changes: 42 additions & 0 deletions jdaviz/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import pytest

import numpy as np
from astropy import units as u
from astropy.wcs import WCS
from specutils import Spectrum1D
from jdaviz import Application, Specviz
from jdaviz.configs.default.plugins.gaussian_smooth.gaussian_smooth import GaussianSmooth
from jdaviz.app import UnitConverterWithSpectral as uc


# This applies to all viz but testing with Imviz should be enough.
Expand Down Expand Up @@ -192,3 +196,41 @@ def test_data_associations(imviz_helper):
with pytest.raises(ValueError):
# ensure the parent actually exists:
imviz_helper.load_data(data_child, data_label='child_data', parent='absent parent')


def test_to_unit(cubeviz_helper):
# custom cube to have Surface Brightness units
wcs_dict = {"CTYPE1": "WAVE-LOG", "CTYPE2": "DEC--TAN", "CTYPE3": "RA---TAN",
"CRVAL1": 4.622e-7, "CRVAL2": 27, "CRVAL3": 205,
"CDELT1": 8e-11, "CDELT2": 0.0001, "CDELT3": -0.0001,
"CRPIX1": 0, "CRPIX2": 0, "CRPIX3": 0, "PIXAR_SR": 8e-11}
w = WCS(wcs_dict)
flux = np.zeros((30, 20, 3001), dtype=np.float32)
flux[5:15, 1:11, :] = 1
cube = Spectrum1D(flux=flux * (u.MJy / u.sr), wcs=w, meta=wcs_dict)
cubeviz_helper.load_data(cube, data_label="test")

# this can be removed once spectra pass through spectral extraction
extract_plg = cubeviz_helper.plugins['Spectral Extraction']

extract_plg.aperture = extract_plg.aperture.choices[-1]
extract_plg.aperture_method.selected = 'Exact'
extract_plg.wavelength_dependent = True
extract_plg.function = 'Sum'
# set so pixel scale factor != 1
extract_plg.reference_spectral_value = 0.000001

extract_plg.collapse_to_spectrum()

cid = cubeviz_helper.app.data_collection[0].data.find_component_id('flux')
data = cubeviz_helper.app.data_collection[-1].data
values = 1
original_units = u.MJy / u.sr
target_units = u.MJy

value = uc.to_unit(cubeviz_helper, data, cid, values, original_units, target_units)

assert np.allclose(value, 4.7945742429049767e-11)

original_units = u.MJy
target_units = u.MJy / u.sr

0 comments on commit 8387d03

Please sign in to comment.