-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3346 from VesnaT/owtransform
[ENH] Transform: Add new widget
- Loading branch information
Showing
6 changed files
with
252 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
from Orange.data import Table | ||
from Orange.preprocess.preprocess import Preprocess, Discretize | ||
from Orange.widgets import gui | ||
from Orange.widgets.utils.sql import check_sql_input | ||
from Orange.widgets.widget import OWWidget, Input, Output, Msg | ||
|
||
|
||
class OWTransform(OWWidget): | ||
name = "Transform" | ||
description = "Transform data table." | ||
icon = "icons/Transform.svg" | ||
priority = 2110 | ||
keywords = [] | ||
|
||
class Inputs: | ||
data = Input("Data", Table, default=True) | ||
preprocessor = Input("Preprocessor", Preprocess) | ||
|
||
class Outputs: | ||
transformed_data = Output("Transformed Data", Table) | ||
|
||
class Error(OWWidget.Error): | ||
pp_error = Msg("An error occurred while transforming data.\n{}") | ||
|
||
resizing_enabled = False | ||
want_main_area = False | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.data = None | ||
self.preprocessor = None | ||
self.transformed_data = None | ||
|
||
info_box = gui.widgetBox(self.controlArea, "Info") | ||
self.input_label = gui.widgetLabel(info_box, "") | ||
self.preprocessor_label = gui.widgetLabel(info_box, "") | ||
self.output_label = gui.widgetLabel(info_box, "") | ||
self.set_input_label_text() | ||
self.set_preprocessor_label_text() | ||
|
||
def set_input_label_text(self): | ||
text = "No data on input." | ||
if self.data is not None: | ||
text = "Input data with {:,} instances and {:,} features.".format( | ||
len(self.data), | ||
len(self.data.domain.attributes)) | ||
self.input_label.setText(text) | ||
|
||
def set_preprocessor_label_text(self): | ||
text = "No preprocessor on input." | ||
if self.transformed_data is not None: | ||
text = "Preprocessor {} applied.".format(self.preprocessor) | ||
elif self.preprocessor is not None: | ||
text = "Preprocessor {} on input.".format(self.preprocessor) | ||
self.preprocessor_label.setText(text) | ||
|
||
def set_output_label_text(self): | ||
text = "" | ||
if self.transformed_data: | ||
text = "Output data includes {:,} features.".format( | ||
len(self.transformed_data.domain.attributes)) | ||
self.output_label.setText(text) | ||
|
||
@Inputs.data | ||
@check_sql_input | ||
def set_data(self, data): | ||
self.data = data | ||
self.set_input_label_text() | ||
|
||
@Inputs.preprocessor | ||
def set_preprocessor(self, preprocessor): | ||
self.preprocessor = preprocessor | ||
|
||
def handleNewSignals(self): | ||
self.apply() | ||
|
||
def apply(self): | ||
self.clear_messages() | ||
self.transformed_data = None | ||
if self.data is not None and self.preprocessor is not None: | ||
try: | ||
self.transformed_data = self.preprocessor(self.data) | ||
except Exception as ex: # pylint: disable=broad-except | ||
self.Error.pp_error(ex) | ||
self.Outputs.transformed_data.send(self.transformed_data) | ||
|
||
self.set_preprocessor_label_text() | ||
self.set_output_label_text() | ||
|
||
def send_report(self): | ||
if self.preprocessor is not None: | ||
self.report_items("Settings", | ||
(("Preprocessor", self.preprocessor),)) | ||
if self.data is not None: | ||
self.report_data("Data", self.data) | ||
if self.transformed_data is not None: | ||
self.report_data("Transformed data", self.transformed_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
from AnyQt.QtWidgets import QApplication | ||
|
||
app = QApplication([]) | ||
ow = OWTransform() | ||
d = Table("iris") | ||
pp = Discretize() | ||
ow.set_data(d) | ||
ow.set_preprocessor(pp) | ||
ow.handleNewSignals() | ||
ow.show() | ||
app.exec_() | ||
ow.saveSettings() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# Test methods with long descriptive names can omit docstrings | ||
# 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): | ||
def setUp(self): | ||
self.widget = self.create_widget(OWTransform) | ||
self.data = Table("iris") | ||
self.preprocessor = Discretize() | ||
|
||
def test_output(self): | ||
# send data and preprocessor | ||
self.send_signal(self.widget.Inputs.data, self.data) | ||
self.send_signal(self.widget.Inputs.preprocessor, self.preprocessor) | ||
output = self.get_output(self.widget.Outputs.transformed_data) | ||
self.assertIsInstance(output, Table) | ||
self.assertEqual("Input data with 150 instances and 4 features.", | ||
self.widget.input_label.text()) | ||
self.assertEqual("Preprocessor Discretize() applied.", | ||
self.widget.preprocessor_label.text()) | ||
self.assertEqual("Output data includes 4 features.", | ||
self.widget.output_label.text()) | ||
|
||
# remove preprocessor | ||
self.send_signal(self.widget.Inputs.preprocessor, None) | ||
output = self.get_output(self.widget.Outputs.transformed_data) | ||
self.assertIsNone(output) | ||
self.assertEqual("Input data with 150 instances and 4 features.", | ||
self.widget.input_label.text()) | ||
self.assertEqual("No preprocessor on input.", self.widget.preprocessor_label.text()) | ||
self.assertEqual("", self.widget.output_label.text()) | ||
|
||
# send preprocessor | ||
self.send_signal(self.widget.Inputs.preprocessor, self.preprocessor) | ||
output = self.get_output(self.widget.Outputs.transformed_data) | ||
self.assertIsInstance(output, Table) | ||
self.assertEqual("Input data with 150 instances and 4 features.", | ||
self.widget.input_label.text()) | ||
self.assertEqual("Preprocessor Discretize() applied.", | ||
self.widget.preprocessor_label.text()) | ||
self.assertEqual("Output data includes 4 features.", | ||
self.widget.output_label.text()) | ||
|
||
# remove data | ||
self.send_signal(self.widget.Inputs.data, None) | ||
output = self.get_output(self.widget.Outputs.transformed_data) | ||
self.assertIsNone(output) | ||
self.assertEqual("No data on input.", self.widget.input_label.text()) | ||
self.assertEqual("Preprocessor Discretize() on input.", | ||
self.widget.preprocessor_label.text()) | ||
self.assertEqual("", self.widget.output_label.text()) | ||
|
||
# remove preprocessor | ||
self.send_signal(self.widget.Inputs.preprocessor, None) | ||
self.assertEqual("No data on input.", self.widget.input_label.text()) | ||
self.assertEqual("No preprocessor on input.", | ||
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_error_transforming(self): | ||
self.send_signal(self.widget.Inputs.data, self.data) | ||
self.send_signal(self.widget.Inputs.preprocessor, Preprocess()) | ||
self.assertTrue(self.widget.Error.pp_error.is_shown()) | ||
output = self.get_output(self.widget.Outputs.transformed_data) | ||
self.assertIsNone(output) | ||
self.send_signal(self.widget.Inputs.data, None) | ||
self.assertFalse(self.widget.Error.pp_error.is_shown()) | ||
|
||
def test_send_report(self): | ||
self.send_signal(self.widget.Inputs.data, self.data) | ||
self.widget.report_button.click() | ||
self.send_signal(self.widget.Inputs.data, None) | ||
self.widget.report_button.click() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters