Skip to content

Commit

Permalink
Merge pull request #2254 from lanzagar/migrate
Browse files Browse the repository at this point in the history
[FIX] owtestlearners: Fix migration of old settings
  • Loading branch information
astaric authored May 3, 2017
2 parents 8228bcc + 32ec4bf commit 0155051
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
9 changes: 6 additions & 3 deletions Orange/widgets/evaluate/owtestlearners.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class OWTestLearners(OWWidget):
outputs = [("Predictions", Table),
("Evaluation Results", Results)]

settings_version = 2
settings_version = 3
settingsHandler = settings.PerfectDomainContextHandler(metas_in_res=True)

#: Resampling/testing types
Expand Down Expand Up @@ -750,10 +750,13 @@ def send_report(self):
@classmethod
def migrate_settings(cls, settings_, version):
if version < 2:
if not hasattr(settings_["context_settings"][0], "attributes"):
settings_["context_settings"][0].attributes = {}
if settings_["resampling"] > 0:
settings_["resampling"] += 1
if version < 3:
# Older version used an incompatible context handler
settings_["context_settings"] = [
c for c in settings_.get("context_settings", ())
if not hasattr(c, 'classes')]


def learner_name(learner):
Expand Down
29 changes: 18 additions & 11 deletions Orange/widgets/evaluate/tests/test_owtestlearners.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
# pylint: disable=missing-docstring
# pylint: disable=protected-access
import collections
import unittest

import collections
import numpy as np

from AnyQt.QtWidgets import QMenu
from AnyQt.QtCore import QPoint

from Orange.data import Table, Domain
from Orange.classification import MajorityLearner
from Orange.regression import MeanLearner
from Orange.modelling import ConstantLearner

from Orange.data import Table, Domain
from Orange.evaluation import Results, TestOnTestData
from Orange.modelling import ConstantLearner
from Orange.regression import MeanLearner
from Orange.widgets.evaluate.owtestlearners import (
OWTestLearners, results_one_vs_rest)
from Orange.widgets.settings import (
ClassValuesContextHandler, PerfectDomainContextHandler)
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.evaluate.owtestlearners import OWTestLearners
from Orange.widgets.evaluate import owtestlearners


class TestOWTestLearners(WidgetTest):
Expand Down Expand Up @@ -132,15 +132,22 @@ def execmenu(*_):
unittest.mock.patch("AnyQt.QtWidgets.QMenu.exec", execmenu):
w.show_column_chooser(QPoint(0, 0))

def test_migrate_removes_invalid_contexts(self):
context_invalid = ClassValuesContextHandler().new_context([0, 1, 2])
context_valid = PerfectDomainContextHandler().new_context(*[[]] * 4)
settings = {'context_settings': [context_invalid, context_valid]}
self.widget.migrate_settings(settings, 2)
self.assertEqual(settings['context_settings'], [context_valid])


class TestHelpers(unittest.TestCase):
def test_results_one_vs_rest(self):
data = Table("lenses")
learners = [MajorityLearner()]
res = TestOnTestData(data[1::2], data[::2], learners=learners)
r1 = owtestlearners.results_one_vs_rest(res, pos_index=0)
r2 = owtestlearners.results_one_vs_rest(res, pos_index=1)
r3 = owtestlearners.results_one_vs_rest(res, pos_index=2)
r1 = results_one_vs_rest(res, pos_index=0)
r2 = results_one_vs_rest(res, pos_index=1)
r3 = results_one_vs_rest(res, pos_index=2)

np.testing.assert_almost_equal(np.sum(r1.probabilities, axis=2), 1.0)
np.testing.assert_almost_equal(np.sum(r2.probabilities, axis=2), 1.0)
Expand Down

0 comments on commit 0155051

Please sign in to comment.