source: orange/Orange/OrangeWidgets/Data/OWEditDomain.py @ 11430:f9e572f80f4e

Revision 11430:f9e572f80f4e, 20.8 KB checked in by Ales Erjavec <ales.erjavec@…>, 13 months ago (diff)

Edit Domain widget code style fix.

Empty and trailing line whitespace.

RevLine 
[9320]1"""
2<name>Edit Domain</name>
3<description>Edit domain variables</description>
[11096]4<icon>icons/EditDomain.svg</icon>
[9320]5<contact>Ales Erjavec (ales.erjavec(@ at @)fri.uni-lj.si)</contact>
6<priority>3125</priority>
[9323]7<keywords>change,name,variable,domain</keywords>
[9320]8"""
9
10from OWWidget import *
11from OWItemModels import VariableListModel, PyListModel
12
13import OWGUI
14
15import Orange
16
[11430]17
[9320]18def is_discrete(var):
[10046]19    return isinstance(var, Orange.feature.Discrete)
[9320]20
[11430]21
[9320]22def is_continuous(var):
[10046]23    return isinstance(var, Orange.feature.Continuous)
[9320]24
[11430]25
[9320]26def get_qualified(module, name):
[11430]27    """Return a qualified module member ``name`` inside the named
[9323]28    ``module``.
[11430]29
[10358]30    The module (or package) first gets imported and the name
[9323]31    is retrieved from the module's global namespace.
[11430]32
[9323]33    """
[10358]34    # see __import__.__doc__ for why 'fromlist' is used
35    module = __import__(module, fromlist=[name])
[9320]36    return getattr(module, name)
37
[11430]38
[9320]39def variable_description(var):
[10358]40    """Return a variable descriptor.
[11430]41
42    A descriptor is a hashable tuple which should uniquely define
43    the variable i.e. (module, type_name, variable_name,
[10358]44    any_kwargs, sorted-attributes-items).
[11430]45
[9323]46    """
[9320]47    var_type = type(var)
48    if is_discrete(var):
49        return (var_type.__module__,
50                var_type.__name__,
[11430]51                var.name,
52                (("values", tuple(var.values)),),
[9320]53                tuple(sorted(var.attributes.items())))
54    else:
55        return (var_type.__module__,
56                var_type.__name__,
[11430]57                var.name,
58                (),
[9320]59                tuple(sorted(var.attributes.items())))
60
[11430]61
[9320]62def variable_from_description(description):
[11430]63    """Construct a variable from its description (see
64    :func:`variable_description`).
65
[9323]66    """
[9320]67    module, type_name, name, kwargs, attrs = description
[10358]68    try:
69        type = get_qualified(module, type_name)
70    except (ImportError, AttributeError), ex:
[11430]71        raise ValueError("Invalid descriptor type '{}.{}"
72                         "".format(module, type_name))
73
[9320]74    var = type(name, **dict(list(kwargs)))
75    var.attributes.update(attrs)
76    return var
[11430]77
[9320]78from PyQt4 import QtCore, QtGui
79
80QtCore.Slot = QtCore.pyqtSlot
81QtCore.Signal = QtCore.pyqtSignal
82
[11430]83
[9320]84class PyStandardItem(QStandardItem):
85    def __lt__(self, other):
86        return id(self) < id(other)
[11430]87
88
[9320]89class DictItemsModel(QStandardItemModel):
[10358]90    """A Qt Item Model class displaying the contents of a python
[9323]91    dictionary.
[11430]92
[9323]93    """
94    # Implement a proper model with in-place editing.
95    # (Maybe it should be a TableModel with 2 columns)
[9320]96    def __init__(self, parent=None, dict={}):
97        QStandardItemModel.__init__(self, parent)
98        self.setHorizontalHeaderLabels(["Key", "Value"])
99        self.set_dict(dict)
[11430]100
[9320]101    def set_dict(self, dict):
102        self._dict = dict
103        self.clear()
104        self.setHorizontalHeaderLabels(["Key", "Value"])
105        for key, value in sorted(dict.items()):
106            key_item = PyStandardItem(QString(key))
107            value_item = PyStandardItem(QString(value))
108            key_item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
109            value_item.setFlags(value_item.flags() | Qt.ItemIsEditable)
110            self.appendRow([key_item, value_item])
[11430]111
[9320]112    def get_dict(self):
113        dict = {}
114        for row in range(self.rowCount()):
115            key_item = self.item(row, 0)
116            value_item = self.item(row, 1)
117            dict[str(key_item.text())] = str(value_item.text())
118        return dict
119
[11430]120
[9320]121class VariableEditor(QWidget):
[10358]122    """An editor widget for a variable.
[11430]123
[9323]124    Can edit the variable name, and its attributes dictionary.
[11430]125
[9323]126    """
[9320]127    def __init__(self, parent=None):
128        QWidget.__init__(self, parent)
129        self.setup_gui()
[11430]130
[9320]131    def setup_gui(self):
132        layout = QVBoxLayout()
133        self.setLayout(layout)
[11430]134
[9320]135        self.main_form = QFormLayout()
136        self.main_form.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)
137        layout.addLayout(self.main_form)
[11430]138
[9320]139        self._setup_gui_name()
140        self._setup_gui_labels()
[11430]141
[9320]142    def _setup_gui_name(self):
143        self.name_edit = QLineEdit()
144        self.main_form.addRow("Name", self.name_edit)
145        self.name_edit.editingFinished.connect(self.on_name_changed)
[11430]146
[9320]147    def _setup_gui_labels(self):
[9323]148        vlayout = QVBoxLayout()
149        vlayout.setContentsMargins(0, 0, 0, 0)
150        vlayout.setSpacing(1)
[11430]151
[9320]152        self.labels_edit = QTreeView()
[10726]153        self.labels_edit.setEditTriggers(QTreeView.CurrentChanged)
[9323]154        self.labels_edit.setRootIsDecorated(False)
[11430]155
[9320]156        self.labels_model = DictItemsModel()
157        self.labels_edit.setModel(self.labels_model)
[11430]158
[9323]159        self.labels_edit.selectionModel().selectionChanged.connect(\
160                                    self.on_label_selection_changed)
[11430]161
[9323]162        # Necessary signals to know when the labels change
[9320]163        self.labels_model.dataChanged.connect(self.on_labels_changed)
[9323]164        self.labels_model.rowsInserted.connect(self.on_labels_changed)
165        self.labels_model.rowsRemoved.connect(self.on_labels_changed)
[11430]166
[9323]167        vlayout.addWidget(self.labels_edit)
168        hlayout = QHBoxLayout()
169        hlayout.setContentsMargins(0, 0, 0, 0)
170        hlayout.setSpacing(1)
171        self.add_label_action = QAction("+", self,
172                        toolTip="Add a new label.",
173                        triggered=self.on_add_label,
174                        enabled=False,
175                        shortcut=QKeySequence(QKeySequence.New))
176
177        self.remove_label_action = QAction("-", self,
178                        toolTip="Remove selected label.",
179                        triggered=self.on_remove_label,
180                        enabled=False,
181                        shortcut=QKeySequence(QKeySequence.Delete))
[11430]182
[9325]183        button_size = OWGUI.toolButtonSizeHint()
[9326]184        button_size = QSize(button_size, button_size)
[11430]185
[9323]186        button = QToolButton(self)
[9325]187        button.setFixedSize(button_size)
[9323]188        button.setDefaultAction(self.add_label_action)
189        hlayout.addWidget(button)
[11430]190
[9323]191        button = QToolButton(self)
[9325]192        button.setFixedSize(button_size)
[9323]193        button.setDefaultAction(self.remove_label_action)
194        hlayout.addWidget(button)
195        hlayout.addStretch(10)
196        vlayout.addLayout(hlayout)
[11430]197
[9323]198        self.main_form.addRow("Labels", vlayout)
[11430]199
[9320]200    def set_data(self, var):
[10358]201        """Set the variable to edit.
[9323]202        """
[9320]203        self.clear()
204        self.var = var
[11430]205
[9320]206        if var is not None:
207            self.name_edit.setText(var.name)
208            self.labels_model.set_dict(dict(var.attributes))
[9323]209            self.add_label_action.setEnabled(True)
210        else:
211            self.add_label_action.setEnabled(False)
212            self.remove_label_action.setEnabled(False)
[11430]213
[9320]214    def get_data(self):
[10358]215        """Retrieve the modified variable.
[9323]216        """
[9320]217        name = str(self.name_edit.text())
218        labels = self.labels_model.get_dict()
[11430]219
220        # Is the variable actually changed.
[9320]221        if not self.is_same():
222            var = type(self.var)(name)
223            var.attributes.update(labels)
224            self.var = var
225        else:
226            var = self.var
[11430]227
[9320]228        return var
[11430]229
[9320]230    def is_same(self):
[11430]231        """Is the current model state the same as the input.
[9320]232        """
233        name = str(self.name_edit.text())
234        labels = self.labels_model.get_dict()
[11430]235
[9320]236        return self.var and name == self.var.name and labels == self.var.attributes
[11430]237
[9320]238    def clear(self):
[10358]239        """Clear the editor state.
[9323]240        """
[9320]241        self.var = None
242        self.name_edit.setText("")
243        self.labels_model.set_dict({})
[11430]244
[9320]245    def maybe_commit(self):
246        if not self.is_same():
247            self.commit()
[11430]248
[9320]249    def commit(self):
[10358]250        """Emit a ``variable_changed()`` signal.
[9323]251        """
[9320]252        self.emit(SIGNAL("variable_changed()"))
[11430]253
[9320]254    @QtCore.Slot()
255    def on_name_changed(self):
256        self.maybe_commit()
[11430]257
[9320]258    @QtCore.Slot()
[9323]259    def on_labels_changed(self, *args):
[9320]260        self.maybe_commit()
[11430]261
[9323]262    @QtCore.Slot()
263    def on_add_label(self):
264        self.labels_model.appendRow([PyStandardItem(""), PyStandardItem("")])
265        row = self.labels_model.rowCount() - 1
266        index = self.labels_model.index(row, 0)
267        self.labels_edit.edit(index)
[11430]268
[9323]269    @QtCore.Slot()
270    def on_remove_label(self):
271        rows = self.labels_edit.selectionModel().selectedRows()
272        if rows:
273            row = rows[0]
274            self.labels_model.removeRow(row.row())
[11430]275
[9323]276    @QtCore.Slot()
277    def on_label_selection_changed(self):
278        selected = self.labels_edit.selectionModel().selectedRows()
279        self.remove_label_action.setEnabled(bool(len(selected)))
[11430]280
281
[9320]282class DiscreteVariableEditor(VariableEditor):
[10358]283    """An editor widget for editing a discrete variable.
[11430]284
[9323]285    Extends the :class:`VariableEditor` to enable editing of
286    variables values.
[11430]287
[9323]288    """
[9320]289    def setup_gui(self):
290        layout = QVBoxLayout()
291        self.setLayout(layout)
[11430]292
[9320]293        self.main_form = QFormLayout()
294        self.main_form.setFieldGrowthPolicy(QFormLayout.AllNonFixedFieldsGrow)
295        layout.addLayout(self.main_form)
[11430]296
[9320]297        self._setup_gui_name()
298        self._setup_gui_values()
299        self._setup_gui_labels()
[11430]300
[9320]301    def _setup_gui_values(self):
302        self.values_edit = QListView()
[10726]303        self.values_edit.setEditTriggers(QTreeView.CurrentChanged)
[9320]304        self.values_model = PyListModel(flags=Qt.ItemIsSelectable | \
305                                        Qt.ItemIsEnabled | Qt.ItemIsEditable)
306        self.values_edit.setModel(self.values_model)
[11430]307
[9320]308        self.values_model.dataChanged.connect(self.on_values_changed)
309        self.main_form.addRow("Values", self.values_edit)
310
311    def set_data(self, var):
[10358]312        """Set the variable to edit
[9323]313        """
[9320]314        VariableEditor.set_data(self, var)
315        self.values_model.wrap([])
316        if var is not None:
317            for v in var.values:
318                self.values_model.append(v)
[11430]319
[9320]320    def get_data(self):
[10358]321        """Retrieve the modified variable
[9323]322        """
[9320]323        name = str(self.name_edit.text())
324        labels = self.labels_model.get_dict()
325        values = map(str, self.values_model)
[11430]326
[9320]327        if not self.is_same():
328            var = type(self.var)(name, values=values)
329            var.attributes.update(labels)
330            self.var = var
331        else:
332            var = self.var
[11430]333
[9320]334        return var
[11430]335
[9320]336    def is_same(self):
[11430]337        """Is the current model state the same as the input.
[9320]338        """
339        values = map(str, self.values_model)
340        return VariableEditor.is_same(self) and self.var.values == values
[11430]341
[9320]342    def clear(self):
[10358]343        """Clear the model state.
[9323]344        """
[9320]345        VariableEditor.clear(self)
346        self.values_model.wrap([])
[11430]347
[9320]348    @QtCore.Slot()
349    def on_values_changed(self):
350        self.maybe_commit()
[11430]351
352
[9320]353class ContinuousVariableEditor(VariableEditor):
[11430]354    # TODO: enable editing of number_of_decimals, scientific format ...
355    pass
[9320]356
357
358class OWEditDomain(OWWidget):
[11430]359    contextHandlers = {
360        "": DomainContextHandler(
361            "",
362            ["domain_change_hints", "selected_index"]
363        )
364    }
[9323]365    settingsList = ["auto_commit"]
[11430]366
[9320]367    def __init__(self, parent=None, signalManager=None, title="Edit Domain"):
368        OWWidget.__init__(self, parent, signalManager, title)
[11430]369
[9546]370        self.inputs = [("Data", Orange.data.Table, self.set_data)]
371        self.outputs = [("Data", Orange.data.Table)]
[11430]372
[9320]373        # Settings
[11430]374
[9323]375        # Domain change hints maps from input variables description to
376        # the modified variables description as returned by
377        # `variable_description` function
[9320]378        self.domain_change_hints = {}
379        self.selected_index = 0
380        self.auto_commit = False
381        self.changed_flag = False
[11430]382
[9320]383        self.loadSettings()
[11430]384
[9320]385        #####
386        # GUI
387        #####
[11430]388
[9323]389        # The list of domain's variables.
[9320]390        box = OWGUI.widgetBox(self.controlArea, "Domain Features")
391        self.domain_view = QListView()
392        self.domain_view.setSelectionMode(QListView.SingleSelection)
[11430]393
[9320]394        self.domain_model = VariableListModel()
[11430]395
[9320]396        self.domain_view.setModel(self.domain_model)
[11430]397
[9320]398        self.connect(self.domain_view.selectionModel(),
399                     SIGNAL("selectionChanged(QItemSelection, QItemSelection)"),
400                     self.on_selection_changed)
[11430]401
[9320]402        box.layout().addWidget(self.domain_view)
[11430]403
[9323]404        # A stack for variable editor widgets.
[9320]405        box = OWGUI.widgetBox(self.mainArea, "Edit Feature")
406        self.editor_stack = QStackedWidget()
407        box.layout().addWidget(self.editor_stack)
[11430]408
[9320]409        box = OWGUI.widgetBox(self.controlArea, "Reset")
[11430]410
[9320]411        OWGUI.button(box, self, "Reset selected",
412                     callback=self.reset_selected,
413                     tooltip="Reset changes made to the selected feature"
414                     )
[11430]415
[9320]416        OWGUI.button(box, self, "Reset all",
417                     callback=self.reset_all,
418                     tooltip="Reset all changes made to the domain"
419                     )
[11430]420
[9320]421        box = OWGUI.widgetBox(self.controlArea, "Commit")
[11430]422
[9320]423        b = OWGUI.button(box, self, "&Commit",
424                         callback=self.commit,
425                         tooltip="Commit the data with the changed domain",
426                         )
[11430]427
[9320]428        cb = OWGUI.checkBox(box, self, "auto_commit",
429                            label="Commit automatically",
430                            tooltip="Commit the changed domain on any change",
431                            callback=self.commit_if)
[11430]432
[9320]433        OWGUI.setStopper(self, b, cb, "changed_flag",
434                         callback=self.commit)
[11430]435
[9320]436        self._editor_cache = {}
[11430]437
[9320]438        self.resize(600, 500)
[11430]439
[9320]440    def clear(self):
[10358]441        """Clear the widget state.
[9323]442        """
[9320]443        self.data = None
444        self.domain_model[:] = []
445        self.domain_change_hints = {}
446        self.clear_editor()
[11430]447
[9320]448    def clear_editor(self):
[10358]449        """Clear the current editor widget
[9323]450        """
[9320]451        current = self.editor_stack.currentWidget()
452        if current:
453            QObject.disconnect(current, SIGNAL("variable_changed()"),
454                               self.on_variable_changed)
455            current.set_data(None)
[11430]456
[9320]457    def set_data(self, data=None):
458        self.closeContext("")
459        self.clear()
460        self.data = data
461        if data is not None:
462            input_domain = data.domain
463            all_vars = list(input_domain.variables) + \
464                       input_domain.getmetas().values()
[11430]465
[9320]466            self.openContext("", data)
[11430]467
[9320]468            edited_vars = []
[11430]469
470            # Apply any saved transformations as listed in
[9323]471            # `domain_change_hints`
[11430]472
[9320]473            for var in all_vars:
474                desc = variable_description(var)
475                changed = self.domain_change_hints.get(desc, None)
476                if changed is not None:
[10358]477                    try:
478                        new = variable_from_description(changed)
479                    except ValueError, ex:
480#                        print ex
481                        new = None
[11430]482
[10358]483                    if new is not None:
484                        # Make sure orange's domain transformations will work.
485                        new.source_variable = var
486                        new.get_value_from = Orange.core.ClassifierFromVar(whichVar=var)
487                        var = new
[11430]488
[9320]489                edited_vars.append(var)
[11430]490
[9320]491            self.all_vars = all_vars
492            self.input_domain = input_domain
[11430]493
[9323]494            # Sets the model to display in the 'Domain Features' view
[9320]495            self.domain_model[:] = edited_vars
[11430]496
[9320]497            # Try to restore the variable selection
498            index = self.selected_index
499            if self.selected_index >= len(all_vars):
500                index = 0 if len(all_vars) else -1
501            if index >= 0:
502                self.select_variable(index)
[11430]503
[9321]504            self.changed_flag = True
505            self.commit_if()
506        else:
507            # To force send None on output
508            self.commit()
[11430]509
[9320]510    def on_selection_changed(self, *args):
[11430]511        """When selection in 'Domain Features' view changes.
[9323]512        """
[9320]513        i = self.selected_var_index()
514        if i is not None:
515            self.open_editor(i)
516            self.selected_index = i
[11430]517
[9320]518    def selected_var_index(self):
[11430]519        """Return the selected row in 'Domain Features' view or None
[9323]520        if no row is selected.
[11430]521
[9323]522        """
[9320]523        rows = self.domain_view.selectionModel().selectedRows()
524        if rows:
525            return rows[0].row()
526        else:
527            return None
[11430]528
[9320]529    def select_variable(self, index):
[10358]530        """Select the variable with ``index`` in the 'Domain Features'
[9323]531        view.
[11430]532
[9323]533        """
[9320]534        sel_model = self.domain_view.selectionModel()
535        sel_model.select(self.domain_model.index(index, 0),
536                         QItemSelectionModel.ClearAndSelect)
[11430]537
[9320]538    def open_editor(self, index):
[10358]539        """Open the editor for variable at ``index`` and move it
[9323]540        to the top if the stack.
[11430]541
[9323]542        """
[9320]543        # First remove and clear the current editor if any
544        self.clear_editor()
[11430]545
[9320]546        var = self.domain_model[index]
[11430]547
[9320]548        editor = self.editor_for_variable(var)
549        editor.set_data(var)
550        self.edited_variable_index = index
[11430]551
[9320]552        QObject.connect(editor, SIGNAL("variable_changed()"),
553                        self.on_variable_changed)
554        self.editor_stack.setCurrentWidget(editor)
[11430]555
[9320]556    def editor_for_variable(self, var):
[10358]557        """Return the editor for ``var``'s variable type.
[11430]558
[9323]559        The editors are cached and reused by type.
[11430]560
[9323]561        """
[9320]562        editor = None
563        if is_discrete(var):
564            editor = DiscreteVariableEditor
565        elif is_continuous(var):
566            editor = ContinuousVariableEditor
567        else:
568            editor = VariableEditor
[11430]569
[9320]570        if type(var) not in self._editor_cache:
571            editor = editor()
572            self._editor_cache[type(var)] = editor
573            self.editor_stack.addWidget(editor)
[11430]574
[9320]575        return self._editor_cache[type(var)]
[11430]576
[9320]577    def on_variable_changed(self):
[10358]578        """When the user edited the current variable in editor.
[9323]579        """
[9320]580        var = self.domain_model[self.edited_variable_index]
581        editor = self.editor_stack.currentWidget()
582        new_var = editor.get_data()
[11430]583
[9323]584        # Replace the variable in the 'Domain Features' view/model
[9320]585        self.domain_model[self.edited_variable_index] = new_var
586        old_var = self.all_vars[self.edited_variable_index]
[11430]587
[9323]588        # Store the transformation hint.
[9320]589        self.domain_change_hints[variable_description(old_var)] = \
590                    variable_description(new_var)
[9323]591
592        # Make orange's domain transformation work.
[9320]593        new_var.source_variable = old_var
594        new_var.get_value_from = Orange.core.ClassifierFromVar(whichVar=old_var)
[11430]595
[9320]596        self.commit_if()
[11430]597
[9320]598    def reset_all(self):
[10358]599        """Reset all variables to the input state.
[9323]600        """
[9320]601        self.domain_change_hints = {}
602        if self.data is not None:
603            # To invalidate stored hints
604            self.closeContext("")
605            self.openContext("", self.data)
606            self.domain_model[:] = self.all_vars
607            self.select_variable(self.selected_index)
[9324]608            self.commit_if()
[11430]609
[9320]610    def reset_selected(self):
[10358]611        """Reset the currently selected variable to its original
[9323]612        state.
[11430]613
[9323]614        """
[9320]615        if self.data is not None:
616            var = self.all_vars[self.selected_index]
[9324]617            desc = variable_description(var)
618            if desc in self.domain_change_hints:
619                del self.domain_change_hints[desc]
[11430]620
[9324]621            # To invalidate stored hints
622            self.closeContext("")
623            self.openContext("", self.data)
[11430]624
[9320]625            self.domain_model[self.selected_index] = var
626            self.editor_stack.currentWidget().set_data(var)
[9324]627            self.commit_if()
[11430]628
[9320]629    def commit_if(self):
630        if self.auto_commit:
631            self.commit()
632        else:
633            self.changed_flag = True
[11430]634
[9320]635    def commit(self):
[11430]636        """Commit the changed data to output.
[9323]637        """
[9320]638        new_data = None
639        if self.data is not None:
640            new_vars = list(self.domain_model)
641            variables = new_vars[: len(self.input_domain.variables)]
642            class_var = None
643            if self.input_domain.class_var:
644                class_var = variables[-1]
645                variables = variables[:-1]
[11430]646
647            new_metas = new_vars[len(self.input_domain.variables):]
[9320]648            new_domain = Orange.data.Domain(variables, class_var)
[11430]649
[9320]650            # Assumes getmetas().items() order has not changed.
[9323]651            # TODO: store metaids in set_data method
[11430]652            for (mid, _), new in zip(self.input_domain.getmetas().items(),
[9320]653                                       new_metas):
654                new_domain.addmeta(mid, new)
[11430]655
[9320]656            new_data = Orange.data.Table(new_domain, self.data)
[11430]657
[9546]658        self.send("Data", new_data)
[9320]659        self.changed_flag = False
[11430]660
661
[9320]662def main():
663    import sys
664    app = QApplication(sys.argv)
665    w = OWEditDomain()
666    data = Orange.data.Table("iris")
667#    data = Orange.data.Table("rep:GDS636.tab")
668    w.set_data(data)
669    w.show()
670    rval = app.exec_()
671    w.set_data(None)
672    w.saveSettings()
673    return rval
[11430]674
[9320]675if __name__ == "__main__":
676    import sys
677    sys.exit(main())
Note: See TracBrowser for help on using the repository browser.