Skip to content

Commit 314f5e7

Browse files
Code simplification, minor test fix
1 parent 6a53bd1 commit 314f5e7

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

Orange/widgets/visualize/owvenndiagram.py

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class OWVennDiagram(widget.OWWidget):
4848
icon = "icons/VennDiagram.svg"
4949
priority = 280
5050
keywords = []
51+
settings_version = 2
5152

5253
class Inputs:
5354
data = Input("Data", Table, multiple=True)
@@ -59,7 +60,7 @@ class Outputs:
5960
class Error(widget.OWWidget.Error):
6061
domain_mismatch = Msg("Input data domains do not match.")
6162
instances_mismatch = Msg("Data sets do not contain the same instances.")
62-
too_much_inputs = Msg("venn diagram accepts at most five datasets.")
63+
too_many_outputs = Msg("venn diagram accepts at most five datasets.")
6364

6465
selection: list
6566

@@ -72,6 +73,7 @@ class Error(widget.OWWidget.Error):
7273
output_duplicates = settings.Setting(False)
7374
autocommit = settings.Setting(True)
7475
usecols = settings.Setting(False)
76+
selected_feature = None
7577

7678
graph_name = "scene"
7779

@@ -89,23 +91,17 @@ def __init__(self):
8991
# Extracted input item sets in the order they were 'connected'
9092
self.itemsets = OrderedDict()
9193

94+
9295
# GUI
9396
box = gui.vBox(self.controlArea, "Info")
9497
self.infolabel = gui.widgetLabel(box, "No data on input.\n")
9598

9699
self.elementsBox = gui.radioButtonsInBox(
97-
self.controlArea, self, 'usecols', [],
100+
self.controlArea, self, 'usecols',
101+
["Rows (data instances)", "Columns (features)"],
98102
box="Elements", callback=self._on_elements_changed
99103
)
100104

101-
self.useRowsButton = gui.appendRadioButton(
102-
self.elementsBox, "Rows (data instances)"
103-
)
104-
105-
self.useColumnsButton = gui.appendRadioButton(
106-
self.elementsBox, "Columns (features)"
107-
)
108-
109105
self.identifiersBox = gui.radioButtonsInBox(
110106
self.controlArea, self, "useidentifiers", [],
111107
box="Row identifier",
@@ -118,22 +114,21 @@ def __init__(self):
118114
self.identifiersBox, "Use instance equality"
119115
)
120116
self.useidentifiersButton = rb = gui.appendRadioButton(
121-
self.identifiersBox, "Use identifiers"
117+
self.identifiersBox, "Match by Features"
122118
)
123119
self.inputsBox = gui.indentedBox(
124120
self.identifiersBox, sep=gui.checkButtonOffsetHint(rb)
125121
)
126122
self.inputsBox.setEnabled(bool(self.useidentifiers))
127123

128-
box = gui.vBox(self.inputsBox, 'feature', addSpace=False)
124+
box = gui.vBox(self.inputsBox, ' ', addSpace=False)
129125
box.setFlat(True)
130-
model = itemmodels.VariableListModel(parent=self)
131-
self.cb = cb = gui.OrangeComboBox(
126+
self.cb = cb = gui.comboBox(
127+
box, self, "selected_feature",
132128
minimumContentsLength=12,
133-
sizeAdjustPolicy=QComboBox.AdjustToMinimumContentsLengthWithIcon
129+
model=itemmodels.VariableListModel(parent=self),
130+
callback=self._on_inputAttrActivated
134131
)
135-
cb.setModel(model)
136-
cb.activated[int].connect(self._on_inputAttrActivated)
137132
box.setEnabled(False)
138133
# Store the combo in the box for later use.
139134
box.combo_box = cb
@@ -144,7 +139,7 @@ def __init__(self):
144139
self.output_duplicates_box = box = gui.vBox(self.controlArea, "Output")
145140
self.output_duplicates_box.setEnabled(not self.usecols)
146141
gui.checkBox(box, self, "output_duplicates", "Output duplicates",
147-
callback=self.commit)
142+
callback=lambda: self.commit())
148143
gui.auto_send(box, self, "autocommit", box=False)
149144

150145
# Main area view
@@ -170,14 +165,14 @@ def __init__(self):
170165
@Inputs.data
171166
@check_sql_input
172167
def setData(self, data, key=None):
173-
self.Error.too_much_inputs.clear()
168+
self.Error.too_many_outputs.clear()
174169
if not self._inputUpdate:
175170
self._inputUpdate = True
176171
if key in self.data:
177172
if data is None:
178173
# Remove the input
179174
# Clear possible warnings.
180-
self.warning()
175+
self.Warning.clear()
181176
del self.data[key]
182177
else:
183178
# Update existing item
@@ -187,7 +182,7 @@ def setData(self, data, key=None):
187182
# TODO: Allow setting more them 5 inputs and let the user
188183
# select the 5 to display.
189184
if len(self.data) == 5:
190-
self.Error.too_much_inputs()
185+
self.Error.too_many_outputs()
191186
return
192187
# Add a new input
193188
self.data[key] = _InputData(key, data.name, data)
@@ -201,8 +196,15 @@ def data_equality(self):
201196
for val in self.data.values():
202197
sets.append(set(val.table.ids))
203198
inter = reduce(set.intersection, sets)
204-
if len(inter) == max(map(len, sets)):
205-
return True
199+
return len(inter) == max(map(len, sets))
200+
201+
def settings_incompatible(self):
202+
self.vennwidget.clear()
203+
if not self.usecols:
204+
self.Error.domain_mismatch()
205+
else:
206+
self.Error.instances_mismatch()
207+
self.itemsets = OrderedDict()
206208
return False
207209

