From 0d0cb534de7a4539567dab0cf19dc1e4d5426142 Mon Sep 17 00:00:00 2001 From: janezd Date: Sat, 19 Nov 2022 18:11:01 +0100 Subject: [PATCH] Preprocess: Add missing __repr__methods --- Orange/widgets/data/owpreprocess.py | 24 +++++++++++++++---- .../widgets/data/tests/test_owpreprocess.py | 20 +++++++++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Orange/widgets/data/owpreprocess.py b/Orange/widgets/data/owpreprocess.py index 1c84bb68d68..828b7805265 100644 --- a/Orange/widgets/data/owpreprocess.py +++ b/Orange/widgets/data/owpreprocess.py @@ -27,6 +27,7 @@ from Orange.preprocess import Continuize, ProjectPCA, RemoveNaNRows, \ ProjectCUR, Scale as _Scale, Randomize as _Randomize, RemoveSparse from Orange.widgets import widget, gui +from Orange.widgets.utils.localization import pl from Orange.widgets.settings import Setting from Orange.widgets.utils.overlay import OverlayWidget from Orange.widgets.utils.sql import check_sql_input @@ -253,9 +254,9 @@ def createinstance(params): def __repr__(self): return self.Continuizers[self.__treatment] -class RemoveSparseEditor(BaseEditor): - options = ["missing", "zeros"] +class RemoveSparseEditor(BaseEditor): + options = ["missing values", "zeros"] def __init__(self, parent=None, **kwargs): super().__init__(parent, **kwargs) @@ -266,11 +267,9 @@ def __init__(self, parent=None, **kwargs): self.setLayout(QVBoxLayout()) self.layout().addWidget(QLabel("Remove features with too many")) - options = ["missing values", - "zeros"] self.filter_buttons = QButtonGroup(exclusive=True) self.filter_buttons.buttonClicked.connect(self.filterByClicked) - for idx, option, in enumerate(options): + for idx, option, in enumerate(self.options): btn = QRadioButton(self, text=option, checked=idx == 0) self.filter_buttons.addButton(btn, id=idx) self.layout().addWidget(btn) @@ -354,6 +353,15 @@ def createinstance(params): threshold = params.pop('percThresh', 5) / 100 return RemoveSparse(threshold, filter0) + def __repr__(self): + desc = f"remove features with too many {self.options[self.filter0]}, threshold: " + if self.useFixedThreshold: + desc += f"{self.fixedThresh} {pl(self.fixedThresh, 'instance')}" + else: + desc += f"{self.percThresh} %" + return desc + + class ImputeEditor(BaseEditor): (NoImputation, Constant, Average, Model, Random, DropRows, DropColumns) = 0, 1, 2, 3, 4, 5, 6 @@ -727,6 +735,12 @@ def createinstance(params): # further implementations raise NotImplementedError + def __repr__(self): + if self.__strategy == self.Fixed: + return f"select {self.__k} {pl(self.__k,'feature')}" + else: + return f"select {self.__p} % features" + def index_to_enum(enum, i): """Enums, by default, are not int-comparable, so use an ad-hoc mapping of diff --git a/Orange/widgets/data/tests/test_owpreprocess.py b/Orange/widgets/data/tests/test_owpreprocess.py index 8499f24ba6a..9d2f13f4486 100644 --- a/Orange/widgets/data/tests/test_owpreprocess.py +++ b/Orange/widgets/data/tests/test_owpreprocess.py @@ -1,5 +1,7 @@ # Test methods with long descriptive names can omit docstrings # pylint: disable=missing-docstring,unsubscriptable-object +import unittest + import numpy as np from Orange.data import Table @@ -231,6 +233,13 @@ def test_editor(self): self.assertIsInstance(p, fss.SelectRandomFeatures) self.assertEqual(p.k, 0.25) + def test_repr(self): + widget = owpreprocess.RandomFeatureSelectEditor() + for strategy in (owpreprocess.RandomFeatureSelectEditor.Fixed, + owpreprocess.RandomFeatureSelectEditor.Percentage): + widget.setStrategy(strategy) + repr(widget) + class TestRandomizeEditor(WidgetTest): def test_editor(self): @@ -281,8 +290,8 @@ def test_editor(self): self.assertEqual(p.rank, 5) self.assertEqual(p.max_error, 0.5) -class TestRemoveSparseEditor(WidgetTest): +class TestRemoveSparseEditor(WidgetTest): def test_editor(self): widget = owpreprocess.RemoveSparseEditor() self.assertEqual( @@ -304,3 +313,12 @@ def test_editor(self): self.assertIsInstance(p, RemoveSparse) self.assertEqual(p.threshold, 30) self.assertFalse(p.filter0) + + def test_repr(self): + widget = owpreprocess.RemoveSparseEditor() + for widget.useFixedThreshold in (False, True): + repr(widget) + + +if __name__ == "__main__": + unittest.main()