Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
and add change log. [ci skip] [rtd skip]
  • Loading branch information
pllim committed Mar 28, 2023
1 parent 96f241d commit 29d8b96
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ Mosviz
Specviz
^^^^^^^

* Re-enabled unit conversion support. [#2048]

Specviz2d
^^^^^^^^^

Expand Down
27 changes: 20 additions & 7 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from glue.config import colormaps, data_translator
from glue.config import settings as glue_settings
from glue.core import BaseData, HubListener, Data, DataCollection
from glue.core.link_helpers import LinkSame
from glue.core.link_helpers import LinkSame, LinkSameWithUnits
from glue.plugins.wcs_autolinking.wcs_autolinking import WCSLink, IncompatibleWCS
from glue.core.message import (DataCollectionAddMessage,
DataCollectionDeleteMessage,
Expand Down Expand Up @@ -75,10 +75,23 @@ class UnitConverterWithSpectral:
def equivalent_units(self, data, cid, units):
if cid.label == "flux":
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',
'W / (m2 Hz)', 'W / (Hz m2)', # Order is different in astropy v5.3
'eV / (s m2 Hz)', 'eV / (Hz s m2)',
'erg / (s cm2)',
'erg / (s cm2 um)', 'erg / (um s cm2)',
'erg / (s cm2 Angstrom)', 'erg / (Angstrom s cm2)',
'erg / (s cm2 Hz)', 'erg / (Hz s cm2)',
'ph / (s cm2 um)', 'ph / (um s cm2)',
'ph / (s cm2 Angstrom)', 'ph / (Angstrom s cm2)',
'ph / (s cm2 Hz)', 'ph / (Hz s cm2)'
])
else: # spectral axis
eqv = u.spectral()
return map(str, u.Unit(units).find_equivalent_units(
include_prefix_units=True, equivalencies=eqv))
list_of_units = map(str, u.Unit(units).find_equivalent_units(
include_prefix_units=True, equivalencies=u.spectral()))
return list_of_units

def to_unit(self, data, cid, values, original_units, target_units):
# Given a glue data object (data), a component ID (cid), the values
Expand Down Expand Up @@ -475,7 +488,7 @@ def _link_new_data(self, reference_data=None, data_to_be_linked=None):
if isinstance(linked_data.coords, SpectralCoordinates):
wc_old = ref_data.world_component_ids[-1]
wc_new = linked_data.world_component_ids[0]
self.data_collection.add_link(LinkSame(wc_old, wc_new))
self.data_collection.add_link(LinkSameWithUnits(wc_old, wc_new))
return

try:
Expand Down Expand Up @@ -521,8 +534,8 @@ def _link_new_data(self, reference_data=None, data_to_be_linked=None):
else:
continue

links.append(LinkSame(ref_data.pixel_component_ids[ref_index],
linked_data.pixel_component_ids[linked_index]))
links.append(LinkSameWithUnits(ref_data.pixel_component_ids[ref_index],
linked_data.pixel_component_ids[linked_index]))

dc.add_link(links)

Expand Down
4 changes: 2 additions & 2 deletions jdaviz/configs/mosviz/plugins/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from astropy.io.registry import IORegistryError
from astropy.wcs import WCS
from glue.core.data import Data
from glue.core.link_helpers import LinkSame
from glue.core.link_helpers import LinkSameWithUnits
from specutils import Spectrum1D, SpectrumList, SpectrumCollection
from specutils.io.default_loaders.jwst_reader import identify_jwst_s2d_multi_fits

Expand Down Expand Up @@ -141,7 +141,7 @@ def link_data_in_table(app, data_obj=None):
wc_spec_1d = app.session.data_collection[spec_1d].world_component_ids
wc_spec_2d = app.session.data_collection[spec_2d].world_component_ids

wc_spec_ids.append(LinkSame(wc_spec_1d[0], wc_spec_2d[1]))
wc_spec_ids.append(LinkSameWithUnits(wc_spec_1d[0], wc_spec_2d[1]))

app.session.data_collection.add_link(wc_spec_ids)

Expand Down
2 changes: 2 additions & 0 deletions jdaviz/configs/specviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ def set_plot_axes(self):
x_unit = u.Unit(self.state.x_display_unit)
if x_unit.is_equivalent(u.m):
spectral_axis_unit_type = "Wavelength"
elif x_unit.is_equivalent(u.Hz):
spectral_axis_unit_type = "Frequency"
elif x_unit.is_equivalent(u.pixel):
spectral_axis_unit_type = "Pixel"
else:
Expand Down
102 changes: 101 additions & 1 deletion notebooks/concepts/specviz_glue_unit_conversion.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,110 @@
"specviz.load_data(spec2, data_label=\"mJy_GHz\")"
]
},
{
"cell_type": "markdown",
"id": "e6012551",
"metadata": {},
"source": [
"Change the spectral axis display unit to GHz."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6a4611b3",
"metadata": {},
"outputs": [],
"source": [
"viewer = specviz.app.get_viewer(\"spectrum-viewer\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac319e71",
"metadata": {},
"outputs": [],
"source": [
"viewer.state.x_display_unit = \"GHz\"\n",
"viewer.set_plot_axes()"
]
},
{
"cell_type": "markdown",
"id": "b2e84da2",
"metadata": {},
"source": [
"Change the flux axis display unit to FLAM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4892cb38",
"metadata": {},
"outputs": [],
"source": [
"FLAM = u.erg / (u.s * u.cm * u.cm * u.AA)\n",
"\n",
"# If astropy can do it, Jdaviz should too.\n",
"spec1.flux.to(FLAM, u.spectral_density(spec1.spectral_axis))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3be87c54",
"metadata": {},
"outputs": [],
"source": [
"viewer.state.y_display_unit = FLAM.to_string()\n",
"viewer.set_plot_axes()"
]
},
{
"cell_type": "markdown",
"id": "8be218b4",
"metadata": {},
"source": [
"Change the spectral axis again, this time to Angstrom."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8f9073f7",
"metadata": {},
"outputs": [],
"source": [
"viewer.state.x_display_unit = \"Angstrom\"\n",
"viewer.set_plot_axes()"
]
},
{
"cell_type": "markdown",
"id": "fa487529",
"metadata": {},
"source": [
"Change everything back to original units."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8c110686",
"metadata": {},
"outputs": [],
"source": [
"viewer.state.x_display_unit = \"micron\"\n",
"viewer.state.y_display_unit = \"Jy\"\n",
"viewer.set_plot_axes()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed8974c9",
"id": "87642b18",
"metadata": {},
"outputs": [],
"source": []
Expand Down

0 comments on commit 29d8b96

Please sign in to comment.