Skip to content

Commit

Permalink
viewer user-APIs exposing zoom/limit options
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Nov 13, 2023
1 parent 6acb64a commit a414887
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
3 changes: 2 additions & 1 deletion jdaviz/configs/cubeviz/plugins/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from bqplot import Lines
from glue_jupyter.bqplot.image import BqplotImageView

from jdaviz.core.astrowidgets_api import AstrowidgetsImageViewerMixin
from jdaviz.core.registries import viewer_registry
from jdaviz.core.marks import SliceIndicatorMarks, ShadowSpatialSpectral
from jdaviz.configs.cubeviz.helper import layer_is_cube_image_data
Expand All @@ -16,7 +17,7 @@


@viewer_registry("cubeviz-image-viewer", label="Image 2D (Cubeviz)")
class CubevizImageView(JdavizViewerMixin, BqplotImageView):
class CubevizImageView(JdavizViewerMixin, BqplotImageView, AstrowidgetsImageViewerMixin):
# categories: zoom resets, (zoom, pan), subset, select tools, shortcuts
# NOTE: zoom and pan are merged here for space consideration and to avoid
# overflow to second row when opening the tray
Expand Down
22 changes: 22 additions & 0 deletions jdaviz/configs/default/plugins/viewers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from echo import delay_callback

from glue.viewers.scatter.state import ScatterLayerState as BqplotScatterLayerState
from glue_jupyter.bqplot.profile import BqplotProfileView
Expand All @@ -7,7 +8,9 @@

from jdaviz.configs.imviz.helper import layer_is_image_data
from jdaviz.components.toolbar_nested import NestedJupyterToolbar
from jdaviz.core.astrowidgets_api import AstrowidgetsImageViewerMixin
from jdaviz.core.registries import viewer_registry
from jdaviz.core.user_api import ViewerUserApi
from jdaviz.utils import ColorCycler, get_subset_type

__all__ = ['JdavizViewerMixin']
Expand All @@ -30,6 +33,25 @@ def __init__(self, *args, **kwargs):
# Allow each viewer to cycle through colors for each new addition to the viewer:
self.color_cycler = ColorCycler()

@property
def user_api(self):
if isinstance(self, AstrowidgetsImageViewerMixin):
expose = ['zoom', 'zoom_level']

Check warning on line 39 in jdaviz/configs/default/plugins/viewers.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/default/plugins/viewers.py#L38-L39

Added lines #L38 - L39 were not covered by tests
else:
expose = ['set_lims']
return ViewerUserApi(self, expose=expose)

Check warning on line 42 in jdaviz/configs/default/plugins/viewers.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/default/plugins/viewers.py#L41-L42

Added lines #L41 - L42 were not covered by tests

def set_lims(self, x_min=None, x_max=None, y_min=None, y_max=None):
with delay_callback(self.state, 'x_min', 'x_max', 'y_min', 'y_max'):
if x_min is not None:
self.state.x_min = x_min
if x_max is not None:
self.state.x_max = x_max
if y_min is not None:
self.state.y_min = y_min
if y_max is not None:
self.state.y_max = y_max

Check warning on line 53 in jdaviz/configs/default/plugins/viewers.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/configs/default/plugins/viewers.py#L45-L53

Added lines #L45 - L53 were not covered by tests

@property
def native_marks(self):
"""
Expand Down
13 changes: 13 additions & 0 deletions jdaviz/core/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ def plugins(self):
return {item['label']: widget_serialization['from_json'](item['widget'], None).user_api
for item in self.app.state.tray_items}

@property
def viewers(self):
"""
Access API objects for any viewer.
Returns
-------
viewers : dict
dict of viewer objects
"""
return {getattr(viewer, 'reference', k): viewer.user_api

Check warning on line 135 in jdaviz/core/helpers.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/core/helpers.py#L135

Added line #L135 was not covered by tests
for k, viewer in self.app._viewer_store.items()}

@property
def fitted_models(self):
"""
Expand Down
19 changes: 18 additions & 1 deletion jdaviz/core/user_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import astropy.units as u

__all__ = ['UserApiWrapper', 'PluginUserApi']
__all__ = ['UserApiWrapper', 'PluginUserApi', 'ViewerUserApi']

_internal_attrs = ('_obj', '_expose', '_readonly', '__doc__')

Expand Down Expand Up @@ -96,3 +96,20 @@ def __init__(self, plugin, expose=[], readonly=[]):

def __repr__(self):
return f'<{self._obj._registry_label} API>'


class ViewerUserApi(UserApiWrapper):
"""
This is an API wrapper around a viewer. For a full list of attributes/methods,
call dir(viewer_object) and for help on any of those methods,
call help(viewer_object.attribute).
For example::
help(viewer_object.show)
"""
def __init__(self, viewer, expose=[], readonly=[]):
expose = list(set(list(expose) + []))
super().__init__(viewer, expose, readonly)

Check warning on line 112 in jdaviz/core/user_api.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/core/user_api.py#L111-L112

Added lines #L111 - L112 were not covered by tests

def __repr__(self):
return f'<{self._obj.reference} API>'

Check warning on line 115 in jdaviz/core/user_api.py

View check run for this annotation

Codecov / codecov/patch

jdaviz/core/user_api.py#L115

Added line #L115 was not covered by tests

0 comments on commit a414887

Please sign in to comment.