From 60ca1055d4caca1c50f6192b1472c10d5c2384cb Mon Sep 17 00:00:00 2001 From: Jernej Urankar Date: Fri, 23 Jun 2017 12:26:41 +0200 Subject: [PATCH 1/2] Hierarchical Clustering: numpy -> np --- Orange/widgets/unsupervised/owhierarchicalclustering.py | 6 +++--- .../unsupervised/tests/test_owhierarchicalclustering.py | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Orange/widgets/unsupervised/owhierarchicalclustering.py b/Orange/widgets/unsupervised/owhierarchicalclustering.py index a831d6eb411..b669d75924f 100644 --- a/Orange/widgets/unsupervised/owhierarchicalclustering.py +++ b/Orange/widgets/unsupervised/owhierarchicalclustering.py @@ -5,7 +5,7 @@ from itertools import chain from contextlib import contextmanager -import numpy +import numpy as np from AnyQt.QtWidgets import ( QGraphicsWidget, QGraphicsObject, QGraphicsLinearLayout, QGraphicsPathItem, @@ -605,7 +605,7 @@ def _rescale(self): else: drect = QSizeF(leaf_count, self._root.value.height) - eps = numpy.finfo(numpy.float64).eps + eps = np.finfo(np.float64).eps if abs(drect.width()) < eps: sx = 1.0 @@ -1135,7 +1135,7 @@ def commit(self): if isinstance(items, Orange.data.Table) and self.matrix.axis == 1: # Select rows - c = numpy.zeros(self.matrix.shape[0]) + c = np.zeros(self.matrix.shape[0]) for i, indices in enumerate(maps): c[indices] = i diff --git a/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py b/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py index 588ac41734a..8f32c56bea8 100644 --- a/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py +++ b/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py @@ -1,6 +1,6 @@ # Test methods with long descriptive names can omit docstrings # pylint: disable=missing-docstring -import numpy +import numpy as np import Orange.misc from Orange.distance import Euclidean from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin @@ -53,7 +53,7 @@ def test_selection_box_output(self): self.assertIsNone(self.get_output(self.widget.Outputs.annotated_data)) def test_all_zero_inputs(self): - d = Orange.misc.DistMatrix(numpy.zeros((10, 10))) + d = Orange.misc.DistMatrix(np.zeros((10, 10))) self.widget.set_distances(d) def test_annotation_settings_retrieval(self): @@ -61,8 +61,8 @@ def test_annotation_settings_retrieval(self): widget = self.widget dist_names = Orange.misc.DistMatrix( - numpy.zeros((4, 4)), self.data, axis=0) - dist_no_names = Orange.misc.DistMatrix(numpy.zeros((10, 10)), axis=1) + np.zeros((4, 4)), self.data, axis=0) + dist_no_names = Orange.misc.DistMatrix(np.zeros((10, 10)), axis=1) self.send_signal(self.widget.Inputs.distances, self.distances) # Check that default is set (class variable) From 510901e1d65357d98a70f529536aa5ff7c4a75fd Mon Sep 17 00:00:00 2001 From: Jernej Urankar Date: Fri, 23 Jun 2017 13:03:36 +0200 Subject: [PATCH 2/2] [FIX] Hierarchical Clustering: infinite distances --- .../unsupervised/owhierarchicalclustering.py | 10 ++++++++- .../tests/test_owhierarchicalclustering.py | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Orange/widgets/unsupervised/owhierarchicalclustering.py b/Orange/widgets/unsupervised/owhierarchicalclustering.py index b669d75924f..cbf95a97670 100644 --- a/Orange/widgets/unsupervised/owhierarchicalclustering.py +++ b/Orange/widgets/unsupervised/owhierarchicalclustering.py @@ -34,7 +34,7 @@ from Orange.widgets.utils import colorpalette, itemmodels from Orange.widgets.utils.annotated_data import (create_annotated_table, ANNOTATED_DATA_SIGNAL_NAME) -from Orange.widgets.widget import Input, Output +from Orange.widgets.widget import Input, Output, Msg __all__ = ["OWHierarchicalClustering"] @@ -763,6 +763,9 @@ class Outputs: cluster_roles = ["Attribute", "Class variable", "Meta variable"] basic_annotations = ["None", "Enumeration"] + class Error(widget.OWWidget.Error): + not_finite_distances = Msg("Some distances are infinite") + def __init__(self): super().__init__() @@ -962,11 +965,16 @@ def axis_view(orientation): @Inputs.distances def set_distances(self, matrix): self.error() + self.Error.clear() if matrix is not None: N, _ = matrix.shape if N < 2: self.error("Empty distance matrix") matrix = None + if matrix is not None: + if not np.all(np.isfinite(matrix)): + self.Error.not_finite_distances() + matrix = None self.matrix = matrix if matrix is not None: diff --git a/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py b/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py index 8f32c56bea8..99699fa2f55 100644 --- a/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py +++ b/Orange/widgets/unsupervised/tests/test_owhierarchicalclustering.py @@ -2,6 +2,7 @@ # pylint: disable=missing-docstring import numpy as np import Orange.misc +from Orange.data import Table, Domain, ContinuousVariable, DiscreteVariable from Orange.distance import Euclidean from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin from Orange.widgets.unsupervised.owhierarchicalclustering import \ @@ -102,3 +103,23 @@ def test_domain_loses_class(self): data = self.data[:, :4] distances = Euclidean(data) self.send_signal(self.widget.Inputs.distances, distances) + + def test_infinite_distances(self): + """ + Scipy does not accept infinite distances and neither does this widget. + Error is shown. + GH-2380 + """ + table = Table( + Domain( + [ContinuousVariable("a")], + [DiscreteVariable("b", values=["y"])]), + list(zip([1.79e308, -1e120], + "yy")) + ) + distances = Euclidean(table) + self.assertFalse(self.widget.Error.not_finite_distances.is_shown()) + self.send_signal(self.widget.Inputs.distances, distances) + self.assertTrue(self.widget.Error.not_finite_distances.is_shown()) + self.send_signal(self.widget.Inputs.distances, self.distances) + self.assertFalse(self.widget.Error.not_finite_distances.is_shown())