diff --git a/Orange/preprocess/preprocess.py b/Orange/preprocess/preprocess.py index a272739c751..4e5eb798cc8 100644 --- a/Orange/preprocess/preprocess.py +++ b/Orange/preprocess/preprocess.py @@ -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. diff --git a/Orange/widgets/data/tests/test_owtransform.py b/Orange/widgets/data/tests/test_owtransform.py index c84cc504254..8b2ec1f0fed 100644 --- a/Orange/widgets/data/tests/test_owtransform.py +++ b/Orange/widgets/data/tests/test_owtransform.py @@ -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): @@ -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() diff --git a/Orange/widgets/unsupervised/owpca.py b/Orange/widgets/unsupervised/owpca.py index 434c05398ce..4656e481e3e 100644 --- a/Orange/widgets/unsupervised/owpca.py +++ b/Orange/widgets/unsupervised/owpca.py @@ -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 @@ -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() @@ -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: @@ -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. @@ -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: diff --git a/Orange/widgets/unsupervised/tests/test_owpca.py b/Orange/widgets/unsupervised/tests/test_owpca.py index adea6b808c6..6bf4a050e04 100644 --- a/Orange/widgets/unsupervised/tests/test_owpca.py +++ b/Orange/widgets/unsupervised/tests/test_owpca.py @@ -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 @@ -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])