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 export tools for bqplot viewers #45

Merged
merged 21 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8557f16
Modify several image functions to only require state as input.
Carifio24 Aug 20, 2023
eb5f01b
More changes to move towards better frontend flexibility.
Carifio24 Aug 21, 2023
0cf51bb
Move current exporters to Qt folder and start adding exporters to Jup…
Carifio24 Aug 26, 2023
4af1c1f
Add initial implementations of histogram, profile and image bqplot ex…
Carifio24 Aug 26, 2023
f2719d3
Update bar gap for bqplot exporter.
Carifio24 Aug 27, 2023
fb83fb6
Use SVG logo for glue-jupyter compatibility.
Carifio24 Aug 27, 2023
4194fbd
Add configuration for bqplot image viewer and use that in exporter.
Carifio24 Sep 3, 2023
3eb05f6
Use viewer output widget to create file selection dialog.
Carifio24 Sep 6, 2023
e3928e4
Codestyle fixes.
Carifio24 Sep 6, 2023
52c6329
Use png logo for Qt viewers. Add init file for Qt exporter tests.
Carifio24 Sep 7, 2023
54b2a5b
Update test of common histogram functionality.
Carifio24 Sep 7, 2023
e2f9f77
Create base test for Qt exporter tests.
Carifio24 Sep 8, 2023
2610699
Make sure each dialog is destroyed when it's closed.
Carifio24 Sep 8, 2023
e67cd36
Add tests for bqplot exporters and make codestyle fixes.
Carifio24 Sep 9, 2023
808730e
Refactoring of exporter tests and updates to package data.
Carifio24 Sep 9, 2023
47f235d
Codestyle fixes.
Carifio24 Sep 9, 2023
8fc9629
Add importorskip statements to exporter tests.
Carifio24 Sep 10, 2023
938a763
Add importorskip for ipyvuetify in base bqplot exporter test.
Carifio24 Sep 10, 2023
b1bc72f
Add noqa comments.
Carifio24 Sep 10, 2023
6f8a6a3
Add jupyter to test_extras.
Carifio24 Sep 10, 2023
c49208f
Remove importorskip on non-test file.
Carifio24 Sep 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci_workflows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
uses: OpenAstronomy/github-actions-workflows/.github/workflows/publish_pure_python.yml@v1
with:
libraries: '^libxcb.*-dev libxkbcommon-x11-dev libgl1-mesa-glx xvfb'
test_extras: 'test,qt'
test_extras: 'test,qt,jupyter'
test_command: Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 & sleep 3; DISPLAY=:99.0 pytest --pyargs glue_plotly
secrets:
pypi_token: ${{ secrets.pypi_token }}
30 changes: 28 additions & 2 deletions glue_plotly/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@
except DistributionNotFound:
pass

PLOTLY_LOGO = os.path.abspath(os.path.join(os.path.dirname(__file__), 'logo.png'))

PLOTLY_LOGO = os.path.abspath(os.path.join(os.path.dirname(__file__), 'logo'))
PLOTLY_ERROR_MESSAGE = "An error occurred during the export to Plotly:"


def setup():
try:
setup_qt()
except ImportError:
pass

Check warning on line 19 in glue_plotly/__init__.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/__init__.py#L18-L19

Added lines #L18 - L19 were not covered by tests

try:
setup_jupyter()
except ImportError:
pass

Check warning on line 24 in glue_plotly/__init__.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/__init__.py#L23-L24

Added lines #L23 - L24 were not covered by tests


def setup_qt():

from . import common # noqa
from . import html_exporters # noqa
from .html_exporters import qt # noqa
from .web.qt import setup
setup()

Expand Down Expand Up @@ -61,3 +74,16 @@
pass
else:
VispyScatterViewer.subtools['save'] = VispyScatterViewer.subtools['save'] + ['save:plotly3d']


def setup_jupyter():
from .html_exporters import bqplot # 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

BqplotHistogramView.tools += ['save:bqplot_plotlyhist']
BqplotImageView.tools += ['save:bqplot_plotlyimage2d']
BqplotProfileView.tools += ['save:bqplot_plotlyprofile']
BqplotScatterView.tools += ['save:bqplot_plotly2d']
9 changes: 8 additions & 1 deletion glue_plotly/common/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

from glue.config import settings
from glue.core import BaseData
from glue_qt.viewers.common.data_viewer import DataViewer
try:
from glue_qt.viewers.common.data_viewer import DataViewer
from glue_jupyter.bqplot.common import BqplotBaseView
except ImportError:
DataViewer = type(None)
BqplotBaseView = type(None)

