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
403 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,107 @@ | ||
from AnyQt.QtCore import Qt | ||
|
||
from Orange.data import Table, StringVariable | ||
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_type = ContextSetting(0) | ||
feature_names_column = ContextSetting("") | ||
auto_apply = Setting(True) | ||
|
||
class Error(OWWidget.Error): | ||
value_error = Msg("{}") | ||
|
||
def __init__(self): | ||
super().__init__() | ||
self.data = None | ||
|
||
# GUI | ||
box = gui.vBox(self.controlArea, "Feature names") | ||
self.feature_radio = gui.radioButtonsInBox( | ||
box, self, "feature_type", callback=self._feature_type_changed, | ||
btnLabels=["Generic", "From meta attributes"]) | ||
|
||
self.feature_model = DomainModel( | ||
order=DomainModel.METAS, valid_types=StringVariable, | ||
alphabetical=True) | ||
self.feature_combo = gui.comboBox( | ||
box, self, "feature_names_column", orientation=Qt.Horizontal, | ||
labelWidth=100, sendSelectedValue=True, callback=self.apply, | ||
contentsLength=12, enabled=self.feature_type) | ||
self.feature_combo.setModel(self.feature_model) | ||
|
||
self.apply_button = gui.auto_commit( | ||
self.controlArea, self, "auto_apply", "&Apply", | ||
box=False, commit=self.apply) | ||
|
||
def _feature_type_changed(self): | ||
self.feature_combo.setEnabled(self.feature_type) | ||
self.apply() | ||
|
||
def set_data(self, data): | ||
self.closeContext() | ||
self.data = data | ||
self.update_controls() | ||
self.openContext(data) | ||
self.feature_combo.setEnabled(self.feature_type) | ||
self.apply() | ||
|
||
def update_controls(self): | ||
self.feature_model.set_domain(None) | ||
if self.data: | ||
self.feature_model.set_domain(self.data.domain) | ||
if len(self.feature_model): | ||
_names = [m.name for m in self.data.domain.metas if m.is_string] | ||
self.feature_names_column = _names[0] | ||
self.feature_radio.buttons[1].setEnabled(len(self.feature_model)) | ||
self.feature_type = int(bool(len(self.feature_model))) | ||
|
||
def apply(self): | ||
self.clear_messages() | ||
transposed = None | ||
if self.data: | ||
options = dict() | ||
if self.feature_type: | ||
options["feature_names_column"] = self.feature_names_column | ||
|
||
try: | ||
transposed = Table.transpose(self.data, **options) | ||
except ValueError as e: | ||
self.Error.value_error(e) | ||
self.send("Data", transposed) | ||
|
||
def send_report(self): | ||
text = "from meta attribute: {}".format(self.feature_names_column) \ | ||
if self.feature_type else "generic" | ||
self.report_items("", [("Feature names", text)]) | ||
if self.data: | ||
self.report_data("Data", self.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
from AnyQt.QtWidgets import QApplication | ||
|
||
app = QApplication([]) | ||
ow = OWTranspose() | ||
d = Table("zoo") | ||
ow.set_data(d) | ||
ow.show() | ||
app.exec_() | ||
ow.saveSettings() |
Oops, something went wrong.