Skip to content

Commit b6cb734

Browse files
committed
Pythagorean Forest: new signals and minor refactoring
1 parent 6b261e0 commit b6cb734

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

Orange/widgets/visualize/owpythagoreanforest.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from Orange.base import RandomForestModel, TreeModel
1010
from Orange.data import Table
1111
from Orange.widgets import gui, settings
12+
from Orange.widgets.utils.signals import Input, Output
1213
from Orange.widgets.visualize.pythagorastreeviewer import (
1314
PythagorasTreeViewer,
1415
ContinuousTreeNode,
@@ -30,8 +31,11 @@ class OWPythagoreanForest(OWWidget):
3031

3132
priority = 1001
3233

33-
inputs = [('Random forest', RandomForestModel, 'set_rf')]
34-
outputs = [('Tree', TreeModel)]
34+
class Inputs:
35+
random_forest = Input("Random forest", RandomForestModel)
36+
37+
class Outputs:
38+
tree = Output("Tree", TreeModel)
3539

3640
# Enable the save as feature
3741
graph_name = 'scene'
@@ -110,6 +114,7 @@ def __init__(self):
110114

111115
self.clear()
112116

117+
@Inputs.random_forest
113118
def set_rf(self, model=None):
114119
"""When a different forest is given."""
115120
self.clear()
@@ -273,7 +278,7 @@ def commit(self):
273278
return
274279

275280
if not self.scene.selectedItems():
276-
self.send('Tree', None)
281+
self.Outputs.tree.send(None)
277282
# The selected tree index should only reset when model changes
278283
if self.model is None:
279284
self.selected_tree_index = -1
@@ -287,7 +292,7 @@ def commit(self):
287292
tree.meta_size_calc_idx = self.size_calc_idx
288293
tree.meta_depth_limit = self.depth_limit
289294

290-
self.send('Tree', tree)
295+
self.Outputs.tree.send(tree)
291296

292297
def send_report(self):
293298
"""Send report."""

Orange/widgets/visualize/tests/test_owpythagoreanforest.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,47 +39,48 @@ def get_grid_items(self):
3939
if isinstance(x, GridItem)]
4040

4141
def test_sending_rf_draws_trees(self):
42+
w = self.widget
4243
# No trees by default
4344
self.assertEqual(len(self.get_tree_widgets()), 0,
4445
'No trees should be drawn when no forest on input')
4546

4647
# Draw trees for classification rf
47-
self.send_signal('Random forest', self.titanic)
48+
self.send_signal(w.Inputs.random_forest, self.titanic)
4849
self.assertEqual(len(self.get_tree_widgets()), 3,
4950
'Incorrect number of trees when forest on input')
5051

5152
# Clear trees when None
52-
self.send_signal('Random forest', None)
53+
self.send_signal(w.Inputs.random_forest, None)
5354
self.assertEqual(len(self.get_tree_widgets()), 0,
5455
'Trees are cleared when forest is disconnected')
5556

5657
# Draw trees for regression rf
57-
self.send_signal('Random forest', self.housing)
58+
self.send_signal(w.Inputs.random_forest, self.housing)
5859
self.assertEqual(len(self.get_tree_widgets()), 3,
5960
'Incorrect number of trees when forest on input')
6061

