22import math
33import unittest
44
5- from Orange .classification import TreeLearner as TreeClassificationLearner
5+ from os import path
6+
67from Orange .data import Table
7- from Orange .regression import TreeLearner as TreeRegressionLearner
8+ from Orange .modelling import TreeLearner
89from Orange .widgets .tests .base import WidgetTest , WidgetOutputsTestMixin
910from Orange .widgets .visualize .owpythagorastree import OWPythagorasTree
1011from Orange .widgets .visualize .pythagorastreeviewer import (
@@ -93,7 +94,7 @@ def setUpClass(cls):
9394 WidgetOutputsTestMixin .init (cls )
9495
9596 # Set up for output tests
96- tree = TreeClassificationLearner ()
97+ tree = TreeLearner ()
9798 cls .model = tree (cls .data )
9899 cls .model .instances = cls .data
99100
@@ -102,11 +103,11 @@ def setUpClass(cls):
102103
103104 # Set up for widget tests
104105 titanic_data = Table ('titanic' )[::50 ]
105- cls .titanic = TreeClassificationLearner (max_depth = 1 )(titanic_data )
106+ cls .titanic = TreeLearner (max_depth = 1 )(titanic_data )
106107 cls .titanic .instances = titanic_data
107108
108109 housing_data = Table ('housing' )[:10 ]
109- cls .housing = TreeRegressionLearner (max_depth = 1 )(housing_data )
110+ cls .housing = TreeLearner (max_depth = 1 )(housing_data )
110111 cls .housing .instances = housing_data
111112
112113 def setUp (self ):
@@ -283,3 +284,46 @@ def test_label_on_tree_connect_and_disconnect(self):
283284 self .assertNotRegex (
284285 self .widget .info .text (), regex ,
285286 'Initial info should not contain node or depth info' )
287+
288+ def test_tree_determinism (self ):
289+ """Check that the tree is drawn identically upon receiving the same
290+ dataset with no parameter changes."""
291+ n_tries = 10
292+
293+ def _check_all_same (data ):
294+ """Check that all the elements within an iterable are identical."""
295+ iterator = iter (data )
296+ try :
297+ first = next (iterator )
298+ except StopIteration :
299+ return True
300+ return all (first == rest for rest in iterator )
301+
302+ # Make sure the tree are deterministic for iris
303+ scene_nodes = []
304+ for i in range (n_tries ):
305+ self .send_signal (self .signal_name , self .signal_data )
306+ scene_nodes .append ([n .pos () for n in self .get_visible_squares ()])
307+ for node_row in zip (* scene_nodes ):
308+ self .assertTrue (
309+ _check_all_same (node_row ),
310+ "The tree was not drawn identically in the %d times it was "
311+ "sent to widget after receiving the iris dataset." % n_tries
312+ )
313+
314+ # Make sure trees are deterministic with data where some variables have
315+ # the same entropy
316+ data_same_entropy = Table (path .join (
317+ path .dirname (__file__ ), "../../tests/datasets/same_entropy.tab" ))
318+ data_same_entropy = TreeLearner ()(data_same_entropy )
319+ scene_nodes = []
320+ for i in range (n_tries ):
321+ self .send_signal (self .signal_name , data_same_entropy )
322+ scene_nodes .append ([n .pos () for n in self .get_visible_squares ()])
323+ for node_row in zip (* scene_nodes ):
324+ self .assertTrue (
325+ _check_all_same (node_row ),
326+ "The tree was not drawn identically in the %d times it was "
327+ "sent to widget after receiving a dataset with variables with "
328+ "same entropy." % n_tries
329+ )
0 commit comments