Check warning on line 11 in glue_plotly/common/common.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/common.py#L9-L11

Added lines #L9 - L11 were not covered by tests

from glue_plotly.utils import is_rgba_hex, opacity_value_string, rgba_hex_to_rgb_hex

Expand All @@ -14,6 +19,8 @@
# TODO: Add implementation for bqplot viewers
if isinstance(viewer, DataViewer):
return viewer.figure.get_size_inches() * viewer.figure.dpi
elif isinstance(viewer, BqplotBaseView):
return 1200, 600 # TODO: What to do here?
else: # For now, this means a Plotly-based viewer
return viewer.figure.layout.width, viewer.figure.layout.height

Expand Down
18 changes: 14 additions & 4 deletions glue_plotly/common/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@ def axis_from_mpl(viewer, ax, glue_ticks=True):
return a


def layout_config_from_mpl(viewer):
config = base_layout_config(viewer, barmode="overlay", bargap=0)
def layout_config(viewer, **kwargs):
kwargs.setdefault("barmode", "overlay")
kwargs.setdefault("bargap", 0)
config = base_layout_config(viewer, **kwargs)
x_axis = base_rectilinear_axis(viewer.state, 'x')
y_axis = base_rectilinear_axis(viewer.state, 'y')
config.update(xaxis=x_axis, yaxis=y_axis)
return config


def layout_config_from_mpl(viewer, **kwargs):
kwargs.setdefault("barmode", "overlay")
kwargs.setdefault("bargap", 0)
config = base_layout_config(viewer, **kwargs)
x_axis = axis_from_mpl(viewer, 'x')
y_axis = axis_from_mpl(viewer, 'y')
config.update(xaxis=x_axis, yaxis=y_axis)
Expand Down Expand Up @@ -56,11 +68,9 @@ def traces_for_layer(viewer_state, layer_state, add_data_label=True):
)
traces.append(Bar(**hist_info))
else:
width = edges[1] - edges[0]
hist_info.update(
x=x,
y=y,
width=width,
meta=bars_id
)
traces.append(Bar(**hist_info))
Expand Down
102 changes: 57 additions & 45 deletions glue_plotly/common/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from glue.config import settings
from glue.utils import ensure_numerical

from glue_plotly.common.common import base_rectilinear_axis
try:
from glue.config import stretches
except ImportError:
Expand All @@ -33,21 +35,21 @@
if using_colormaps:
return [256, 256, 256, 1]
else:
img = viewer.axes._composite()
img = composite_array(viewer)()

Check warning on line 38 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L38

Added line #L38 was not covered by tests
bg_color = [_ for _ in img[0][0]]
for i in range(3):
bg_color[i] *= 256
return bg_color


def layout_config_from_mpl(viewer):
def layout_config(viewer):
bg_color = background_color(viewer)
return base_layout_config(viewer,
plot_bgcolor='rgba{0}'.format(tuple(bg_color)),
showlegend=True)


def axes_data(viewer):
def axes_data_from_mpl(viewer):
axes_data = {}

axes = viewer.axes
Expand Down Expand Up @@ -120,9 +122,25 @@
return axes_data


def shape(viewer):
xy_axes = sorted([viewer.state.x_att.axis, viewer.state.y_att.axis])
return [viewer.state.reference_data.shape[i] for i in xy_axes]
def axes_data_from_bqplot(viewer):
return dict(
xaxis=base_rectilinear_axis(viewer.state, 'x'),
yaxis=base_rectilinear_axis(viewer.state, 'y')
)


def shape(viewer_state):
xy_axes = sorted([viewer_state.x_att.axis, viewer_state.y_att.axis])
return [viewer_state.reference_data.shape[i] for i in xy_axes]


def composite_array(viewer):
# Qt viewer
try:
return viewer.axes._composite

Check warning on line 140 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L139-L140

Added lines #L139 - L140 were not covered by tests
# bqplot
except AttributeError:
return viewer._composite

Check warning on line 143 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L142-L143

Added lines #L142 - L143 were not covered by tests


def image_size_info(layer_state):
Expand Down Expand Up @@ -176,20 +194,19 @@
return dict(scatter=scatter_layers, image=image_layers, image_subset=image_subset_layers)


def full_view_transpose(viewer):
state = viewer.state
full_view, _agg_func, transpose = state.numpy_slice_aggregation_transpose
full_view[state.x_att.axis] = slice(None)
full_view[state.y_att.axis] = slice(None)
for i in range(state.reference_data.ndim):
def full_view_transpose(viewer_state):
full_view, _agg_func, transpose = viewer_state.numpy_slice_aggregation_transpose
full_view[viewer_state.x_att.axis] = slice(None)
full_view[viewer_state.y_att.axis] = slice(None)
for i in range(viewer_state.reference_data.ndim):

