Skip to content

Commit

Permalink
Remove deprecated methods and arguments. (#2578)
Browse files Browse the repository at this point in the history
* Remove deprecated methods from app.py

* Remove more deprecations

* Also remove this from user_api

* Add changelog

* More detailed changelog
  • Loading branch information
rosteen authored Nov 28, 2023
1 parent c788287 commit db7c9f1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 299 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,26 @@ Specviz2d
API Changes
-----------

- Deprecated ``app.get_data_from_viewer`` is removed, use ``viz_helper.get_data`` instead. [#2578]

- Deprecated ``app.get_subsets_from_viewer`` is removed, use ``viz_helper.get_subsets`` instead. [#2578]

Cubeviz
^^^^^^^

Imviz
^^^^^

- Deprecated ``do_link`` argument of ``imviz.load_data`` is removed, use ``batch_load`` context manager instead. [#2578]

Mosviz
^^^^^^

Specviz
^^^^^^^

- Deprecated ``specviz.load_spectrum`` is removed, use ``specviz.load_data`` instead. [#2578]

Specviz2d
^^^^^^^^^

Expand Down
256 changes: 4 additions & 252 deletions jdaviz/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
import re
import uuid
import warnings
from inspect import isclass
import operator

from ipywidgets import widget_serialization
import ipyvue

from astropy import units as u
from astropy.nddata import CCDData, NDData
from astropy.nddata import NDData
from astropy.io import fits
from astropy.time import Time
from astropy.utils.decorators import deprecated
from echo import CallbackProperty, DictCallbackProperty, ListCallbackProperty
from ipygoldenlayout import GoldenLayout
from ipysplitpanes import SplitPanes
Expand All @@ -22,18 +20,17 @@
import matplotlib.cm as cm
import numpy as np

from glue.core.exceptions import IncompatibleAttribute
from glue.config import colormaps, data_translator
from glue.config import colormaps
from glue.config import settings as glue_settings
from glue.core import BaseData, HubListener, Data, DataCollection
from glue.core import HubListener
from glue.core.link_helpers import LinkSame, LinkSameWithUnits
from glue.core.message import (DataCollectionAddMessage,
DataCollectionDeleteMessage,
SubsetCreateMessage,
SubsetUpdateMessage,
SubsetDeleteMessage)
from glue.core.state_objects import State
from glue.core.subset import (Subset, RangeSubsetState, RoiSubsetState,
from glue.core.subset import (RangeSubsetState, RoiSubsetState,
CompositeSubsetState, InvertState)
from glue.core.roi import CircularROI, CircularAnnulusROI, EllipticalROI, RectangularROI
from glue.core.units import unit_converter
Expand All @@ -43,7 +40,6 @@
from glue_jupyter.common.toolbar_vuetify import read_icon
from glue_jupyter.bqplot.common.tools import TrueCircularROI
from glue_jupyter.state_traitlets_helpers import GlueState
from glue_jupyter.bqplot.profile import BqplotProfileView
from ipyvuetify import VuetifyTemplate

from jdaviz import __version__
Expand Down Expand Up @@ -681,250 +677,6 @@ def get_viewer_by_id(self, vid):
"""
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):
"""
Returns each data component currently rendered within a viewer
instance. Viewers themselves store a default data type to which the
Glue data components are transformed upon retrieval. This can be
optionally overridden with the ``cls`` keyword.
Notes
-----
This is only used in cases where the viewers have been pre-defined
in the configuration file. Otherwise, viewers are not stored via
reference.
Parameters
----------
viewer_reference : str
The reference to the viewer defined with the ``reference`` key
in the yaml configuration file.
data_label : str, optional
Optionally provide a label to retrieve a specific data set from the
viewer instance.
cls : class
The class definition the Glue data components get transformed to
when retrieved. This requires that a working set of translation
functions exist in the ``glue_astronomy`` package. See
https://github.com/glue-viz/glue-astronomy for more info.
If this is the special string ``'default'``, the ``default_class``
attribute of the viewer referenced by ``viewer_reference`` is used.
include_subsets : bool
Whether to include subset layer data that exists in the viewer but
has not been included in the core data collection object.
Returns
-------
data : dict
A dict of the transformed Glue data objects, indexed to
corresponding viewer data labels.
"""
viewer = self.get_viewer(viewer_reference)
cls = viewer.default_class if cls == 'default' else cls

if cls is not None and not isclass(cls):
raise TypeError(
"cls in get_data_from_viewer must be a class, None, or "
"the 'default' string.")

data = {}

# If the viewer also supports collapsing, then grab the user's chosen
# statistic for collapsing data
if hasattr(viewer.state, 'function'):
statistic = viewer.state.function
else:
statistic = None

for layer_state in viewer.state.layers:
label = layer_state.layer.label

if (hasattr(layer_state, 'layer') and
(data_label is None or label == data_label)):

# For raw data, just include the data itself
if isinstance(layer_state.layer, BaseData):
layer_data = layer_state.layer

if cls is not None:
# If data is one-dimensional, assume that it can be
# collapsed via the defined statistic
if 'Trace' in layer_data.meta:
layer_data = layer_data.get_object()
elif cls == Spectrum1D:
layer_data = layer_data.get_object(cls=cls,
statistic=statistic)
else:
layer_data = layer_data.get_object(cls=cls)
# If the shape of the data is 2d, then use CCDData as the
# output data type
elif len(layer_data.shape) == 2:
layer_data = layer_data.get_object(cls=CCDData)

data[label] = layer_data

# For subsets, make sure to apply the subset mask to the
# layer data first
elif isinstance(layer_state.layer, Subset):
if include_subsets:
layer_data = layer_state.layer

if cls is not None:
handler, _ = data_translator.get_handler_for(cls)
try:
if cls == Spectrum1D:
# if this is a spectrum, apply the `statistic`:
layer_data = handler.to_object(layer_data,
statistic=statistic)
else:
# otherwise simply translate to an object:
layer_data = handler.to_object(layer_data)
except IncompatibleAttribute:
continue

data[label] = layer_data

# 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
objects.
It should be noted that the subset translation machinery lives in the
glue-astronomy repository. Currently, the machinery only works on 2D
data for cases like range selection. For e.g. a profile viewer that is
ostensibly just a view into a 3D data set, it is necessary to first
reduce the dimensions of the data, then retrieve the subset information
as a regions object. This means that the returned y extents in the
region are not strictly representative of the subset range in y.
Parameters
----------
viewer_reference : str
The reference to the viewer defined with the ``reference`` key
in the yaml configuration file.
data_label : str, optional
Optionally provide a label to retrieve a specific data set from the
viewer instance.
subset_type : str, optional
Optionally specify either "spectral" or "spatial" to return only
subsets created in a profile (spectrum) viewer or image viewer,
respectively.
Returns
-------
data : dict
A dict of the transformed Glue subset objects, with keys
representing the subset name and values as astropy regions
objects.
"""
viewer = self.get_viewer(viewer_reference)
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:
data = {data_label: data}

for key, value in data.items():
if isinstance(value, Subset):
# Get the component type in a compound subset
if hasattr(value.subset_state, "state1"):
this_type = type(value.subset_state.state1)
else:
this_type = type(value.subset_state)

# Skip spatial or spectral subsets if only the other is wanted
if subset_type == "spectral" or isinstance(viewer, BqplotProfileView):
if this_type == RoiSubsetState:
continue
elif subset_type == "spatial" or not isinstance(viewer, BqplotProfileView):
if this_type == RangeSubsetState:
continue

# Range selection on a profile is currently not supported in
# the glue translation machinery for astropy regions, so we
# have to do it manually. Only data that is 2d is supported,
# therefore, if the data is already 2d, simply use as is.
if value.data.ndim == 2:
region = value.data.get_selection_definition(
subset_id=key, format='astropy-regions')
regions[key] = region
continue

# There is a special case for 1d data (which is also not
# supported currently). We now eschew the use of the
# translation machinery entirely and construct the astropy
# region ourselves.
elif value.data.ndim == 1:
regions[key] = self.get_subsets(key)
continue

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

# Get the pixel coordinate [z] of the 3D data, repeating the
# wavelength axis. This doesn't seem strictly necessary as it
# returns the same data if the pixel axis is [y] or [x]
xid = value.data.pixel_component_ids[0]

# Construct a new data object collapsing over one of the
# spatial dimensions. This is necessary because the astropy
# region translation machinery in glue-astronomy does not
# support non-2D data, even for range objects.
stat_func = 'median'

if hasattr(viewer.state, 'function'):
stat_func = viewer.state.function

# Compute reduced data based on the current viewer's statistic
# function. This doesn't seem particularly useful, but better
# to be consistent.
reduced_data = Data(x=value.data.compute_statistic(
stat_func, value.data.id[xid],
subset_state=value.subset_state.copy(), axis=1))

# Instantiate a new data collection since we need to compose
# the collapsed data with the current subset state. We use a
# new data collection so as not to inference with the one used
# by the application.
temp_data_collection = DataCollection()
temp_data_collection.append(reduced_data)

# Get the data id of the pixel axis that will be used in the
# range composition. This is the wavelength axis for the new
# 2d data.
xid = reduced_data.pixel_component_ids[1]

# Create a new subset group to hold our current subset state
subset_group = temp_data_collection.new_subset_group(
label=value.label, subset_state=value.subset_state.copy())

# Set the subset state axis to the wavelength pixel coordinate
subset_group.subsets[0].subset_state.att = xid

# Use the associated collapsed subset data to get an astropy
# regions object dependent on the extends of the subset.
# **Note** that the y values in this region object are not
# useful, only the x values are.
region = subset_group.subsets[0].data.get_selection_definition(
format='astropy-regions')
regions[key] = region

return regions

def get_subsets(self, subset_name=None, spectral_only=False,
spatial_only=False, object_only=False,
simplify_spectral=True, use_display_units=False,
Expand Down
18 changes: 4 additions & 14 deletions jdaviz/configs/imviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from copy import deepcopy

import numpy as np
from astropy.utils.exceptions import AstropyDeprecationWarning
from glue.core import BaseData
from glue.core.link_helpers import LinkSame
from glue.plugins.wcs_autolinking.wcs_autolinking import WCSLink, NoAffineApproximation
Expand Down Expand Up @@ -71,7 +70,7 @@ def destroy_viewer(self, viewer_id):
raise ValueError(f"Default viewer '{viewer_id}' cannot be destroyed")
self.app.vue_destroy_viewer_item(viewer_id)

def load_data(self, data, data_label=None, do_link=True, show_in_viewer=True, **kwargs):
def load_data(self, data, data_label=None, show_in_viewer=True, **kwargs):
"""Load data into Imviz.
Parameters
Expand Down Expand Up @@ -106,11 +105,6 @@ def load_data(self, data, data_label=None, do_link=True, show_in_viewer=True, **
The final label shown in Imviz may have additional information
appended for clarity.
do_link : bool
Link the data after parsing. Set this to `False` if you want to
load multiple data back-to-back but you must remember to run
:meth:`link_data` manually at the end.
show_in_viewer : str or bool
If `True`, show the data in default viewer. If a string, show in that viewer.
Expand Down Expand Up @@ -190,21 +184,17 @@ def load_data(self, data, data_label=None, do_link=True, show_in_viewer=True, **
for applied_label in applied_labels:
self._delayed_show_in_viewer_labels[applied_label] = show_in_viewer

elif do_link:
else:
if 'Links Control' not in self.plugins.keys():
# otherwise plugin will handle linking automatically with DataCollectionAddMessage
self.link_data(link_type='pixels', error_on_fail=False)

# One input might load into multiple Data objects.
# NOTE: this will not add entries that were skipped with do_link=False
# but the batch_load context manager will handle that logic
# NOTE: If the batch_load context manager was used, it will
# handle that logic instead.
if show_in_viewer:
for applied_label in applied_labels:
self.app.add_data_to_viewer(show_in_viewer, applied_label)
else:
warnings.warn(AstropyDeprecationWarning("do_link=False is deprecated in v3.1 and will "
"be removed in a future release. Use with "
"viz.batch_load() instead."))

def link_data(self, **kwargs):
"""(Re)link loaded data in Imviz with the desired link type.
Expand Down
Loading

0 comments on commit db7c9f1

Please sign in to comment.