def test_build_auto_axis_sequences(): """Extract non-redundant sequences from a tree. Cut trees at '+' edges. Unique children are considered as '<' in every case.""" mtg_t = test_mtg_build() from openalea.tree_statistic.trees import etrees for t in range(mtg_t.NbTrees()): msg = "Bad number of items in tree " + str(t) msg += ": " Tr = etrees.Tree(mtg_t.Tree(t)) T = etrees.Trees([Tr]) seq = T.BuildPySequences(False, True) assert seq nb_items = seq.build_vectors(True).nb_vector msg += str(nb_items) msg += " - should be :" msg += str(T.Size()) assert (nb_items == T.Size()), msg e = [v for v in Tr.Postorder() if not(Tr.IsRoot(v)) and \ (Tr.EdgeType(Tr.Parent(v), v) == "+") and \ (Tr.NbChildren(Tr.Parent(v)) > 1)] nb_seq = len(e) + 1 msg = "Bad number of sequences in tree " + str(t) msg += ": " + str(len(seq)) msg += " - should be: " + str(nb_seq) assert len(seq) == nb_seq, msg
def build_tree(vids): """Build a Tree from a list of vertices""" root = vids[0] nb_vertices = len(vids) tree_root = 0 mtg2tree = {} t = etrees.Tree(props(root), nb_vertices, tree_root) # Root management for vid in vids: edge_type = g.edge_type(vid) v = t.AddVertex(props(vid)) mtg2tree[vid] = v if vid != root: t.AddEdge(mtg2tree[g.parent(vid)],v, edge_type) return t, mtg2tree
def test_build_redundant_sequences(): """Extract redundant sequences from a tree. Follow every path from a leaf to root vertex.""" mtg_t = test_mtg_build() from openalea.tree_statistic.trees import etrees for t in range(mtg_t.NbTrees()): msg = "Bad number of items in tree " + str(t) msg += ": " Tr = etrees.Tree(mtg_t.Tree(t)) T = etrees.Trees([Tr]) seq = T.BuildPySequences(True) assert seq e = [v for v in Tr.Postorder() if Tr.NbChildren(v) == 0] nb_seq = len(e) msg = "Bad number of sequences in tree " + str(t) msg += ": " + str(len(seq)) msg += " - should be: " + str(nb_seq) assert len(seq) == nb_seq, msg
def Success1stCheck(T): """ Check whether < is always the first child in T :Parameters: `T` (trees.Trees) - Tree to be tested. :Returns: True if and only if < is always the first child in T """ import openalea.tree_statistic.trees.etrees as etrees ET = etrees.Tree(T) for v in ET.Breadthorder(): # get children of every vertex c = list(ET.Children(v)) has_successor = False # types of children ctypes = [ET.EdgeType(v, d) for d in c] if (('<' in ctypes) and (ctypes[0] != '<')): return False return True
def Success1stBuild(T, Reverse=False): """ Build a copy of T where < is always the first child. Reverse or not the other children. :Parameters: `T` (trees.Trees) - Tree to rearrange. `Reverse` (bool) - True iif the order of + children should be inversed :Returns: A pair composed by a :ref:`openalea.tree_statistic.trees.etrees` instance and by a dictionary d with correspondance between the vertices in T is returned (use as: d[T::Vertex] = Success1stBuild(T)::Vertex) :Examples: .. doctest:: >>> T = Success1stBuild(T, Reverse=False) """ import openalea.tree_statistic.trees.etrees as etrees ET = etrees.Tree(T) copy_vid_dic = True nvid_to_ovid = {} # conversion from new to old vids ovid_to_nvid = {} # conversion from new to old vids # conversion from Tree to MTG if any try: mtg_to_tree_vid_old = T.TreeVertex() tree_to_mtg_vid_old = T.MTGVertex() mtg_tid = T._mtg_tid() # root of mtg except Warning: copy_vid_dic = False # censored += [{}] mtg_to_tree_vid_new = {} tree_to_mtg_vid_new = {} # build a new etrees.Tree R = etrees.Tree(ET.Get(0), 0, 0) children_list = [] # list of list of children type_list = [] # list of list of edge types for v in ET.Breadthorder(): # get children of every vertex c = [] t = [] for d in ET.Children(v): if ET.EdgeType(v, d) == "<": c = [d] + c t = ["<"] + t elif Reverse: c = [d] + c t = ["+"] + t else: c += [d] t += ["+"] children_list.append(c) type_list.append(t) lindex = 0 # index in children_ and type_lists # add root vertex v = ET.Root() val = ET.Get(v) vid = R.AddVertex(val) tree_to_mtg_vid_new[vid] = ET.MTGVertex(v) mtg_to_tree_vid_new[tree_to_mtg_vid_new[vid]] = vid ovid_to_nvid[v] = vid nvid_to_ovid[vid] = v for v in ET.Breadthorder(): # add the children of v and their attributes llindex = 0 # index in children_ and type_[lindex] lists for d in children_list[lindex]: val = ET.Get(d) vid = R.AddVertex(val) tree_to_mtg_vid_new[vid] = ET.MTGVertex(d) mtg_to_tree_vid_new[tree_to_mtg_vid_new[vid]] = vid ovid_to_nvid[d] = vid nvid_to_ovid[vid] = d R.AddEdge(ovid_to_nvid[v], vid, type_list[lindex][llindex]) llindex += 1 lindex += 1 if copy_vid_dic: R._copy_vid_conversion(mtg_to_tree_vid_new, tree_to_mtg_vid_new) R._copy_mtg_tid(mtg_tid) # copy root of mtg return R, ovid_to_nvid return R, ovid_to_nvid