Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Preprocess Widget: Continuize type error #1978

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Orange/widgets/data/owpreprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ def __repr__(self):


class ContinuizeEditor(BaseEditor):
_Type = type(Continuize.FirstAsBase)

Continuizers = OrderedDict({
Continuize.FrequentAsBase: "Most frequent is base",
Continuize.Indicators: "One attribute per value",
Expand All @@ -206,7 +208,6 @@ class ContinuizeEditor(BaseEditor):
def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)
self.setLayout(QVBoxLayout())

self.__treatment = Continuize.Indicators
self.__group = group = QButtonGroup(exclusive=True)
group.buttonClicked.connect(self.__on_buttonClicked)
Expand All @@ -215,13 +216,15 @@ def __init__(self, parent=None, **kwargs):
rb = QRadioButton(
text=text,
checked=self.__treatment == treatment)
group.addButton(rb, int(treatment))
group.addButton(rb, _enum_to_index(ContinuizeEditor._Type,
treatment))
self.layout().addWidget(rb)

self.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)

def setTreatment(self, treatment):
b = self.__group.button(treatment)
buttonid = _enum_to_index(ContinuizeEditor._Type, treatment)
b = self.__group.button(buttonid)
if b is not None:
b.setChecked(True)
self.__treatment = treatment
Expand All @@ -238,7 +241,8 @@ def parameters(self):
return {"multinomial_treatment": self.__treatment}

def __on_buttonClicked(self):
self.__treatment = self.__group.checkedId()
self.__treatment = _index_to_enum(
ContinuizeEditor._Type, self.__group.checkedId())
self.changed.emit()
self.edited.emit()

Expand Down
133 changes: 131 additions & 2 deletions Orange/widgets/data/tests/test_owpreprocess.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Test methods with long descriptive names can omit docstrings
# pylint: disable=missing-docstring

import numpy as np

from Orange.data import Table
from Orange.preprocess import Randomize, Scale
from Orange.preprocess import (
Randomize, Scale, Discretize, Continuize, Impute, ProjectPCA, ProjectCUR
)
from Orange.preprocess import discretize, impute, fss, score
from Orange.widgets.data import owpreprocess
from Orange.widgets.data.owpreprocess import OWPreprocess
from Orange.widgets.tests.base import WidgetTest

Expand Down Expand Up @@ -40,3 +43,129 @@ def test_normalize(self):

np.testing.assert_allclose(output.X.mean(0), 0, atol=1e-7)
np.testing.assert_allclose(output.X.std(0), 1, atol=1e-7)


# Test for editors
class TestDiscretizeEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.DiscretizeEditor()
self.assertEqual(widget.parameters(),
{"method": owpreprocess.DiscretizeEditor.EqualFreq,
"n": 4})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Discretize)
self.assertIsInstance(p.method, discretize.EqualFreq)
widget.setParameters(
{"method": owpreprocess.DiscretizeEditor.EntropyMDL}
)
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Discretize)
self.assertIsInstance(p.method, discretize.EntropyMDL)

widget.setParameters(
{"method": owpreprocess.DiscretizeEditor.EqualWidth,
"n": 10}
)
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Discretize)
self.assertIsInstance(p.method, discretize.EqualWidth)
self.assertEqual(p.method.n, 10)


class TestContinuizeEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.ContinuizeEditor()
self.assertEqual(widget.parameters(),
{"multinomial_treatment": Continuize.Indicators})

p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Continuize)
self.assertEqual(p.multinomial_treatment, Continuize.Indicators)

widget.setParameters(
{"multinomial_treatment": Continuize.FrequentAsBase})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Continuize)
self.assertEqual(p.multinomial_treatment, Continuize.FrequentAsBase)


class TestImputeEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.ImputeEditor()
self.assertEqual(widget.parameters(),
{"method": owpreprocess.ImputeEditor.Average})
widget.setParameters(
{"method": owpreprocess.ImputeEditor.Average}
)
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Impute)
self.assertIsInstance(p.method, impute.Average)


class TestFeatureSelectEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.FeatureSelectEditor()
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, fss.SelectBestFeatures)
self.assertEqual(p.method, score.InfoGain)
self.assertEqual(p.k, 10)


class TestRandomFeatureSelectEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.RandomFeatureSelectEditor()
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, fss.SelectRandomFeatures)
self.assertEqual(p.k, 10)

widget.setParameters(
{"strategy": owpreprocess.RandomFeatureSelectEditor.Percentage,
"p": 25})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, fss.SelectRandomFeatures)
self.assertEqual(p.k, 0.25)


class TestRandomizeEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.Randomize()
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Randomize)
self.assertEqual(p.rand_type, Randomize.RandomizeClasses)

widget.setParameters({"rand_type": Randomize.RandomizeAttributes})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, Randomize)
self.assertEqual(p.rand_type, Randomize.RandomizeAttributes)


class TestPCAEditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.PCA()
self.assertEqual(widget.parameters(),
{"n_components": 10})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, ProjectPCA)
self.assertEqual(p.n_components, 10)

widget.setParameters({"n_components": 5})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, ProjectPCA)
self.assertEqual(p.n_components, 5)


class TestCUREditor(WidgetTest):
def test_editor(self):
widget = owpreprocess.CUR()
self.assertEqual(widget.parameters(),
{"rank": 10, "max_error": 1})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, ProjectCUR)
self.assertEqual(p.rank, 10)
self.assertEqual(p.max_error, 1)

widget.setParameters({"rank": 5, "max_error": 0.5})
p = widget.createinstance(widget.parameters())
self.assertIsInstance(p, ProjectCUR)
self.assertEqual(p.rank, 5)
self.assertEqual(p.max_error, 0.5)