Example #1
0
    def test_point_types(self):
        emptyTree = KDTree.create(dimensions=3)
        point1 = (2, 3, 4)
        point2 = [4, 5, 6]
        Point = collections.namedtuple('Point', 'x y z')
        point3 = Point(5, 3, 2)
        tree = KDTree.create([point1, point2, point3])
        res, dist = tree.search_nn((1, 2, 3))

        self.assertEqual(res, KDTree.KDNode((2, 3, 4)))
Example #2
0
    def test_invalid_child(self):
        """ Children on wrong subtree invalidate Tree """
        child = KDTree.KDNode((3, 2))
        child.axis = 2
        tree = KDTree.create([(2, 3)])
        tree.left = child
        self.assertFalse(tree.is_valid())

        tree = KDTree.create([(4, 1)])
        tree.right = child
        self.assertFalse(tree.is_valid())
Example #3
0
    def test_search_nn(self, nodes=100):
        points = list(islice(random_points(), 0, nodes))
        tree = KDTree.create(points)
        point = random_point()

        nn, dist = tree.search_nn(point)
        best, best_dist = self.find_best(tree, point)
        self.assertEqual(best_dist,
                         dist,
                         msg=', '.join(repr(p)
                                       for p in points) + ' / ' + repr(point))
Example #4
0
    def do_random_add(self, num_points=100):

        points = list(set(islice(random_points(), 0, num_points)))
        tree = KDTree.create(dimensions=len(points[0]))
        for n, point in enumerate(points, 1):

            tree.add(point)

            self.assertTrue(tree.is_valid())

            self.assertTrue(point in [node.data for node in tree.inorder()])

            nodes_in_tree = len(list(tree.inorder()))
            self.assertEqual(nodes_in_tree, n)
Example #5
0
    def test_remove_duplicates(self):
        """ creates a tree with only duplicate points, and removes them all """

        points = [(1, 1)] * 100
        tree = KDTree.create(points)
        self.assertTrue(tree.is_valid())

        random.shuffle(points)
        while points:
            point = points.pop(0)

            tree = tree.remove(point)

            # Check if the Tree is valid after the removal
            self.assertTrue(tree.is_valid())

            # Check if the removal reduced the number of nodes by 1 (not more, not less)
            remaining_points = len(points)
            nodes_in_tree = len(list(tree.inorder()))
            self.assertEqual(nodes_in_tree, remaining_points)
Example #6
0
    def do_random_remove(self):
        """ Creates a random tree, removes all points in random order """

        points = list(set(islice(random_points(), 0, 20)))
        tree = KDTree.create(points)
        self.assertTrue(tree.is_valid())

        random.shuffle(points)
        while points:
            point = points.pop(0)

            tree = tree.remove(point)

            # Check if the Tree is valid after the removal
            self.assertTrue(tree.is_valid())

            # Check if the point has actually been removed
            self.assertTrue(point not in [n.data for n in tree.inorder()])

            # Check if the removal reduced the number of nodes by 1 (not more, not less)
            remaining_points = len(points)
            nodes_in_tree = len(list(tree.inorder()))
            self.assertEqual(nodes_in_tree, remaining_points)
Example #7
0
    def test_search_knn(self):
        points = [(50, 20), (51, 19), (1, 80)]
        tree = KDTree.create(points)
        point = (48, 18)

        all_dist = []
        for p in tree.inorder():
            dist = p.dist(point)
            all_dist.append([p, dist])

        all_dist = sorted(all_dist, key=lambda n: n[1])

        result = tree.search_knn(point, 1)
        self.assertEqual(result[0][1], all_dist[0][1])

        result = tree.search_knn(point, 2)
        self.assertEqual(result[0][1], all_dist[0][1])
        self.assertEqual(result[1][1], all_dist[1][1])

        result = tree.search_knn(point, 3)
        self.assertEqual(result[0][1], all_dist[0][1])
        self.assertEqual(result[1][1], all_dist[1][1])
        self.assertEqual(result[2][1], all_dist[2][1])
Example #8
0
import KDTree

from timeit import default_timer as timer
#eisagwgi tou dataset
#tous xronous mporeite na tous deite metaferontas tis 3 entoles tou timer
import csv
results = []
with open("5000dataset.csv") as csvfile:
    reader = csv.reader(csvfile)  # change contents to floats
    for row in reader:  # each row is a list
        results.append(row)

#dimiourgia tou dentrou
tree = KDTree.create(dimensions=2)

#loop insert ta simeia tou csv
for x in range(5010):
    tree.insert([int(results[x][0]), int(results[x][1])])

if tree.is_balanced == False:
    tree = tree.rebalance()

#elegxos an to dentro einai balanced
if tree.is_balanced == False:
    tree = tree.rebalance()

print(tree)

#diagrafi enos simeiou
start = timer()
Example #9
0
 def test_remove_empty_tree(self):
     tree = KDTree.create(dimensions=2)
     tree.remove((1, 2))
     self.assertFalse(bool(tree))
Example #10
0
def random_tree(nodes=20, dimensions=3, minval=0, maxval=100):
    points = list(islice(random_points(), 0, nodes))
    tree = KDTree.create(points)
    return tree