def setup_func(): global T, tv, nb_trees, tree_list, mtg_name, g # build some random initial tree stat_tool.plot.DISABLE_PLOT = True inf_bound = 0 sup_bound = 3 distrib = stat_tool.Uniform(inf_bound, sup_bound) max_depth = 3 max_size = 10 nbtrees = 40 # define a set of trees tree_list = [] tv = [1., 0, 1, 2.] # trees.TreeValue([1., 0]) R = trees.TreeStructure(distrib, max_size, max_depth) tmp_tree = trees.Tree(tv, R) n = 1 tree_list.append(trees.Tree(tmp_tree)) while n < nbtrees: n = n + 1 R.Simulate(distrib, max_size, max_depth) tmp_tree = trees.Tree(tv, R) tree_list.append(trees.Tree(tmp_tree)) distrib_list = [] for i in range(tmp_tree.NbInt()): distrib_list.append(distrib) for n in range(len(tree_list)): tree_list[n].Simulate(distrib_list) T = trees.Trees(tree_list) nb_trees = nbtrees mtg_name = "data/sample_mtg_forest.mtg" g = build_mtg() return T, tv, nb_trees, tree_list, mtg_name, g
def setup_func(): global hmt_name, hmt_name, T, HInit mtg_name = "data/sim3.mtg" hmt_name = "data/hmot3_init.hmt" HInit = hmt.HiddenMarkovIndOutTree(hmt_name) T = trees.Trees(mtg_name) return hmt_name, mtg_name, T, HInit
def test_attribute_function_failure(): """use bad attribute function in building trees""" filter = lambda x: True attributes = ["anything"] try: T = trees.Trees(mtg_name, filter, attributes, attributes, scale=2) except TypeError, t: print t
def test_attribute_type_failure(): """use bad attribute types in building trees"sample_mtg_forest.txt""" filter = lambda x: x < 6 attributes = ["anything"] try: T = trees.Trees(mtg_name, filter, attributes, [lambda x: "z"], scale=2) except TypeError, t: print t
def test_attribute_name_failure(): """use bad attribute names in building trees""" filter = lambda x: x < 6 f = lambda x: 1 try: T = trees.Trees(mtg_name, filter, [f], [f], scale=2) except TypeError, t: print t
def test_filter_failure(): """use bad filtering function in building trees""" f = lambda x: 1 attributes = ["anything"] try: T = trees.Trees(mtg_name, attributes, attributes, [f], scale=2) except TypeError, t: print t
def test_nonrecursive_filter_failure(): """use nonrecursive filtering function in building trees""" f = lambda x: 1 attributes = ["anything"] try: T = trees.Trees(mtg_name, lambda x: x != 24, attributes, [f], scale=2) except IndexError, i: print i
def test_build_attribute_failure(): """use inconsistent attribute number in building trees""" f = lambda x: 1 filter = lambda x: True try: T = trees.Trees(mtg_name, filter, [], [f], scale=2) except ValueError, v: print v
def test_attribute_name_failure(): """use bad attribute name in building trees""" """does not seem to be run after all""" f = lambda x: 1 filter = lambda x: True try: T = trees.Trees(mtg_name, filter, [f], [f], scale=2) except TypeError, t: print t
def test_filter_type_failure(): """use incorrect type return for filter in building trees""" filt = lambda x: "a" f = lambda x: 1 attributes = ["anything"] try: T = trees.Trees(mtg_name, filt, attributes, [f], scale=2) except TypeError, t: print t
def test_build_mtg_filter(): """Read a Tree from a MTG with filter and custom attributes""" import openalea.mtg as mtg g = mtg.MTG(mtg_name) f = lambda x: g.node(x).Length * 3 * (g.node(x).Diam / 2)**2 filter = lambda x: x < 6 attributes = ["anything"] T = trees.Trees(mtg_name, filter, attributes, [f], scale=2) msg = "Error building tree from MTG using filters" assert T, msg assert T.NbTrees() == 1
def test_mtg_build(): """constructor from a MTG""" msg1 = "Bad number of trees: " msg2 = "Bad number of variables: " mtg_t = trees.Trees(mtg_name) msg1 += str(mtg_t.NbTrees()) msg1 += " - should be 3" msg2 += str(mtg_t.NbVariables()) msg2 += " - should be 4" assert mtg_t assert (mtg_t.NbTrees() == 3), msg1 assert (mtg_t.NbVariables() == 4), msg2 return mtg_t
def test_tree_structures(): """Test TreeStructure class""" msg1 = "Bad number of vertices for TreeStructure: " msg2 = "Bad correspondence from MTG to Tree vertices: " mtg_t = test_mtg_build() TrMTG = mtg_t.Tree(0) # Copy tree structures Tr = trees.TreeStructure(TrMTG) assert Tr.Size() == TrMTG.Size(), msg1 + str(Tr.Size()) assert Tr.TreeVertex() == TrMTG.TreeVertex(), msg2 assert Tr.MTGVertex() == TrMTG.MTGVertex(), msg2 # Build trees with same structures as mtg_t tree_list = [] attributes = mtg_t.Attributes() for t in range(mtg_t.NbTrees()): TrMTG2 = mtg_t.Tree(t) Tr = trees.TreeStructure(TrMTG2) tree_list += [trees.Tree([0], Tr)] T0 = trees.Trees(tree_list, attribute_names=[attributes[0]]) Tr = T0.Tree(0) assert Tr.Size() == TrMTG.Size(), msg1 + str(Tr.Size()) assert Tr.TreeVertex() == TrMTG.TreeVertex(), msg2 assert Tr.MTGVertex() == TrMTG.MTGVertex(), msg2
while n < nbtrees: n = n+1 R.Simulate(children_distrib, max_size, max_depth) tmp_tree = trees.Tree([1., 0], R) tree_list.append(trees.Tree(tmp_tree)) distrib_list = [] # simulation of the labels for i in range(tmp_tree.NbInt()): distrib_list.append(attributes_distrib) for n in range(len(tree_list)): tree_list[n].Simulate(distrib_list) # initialize a Trees object T = trees.Trees(tree_list) print T # T.Plot() # T.Display() # extract histograms print "Histogram of the tree size: " histo = T.ExtractHistogram("Size") print(histo.display(Detail=2)) print "Histogram of the number of children: " histo = T.ExtractHistogram("NbChildren") print(histo.display(Detail=2)) print "Histogram of marginal distribution: " histo = T.ExtractHistogram("Value", 1) print(histo.display(Detail=2)) # save trees T.Save("fake_mtg_forest.txt", True)
tree_list.append(trees.Tree(tmp_tree)) while n < nbtrees: n = n + 1 R.Simulate(distrib, max_size, max_depth) tmp_tree = trees.Tree(tv, R) tree_list.append(trees.Tree(tmp_tree)) distrib_list = [] for i in range(tmp_tree.NbInt()): distrib_list.append(distrib) for n in range(len(tree_list)): tree_list[n].Simulate(distrib_list) # initialize a Trees object T = trees.Trees(tree_list) H = _hmt.HmtAsciiRead("hmot_np_2s.hmt") print "A HMT read from file 'hmot_np_2s.hmt':" print H.Display(False) T = H.Simulate(3, 20, 3, True) print "Set of simulated trees:" print T.Display(False) # The remaining part of this test does not work : Cpp and python prototype # do not seem to match each other. I comment this piece of code and the # author should fix it. TC, Dec 2008 #print "\nEstimated HMT (initialization by HMT): \n " #EH = T.EstimationCiHmot(H, True, cstat_tools.RestorationAlgorithm.VITERBI, 20, True) #print EH.Display(False) #print "\nEstimated HMT (initialization by self-transition): \n " #EH = T.EstimationCiHmot(2, False, False, cstat_tools.RestorationAlgorithm.VITERBI, 0.9999, 10)
tmp_tree=trees.Tree(tv, R) n=1 tree_list.append(trees.Tree(tmp_tree)) while n < nbtrees: n=n+1 R.Simulate(distrib, max_size, max_depth) tmp_tree=trees.Tree(tv, R) tree_list.append(trees.Tree(tmp_tree)) distrib_list=[] for i in range(tmp_tree.NbInt()): distrib_list.append(distrib) for n in range(len(tree_list)): tree_list[n].Simulate(distrib_list) # initialize a Trees object T=trees.Trees(tree_list) print "A set of trees with variable types", T.Types(), \ " (initialized by ", tv, "):" print T print "Display Trees object with detail level 2:" T.Display(Detail=2) print "Display Trees object with data and detail level 1:" T.Display(ViewPoint="DATA", Detail=1) T.ExtractHistogram("Value", variable=1).plot() T.Plot(ViewPoint="FirstOccurrenceRoot", variable=1) T.MPlot(ViewPoint="FirstOccurrenceRoot", variable=1) # save trees as openalea.mtg.MTG T.PickleDump("tree.pkl") T2 = trees.PickleLoad("tree.pkl")
# {Short / Long} x {Vegetative / Floral} import sys, os, datetime import openalea.tree_statistic.trees as trees import openalea.tree_statistic.trees as trees, openalea.tree_statistic.mot as mot import openalea.stat_tool as stat_tool import openalea.tree_statistic.dgdistributions as dgd import matplotlib.pyplot as plt #from stat_tool.plot import gnuplot, mplotlib #stat_tool.plot.PLOTTER = mplotlib() #stat_tool.plot.PLOTTER = gnuplot() import openalea.aml as amlPy from amlPy import * T = trees.Trees("Data/mtg_fuji_42_47.txt") # 2 pommiers a l'echelle de la pousse annuelle # variable 0 : nombre d'entre-noeus # variable 1 : etat (0: long veg. 1: long fl. 2: court veg. 3: court fl. # variable 2 : nombre de cycles de la pousse annuelle # variable 3 : descendance censuree (cas en 1999) # recupere le dictionnaire des sommets censures censored = [] for tid in range(T.NbTrees()): censored += [{}] TA = T.Tree(tid) # identificateurs vids = TA.TreeVertex().values() for v in vids: # variables observees
while 1: vertices.append(leavesfirstorder.next() + 1) except StopIteration: pass print "A tree traversing that minimizes the distance " \ "to nearest leaf node:", vertices # select subtree ST = T.SelectSubTree(1) print "Extract subtree rooted at vertex 2 from T:" print ST ST = T.SelectSubTree(1, False) print "Prune subtree rooted at vertex 2 from T:" print ST # extract sequences import openalea.aml as amlPy TS = trees.Trees([T]) S1 = TS.BuildSequences(True) S2 = TS.BuildSequences(False) amlPy.Display(S1, ViewPoint="Data") amlPy.Display(S2, ViewPoint="Data") # W=T creates an alias W = trees.etrees.Tree(T) W.Put(W.Root(), [100]) print "A (non-alias) copy of T with modified root attribute:" print W # Tree structures print "A random tree structure R:" R = trees.etrees.TreeStructure(distrib, max_size, max_depth) print R print "(size, depth) = (", R.Size(), ", ", R.Depth(), ")."
def test_mtg_build(): """constructor from a MTG""" mtg_t = trees.Trees(mtg_name) assert mtg_t check_mtg_mapping(mtg_t) return mtg_t
def test_copy(): """test the copy of trees""" T2 = trees.Trees(T) msg = "Copy error" assert T2._ctrees_display() == T._ctrees_display(), msg
tmp_tree = trees.Tree(tv, R) n = 3 tree_list.append(trees.Tree(tmp_tree)) while n < nbtrees: n = n + 1 R.Simulate(distrib, max_size, max_depth) tmp_tree = trees.Tree(tv, R) tree_list.append(trees.Tree(tmp_tree)) distrib_list = [] for i in range(tmp_tree.NbInt()): distrib_list.append(distrib) for n in range(len(tree_list)): tree_list[n].Simulate(distrib_list) # initializing a Trees object T_ind = trees.Trees(tree_list) H = hmt.HiddenMarkovIndOutTree("hmot.hmt") sample_size = 2 tree_size = 20 nb_children = 3 print 'A hidden Markov tree H read from file "hmot.hmt":' print H H.Save("hmot_ascii.hmt", "ASCII", True) I = hmt.HiddenMarkovIndOutTree("hmot_ascii.hmt") I.Save("hmot_ascii.hmt", "ASCII", True) J = hmt.HiddenMarkovIndOutTree("hmot_ascii.hmt") if str(I) == str(J): print "load o (save o load) == load" else: print "load o (save o load) != load" print I
def test_mtg_build_failure(): """use invalid MTG file name""" try: T = trees.Trees("no_such_file.t\/t") except IOError, e: print e
def test_list_build_failure(): """use list of trees""" try: T = trees.Trees([0]) except TypeError, e: print e