Skip to content

Commit

Permalink
OWConfusionMatrix: Output Flagged Data
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Oct 14, 2016
1 parent c8341a9 commit 7dc0295
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 55 deletions.
81 changes: 44 additions & 37 deletions Orange/widgets/evaluate/owconfusionmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import sklearn.metrics as skl_metrics

import Orange
from Orange.misc.flagged_data import create_flagged_table, FLAGGED_SIGNAL_NAME
from Orange.widgets import widget, settings, gui


Expand Down Expand Up @@ -78,7 +79,8 @@ class OWConfusionMatrix(widget.OWWidget):
priority = 1001

inputs = [("Evaluation Results", Orange.evaluation.Results, "set_results")]
outputs = [("Selected Data", Orange.data.Table)]
outputs = [("Selected Data", Orange.data.Table, widget.Default),
(FLAGGED_SIGNAL_NAME, Orange.data.Table)]

quantities = ["Number of instances",
"Proportion of predicted",
Expand Down Expand Up @@ -324,51 +326,56 @@ 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]

extra = []
class_var = self.data.domain.class_var
metas = self.data.domain.metas

if self.append_predictions:
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]]
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
Y = self.data.Y
M = self.data.metas
row_ids = self.data.ids

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

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
)
data = Orange.data.Table.from_numpy(domain, X, Y, M)
data.ids = row_ids
data.name = learner_name
flagged_data = create_flagged_table(data, row_indices)
data = data[row_indices]
else:
flagged_data = create_flagged_table(data, [])
data = None

else:
data = None
flagged_data = None

self.send("Selected Data", data)
self.send(FLAGGED_SIGNAL_NAME, flagged_data)

def _invalidate(self):
indices = self.tableview.selectedIndexes()
Expand Down
28 changes: 10 additions & 18 deletions Orange/widgets/evaluate/tests/test_owconfusionmatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@
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
from Orange.widgets.tests.base import WidgetTest, WidgetOutputsTestMixin


class TestOWConfusionMatrix(WidgetTest):
class TestOWConfusionMatrix(WidgetTest, WidgetOutputsTestMixin):
@classmethod
def setUpClass(cls):
super().setUpClass()
WidgetOutputsTestMixin.init(cls)

bayes = NaiveBayesLearner()
tree = TreeLearner()
iris = Table("iris")
iris = cls.data
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)

cls.signal_name = "Evaluation Results"
cls.signal_data = cls.results_1_iris
cls.same_input_output_domain = False

def setUp(self):
self.widget = self.create_widget(OWConfusionMatrix,
stored_settings={"auto_apply": False})
Expand All @@ -40,19 +46,5 @@ def test_selected_learner(self):
self.send_signal("Evaluation 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
def _select_data(self):
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"))

0 comments on commit 7dc0295

Please sign in to comment.