Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exporters for Jupyter VisPy viewers #84

Merged
merged 6 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion glue_plotly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,21 @@


def setup_jupyter():
from .html_exporters import bqplot # noqa
from .html_exporters import jupyter # noqa
from glue_jupyter.bqplot.histogram import BqplotHistogramView
from glue_jupyter.bqplot.image import BqplotImageView
from glue_jupyter.bqplot.profile import BqplotProfileView
from glue_jupyter.bqplot.scatter import BqplotScatterView

try:
from glue_vispy_viewers.scatter.jupyter import JupyterVispyScatterViewer
from glue_vispy_viewers.volume.jupyter import JupyterVispyVolumeViewer
except ImportError:
pass

Check warning on line 98 in glue_plotly/__init__.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/__init__.py#L97-L98

Added lines #L97 - L98 were not covered by tests
else:
JupyterVispyScatterViewer.tools += ['save:jupyter_plotly3dscatter']
JupyterVispyVolumeViewer.tools += ['save:jupyter_plotlyvolume']

BqplotHistogramView.tools += ['save:bqplot_plotlyhist']
BqplotImageView.tools += ['save:bqplot_plotlyimage2d']
BqplotProfileView.tools += ['save:bqplot_plotlyprofile']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
from . import image # noqa
from . import profile # noqa
from . import scatter2d # noqa
from . import vispy_scatter # noqa
from . import vispy_volume # noqa
24 changes: 24 additions & 0 deletions glue_plotly/html_exporters/jupyter/tests/test_scatter3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

from glue.core import Data
from glue_plotly.html_exporters.jupyter.tests.test_base import TestBqplotExporter

from pytest import importorskip

importorskip('glue_jupyter')
importorskip('glue_vispy_viewers')

from glue_vispy_viewers.scatter.jupyter import JupyterVispyScatterViewer # noqa: E402


class TestScatter3D(TestBqplotExporter):

viewer_type = JupyterVispyScatterViewer
tool_id = 'save:jupyter_plotly3dscatter'

def make_data(self):
return Data(x=[1, 2, 3], y=[4, 5, 6], z=[7, 8, 9], label='d1')

def test_default(self, tmpdir):
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)
29 changes: 29 additions & 0 deletions glue_plotly/html_exporters/jupyter/tests/test_volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

from glue.core import Data
from glue_plotly.html_exporters.jupyter.tests.test_base import TestBqplotExporter

from pytest import importorskip

importorskip('glue_jupyter')
importorskip('glue_vispy_viewers')

from glue_vispy_viewers.volume.jupyter import JupyterVispyVolumeViewer # noqa: E402

from numpy import arange, ones # noqa: E402


class TestVolume(TestBqplotExporter):

viewer_type = JupyterVispyVolumeViewer
tool_id = 'save:jupyter_plotlyvolume'

def make_data(self):
return Data(label='d1',
x=arange(24).reshape((2, 3, 4)),
y=ones((2, 3, 4)),
z=arange(100, 124).reshape((2, 3, 4)))

def test_default(self, tmpdir):
output_path = self.export_figure(tmpdir, 'test_default.html')
assert os.path.exists(output_path)
33 changes: 33 additions & 0 deletions glue_plotly/html_exporters/jupyter/vispy_scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from glue.config import viewer_tool

from glue_plotly.common.base_3d import layout_config
from glue_plotly.common.common import data_count, layers_to_export
from glue_plotly.common.scatter3d import traces_for_layer
from glue_plotly.jupyter_base_export_tool import JupyterBaseExportTool

import plotly.graph_objs as go
from plotly.offline import plot


@viewer_tool
class PlotlyScatter3DStaticExport(JupyterBaseExportTool):
tool_id = 'save:jupyter_plotly3dscatter'

def save_figure(self, filepath):

if not filepath:
return

Check warning on line 19 in glue_plotly/html_exporters/jupyter/vispy_scatter.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/html_exporters/jupyter/vispy_scatter.py#L19

Added line #L19 was not covered by tests

config = layout_config(self.viewer.state)
layout = go.Layout(**config)
fig = go.Figure(layout=layout)

layers = layers_to_export(self.viewer)
add_data_label = data_count(layers) > 1
for layer in layers:
traces = traces_for_layer(self.viewer.state, layer.state,
add_data_label=add_data_label)
for trace in traces:
fig.add_trace(trace)

plot(fig, filename=filepath, auto_open=False)
43 changes: 43 additions & 0 deletions glue_plotly/html_exporters/jupyter/vispy_volume.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from glue.config import viewer_tool
from glue_vispy_viewers.scatter.layer_artist import ScatterLayerArtist

from glue_plotly.common.base_3d import layout_config
from glue_plotly.common.common import data_count, layers_to_export
from glue_plotly.common.scatter3d import traces_for_layer as scatter3d_traces_for_layer
from glue_plotly.common.volume import traces_for_layer as volume_traces_for_layer
from glue_plotly.jupyter_base_export_tool import JupyterBaseExportTool

import plotly.graph_objs as go
from plotly.offline import plot


@viewer_tool
class PlotlyScatter3DStaticExport(JupyterBaseExportTool):
tool_id = 'save:jupyter_plotlyvolume'

def save_figure(self, filepath):

if not filepath:
return

Check warning on line 21 in glue_plotly/html_exporters/jupyter/vispy_volume.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/html_exporters/jupyter/vispy_volume.py#L21

Added line #L21 was not covered by tests

config = layout_config(self.viewer.state)
layout = go.Layout(**config)
fig = go.Figure(layout=layout)

layers = layers_to_export(self.viewer)
add_data_label = data_count(layers) > 1
bounds = self.viewer._vispy_widget._multivol._data_bounds
count = 5
for layer in layers:
if isinstance(layer, ScatterLayerArtist):
traces = scatter3d_traces_for_layer(self.viewer.state, layer.state,

Check warning on line 33 in glue_plotly/html_exporters/jupyter/vispy_volume.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/html_exporters/jupyter/vispy_volume.py#L33

Added line #L33 was not covered by tests
add_data_label=add_data_label)
else:
traces = volume_traces_for_layer(self.viewer.state, layer.state, bounds,
isosurface_count=count,
add_data_label=add_data_label)

for trace in traces:
fig.add_trace(trace)

plot(fig, filename=filepath, auto_open=False)
2 changes: 1 addition & 1 deletion glue_plotly/html_exporters/qt/scatter3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

from glue.config import viewer_tool, settings
from glue.core import DataCollection, Data
from glue.viewers.common.tool import Tool
from glue_qt.core.dialogs import warn
from glue_qt.utils import messagebox_on_error
from glue_qt.utils.threading import Worker
from glue_qt.viewers.common.tool import Tool

from glue_plotly import PLOTLY_ERROR_MESSAGE, PLOTLY_LOGO
from glue_plotly.common import data_count, layers_to_export
Expand Down