Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Confusion matrix: fix selected_learner setting #1523

Merged
merged 2 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions Orange/widgets/evaluate/owconfusionmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Empty file.
41 changes: 41 additions & 0 deletions Orange/widgets/evaluate/tests/test_owconfusionmatrix.py
Original file line number Diff line number Diff line change
@@ -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]

2 changes: 1 addition & 1 deletion Orange/widgets/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,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):
Expand Down