Skip to content

Commit

Permalink
update conversion logic, .pix support for counts
Browse files Browse the repository at this point in the history
  • Loading branch information
gibsongreen committed Jul 24, 2024
1 parent e7fc511 commit 728c0ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 35 deletions.
58 changes: 26 additions & 32 deletions jdaviz/configs/specviz/plugins/unit_conversion/unit_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
SelectPluginComponent, PluginUserApi)
from jdaviz.core.validunits import (create_spectral_equivalencies_list,
create_flux_equivalencies_list,
create_sb_equivalencies_list,
check_if_unit_is_per_solid_angle,
units_to_strings,
create_angle_equivalencies_list)
Expand Down Expand Up @@ -110,22 +109,17 @@ def __init__(self, *args, **kwargs):
self.flux_unit = UnitSelectPluginComponent(self,
items='flux_unit_items',
selected='flux_unit_selected')

self.angle_unit = UnitSelectPluginComponent(self,
items='angle_unit_items',
selected='angle_unit_selected')
items='angle_unit_items',
selected='angle_unit_selected')

@property
def user_api(self):
if self.app.config == 'cubeviz':
expose = ('spectral_unit', 'flux_or_sb', 'flux_unit', 'sb_unit')
elif self.app.config == 'specviz' and not self.flux_or_sb_config_disabler:
expose = ('spectral_unit', 'flux_unit', 'sb_unit')
elif self.flux_or_sb_config_disabler == 'Flux':
expose = ('spectral_unit', 'sb_unit')
else: # self.flux_or_sb_config_disabler == 'Surface Brightness'
expose = ('spectral_unit', 'flux_unit')
return PluginUserApi(self, expose=expose)
return PluginUserApi(self, expose=('spectral_unit', 'flux_or_sb', 'flux_unit', 'angle_unit')) # noqa
else:
return PluginUserApi(self, expose=('spectral_unit', 'flux_unit', 'angle_unit'))

def _on_glue_x_display_unit_changed(self, x_unit):
if x_unit is None:
Expand Down Expand Up @@ -166,6 +160,7 @@ def _on_glue_y_display_unit_changed(self, y_unit):
x_u = u.Unit(self.spectral_unit.selected)
y_unit = _valid_glue_display_unit(y_unit, self.spectrum_viewer, 'y')
y_u = u.Unit(y_unit)
sb_unit = self.flux_unit.selected + ' / ' + self.angle_unit.selected

if flux_or_sb == 'Flux' and y_unit != self.flux_unit.selected:
flux_choices = create_flux_equivalencies_list(y_u, x_u)
Expand All @@ -176,18 +171,20 @@ def _on_glue_y_display_unit_changed(self, y_unit):

self.flux_unit.choices = flux_choices
self.flux_unit.selected = y_unit

### TODO
elif flux_or_sb == 'Surface Brightness' and y_unit != (self.flux_unit.selected + ' \ ' + self.angle_unit.selected):
if self.app.config == 'cubeviz':
flux_choices = create_flux_equivalencies_list(y_u * u.sr, x_u)
self.flux_unit.choices = flux_choices

elif flux_or_sb == 'Surface Brightness' and y_unit != sb_unit:
flux_choices = create_flux_equivalencies_list(y_u * u.sr, x_u)
self.flux_unit.choices = flux_choices

if self.app.data_collection[0]:
self.angle_unit.choices = create_angle_equivalencies_list(self.app.data_collection[0].get_object()._unit)
dc_unit = self.app.data_collection[0].get_object()._unit
self.angle_unit.choices = create_angle_equivalencies_list(dc_unit)
self.angle_unit.selected = self.angle_unit.choices[0]
self.sb_unit = self.flux_unit.selected + " / " + self.angle_unit.selected

if check_if_unit_is_per_solid_angle(self.spectrum_viewer.state.y_display_unit):
self.flux_or_sb = 'Flux'

