From 85d4037e69a04a1b2cf81f67d7ee0ae6455ad840 Mon Sep 17 00:00:00 2001 From: gibsongreen Date: Thu, 29 Feb 2024 16:27:41 -0500 Subject: [PATCH 1/5] adjusting links to enable pixel, wavelength linking in specviz2d --- jdaviz/app.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/jdaviz/app.py b/jdaviz/app.py index 2a462fc546..d031cab2d2 100644 --- a/jdaviz/app.py +++ b/jdaviz/app.py @@ -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 From 44edecd5c41e923e7e6db9d920c1725f21ebf3fb Mon Sep 17 00:00:00 2001 From: gibsongreen Date: Fri, 8 Mar 2024 01:15:49 -0500 Subject: [PATCH 2/5] added changes log, pre-science test case that fails currently --- CHANGES.rst | 2 ++ jdaviz/tests/test_subsets.py | 70 ++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index c8bf57f547..44b0a27744 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 ^^^^^^^ diff --git a/jdaviz/tests/test_subsets.py b/jdaviz/tests/test_subsets.py index 63cccdf374..6c2b58478e 100644 --- a/jdaviz/tests/test_subsets.py +++ b/jdaviz/tests/test_subsets.py @@ -791,6 +791,76 @@ def test_only_overlapping_in_specviz2d(specviz2d_helper, mos_spectrum2d): assert reg[1].lower.value == 7600 and reg[1].upper.value == 7800 +def test_subset_linking_specviz2d(specviz2d_helper, mos_spectrum2d): + specviz2d_helper.load_data(spectrum_2d=mos_spectrum2d) + 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) + + print(np.min(mos_spectrum2d.data)) + print(np.max(mos_spectrum2d.data)) + + # appear to be in meters + viewer_2d.apply_roi(XRangeROI(6400, 6800)) + + # reg1 = specviz2d_helper.app.get_subsets("Subset 1") + # print(reg1) + + # subset drawn in 2d viewer, want data in the 1d viewer + subset_drawn_2d = viewer_1d.native_marks[-1] + + # get x and y components to compute subset mask + y1 = subset_drawn_2d.y + x1 = subset_drawn_2d.x + + # print(len(y1)) + # print(len(x1)) + + sub_highlighted1 = x1[np.isfinite(y1)] + min_sub1 = np.min(sub_highlighted1) + max_sub1 = np.max(sub_highlighted1) + + # print(min_sub1) + # print(max_sub1) + + ''' + Need to update asserts with pixel to wavelength science case + ''' + + assert min_sub1 == 1 + assert max_sub1 == 1 + + # appear to be in meters + viewer_1d.apply_roi(XRangeROI(6000, 6400)) + + # reg2 = specviz2d_helper.app.get_subsets("Subset 1") + + # subset drawn in 1d viewer, want data in 2d viewer + subset_drawn_1d = viewer_1d.native_marks[-1] + + # get x and y components to compute subset mask + y2 = subset_drawn_1d.y + x2 = subset_drawn_1d.x + + sub_highlighted2 = x2[np.isfinite(y2)] + min_sub2 = np.min(sub_highlighted2) + max_sub2 = np.max(sub_highlighted2) + + # print(len(y2)) + # print(len(x2)) + + # reg2 = specviz2d_helper.app.get_subsets("Subset 1") + # print(reg2) + + ''' + Need to update asserts with wavelength to pixel science case + ''' + + assert min_sub2 == 1 + assert max_sub2 == 1 + + 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) From e9e7f234621eb71777107077e17bb2b9b7269d34 Mon Sep 17 00:00:00 2001 From: gibsongreen Date: Fri, 8 Mar 2024 16:31:43 -0500 Subject: [PATCH 3/5] add link testing between viewers, drawing in 1d working in notebook, not in testing --- jdaviz/tests/test_subsets.py | 109 ++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/jdaviz/tests/test_subsets.py b/jdaviz/tests/test_subsets.py index 6c2b58478e..fd8664e67e 100644 --- a/jdaviz/tests/test_subsets.py +++ b/jdaviz/tests/test_subsets.py @@ -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 @@ -791,74 +792,92 @@ def test_only_overlapping_in_specviz2d(specviz2d_helper, mos_spectrum2d): assert reg[1].lower.value == 7600 and reg[1].upper.value == 7800 -def test_subset_linking_specviz2d(specviz2d_helper, mos_spectrum2d): - specviz2d_helper.load_data(spectrum_2d=mos_spectrum2d) +def test_draw2d_linking_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) + + # Create a continuous 2D + 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) - print(np.min(mos_spectrum2d.data)) - print(np.max(mos_spectrum2d.data)) - - # appear to be in meters - viewer_2d.apply_roi(XRangeROI(6400, 6800)) - - # reg1 = specviz2d_helper.app.get_subsets("Subset 1") - # print(reg1) - - # subset drawn in 2d viewer, want data in the 1d viewer + # 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 to compute subset mask y1 = subset_drawn_2d.y x1 = subset_drawn_2d.x - # print(len(y1)) - # print(len(x1)) + subset_highlighted_region1 = x1[np.isfinite(y1)] + min_value_subset1 = np.min(subset_highlighted_region1) + max_value_subset1 = np.max(subset_highlighted_region1) - sub_highlighted1 = x1[np.isfinite(y1)] - min_sub1 = np.min(sub_highlighted1) - max_sub1 = np.max(sub_highlighted1) + tolerance1 = 1e-6 + expected_min1 = 6.e-5 + expected_max1 = 8.e-05 - # print(min_sub1) - # print(max_sub1) + assert np.allclose(min_value_subset1, expected_min1, atol=tolerance1) + assert np.allclose(max_value_subset1, expected_max1, atol=tolerance1) - ''' - Need to update asserts with pixel to wavelength science case - ''' - assert min_sub1 == 1 - assert max_sub1 == 1 +def test_draw1d_linking_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) - # appear to be in meters - viewer_1d.apply_roi(XRangeROI(6000, 6400)) + x_values = np.linspace(0, 10, 128) + y_values = np.linspace(0, 5, 256) - # reg2 = specviz2d_helper.app.get_subsets("Subset 1") + # Create a continuous 2D image + data = np.sin(x_values[:, np.newaxis]) * np.cos(y_values) * u.one + spectrum_data = Spectrum1D(data, wcs=wcs, meta=header) - # subset drawn in 1d viewer, want data in 2d viewer - subset_drawn_1d = viewer_1d.native_marks[-1] - - # get x and y components to compute subset mask - y2 = subset_drawn_1d.y - x2 = subset_drawn_1d.x + 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) - sub_highlighted2 = x2[np.isfinite(y2)] - min_sub2 = np.min(sub_highlighted2) - max_sub2 = np.max(sub_highlighted2) + # 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 - # print(len(y2)) - # print(len(x2)) + subset_highlighted_region2 = np.atleast_1d(np.nonzero(subset_drawn_1d))[1] - # reg2 = specviz2d_helper.app.get_subsets("Subset 1") - # print(reg2) + # Get the start and stop indices + min_value_subset2 = np.min(subset_highlighted_region2) + max_value_subset2 = np.max(subset_highlighted_region2) - ''' - Need to update asserts with wavelength to pixel science case - ''' + tolerance2 = 2 + expected_min2 = 338 + expected_max2 = 674 - assert min_sub2 == 1 - assert max_sub2 == 1 + 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): From 085c7f00200dfb4695be35efc74fca60860aaf59 Mon Sep 17 00:00:00 2001 From: gibsongreen Date: Tue, 12 Mar 2024 11:33:09 -0400 Subject: [PATCH 4/5] add viewer limits to 2d-viewer, cleaned up comments --- jdaviz/tests/test_subsets.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/jdaviz/tests/test_subsets.py b/jdaviz/tests/test_subsets.py index fd8664e67e..d8f7605d54 100644 --- a/jdaviz/tests/test_subsets.py +++ b/jdaviz/tests/test_subsets.py @@ -792,7 +792,7 @@ def test_only_overlapping_in_specviz2d(specviz2d_helper, mos_spectrum2d): assert reg[1].lower.value == 7600 and reg[1].upper.value == 7800 -def test_draw2d_linking_specviz2d(specviz2d_helper): +def test_draw_2D_subset_specviz2d(specviz2d_helper): # custom test data to predict values for different viewers header = { 'WCSAXES': 2, @@ -807,21 +807,20 @@ def test_draw2d_linking_specviz2d(specviz2d_helper): x_values = np.linspace(0, 10, 128) y_values = np.linspace(0, 5, 256) - # Create a continuous 2D 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 + # 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 to compute subset mask + # get x and y components of 1D subset created to compute mask y1 = subset_drawn_2d.y x1 = subset_drawn_2d.x @@ -837,7 +836,16 @@ def test_draw2d_linking_specviz2d(specviz2d_helper): assert np.allclose(max_value_subset1, expected_max1, atol=tolerance1) -def test_draw1d_linking_specviz2d(specviz2d_helper): +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) + + # similiar to Imviz, without show(), need to manually set limits + # of 2D-viewer so the subset mask can be applied to it + viewer_2d.jdaviz_helper.viewers['spectrum-2d-viewer']._obj.shape = (155, 864) + # custom test data to predict values for different viewers header = { 'WCSAXES': 2, @@ -852,23 +860,15 @@ def test_draw1d_linking_specviz2d(specviz2d_helper): x_values = np.linspace(0, 10, 128) y_values = np.linspace(0, 5, 256) - # Create a continuous 2D image 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) - # subset drawn in 1d viewer, want data in 2d viewer + # 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 subset_highlighted_region2 = np.atleast_1d(np.nonzero(subset_drawn_1d))[1] - - # Get the start and stop indices min_value_subset2 = np.min(subset_highlighted_region2) max_value_subset2 = np.max(subset_highlighted_region2) From 4c87024abcb678b06682cac7f3a4e44f60a47d58 Mon Sep 17 00:00:00 2001 From: gibsongreen Date: Thu, 14 Mar 2024 12:02:23 -0400 Subject: [PATCH 5/5] update comment explaining 2d-viewer/subsets workaround --- jdaviz/tests/test_subsets.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/jdaviz/tests/test_subsets.py b/jdaviz/tests/test_subsets.py index d8f7605d54..ec74a28c5f 100644 --- a/jdaviz/tests/test_subsets.py +++ b/jdaviz/tests/test_subsets.py @@ -842,8 +842,9 @@ def test_draw_1D_subset_specviz2d(specviz2d_helper): viewer_2d = specviz2d_helper.app.get_viewer( specviz2d_helper._default_spectrum_2d_viewer_reference_name) - # similiar to Imviz, without show(), need to manually set limits - # of 2D-viewer so the subset mask can be applied to it + # 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