Check warning on line 201 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L198-L201

Added lines #L198 - L201 were not covered by tests
if isinstance(full_view[i], slice):
full_view[i] = slice_to_bound(full_view[i], state.reference_data.shape[i])
full_view[i] = slice_to_bound(full_view[i], viewer_state.reference_data.shape[i])

Check warning on line 203 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L203

Added line #L203 was not covered by tests

return full_view, transpose


def empty_secondary_layer(viewer, secondary_x, secondary_y):
bg = np.ones(shape(viewer))
def empty_secondary_layer(viewer_state, secondary_x, secondary_y):
bg = np.ones(shape(viewer_state))

Check warning on line 209 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L209

Added line #L209 was not covered by tests
secondary_info = dict(z=bg,
colorscale=[[0, 'rgb(0,0,0)'], [1, 'rgb(0,0,0)']],
hoverinfo='skip',
Expand All @@ -200,48 +217,44 @@
return Heatmap(**secondary_info)


def background_heatmap_layer(viewer):
def background_heatmap_layer(viewer_state):
"""
This function creates an all-white heatmap which we can use as the bottom layer
when the viewer is using colormap, to match what we see in glue
"""
bg = np.ones(shape(viewer))
bg = np.ones(shape(viewer_state))
bottom_color = (256, 256, 256)
bottom_colorstring = 'rgb{0}'.format(bottom_color)
bottom_info = dict(z=bg, hoverinfo='skip', opacity=1, showscale=False,
colorscale=[[0, bottom_colorstring], [1, bottom_colorstring]])
return Heatmap(**bottom_info)


def traces_for_pixel_subset_layer(viewer, layer):
state = layer.state
subset_state = layer.layer.subset_state
xmin, xmax = viewer.axes.get_xlim()
ymin, ymax = viewer.axes.get_ylim()
def traces_for_pixel_subset_layer(viewer_state, layer_state):
subset_state = layer_state.layer.subset_state

Check warning on line 234 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L234

Added line #L234 was not covered by tests

try:
x, y = subset_state.get_xy(layer.layer.data, viewer.state.x_att.axis, viewer.state.y_att.axis)
x, y = subset_state.get_xy(layer_state.layer.data, viewer_state.x_att.axis, viewer_state.y_att.axis)

Check warning on line 237 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L237

Added line #L237 was not covered by tests
line_data = dict(
mode="lines",
marker=dict(
color=state.color
color=layer_state.color
),
opacity=state.alpha * 0.5,
name=state.layer.label,
opacity=layer_state.alpha * 0.5,
name=layer_state.layer.label,
legendgroup=uuid4().hex
)

x_line_data = {**line_data, 'x': [x, x], 'y': [ymin, ymax], 'showlegend': True}
y_line_data = {**line_data, 'x': [xmin, xmax], 'y': [y, y], 'showlegend': False}
x_line_data = {**line_data, 'x': [x, x], 'y': [viewer_state.y_min, viewer_state.y_max], 'showlegend': True}
y_line_data = {**line_data, 'x': [viewer_state.x_min, viewer_state.x_max], 'y': [y, y], 'showlegend': False}

Check warning on line 249 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L248-L249

Added lines #L248 - L249 were not covered by tests
return [Scatter(**x_line_data), Scatter(**y_line_data)]
except IncompatibleAttribute:
return []


def traces_for_nonpixel_subset_layer(viewer, layer, full_view, transpose):
layer_state = layer.state
subset_state = layer.layer.subset_state
ref_data = viewer.state.reference_data
def traces_for_nonpixel_subset_layer(viewer_state, layer_state, full_view, transpose):
subset_state = layer_state.layer.subset_state
ref_data = viewer_state.reference_data

Check warning on line 257 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L256-L257

Added lines #L256 - L257 were not covered by tests
color = fixed_color(layer_state)
buffer = ref_data.compute_fixed_resolution_buffer(full_view, target_data=ref_data,
broadcast=False, subset_state=subset_state)
Expand Down Expand Up @@ -270,10 +283,7 @@
return [Heatmap(**image_info)]


def traces_for_scatter_layer(viewer, layer, hover_data=None, add_data_label=True):
viewer_state = viewer.state
layer_state = layer.state

def traces_for_scatter_layer(viewer_state, layer_state, hover_data=None, add_data_label=True):
x = layer_state.layer[viewer_state.x_att].copy()
y = layer_state.layer[viewer_state.y_att].copy()
mask, (x, y) = sanitize(x, y)
Expand All @@ -300,8 +310,8 @@
hover_values[k]))

