From 4e7e541c4f9e711519dc2887046df403aefc3c4a 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, 6 insertions(+), 8 deletions(-) diff --git a/Orange/widgets/model/owcalibratedlearner.py b/Orange/widgets/model/owcalibratedlearner.py index 7b4009573e9..8723015cae0 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 @@ -93,10 +90,11 @@ def fit_storage(self, data): if self.threshold != self.NoThresholdOptimization: learner = ThresholdLearner(learner, self.ThresholdMap[self.threshold]) + if learner is self.base_learner: + learner = copy.deepcopy(learner) if self.preprocessors: - if learner is self.base_learner: - learner = IdentityWrapper() learner.preprocessors = (self.preprocessors, ) + 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