6162
def test_info_label(self):
63+
w = self.widget
6264
regex = r'Trees:(.+)'
6365
# If no forest on input, display a message saying that
64-
self.assertNotRegex(self.widget.ui_info.text(), regex,
66+
self.assertNotRegex(w.ui_info.text(), regex,
6567
'Initial info should not contain info on trees')
6668

67-
self.send_signal('Random forest', self.titanic)
68-
self.assertRegex(self.widget.ui_info.text(), regex,
69-
'Valid RF does not update info')
69+
self.send_signal(w.Inputs.random_forest, self.titanic)
70+
self.assertRegex(self.widget.ui_info.text(), regex, 'Valid RF does not update info')
7071

71-
self.send_signal('Random forest', None)
72-
self.assertNotRegex(self.widget.ui_info.text(), regex,
73-
'Removing RF does not clear info box')
72+
self.send_signal(w.Inputs.random_forest, None)
73+
self.assertNotRegex(w.ui_info.text(), regex, 'Removing RF does not clear info box')
7474

7575
def test_depth_slider(self):
76-
self.send_signal('Random forest', self.titanic)
76+
w = self.widget
77+
self.send_signal(w.Inputs.random_forest, self.titanic)
7778

7879
trees = self.get_tree_widgets()
7980
for tree in trees:
8081
tree.set_depth_limit = Mock()
8182

82-
self.widget.ui_depth_slider.setValue(0)
83+
w.ui_depth_slider.setValue(0)
8384
for tree in trees:
8485
tree.set_depth_limit.assert_called_once_with(0)
8586

@@ -106,77 +107,78 @@ def _check_all_same(self, items):
106107

107108
def test_changing_target_class_changes_coloring(self):
108109
"""Changing the `Target class` combo box should update colors."""
110+
w = self.widget
109111
def _test(data_type):
110112
colors, tree = [], self._pick_random_tree()
111113

112114
def _callback():
113115
colors.append([sq.brush().color() for sq in self._get_visible_squares(tree)])
114116

115-
simulate.combobox_run_through_all(
116-
self.widget.ui_target_class_combo, callback=_callback)
117+
simulate.combobox_run_through_all(w.ui_target_class_combo, callback=_callback)
117118

118119
# Check that individual squares all have different colors
119120
squares_same = [self._check_all_same(x) for x in zip(*colors)]
120121
# Check that at least some of the squares have different colors
121122
self.assertTrue(any(x is False for x in squares_same),
122123
'Colors did not change for %s data' % data_type)
123124

124-
self.send_signal('Random forest', self.titanic)
125+
self.send_signal(w.Inputs.random_forest, self.titanic)
125126
_test('classification')
126-
self.send_signal('Random forest', self.housing)
127+
self.send_signal(w.Inputs.random_forest, self.housing)
127128
_test('regression')
128129

129130
def test_changing_size_adjustment_changes_sizes(self):
130-
self.send_signal('Random forest', self.titanic)
131+
w = self.widget
132+
self.send_signal(w.Inputs.random_forest, self.titanic)
131133
squares = []
132134
# We have to get the same tree with an index on the grid items since
133135
# the tree objects are deleted and recreated with every invalidation
134-
tree_index = self.widget.grid_items.index(random.choice(self.get_grid_items()))
136+
tree_index = w.grid_items.index(random.choice(self.get_grid_items()))
135137

136138
def _callback():
137139
squares.append([sq.rect() for sq in self._get_visible_squares(
138140
self.get_tree_widgets()[tree_index])])
139141

140-
simulate.combobox_run_through_all(
141-
self.widget.ui_size_calc_combo, callback=_callback)
142+
simulate.combobox_run_through_all(w.ui_size_calc_combo, callback=_callback)
142143

143144
# Check that individual squares are in different position
144145
squares_same = [self._check_all_same(x) for x in zip(*squares)]
145146
# Check that at least some of the squares have different positions
146147
self.assertTrue(any(x is False for x in squares_same))
147148

148149
def test_zoom(self):
149-
self.send_signal('Random forest', self.titanic)
150+
w = self.widget
151+
self.send_signal(w.Inputs.random_forest, self.titanic)
150152

151-
grid_item, zoom = self.get_grid_items()[0], self.widget.zoom
153+
grid_item, zoom = self.get_grid_items()[0], w.zoom
152154

153155
def _destructure_rectf(r):
154156
return r.width(), r.height()
155157

156158
iw, ih = _destructure_rectf(grid_item.boundingRect())
157159

158160
# Increase the size of grid item
159-
self.widget.ui_zoom_slider.setValue(zoom + 1)
161+
w.ui_zoom_slider.setValue(zoom + 1)
160162
lw, lh = _destructure_rectf(grid_item.boundingRect())
161163
self.assertTrue(iw < lw and ih < lh)
162164

163165
# Decrease the size of grid item
164-
self.widget.ui_zoom_slider.setValue(zoom - 1)
166+
w.ui_zoom_slider.setValue(zoom - 1)
165167
lw, lh = _destructure_rectf(grid_item.boundingRect())
166168
self.assertTrue(iw > lw and ih > lh)
167169

168170
def test_keep_colors_on_sizing_change(self):
169171
"""The color should be the same after a full recompute of the tree."""
170-
self.send_signal('Random forest', self.titanic)
172+
w = self.widget
173+
self.send_signal(w.Inputs.random_forest, self.titanic)
171174
colors = []
172-
tree_index = self.widget.grid_items.index(random.choice(self.get_grid_items()))
175+
tree_index = w.grid_items.index(random.choice(self.get_grid_items()))
173176

174177
def _callback():
175178
colors.append([sq.brush().color() for sq in self._get_visible_squares(
176179
self.get_tree_widgets()[tree_index])])
177180

178-
simulate.combobox_run_through_all(
179-
self.widget.ui_size_calc_combo, callback=_callback)
181+
simulate.combobox_run_through_all(w.ui_size_calc_combo, callback=_callback)
180182

181183
# Check that individual squares all have the same color
182184
colors_same = [self._check_all_same(x) for x in zip(*colors)]

0 commit comments

Comments
 (0)