Skip to content

Commit

Permalink
Annotate settings in various widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
janezd committed Mar 12, 2021
1 parent dff6de6 commit c2fc3da
Show file tree
Hide file tree
Showing 22 changed files with 109 additions and 74 deletions.
5 changes: 3 additions & 2 deletions Orange/widgets/data/owcorrelations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from operator import attrgetter
from types import SimpleNamespace
from itertools import combinations, groupby, chain
from typing import List

import numpy as np
from scipy.stats import spearmanr, pearsonr
Expand Down Expand Up @@ -254,8 +255,8 @@ class Outputs:

settings_version = 3
settingsHandler = DomainContextHandler()
selection = ContextSetting([])
feature = ContextSetting(None)
selection: List[ContinuousVariable] = ContextSetting([])
feature: ContinuousVariable = ContextSetting(None)
correlation_type = Setting(0)

class Information(OWWidget.Information):
Expand Down
7 changes: 4 additions & 3 deletions Orange/widgets/data/owcreateclass.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Widget for creating classes from non-numeric attribute by substrings"""
import re
from itertools import count
from typing import List, Dict

import numpy as np

from AnyQt.QtWidgets import QGridLayout, QLabel, QLineEdit, QSizePolicy, QWidget
from AnyQt.QtCore import QSize, Qt

from Orange.data import StringVariable, DiscreteVariable, Domain
from Orange.data import StringVariable, DiscreteVariable, Domain, Variable
from Orange.data.table import Table
from Orange.statistics.util import bincount
from Orange.preprocess.transformation import Transformation, Lookup
Expand Down Expand Up @@ -176,9 +177,9 @@ class Outputs:
buttons_area_orientation = Qt.Vertical

settingsHandler = DomainContextHandler()
attribute = ContextSetting(None)
attribute: Variable = ContextSetting(None)
class_name = ContextSetting("class")
rules = ContextSetting({})
rules: Dict[str, List[List[str]]] = ContextSetting({})
match_beginning = ContextSetting(False)
case_sensitive = ContextSetting(False)

Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/owdatasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class Outputs:
data = Output("Data", Orange.data.Table)

#: Selected dataset id
selected_id = settings.Setting(None) # type: Optional[str]
selected_id: Optional[str] = settings.Setting(None)

#: main area splitter state
splitter_state = settings.Setting(b'') # type: bytes
Expand Down
13 changes: 12 additions & 1 deletion Orange/widgets/data/owmergedata.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from collections import namedtuple
from itertools import chain, product
from typing import List, Tuple, Dict

import numpy as np

from AnyQt.QtCore import Qt, QModelIndex, pyqtSignal as Signal
from AnyQt.QtWidgets import (
QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout
)

from orangewidget.settings import Context
from orangewidget.utils.combobox import ComboBoxSearch

import Orange
Expand Down Expand Up @@ -173,12 +176,19 @@ def data(self, index, role=Qt.DisplayRole):
return super().data(index, role)


class MergeDataContext(Context):
variables1: Dict[str, int]
variables2: Dict[str, int]


class MergeDataContextHandler(ContextHandler):
# `widget` is used as an argument in most methods
# pylint: disable=redefined-outer-name
# context handlers override methods using different signatures
# pylint: disable=arguments-differ

ContextType = MergeDataContext

def new_context(self, variables1, variables2):
context = super().new_context()
context.variables1 = variables1
Expand Down Expand Up @@ -286,7 +296,8 @@ class Outputs:
"merging_types")]

settingsHandler = MergeDataContextHandler()
attr_pairs = ContextSetting(None, schema_only=True)
attr_pairs: List[Tuple[Variable, Variable]] \
= ContextSetting([], schema_only=True)
merging = Setting(LeftJoin)
auto_apply = Setting(True)
settings_version = 2
Expand Down
5 changes: 3 additions & 2 deletions Orange/widgets/data/owpaintdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import itertools
from functools import partial, singledispatch
from collections import namedtuple
from typing import List

import numpy as np

Expand Down Expand Up @@ -761,8 +762,8 @@ class Outputs:
symbol_size = Setting(10)

#: current data array (shape=(N, 3)) as presented on the output
data = Setting(None, schema_only=True)
labels = Setting(["C1", "C2"], schema_only=True)
data: List[List[float]] = Setting(None, schema_only=True)
labels: List[str] = Setting(["C1", "C2"], schema_only=True)

buttons_area_orientation = Qt.Vertical
graph_name = "plot"
Expand Down
18 changes: 9 additions & 9 deletions Orange/widgets/data/owpivot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# pylint: disable=missing-docstring
from typing import Iterable, Set
from enum import IntEnum
from typing import Iterable, Set, Tuple
from collections import defaultdict
from itertools import product, chain

Expand All @@ -20,7 +21,6 @@
from Orange.data.filter import FilterContinuous, FilterDiscrete, Values
from Orange.statistics.util import (nanmin, nanmax, nanunique, nansum, nanvar,
nanmean, nanmedian, nanmode, bincount)
from Orange.util import Enum
from Orange.widgets import gui
from Orange.widgets.settings import (Setting, ContextSetting,
DomainContextHandler)
Expand All @@ -36,7 +36,7 @@
BorderColorRole = next(gui.OrangeUserRole)


class AggregationFunctionsEnum(Enum):
class AggregationFunctionsEnum(IntEnum):
(Count, Count_defined, Sum, Mean, Min, Max,
Mode, Median, Var, Majority) = range(10)

Expand All @@ -45,7 +45,7 @@ def __init__(self, *_, **__):
self.func = None

@property
def value(self):
def value(self): # pylint: disable=invalid-overridden-method
return self._value_

def __call__(self, *args):
Expand Down Expand Up @@ -748,11 +748,11 @@ class Warning(OWWidget.Warning):
too_many_values = Msg("Selected variable has too many values.")

settingsHandler = DomainContextHandler()
row_feature = ContextSetting(None)
col_feature = ContextSetting(None)
val_feature = ContextSetting(None)
sel_agg_functions = Setting(set([Pivot.Count]))
selection = Setting(set(), schema_only=True)
row_feature: Variable = ContextSetting(None)
col_feature: DiscreteVariable = ContextSetting(None)
val_feature: Variable = ContextSetting(None)
sel_agg_functions: Set[AggregationFunctionsEnum] = Setting({Pivot.Count})
selection: Set[Tuple[int, int]] = Setting(set(), schema_only=True)
auto_commit = Setting(True)

AGGREGATIONS = (Pivot.Count,
Expand Down
12 changes: 6 additions & 6 deletions Orange/widgets/data/owrank.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import partial
from itertools import chain
from types import SimpleNamespace
from typing import Any, Callable, List, Tuple
from typing import Any, Callable, List, Tuple, Set

import numpy as np
from AnyQt.QtCore import (
Expand All @@ -20,8 +20,8 @@
from scipy.sparse import issparse

from Orange.data import (
ContinuousVariable, DiscreteVariable, Domain, StringVariable, Table
)
Table, Domain,
ContinuousVariable, DiscreteVariable, StringVariable, Variable)
from Orange.data.util import get_unique_names_duplicates
from Orange.preprocess import score
from Orange.widgets import gui, report
Expand Down Expand Up @@ -269,12 +269,12 @@ class Outputs:
nSelected = ContextSetting(5)
auto_apply = Setting(True)

sorting = Setting((0, Qt.DescendingOrder))
selected_methods = Setting(set())
sorting: Tuple[int, int] = Setting((0, Qt.DescendingOrder))
selected_methods: Set[str] = Setting(set())

settings_version = 3
settingsHandler = DomainContextHandler()
selected_attrs = ContextSetting([], schema_only=True)
selected_attrs: List[Variable] = ContextSetting([], schema_only=True)
selectionMethod = ContextSetting(SelectNBest)

class Information(OWWidget.Information):
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/owselectcolumns.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class Outputs:
want_control_area = True

settingsHandler = SelectAttributesDomainContextHandler(first_match=False)
domain_role_hints = ContextSetting({})
domain_role_hints: Dict[Tuple[str, int], Tuple[str, int]] = ContextSetting({})
use_input_features = Setting(False)
ignore_new_features = Setting(False)
auto_commit = Setting(True)
Expand Down
11 changes: 6 additions & 5 deletions Orange/widgets/data/owtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections import OrderedDict, namedtuple

from math import isnan
from typing import List

import numpy
from scipy.sparse import issparse
Expand Down Expand Up @@ -177,24 +178,24 @@ class OWDataTable(OWWidget):
priority = 50
keywords = []

buttons_area_orientation = Qt.Vertical
dist_color_RGB = (220, 220, 220, 255)

class Inputs:
data = Input("Data", Table, multiple=True)

class Outputs:
selected_data = Output("Selected Data", Table, default=True)
annotated_data = Output(ANNOTATED_DATA_SIGNAL_NAME, Table)

buttons_area_orientation = Qt.Vertical

show_distributions = Setting(False)
dist_color_RGB = Setting((220, 220, 220, 255))
show_attribute_labels = Setting(True)
select_rows = Setting(True)
auto_commit = Setting(True)

color_by_class = Setting(True)
selected_rows = Setting([], schema_only=True)
selected_cols = Setting([], schema_only=True)
selected_rows: List[int] = Setting([], schema_only=True)
selected_cols: List[int] = Setting([], schema_only=True)

settings_version = 2

Expand Down
4 changes: 2 additions & 2 deletions Orange/widgets/data/owtranspose.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from Orange.data import Table, ContinuousVariable, StringVariable
from Orange.data import Table, ContinuousVariable, StringVariable, Variable
from Orange.widgets.settings import (Setting, ContextSetting,
DomainContextHandler)
from Orange.widgets.utils.itemmodels import DomainModel
Expand Down Expand Up @@ -32,7 +32,7 @@ class Outputs:
settingsHandler = DomainContextHandler()
feature_type = ContextSetting(GENERIC)
feature_name = ContextSetting("")
feature_names_column = ContextSetting(None)
feature_names_column: Variable = ContextSetting(None)
auto_apply = Setting(True)

class Warning(OWWidget.Warning):
Expand Down
2 changes: 1 addition & 1 deletion Orange/widgets/data/tests/test_owfeaturestatistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ def test_restores_previous_selection(self):

def test_settings_migration_to_ver21(self):
settings = {
'controlAreaVisible': True, 'savedWidgetGeometry': '',
'controlAreaVisible': True, 'savedWidgetGeometry': None,
'__version__': 1,
'context_settings': [
Context(
Expand Down
4 changes: 2 additions & 2 deletions Orange/widgets/evaluate/owpredictions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ class Error(OWWidget.Error):
score_table = settings.SettingProvider(ScoreTable)

#: List of selected class value indices in the `class_values` list
selected_classes = settings.ContextSetting([])
selection = settings.Setting([], schema_only=True)
selected_classes: List[int] = settings.ContextSetting([])
selection: List[int] = settings.Setting([], schema_only=True)

def __init__(self):
super().__init__()
Expand Down
3 changes: 2 additions & 1 deletion Orange/widgets/evaluate/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import warnings
from functools import partial
from itertools import chain
from typing import Set

import numpy as np

Expand Down Expand Up @@ -131,7 +132,7 @@ def is_bad(x):


class ScoreTable(OWComponent, QObject):
shown_scores = \
shown_scores: Set[str] = \
Setting(set(chain(*BUILTIN_SCORERS_ORDER.values())))

shownScoresChanged = Signal()
Expand Down
23 changes: 18 additions & 5 deletions Orange/widgets/unsupervised/owdistancematrix.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
import math
from typing import List

import numpy as np

Expand All @@ -8,15 +9,18 @@
from AnyQt.QtGui import QColor, QPen, QBrush
from AnyQt.QtCore import Qt, QAbstractTableModel, QSize

from orangewidget.settings import Context

from Orange.data import Table, Variable, StringVariable
from Orange.misc import DistMatrix
from Orange.widgets import widget, gui

from Orange.widgets import gui
from Orange.widgets.gui import OrangeUserRole
from Orange.widgets.settings import Setting, ContextSetting, ContextHandler
from Orange.widgets.utils.itemmodels import VariableListModel
from Orange.widgets.utils.itemselectionmodel import SymmetricSelectionModel
from Orange.widgets.utils.widgetpreview import WidgetPreview
from Orange.widgets.widget import Input, Output
from Orange.widgets.widget import OWWidget, AttributeList, Input, Output


class DistanceMatrixModel(QAbstractTableModel):
Expand Down Expand Up @@ -147,7 +151,16 @@ def sizeHintForColumn(self, column: int) -> int:
return hint + 1 if self.showGrid() else hint


class DistanceMatrixContext(Context):
dim: int
annotations: List[str]
annotation: str
selection: List[int]


class DistanceMatrixContextHandler(ContextHandler):
ContextType = DistanceMatrixContext

@staticmethod
def _var_names(annotations):
return [a.name if isinstance(a, Variable) else a for a in annotations]
Expand Down Expand Up @@ -180,7 +193,7 @@ def settings_to_widget(self, widget, *args):
widget.tableview.selectionModel().setSelectedItems(context.selection)


class OWDistanceMatrix(widget.OWWidget):
class OWDistanceMatrix(OWWidget):
name = "Distance Matrix"
description = "View distance matrix."
icon = "icons/DistanceMatrix.svg"
Expand All @@ -197,7 +210,7 @@ class Outputs:
settingsHandler = DistanceMatrixContextHandler()
auto_commit = Setting(True)
annotation_idx = ContextSetting(1)
selection = ContextSetting([])
selection: List[int] = ContextSetting([])

want_control_area = True
want_main_area = False
Expand Down Expand Up @@ -288,7 +301,7 @@ def _update_labels(self):
attr = self.distances.row_items.domain.attributes
labels = [str(attr[i]) for i in range(self.distances.shape[0])]
elif self.annotation_idx == 2 and \
isinstance(self.items, widget.AttributeList):
isinstance(self.items, AttributeList):
labels = [v.name for v in self.items]
elif isinstance(self.items, Table):
var = self.annot_combo.model()[self.annotation_idx]
Expand Down
12 changes: 6 additions & 6 deletions Orange/widgets/visualize/owbarplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,13 +383,13 @@ class Outputs:

buttons_area_orientation = Qt.Vertical
settingsHandler = DomainContextHandler()
selected_var = ContextSetting(None)
group_var = ContextSetting(None)
annot_var = ContextSetting(None)
color_var = ContextSetting(None)
selected_var: ContinuousVariable = ContextSetting(None)
group_var: DiscreteVariable = ContextSetting(None)
annot_var: Variable = ContextSetting(None)
color_var: DiscreteVariable = ContextSetting(None)
auto_commit = Setting(True)
selection = Setting(None, schema_only=True)
visual_settings = Setting({}, schema_only=True)
selection: List[int] = Setting(None, schema_only=True)
visual_settings: Dict[KeyType, ValueType] = Setting({}, schema_only=True)

graph = SettingProvider(BarPlotGraph)
graph_name = "graph.plotItem"
Expand Down
Loading

0 comments on commit c2fc3da

Please sign in to comment.