Skip to content

Commit

Permalink
WIP: deprecate helper-specific methods in specviz/2d
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Jan 13, 2025
1 parent 2084d96 commit d134ff9
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 58 deletions.
3 changes: 3 additions & 0 deletions jdaviz/configs/cubeviz/helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from astropy.utils.decorators import deprecated

from jdaviz.configs.default.plugins.line_lists.line_list_mixin import LineListMixin
from jdaviz.configs.specviz import Specviz
from jdaviz.core.events import AddDataMessage, SnackbarMessage
Expand Down Expand Up @@ -112,6 +114,7 @@ def select_wavelength(self, wavelength):
self.select_slice(wavelength)

@property
@deprecated(since="4.2", alternative="viewers['spectrum-viewer']")
def specviz(self):
"""
A Specviz helper (:class:`~jdaviz.configs.specviz.helper.Specviz`) for the Jdaviz
Expand Down
2 changes: 0 additions & 2 deletions jdaviz/configs/default/plugins/data_quality/data_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ class DataQuality(PluginTemplateMixin, ViewerSelectMixin):
"""
template_file = __file__, "data_quality.vue"

irrelevant_msg = Unicode("").tag(sync=True)

# `layer` is the science data layer
science_layer_multiselect = Bool(False).tag(sync=True)
science_layer_items = List().tag(sync=True)
Expand Down
9 changes: 9 additions & 0 deletions jdaviz/configs/default/plugins/model_fitting/model_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class ModelFitting(PluginTemplateMixin, DatasetSelectMixin,
Label of the residuals to apply when calling :meth:`calculate_fit` if ``residuals_calculate``
is ``True``.
* :meth:`calculate_fit`
* :meth:`fitted_models`
"""
dialog = Bool(False).tag(sync=True)
template_file = __file__, "model_fitting.vue"
Expand Down Expand Up @@ -779,6 +780,14 @@ def equation_components(self):
"""
return re.split(r'[+*/-]', self.equation.value.replace(' ', ''))

@property
def fitted_models(self):
"""
Dictionary of all previously fitted models.
"""
# TODO: store this internally instead of within the app
return self.app.fitted_models

def vue_add_model(self, event):
self.create_model_component()

Expand Down
20 changes: 19 additions & 1 deletion jdaviz/configs/default/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def user_api(self):
elif isinstance(self, TableViewer):
expose += []
else:
expose += ['set_limits', 'reset_limits']
expose += ['set_limits', 'reset_limits', 'set_tick_format']
return ViewerUserApi(self, expose=expose)

@property
Expand Down Expand Up @@ -180,6 +180,24 @@ def get_limits(self):
"""
return self.state.x_min, self.state.x_max, self.state.y_min, self.state.y_max

def set_tick_format(self, fmt, axis):
"""
Manually set the tick format of one of the axes.
Parameters
----------
fmt : str
Format of tick marks.
For example, ``'0.1e'`` to set scientific notation or ``'0.2f'`` to turn it off.
axis : {x, y}
The viewer axis.
"""
if axis not in ('x', 'y'):
raise ValueError("axis must be 'x' or 'y'")
# Examples of values for fmt are '0.1e' or '0.2f'
axis = {'x': 0, 'y': 1}[axis]
self.figure.axes[axis].tick_format = fmt

@property
def native_marks(self):
"""
Expand Down
3 changes: 3 additions & 0 deletions jdaviz/configs/mosviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from astropy import units as u
from astropy.coordinates import SkyCoord
from astropy.table import QTable
from astropy.utils.decorators import deprecated
from glue.core.data import Data
from glue.core.exceptions import IncompatibleAttribute

Expand Down Expand Up @@ -862,6 +863,7 @@ def to_csv(self, filename="MOS_data.csv", selected=False, overwrite=False):
table_df.to_csv(filename, index_label="Table Index")

@property
@deprecated(since="4.2", alternative="viewers['spectrum-viewer']")
def specviz(self):
"""
A Specviz helper (:class:`~jdaviz.configs.specviz.helper.Specviz`) for the Jdaviz
Expand All @@ -872,6 +874,7 @@ def specviz(self):
return self._specviz

@property
@deprecated(since="4.2", alternative="viewers['spectrum-2d-viewer']")
def specviz2d(self):
"""
A Specviz2d helper (:class:`~jdaviz.configs.specviz2d.helper.Specviz2d`) for the Jdaviz
Expand Down
13 changes: 9 additions & 4 deletions jdaviz/configs/specviz/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def load_data(self, data, data_label=None, format=None, show_in_viewer=True,
timeout=timeout,
load_as_list=load_as_list)

@deprecated(since="4.2", alternative="get_data")
def get_spectra(self, data_label=None, spectral_subset=None, apply_slider_redshift="Warn"):
"""Returns the current data loaded into the main viewer
Expand Down Expand Up @@ -168,6 +169,7 @@ def get_spectral_regions(self, use_display_units=False):
"""
return self.app.get_subsets(spectral_only=True, use_display_units=use_display_units)

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].set_limits")
def x_limits(self, x_min=None, x_max=None):
"""Sets the limits of the x-axis
Expand All @@ -190,6 +192,7 @@ def x_limits(self, x_min=None, x_max=None):
ref_spec = self.get_spectra(ref_index, apply_slider_redshift=False)
self._set_scale(scale, ref_spec.spectral_axis, x_min, x_max)

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].set_limits")
def y_limits(self, y_min=None, y_max=None):
"""Sets the limits of the y-axis
Expand Down Expand Up @@ -249,32 +252,37 @@ def _set_scale(self, scale, axis, min_val=None, max_val=None):

