Repository: spacetelescope/jdaviz

Issue:
Deprecate certain "get_..." methods
**Reporter: Jesse Averbukh**

The following methods need to be deprecated and all instances switched with their respective replacements:
 * get_subsets_from_viewer() replaced with get_subsets()
 * get_data_from_viewer() replaced with get_data()
 * specviz.get_spectra(subset_to_apply) replaced with specviz.get_spectra(spectral_subset)

 

Updating the docs can be part of this ticket or a separate effort.

 --- 

 DISCLAIMER: This issue was autocreated by the Jdaviz Issue Creation Bot on behalf of the reporter. If any information is incorrect, please contact [Duy Nguyen](https://github.com/duytnguyendtn)

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope) Report
Patch coverage: **`91.66`**% and project coverage change: **`-0.29`** :warning:
> Comparison is base [(`f530f52`)](https://app.codecov.io/gh/spacetelescope/jdaviz/commit/f530f527be337aa16bf7a35b9336e367b203b264?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope) 91.63% compared to head [(`02338fe`)](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope) 91.34%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main    #2242      +/-   ##
==========================================
- Coverage   91.63%   91.34%   -0.29%     
==========================================
  Files         148      148              
  Lines       16538    16542       +4     
==========================================
- Hits        15154    15111      -43     
- Misses       1384     1431      +47     
```


| [Impacted Files](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope) | Coverage Δ | |
|---|---|---|
| [jdaviz/app.py](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope#diff-amRhdml6L2FwcC5weQ==) | `87.63% <50.00%> (-5.52%)` | :arrow_down: |
| [...aviz/configs/cubeviz/plugins/tests/test\_parsers.py](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope#diff-amRhdml6L2NvbmZpZ3MvY3ViZXZpei9wbHVnaW5zL3Rlc3RzL3Rlc3RfcGFyc2Vycy5weQ==) | `97.95% <100.00%> (ø)` | |
| [jdaviz/configs/cubeviz/plugins/tests/test\_tools.py](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope#diff-amRhdml6L2NvbmZpZ3MvY3ViZXZpei9wbHVnaW5zL3Rlc3RzL3Rlc3RfdG9vbHMucHk=) | `100.00% <100.00%> (ø)` | |
| [jdaviz/core/template\_mixin.py](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope#diff-amRhdml6L2NvcmUvdGVtcGxhdGVfbWl4aW4ucHk=) | `93.13% <100.00%> (+0.01%)` | :arrow_up: |


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/spacetelescope/jdaviz/pull/2242?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=spacetelescope).

duytnguyendtn: > Also, are you feeling adventurous? 😆
> 
> [#2201 (review)](https://github.com/spacetelescope/jdaviz/pull/2201#pullrequestreview-1468300659)
> 
> @javerbukh , "... do we want to keep using `get_interactive_regions()` over `get_subsets()`?"

Falling a little behind schedule so I'm going to leave this work out
javerbukh: Not a blocker but when I do `specviz.app.get_subsets_from_viewer("spectrum-viewer")` I see
```
WARNING: AstropyDeprecationWarning: The get_subsets_from_viewer function is deprecated and may be removed in a future version.
        Use get_subsets instead. [warnings]
WARNING:astroquery:AstropyDeprecationWarning: The get_subsets_from_viewer function is deprecated and may be removed in a future version.
        Use get_subsets instead.
WARNING: AstropyDeprecationWarning: The get_data_from_viewer function is deprecated and may be removed in a future version.
        Use get_data instead. [jdaviz.app]
WARNING:astroquery:AstropyDeprecationWarning: The get_data_from_viewer function is deprecated and may be removed in a future version.
        Use get_data instead.
```
I understand why the warning was issued for `get_data_from_viewer` as well but why were both warnings printed twice?
pllim: `astroquery:AstropyDeprecationWarning` ??? Why is it calling `astroquery`?
duytnguyendtn: I'm not sure; the even weirder thing is that it's warning is coming from `astroquery`??
duytnguyendtn: `app.py` doesn't even import anything from astroquery!!
pllim: Try turning warning into error in your session and inspect the traceback. Maybe somewhere deep in the machinery, one deprecated function is calling another deprecated function.
duytnguyendtn: > So, did that problem with multiple deprecating warnings ever got resolved? What was the problem?

Nope, though @javerbukh indicated this wasn't a blocker, so I think we're safe to proceed
duytnguyendtn: I think I addressed all the comments. Ready for rereview!
pllim: Please add a change log. Thanks!
pllim: Also need a rebase because that one-liner bug fix has been merged as #2258 .

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 98c9b635..a8b8611b 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -39,6 +39,13 @@ Specviz2d
 API Changes
 -----------
 
+- ``viz.app.get_data_from_viewer()`` is deprecated; use ``viz.get_data()``. [#2242]
+
+- ``viz.app.get_subsets_from_viewer()`` is deprecated; use ``viz.app.get_subsets()``. [#2242]
+
+- ``viz.get_data()`` now takes optional ``**kwargs``; e.g., you could pass in
+  ``function="sum"`` to collapse a cube in Cubeviz. [#2242]
+
 Cubeviz
 ^^^^^^^
 
@@ -48,10 +55,13 @@ Imviz
 Mosviz
 ^^^^^^
 
+- Added new ``statistic`` keyword to ``mosviz.get_viewer("spectrum-2d-viewer").data()``
+  to allow user to collapse 2D spectrum to 1D. [#2242]
+
 Specviz
 ^^^^^^^
 
-* Re-enabled unit conversion support. [#2127]
+- Re-enabled unit conversion support. [#2127]
 
 Specviz2d
 ^^^^^^^^^
@@ -61,6 +71,8 @@ Bug Fixes
 
 - Fixed wrong elliptical region translation in ``app.get_subsets()``. [#2244]
 
+- Fixed ``cls`` input being ignored in ``viz.get_data()``. [#2242]
+
 Cubeviz
 ^^^^^^^
 
diff --git a/docs/cubeviz/export_data.rst b/docs/cubeviz/export_data.rst
index 248ad932..11c439e7 100644
--- a/docs/cubeviz/export_data.rst
+++ b/docs/cubeviz/export_data.rst
@@ -22,7 +22,7 @@ can be used to extract the *spectrum* of a spatial subset named "Subset 1":
 
 .. code-block:: python
 
-    subset1_spec1d = cubeviz.specviz.get_spectra(subset_to_apply="Subset 1")
+    subset1_spec1d = cubeviz.specviz.get_spectra(spectral_subset="Subset 1")
 
 An example without accessing Specviz:
 
@@ -41,7 +41,7 @@ To get all subsets from the spectrum viewer:
 
 .. code-block:: python
 
-    subset1_spec1d = cubeviz.app.get_subsets_from_viewer("spectrum-viewer")
+    subset1_spec1d = cubeviz.specviz.app.get_subsets()
 
 To access the spatial regions themselves:
 
diff --git a/jdaviz/app.py b/jdaviz/app.py
index 3b8afb2c..f8d16356 100644
--- a/jdaviz/app.py
+++ b/jdaviz/app.py
@@ -14,6 +14,7 @@ from astropy.nddata import CCDData, NDData
 from astropy.io import fits
 from astropy.coordinates import Angle
 from astropy.time import Time
+from astropy.utils.decorators import deprecated
 from regions import PixCoord, CirclePixelRegion, RectanglePixelRegion, EllipsePixelRegion
 
 from echo import CallbackProperty, DictCallbackProperty, ListCallbackProperty
@@ -662,6 +663,7 @@ class Application(VuetifyTemplate, HubListener):
         """
         return self._viewer_store.get(vid)
 
+    @deprecated(since="3.6", alternative="viz_helper.get_data")
     def get_data_from_viewer(self, viewer_reference, data_label=None,
                              cls='default', include_subsets=True):
         """
@@ -769,6 +771,7 @@ class Application(VuetifyTemplate, HubListener):
         # If a data label was provided, return only the corresponding data, otherwise return all:
         return data.get(data_label, data)
 
+    @deprecated(since="3.6", alternative="get_subsets")
     def get_subsets_from_viewer(self, viewer_reference, data_label=None, subset_type=None):
         """
         Returns the subsets of a specified viewer converted to astropy regions
@@ -803,9 +806,11 @@ class Application(VuetifyTemplate, HubListener):
             objects.
         """
         viewer = self.get_viewer(viewer_reference)
-        data = self.get_data_from_viewer(viewer_reference,
-                                         data_label,
-                                         cls=None)
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+            data = self.get_data_from_viewer(viewer_reference,
+                                             data_label,
+                                             cls=None)
         regions = {}
 
         if data_label is not None:
@@ -845,7 +850,9 @@ class Application(VuetifyTemplate, HubListener):
                     regions[key] = self.get_subsets(key)
                     continue
 
-                temp_data = self.get_data_from_viewer(viewer_reference, value.label)
+                with warnings.catch_warnings():
+                    warnings.simplefilter("ignore")
+                    temp_data = self.get_data_from_viewer(viewer_reference, value.label)
                 if isinstance(temp_data, Spectrum1D):
                     regions[key] = self.get_subsets(key)
                     continue
diff --git a/jdaviz/configs/mosviz/plugins/viewers.py b/jdaviz/configs/mosviz/plugins/viewers.py
index ba655873..c5ad8046 100644
--- a/jdaviz/configs/mosviz/plugins/viewers.py
+++ b/jdaviz/configs/mosviz/plugins/viewers.py
@@ -43,7 +43,7 @@ class MosvizImageView(JdavizViewerMixin, BqplotImageView, AstrowidgetsImageViewe
         self.figure.fig_margin = {'left': 0, 'bottom': 0, 'top': 0, 'right': 0}
 
     def data(self, cls=None):
-        return [layer_state.layer  # .get_object(cls=cls or self.default_class)
+        return [layer_state.layer.get_object(cls=cls or self.default_class)
                 for layer_state in self.state.layers
                 if hasattr(layer_state, 'layer') and
                 isinstance(layer_state.layer, BaseData)]
@@ -207,8 +207,9 @@ class MosvizProfile2DView(JdavizViewerMixin, BqplotImageView):
             x_scales.min = max(limits) if self.inverted_x_axis else min(limits)
             x_scales.max = min(limits) if self.inverted_x_axis else max(limits)
 
-    def data(self, cls=None):
-        return [layer_state.layer.get_object(cls=cls or self.default_class)
+    def data(self, cls=None, statistic=None):
+        return [layer_state.layer.get_object(statistic=statistic,
+                                             cls=cls or self.default_class)
                 for layer_state in self.state.layers
                 if hasattr(layer_state, 'layer') and
                 isinstance(layer_state.layer, BaseData)]
diff --git a/jdaviz/configs/specviz/helper.py b/jdaviz/configs/specviz/helper.py
index e0969e7c..3a7c843b 100644
--- a/jdaviz/configs/specviz/helper.py
+++ b/jdaviz/configs/specviz/helper.py
@@ -1,6 +1,7 @@
 import warnings
 
 from astropy import units as u
+from astropy.utils.decorators import deprecated_renamed_argument
 from regions.core.core import Region
 from glue.core.subset_group import GroupedSubset
 from specutils import SpectralRegion, Spectrum1D
@@ -68,7 +69,8 @@ class Specviz(ConfigHelper, LineListMixin):
                           show_in_viewer=show_in_viewer,
                           concat_by_file=concat_by_file)
 
-    def get_spectra(self, data_label=None, subset_to_apply=None, apply_slider_redshift="Warn"):
+    @deprecated_renamed_argument("subset_to_apply", "spectral_subset", "3.6")
+    def get_spectra(self, data_label=None, spectral_subset=None, apply_slider_redshift="Warn"):
         """Returns the current data loaded into the main viewer
 
         """
@@ -81,16 +83,16 @@ class Specviz(ConfigHelper, LineListMixin):
 
         if data_label is not None:
             spectrum = get_data_method(data_label=data_label,
-                                       spectral_subset=subset_to_apply,
+                                       spectral_subset=spectral_subset,
                                        cls=Spectrum1D)
             spectra[data_label] = spectrum
         else:
             for layer_state in viewer.state.layers:
                 lyr = layer_state.layer
-                if subset_to_apply is not None:
-                    if lyr.label == subset_to_apply:
+                if spectral_subset is not None:
+                    if lyr.label == spectral_subset:
                         spectrum = get_data_method(data_label=lyr.data.label,
-                                                   spectral_subset=subset_to_apply,
+                                                   spectral_subset=spectral_subset,
                                                    cls=Spectrum1D,
                                                    **function_kwargs)
                         spectra[lyr.data.label] = spectrum
@@ -291,7 +293,7 @@ class Specviz(ConfigHelper, LineListMixin):
                  use_display_units=False, **kwargs):
         """
         Returns data with name equal to data_label of type cls with subsets applied from
-        subset_to_apply.
+        spectral_subset.
 
         Parameters
         ----------
diff --git a/jdaviz/core/helpers.py b/jdaviz/core/helpers.py
index 986547f2..7a021d64 100644
--- a/jdaviz/core/helpers.py
+++ b/jdaviz/core/helpers.py
@@ -554,7 +554,7 @@ class ConfigHelper(HubListener):
 
         return _handle_display_units(data, use_display_units)
 
-    def get_data(self, data_label=None, cls=None, use_display_units=False):
+    def get_data(self, data_label=None, cls=None, use_display_units=False, **kwargs):
         """
         Returns data with name equal to data_label of type cls.
 
@@ -564,18 +564,20 @@ class ConfigHelper(HubListener):
             Provide a label to retrieve a specific data set from data_collection.
         cls : `~specutils.Spectrum1D`, `~astropy.nddata.CCDData`, optional
             The type that data will be returned as.
-        use_display_units: bool, optional
+        use_display_units : bool, optional
             Whether to convert to the display units defined in the <unit-conversion> plugin.
+        kwargs : dict
+            For Cubeviz, you could also pass in ``function`` (str) to collapse
+            the cube into 1D spectrum using provided function.
 
         Returns
         -------
         data : cls
-            Data is returned as type cls.
+            Data is returned as type ``cls``.
 
         """
-        return self._get_data(data_label=data_label, spatial_subset=None,
-                              spectral_subset=None, function=None,
-                              cls=None, use_display_units=use_display_units)
+        return self._get_data(data_label=data_label,
+                              cls=cls, use_display_units=use_display_units, **kwargs)
 
 
 class ImageConfigHelper(ConfigHelper):
diff --git a/jdaviz/core/template_mixin.py b/jdaviz/core/template_mixin.py
index 5bc71b4e..1b8fece4 100644
--- a/jdaviz/core/template_mixin.py
+++ b/jdaviz/core/template_mixin.py
@@ -1067,7 +1067,7 @@ class SubsetSelect(SelectPluginComponent):
     def selected_obj(self):
         if self.selected in self.manual_options or self.selected not in self.labels:
             return None
-        # NOTE: we use reference names here instead of IDs since get_subsets_from_viewer requires
+        # NOTE: we use reference names here instead of IDs since get_subsets requires
         # that.  For imviz, this will mean we won't be able to loop through each of the viewers,
         # but the original viewer should have access to all the subsets.
         for viewer_ref in self.viewer_refs:
diff --git a/notebooks/CubevizExample.ipynb b/notebooks/CubevizExample.ipynb
index ac78ed88..af16b399 100644
--- a/notebooks/CubevizExample.ipynb
+++ b/notebooks/CubevizExample.ipynb
@@ -155,7 +155,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "data = cubeviz.app.get_data_from_viewer('flux-viewer', 'contents[FLUX]')"
+    "data = cubeviz.get_data('jw02732-o004_t004_miri_ch1-shortmediumlong_s3d.fits[SCI]')"
    ]
   },
   {
@@ -187,7 +187,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "spectra_dict = cubeviz.specviz.get_spectra()\n",
+    "spectra_dict = cubeviz.specviz.get_spectra(apply_slider_redshift=True)\n",
     "spectra_dict"
    ]
   },
diff --git a/notebooks/ImvizDitheredExample.ipynb b/notebooks/ImvizDitheredExample.ipynb
index 8af6aac5..52c75d05 100644
--- a/notebooks/ImvizDitheredExample.ipynb
+++ b/notebooks/ImvizDitheredExample.ipynb
@@ -301,7 +301,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "data = imviz.app.get_data_from_viewer('imviz-0', 'acs_47tuc_1[SCI,1]')"
+    "data = imviz.get_data('acs_47tuc_1[SCI,1]')"
    ]
   },
   {
diff --git a/notebooks/ImvizExample.ipynb b/notebooks/ImvizExample.ipynb
index b39014d4..1841efb1 100644
--- a/notebooks/ImvizExample.ipynb
+++ b/notebooks/ImvizExample.ipynb
@@ -348,7 +348,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "data = imviz.app.get_data_from_viewer('imviz-0', 'jw02727-o002_t062_nircam_clear-f090w_i2d[DATA]')"
+    "data = imviz.get_data('jw02727-o002_t062_nircam_clear-f090w_i2d[DATA]')"
    ]
   },
   {
diff --git a/notebooks/concepts/cubeviz_data_interactions.ipynb b/notebooks/concepts/cubeviz_data_interactions.ipynb
index 862ba920..fd6b6d0a 100644
--- a/notebooks/concepts/cubeviz_data_interactions.ipynb
+++ b/notebooks/concepts/cubeviz_data_interactions.ipynb
@@ -149,7 +149,7 @@
    "source": [
     "from specutils import Spectrum1D\n",
     "\n",
-    "spec_data = app.get_data_from_viewer('spectrum-viewer')\n",
+    "spec_data = app.get_viewer('spectrum-viewer').data()\n",
     "\n",
     "# The returned data from `get_data` is in list format, as it's \n",
     "# possible for there to be several data plotted in the viewer\n",
@@ -170,7 +170,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "spec = app.get_data_from_viewer('spectrum-viewer', '6de4c8ee5659e87a302e3de595074ba5[FLUX]')\n",
+    "spec = app._jdaviz_helper.get_data('6de4c8ee5659e87a302e3de595074ba5[FLUX]', function='sum')\n",
     "spec"
    ]
   },
@@ -267,7 +267,7 @@
     "import matplotlib.pyplot as plt\n",
     "\n",
     "# The returned data object is a `CCData` class to represent the 2D nature of the image data.\n",
-    "image_data = app.get_data_from_viewer('flux-viewer')\n",
+    "image_data = app.get_viewer('flux-viewer').data()[0]\n",
     "\n",
     "f, ax = plt.subplots()\n",
     "\n",
diff --git a/notebooks/concepts/cubeviz_ndarray_gif.ipynb b/notebooks/concepts/cubeviz_ndarray_gif.ipynb
index e4902618..f8dfc864 100644
--- a/notebooks/concepts/cubeviz_ndarray_gif.ipynb
+++ b/notebooks/concepts/cubeviz_ndarray_gif.ipynb
@@ -102,8 +102,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "data = cubeviz.app.get_data_from_viewer('flux-viewer', 'ordered')\n",
-    "orig_cube = data.get_object(statistic=None)\n",
+    "orig_cube = cubeviz.get_data('ordered')\n",
     "orig_cube.shape  # Input was (8, 5, 10)  # x, y, z"
    ]
   },
@@ -124,7 +123,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "data_2 = cubeviz.app.get_data_from_viewer('flux-viewer', 'roundtrip_test')\n",
+    "data_2 = cubeviz.get_data('roundtrip_test')\n",
     "data_2.shape"
    ]
   },
diff --git a/notebooks/concepts/default_programmatic_viewers_from_blank.ipynb b/notebooks/concepts/default_programmatic_viewers_from_blank.ipynb
index 08b20d5f..3b885fa2 100644
--- a/notebooks/concepts/default_programmatic_viewers_from_blank.ipynb
+++ b/notebooks/concepts/default_programmatic_viewers_from_blank.ipynb
@@ -280,7 +280,7 @@
    "outputs": [],
    "source": [
     "# can I access the data back out? \n",
-    "app.get_data_from_viewer('image-viewer')"
+    "app.get_viewer('image-viewer').data()"
    ]
   },
   {
diff --git a/notebooks/concepts/imviz_roman_asdf.ipynb b/notebooks/concepts/imviz_roman_asdf.ipynb
index 818cd22d..7d03ba98 100644
--- a/notebooks/concepts/imviz_roman_asdf.ipynb
+++ b/notebooks/concepts/imviz_roman_asdf.ipynb
@@ -202,7 +202,7 @@
    "metadata": {},
    "outputs": [],
    "source": [
-    "data = imviz.app.get_data_from_viewer('imviz-0', imviz.app.data_collection[0].label)"
+    "data = imviz.get_data(imviz.app.data_collection[0].label)"
    ]
   },
   {
diff --git a/notebooks/concepts/mosviz_concept.ipynb b/notebooks/concepts/mosviz_concept.ipynb
index c5372c5d..d5426ce9 100644
--- a/notebooks/concepts/mosviz_concept.ipynb
+++ b/notebooks/concepts/mosviz_concept.ipynb
@@ -637,7 +637,7 @@
    "source": [
     "# Assuming the user has done something with a glue plugin that adds another spectrum to the spectrum view\n",
     "\n",
-    "currspec = mosviz.app.get_data_from_viewer('spectrum-viewer', 'spectrum-added-by-plugin')\n",
+    "currspec = mosviz.get_data('spectrum-added-by-plugin')\n",
     "\n",
     "... do something with currspec ..."
    ]


Test patch:
diff --git a/jdaviz/configs/cubeviz/plugins/tests/test_data_retrieval.py b/jdaviz/configs/cubeviz/plugins/tests/test_data_retrieval.py
index 7509b7cc..b5f1dfc6 100644
--- a/jdaviz/configs/cubeviz/plugins/tests/test_data_retrieval.py
+++ b/jdaviz/configs/cubeviz/plugins/tests/test_data_retrieval.py
@@ -1,16 +1,17 @@
+import warnings
+
 import pytest
 import numpy as np
 
 from astropy.utils.data import download_file
 
 
-@pytest.mark.filterwarnings('ignore')
 @pytest.mark.remote_data
 def test_data_retrieval(cubeviz_helper):
     """The purpose of this test is to check that both methods:
 
     - app.get_viewer('spectrum-viewer').data()
-    - app.get_data_from_viewer("spectrum-viewer")
+    - cubeviz_helper.get_data()
 
     return the same spectrum values.
     """
@@ -20,14 +21,16 @@ def test_data_retrieval(cubeviz_helper):
 
     spectrum_viewer_reference_name = "spectrum-viewer"
     fn = download_file(URL, cache=True)
-    cubeviz_helper.load_data(fn)
+    with warnings.catch_warnings():
+        warnings.filterwarnings("ignore")
+        cubeviz_helper.load_data(fn)
 
     # two ways of retrieving data from the viewer.
     # They should return the same spectral values
     a1 = cubeviz_helper.app.get_viewer(spectrum_viewer_reference_name).data()
-    a2 = cubeviz_helper.app.get_data_from_viewer(spectrum_viewer_reference_name)
+    a2 = cubeviz_helper.get_data("contents[FLUX]", function="sum")
 
     test_value_1 = a1[0].data
-    test_value_2 = list(a2.values())[0].data
+    test_value_2 = a2.flux.value
 
     assert np.allclose(test_value_1, test_value_2, atol=1e-5)
diff --git a/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py b/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py
index 48b9ba81..5fb34a91 100644
--- a/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py
+++ b/jdaviz/configs/cubeviz/plugins/tests/test_parsers.py
@@ -73,7 +73,7 @@ def test_spectrum1d_with_fake_fixed_units(spectrum1d, cubeviz_helper):
     cubeviz_helper.app.add_data_to_viewer('spectrum-viewer', 'test')
     cubeviz_helper.app.get_viewer('spectrum-viewer').apply_roi(XRangeROI(6600, 7400))
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer("spectrum-viewer")
+    subsets = cubeviz_helper.app.get_subsets()
     reg = subsets.get('Subset 1')
 
     assert len(subsets) == 1
diff --git a/jdaviz/configs/cubeviz/plugins/tests/test_tools.py b/jdaviz/configs/cubeviz/plugins/tests/test_tools.py
index 5ce48674..b97d181a 100644
--- a/jdaviz/configs/cubeviz/plugins/tests/test_tools.py
+++ b/jdaviz/configs/cubeviz/plugins/tests/test_tools.py
@@ -25,8 +25,8 @@ def test_spectrum_at_spaxel(cubeviz_helper, spectrum1d_cube):
     assert len(spectrum_viewer.data()) == 2
 
     # Check that a new subset was created
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer')
-    reg = subsets.get('Subset 1')
+    subsets = cubeviz_helper.app.get_subsets()
+    reg = subsets.get('Subset 1')[0]['region']
     assert len(subsets) == 1
     assert isinstance(reg, RectanglePixelRegion)
 
@@ -64,8 +64,8 @@ def test_spectrum_at_spaxel_altkey_true(cubeviz_helper, spectrum1d_cube):
     assert len(spectrum_viewer.data()) == 2
 
     # Check that subset was created
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer')
-    reg = subsets.get('Subset 1')
+    subsets = cubeviz_helper.app.get_subsets()
+    reg = subsets.get('Subset 1')[0]['region']
     assert len(subsets) == 1
     assert isinstance(reg, RectanglePixelRegion)
 
@@ -77,8 +77,8 @@ def test_spectrum_at_spaxel_altkey_true(cubeviz_helper, spectrum1d_cube):
     assert len(flux_viewer.figure.marks) == 4
     assert len(spectrum_viewer.data()) == 3
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer')
-    reg2 = subsets.get('Subset 2')
+    subsets = cubeviz_helper.app.get_subsets()
+    reg2 = subsets.get('Subset 2')[0]['region']
     assert len(subsets) == 2
     assert isinstance(reg2, RectanglePixelRegion)
 
diff --git a/jdaviz/configs/imviz/tests/test_helper.py b/jdaviz/configs/imviz/tests/test_helper.py
index f7c8a330..f2e4410c 100644
--- a/jdaviz/configs/imviz/tests/test_helper.py
+++ b/jdaviz/configs/imviz/tests/test_helper.py
@@ -25,12 +25,12 @@ def test_create_new_viewer(imviz_helper, image_2d_wcs):
     assert len(imviz_helper.app.get_viewer_ids()) == 2
 
     # there should be no data in the new viewer
-    assert len(imviz_helper.app.get_data_from_viewer(viewer_name)) == 0
+    assert len(imviz_helper.app.get_viewer(viewer_name).data()) == 0
 
     # then add data, and check that data were added to the new viewer
     imviz_helper.app.add_data_to_viewer(viewer_name, data_label)
-    assert len(imviz_helper.app.get_data_from_viewer(viewer_name)) == 1
+    assert len(imviz_helper.app.get_viewer(viewer_name).data()) == 1
 
     # remove data from the new viewer, check that it was removed
     imviz_helper.app.remove_data_from_viewer(viewer_name, data_label)
-    assert len(imviz_helper.app.get_data_from_viewer(viewer_name)) == 0
+    assert len(imviz_helper.app.get_viewer(viewer_name).data()) == 0
diff --git a/jdaviz/configs/mosviz/tests/test_data_loading.py b/jdaviz/configs/mosviz/tests/test_data_loading.py
index 9a5ab2ca..67d73ffe 100644
--- a/jdaviz/configs/mosviz/tests/test_data_loading.py
+++ b/jdaviz/configs/mosviz/tests/test_data_loading.py
@@ -2,9 +2,9 @@
 
 from zipfile import ZipFile
 
-from astropy.nddata import CCDData
 import numpy as np
 import pytest
+from astropy.nddata import CCDData
 from specutils import Spectrum1D
 
 from jdaviz.utils import PRIHDR_KEY
@@ -20,12 +20,14 @@ def test_load_spectrum1d(mosviz_helper, spectrum1d):
     assert dc_1.label == label
     assert dc_1.meta['uncertainty_type'] == 'std'
 
-    table = mosviz_helper.app.get_viewer('table-viewer')
+    table = mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name)
     table.widget_table.vue_on_row_clicked(0)
 
-    data = mosviz_helper.app.get_data_from_viewer('spectrum-viewer')
+    data = mosviz_helper.app.get_viewer(mosviz_helper._default_spectrum_viewer_reference_name
+                                        ).data()
 
-    assert isinstance(data[label], Spectrum1D)
+    assert len(data) == 1
+    assert isinstance(data[0], Spectrum1D)
 
     with pytest.raises(AttributeError):
         mosviz_helper.load_1d_spectra([1, 2, 3])
@@ -42,12 +44,14 @@ def test_load_image(mosviz_helper, mos_image):
     assert PRIHDR_KEY not in dc_1.meta
     assert dc_1.meta['RADESYS'] == 'ICRS'
 
-    table = mosviz_helper.app.get_viewer('table-viewer')
+    table = mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name)
     table.widget_table.vue_on_row_clicked(0)
 
-    data = mosviz_helper.app.get_data_from_viewer('image-viewer')
+    data = mosviz_helper.app.get_viewer(mosviz_helper._default_image_viewer_reference_name
+                                        ).data(cls=CCDData)
 
-    dataval = data[f"{label} 0"]
+    assert len(data) == 1
+    dataval = data[0]
     assert isinstance(dataval, CCDData)
     assert dataval.shape == (55, 55)
 
@@ -63,12 +67,14 @@ def test_load_spectrum_collection(mosviz_helper, spectrum_collection):
     assert dc_1.label == labels[0]
     assert dc_1.meta['uncertainty_type'] == 'std'
 
-    table = mosviz_helper.app.get_viewer('table-viewer')
+    table = mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name)
     table.select_row(0)
 
-    data = mosviz_helper.app.get_data_from_viewer('spectrum-viewer')
+    data = mosviz_helper.app.get_viewer(mosviz_helper._default_spectrum_viewer_reference_name
+                                        ).data()
 
-    assert isinstance(data[labels[0]], Spectrum1D)
+    assert len(data) == 1
+    assert isinstance(data[0], Spectrum1D)
 
 
 def test_load_list_of_spectrum1d(mosviz_helper, spectrum1d):
@@ -83,12 +89,14 @@ def test_load_list_of_spectrum1d(mosviz_helper, spectrum1d):
     assert dc_1.label == labels[0]
     assert dc_1.meta['uncertainty_type'] == 'std'
 
-    table = mosviz_helper.app.get_viewer('table-viewer')
+    table = mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name)
     table.widget_table.vue_on_row_clicked(0)
 
-    data = mosviz_helper.app.get_data_from_viewer('spectrum-viewer')
+    data = mosviz_helper.app.get_viewer(mosviz_helper._default_spectrum_viewer_reference_name
+                                        ).data()
 
-    assert isinstance(data[labels[0]], Spectrum1D)
+    assert len(data) == 1
+    assert isinstance(data[0], Spectrum1D)
 
 
 def test_load_mos_spectrum2d(mosviz_helper, mos_spectrum2d):
@@ -102,12 +110,14 @@ def test_load_mos_spectrum2d(mosviz_helper, mos_spectrum2d):
     assert dc_1.label == label
     assert dc_1.meta['INSTRUME'] == 'nirspec'
 
-    table = mosviz_helper.app.get_viewer('table-viewer')
+    table = mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name)
     table.widget_table.vue_on_row_clicked(0)
 
-    data = mosviz_helper.app.get_data_from_viewer('spectrum-2d-viewer')
+    data = mosviz_helper.app.get_viewer(mosviz_helper._default_spectrum_2d_viewer_reference_name
+                                        ).data()
 
-    assert data[label].shape == (1024, 15)
+    assert len(data) == 1
+    assert data[0].shape == (1024, 15)
 
 
 @pytest.mark.parametrize('label', [None, "Test Label"])
@@ -118,7 +128,8 @@ def test_load_multi_image_spec(mosviz_helper, mos_image, spectrum1d, mos_spectru
 
     mosviz_helper.load_data(spectra1d, spectra2d, images=images, images_label=label)
 
-    assert mosviz_helper.app.get_viewer("table-viewer").figure_widget.highlighted == 0
+    assert mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name
+                                        ).figure_widget.highlighted == 0
     assert len(mosviz_helper.app.data_collection) == 10
 
     qtable = mosviz_helper.to_table()
@@ -134,7 +145,8 @@ def test_load_multi_image_and_spec1d_only(mosviz_helper, mos_image, spectrum1d):
 
     mosviz_helper.load_data(spectra_1d=spectra1d, images=images)
 
-    assert mosviz_helper.app.get_viewer("table-viewer").figure_widget.highlighted == 0
+    assert mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name
+                                        ).figure_widget.highlighted == 0
     assert len(mosviz_helper.app.data_collection) == 7
 
 
@@ -144,7 +156,8 @@ def test_load_multi_image_and_spec2d_only(mosviz_helper, mos_image, mos_spectrum
 
     mosviz_helper.load_data(spectra_2d=spectra2d, images=images)
 
-    assert mosviz_helper.app.get_viewer("table-viewer").figure_widget.highlighted == 0
+    assert mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name
+                                        ).figure_widget.highlighted == 0
     assert len(mosviz_helper.app.data_collection) == 7
 
 
@@ -170,7 +183,8 @@ def test_load_single_image_multi_spec(mosviz_helper, mos_image, spectrum1d, mos_
 
     mosviz_helper.load_data(spectra1d, spectra2d, images=mos_image, images_label=label)
 
-    assert mosviz_helper.app.get_viewer("table-viewer").figure_widget.highlighted == 0
+    assert mosviz_helper.app.get_viewer(mosviz_helper._default_table_viewer_reference_name
+                                        ).figure_widget.highlighted == 0
     assert len(mosviz_helper.app.data_collection) == 8
 
     qtable = mosviz_helper.to_table()
diff --git a/jdaviz/configs/specviz/tests/test_helper.py b/jdaviz/configs/specviz/tests/test_helper.py
index 0f47db0d..bade4d68 100644
--- a/jdaviz/configs/specviz/tests/test_helper.py
+++ b/jdaviz/configs/specviz/tests/test_helper.py
@@ -32,10 +32,9 @@ class TestSpecvizHelper:
         assert dc_0.label == self.label
         assert dc_0.meta['uncertainty_type'] == 'std'
 
-        data = self.spec_app.app.get_data_from_viewer('spectrum-viewer')
+        data = self.spec_app.get_data()
 
-        assert isinstance(list(data.values())[0], Spectrum1D)
-        assert list(data.keys())[0] == self.label
+        assert isinstance(data, Spectrum1D)
 
     def test_load_spectrum_list_no_labels(self):
         # now load three more spectra from a SpectrumList, without labels
diff --git a/jdaviz/tests/test_app.py b/jdaviz/tests/test_app.py
index f0138f08..c72d4fee 100644
--- a/jdaviz/tests/test_app.py
+++ b/jdaviz/tests/test_app.py
@@ -54,7 +54,8 @@ def test_nonstandard_specviz_viewer_name(spectrum1d):
     assert viz.app.get_viewer_reference_names() == ['h', 'k']
 
     viz.load_spectrum(spectrum1d, data_label='example label')
-    assert not len(viz.app.get_data_from_viewer("h", "non-existent label"))
+    with pytest.raises(ValueError):
+        viz.get_data("non-existent label")
 
 
 def test_duplicate_data_labels(specviz_helper, spectrum1d):
diff --git a/jdaviz/tests/test_subsets.py b/jdaviz/tests/test_subsets.py
index 7dd56bca..8d4066bd 100644
--- a/jdaviz/tests/test_subsets.py
+++ b/jdaviz/tests/test_subsets.py
@@ -22,8 +22,8 @@ def test_region_from_subset_2d(cubeviz_helper):
 
     cubeviz_helper.app.get_viewer('flux-viewer').apply_roi(EllipticalROI(1, 3.5, 1.2, 3.3))
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer')
-    reg = subsets.get('Subset 1')
+    subsets = cubeviz_helper.app.get_subsets()
+    reg = subsets.get('Subset 1')[0]['region']
 
     assert len(subsets) == 1
     assert isinstance(reg, EllipsePixelRegion)
@@ -99,8 +99,8 @@ def test_region_from_subset_3d(cubeviz_helper):
         assert subset_plugin._get_value_from_subset_definition(0, "Ymax", key) == 3.3
         assert subset_plugin._get_value_from_subset_definition(0, "Angle", key) == 45
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer')
-    reg = subsets.get('Subset 1')
+    subsets = cubeviz_helper.app.get_subsets()
+    reg = subsets.get('Subset 1')[0]['region']
 
     assert_allclose(reg.center.x, 2.75)
     assert_allclose(reg.center.y, 1.65)
@@ -110,8 +110,8 @@ def test_region_from_subset_3d(cubeviz_helper):
 
     # Move the rectangle
     subset_plugin.set_center((3, 2), update=True)
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer')
-    reg = subsets.get('Subset 1')
+    subsets = cubeviz_helper.app.get_subsets()
+    reg = subsets.get('Subset 1')[0]['region']
     assert_allclose(reg.center.x, 3)
     assert_allclose(reg.center.y, 2)
     assert_allclose(reg.width, 1.5)
@@ -140,7 +140,7 @@ def test_region_from_subset_profile(cubeviz_helper, spectral_cube_wcs):
 
     cubeviz_helper.app.get_viewer("spectrum-viewer").apply_roi(XRangeROI(5, 15.5))
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('spectrum-viewer', subset_type='spectral')
+    subsets = cubeviz_helper.app.get_subsets(spectral_only=True)
     reg = subsets.get('Subset 1')
 
     assert len(subsets) == 1
@@ -165,7 +165,7 @@ def test_region_from_subset_profile(cubeviz_helper, spectral_cube_wcs):
         assert subset_plugin._get_value_from_subset_definition(0, "Lower bound", key) == 10
         assert subset_plugin._get_value_from_subset_definition(0, "Upper bound", key) == 15.5
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('spectrum-viewer', subset_type='spectral')
+    subsets = cubeviz_helper.app.get_subsets(spectral_only=True)
     reg = subsets.get('Subset 1')
 
     assert_quantity_allclose(reg.lower, 10.0 * u.Hz)
@@ -173,7 +173,7 @@ def test_region_from_subset_profile(cubeviz_helper, spectral_cube_wcs):
 
     # Move the Subset.
     subset_plugin.set_center(10, update=True)
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('spectrum-viewer', subset_type='spectral')
+    subsets = cubeviz_helper.app.get_subsets(spectral_only=True)
     reg = subsets.get('Subset 1')
     assert_quantity_allclose(reg.lower, 7.25 * u.Hz)
     assert_quantity_allclose(reg.upper, 12.75 * u.Hz)
@@ -200,7 +200,7 @@ def test_region_spectral_spatial(cubeviz_helper, spectral_cube_wcs):
 
     assert len([m for m in spectrum_viewer.figure.marks if isinstance(m, ShadowSpatialSpectral)]) == 1  # noqa
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('spectrum-viewer', subset_type='spectral')
+    subsets = cubeviz_helper.app.get_subsets(spectral_only=True)
     reg = subsets.get('Subset 1')
 
     assert len(subsets) == 1
@@ -209,8 +209,8 @@ def test_region_spectral_spatial(cubeviz_helper, spectral_cube_wcs):
     assert_quantity_allclose(reg.lower, 5.0 * u.Hz)
     assert_quantity_allclose(reg.upper, 15.5 * u.Hz)
 
-    subsets = cubeviz_helper.app.get_subsets_from_viewer('flux-viewer', subset_type='spatial')
-    reg = subsets.get('Subset 2')
+    subsets = cubeviz_helper.app.get_subsets(spatial_only=True)
+    reg = subsets.get('Subset 2')[0]['region']
 
     assert len(subsets) == 1
     assert isinstance(reg, RectanglePixelRegion)