Esempio n. 1
0
def test_hmt_read_failure():
    """Use bad file name reading some HMT from a file"""
    msg = "Failed to raise exception for bad file name"
    try:
        H = hmt.HiddenMarkovIndOutTree("too_few_states_param.hmt")
    except hmt.StatTreeError, e:
        print e
Esempio n. 2
0
def test_hmt_too_few_states_param_failure():
    """Read HMT from a file with too few states (parametric case)"""
    msg = "Failed to raise exception for too few state in parametric HMT"
    try:
        H = hmt.HiddenMarkovIndOutTree("")
    except hmt.StatTreeError, e:
        print e
Esempio n. 3
0
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
Esempio n. 4
0
def test_hmt_ascii_read():
    """Read some HMT from a file"""
    msg = "Could not read HMT file"
    try:
        H = hmt.HiddenMarkovIndOutTree(hmt_name2)
    except hmt.StatTreeError:
        assert False, msg
    else:
        assert H
        return H
Esempio n. 5
0
def test_compare_display():
    """Compare display for two copies of an HMT"""
    msg = "Differences found in displaying equal HMTs: "
    equal = True
    differences = []
    H = test_hmt_read()
    H2 = hmt.HiddenMarkovIndOutTree(H)
    if str(H) != str(H2):
        equal = False
    assert equal, msg + str(H) + "\n and" + str(H2)
Esempio n. 6
0
def test_estimate_init_hmt_value():
    """Estimate with an initial HMT read from a file, and compare result
    with a save HMT model"""
    msg = "Bad estimation for HiddenMarkovIndOutTree"
    hmtref_name = "data/hmot3estim.hmt"
    HREF = hmt.HiddenMarkovIndOutTree(hmtref_name)
    try:
        EH = T.Estimate("HIDDEN_MARKOV_TREE", HInit, 50, "ForwardBackward", True)
    except hmt.StatTreeError:
        assert False, msg
    else:
        assert (EH.Display() == HREF.Display())
        return EH
Esempio n. 7
0
def test_hmt_SAVEoREAD():
    """Compare Read and Save HMT"""
    msg = "load o (save o load) != load"
    equal = True
    H = test_hmt_read()
    file_name = "hmot_write.hmt"
    H.Save(file_name, "ASCII", True)
    try:
        f = file(file_name)
    except IOError:
        assert False, msg
    else:
        f.close()
    H2 = hmt.HiddenMarkovIndOutTree(file_name)
    if str(H) != str(H2):
        equal = False
    import os
    os.remove(file_name)
    assert equal, msg
Esempio n. 8
0
import sys, os
import openalea.stat_tool as stat_tool
from openalea.stat_tool.plot import gnuplot, set_plotter
# set_plotter(gnuplot())
import openalea.tree_statistic.trees as trees, openalea.tree_statistic.hmt as hmt
inf_bound=0
sup_bound=3
probability= 0.6
ident=stat_tool.DistributionIdentifierType.UNIFORM
parameter=stat_tool.D_DEFAULT
distrib= stat_tool.distribution._DiscreteParametricModel(ident, inf_bound, sup_bound, parameter, probability)
# Distribution used for the number of children and the tree attributes
file_name="hmot_np_2s.hmt"
# read a non parametric HMT from a file
print 'A hidden Markov tree H read from file "', file_name, '"'
H=hmt.HiddenMarkovIndOutTree(file_name)
sample_size=10
tree_size=30
nb_children=3
print H
# simulate labels from this HMT
print "Label simulation using H: "
T=H.Simulate(sample_size, tree_size, nb_children)
HISTO1=T.ExtractHistogram("Value", 1)
print "Marginal distribution for variable 1:"
print HISTO1
print "Observation histogram for variable 1 and state 0:"
OBS1=T.ExtractHistogram("Observation", 1, 0)
OBS1.display(Detail=2)
# OBS1.plot()
file_name="hmot_param.hmt"
# -*- coding: utf-8 -*-
# comparison of the outputs for the Sequences and Trees classes
import sys, os
import openalea.stat_tool as stat_tool
import openalea.tree_statistic.trees as trees
import openalea.tree_statistic.hmt as hmt
import openalea.sequence_analysis as aml
nb_children=1
size=100
distrib = stat_tool.Uniform(1, 1)
# name of 
hmotrefpath= "./hmot_np.hmt";
hmotinitpath= "./hmot_np_init.hmt";
hmcinitpath= "./hmc_init.hmc";
HMT=hmt.HiddenMarkovIndOutTree(hmotrefpath)
HMTI=hmt.HiddenMarkovIndOutTree(hmotinitpath)
HMCI=aml.HiddenSemiMarkov(hmcinitpath)
T=HMT.Simulate(2, size, nb_children)
# delete state variable
T=T.SelectVariable(1, "Keep")
# build sequence from T
S=T.BuildPySequences()
# S = S.markovian_sequences()

# Print Sequences object
print S
# Print Trees object
print T

# Display sequence object
print(S.display())
# -*- coding: utf-8 -*-
# a test for the class hmt.Hmt: estimation - syntax
import sys, os
import openalea.stat_tool as stat_tool
import openalea.tree_statistic.trees as trees, openalea.tree_statistic.hmt as hmt
inf_bound = 1
sup_bound = 3
distrib = stat_tool.Uniform(inf_bound, sup_bound)
# Distribution used for the number of children and the tree attributes
file_name = "hmot_np_2s.hmt"
# read a HMT from a file
print 'A hidden Markov tree H read from file "', file_name, '"'
H = hmt.HiddenMarkovIndOutTree(file_name)
H2O = hmt.HiddenMarkovIndOutTree("hmot_np_2o.hmt")
sample_size = 2
tree_size = 30
nb_children = 2
H.Display()
# simulate labels from this HMT
print "Label simulation using H (1st tree): "
T = H.Simulate(sample_size, tree_size, nb_children)
print T.Tree(0)
# delete the state variable
print "Delete the state variable and the smoothed probabilities."
T = T.SelectVariable(1, "Keep")
print "State variable deleted: "
print T.Tree(0)
# parameter estimation from the simulated trees
# initialization from a model
print "Parameter estimation from the simulated trees" + \
" using the initial model specification:"
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
    print J
# -*- coding: utf-8 -*-
# a test for the class hmt.Hmt: estimation accurracy
import sys, os
import openalea.stat_tool as stat_tool
import openalea.tree_statistic.trees as trees, openalea.tree_statistic.hmt as hmt
inf_bound=1
sup_bound=3
distrib = stat_tool.Uniform(inf_bound, sup_bound)
# Distribution used for the number of children and the tree attributes
H=hmt.HiddenMarkovIndOutTree("hmot.hmt")
sample_size=200
tree_size=30
nb_children=2
print 'A hidden Markov tree H read from file "hmot.hmt":'
H.Display()
T=H.Simulate(sample_size, tree_size, nb_children)
print "Label simulation using H: "
T.Display()
print "Delete the state variable"
T=T.SelectVariable([1, 2])
T.Display()
print "Parameter estimation from the simulated trees " + \
"using initial model specification:"
EH=T.Estimate("HIDDEN_MARKOV_TREE", H, 100)
EH.Display()
# raise UserWarning, "Warning: need to be debugged"
print "Parameter estimation from the simulated trees " + \
"using state number and self transition specifications:"
EH=T.Estimate("HIDDEN_MARKOV_TREE", 2, "IRREDUCIBLE", 0.99, 100)
EH.Display()