From 32ec4bfb8d868a3825f63de638586d20f4c95f66 Mon Sep 17 00:00:00 2001 From: Lan Zagar Date: Fri, 21 Apr 2017 11:00:38 +0200 Subject: [PATCH] owtestlearners: Fix migration of old settings --- Orange/widgets/evaluate/owtestlearners.py | 9 ++++-- .../evaluate/tests/test_owtestlearners.py | 29 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Orange/widgets/evaluate/owtestlearners.py b/Orange/widgets/evaluate/owtestlearners.py index e45461c92ec..1425008c32a 100644 --- a/Orange/widgets/evaluate/owtestlearners.py +++ b/Orange/widgets/evaluate/owtestlearners.py @@ -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 @@ -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): diff --git a/Orange/widgets/evaluate/tests/test_owtestlearners.py b/Orange/widgets/evaluate/tests/test_owtestlearners.py index 203db08be03..6ae4905fbf3 100644 --- a/Orange/widgets/evaluate/tests/test_owtestlearners.py +++ b/Orange/widgets/evaluate/tests/test_owtestlearners.py @@ -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): @@ -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)