Skip to content

Commit

Permalink
OWPCA: Output ApplyDomain preprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Oct 30, 2018
1 parent 62bd4b7 commit a31d35b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Orange/preprocess/preprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,18 @@ def transform(var):
return data.transform(domain)


class ApplyDomain(Preprocess):
def __init__(self, domain, name):
self._domain = domain
self._name = name

def __call__(self, data):
return data.transform(self._domain)

def __str__(self):
return self._name


class PreprocessorList(Preprocess):
"""
Store a list of preprocessors and on call apply them to the dataset.
Expand Down
15 changes: 15 additions & 0 deletions Orange/widgets/data/tests/test_owtransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# pylint: disable=missing-docstring
from Orange.data import Table
from Orange.preprocess import Discretize
from Orange.preprocess.preprocess import Preprocess
from Orange.widgets.data.owtransform import OWTransform
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.unsupervised.owpca import OWPCA


class TestOWTransform(WidgetTest):
Expand Down Expand Up @@ -61,6 +63,19 @@ def test_output(self):
self.widget.preprocessor_label.text())
self.assertEqual("", self.widget.output_label.text())

def test_input_pca_preprocessor(self):
owpca = self.create_widget(OWPCA)
self.send_signal(owpca.Inputs.data, self.data, widget=owpca)
owpca.components_spin.setValue(2)
pp = self.get_output(owpca.Outputs.preprocessor, widget=owpca)
self.assertIsNotNone(pp, Preprocess)

self.send_signal(self.widget.Inputs.data, self.data)
self.send_signal(self.widget.Inputs.preprocessor, pp)
output = self.get_output(self.widget.Outputs.transformed_data)
self.assertIsInstance(output, Table)
self.assertEqual(output.X.shape, (len(self.data), 2))

def test_send_report(self):
self.send_signal(self.widget.Inputs.data, self.data)
self.widget.report_button.click()
Expand Down
8 changes: 7 additions & 1 deletion Orange/widgets/unsupervised/owpca.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from Orange.data import Table, Domain, StringVariable, ContinuousVariable
from Orange.data.sql.table import SqlTable, AUTO_DL_LIMIT
from Orange.preprocess import Normalize
from Orange.preprocess.preprocess import Preprocess, ApplyDomain
from Orange.projection import PCA, TruncatedSVD
from Orange.widgets import widget, gui, settings
from Orange.widgets.widget import Input, Output
Expand Down Expand Up @@ -44,6 +45,7 @@ class Outputs:
transformed_data = Output("Transformed data", Table)
components = Output("Components", Table)
pca = Output("PCA", PCA, dynamic=False)
preprocessor = Output("Preprocessor", Preprocess)

settingsHandler = settings.DomainContextHandler()

Expand Down Expand Up @@ -290,6 +292,7 @@ def clear_outputs(self):
self.Outputs.transformed_data.send(None)
self.Outputs.components.send(None)
self.Outputs.pca.send(self._pca_projector)
self.Outputs.preprocessor.send(None)

def get_model(self):
if self.rpca is None:
Expand Down Expand Up @@ -455,7 +458,7 @@ def _update_axis(self):
axis.setTicks([[(i, str(i+1)) for i in range(0, p, d)]])

def commit(self):
transformed = components = None
transformed = components = pp = None
if self._pca is not None:
if self._transformed is None:
# Compute the full transform (MAX_COMPONENTS components) only once.
Expand All @@ -479,10 +482,13 @@ def commit(self):
metas=metas)
components.name = 'components'

pp = ApplyDomain(domain, "PCA")

self._pca_projector.component = self.ncomponents
self.Outputs.transformed_data.send(transformed)
self.Outputs.components.send(components)
self.Outputs.pca.send(self._pca_projector)
self.Outputs.preprocessor.send(pp)

def send_report(self):
if self.data is None:
Expand Down
14 changes: 14 additions & 0 deletions Orange/widgets/unsupervised/tests/test_owpca.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import scipy.sparse as sp

from Orange.data import Table, Domain, ContinuousVariable, TimeVariable
from Orange.preprocess.preprocess import Preprocess
from Orange.widgets.tests.base import WidgetTest
from Orange.widgets.unsupervised.owpca import OWPCA, DECOMPOSITIONS

Expand Down Expand Up @@ -131,3 +132,16 @@ def test_do_not_mask_features(self):
self.widget.set_data(data)
ndata = Table("iris.tab")
self.assertEqual(data.domain[0], ndata.domain[0])

def test_output_preprocessor(self):
data = Table("iris")
self.send_signal(self.widget.Inputs.data, data)
pp = self.get_output(self.widget.Outputs.preprocessor)
self.assertIsInstance(pp, Preprocess)
transformed_data = pp(data[::10])
self.assertIsInstance(transformed_data, Table)
self.assertEqual(transformed_data.X.shape, (15, 2))
output = self.get_output(self.widget.Outputs.transformed_data)
np.testing.assert_array_equal(transformed_data.X, output.X[::10])
self.assertEqual([a.name for a in transformed_data.domain.attributes],
[m.name for m in output.domain.attributes])

0 comments on commit a31d35b

Please sign in to comment.