diff --git a/Orange/widgets/evaluate/owconfusionmatrix.py b/Orange/widgets/evaluate/owconfusionmatrix.py index 47a33316e3f..d23592df1a1 100644 --- a/Orange/widgets/evaluate/owconfusionmatrix.py +++ b/Orange/widgets/evaluate/owconfusionmatrix.py @@ -86,7 +86,7 @@ class OWConfusionMatrix(widget.OWWidget): settingsHandler = settings.ClassValuesContextHandler() - selected_learner = settings.Setting(0) + selected_learner = settings.Setting([0], schema_only=True) selection = settings.ContextSetting(set()) selected_quantity = settings.Setting(0) append_predictions = settings.Setting(True) @@ -101,8 +101,6 @@ class OWConfusionMatrix(widget.OWWidget): def __init__(self): super().__init__() - if isinstance(self.selected_learner, list): - self.selected_learner = (self.selected_learner + [0])[0] self.data = None self.results = None @@ -214,7 +212,7 @@ def _init_table(self, nclasses): def set_results(self, results): """Set the input results.""" - prev_sel_learner = self.selected_learner + prev_sel_learner = self.selected_learner.copy() self.clear() self.warning() self.closeContext() @@ -252,11 +250,10 @@ def set_results(self, results): self._init_table(len(class_values)) self.openContext(data.domain.class_var) - if prev_sel_learner is None or \ - prev_sel_learner >= len(self.learners): - self.selected_learner = 0 + if not prev_sel_learner or prev_sel_learner[0] >= len(self.learners): + self.selected_learner[:] = [0] else: - self.selected_learner = prev_sel_learner + self.selected_learner[:] = prev_sel_learner self._update() self._set_selection() self.unconditional_commit() @@ -319,12 +316,12 @@ def cell_clicked(self, model_index): def commit(self): """Output data instances corresponding to selected cells""" if self.results is not None and self.data is not None \ - and self.selected_learner is not None: + and self.selected_learner: indices = self.tableview.selectedIndexes() indices = {(ind.row() - 2, ind.column() - 2) for ind in indices} actual = self.results.actual - learner_name = self.learners[self.selected_learner] - predicted = self.results.predicted[self.selected_learner] + learner_name = self.learners[self.selected_learner[0]] + 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] @@ -344,7 +341,7 @@ def commit(self): if self.append_probabilities and \ self.results.probabilities is not None: - probs = self.results.probabilities[self.selected_learner, + probs = self.results.probabilities[self.selected_learner[0], selected] extra.append(numpy.array(probs, dtype=object)) pvars = [Orange.data.ContinuousVariable("p({})".format(value)) @@ -395,8 +392,8 @@ def _isinvalid(x): return isnan(x) or isinf(x) # Update the displayed confusion matrix - if self.results is not None and self.selected_learner is not None: - cmatrix = confusion_matrix(self.results, self.selected_learner) + if self.results is not None and self.selected_learner: + cmatrix = confusion_matrix(self.results, self.selected_learner[0]) colsum = cmatrix.sum(axis=0) rowsum = cmatrix.sum(axis=1) n = len(cmatrix) @@ -459,10 +456,10 @@ def _sum_item(value, border=""): def send_report(self): """Send report""" - if self.results is not None and self.selected_learner is not None: + if self.results is not None and self.selected_learner: self.report_table( "Confusion matrix for {} (showing {})". - format(self.learners[self.selected_learner], + format(self.learners[self.selected_learner[0]], self.quantities[self.selected_quantity].lower()), self.tableview) diff --git a/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py b/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py new file mode 100644 index 00000000000..cfe0c79fea4 --- /dev/null +++ b/Orange/widgets/evaluate/tests/test_owconfusionmatrix.py @@ -0,0 +1,41 @@ +# pylint: disable=missing-docstring + +from Orange.data import Table +from Orange.classification import NaiveBayesLearner, TreeLearner +from Orange.evaluation.testing import CrossValidation +from Orange.widgets.evaluate.owconfusionmatrix import OWConfusionMatrix +from Orange.widgets.tests.base import WidgetTest + +class TestOWClassificationTree(WidgetTest): + @classmethod + def setUpClass(cls): + super().setUpClass() + bayes = NaiveBayesLearner() + tree = TreeLearner() + iris = Table("iris") + titanic = Table("titanic") + common = dict(k=3, store_data=True) + cls.results_1_iris = CrossValidation(iris, [bayes], **common) + cls.results_2_iris = CrossValidation(iris, [bayes, tree], **common) + cls.results_2_titanic = CrossValidation(titanic, [bayes, tree], + **common) + + def setUp(self): + self.widget = self.create_widget(OWConfusionMatrix, + stored_settings={"auto_apply": False}) + + 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.assertEqual(self.widget.selected_learner, [0]) + self.widget.selected_learner[:] = [1] + self.widget.set_results(self.results_2_titanic) + self.widget.selected_learner[:] = [1] + self.widget.set_results(self.results_1_iris) + self.widget.selected_learner[:] = [0] + self.widget.set_results(None) + self.widget.set_results(self.results_1_iris) + self.widget.selected_learner[:] = [0] + diff --git a/Orange/widgets/gui.py b/Orange/widgets/gui.py index 381d4edfbf0..1afbcbbdbad 100644 --- a/Orange/widgets/gui.py +++ b/Orange/widgets/gui.py @@ -2253,7 +2253,7 @@ class ControlledList(list): selection in the list box. """ def __init__(self, content, listBox=None): - super().__init__(content) + super().__init__(content if content is not None else []) self.listBox = listBox def __reduce__(self):