scale.max = float(max_val)

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].reset_limits")
def autoscale_x(self):
"""Sets the x-axis limits to the min/max of the reference data
"""
self.x_limits("auto", "auto")

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].reset_limits")
def autoscale_y(self):
"""Sets the y-axis limits to the min/max of the reference data
"""
self.y_limits("auto", "auto")

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].set_limits")
def flip_x(self):
"""Flips the current limits of the x-axis
"""
scale = self.app.get_viewer(self._default_spectrum_viewer_reference_name).scale_x
self.x_limits(x_min=scale.max, x_max=scale.min)

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].set_limits")
def flip_y(self):
"""Flips the current limits of the y-axis
"""
scale = self.app.get_viewer(self._default_spectrum_viewer_reference_name).scale_y
self.y_limits(y_min=scale.max, y_max=scale.min)

@deprecated(since="4.2", alternative="viewers['spectrum-viewer'].set_tick_format")
def set_spectrum_tick_format(self, fmt, axis=None):
"""
Manually set the tick format of one of the axes of the profile viewer.
Expand All @@ -293,10 +301,7 @@ def set_spectrum_tick_format(self, fmt, axis=None):
warnings.warn("Please use either 0 or 1 for the axis value")
return

# Examples of values for fmt are '0.1e' or '0.2f'
self.app.get_viewer(
self._default_spectrum_viewer_reference_name
).figure.axes[axis].tick_format = fmt
self.viewers[self._default_spectrum_viewer_reference_name].set_tick_format(fmt, axis=['x', 'y'][axis])

def get_data(self, data_label=None, spectral_subset=None, cls=None,
use_display_units=False):
Expand Down
4 changes: 4 additions & 0 deletions jdaviz/configs/specviz2d/helper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from astropy.utils.decorators import deprecated

from jdaviz.configs.specviz import Specviz
from jdaviz.core.helpers import ConfigHelper
from jdaviz.core.events import SnackbarMessage
Expand All @@ -19,6 +21,7 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

@property
@deprecated(since="4.2", alternative="viewers['spectrum-viewer']")
def specviz(self):
"""
A Specviz helper (`~jdaviz.configs.specviz.helper.Specviz`) for the Jdaviz
Expand Down Expand Up @@ -152,6 +155,7 @@ def load_data(self, spectrum_2d=None, spectrum_1d=None, spectrum_1d_label=None,
timeout=timeout
)

