Skip to content

Commit

Permalink
OWTransform: Add new widget
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Oct 29, 2018
1 parent 7ee820f commit 62bd4b7
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Orange/widgets/data/icons/Transform.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
105 changes: 105 additions & 0 deletions Orange/widgets/data/owtransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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


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)

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.transformed_data = None
if self.data is not None and self.preprocessor is not None:
self.transformed_data = self.preprocessor(self.data)
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()
68 changes: 68 additions & 0 deletions Orange/widgets/data/tests/test_owtransform.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# 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.widgets.data.owtransform import OWTransform
from Orange.widgets.tests.base import WidgetTest


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_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()

0 comments on commit 62bd4b7

Please sign in to comment.