From 9aa3e34184360c671159cb1ee4bae3f3c712a063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Erjavec?= Date: Wed, 16 Oct 2024 10:20:34 +0200 Subject: [PATCH] owcalibratedlearner: Never return base_learner instance from create_learner The instance's name is modified in place after. Replace IdentityWrapper (which did not work) with copy.deepcopy of the instance. --- Orange/widgets/model/owcalibratedlearner.py | 12 ++++++------ .../widgets/model/tests/test_owcalibratedlearner.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Orange/widgets/model/owcalibratedlearner.py b/Orange/widgets/model/owcalibratedlearner.py index 7b4009573e9..549d5b2bb2d 100644 --- a/Orange/widgets/model/owcalibratedlearner.py +++ b/Orange/widgets/model/owcalibratedlearner.py @@ -1,3 +1,5 @@ +import copy + from Orange.classification import CalibratedLearner, ThresholdLearner, \ NaiveBayesLearner from Orange.data import Table @@ -65,7 +67,6 @@ def set_learner(self, learner): self.learner = self.model = None def _set_default_name(self): - if self.base_learner is None: self.set_default_learner_name("") else: @@ -80,10 +81,6 @@ def calibration_options_changed(self): self.apply() def create_learner(self): - class IdentityWrapper(Learner): - def fit_storage(self, data): - return self.base_learner.fit_storage(data) - if self.base_learner is None: return None learner = self.base_learner @@ -95,8 +92,11 @@ def fit_storage(self, data): self.ThresholdMap[self.threshold]) if self.preprocessors: if learner is self.base_learner: - learner = IdentityWrapper() + learner = copy.deepcopy(learner) learner.preprocessors = (self.preprocessors, ) + if learner is self.base_learner: + learner = copy.deepcopy(learner) + assert learner is not self.base_learner return learner def get_learner_parameters(self): diff --git a/Orange/widgets/model/tests/test_owcalibratedlearner.py b/Orange/widgets/model/tests/test_owcalibratedlearner.py index 27dcf378bc6..2058d73db6f 100644 --- a/Orange/widgets/model/tests/test_owcalibratedlearner.py +++ b/Orange/widgets/model/tests/test_owcalibratedlearner.py @@ -95,7 +95,7 @@ def test_create_learner(self): widget.calibration = widget.NoCalibration widget.threshold = widget.NoThresholdOptimization learner = self.widget.create_learner() - self.assertIs(learner, self.widget.base_learner) + self.assertIsNot(learner, self.widget.base_learner) widget.calibration = widget.SigmoidCalibration widget.threshold = widget.OptimizeF1