diff --git a/glue_plotly/common/base_3d.py b/glue_plotly/common/base_3d.py index b2b6749..36a2a4b 100644 --- a/glue_plotly/common/base_3d.py +++ b/glue_plotly/common/base_3d.py @@ -1,4 +1,5 @@ import re +from contextlib import suppress from glue.config import settings from glue_plotly.common import DEFAULT_FONT @@ -29,21 +30,15 @@ def projection_type(viewer_state): def get_resolution(viewer_state): - try: - from glue_vispy_viewers.volume.viewer_state import Vispy3DVolumeViewerState - if isinstance(viewer_state, Vispy3DVolumeViewerState): - return viewer_state.resolution - except ImportError: - pass - - try: - from glue_jupyter.common.state3d import VolumeViewerState - if isinstance(viewer_state, VolumeViewerState): - resolutions = tuple(getattr(state, "max_resolution", None) + resolution = getattr(viewer_state, "resolution", None) + if resolution is not None: + return resolution + + resolutions = tuple(getattr(state, "max_resolution", None) for state in viewer_state.layers) - return max((res for res in resolutions if res is not None), default=256) - except ImportError: - pass + + with suppress(ValueError): + return max((res for res in resolutions if res is not None), default=256) return 256 diff --git a/glue_plotly/common/scatter3d.py b/glue_plotly/common/scatter3d.py index 5dd947d..718160a 100644 --- a/glue_plotly/common/scatter3d.py +++ b/glue_plotly/common/scatter3d.py @@ -10,11 +10,6 @@ from glue_plotly.common import color_info, sanitize from glue_plotly.common.base_3d import bbox_mask -try: - from glue_vispy_viewers.scatter.layer_state import ScatterLayerState -except ImportError: - ScatterLayerState = type(None) - def size_info(layer_state, mask, size_att="size_attribute"): @@ -123,10 +118,15 @@ def traces_for_layer(viewer_state, layer_state, hover_data=None, add_data_label= y = layer_state.layer[viewer_state.y_att] z = layer_state.layer[viewer_state.z_att] - vispy_layer_state = isinstance(layer_state, ScatterLayerState) - cmap_mode_attr = "color_mode" if vispy_layer_state else "cmap_mode" - cmap_attr = "cmap_attribute" if vispy_layer_state else "cmap_att" - size_attr = "size_attribute" if vispy_layer_state else "size_att" + cmap_mode_attr = "color_mode" \ + if hasattr(layer_state, "color_mode") \ + else "cmap_mode" + cmap_attr = "cmap_attribute" \ + if hasattr(layer_state, "cmap_attribute") \ + else "cmap_att" + size_attr = "size_attribute" \ + if hasattr(layer_state, "size_attribute") \ + else "size_att" arrs = [x, y, z] if getattr(layer_state, cmap_mode_attr) == "Linear": cvals = layer_state.layer[getattr(layer_state, cmap_attr)].copy() diff --git a/glue_plotly/html_exporters/jupyter/volume.py b/glue_plotly/html_exporters/jupyter/volume.py index 5a8a50b..e9ffe8f 100644 --- a/glue_plotly/html_exporters/jupyter/volume.py +++ b/glue_plotly/html_exporters/jupyter/volume.py @@ -1,5 +1,6 @@ +from contextlib import suppress + import plotly.graph_objs as go -from glue_vispy_viewers.scatter.layer_artist import ScatterLayerArtist from plotly.offline import plot from glue.config import viewer_tool @@ -9,6 +10,20 @@ from glue_plotly.common.volume import traces_for_layer as volume_traces_for_layer from glue_plotly.jupyter_base_export_tool import JupyterBaseExportTool +VOLUME_LAYER_STATES = [] + +with suppress(ImportError): + from glue_jupyter.ipyvolume.volume.layer_state import VolumeLayerState + VOLUME_LAYER_STATES.append(VolumeLayerState) + +with suppress(ImportError): + from glue.viewers.volume3d.layer_state import VolumeLayerState3D + VOLUME_LAYER_STATES.append(VolumeLayerState3D) + +with suppress(ImportError): + from glue_vispy_viewers.volume.layer_state import VolumeLayerState + VOLUME_LAYER_STATES.append(VolumeLayerState) + @viewer_tool class PlotlyScatter3DStaticExport(JupyterBaseExportTool): @@ -28,13 +43,13 @@ def save_figure(self, filepath): bds = bounds(self.viewer.state, with_resolution=True) count = 5 for layer in layers: - if isinstance(layer, ScatterLayerArtist): - traces = scatter3d_traces_for_layer(self.viewer.state, layer.state, - add_data_label=add_data_label) - else: + if isinstance(layer.state, tuple(VOLUME_LAYER_STATES)): traces = volume_traces_for_layer(self.viewer.state, layer.state, bds, isosurface_count=count, add_data_label=add_data_label) + else: + traces = scatter3d_traces_for_layer(self.viewer.state, layer.state, + add_data_label=add_data_label) for trace in traces: fig.add_trace(trace) diff --git a/tox.ini b/tox.ini index 6cae707..61fafc5 100644 --- a/tox.ini +++ b/tox.ini @@ -19,11 +19,11 @@ extras = all: jupyter all: 3d commands = - glue118: pip install glue-core==1.18.* glue-jupyter<=0.20.1 - glue119: pip install glue-core==1.19.* glue-jupyter<=0.20.1 - glue120: pip install glue-core==1.20.* glue-jupyter<=0.20.1 - glue121: pip install glue-core==1.21.* - glue122: pip install glue-core==1.22.* + glue118: pip install glue-core==1.18.* glue-jupyter<=0.20.1 glue-qt<=0.4 setuptools==81 + glue119: pip install glue-core==1.19.* glue-jupyter<=0.20.1 glue-qt<=0.4 setuptools==81 + glue120: pip install glue-core==1.20.* glue-jupyter<=0.20.1 glue-qt<=0.4 setuptools==81 + glue121: pip install glue-core==1.21.* glue-qt<=0.4 glue-jupyter<0.26 setuptools==81 + glue122: pip install glue-core==1.22.* glue-qt<=0.4 glue-jupyter<0.26 setuptools==81 plotly5: pip install plotly~=5.24 plotly6: pip install plotly>=6 test: pip freeze