コード例 #1
0
ファイル: test_aggregator.py プロジェクト: frankma/Risk
    def test_init(self):
        root = Node("root", None, None)
        child_1 = root.create_child("child1")
        grandchild_1 = child_1.create_child("grandchild1")
        grandchild_2 = child_1.create_child("grandchild2")
        child_2 = root.create_child("child2")
        child_3 = root.create_child("child3")
        grandchild_3 = child_3.create_child("grandchild3")

        tree = Tree("tree", root)
        tree.add_node_to_tree(grandchild_1)
        tree.add_node_to_tree(grandchild_2)
        tree.add_node_to_tree(child_2)
        tree.add_node_to_tree(grandchild_3)

        leaf_values = {grandchild_1: np.array([1.0, 2.0, 3.0]),
                       grandchild_2: np.array([1.0, 2.0, 3.0]),
                       grandchild_3: np.array([1.0, 2.0, 3.0]),
                       child_2: np.array([1.0, 2.0, 3.0])}

        nodes_for_store = {root, child_1, child_3}

        agg = Aggregator(tree, leaf_values, nodes_for_store)
        agg.process()
        pass
コード例 #2
0
    def test_drop_node_from_tree(self):
        root = Node("root", None, None)
        child = root.create_child("child")
        grandchild_1 = child.create_child("grandchild1")
        grandchild_2 = child.create_child("grandchild2")

        tree = Tree("tree", root)
        self.assertTrue(tree.is_node_on_tree(root))
        self.assertFalse(tree.is_node_on_tree(child))
        self.assertFalse(tree.is_node_on_tree(grandchild_1))
        self.assertFalse(tree.is_node_on_tree(grandchild_2))

        tree.add_node_to_tree(grandchild_1)
        self.assertTrue(tree.is_node_on_tree(root))
        self.assertTrue(tree.is_node_on_tree(child))
        self.assertTrue(tree.is_node_on_tree(grandchild_1))
        self.assertFalse(tree.is_node_on_tree(grandchild_2))
        tree.add_node_to_tree(grandchild_2)
        self.assertTrue(tree.is_node_on_tree(grandchild_2))

        tree.drop_node_from_tree(grandchild_2)
        self.assertTrue(tree.is_node_on_tree(root))
        self.assertTrue(tree.is_node_on_tree(child))
        self.assertTrue(tree.is_node_on_tree(grandchild_1))
        self.assertFalse(tree.is_node_on_tree(grandchild_2))

        tree.drop_node_from_tree(child)
        self.assertTrue(tree.is_node_on_tree(root))
        self.assertFalse(tree.is_node_on_tree(child))
        self.assertFalse(tree.is_node_on_tree(grandchild_1))
        self.assertFalse(tree.is_node_on_tree(grandchild_2))

        pass
コード例 #3
0
ファイル: test_node.py プロジェクト: frankma/Risk
 def test_node_level(self):
     root = Node('root')
     child = root.create_child('child')
     grand_child = child.create_child('grandchild')
     self.assertEqual(1, root.count_node_level())
     self.assertEqual(2, child.count_node_level())
     self.assertEqual(3, grand_child.count_node_level())
     pass
コード例 #4
0
 def drop_node_from_tree(self, node: Node):
     while self.is_node_on_tree(node):
         for child in node.get_children():
             if self.is_node_on_tree(child):
                 self.drop_node_from_tree(child)
         else:
             self._node_on_tree[node.count_node_level()].pop(
                 node.get_name_full(), None)
     pass
コード例 #5
0
 def is_node_on_tree(self, node: Node) -> bool:
     is_on_tree = self._root.__eq__(node.get_root())
     is_on_tree = is_on_tree and (self._root.__eq__(node.get_root()))
     is_on_tree = is_on_tree and (node.count_node_level()
                                  in self._node_on_tree)
     is_on_tree = is_on_tree and (
         node.get_name_full()
         in self._node_on_tree[node.count_node_level()].keys())
     return is_on_tree
コード例 #6
0
    def test_is_node_on_tree(self):
        root = Node("root", None, None)
        child = root.create_child("child")
        grandchild = child.create_child("grandchild")

        tree = Tree("tree", root)
        self.assertTrue(tree.is_node_on_tree(root))
        self.assertFalse(tree.is_node_on_tree(child))
        self.assertFalse(tree.is_node_on_tree(grandchild))
        pass
コード例 #7
0
ファイル: test_node.py プロジェクト: frankma/Risk
 def test_get_name_full(self):
     name_root = 'root'
     name_child = 'child'
     root = Node(name_root)
     child = root.create_child(name_child)
     self.assertEqual(name_root, root.get_name())
     self.assertEqual(name_root, root.get_name_full())
     self.assertEqual(name_child, child.get_name())
     self.assertEqual(name_root + Node.JOINER_NAME + name_child, child.get_name_full())
     pass