208210
def settings_compatible(self):
@@ -212,10 +214,7 @@ def settings_compatible(self):
212214
domains = [input.table.domain for input in self.data.values()]
213215
samedomain = all((d1 == d2) for d1, d2 in pairwise(domains))
214216
if not samedomain:
215-
self.vennwidget.clear()
216-
self.Error.domain_mismatch()
217-
self.itemsets = OrderedDict()
218-
return False
217+
return self.settings_incompatible()
219218
self.samedomain = samedomain
220219
has_identifiers = all(source_attributes(input.table.domain)
221220
for input in self.data.values())
@@ -225,10 +224,7 @@ def settings_compatible(self):
225224
return True
226225
else:
227226
if not self.data_equality():
228-
self.vennwidget.clear()
229-
self.Error.instances_mismatch()
230-
self.itemsets = OrderedDict()
231-
return False
227+
return self.settings_incompatible()
232228
return True
233229

234230
def handleNewSignals(self):
@@ -494,6 +490,8 @@ def create_from_columns(self, columns):
494490
Columns are duplicated only if values differ (even
495491
if only in order of values), origin table name and input slot is added to column name.
496492
"""
493+
if not self.data.keys():
494+
return None, None
497495
selected = None
498496
atr_vals = {'metas': 'metas', 'attributes': 'X', 'class_vars': 'Y'}
499497

@@ -543,7 +541,8 @@ def merge_vars(new_atrs, atr):
543541
annotated = self.extract_new_table(var_dict)
544542

545543
for atr in annotated.domain.attributes:
546-
atr.attributes['Selected'] = selected and atr in selected.domain.attributes
544+
#written in this form to allow for selected to be None
545+
atr.attributes['Selected'] = not (not columns or not atr in selected.domain.attributes)
547546

548547
return selected, annotated
549548

@@ -1719,9 +1718,8 @@ def arrays_equal(a, b, type_):
17191718

17201719
data = append_column(data, "M", StringVariable("Test"),
17211720
numpy.arange(len(data)).reshape(-1, 1) % 30)
1722-
res = ShuffleSplit(data, [None], n_resamples=5,
1723-
test_size=0.7, stratified=False)
1724-
indices = iter(res.indices)
1721+
res = ShuffleSplit(n_resamples=5, test_size=0.7, stratified=False)
1722+
indices = iter(res.get_indices(data))
17251723
datasets = []
17261724
for i in range(1, 6):
17271725
sample, _ = next(indices)
@@ -1730,3 +1728,5 @@ def arrays_equal(a, b, type_):
17301728
datasets.append((data1, i))
17311729

17321730
WidgetPreview(OWVennDiagram).run(setData=datasets)
1731+
1732+
indices = iter(res.indices)

Orange/widgets/visualize/tests/test_owvenndiagram.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ def test_multiple_input_over_cols(self):
178178
self.assertEqual(len(annotated), len(self.data))
179179
self.assertEqual(annotated.domain, self.data.domain)
180180
for atr in annotated.domain.attributes:
181+
self.assertIsNotNone(atr.attributes[selected_atr_name])
181182
self.assertFalse(atr.attributes[selected_atr_name])
182183

183184
# select data instances
@@ -271,17 +272,17 @@ def test_rows_identifiers(self):
271272
annotated = self.get_output(self.widget.Outputs.annotated_data)
272273
self.assertEqual(len(annotated), 100)
273274

274-
def test_too_much_inputs(self):
275+
def test_too_many_outputs(self):
275276
self.send_signal(self.signal_name, self.data, 1)
276277
self.send_signal(self.signal_name, self.data, 2)
277278
self.send_signal(self.signal_name, self.data, 3)
278279
self.send_signal(self.signal_name, self.data, 4)
279280
self.send_signal(self.signal_name, self.data, 5)
280281
self.send_signal(self.signal_name, self.data, 6)
281-
self.assertTrue(self.widget.Error.too_much_inputs.is_shown())
282+
self.assertTrue(self.widget.Error.too_many_outputs.is_shown())
282283

283284
self.send_signal(self.signal_name, None, 6)
284-
self.assertFalse(self.widget.Error.too_much_inputs.is_shown())
285+
self.assertFalse(self.widget.Error.too_many_outputs.is_shown())
285286

286287

287288
class GroupTableIndicesTest(unittest.TestCase):

0 commit comments

Comments
 (0)