def test_compare_old_to_new_method_to_create_trees(self): """ tree created with old method should be equal to tree created with new method """ nodes = util.generate_sequence_of_points(2, 2) tree1 = kdtree.createNewTree(nodes) kdtree.visualize(tree1) sel_axis = (lambda axis: axis) tree2 = kdtree.createNewTree([[0.5, 0.5]],axis = 0, sel_axis= sel_axis) tree2.split2([0.25, 0.5], axis = 1) tree2.split2([0.75, 0.5], axis = 1) #left tree2.split2([0.25, 0.25], axis = 0, sel_axis = sel_axis) tree2.split2([0.25, 0.75], axis = 0, sel_axis = sel_axis) #right tree2.split2([0.75, 0.25], axis = 0, sel_axis = sel_axis) tree2.split2([0.75, 0.75], axis = 0, sel_axis = sel_axis) kdtree.visualize(tree2) for n in zip(kdtree.level_order(tree1), kdtree.level_order(tree2)): self.assertEqual(n[0].data, n[1].data, "elements not equal") if n[0].data is not None and n[1].data is not None: self.assertEqual(n[0].axis, n[1].axis, "elements not equal")
def test_createAndLableTree(self): ''' Create new tree with new ids starting from 0''' print "---------- test createAndLabelTree 1--------" no1dN = [] points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]]) util.splitN(points, 0, 0, 6, no1dN) tree1 = kdtree.createNewTree(no1dN) label=0 for n in kdtree.level_order(tree1): self.assertIsNotNone(kdtree.getNode(tree1, label), "1: node with label: "+ str(label) + " not found in tree") label+=1 kdtree.visualize(tree1) print "---------- test createAndLabelTree 2--------" no2dN = [] points = numpy.array([[0.0, 0.01], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]]) util.splitN(points, 0, 0, 6, no2dN) tree2 = kdtree.createNewTree(no2dN) kdtree.visualize(tree2) label=0 for n in kdtree.level_order(tree2): self.assertIsNotNone(kdtree.getNode(tree2, label), "2: node with label: "+ str(label) + " not found in tree") label+=1 self.assertNotEqual(tree1, tree2, "trees have to be different")
def test_showQ(self): import matplotlib.pyplot as plt import time print "---------- DisplayTreeTest ----------" #plt.figure(self.fig_values.number) maxLevel=2 no1dN = [] points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]]) util.splitN(points, 0, 0, maxLevel, no1dN) tree = kdtree.createNewTree(no1dN) numberOfStates= tree.getHighestNodeId numberOfActions = 4 kdtree.visualize(tree) Q = numpy.ones((100,numberOfActions)) n = tree.get_path_to_best_matching_node([0.75, 0.75])[-1] print n.label n.split2([0.85, 0.75], axis=0, sel_axis = (lambda axis: axis)) kdtree.visualize(tree) # only leaves are shown! # States are positioned like in the tree i.e. xy axes splits in tree represent xy position in coord system # 0, 0 is bottom left, x increases to the right # action 0 is to the left # Q[State][action] Q[3][0] = 0 # bottom left, action left # Q[5][1] = 0.1 # above Q[2] (in y direction), right # Q[58][2] = 0.1 #right top corner, down # Q[4][0] = 0.5 kdtree.plotQ2D(tree, min_coord=[0, 0], max_coord=[1, 1],Values = Q, plt=plt, plot="Q") time.sleep(5)
def test_add_node_and_pickle_tree(self): print "-------------- test_pickle_tree ---------------" nodes = [] netDimension = 3 levels = 3 sequence = ["".join(seq) for seq in itertools.product("01", repeat=netDimension)] points_temp= numpy.array([list(s) for s in sequence]) points = numpy.array([map(float, f) for f in points_temp]) util.splitN(points, 0, 0, levels, nodes) tree = kdtree.createNewTree(nodes) for i in range(10): tree.split2([random.random(), random.random(), random.random()], axis=random.randint(0, netDimension-1)) points_tree = [(d.data, d.axis) for d in kdtree.level_order(tree) if d.data is not None] kdtree.visualize(tree) kdtree.save( tree, "save_tree_test.pkl" ) tree_loaded = kdtree.load("save_tree_test.pkl") points_tree_loaded = [(d.data, d.axis) for d in kdtree.level_order(tree_loaded) if d.data is not None] numpy.testing.assert_array_equal(points_tree, points_tree_loaded, "trees not equal?")
def test_plotTree(self): # function to chose next spillting axis sel_axis = (lambda axis: axis) #create tree, first node splits in x direction tree = kdtree.createNewTree([[0.5, 0.5]],axis = 0, sel_axis= sel_axis) tree.split2([0.4, 0.5], axis = 0, sel_axis = sel_axis) #add right node root node and left node to new node tree.split2([0.6, 0.5], axis = 1, sel_axis = sel_axis) tree.split2([0.7, 0.4], axis = 0, sel_axis = sel_axis) print "node before: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label print "node before: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label #add a node tree.split2([0.3, 0.6], axis = 1, sel_axis = sel_axis) print "node after: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label print "node after: ", tree.get_path_to_best_matching_node([0.3, 0.5])[-1].label kdtree.visualize(tree) # img=mpimg.imread("test_unconstraint_tree.png") # plt.imshow(img) # Compare to image test_unconstraint_tree.png kdtree.plot2D(tree, plt=plt)
def example_kdtree(): # An example of how to use kdtree print("*" * 60) print( "*" * 15, "An Example of kdtree's Usage", "*" * 15) print( "*" * 60) point = [(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2), (8, 8)] point1 = [] for i in point: point1.append({0: i[0], 1: i[1]}) print ("point list") print (point1) # Create a kdtree root = kdtree.create(point1, dimensions=2) # Visualize the kdtree print ("visualize the kd-tree: ") kdtree.visualize(root) # Search for k-nearsest neighbor by given p-Minkowski distance f = ds.EuclideanDistance ans = root.search_knn(point={0: 7, 1: 3}, k=4, dist=f) print("The 3 nearest nodes to point (7, 3) are:") print(ans) print("The nearest node to the point is:") print(ans[0][0].data) ans = root.search_inrange(point={0: 7, 1: 3}, r=4, dist=f) print("the nodes in range are:") print(ans) print("The nearest node to the point is:") print(ans[0][0].data)
def visual(self): global data, dim, vtree, new, tree, type, newtree print( colored( "\n----------------------------\nV I S U A L\n----------------------------", "red")) kdtree.visualize(vtree) input("Press Enter to continue...\n") tree.visual_tree() input("Press Enter to continue...\n") main_menu(tree)
def test_getNode(self): print "---------- test getNode --------" listSplitPoints = [] points = numpy.array([[0.0, 0.0],[0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]]) util.splitN(points, 0,0,6, listSplitPoints) tree = kdtree.createNewTree(listSplitPoints) util.activate(tree, 6) kdtree.visualize(tree) nodeLabel = 117 node = kdtree.getNode(tree, nodeLabel) self.assertEqual( node.label, nodeLabel, "returned wrong node") del tree
def test_splitNode(self): ''' find the best matching node and split it, then find the best matching node again. Check if point lies in new generated node''' print "---------- test splitNode --------" #create tree with 2 levels listSplitPoints = [] points = numpy.array([[0.0, 0.0],[0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]]) util.splitN(points, 0,0,5, listSplitPoints) tree2dN = kdtree.createNewTree(listSplitPoints) util.activate(tree2dN, 2) #points point1 = [0.9,0.1] point2 = [0.1,0.9] kdtree.visualize(tree2dN) # split print "found: ", tree2dN.get_path_to_best_matching_node(point1)[-1] tree2dN.get_path_to_best_matching_node(point1)[-1].activate_subnodes() kdtree.visualize(tree2dN) tree2dN.get_path_to_best_matching_node(point1)[-1].activate_subnodes() kdtree.visualize(tree2dN) print "data: ", tree2dN.get_path_to_best_matching_node(point1)[-1].data self.assertEqual( tree2dN.get_path_to_best_matching_node(point1)[-1].data, [0.875, 0.125], "wrong node") tree2dN.get_path_to_best_matching_node(point2)[-1].activate_subnodes() tree2dN.get_path_to_best_matching_node(point2)[-1].activate_subnodes() self.assertEqual( tree2dN.get_path_to_best_matching_node(point2)[-1].data, [0.125, 0.875], "wrong node") del tree2dN
def test_add_empty_nodes_with_label_when_splitting(self): """ When a node is split along a certain axis, then this split should be active immediately. Create tree, split right and left node, try to find matching node """ print "----- test_add_empty_nodes_with_label_when_splitting -----" sel_axis = (lambda axis: axis) #create tree, first node splits in x direction tree = kdtree.createNewTree([[0.5, 0.5]],axis = 0, sel_axis= sel_axis) kdtree.visualize(tree) point_left = [0.4, 0.5] tree.split2(point_left, axis = 0) kdtree.visualize(tree) point1 = [0.3, 0.5] found_node = tree.get_path_to_leaf(point1)[-1] correct_node1 = 3 self.assertEqual(found_node.label, correct_node1, "Not correct node found") point_right = [0.6, 0.5] tree.split2(point_right, axis = 1) kdtree.visualize(tree) point2 = [0.6, 0.7] found_node = tree.get_path_to_leaf(point2)[-1] correct_node2 = 6 self.assertEqual(found_node.label, correct_node2, "Not correct node found") print "----- end: test_add_empty_nodes_with_label_when_splitting -----"
def example_kdtree(): # An example of how to use kdtree print "*" * 60 print "*" * 15, "An Example of kdtree's Usage", "*" * 15 print "*" * 60 point = [(2, 3), (5, 4), (9, 6), (4, 7), (8, 1), (7, 2), (8, 8)] point1 = [] for i in point: point1.append({1: i[0], 2: i[1]}) print "point list" print point1 # Create a kdtree root = kdtree.create(point1, dimensions=2) # Visualize the kdtree print "visualize the kd-tree: " kdtree.visualize(root) # Search for k-nearsest neighbor by given p-Minkowski distance f = ds.EuclideanDistance ans = root.search_knn(point={1: 7, 2: 3}, k=10, dist=f) print "The 3 nearest nodes to point (7, 3) are:" print ans print "The nearest node to the point is:" print ans[0][0].data
tree = tree.remove( (5, 4, 3) ) # Retrieving the Tree in inorder print(list(tree.inorder())) # Retrieving the Tree in level order print(list(kdtree.level_order(tree))) # Find the nearest node to the location (1, 2, 3) tree.search_nn( (1, 2, 3) ) # Add a point to make the tree more interesting tree.add( (10, 2, 1) ) # Visualize the Tree kdtree.visualize(tree) # Take the right subtree of the root subtree = tree.right # and detatch it tree.right = None kdtree.visualize(tree) kdtree.visualize(subtree) # and re-attach it tree.right = subtree kdtree.visualize(tree) # Add a node to make the tree unbalanced
def visualize_kdtree(self): """ Visualize the kdtree. """ kdtree.visualize(self.kdtree)
# tree2dN = kdtree.create(no2dN) # kdtree.visualize(tree2dN) # kdtree.plot2D(tree2dN) no3dN = [] #points = numpy.array([[0.0, 0.0, 0.0],[0.0 , 0.0, 1.0], [ 0.0, 1.0, 0.0], [ 0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) points = numpy.array([[0.0, 0.0, 0.0, 0.0],[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 1.0, 1.0], [0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 1.0], [1.0, 0.0, 0.0, 0.0], [1.0, 0.0, 0.0, 1.0], [1.0, 0.0, 1.0, 0.0], [1.0, 0.0, 1.0, 1.0], [1.0, 1.0, 0.0, 0.0], [1.0, 1.0, 0.0, 1.0], [1.0, 1.0, 1.0, 0.0], [1.0, 1.0, 1.0, 1.0]]) util.splitN(points, 0,0,5, no3dN) #print "no3dN:", no3dN print "Number of nodes3dN: ", len(no3dN) point = [1,0,0,0] tree3dN = kdtree.create(no3dN) util.activate(tree3dN, 2) print tree3dN.get_path_to_best_matching_node(point)[-1].label kdtree.visualize(tree3dN) #kdtree.plot2D(tree3dN) # # # no3dN_test = [] # points = numpy.array([[0.0, 0.0, 0.0],[0.0 , 0.0, 1.0], [ 0.0, 1.0, 0.0], [ 0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) # splitN_test(points, 0,0,7, no3dN_test) # # assert all(x in no3dN_test for x in no3dN), "NOT EQUAL !!!" #kdtree.plot2D(tree) # print [ n.label for n in tree.get_path_to_best_matching_node((0.1,0.1))] # tree.get_path_to_best_matching_node((0.1,0.1))[-1].split() # # print [ n.label for n in tree.get_path_to_best_matching_node((0.1,0.1))]
print "no:", no print "Number of nodes: ", len(no) tree = kdtree.create(no) #kdtree.visualize(tree) #print [ n.label for n in tree.get_path_to_best_matching_node((0.6,0.4))] print [ str(n.label) +" " + str(n.height()) for n in tree.get_path_to_best_matching_node((0.6, 0.4))] print [ n.label for n in tree.get_path_to_best_matching_node((0.6, 0.1))] print [ n.label for n in tree.get_path_to_best_matching_node((0.175, 0.6))] kdtree.visualize(tree) # unbalanced tree tree2=kdtree.create([ (1, 2), (2, 3) ]) node=tree2.search_nn((1.1,2.1)) print node[0].label node[0].add((1,1)) kdtree.visualize(tree2) print "tree2.height(): ", tree2.height() print "node[0].level(tree2): ", node[0].level(tree2) print "tree2.level(tree2): ", tree2.level(tree2)
import kdtree if __name__ == '__main__': t = kdtree.create([[1, 1], [5, 4], [6, 1]]) #t = kdtree.create([[500,0], [ 0,10000], [ 1000,10001]]) kdtree.visualize(t) print(t.search_nn([4, 1])) #print(t.search_nn([ 1000,5000]))
# ax = fig.add_subplot(111) # patch = patches.PathPatch(path, facecolor='orange', lw=2) # ax.add_patch(patch) # ax.set_xlim(-2,2) # ax.set_ylim(-2,2) # plt.show() no2dN = [] points = numpy.array([[0.0, 0.0], [0.0, 1.0], [ 1.0, 0.0], [1.0, 1.0]]) util.splitN(points, 0, 0, 6, no2dN) tree2dN = kdtree.create(no2dN) util.activate(tree2dN, 4) kdtree.visualize(tree2dN) V = numpy.random.rand(len(no2dN),1) kdtree.plotQ2D(tree2dN, Values=V) #kdtree.plot2D(tree2dN) #kdtree.plot2DUpdate(tree2dN) # # # no3dN_test = [] # points = numpy.array([[0.0, 0.0, 0.0],[0.0 , 0.0, 1.0], [ 0.0, 1.0, 0.0], [ 0.0, 1.0, 1.0], [1.0, 0.0, 0.0], [1.0, 0.0, 1.0], [1.0, 1.0, 0.0], [1.0, 1.0, 1.0]]) # splitN_test(points, 0,0,7, no3dN_test) # # assert all(x in no3dN_test for x in no3dN), "NOT EQUAL !!!"