コード例 #1
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_traverse(self):
        #
        # Binary Tree
        #
        # Standard
        node = Tree(n_children=2)
        node.split()
        node.get_child(0).split()
        node.get_child(0).get_child(1).remove()
        addresses = {
            'breadth-first': [[], [0], [1], [0, 0]],
            'depth-first': [[], [0], [0, 0], [1]]
        }

        for mode in ['depth-first', 'breadth-first']:
            count = 0
            for leaf in node.traverse(mode=mode):
                self.assertEqual(leaf.get_node_address(),\
                                 addresses[mode][count],\
                                 'Binary tree traversal incorrect.')
                count += 1

        #
        # Quadtree
        #
        node = Tree(n_children=4)
        node.split()
        node.get_child(1).split()
        node.get_child(1).get_child(2).remove()
        addresses = [[], [0], [1], [2], [3], [1, 0], [1, 1], [1, 3]]
        count = 0
        for n in node.traverse(mode='breadth-first'):
            self.assertEqual(n.get_node_address(), addresses[count],\
                             'Incorrect address.')
            count += 1
コード例 #2
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_get_children(self):
        #
        # Binomial Tree
        #
        node = Tree(n_children=2)
        node.split()
        count = 0
        pos = [0, 1]
        for child in node.get_children():
            self.assertEqual(child.get_node_position(), pos[count],\
                             'Incorrect child.')
            count += 1

        # Reversed
        count = 0
        for child in node.get_children(reverse=True):
            self.assertEqual(child.get_node_position(), pos[-1 - count])
            count += 1

        # Flagged
        child_0 = node.get_child(0)
        child_0.mark(1)
        for child in node.get_children(flag=1):
            self.assertEqual(child, child_0, \
                             'The only marked child is child_0')
        #
        # Quadtree
        #
        node = Tree(n_children=4)
        node.split()
        count = 0
        pos = [0, 1, 2, 3]
        for child in node.get_children():
            self.assertEqual(child.get_node_position(), pos[count],\
                             'Incorrect child.')
            count += 1

        #
        # Now remove child
        #
        node.delete_children(position=0)
        count = 0
        for child in node.get_children():
            count += 1
        self.assertEqual(count, 3, 'There should only be 3 children left.')
        #
        # Node with no children
        #
        node = Tree(n_children=4)
        for child in node.get_children():
            print('Hallo')

        #
        # Try a logical statement
        #
        self.assertFalse(any([child.is_marked(1) for \
                              child in node.get_children()]), \
                         'No marked children because there are none.')
コード例 #3
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
 def test_remove(self):
     #
     # Remove child 2
     #
     node = Tree(n_children=4)
     node.split()
     child = node.get_child(2)
     child.remove()
     self.assertIsNone(node.get_child(2))
コード例 #4
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_find_node(self):
        node = Tree(n_children=4)
        address = [0, 2, 3, 0]
        # No-one lives at this address: return None
        self.assertIsNone(node.find_node(address))

        # Generate node with given address and try to recover it.
        for a in address:
            node.split()
            node = node.get_child(a)
        self.assertEqual(node, node.get_root().find_node(address))
コード例 #5
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_set_node_type(self):
        # Define a new node
        node = Tree(n_children=3)

        # Should complain about changing root to branch
        self.assertRaises(Exception, node.set_node_type, *('BRANCH'))

        # Split
        node.split()
        child = node.get_child(0)
        self.assertRaises(Exception, child.set_node_type, *('ROOT'))
        self.assertRaises(Exception, child.set_node_type, *('BRANCH'))
コード例 #6
0
    def test_find_node(self):
        t0 = Tree(2)
        t1 = Tree(3)
        forest = Forest([t0, t1])

        t0.split()
        t00 = t0.get_child(0)
        t00.split()
        t001 = t00.get_child(1)

        self.assertEqual(forest.find_node([0, 0, 1]), t001)
        self.assertIsNone(forest.find_node([4, 5, 6]))
コード例 #7
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_get_depth(self):
        # New node should have depth 0
        node = Tree(n_children=2)
        self.assertEqual(node.get_depth(), 0, 'ROOT node should have depth 0')

        # Split node 10 times
        for _ in range(10):
            node.split()
            node = node.get_child(1)
        # Last generation should have depth 10
        self.assertEqual(node.get_depth(), 10, 'Node should have depth 10.')
        self.assertEqual(node.get_root().get_depth(), 0, \
                         'ROOT node should have depth 0')
コード例 #8
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
 def test_delete_children(self):
     #
     # Delete child 2
     #
     node = Tree(n_children=4)
     node.split()
     node.delete_children(2)
     self.assertIsNone(node.get_child(2))
     #
     # Delete all children
     #
     node.delete_children()
     self.assertFalse(node.has_children())
