Skip to content

Commit

Permalink
Merge pull request #1069 from astrofrog/fix-index-error
Browse files Browse the repository at this point in the history
Fix errors in categorical selection
  • Loading branch information
astrofrog authored Aug 8, 2016
2 parents b43208b + 142d5ec commit 268082e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ v0.9.0 (unreleased)
``glue.external.qt`` package is now deprecated. The ``get_qapp`` and
``load_ui`` functions are now available in ``glue.utils.qt``.

* Avoid raising a (harmless) error when selecting a region in between two
categorical components.

v0.8.3 (unreleased)
-------------------

Expand Down
6 changes: 3 additions & 3 deletions glue/core/qt/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import numpy as np

from qtpy.QtCore import Qt
from qtpy import QtCore, QtWidgets
from qtpy import QtCore, QtGui, QtWidgets
from glue.core import roi
from glue.utils.qt import mpl_to_qt4_color

Expand Down Expand Up @@ -46,7 +46,7 @@ def paint(self, canvas):

def draw_polygon(self, canvas, x, y):
x, y = self._transform(x, y)
poly = QtWidgets.QPolygon()
poly = QtGui.QPolygon()
points = [QtCore.QPoint(xx, yy) for xx, yy in zip(x, y)]
for p in points:
poly.append(p)
Expand All @@ -70,7 +70,7 @@ def get_painter(self, canvas):
edgecolor = mpl_to_qt4_color(self.plot_opts['edgecolor'],
self.plot_opts['alpha'])

pen = QtWidgets.QPen(edgecolor)
pen = QtGui.QPen(edgecolor)
pen.setWidth(self.plot_opts.get('edgewidth', 0))
p.setPen(pen)

Expand Down
13 changes: 7 additions & 6 deletions glue/core/roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,14 +1219,15 @@ def contains(self, x, y):
within the ROI
"""

check = self._categorical_helper(x)
index = np.minimum(np.searchsorted(self.categories, check),
len(self.categories) - 1)
return self.categories[index] == check
if self.categories is None or len(self.categories) == 0:
return np.zeros(x.shape, dtype=bool)
else:
check = self._categorical_helper(x)
index = np.minimum(np.searchsorted(self.categories, check),
len(self.categories) - 1)
return self.categories[index] == check

def update_categories(self, categories):

self.categories = np.unique(self._categorical_helper(categories))

def defined(self):
Expand Down
5 changes: 5 additions & 0 deletions glue/core/tests/test_roi.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ def test_from_range(self):
np.testing.assert_array_equal(roi.categories,
np.array(list('ghij')))

def test_empty_categories(self):
roi = CategoricalROI()
contains = roi.contains(np.array(['a','b','c']), None)
np.testing.assert_array_equal(contains, [0, 0, 0])


class DummyEvent(object):
def __init__(self, x, y, inaxes=True, key=None):
Expand Down

0 comments on commit 268082e

Please sign in to comment.