@deprecated(since="4.2")
def load_trace(self, trace, data_label, show_in_viewer=True):
"""
Load a trace object and load into the spectrum-2d-viewer
Expand Down
9 changes: 7 additions & 2 deletions jdaviz/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ def viewers(self):
for viewer in self.app._viewer_store.values()}

@property
@deprecated(since="4.2", alternative="plugins['Model Fitting'].fitted_models")
def fitted_models(self):
"""
Returns the fitted models.
Expand All @@ -175,9 +176,12 @@ def fitted_models(self):
parameters : dict
dict of `astropy.modeling.Model` objects, or None.
"""
plg = self.plugins.get('Model Fitting', None)
if plg is None:
raise ValueError("Model Fitting plugin does not exist")
return plg.fitted_models

return self.app.fitted_models

@deprecated(since="4.2", alternative="plugins['Model Fitting'].fitted_models")
def get_models(self, models=None, model_label=None, x=None, y=None):
"""
Loop through all models and output models of the label model_label.
Expand Down Expand Up @@ -239,6 +243,7 @@ def get_models(self, models=None, model_label=None, x=None, y=None):

return selected_models

@deprecated(since="4.2")
def get_model_parameters(self, models=None, model_label=None, x=None, y=None):
"""
Convert each parameter of model inside models into a coordinate that
Expand Down
62 changes: 13 additions & 49 deletions notebooks/SpecvizExample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Or, if you've defined multiple regions, you can retrieve all defined regions/subsets via:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"specviz.get_spectra()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Screenshot Saving"
"## Exporting Viewer Image"
]
},
{
Expand All @@ -151,7 +133,9 @@
"metadata": {},
"outputs": [],
"source": [
"specviz.app.get_viewer(\"spectrum-viewer\").figure.save_png()"
"exp = specviz.plugins['Export']\n",
"exp.viewer = 'spectrum-viewer'\n",
"exp.export()"
]
},
{
Expand All @@ -160,7 +144,7 @@
"source": [
"## Panning/Zooming in Specviz\n",
"### Limit Methods\n",
"You can use the methods x_limits() and y_limits() to modify the field of view of Specviz. You can provide it a scalar (which assumes the units of the loaded spectra), an Astropy Quantity, or 'auto' to automatically scale."
"You can use the methods to modify the field of view of the default spectrum viewer. You can provide it a scalar (which assumes the units of the loaded spectra) or an Astropy Quantity."
]
},
{
Expand All @@ -171,9 +155,8 @@
},
"outputs": [],
"source": [
"specviz.x_limits()\n",
"specviz.x_limits(7*u.um,7.2*u.um)\n",
"specviz.y_limits('auto', 170.0)"
"specviz.viewers['spectrum-viewer'].get_limits()\n",
"specviz.viewers['spectrum-viewer'].set_limits(x_min=7*u.um, x_max=7.2*u.um, y_max=170.0)"
]
},
{
Expand All @@ -190,17 +173,16 @@
"metadata": {},
"outputs": [],
"source": [
"# axis 1 corresponds to the Y-axis and axis 0 to the X-axis\n",
"# fmt can be '0.1e' to set scientific notation or '0.2f' to turn it off\n",
"specviz.set_spectrum_tick_format(fmt=\"0.2f\", axis=1)"
"specviz.viewers['spectrum-viewer'].set_tick_format(fmt=\"0.2f\", axis='x')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Autoscale Methods\n",
"You can also quickly return to the default zoom using autoscale_x() and autoscale_y()"
"### Reset Zoom\n",
"You can also quickly return to the default zoom"
]
},
{
Expand All @@ -209,8 +191,7 @@
"metadata": {},
"outputs": [],
"source": [
"specviz.autoscale_x()\n",
"specviz.autoscale_y()"
"specviz.viewers['spectrum-viewer'].reset_limits()"
]
},
{
Expand All @@ -227,7 +208,7 @@
"metadata": {},
"outputs": [],
"source": [
"specviz.fitted_models"
"specviz.plugins['Model Fitting'].fitted_models"
]
},
{
Expand All @@ -243,24 +224,7 @@
"metadata": {},
"outputs": [],
"source": [
"specviz.fitted_models['Model']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Alternatively, the following getter can be used"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"models = specviz.get_models()\n",
"models"
"specviz.plugins['Model Fitting'].fitted_models['Model']"
]
}
],
Expand Down

0 comments on commit d134ff9

Please sign in to comment.