Skip to content

Commit 8c2c6e9

Browse files
committed
OWTranspose: Add a new widget
1 parent 22892cc commit 8c2c6e9

File tree

3 files changed

+418
-0
lines changed

3 files changed

+418
-0
lines changed
Lines changed: 257 additions & 0 deletions
Loading

Orange/widgets/data/owtranspose.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from itertools import chain
2+
3+
from AnyQt.QtCore import Qt
4+
5+
from Orange.data import Table, ContinuousVariable, DiscreteVariable
6+
from Orange.widgets.settings import (Setting, ContextSetting,
7+
PerfectDomainContextHandler)
8+
from Orange.widgets.utils import itemmodels
9+
from Orange.widgets.widget import OWWidget, Msg
10+
from Orange.widgets import gui
11+
12+
13+
class OWTranspose(OWWidget):
14+
name = "Transpose"
15+
description = "Transpose data table."
16+
icon = "icons/Transpose.svg"
17+
priority = 2000
18+
19+
inputs = [("Data", Table, "set_data")]
20+
outputs = [("Data", Table)]
21+
22+
resizing_enabled = False
23+
want_main_area = False
24+
25+
settingsHandler = PerfectDomainContextHandler(metas_in_res=True)
26+
feature_names_column = ContextSetting("None")
27+
class_variable_index = ContextSetting(0)
28+
auto_apply = Setting(True)
29+
30+
class Error(OWWidget.Error):
31+
value_error = Msg("{}")
32+
33+
def __init__(self):
34+
super().__init__()
35+
self.data = None
36+
37+
# GUI
38+
options = dict(callback=self.apply, orientation=Qt.Horizontal,
39+
labelWidth=100, contentsLength=12)
40+
self.feature_model = itemmodels.VariableListModel()
41+
self.feature_combo = gui.comboBox(
42+
self.controlArea, self, "feature_names_column",
43+
box="Create feature names from", sendSelectedValue=True, **options)
44+
self.feature_combo.setModel(self.feature_model)
45+
46+
self.class_model = itemmodels.VariableListModel()
47+
self.class_combo = gui.comboBox(
48+
self.controlArea, self, "class_variable_index",
49+
box="Class variable as", **options)
50+
self.class_combo.setModel(self.class_model)
51+
52+
self.apply_button = gui.auto_commit(
53+
self.controlArea, self, "auto_apply", "&Apply",
54+
box=False, commit=self.apply)
55+
56+
def set_data(self, data):
57+
self.closeContext()
58+
self.data = data
59+
self.update_controls()
60+
self.openContext(data)
61+
self.apply()
62+
63+
def update_controls(self):
64+
self.feature_model[:] = ["None"]
65+
self.class_model[:] = ["None"]
66+
self.feature_names_column = "None"
67+
if self.data:
68+
self.feature_model[:] += list(self.data.domain.metas)
69+
if self.data.domain.metas:
70+
self.feature_names_column = self.data.domain.metas[0].name
71+
names = chain.from_iterable(
72+
list(a.attributes.keys()) for a in self.data.domain.attributes)
73+
variables = chain.from_iterable(
74+
(DiscreteVariable(name), ContinuousVariable(name))
75+
for name in sorted(set(names)))
76+
self.class_model[:] += list(variables)
77+
self.class_variable_index = min(2, len(self.class_model[:])) - 1
78+
79+
def apply(self):
80+
self.clear_messages()
81+
transposed = None
82+
if self.data:
83+
options = dict()
84+
if self.feature_names_column != "None":
85+
options["feature_names_column"] = self.feature_names_column
86+
class_var = self.class_model[self.class_variable_index]
87+
if class_var != "None":
88+
options["class_name"] = class_var.name
89+
options["class_type"] = "d" if class_var.is_discrete else "c"
90+
91+
try:
92+
transposed = Table.transpose(self.data, **options)
93+
except ValueError as e:
94+
self.Error.value_error(e)
95+
self.send("Data", transposed)
96+
97+
def send_report(self):
98+
class_var = self.class_model[self.class_variable_index] \
99+
if len(self.class_model) else "None"
100+
self.report_items(
101+
"", [("Create feature names from", self.feature_names_column),
102+
("Class variable as", class_var)])
103+
if self.data:
104+
self.report_data("Data", self.data)
105+
106+
107+
if __name__ == "__main__":
108+
from PyQt4.QtGui import QApplication
109+
110+
app = QApplication([])
111+
ow = OWTranspose()
112+
d = Table("zoo")
113+
d.domain.attributes[0].attributes = {"key": "value"}
114+
ow.set_data(d)
115+
ow.show()
116+
app.exec_()
117+
ow.saveSettings()

0 commit comments

Comments
 (0)