Skip to content

Commit

Permalink
Preprocess: Add missing __repr__methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Nov 19, 2022
1 parent 0d3e3ee commit 3bdc218
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
24 changes: 19 additions & 5 deletions Orange/widgets/data/owpreprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion Orange/widgets/data/tests/test_owpreprocess.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -281,8 +283,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(
Expand All @@ -304,3 +306,21 @@ 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)


class TestRandomFeatureSelectEditor(WidgetTest):
def test_repr(self):
widget = owpreprocess.RandomFeatureSelectEditor()
for strategy in (owpreprocess.RandomFeatureSelectEditor.Fixed,
owpreprocess.RandomFeatureSelectEditor.Percentage):
widget.setStrategy(strategy)
repr(widget)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3bdc218

Please sign in to comment.