def test_tree_propagate(self): tree = TestTreeAccumulators.get_tree() input_array = np.asarray( ((1, 8), (2, 7), (3, 6), (4, 5), (5, 4), (6, 3), (7, 2), (8, 1)), dtype=np.float64) condition = np.asarray( (True, False, True, False, True, True, False, False)) output = hg.propagate_parallel(tree, input_array) ref = np.asarray( ((6, 3), (6, 3), (7, 2), (7, 2), (7, 2), (8, 1), (8, 1), (8, 1))) self.assertTrue(np.allclose(ref, output)) output = hg.propagate_parallel(tree, input_array, condition=condition) ref = np.asarray( ((6, 3), (2, 7), (7, 2), (4, 5), (7, 2), (8, 1), (7, 2), (8, 1))) self.assertTrue(np.allclose(ref, output)) output2 = hg.propagate_sequential(tree, input_array, condition) ref2 = np.asarray( ((8, 1), (2, 7), (7, 2), (4, 5), (7, 2), (8, 1), (7, 2), (8, 1))) self.assertTrue(np.allclose(ref2, output2)) output2 = hg.propagate_sequential_and_accumulate( tree, input_array, hg.Accumulators.sum) ref2 = np.asarray(((15, 12), (16, 11), (18, 9), (19, 8), (20, 7), (14, 4), (15, 3), (8, 1))) self.assertTrue(np.allclose(ref2, output2)) output2 = hg.propagate_sequential_and_accumulate( tree, input_array, hg.Accumulators.prod, condition) ref2 = np.asarray(((48, 24), (2, 7), (21, 12), (4, 5), (35, 8), (48, 3), (7, 2), (8, 1))) self.assertTrue(np.allclose(ref2, output2))
def reconstruct_leaf_data(tree, altitudes, deleted_nodes, leaf_graph=None): """ Each leaf of the tree takes the altitude of its closest non deleted ancestor. :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param altitudes: node altitudes of the input tree :param deleted_nodes: binary node weights indicating which nodes are deleted :param leaf_graph: graph of the tree leaves (optional, deduced from :class:`~higra.CptHierarchy`) :return: Leaf weights """ reconstruction = hg.propagate_sequential(tree, altitudes, deleted_nodes) leaf_weights = reconstruction[0:tree.num_leaves(), ...] if leaf_graph is not None: leaf_weights = hg.delinearize_vertex_weights(leaf_weights, leaf_graph) return leaf_weights
def reconstruct_leaf_data(tree, altitudes, deleted_nodes=None, leaf_graph=None): """ Each leaf of the tree takes the altitude of its closest non deleted ancestor. The root node is never deleted. In a component tree, leaves are always deleted. If :attr:`deleted_nodes` is ``None`` then its default value is set to `np.zeros((tree.numvertices(),)` (no nodes are deleted). :param tree: input tree (Concept :class:`~higra.CptHierarchy`) :param altitudes: node altitudes of the input tree :param deleted_nodes: binary node weights indicating which nodes are deleted (optional) :param leaf_graph: graph of the tree leaves (optional, deduced from :class:`~higra.CptHierarchy`) :return: Leaf weights """ if deleted_nodes is None: if tree.category() == hg.TreeCategory.PartitionTree: leaf_weights = altitudes[0:tree.num_leaves(), ...] elif tree.category() == hg.TreeCategory.ComponentTree: parents = tree.parents() leaf_weights = altitudes[parents[np.arange(tree.num_leaves())], ...] else: if tree.category() == hg.TreeCategory.ComponentTree: deleted_nodes[:tree.num_leaves()] = True reconstruction = hg.propagate_sequential(tree, altitudes, deleted_nodes) leaf_weights = reconstruction[0:tree.num_leaves(), ...] if leaf_graph is not None: leaf_weights = hg.delinearize_vertex_weights(leaf_weights, leaf_graph) return leaf_weights