name = layer_state.layer.label
if add_data_label and not isinstance(layer.layer, BaseData):
name += " ({0})".format(layer.layer.data.label)
if add_data_label and not isinstance(layer_state.layer, BaseData):
name += " ({0})".format(layer_state.layer.data.label)

Check warning on line 314 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L313-L314

Added lines #L313 - L314 were not covered by tests
scatter_info = dict(mode='markers',
marker=marker,
x=x,
Expand All @@ -320,6 +330,8 @@

interval = ManualInterval(layer_state.v_min, layer_state.v_max)
constrast_bias = ContrastBiasStretch(layer_state.contrast, layer_state.bias)

# This works for either Qt or bqplot layers
array = layer.get_image_data
if callable(array):
array = array(bounds=None)
Expand Down Expand Up @@ -348,7 +360,7 @@


def single_color_trace(viewer):
img = viewer.axes._composite()
img = composite_array(viewer)()

Check warning on line 363 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L363

Added line #L363 was not covered by tests
img[:, :, :3] *= 256
image_info = dict(z=img,
opacity=1,
Expand All @@ -365,10 +377,10 @@
has_nonpixel_subset = any(not isinstance(layer.layer.subset_state, PixelSubsetState)
for layer in layers['image_subset'])
if has_nonpixel_subset:
full_view, transpose = full_view_transpose(viewer)
full_view, transpose = full_view_transpose(viewer.state)

Check warning on line 380 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L380

Added line #L380 was not covered by tests

if using_colormaps:
traces.append(background_heatmap_layer(viewer))
traces.append(background_heatmap_layer(viewer.state))
for layer in layers['image']:
traces += traces_for_image_layer(layer)
else:
Expand All @@ -377,16 +389,16 @@
for layer in layers['image_subset']:
subset_state = layer.layer.subset_state
if isinstance(subset_state, PixelSubsetState):
traces += traces_for_pixel_subset_layer(viewer, layer)
traces += traces_for_pixel_subset_layer(viewer.state, layer.state)

Check warning on line 392 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L392

Added line #L392 was not covered by tests
else:
traces += traces_for_nonpixel_subset_layer(viewer, layer, full_view, transpose)
traces += traces_for_nonpixel_subset_layer(viewer.state, layer.state, full_view, transpose)

Check warning on line 394 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L394

Added line #L394 was not covered by tests

for layer in layers['scatter']:
traces += traces_for_scatter_layer(viewer, layer,
traces += traces_for_scatter_layer(viewer.state, layer.state,

Check warning on line 397 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L397

Added line #L397 was not covered by tests
hover_data=hover_selections[layer.state.layer.label],
add_data_label=add_data_label)

if secondary_x or secondary_y:
traces.append(empty_secondary_layer(viewer, secondary_x, secondary_y))
traces.append(empty_secondary_layer(viewer.state, secondary_x, secondary_y))

Check warning on line 402 in glue_plotly/common/image.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/common/image.py#L402

Added line #L402 was not covered by tests

return traces
2 changes: 1 addition & 1 deletion glue_plotly/common/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from plotly.graph_objs import Scatter

from glue_plotly.common import fixed_color
from glue_plotly.common.histogram import axis_from_mpl, layout_config_from_mpl # noqa
from glue_plotly.common.histogram import axis_from_mpl, layout_config, layout_config_from_mpl # noqa


def traces_for_layer(viewer_state, layer_state, add_data_label=True):
Expand Down
2 changes: 0 additions & 2 deletions glue_plotly/common/tests/test_histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@ def test_basic_trace(self):
assert len(traces) == 1
trace = traces[0]
assert isinstance(trace, Bar)
edges, _ = self.layer.state.histogram
assert trace['width'] == edges[1] - edges[0]

def test_log_trace(self):
self.viewer.state.x_log = True
Expand Down
7 changes: 0 additions & 7 deletions glue_plotly/html_exporters/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from . import scatter2d # noqa
from . import scatter3d # noqa
from . import image # noqa
from . import histogram # noqa
from . import profile # noqa
from . import table # noqa
from . import dendrogram # noqa
4 changes: 4 additions & 0 deletions glue_plotly/html_exporters/bqplot/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import histogram # noqa
from . import image # noqa
from . import profile # noqa
from . import scatter2d # noqa