@observe('spectral_unit_selected')
def _on_spectral_unit_changed(self, *args):
xunit = _valid_glue_display_unit(self.spectral_unit.selected, self.spectrum_viewer, 'x')
Expand Down Expand Up @@ -224,9 +221,6 @@ def _on_flux_unit_changed(self, msg):
name = msg.get('name')
# determine if flux or surface brightness unit was changed by user
if name == 'flux_unit_selected':
with open('example.txt', 'a') as file:
if self.app.data_collection and len(self.app.data_collection) > 0:
file.write(f'select flux unit \n')
# when the configuration is Specviz, translation is not currently supported.
# If in Cubeviz, all spectra pass through Spectral Extraction plugin and will
# have a scale factor assigned in the metadata, enabling translation.
Expand All @@ -235,7 +229,11 @@ def _on_flux_unit_changed(self, msg):
f"Unit translation between Flux and Surface Brightness "
f"is not supported in {self.app.config}."
)
flux_or_sb = self.flux_unit.selected
current_y_unit = self.spectrum_viewer.state.y_display_unit
if self.angle_unit.selected and check_if_unit_is_per_solid_angle(current_y_unit):
flux_or_sb = self.flux_unit.selected + ' / ' + self.angle_unit.selected
else:
flux_or_sb = self.flux_unit.selected
untranslatable_units = self._untranslatable_units
# disable translator if flux unit is untranslatable,
# still can convert flux units, this just disables flux
Expand All @@ -258,10 +256,11 @@ def _on_flux_unit_changed(self, msg):
flux_or_sb,
sender=self))
self.spectrum_viewer.reset_limits()

with open('example.txt', 'a') as file:
if self.app.data_collection and len(self.app.data_collection) > 0:
file.write(f'post flux unit \n')

if not check_if_unit_is_per_solid_angle(self.spectrum_viewer.state.y_display_unit):
self.flux_or_sb.selected = 'Flux'
else:
self.flux_or_sb = 'Surface Brightness'

if (
len(self.app.data_collection) > 0
Expand All @@ -274,9 +273,6 @@ def _translate(self, flux_or_sb=None):
if self.app.config == 'specviz':
return

with open('example.txt', 'a') as file:
if self.app.data_collection:
file.write(f'{self.flux_or_sb_selected}\n')
# we want to raise an error if a user tries to translate with an
# untranslated Flux unit using the API
untranslatable_units = self._untranslatable_units
Expand All @@ -303,15 +299,13 @@ def _translate(self, flux_or_sb=None):
spec_units *= u.sr
# update display units
self.spectrum_viewer.state.y_display_unit = str(spec_units)
#self.flux_or_sb.selected = 'Flux'

# Flux -> Surface Brightness
elif (not check_if_unit_is_per_solid_angle(spec_units)
and flux_or_sb == 'Surface Brightness'):
spec_units /= u.sr
# update display units
self.spectrum_viewer.state.y_display_unit = str(spec_units)
#self.flux_or_sb.selected = 'Surface Brightness'
# entered the translator when we shouldn't translate
else:
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
></v-select>
</v-row>

<v-row v-if="flux_or_sb_config_disabler === 'Surface Brightness' || config == 'cubeviz'">
<v-row>
<v-select
:menu-props="{ left: true }"
attach
Expand All @@ -31,7 +31,7 @@
></v-select>
</v-row>

<v-row v-if="flux_or_sb_config_disabler === 'Surface Brightness' || config == 'cubeviz'">
<v-row>
<v-select
:menu-props="{ left: true }"
attach
Expand All @@ -43,7 +43,7 @@
></v-select>
</v-row>

<v-row v-if="flux_or_sb_config_disabler === 'Flux' || config == 'cubeviz'">
<v-row>
<v-text-field
v-model="sb_unit"
label="Surface Brightness Unit"
Expand Down
4 changes: 4 additions & 0 deletions jdaviz/core/validunits.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def create_sb_equivalencies_list(sb_unit, spectral_axis_unit):
# Concatenate both lists with the local units coming first.
return sorted(units_to_strings(local_units)) + sb_unit_equivalencies_titles


def create_angle_equivalencies_list(unit):
# first, convert string to u.Unit obj.
# this will take care of some formatting consistency like
Expand All @@ -133,6 +134,8 @@ def create_angle_equivalencies_list(unit):
elif isinstance(unit, str):
unit = u.Unit(unit)
unit_str = unit.to_string()
elif unit == 'ct':
return ['pix']
else:
raise ValueError('Unit must be u.Unit, or string that can be converted into a u.Unit')

Expand All @@ -144,6 +147,7 @@ def create_angle_equivalencies_list(unit):
# this could be where force / u.pix
return ['pix']


def check_if_unit_is_per_solid_angle(unit):
"""
Check if a given u.Unit or unit string (that can be converted to
Expand Down

0 comments on commit 728c0ae

Please sign in to comment.