Skip to content

Commit

Permalink
owpreprocess: Add basic tests for preprocess editors
Browse files Browse the repository at this point in the history
  • Loading branch information
ales-erjavec committed Jan 30, 2017
1 parent c9f1cc3 commit 59d2d7f
Showing 1 changed file with 131 additions and 2 deletions.
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)

0 comments on commit 59d2d7f

Please sign in to comment.