Skip to content

Commit

Permalink
OWTranspose: Add a new widget
Browse files Browse the repository at this point in the history
  • Loading branch information
VesnaT committed Nov 10, 2016
1 parent 22892cc commit 8c2c6e9
Show file tree
Hide file tree
Showing 3 changed files with 418 additions and 0 deletions.
257 changes: 257 additions & 0 deletions Orange/widgets/data/icons/Transpose.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
117 changes: 117 additions & 0 deletions Orange/widgets/data/owtranspose.py
@@ -0,0 +1,117 @@
from itertools import chain

from AnyQt.QtCore import Qt

from Orange.data import Table, ContinuousVariable, DiscreteVariable
from Orange.widgets.settings import (Setting, ContextSetting,
PerfectDomainContextHandler)
from Orange.widgets.utils import itemmodels
from Orange.widgets.widget import OWWidget, Msg
from Orange.widgets import gui


class OWTranspose(OWWidget):
name = "Transpose"
description = "Transpose data table."
icon = "icons/Transpose.svg"
priority = 2000

inputs = [("Data", Table, "set_data")]
outputs = [("Data", Table)]

resizing_enabled = False
want_main_area = False

settingsHandler = PerfectDomainContextHandler(metas_in_res=True)
feature_names_column = ContextSetting("None")
class_variable_index = ContextSetting(0)
auto_apply = Setting(True)

class Error(OWWidget.Error):
value_error = Msg("{}")

def __init__(self):
super().__init__()
self.data = None

# GUI
options = dict(callback=self.apply, orientation=Qt.Horizontal,
labelWidth=100, contentsLength=12)
self.feature_model = itemmodels.VariableListModel()
self.feature_combo = gui.comboBox(
self.controlArea, self, "feature_names_column",
box="Create feature names from", sendSelectedValue=True, **options)
self.feature_combo.setModel(self.feature_model)

self.class_model = itemmodels.VariableListModel()
self.class_combo = gui.comboBox(
self.controlArea, self, "class_variable_index",
box="Class variable as", **options)
self.class_combo.setModel(self.class_model)

self.apply_button = gui.auto_commit(
self.controlArea, self, "auto_apply", "&Apply",
box=False, commit=self.apply)

def set_data(self, data):
self.closeContext()
self.data = data
self.update_controls()
self.openContext(data)
self.apply()

def update_controls(self):
self.feature_model[:] = ["None"]
self.class_model[:] = ["None"]
self.feature_names_column = "None"
if self.data:
self.feature_model[:] += list(self.data.domain.metas)
if self.data.domain.metas:
self.feature_names_column = self.data.domain.metas[0].name
names = chain.from_iterable(
list(a.attributes.keys()) for a in self.data.domain.attributes)
variables = chain.from_iterable(
(DiscreteVariable(name), ContinuousVariable(name))
for name in sorted(set(names)))
self.class_model[:] += list(variables)
self.class_variable_index = min(2, len(self.class_model[:])) - 1

def apply(self):
self.clear_messages()
transposed = None
if self.data:
options = dict()
if self.feature_names_column != "None":
options["feature_names_column"] = self.feature_names_column
class_var = self.class_model[self.class_variable_index]
if class_var != "None":
options["class_name"] = class_var.name
options["class_type"] = "d" if class_var.is_discrete else "c"

try:
transposed = Table.transpose(self.data, **options)
except ValueError as e:
self.Error.value_error(e)
self.send("Data", transposed)

def send_report(self):
class_var = self.class_model[self.class_variable_index] \
if len(self.class_model) else "None"
self.report_items(
"", [("Create feature names from", self.feature_names_column),
("Class variable as", class_var)])
if self.data:
self.report_data("Data", self.data)


if __name__ == "__main__":
from PyQt4.QtGui import QApplication

app = QApplication([])
ow = OWTranspose()
d = Table("zoo")
d.domain.attributes[0].attributes = {"key": "value"}
ow.set_data(d)
ow.show()
app.exec_()
ow.saveSettings()

0 comments on commit 8c2c6e9

Please sign in to comment.