-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
implement unit conversion in specviz2d #3253
base: main
Are you sure you want to change the base?
Changes from all commits
086cfa4
a14a39f
8032fdb
35da355
b8eb091
34832f0
c9ef4e2
8ad5973
ef83da1
6010858
a06159a
e8d22ac
eb45c00
37e7b6c
19a2868
0a82322
2f1db3e
ea8cb25
ff6ac14
f2920e2
b876cb4
f946861
7c54327
0ffdb5d
98f2852
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -520,9 +520,18 @@ def test_spectral_extraction_with_correct_sum_units(cubeviz_helper, | |
cubeviz_helper.load_data(spectrum1d_cube_fluxunit_jy_per_steradian) | ||
spec_extr_plugin = cubeviz_helper.plugins['Spectral Extraction']._obj | ||
collapsed = spec_extr_plugin.extract() | ||
|
||
assert '_pixel_scale_factor' in collapsed.meta | ||
|
||
# Original units in Jy / sr | ||
expected_flux_values = [190., 590., 990., 1390., 1790., 2190., 2590., 2990., 3390., 3790.] | ||
# After collapsing, sr is removed via the scale factor and the extracted spectrum is in Jy | ||
expected_flux_values = [flux * collapsed.meta.get('_pixel_scale_factor') | ||
for flux in expected_flux_values] | ||
Comment on lines
+527
to
+530
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be done with array math instead of a list comprehension? |
||
|
||
np.testing.assert_allclose( | ||
collapsed.flux.value, | ||
[190., 590., 990., 1390., 1790., 2190., 2590., 2990., 3390., 3790.] | ||
expected_flux_values | ||
) | ||
assert collapsed.flux.unit == u.Jy | ||
assert collapsed.uncertainty.unit == u.Jy | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,10 +64,12 @@ def __init__(self, *args, **kwargs): | |
elif self.config == 'specviz': | ||
headers = ['spectral_axis', 'spectral_axis:unit', | ||
'index', 'value', 'value:unit'] | ||
|
||
elif self.config == 'specviz2d': | ||
# TODO: add "index" if/when specviz2d supports plotting spectral_axis | ||
headers = ['spectral_axis', 'spectral_axis:unit', | ||
'pixel_x', 'pixel_y', 'value', 'value:unit', 'viewer'] | ||
|
||
elif self.config == 'mosviz': | ||
headers = ['spectral_axis', 'spectral_axis:unit', | ||
'pixel_x', 'pixel_y', 'world_ra', 'world_dec', 'index', | ||
|
@@ -223,6 +225,11 @@ def _on_is_active_changed(self, *args): | |
def _on_viewer_key_event(self, viewer, data): | ||
if data['event'] == 'keydown' and data['key'] == 'm': | ||
row_info = self.coords_info.as_dict() | ||
if 'value' in row_info: | ||
# format and cast flux value for Table | ||
row_info['value'] = f"{row_info['value']:.5e}" | ||
else: | ||
row_info['value'] = '' | ||
Comment on lines
+228
to
+232
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this necessary? Can/should we move it to |
||
|
||
if 'viewer' in self.table.headers_avail: | ||
row_info['viewer'] = viewer.reference if viewer.reference is not None else viewer.reference_id # noqa | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -553,6 +553,7 @@ def _initialize_model_component(self, model_comp, comp_label, poly_order=None): | |||||
# equivs for spectral density and flux<>sb | ||||||
pixar_sr = masked_spectrum.meta.get('_pixel_scale_factor', 1.0) | ||||||
equivs = all_flux_unit_conversion_equivs(pixar_sr, init_x) | ||||||
equivs += u.spectral_density(init_x) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be in |
||||||
|
||||||
init_y = flux_conversion_general([init_y.value], | ||||||
init_y.unit, | ||||||
|
@@ -904,8 +905,17 @@ def _fit_model_to_spectrum(self, add_data): | |||||
return | ||||||
models_to_fit = self._reinitialize_with_fixed() | ||||||
|
||||||
masked_spectrum = self._apply_subset_masks(self.dataset.selected_spectrum, | ||||||
spec = self.dataset.selected_spectrum | ||||||
# Needs Attention: | ||||||
# should conversion occur before or after call to _apply_subset_masks? | ||||||
if spec.flux.unit.to_string != self.app._get_display_unit('flux'): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a method? Is it cheaper to compare units instead of casting to string?
Suggested change
|
||||||
equivalencies = u.spectral_density(self.dataset.selected_spectrum.spectral_axis) | ||||||
spec = self.dataset.selected_spectrum.with_flux_unit(self.app._get_display_unit('flux'), | ||||||
equivalencies=equivalencies) | ||||||
Comment on lines
+909
to
+914
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this all be moved into the |
||||||
|
||||||
masked_spectrum = self._apply_subset_masks(spec, | ||||||
self.spectral_subset) | ||||||
|
||||||
try: | ||||||
fitted_model, fitted_spectrum = fit_model_to_spectrum( | ||||||
masked_spectrum, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
from jdaviz.core.unit_conversion_utils import (all_flux_unit_conversion_equivs, | ||
check_if_unit_is_per_solid_angle, | ||
flux_conversion_general) | ||
from jdaviz.utils import flux_conversion | ||
|
||
__all__ = ['CoordsInfo'] | ||
|
||
|
@@ -433,6 +434,12 @@ def _image_viewer_update(self, viewer, x, y): | |
# use WCS to expose the wavelength for a 2d spectrum shown in pixel space | ||
try: | ||
wave, pixel = image.coords.pixel_to_world(x, y) | ||
if wave is not None: | ||
equivalencies = all_flux_unit_conversion_equivs(cube_wave=wave) | ||
wave = wave.to(self.app._get_display_unit('spectral'), | ||
equivalencies=equivalencies) | ||
self._dict['spectral_axis'] = wave.value | ||
self._dict['spectral_axis:unit'] = wave.unit.to_string() | ||
Comment on lines
+441
to
+442
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. where were these handled before (I don't see them in the else 🤔)? |
||
except Exception: # WCS might not be valid # pragma: no cover | ||
coords_status = False | ||
else: | ||
|
@@ -483,12 +490,21 @@ def _image_viewer_update(self, viewer, x, y): | |
|
||
if isinstance(viewer, (ImvizImageView, MosvizImageView, MosvizProfile2DView)): | ||
value = image.get_data(attribute)[int(round(y)), int(round(x))] | ||
|
||
if associated_dq_layers is not None: | ||
associated_dq_layer = associated_dq_layers[0] | ||
dq_attribute = associated_dq_layer.state.attribute | ||
dq_data = associated_dq_layer.layer.get_data(dq_attribute) | ||
dq_value = dq_data[int(round(y)), int(round(x))] | ||
|
||
unit = u.Unit(image.get_component(attribute).units) | ||
if (isinstance(viewer, MosvizProfile2DView) and unit != '' | ||
and unit != self.app._get_display_unit(attribute)): | ||
equivalencies = all_flux_unit_conversion_equivs(cube_wave=wave) | ||
value = flux_conversion(value, unit, self.app._get_display_unit(attribute), | ||
eqv=equivalencies) | ||
unit = self.app._get_display_unit(attribute) | ||
|
||
elif isinstance(viewer, (CubevizImageView, RampvizImageView)): | ||
arr = image.get_component(attribute).data | ||
unit = u.Unit(image.get_component(attribute).units) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -222,8 +222,11 @@ def test_load_single_image_multi_spec(mosviz_helper, mos_image, spectrum1d, mos_ | |||||||||
|
||||||||||
label_mouseover._viewer_mouse_event(spec2d_viewer, | ||||||||||
{'event': 'mousemove', 'domain': {'x': 10, 'y': 100}}) | ||||||||||
|
||||||||||
# Note: spectra2d Wave loaded in meters, but we respect one spectral unit, so the meters in | ||||||||||
# converted to Angstrom (the spectra1d spectral unit). | ||||||||||
Comment on lines
+226
to
+227
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain this a little more? Is it that a previous data entry was in angstroms or just that that is the default spectral display unit?
Suggested change
|
||||||||||
assert label_mouseover.as_text() == ('Pixel x=00010.0 y=00100.0 Value +8.12986e-01', | ||||||||||
'Wave 1.10000e-05 m', '') | ||||||||||
'Wave 1.10000e+05 Angstrom', '') | ||||||||||
assert label_mouseover.icon == 'c' | ||||||||||
|
||||||||||
# need to trigger a mouseleave or mouseover to reset the traitlets | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.