Example #1
0
 def viterbi(self, sequence):
     S = self._S
     E = self.make_emmissions_matrix(sequence)
     T = self._T
     seq = array(range(len(sequence)), dtype=int32)
     p, vp, s = trace.viterbi_log2(seq, S, T, E)
     return map(lambda i: self.states[i], s), p, vp
Example #2
0
 def viterbi_by_lookup(self, fid, widseq):
   S = self._S
   E = self.make_emmissions_matrix_by_lookup([(fid,wid) for wid in widseq])
   T = self._T
   seq = array( range(len(widseq)), dtype=int32)
   p,vp,s = trace.viterbi_log2( seq, S, T, E )
   return map( lambda i: self.states[i], s ), p, vp
Example #3
0
 def viterbi(self, sequence):
   S = self._S
   E = self.make_emmissions_matrix(sequence)
   T = self._T
   seq = array( range(len(sequence)), dtype=int32)
   p,vp,s = trace.viterbi_log2( seq, S, T, E )
   return map( lambda i: self.states[i], s ), p, vp
Example #4
0
 def viterbi_by_lookup(self, fid, widseq):
     S = self._S
     E = self.make_emmissions_matrix_by_lookup([(fid, wid)
                                                for wid in widseq])
     T = self._T
     seq = array(range(len(widseq)), dtype=int32)
     p, vp, s = trace.viterbi_log2(seq, S, T, E)
     return map(lambda i: self.states[i], s), p, vp
Example #5
0
DRY = 0
DRYISH = 1
DAMP = 2
SOGGY = 3
NOBS = 4

sprob = array([0.63, 0.17, 0.20])
tprob = array([
    [0.5, 0.25, 0.25],  ## src = rows, dest = cols       
    [0.375, 0.125, 0.375],
    [0.125, 0.675, 0.375]
])
eprob = array([
    [0.60, 0.20, 0.15, 0.05],  ## state = rows, obs = cols   
    [0.25, 0.25, 0.25, 0.25],
    [0.05, 0.10, 0.35, 0.50]
])
sequence = [DRY, DAMP, SOGGY, DRY, DAMP, SOGGY]
## should give sunny -> rainy -> rainy -> cloudy -> rainy -> rainy with vprob 2.5754047e-5
expected = 2.5754047e-5

from trace import viterbi_log2
t, p, labels = viterbi_log2(sequence,
                            log2(sprob),
                            log2(tprob),
                            log2(eprob),
                            do_checks=True)
print 2**t, 2**p
for e in labels:
    print states[e]
Example #6
0
  "dryish",
  "damp",
  "soggy"]

DRY   =0
DRYISH=1
DAMP  =2
SOGGY =3
NOBS  =4

sprob = array(   [ 0.63, 0.17, 0.20])                                    
tprob = array(   [[0.5  , 0.25 , 0.25 ],## src = rows, dest = cols       
                  [0.375, 0.125, 0.375],                                 
                  [0.125, 0.675, 0.375]])                                
eprob = array(   [[0.60, 0.20, 0.15, 0.05],## state = rows, obs = cols   
                  [0.25, 0.25, 0.25, 0.25],                     
                  [0.05, 0.10, 0.35, 0.50]])                    
sequence = [ DRY, 
             DAMP, 
             SOGGY, 
             DRY, 
             DAMP, 
             SOGGY ]; ## should give sunny -> rainy -> rainy -> cloudy -> rainy -> rainy with vprob 2.5754047e-5
expected = 2.5754047e-5;

from trace import viterbi_log2
t,p,labels = viterbi_log2( sequence, log2(sprob), log2(tprob), log2(eprob), do_checks = True )
print 2**t,2**p
for e in labels:
  print states[e]