From 4c2faa2881b895a20a91eb37677a3273b5419a52 Mon Sep 17 00:00:00 2001 From: Vesna Tanko Date: Wed, 12 Oct 2016 10:19:36 +0200 Subject: [PATCH 1/2] OWConfusionMatrix: Output None when no data is selected --- Orange/widgets/evaluate/owconfusionmatrix.py | 76 ++++++++++--------- .../evaluate/tests/test_owconfusionmatrix.py | 16 ++++ 2 files changed, 55 insertions(+), 37 deletions(-) diff --git a/Orange/widgets/evaluate/owconfusionmatrix.py b/Orange/widgets/evaluate/owconfusionmatrix.py index d23592df1a1..7f15a702571 100644 --- a/Orange/widgets/evaluate/owconfusionmatrix.py +++ b/Orange/widgets/evaluate/owconfusionmatrix.py @@ -324,44 +324,46 @@ def commit(self): predicted = self.results.predicted[self.selected_learner[0]] selected = [i for i, t in enumerate(zip(actual, predicted)) if t in indices] - row_indices = self.results.row_indices[selected] - - extra = [] - class_var = self.data.domain.class_var - metas = self.data.domain.metas - - if self.append_predictions: - predicted = numpy.array(predicted[selected], dtype=object) - extra.append(predicted.reshape(-1, 1)) - var = Orange.data.DiscreteVariable( - "{}({})".format(class_var.name, learner_name), - class_var.values + if selected: + row_indices = self.results.row_indices[selected] + extra = [] + class_var = self.data.domain.class_var + metas = self.data.domain.metas + + if self.append_predictions: + predicted = numpy.array(predicted[selected], dtype=object) + extra.append(predicted.reshape(-1, 1)) + var = Orange.data.DiscreteVariable( + "{}({})".format(class_var.name, learner_name), + class_var.values + ) + metas = metas + (var,) + + if self.append_probabilities and \ + self.results.probabilities is not None: + probs = self.results.probabilities[self.selected_learner[0], + selected] + extra.append(numpy.array(probs, dtype=object)) + pvars = [Orange.data.ContinuousVariable("p({})".format(value)) + for value in class_var.values] + metas = metas + tuple(pvars) + + X = self.data.X[row_indices] + Y = self.data.Y[row_indices] + M = self.data.metas[row_indices] + row_ids = self.data.ids[row_indices] + + M = numpy.hstack((M,) + tuple(extra)) + domain = Orange.data.Domain( + self.data.domain.attributes, + self.data.domain.class_vars, + metas ) - metas = metas + (var,) - - if self.append_probabilities and \ - self.results.probabilities is not None: - probs = self.results.probabilities[self.selected_learner[0], - selected] - extra.append(numpy.array(probs, dtype=object)) - pvars = [Orange.data.ContinuousVariable("p({})".format(value)) - for value in class_var.values] - metas = metas + tuple(pvars) - - X = self.data.X[row_indices] - Y = self.data.Y[row_indices] - M = self.data.metas[row_indices] - row_ids = self.data.ids[row_indices] - - M = numpy.hstack((M,) + tuple(extra)) - domain = Orange.data.Domain( - self.data.domain.attributes, - self.data.domain.class_vars, - metas - ) - data = Orange.data.Table.from_numpy(domain, X, Y, M) - data.ids = row_ids - data.name = learner_name + data = Orange.data.Table.from_numpy(domain, X, Y, M) + data.ids = row_ids + data.name = learner_name + else: + data = None else: data = None diff --git a/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py b/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py index cfe0c79fea4..6f9b025a565 100644 --- a/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py +++ b/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py @@ -39,3 +39,19 @@ def test_selected_learner(self): self.widget.set_results(self.results_1_iris) self.widget.selected_learner[:] = [0] + def test_outputs(self): + self.send_signal("Evaluation Results", self.results_1_iris) + + # check selected data output + self.assertIsNone(self.get_output("Selected Data")) + + # select data instances + self.widget.select_correct() + + # check selected data output + selected = self.get_output("Selected Data") + self.assertGreater(len(selected), 0) + + # check output when data is removed + self.send_signal("Evaluation Results", None) + self.assertIsNone(self.get_output("Selected Data")) From 3285d5d6902e64e6e318c1fec8f821cb5236dde5 Mon Sep 17 00:00:00 2001 From: Vesna Tanko Date: Wed, 12 Oct 2016 10:23:32 +0200 Subject: [PATCH 2/2] TestOWConfusionMatrix: Unittest refactor --- .../evaluate/tests/test_owconfusionmatrix.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py b/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py index 6f9b025a565..ee03d0e9448 100644 --- a/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py +++ b/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py @@ -6,7 +6,8 @@ from Orange.widgets.evaluate.owconfusionmatrix import OWConfusionMatrix from Orange.widgets.tests.base import WidgetTest -class TestOWClassificationTree(WidgetTest): + +class TestOWConfusionMatrix(WidgetTest): @classmethod def setUpClass(cls): super().setUpClass() @@ -28,15 +29,15 @@ def test_selected_learner(self): """Check learner and model for various values of all parameters when pruning parameters are not checked """ - self.widget.set_results(self.results_2_iris) + self.send_signal("Evaluation Results", self.results_2_iris) self.assertEqual(self.widget.selected_learner, [0]) self.widget.selected_learner[:] = [1] - self.widget.set_results(self.results_2_titanic) + self.send_signal("Evaluation Results", self.results_2_titanic) self.widget.selected_learner[:] = [1] - self.widget.set_results(self.results_1_iris) + self.send_signal("Evaluation Results", self.results_1_iris) self.widget.selected_learner[:] = [0] - self.widget.set_results(None) - self.widget.set_results(self.results_1_iris) + self.send_signal("Evaluation Results", None) + self.send_signal("Evaluation Results", self.results_1_iris) self.widget.selected_learner[:] = [0] def test_outputs(self):