コード例 #9
0
    def test_constructor(self):

        t0 = Tree(2)

        # Check whether all entries are Trees
        self.assertRaises(Exception, Forest, **{'trees': [t0, 0]})

        # Check whether Trees are roots
        t0.split()
        t00 = t0.get_child(0)
        self.assertRaises(Exception, Forest, **{'trees': [t0, t00]})

        t1 = Tree(4)
        forest = Forest([t0, t1])
        self.assertEqual(len(forest._trees), 2)
コード例 #10
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_n_children(self):
        for n in range(10):
            #
            # Define regular node with n children
            #
            node = Tree(n_children=n)

            # check if number of children correct
            self.assertEqual(node.n_children(),n,\
                             'Number of children incorrect')
            # split node and check if children inherit the same number of children
            node.split()
            for child in node.get_children():
                self.assertEqual(child.n_children(),n,\
                                 'Children have incorrect number of children.')
コード例 #11
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_get_child(self):
        # New node
        node = Tree(n_children=5)
        node.split()

        # Access child directly and mark it 1
        child_4 = node._children[4]
        child_4.mark(1)

        # Access child via function
        child_4_v1 = node.get_child(4)

        # Check whether it's the same child.
        self.assertTrue(child_4_v1.is_marked(1), 'Child 4 should be marked 1.')
        self.assertEqual(child_4_v1, child_4, 'Children should be the same.')
コード例 #12
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_tree_depth(self):
        # New node should have depth 0
        node = Tree(n_children=2)
        self.assertEqual(node.tree_depth(),0,\
                         'Tree should have depth 0')

        # Split node 10 times
        for i in range(10):
            node.split()
            node = node.get_child(1)

        # All nodes should have the same tree_depth
        self.assertEqual(node.tree_depth(),10,\
                         'Tree should have depth 10.')
        self.assertEqual(node.get_root().tree_depth(),10,\
                         'Tree should have depth 10.')
コード例 #13
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
    def test_get_node_type(self):
        # Define new ROOT node
        node = Tree(n_children=2)
        self.assertEqual(node.get_node_type(),'ROOT',\
                         'Output "node_type" should be "ROOT".')

        # Split node and assert that its child is a LEAF
        node.split()
        child = node.get_child(0)
        self.assertEqual(node.get_node_type(),'ROOT',\
                         'Output "node_type" should be "ROOT".')
        self.assertEqual(child.get_node_type(),'LEAF',\
                         'Output "node_type" should be "LEAF".')

        # Split the child and assert that it is now a BRANCH
        child.split()
        self.assertEqual(child.get_node_type(),'BRANCH',\
                         'Output "node_type" should be "BRANCH".')
コード例 #14
0
    def test_get_leaves(self):
        #
        # 1D
        #
        node = Tree(2)
        forest = Forest([node])
        leaves = forest.get_leaves()

        # Only a ROOT node, it should be the only LEAF
        self.assertEqual(leaves, [node], 'Cell should be its own leaf.')

        #
        # Split cell and L child - find leaves
        #
        node.split()
        l_child = node.get_child(0)
        l_child.split()
        leaves = forest.get_leaves()
        self.assertEqual(len(leaves), 3, 'Cell should have 3 leaves.')

        #
        # Depth first order
        #
        addresses_depth_first = [[0, 0, 0], [0, 0, 1], [0, 1]]
        leaves = forest.get_leaves(mode='depth-first')
        for i in range(len(leaves)):
            leaf = leaves[i]
            self.assertEqual(leaf.get_node_address(), addresses_depth_first[i],
                             'Incorrect order, depth first search.')
        #
        # Breadth first order
        #
        addresses_breadth_first = [[0, 1], [0, 0, 0], [0, 0, 1]]
        leaves = node.get_leaves(mode='breadth-first')
        for i in range(len(leaves)):
            leaf = leaves[i]
            self.assertEqual(leaf.get_node_address(),
                             addresses_breadth_first[i],
                             'Incorrect order, breadth first search.')

        node.get_child(0).get_child(0).mark('1')
        node.get_child(1).mark('1')
        node.make_rooted_subtree('1')
        leaves = node.get_leaves(subtree_flag='1')
        self.assertEqual(len(leaves),2, \
                         'There should only be 2 flagged leaves')

        #
        # 2D
        #
        node = Tree(4)
        forest = Forest([node])

        #
        # Split cell and SW child - find leaves
        #
        node.split()
        sw_child = node.get_child(0)
        sw_child.split()
        leaves = node.get_leaves()
        self.assertEqual(len(leaves), 7, 'Node should have 7 leaves.')

        #
        # Nested traversal
        #
        leaves = node.get_leaves()
        self.assertEqual(leaves[0].get_node_address(),[0,1], \
            'The first leaf in the nested enumeration should have address [1]')

        leaves = node.get_leaves(mode='depth-first')
        self.assertEqual(leaves[0].get_node_address(), [0,0,0], \
                         'First leaf in un-nested enumeration should be [0,0].')

        #
        # Merge SW child - find leaves
        #
        sw_child.delete_children()

        leaves = node.get_leaves()
        self.assertEqual(len(leaves), 4, 'Node should have 4 leaves.')

        #
        # Marked Leaves
        #
        node = Tree(4)
        node.mark(1)
        forest = Forest([node])
        self.assertTrue(node in forest.get_leaves(flag=1), \
                        'Node should be a marked leaf node.')
        self.assertTrue(node in forest.get_leaves(), \
                        'Node should be a marked leaf node.')

        node.split()
        sw_child = node.get_child(0)
        sw_child.split()
        sw_child.mark(1)
        self.assertEqual(node.get_leaves(subtree_flag=1), \
                         [sw_child], 'SW child should be only marked leaf')

        sw_child.remove()
        self.assertEqual(forest.get_leaves(subforest_flag=1), \
                         [node], 'node should be only marked leaf')

        #
        # Nested traversal
        #
        node = Tree(4)
        node.split()
        forest = Forest([node])
        for child in node.get_children():
            child.split()

        node.get_child(1).mark(1, recursive=True)
        node.get_child(3).mark(1)
        forest.root_subtrees(1)
        leaves = forest.get_leaves(subforest_flag=1)
        self.assertEqual(len(leaves), 7, 'This tree has 7 flagged LEAF nodes.')
        self.assertEqual(leaves[0], node.get_child(0),
                         'The first leaf should be the NE child.')
        self.assertEqual(leaves[3],
                         node.get_child(1).get_child(0),
                         '4th flagged leaf should be SE-NW grandchild.')
