Skip to content

Commit 1ca190f

Browse files
committed
Hierarchical clustering: Make annotation a context settings
1 parent 909a119 commit 1ca190f

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

Orange/widgets/unsupervised/owhierarchicalclustering.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,16 @@ class OWHierarchicalClustering(widget.OWWidget):
723723
outputs = [("Selected Data", Orange.data.Table, widget.Default),
724724
(ANNOTATED_DATA_SIGNAL_NAME, Orange.data.Table)]
725725

726+
settingsHandler = settings.DomainContextHandler()
727+
726728
#: Selected linkage
727729
linkage = settings.Setting(1)
728730
#: Index of the selected annotation item (variable, ...)
729-
annotation_idx = settings.Setting(0)
731+
annotation = settings.ContextSetting("Enumeration")
732+
#: Out-of-context setting for the case when the "Name" option is available
733+
annotation_if_names = settings.Setting("Name")
734+
#: Out-of-context setting for the case with just "Enumerate" and "None"
735+
annotation_if_enumerate = settings.Setting("Enumerate")
730736
#: Selected tree pruning (none/max depth)
731737
pruning = settings.Setting(0)
732738
#: Maximum depth when max depth pruning is selected
@@ -753,6 +759,7 @@ class OWHierarchicalClustering(widget.OWWidget):
753759
AttributeRole, ClassRole, MetaRole = 0, 1, 2
754760

755761
cluster_roles = ["Attribute", "Class variable", "Meta variable"]
762+
basic_annotations = ["None", "Enumeration"]
756763

757764
def __init__(self):
758765
super().__init__()
@@ -768,11 +775,11 @@ def __init__(self):
768775
self.controlArea, self, "linkage", items=LINKAGE, box="Linkage",
769776
callback=self._invalidate_clustering)
770777

778+
model = itemmodels.VariableListModel()
779+
model[:] = self.basic_annotations
771780
self.label_cb = gui.comboBox(
772-
self.controlArea, self, "annotation_idx", box="Annotation",
773-
callback=self._update_labels, contentsLength=12)
774-
self.label_cb.setModel(itemmodels.VariableListModel())
775-
self.label_cb.model()[:] = ["None", "Enumeration"]
781+
self.controlArea, self, "annotation", box="Annotation",
782+
model=model, callback=self._update_labels, contentsLength=12)
776783

777784
box = gui.radioButtons(
778785
self.controlArea, self, "pruning", box="Pruning",
@@ -952,46 +959,51 @@ def axis_view(orientation):
952959

953960
def set_distances(self, matrix):
954961
self.error()
955-
self._set_items(None)
956962
if matrix is not None:
957963
N, _ = matrix.shape
958964
if N < 2:
959965
self.error("Empty distance matrix")
960966
matrix = None
961967

962968
self.matrix = matrix
963-
self._invalidate_clustering()
964-
965969
if matrix is not None:
966970
self._set_items(matrix.row_items, matrix.axis)
971+
else:
972+
self._set_items(None)
973+
self._invalidate_clustering()
967974

968975
self.unconditional_commit()
969976

970977
def _set_items(self, items, axis=1):
978+
self.closeContext()
971979
self.items = items
972980
model = self.label_cb.model()
973-
if items is None:
974-
model[:] = ["None", "Enumeration"]
975-
elif not axis:
976-
model[:] = ["None", "Enumeration", "Attribute names"]
977-
self.annotation_idx = 2
978-
elif isinstance(items, Orange.data.Table):
981+
if len(model) == 3:
982+
self.annotation_if_names = self.annotation
983+
elif len(model) == 2:
984+
self.annotation_if_enumerate = self.annotation
985+
if isinstance(items, Orange.data.Table) and axis:
979986
model[:] = chain(
980-
["None", "Enumeration"],
987+
self.basic_annotations,
981988
[model.Separator],
982989
items.domain.class_vars,
983990
items.domain.metas,
984991
[model.Separator] if (items.domain.class_vars or items.domain.metas) and
985992
next(filter_visible(items.domain.attributes), False) else [],
986993
filter_visible(items.domain.attributes)
987994
)
988-
elif isinstance(items, list) and \
989-
all(isinstance(var, Orange.data.Variable) for var in items):
990-
model[:] = ["None", "Enumeration", "Name"]
995+
if items.domain.class_vars:
996+
self.annotation = items.domain.class_vars[0]
997+
self.openContext(items.domain)
991998
else:
992-
model[:] = ["None", "Enumeration"]
993-
self.annotation_idx = min(self.annotation_idx,
994-
len(model) - 1)
999+
name_option = bool(
1000+
items is not None and (
1001+
not axis or
1002+
isinstance(items, list) and
1003+
all(isinstance(var, Orange.data.Variable) for var in items)))
1004+
model[:] = self.basic_annotations + ["Name"] * name_option
1005+
self.annotation = self.annotation_if_names if name_option \
1006+
else self.annotation_if_enumerate
9951007

9961008
def _clear_plot(self):
9971009
self.labels.set_labels([])
@@ -1042,17 +1054,16 @@ def _update_labels(self):
10421054
if self.root and self._displayed_root:
10431055
indices = [leaf.value.index for leaf in leaves(self.root)]
10441056

1045-
if self.annotation_idx == 0:
1057+
if self.annotation == "None":
10461058
labels = []
1047-
elif self.annotation_idx == 1:
1059+
elif self.annotation == "Enumeration":
10481060
labels = [str(i+1) for i in indices]
1049-
elif self.label_cb.model()[self.annotation_idx] == "Attribute names":
1061+
elif self.annotation == "Name":
10501062
attr = self.matrix.row_items.domain.attributes
10511063
labels = [str(attr[i]) for i in indices]
1052-
elif isinstance(self.items, Orange.data.Table):
1053-
var = self.label_cb.model()[self.annotation_idx]
1054-
col_data, _ = self.items.get_column_view(var)
1055-
labels = [var.str_val(val) for val in col_data]
1064+
elif isinstance(self.annotation, Orange.data.Variable):
1065+
col_data, _ = self.items.get_column_view(self.annotation)
1066+
labels = [self.annotation.str_val(val) for val in col_data]
10561067
labels = [labels[idx] for idx in indices]
10571068
else:
10581069
labels = []
@@ -1350,7 +1361,7 @@ def __zoom_factor_changed(self):
13501361

13511362
def send_report(self):
13521363
annot = self.label_cb.currentText()
1353-
if self.annotation_idx <= 1:
1364+
if isinstance(self.annotation, str):
13541365
annot = annot.lower()
13551366
if self.selection_method == 0:
13561367
sel = "manual"

0 commit comments

Comments
 (0)