Skip to content
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

adjusting links to enable pixel, wavelength linking in specviz2d #2736

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ Bug Fixes

- Order of RGB preset colors now matches for less than and greater than 5 layers. [#2731]

- Fix subset linking/displaying between pixel/wavelength in Specviz2d viewers. [#2736]

Cubeviz
^^^^^^^

Expand Down
9 changes: 7 additions & 2 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,13 @@ def _link_new_data(self, reference_data=None, data_to_be_linked=None):
linked_data.ndim < 3 and # Cube linking requires special logic. See below
ref_data.ndim < 3)
):
links = [LinkSame(linked_data.components[0], ref_data.components[0]),
LinkSame(linked_data.components[1], ref_data.components[1])]
if self.config == 'specviz2d':
links = [LinkSameWithUnits(linked_data.components[0], ref_data.components[1]),
LinkSameWithUnits(linked_data.components[1], ref_data.components[3])]
else:
links = [LinkSame(linked_data.components[0], ref_data.components[0]),
LinkSame(linked_data.components[1], ref_data.components[1])]

dc.add_link(links)
return

Expand Down
90 changes: 90 additions & 0 deletions jdaviz/tests/test_subsets.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest
from astropy import units as u
from astropy.wcs import WCS
from astropy.tests.helper import assert_quantity_allclose
from astropy.utils.data import get_pkg_data_filename
from glue.core import Data
Expand Down Expand Up @@ -791,6 +792,95 @@ def test_only_overlapping_in_specviz2d(specviz2d_helper, mos_spectrum2d):
assert reg[1].lower.value == 7600 and reg[1].upper.value == 7800


def test_draw_2D_subset_specviz2d(specviz2d_helper):
# custom test data to predict values for different viewers
header = {
'WCSAXES': 2,
'CRPIX1': 0.0, 'CRPIX2': 8.5,
'CDELT1': 1E-06, 'CDELT2': 7.5E-05,
'CUNIT1': 'm', 'CUNIT2': 'deg',
'CTYPE1': 'WAVE', 'CTYPE2': 'OFFSET',
'CRVAL1': 0.0, 'CRVAL2': 5.0,
'RADESYS': 'ICRS', 'SPECSYS': 'BARYCENT'}
wcs = WCS(header)

x_values = np.linspace(0, 10, 128)
y_values = np.linspace(0, 5, 256)

data = np.sin(x_values[:, np.newaxis]) * np.cos(y_values) * u.one
spectrum_data = Spectrum1D(data, wcs=wcs, meta=header)
specviz2d_helper.load_data(spectrum_2d=spectrum_data)

viewer_1d = specviz2d_helper.app.get_viewer(
specviz2d_helper._default_spectrum_viewer_reference_name)
viewer_2d = specviz2d_helper.app.get_viewer(
specviz2d_helper._default_spectrum_2d_viewer_reference_name)

# create subset in 2d-viewer, want data in 1d-viewer
viewer_2d.apply_roi(XRangeROI(60, 80))
subset_drawn_2d = viewer_1d.native_marks[-1]

# get x and y components of 1D subset created to compute mask
y1 = subset_drawn_2d.y
x1 = subset_drawn_2d.x

subset_highlighted_region1 = x1[np.isfinite(y1)]
min_value_subset1 = np.min(subset_highlighted_region1)
max_value_subset1 = np.max(subset_highlighted_region1)

tolerance1 = 1e-6
expected_min1 = 6.e-5
expected_max1 = 8.e-05

assert np.allclose(min_value_subset1, expected_min1, atol=tolerance1)
assert np.allclose(max_value_subset1, expected_max1, atol=tolerance1)


def test_draw_1D_subset_specviz2d(specviz2d_helper):
viewer_1d = specviz2d_helper.app.get_viewer(
specviz2d_helper._default_spectrum_viewer_reference_name)
viewer_2d = specviz2d_helper.app.get_viewer(
specviz2d_helper._default_spectrum_2d_viewer_reference_name)

# Note/TO DO: 2d-viewer does not display subset if it is drawn before
# specviz2d.show() is called. Work around for this test case,
# similiar to Imviz, 2d-viewer limits set so subset mask can be applied to viewer.
viewer_2d.jdaviz_helper.viewers['spectrum-2d-viewer']._obj.shape = (155, 864)

# custom test data to predict values for different viewers
header = {
'WCSAXES': 2,
'CRPIX1': 0.0, 'CRPIX2': 8.5,
'CDELT1': 1E-06, 'CDELT2': 7.5E-05,
'CUNIT1': 'm', 'CUNIT2': 'deg',
'CTYPE1': 'WAVE', 'CTYPE2': 'OFFSET',
'CRVAL1': 0.0, 'CRVAL2': 5.0,
'RADESYS': 'ICRS', 'SPECSYS': 'BARYCENT'}
wcs = WCS(header)

x_values = np.linspace(0, 10, 128)
y_values = np.linspace(0, 5, 256)

data = np.sin(x_values[:, np.newaxis]) * np.cos(y_values) * u.one
spectrum_data = Spectrum1D(data, wcs=wcs, meta=header)
specviz2d_helper.load_data(spectrum_2d=spectrum_data)

# subset drawn in 1d-viewer, want data in 2d-viewer
viewer_1d.apply_roi(XRangeROI(.0001, .0002))
subset_drawn_1d = viewer_2d.native_marks[-1].image
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the 3d image array this way is loading incorrectly only in the test. It should be (155, 864, 4) [which is how it loads in a Notebook], in the test file it is loading a zero array of shape (10,10,4), so the test thinks there's no mask. This is the last hold up.


subset_highlighted_region2 = np.atleast_1d(np.nonzero(subset_drawn_1d))[1]
min_value_subset2 = np.min(subset_highlighted_region2)
max_value_subset2 = np.max(subset_highlighted_region2)

tolerance2 = 2
expected_min2 = 338
expected_max2 = 674

assert np.allclose(min_value_subset2, expected_min2, atol=tolerance2)
assert np.allclose(max_value_subset2, expected_max2, atol=tolerance2)


def test_multi_mask_subset(specviz_helper, spectrum1d):
specviz_helper.load_data(spectrum1d)
viewer = specviz_helper.app.get_viewer(specviz_helper._default_spectrum_viewer_reference_name)
Expand Down
Loading