コード例 #15
0
ファイル: test_tree.py プロジェクト: hvanwyk/quadmesh
 def test_split(self):
     node = Tree(n_children=5)
     self.assertFalse(node.has_children(), 'Node should not have children.')
     node.split()
     self.assertTrue(node.has_children(), 'Node should now have children.')
コード例 #16
0
    def test_traverse(self):
        #
        # Binary Tree
        #
        # Standard
        node = Tree(2)
        forest = Forest([node])

        node.split()
        node.get_child(0).split()
        node.get_child(0).get_child(1).remove()
        addresses = {
            'breadth-first': [[0], [0, 0], [0, 1], [0, 0, 0]],
            'depth-first': [[0], [0, 0], [0, 0, 0], [0, 1]]
        }

        for mode in ['depth-first', 'breadth-first']:
            count = 0
            for leaf in forest.traverse(mode=mode):
                self.assertEqual(leaf.get_node_address(),
                                 addresses[mode][count]),

                count += 1
        #
        # QuadTree
        #
        node = Tree(4)
        forest = Forest([node])
        node.split()
        node.get_child(1).split()
        node.get_child(1).get_child(2).remove()
        addresses = [[0], [0, 0], [0, 1], [0, 2], [0, 3], [0, 1, 0], [0, 1, 1],
                     [0, 1, 3]]
        count = 0
        for n in node.traverse(mode='breadth-first'):
            self.assertEqual(n.get_node_address(), addresses[count],\
                             'Incorrect address.')
            count += 1

        #
        # Forest with one quadtree and one bitree
        #
        bi = Tree(2)
        quad = Tree(4)
        forest = Forest([bi, quad])
        bi.split()
        bi.get_child(0).split()
        quad.split()
        addresses = {
            'breadth-first': [[0], [1], [0, 0], [0, 1], [1, 0], [1, 1], [1, 2],
                              [1, 3], [0, 0, 0], [0, 0, 1]],
            'depth-first': [[0], [0, 0], [0, 0, 0], [0, 0, 1], [0, 1], [1],
                            [1, 0], [1, 1], [1, 2], [1, 3]]
        }

        for mode in ['depth-first', 'breadth-first']:
            count = 0
            for leaf in forest.traverse(mode=mode):
                self.assertEqual(leaf.get_node_address(),
                                 addresses[mode][count])

                count += 1
コード例 #17
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 21 14:57:53 2021

@author: hans-werner
"""

from mesh import Tree
import matplotlib.pyplot as plt
import numpy as np

tree = Tree(n_children=2, regular=True)
tree.split()
for c in tree.get_children():
    c.split()

    for cc in c.get_children():
        cc.split()

print(tree.get_depth())

leaves = tree.get_leaves()
leaves.pop()

w = 0
max_depth = 0
for leaf in leaves:
    ld = leaf.get_depth()
    w += 2**(-leaf.get_depth())
    if ld > max_depth: