From 862531742247fcff296e7bd1968cb562c171f2fb Mon Sep 17 00:00:00 2001 From: "P. L. Lim" <2090236+pllim@users.noreply.github.com> Date: Fri, 29 Sep 2023 10:00:19 -0400 Subject: [PATCH] Simplify code. Co-authored-by: Thomas Robitaille --- jdaviz/app.py | 2 +- jdaviz/configs/specviz/plugins/viewers.py | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/jdaviz/app.py b/jdaviz/app.py index edc6643d8c..7b30de92d0 100644 --- a/jdaviz/app.py +++ b/jdaviz/app.py @@ -1935,7 +1935,7 @@ def set_data_visibility(self, viewer_reference, data_label, visible=True, replac viewer.add_data(data, percentile=95, color=viewer.color_cycler()) # Specviz removes the data from collection in viewer.py if flux unit incompatible. - if data_label not in self.data_collection.labels: + if data_label not in self.data_collection: return viewer.set_plot_axes() diff --git a/jdaviz/configs/specviz/plugins/viewers.py b/jdaviz/configs/specviz/plugins/viewers.py index b9bef18b02..efca9f0638 100644 --- a/jdaviz/configs/specviz/plugins/viewers.py +++ b/jdaviz/configs/specviz/plugins/viewers.py @@ -5,13 +5,14 @@ from astropy import units as u from astropy.nddata import StdDevUncertainty, VarianceUncertainty, InverseVariance from echo import delay_callback +from glue.config import data_translator from glue.core import BaseData -from glue_astronomy.spectral_coordinates import SpectralCoordinates +from glue.core.exceptions import IncompatibleAttribute +from glue.core.units import UnitConverter from glue.core.subset import Subset from glue.core.subset_group import GroupedSubset -from glue.config import data_translator +from glue_astronomy.spectral_coordinates import SpectralCoordinates from glue_jupyter.bqplot.profile import BqplotProfileView -from glue.core.exceptions import IncompatibleAttribute from matplotlib.colors import cnames from specutils import Spectrum1D @@ -25,6 +26,7 @@ __all__ = ['SpecvizProfileView'] +uc = UnitConverter() uncertainty_str_to_cls_mapping = { "std": StdDevUncertainty, @@ -369,12 +371,10 @@ def add_data(self, data, color=None, alpha=None, **layer_state): if len(self.layers) == 0: reset_plot_axes = True else: - # Check if the new data flux unit is actually compatible since flux not linked - data_flux_unit = u.Unit(y_units) - viewer_flux_unit = u.Unit(self.state.y_display_unit) - wav = 1 * u.Unit(self.state.x_display_unit) + # Check if the new data flux unit is actually compatible since flux not linked. try: - (1 * data_flux_unit).to(viewer_flux_unit, u.spectral_density(wav)) # Error if incompatible # noqa: E501 + uc.to_unit(data, data.find_component_id("flux"), [1, 1], + u.Unit(self.state.y_display_unit)) # Error if incompatible except Exception as err: # Raising exception here introduces a dirty state that messes up next load_data # but not raising exception also causes weird behavior unless we remove the data