Skip to content

Commit

Permalink
Merge pull request #1931 from ales-erjavec/fixes/mds-size-nan
Browse files Browse the repository at this point in the history
[FIX] MDS Widget: Handle NaN values for plot point styling
  • Loading branch information
lanzagar authored Jan 18, 2017
2 parents c3a6b16 + 7165280 commit 6661596
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
9 changes: 6 additions & 3 deletions Orange/widgets/unsupervised/owmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,10 @@ def _setup_plot(self):

size = self._effective_matrix.shape[0]

def column(data, variable):
def column(data, variable, dtype=None):
a, _ = data.get_column_view(variable)
if dtype is not None:
a = a.astype(dtype)
return a.ravel()

def attributes(matrix):
Expand Down Expand Up @@ -835,7 +837,7 @@ def jitter(x, factor=1, rstate=None):
symbols = numpy.array(list(Symbols.keys()))

shape_var = self.shapevar_model[shape_index]
data = column(self.data, shape_var).astype(numpy.float)
data = column(self.data, shape_var, dtype=numpy.float)
data = data % (len(Symbols) - 1)
data[numpy.isnan(data)] = len(Symbols) - 1
shape_data = symbols[data.astype(int)]
Expand All @@ -861,9 +863,10 @@ def jitter(x, factor=1, rstate=None):
size_data = MinPointSize + size_data * point_size
elif have_data and size_index > 0:
size_var = self.sizevar_model[size_index]
size_data = column(self.data, size_var)
size_data = column(self.data, size_var, dtype=float)
size_data = scale(size_data)
size_data = MinPointSize + size_data * point_size
size_data[numpy.isnan(size_data)] = 1 # maybe 0?
else:
size_data = point_size
self._size_data = size_data
Expand Down
44 changes: 43 additions & 1 deletion Orange/widgets/unsupervised/tests/test_owmds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

import numpy as np

from AnyQt.QtCore import QEvent
from AnyQt.QtCore import QEvent, Qt
from AnyQt.QtWidgets import QComboBox
from AnyQt.QtTest import QTest

import Orange.data

from Orange.distance import Euclidean
from Orange.widgets.unsupervised.owmds import OWMDS
Expand Down Expand Up @@ -45,3 +49,41 @@ def test_pca_init(self):
output.X[2, 4:], np.array([-2.90231621, -0.13535431]))
np.testing.assert_array_almost_equal(
output.X[3, 4:], np.array([-2.75269913, -0.33885988]))

def test_nan_plot(self):
nan = np.nan
domain = Orange.data.Domain(
[Orange.data.ContinuousVariable("X{}".format(i))
for i in range(5)],
Orange.data.DiscreteVariable("D", values=["a", "b", "c"]),
[Orange.data.StringVariable("S"),
Orange.data.ContinuousVariable("XM")]
)
data = Orange.data.Table.from_numpy(
domain,
np.array(
[[nan, 2, 3, 2, nan],
[2, nan, 1, nan, 2],
[3, 1, nan, 1, 3],
[2, nan, 1, nan, 2],
[nan, 2, 3, 2, nan]]
),
np.array([[1], [nan], [1], [nan], [1]]),
np.array([["a", nan], ["", 1], ["b", 1], ["", 1], ["c", nan]],
dtype=object)
)
self.send_signal("Data", data)

def run_combo(cbox):
# type: (QComboBox) -> None
assert cbox.focusPolicy() & Qt.TabFocus
cbox.setFocus(Qt.TabFocusReason)
cbox.setCurrentIndex(-1)
for i in range(cbox.count()):
QTest.keyClick(cbox, Qt.Key_Down)

run_combo(self.widget.cb_color_value)
run_combo(self.widget.cb_shape_value)
run_combo(self.widget.cb_size_value)
run_combo(self.widget.cb_label_value)

0 comments on commit 6661596

Please sign in to comment.