Skip to content

Commit

Permalink
add toggle feature between surface brightness and flux, currently inc…
Browse files Browse the repository at this point in the history
…omplete
  • Loading branch information
gibsongreen committed Apr 2, 2024
1 parent d5a049f commit f326273
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 24 deletions.
39 changes: 38 additions & 1 deletion jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def equivalent_units(self, data, cid, units):
eqv = u.spectral_density(1 * u.m) # Value does not matter here.
list_of_units = set(list(map(str, u.Unit(units).find_equivalent_units(
include_prefix_units=True, equivalencies=eqv))) + [
'Jy', 'mJy', 'uJy',
'Jy', 'mJy', 'uJy', 'MJy',
'W / (m2 Hz)', 'W / (Hz m2)', # Order is different in astropy v5.3
'eV / (s m2 Hz)', 'eV / (Hz s m2)',
'erg / (s cm2)',
Expand All @@ -95,13 +95,50 @@ def to_unit(self, data, cid, values, original_units, target_units):
# gives the units of the values array, which might not be the same
# as the original native units of the component in the data.
if cid.label == "flux":
# to debug
print('orginal_units : ', original_units, "target_units : ", target_units)

spec = data.get_object(cls=Spectrum1D)
if len(values) == 2:
# Need this for setting the y-limits
spec_limits = [spec.spectral_axis[0].value, spec.spectral_axis[-1].value]
eqv = u.spectral_density(spec_limits*spec.spectral_axis.unit)

elif '_pixel_scale_factor' in spec.meta:
if (u.sr in u.Unit(original_units).bases) and \
(u.sr not in u.Unit(target_units).bases):
# sb -> flux

# to debug
print('first if')
print('values before')
print(values)

converted_values = values * spec.meta['_pixel_scale_factor']

# to debug
print(converted_values)

return converted_values
else:
# elif (u.sr in u.Unit(target_units).bases) and \
# (u.sr not in u.Unit(original_units).bases):
# flux -> sb

# to debug
print('second elif')
print('values before')
print(values)

converted_values = values / spec.meta['_pixel_scale_factor']
# to debug
print(converted_values)

return converted_values

else:
eqv = u.spectral_density(spec.spectral_axis)

else: # spectral axis
eqv = u.spectral()

Expand Down
1 change: 1 addition & 0 deletions jdaviz/configs/cubeviz/cubeviz.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tray:
- g-subset-plugin
- g-markers
- cubeviz-slice
- g-unit-conversion
- g-gaussian-smooth
- g-collapse
- g-model-fitting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import numpy as np
import astropy
from astropy import units as u
from astropy.utils.decorators import deprecated
from astropy.nddata import (
NDDataArray, StdDevUncertainty
Expand Down Expand Up @@ -524,13 +523,3 @@ def _live_update(self, event={}):
for mark in self.marks.values():
mark.update_xy(sp.spectral_axis.value, sp.flux.value)
mark.visible = True

def translate_units(self, collapsed_spec):
# remove sr
if u.sr in collapsed_spec._unit.bases:
collapsed_spec._data *= collapsed_spec.meta['_pixel_scale_factor']
collapsed_spec._unit *= u.sr
# add sr
elif u.sr not in collapsed_spec._unit.bases:
collapsed_spec._data /= collapsed_spec.meta['_pixel_scale_factor']
collapsed_spec._unit /= u.sr
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@
</div>
</div>

<div>
<v-row>
<v-switch
v-model="translate_unit"
label="Translate units"
hint="Toggle between surface brightness and flux units"
persistent-hint
></v-switch>
</v-row>
</div>

<div @mouseover="() => active_step='ext'">
<j-plugin-section-header :active="active_step==='ext'">Extract</j-plugin-section-header>

Expand Down Expand Up @@ -262,6 +273,5 @@
</v-overlay>
</div>
</div>

</j-tray-plugin>
</template>
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ def test_cube_extraction_with_nan(cubeviz_helper, image_cube_hdu_obj):
assert_allclose(sp_subset.flux.value, 12) # (4 x 4) - 4


# this test will have to move
def test_unit_translation(cubeviz_helper):
# custom cube so we have PIXAR_SR in metadata, and flux units = Jy/pix
wcs_dict = {"CTYPE1": "WAVE-LOG", "CTYPE2": "DEC--TAN", "CTYPE3": "RA---TAN",
Expand Down
42 changes: 37 additions & 5 deletions jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import numpy as np
from astropy import units as u
from traitlets import List, Unicode, observe
from traitlets import List, Unicode, observe, Bool

from jdaviz.core.events import GlobalDisplayUnitChanged
from jdaviz.core.registries import tray_registry
from jdaviz.core.template_mixin import PluginTemplateMixin, UnitSelectPluginComponent, PluginUserApi
from jdaviz.core.template_mixin import (PluginTemplateMixin, UnitSelectPluginComponent,
PluginUserApi)
from jdaviz.core.validunits import (create_spectral_equivalencies_list,
create_flux_equivalencies_list)

Expand Down Expand Up @@ -51,6 +52,7 @@ class UnitConversion(PluginTemplateMixin):
spectral_unit_selected = Unicode().tag(sync=True)
flux_unit_items = List().tag(sync=True)
flux_unit_selected = Unicode().tag(sync=True)
translate_unit = Bool(False).tag(sync=True)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
Expand Down Expand Up @@ -125,6 +127,20 @@ def _on_glue_y_display_unit_changed(self, y_unit):
self.flux_unit.choices = choices
self.flux_unit.selected = y_unit

def translate_units(self):
spec_units = u.Unit(self.spectrum_viewer.state.y_display_unit)
# remove sr
if u.sr in spec_units.bases:
spec_units *= u.sr
# update display units
self.spectrum_viewer.state.y_display_unit = str(spec_units)

# add sr
elif u.sr not in spec_units.bases:
spec_units /= u.sr
# update display units
self.spectrum_viewer.state.y_display_unit = str(spec_units)

@observe('spectral_unit_selected')
def _on_spectral_unit_changed(self, *args):
self.hub.broadcast(GlobalDisplayUnitChanged('spectral',
Expand All @@ -136,9 +152,25 @@ def _on_spectral_unit_changed(self, *args):

@observe('flux_unit_selected')
def _on_flux_unit_changed(self, *args):
self.hub.broadcast(GlobalDisplayUnitChanged('flux',
self.flux_unit.selected,
sender=self))
# self.hub.broadcast(GlobalDisplayUnitChanged('flux',
# self.flux_unit.selected,
# sender=self))
yunit = _valid_glue_display_unit(self.flux_unit.selected, self.spectrum_viewer, 'y')
print('y_unit = ', yunit)
if self.spectrum_viewer.state.y_display_unit != yunit:
self.spectrum_viewer.state.y_display_unit = yunit
self.hub.broadcast(GlobalDisplayUnitChanged('flux',
self.flux_unit.selected,
sender=self))

@observe('translate_unit')
def _translate(self, *args):
# make sure we have a scale factor
specs_w_factor = [spec for spec in self.app.data_collection
if "_pixel_scale_factor" in spec.meta]

if specs_w_factor:
self.translate_units()
else:
raise ValueError("No collapse spectra in data collection, \
please collapse a spectrum first.")
14 changes: 12 additions & 2 deletions jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,20 @@
attach
:items="flux_unit_items.map(i => i.label)"
v-model="flux_unit_selected"
label="Flux Unit"
hint="Global display unit for flux."
:label="translate_unit ? 'Flux Unit' : 'Surface Brightness'"
:hint="translate_unit ? 'Global display unit for flux.' : 'Global display unit for surface brightness.'"
persistent-hint
></v-select>
</v-row>

<v-row v-if="config == 'cubeviz'">
<v-switch
v-model="translate_unit"
label="Translate units"
hint="Toggle between surface brightness and flux units"
persistent-hint
:disabled="config === 'specviz'"
></v-switch>
</v-row>
</j-tray-plugin>
</template>
22 changes: 19 additions & 3 deletions jdaviz/configs/specviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,25 @@ def set_plot_axes(self):
y_display_unit = self.state.y_display_unit
y_unit = u.Unit(y_display_unit) if y_display_unit else u.dimensionless_unscaled

if y_unit.is_equivalent(u.Jy / u.sr):
flux_unit_type = "Surface brightness"
elif y_unit.is_equivalent(u.erg / (u.s * u.cm**2)):
# Get local units.
locally_defined_flux_units = [
u.Jy, u.mJy, u.uJy, u.MJy,
u.W / (u.m**2 * u.Hz),
u.eV / (u.s * u.m**2 * u.Hz),
u.erg / (u.s * u.cm**2),
u.erg / (u.s * u.cm**2 * u.Angstrom),
u.erg / (u.s * u.cm**2 * u.Hz),
u.ph / (u.s * u.cm**2 * u.Angstrom),
u.ph / (u.s * u.cm**2 * u.Hz)
]

locally_defined_sb_units = [
unit / u.sr for unit in locally_defined_flux_units
]

if any(y_unit.is_equivalent(unit) for unit in locally_defined_sb_units):
flux_unit_type = "Surface Brightness"
elif any(y_unit.is_equivalent(unit) for unit in locally_defined_flux_units):
flux_unit_type = 'Flux'
elif y_unit.is_equivalent(u.electron / u.s) or y_unit.physical_type == 'dimensionless':
# electron / s or 'dimensionless_unscaled' should be labeled counts
Expand Down
2 changes: 1 addition & 1 deletion jdaviz/core/validunits.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def create_flux_equivalencies_list(flux_unit, spectral_axis_unit):
return []

# Get local units.
locally_defined_flux_units = ['Jy', 'mJy', 'uJy',
locally_defined_flux_units = ['Jy', 'mJy', 'uJy', 'MJy',
'W / (m2 Hz)',
'eV / (s m2 Hz)',
'erg / (s cm2)',
Expand Down

0 comments on commit f326273

Please sign in to comment.