Skip to content

Commit

Permalink
Pythagoranstree/forest: Remove unnecessary setting combo box indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
PrimozGodec committed Apr 15, 2020
1 parent cf781b1 commit 4dccf71
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
15 changes: 9 additions & 6 deletions Orange/widgets/visualize/owpythagorastree.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Outputs:
graph_name = 'scene'

# Settings
settingsHandler = settings.DomainContextHandler()
settingsHandler = settings.ClassValuesContextHandler()

depth_limit = settings.ContextSetting(10)
target_class_index = settings.ContextSetting(0)
Expand Down Expand Up @@ -155,6 +155,8 @@ def set_tree(self, model=None):

if model is not None:
self.data = model.instances

self._update_target_class_combo()
self.tree_adapter = self._get_tree_adapter(self.model)
self.ptree.clear()

Expand All @@ -169,11 +171,12 @@ def set_tree(self, model=None):
self._update_legend_colors()
self._update_legend_visibility()
self._update_info_box()
self._update_target_class_combo()

self._update_main_area()

self.openContext(self.model)
self.openContext(
model.domain.class_var if model.domain is not None else None
)

self.update_depth()

Expand Down Expand Up @@ -277,8 +280,7 @@ def _clear_depth_slider(self):

def _clear_target_class_combo(self):
self.target_class_combo.clear()
self.target_class_index = 0
self.target_class_combo.setCurrentIndex(self.target_class_index)
self.target_class_index = -1

def _set_max_depth(self):
"""Set the depth to the max depth and update appropriate actors."""
Expand Down Expand Up @@ -339,7 +341,8 @@ def _update_target_class_combo(self):
values = list(ContinuousTreeNode.COLOR_METHODS.keys())
label.setText(label_text)
self.target_class_combo.addItems(values)
self.target_class_combo.setCurrentIndex(self.target_class_index)
# set it to 0, context will change if required
self.target_class_index = 0

def _update_legend_colors(self):
if self.legend is not None:
Expand Down
19 changes: 11 additions & 8 deletions Orange/widgets/visualize/owpythagoreanforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ class Outputs:
graph_name = 'scene'

# Settings
settingsHandler = settings.DomainContextHandler()
settingsHandler = settings.ClassValuesContextHandler()

depth_limit = settings.ContextSetting(10)
depth_limit = settings.Setting(10)
target_class_index = settings.ContextSetting(0)
size_calc_idx = settings.Setting(0)
zoom = settings.Setting(200)
Expand Down Expand Up @@ -274,15 +274,18 @@ def set_rf(self, model=None):
self.rf_model = model

if model is not None:
self.instances = model.instances
self._update_target_class_combo()

self.forest = self._get_forest_adapter(self.rf_model)
self.forest_model[:] = self.forest.trees
self.instances = model.instances

self._update_info_box()
self._update_target_class_combo()
self._update_depth_slider()

self.openContext(model)
self.openContext(
model.domain.class_var if model.domain is not None else None
)
# Restore item selection
if self.selected_index is not None:
index = self.list_view.model().index(self.selected_index)
Expand Down Expand Up @@ -324,15 +327,15 @@ def _update_target_class_combo(self):
values = list(ContinuousTreeNode.COLOR_METHODS.keys())
label.setText(label_text)
self.ui_target_class_combo.addItems(values)
self.ui_target_class_combo.setCurrentIndex(self.target_class_index)
# set it to 0, context will change if required
self.target_class_index = 0

def _clear_info_box(self):
self.ui_info.setText('No forest on input.')

def _clear_target_class_combo(self):
self.ui_target_class_combo.clear()
self.target_class_index = 0
self.ui_target_class_combo.setCurrentIndex(self.target_class_index)
self.target_class_index = -1

def _clear_depth_slider(self):
self.ui_depth_slider.parent().setEnabled(False)
Expand Down
15 changes: 15 additions & 0 deletions Orange/widgets/visualize/tests/test_owpythagorastree.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,21 @@ def test_changing_data_restores_depth_from_previous_settings(self):
self.send_signal(self.widget.Inputs.tree, forest.trees[1])
self.assertEqual(self.widget.ptree._depth_limit, 1)

def test_context(self):
iris_tree = TreeLearner()(Table("iris"))
self.send_signal(self.widget.Inputs.tree, self.titanic)
self.widget.target_class_index = 1

self.send_signal(self.widget.Inputs.tree, iris_tree)
self.assertEqual(0, self.widget.target_class_index)

self.widget.target_class_index = 2
self.send_signal(self.widget.Inputs.tree, self.titanic)
self.assertEqual(1, self.widget.target_class_index)

self.send_signal(self.widget.Inputs.tree, iris_tree)
self.assertEqual(2, self.widget.target_class_index)


if __name__ == "__main__":
unittest.main()
17 changes: 17 additions & 0 deletions Orange/widgets/visualize/tests/test_owpythagoreanforest.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,20 @@ def test_storing_selection(self):
output = self.get_output(self.widget.Outputs.tree)
self.assertIsNotNone(output)
self.assertIs(output.skl_model, self.titanic.trees[idx].skl_model)

def test_context(self):
iris = Table("iris")
iris_tree = RandomForestLearner()(iris)
iris_tree.instances = iris
self.send_signal(self.widget.Inputs.random_forest, self.titanic)
self.widget.target_class_index = 1

self.send_signal(self.widget.Inputs.random_forest, iris_tree)
self.assertEqual(0, self.widget.target_class_index)

self.widget.target_class_index = 2
self.send_signal(self.widget.Inputs.random_forest, self.titanic)
self.assertEqual(1, self.widget.target_class_index)

self.send_signal(self.widget.Inputs.random_forest, iris_tree)
self.assertEqual(2, self.widget.target_class_index)

0 comments on commit 4dccf71

Please sign in to comment.