Skip to content

Commit

Permalink
Add some error handling in the scatter layer artist.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Jul 5, 2024
1 parent 9996c84 commit cb727cf
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions glue_plotly/viewers/scatter/layer_artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from glue_plotly.common import color_info
from glue_plotly.common.scatter2d import LINESTYLES, rectilinear_lines, scatter_mode, size_info
from glue.core import BaseData
from glue.core.exceptions import IncompatibleAttribute

Check warning on line 9 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L9

Added line #L9 was not covered by tests
from glue.utils import ensure_numerical
from glue.viewers.common.layer_artist import LayerArtist
from glue.viewers.scatter.state import ScatterLayerState
Expand Down Expand Up @@ -67,10 +68,6 @@ def __init__(self, view, viewer_state, layer_state=None, layer=None):
layer=layer
)

self._viewer_state.add_global_callback(self._update_display)
self.state.add_global_callback(self._update_display)
self.state.add_callback("zorder", self._update_zorder)

self.view = view

# Somewhat annoyingly, the trace that we pass in to be added
Expand All @@ -93,6 +90,10 @@ def __init__(self, view, viewer_state, layer_state=None, layer=None):
self._error_id = uuid4().hex
self._vector_id = uuid4().hex

self._viewer_state.add_global_callback(self._update_display)
self.state.add_global_callback(self._update_display)
self.state.add_callback("zorder", self._update_zorder)

Check warning on line 95 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L93-L95

Added lines #L93 - L95 were not covered by tests

def remove(self):
self.view._remove_traces([self._get_scatter()])
self.view._remove_traces(self._get_lines())
Expand All @@ -104,7 +105,14 @@ def _get_traces_with_id(self, id):
return self.view.figure.select_traces(dict(meta=id))

def _get_scatter(self):
return next(self._get_traces_with_id(self._scatter_id))
# The scatter trace should always exist
# so if somehow it doesn't, then create it
try:
return next(self._get_traces_with_id(self._scatter_id))
except StopIteration:
scatter = self._create_scatter()
self.view.figure.add_trace(scatter)
return scatter

Check warning on line 115 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L110-L115

Added lines #L110 - L115 were not covered by tests

def _get_lines(self):
return self._get_traces_with_id(self._lines_id)
Expand All @@ -120,8 +128,23 @@ def traces(self):

def _update_data(self):

x = ensure_numerical(self.layer[self._viewer_state.x_att].ravel())
y = ensure_numerical(self.layer[self._viewer_state.y_att].ravel())
try:
x = ensure_numerical(self.layer[self._viewer_state.x_att].ravel())
except (IncompatibleAttribute, IndexError):
if self._viewer_state.x_att is not None:
self.disable_invalid_attributes(self._viewer_state.x_att)
return

Check warning on line 136 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L131-L136

Added lines #L131 - L136 were not covered by tests
else:
self.enable()

Check warning on line 138 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L138

Added line #L138 was not covered by tests

try:
y = ensure_numerical(self.layer[self._viewer_state.y_att].ravel())
except (IncompatibleAttribute, IndexError):
if self._viewer_state.y_att is not None:
self.disable_invalid_attributes(self._viewer_state.y_att)
return

Check warning on line 145 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L140-L145

Added lines #L140 - L145 were not covered by tests
else:
self.enable()

Check warning on line 147 in glue_plotly/viewers/scatter/layer_artist.py

View check run for this annotation

Codecov / codecov/patch

glue_plotly/viewers/scatter/layer_artist.py#L147

Added line #L147 was not covered by tests

scatter = self._get_scatter()
if self._viewer_state.using_rectilinear:
Expand Down

0 comments on commit cb727cf

Please sign in to comment.