Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
425 additions
and
0 deletions.
There are no files selected for viewing
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,124 @@ | ||
from itertools import chain | ||
|
||
from AnyQt.QtCore import Qt | ||
|
||
from Orange.data import (Table, ContinuousVariable, DiscreteVariable, | ||
StringVariable, Domain) | ||
from Orange.widgets.settings import (Setting, ContextSetting, | ||
PerfectDomainContextHandler) | ||
from Orange.widgets.utils.itemmodels import DomainModel | ||
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 = DomainModel( | ||
order=DomainModel.METAS, placeholder="None", | ||
valid_types=StringVariable, alphabetical=True) | ||
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 = DomainModel( | ||
order=DomainModel.ATTRIBUTES, placeholder="None", | ||
valid_types=DomainModel.PRIMITIVE, alphabetical=True) | ||
self.class_combo = gui.comboBox( | ||
self.controlArea, self, "class_variable_index", | ||
box="Class attribute", **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.class_variable_index = min(self.class_variable_index, | ||
len(self.class_model) - 1) | ||
self.apply() | ||
|
||
def update_controls(self): | ||
self.feature_model.set_domain(None) | ||
self.class_model.set_domain(None) | ||
self.feature_names_column = "None" | ||
if self.data: | ||
self.feature_model.set_domain(self.data.domain) | ||
self.feature_names_column = self.data.domain.metas[0].name \ | ||
if self.data.domain.metas else "None" | ||
names = chain.from_iterable( | ||
list(a.attributes) for a in self.data.domain.attributes) | ||
variables = chain.from_iterable( | ||
(DiscreteVariable(name), ContinuousVariable(name)) | ||
for name in set(names)) | ||
self.class_model.set_domain(Domain(list(variables))) | ||
self.class_variable_index = min(1, 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 is not None: | ||
options["class_name"] = class_var.name | ||
options["class_type"] = type(class_var) | ||
|
||
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 AnyQt.QtWidgets 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() |
Oops, something went wrong.