コード例 #8
0
    def test_parent_children_map(self):
        root = Node("root", None, None)
        child_1 = root.create_child("child1")
        grandchild_1 = child_1.create_child("grandchild1")
        grandchild_2 = child_1.create_child("grandchild2")
        child_2 = root.create_child("child2")
        child_3 = root.create_child("child3")
        grandchild_3 = child_3.create_child("grandchild3")

        tree = Tree("tree", root)
        tree.add_node_to_tree(grandchild_1)
        tree.add_node_to_tree(grandchild_2)
        tree.add_node_to_tree(child_2)
        tree.add_node_to_tree(grandchild_3)

        v = tree.scan_parent_children_map()

        pass
コード例 #9
0
    def test_gen_node_serial_number(self):
        root = Node("root", None, None)
        child_1 = root.create_child("child1")
        grandchild_1 = child_1.create_child("grandchild1")
        grandchild_2 = child_1.create_child("grandchild2")
        child_2 = root.create_child("child2")
        child_3 = root.create_child("child3")
        grandchild_3 = child_3.create_child("grandchild3")

        tree = Tree("tree", root)
        tree.add_node_to_tree(grandchild_1)
        tree.add_node_to_tree(grandchild_2)
        tree.add_node_to_tree(child_2)
        tree.add_node_to_tree(grandchild_3)

        tree.get_node_serial_number()

        pass
コード例 #10
0
 def add_node_to_tree(self, node: Node):
     if not self._root.__eq__(node.get_root()):
         raise ValueError('cannot add node from other tree')
     node_level = node.count_node_level()
     while not self.is_node_on_tree(node):
         parent_node = node.get_parent()
         if self.is_node_on_tree(parent_node):
             # add target node to the tree
             if node_level not in self._node_on_tree:
                 self._node_on_tree[node_level] = {}
             self._node_on_tree[node_level][node.get_name_full()] = node
             # update target parent node on tree
             parent_node_level = parent_node.count_node_level()
             self._node_on_tree[parent_node_level][
                 parent_node.get_name_full()].add_child(node)
         else:
             self.add_node_to_tree(node.get_parent())
     pass
コード例 #11
0
ファイル: test_node.py プロジェクト: frankma/Risk
    def test_is_my_parent(self):
        root = Node('root')
        child1 = root.create_child('child1')
        child2 = root.create_child('child2')
        grandchild = child1.create_child('grandchild')

        root_other = Node('other_root')
        child_other = root_other.create_child('child_1')

        self.assertFalse(root.is_my_parent(root))
        self.assertFalse(root.is_my_parent(child1))
        self.assertFalse(root.is_my_parent(child2))
        self.assertFalse(root.is_my_parent(grandchild))
        self.assertFalse(root.is_my_parent(root_other))
        self.assertFalse(root.is_my_parent(child_other))

        self.assertTrue(child1.is_my_parent(root))
        self.assertFalse(child1.is_my_parent(child1))
        self.assertFalse(child1.is_my_parent(child2))
        self.assertFalse(child1.is_my_parent(grandchild))
        self.assertFalse(child1.is_my_parent(root_other))
        self.assertFalse(child1.is_my_parent(child_other))

        self.assertTrue(child2.is_my_parent(root))
        self.assertFalse(child2.is_my_parent(child1))
        self.assertFalse(child2.is_my_parent(child2))
        self.assertFalse(child2.is_my_parent(grandchild))
        self.assertFalse(child2.is_my_parent(root_other))
        self.assertFalse(child2.is_my_parent(child_other))

        self.assertFalse(grandchild.is_my_parent(root))
        self.assertTrue(grandchild.is_my_parent(child1))
        self.assertFalse(grandchild.is_my_parent(child2))
        self.assertFalse(grandchild.is_my_parent(grandchild))
        self.assertFalse(grandchild.is_my_parent(root_other))
        self.assertFalse(grandchild.is_my_parent(child_other))

        pass
コード例 #12
0
ファイル: test_node.py プロジェクト: frankma/Risk
 def test_init(self):
     name = 'root'
     root = Node(name)
     self.assertIsInstance(root, Node)
     pass
コード例 #13
0
ファイル: test_node.py プロジェクト: frankma/Risk
 def test_is_root(self):
     root = Node('root')
     child = root.create_child('child')
     self.assertTrue(root.is_root())
     self.assertFalse(child.is_root())
     pass
コード例 #14
0
ファイル: test_node.py プロジェクト: frankma/Risk
    def test_add_node(self):
        root = Node('root', None, None)
        child_linked = root.create_child('child_linked')
        child_unlinked = Node('child', root, None)

        self.assertTrue(root.is_my_child(child_linked))
        self.assertTrue(child_linked.is_my_parent(root))
        self.assertFalse(root.is_my_child(child_unlinked))
        self.assertTrue(child_unlinked.is_my_parent(root))

        root.add_child(child_unlinked)
        self.assertTrue(root.is_my_child(child_linked))
        self.assertTrue(child_linked.is_my_parent(root))
        self.assertTrue(root.is_my_child(child_unlinked))
        self.assertTrue(child_unlinked.is_my_parent(root))

        pass