Back to index

python-biopython  1.60
Public Member Functions | Public Attributes | Private Member Functions
test_HMMGeneral.HiddenMarkovModelTest Class Reference

List of all members.

Public Member Functions

def setUp
def test_transitions_from
def test_transitions_to
def test_allow_transition
def test_simple_hmm
def test_non_ergodic

Public Attributes

 mm_builder
 mm

Private Member Functions

def _checkSimpleHmm

Detailed Description

Definition at line 136 of file test_HMMGeneral.py.


Member Function Documentation

def test_HMMGeneral.HiddenMarkovModelTest._checkSimpleHmm (   self,
  prob_initial,
  prob_transition,
  prob_emission,
  viterbi,
  observed_emissions 
) [private]

Definition at line 265 of file test_HMMGeneral.py.

00265 
00266                          viterbi, observed_emissions):
00267         max_prob = 0
00268 
00269         # expected first and second states in the sequence, calculated below
00270         seq_first_state = None
00271         seq_second_state = None
00272 
00273         # convert the observed letters 'A' or 'B' into 0 or 1
00274         letter1 = ord(observed_emissions[0]) - ord('A')
00275         letter2 = ord(observed_emissions[1]) - ord('A')
00276 
00277         for first_state in NumberAlphabet.letters:
00278             for second_state in NumberAlphabet.letters:
00279                 # compute the probability of the state sequence first_state,
00280                 # second_state emitting the observed_emissions
00281                 state1 = ord(first_state) - ord('1')
00282                 state2 = ord(second_state) - ord('1')
00283                 prob = prob_initial[state1] * prob_emission[state1][letter1] *\
00284                     prob_transition[state1][state2] *\
00285                     prob_emission[state2][letter2]
00286                 if prob > max_prob:
00287                     seq_first_state = first_state
00288                     seq_second_state = second_state
00289                     max_prob = prob
00290 
00291         max_prob = math.log(max_prob)
00292         seq = viterbi[0]
00293         prob = viterbi[1]
00294         test_assertion("state sequence",
00295                        str(seq),
00296                        seq_first_state + seq_second_state)
00297         test_assertion("log probability", round(prob, 11), round(max_prob, 11))

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 137 of file test_HMMGeneral.py.

00137 
00138     def setUp(self):
00139         self.mm_builder = MarkovModel.MarkovModelBuilder(NumberAlphabet(),
00140                                                     LetterAlphabet())

Testing allow_transition

Definition at line 197 of file test_HMMGeneral.py.

00197 
00198     def test_allow_transition(self):
00199         """Testing allow_transition
00200         """
00201         self.mm_builder.allow_transition('1', '2', 1.0)
00202         self.mm_builder.set_initial_probabilities({})
00203         self.mm = self.mm_builder.get_markov_model()
00204 
00205         state_1 = self.mm.transitions_from("1")
00206         expected_state_1 = ["2"]
00207         state_1.sort()
00208         expected_state_1.sort()
00209         test_assertion("States reached by transitions from state 1",
00210                        state_1, expected_state_1)
00211 
00212         state_2 = self.mm.transitions_from("2")
00213         expected_state_2 = []
00214         state_2.sort()
00215         expected_state_2.sort()
00216         test_assertion("States reached by transitions from state 2",
00217                        state_2, expected_state_2)
00218 
00219         state_1 = self.mm.transitions_to("1")
00220         expected_state_1 = []
00221         state_1.sort()
00222         expected_state_1.sort()
00223         test_assertion("States with transitions to state 1",
00224                        state_1, expected_state_1)
00225 
00226         state_2 = self.mm.transitions_to("2")
00227         expected_state_2 = ["1"]
00228         state_2.sort()
00229         expected_state_2.sort()
00230         test_assertion("States with transitions to state 2",
00231                        state_2, expected_state_2)

Here is the call graph for this function:

Test a non-ergodic model (meaning that some transitions are not
allowed).

Definition at line 298 of file test_HMMGeneral.py.

00298 
00299     def test_non_ergodic(self):
00300         """Test a non-ergodic model (meaning that some transitions are not
00301         allowed).
00302         """
00303 
00304         # make state '1' the initial state
00305         prob_1_initial = 1.0
00306         self.mm_builder.set_initial_probabilities(
00307                 {'1': prob_1_initial})
00308 
00309         # probabilities of transitioning from state 1 to 1, and 1 to 2
00310         prob_1_to_1 = 0.5
00311         prob_1_to_2 = 0.5
00312 
00313         # set up allowed transitions
00314         self.mm_builder.allow_transition('1', '1', prob_1_to_1)
00315         self.mm_builder.allow_transition('1', '2', prob_1_to_2)
00316 
00317         # Emission probabilities
00318         # In state 1 the most likely emission is A, in state 2 the most
00319         # likely emission is B. (Would be simpler just to use 1.0 and 0.0
00320         # emission probabilities here, but the algorithm blows up on zero
00321         # probabilities because of the conversion to log space.)
00322         prob_1_A = 0.95
00323         prob_1_B = 0.05
00324         prob_2_A = 0.05
00325         prob_2_B = 0.95
00326 
00327         # set emission probabilities
00328         self.mm_builder.set_emission_score('1', 'A', prob_1_A)
00329         self.mm_builder.set_emission_score('1', 'B', prob_1_B)
00330         self.mm_builder.set_emission_score('2', 'A', prob_2_A)
00331         self.mm_builder.set_emission_score('2', 'B', prob_2_B)
00332 
00333         # run the Viterbi algorithm to find the most probable state path
00334         model = self.mm_builder.get_markov_model()
00335         observed_emissions = ['A', 'B']
00336         viterbi = model.viterbi(observed_emissions, NumberAlphabet)
00337         seq = viterbi[0]
00338         prob = viterbi[1]
00339 
00340         # the most probable path must be from state 1 to state 2
00341         test_assertion("most probable path", str(seq), '12')
00342 
00343         # The probability of that path is the probability of starting in 
00344         # state 1, then emitting an A, then transitioning 1 -> 2, then 
00345         # emitting a B.
00346         # Note that probabilities are converted into log space.
00347         expected_prob = math.log(prob_1_initial)\
00348         + math.log(prob_1_A)\
00349         + math.log(prob_1_to_2)\
00350         + math.log(prob_2_B)
00351         test_assertion("log probability of most probable path",
00352                        prob, expected_prob)

Here is the call graph for this function:

Test a simple model with 2 states and 2 symbols.

Definition at line 232 of file test_HMMGeneral.py.

00232 
00233     def test_simple_hmm(self):
00234         """Test a simple model with 2 states and 2 symbols.
00235         """
00236 
00237         # set initial probabilities
00238         prob_initial = [0.4, 0.6]
00239         self.mm_builder.set_initial_probabilities(
00240                 {'1': prob_initial[0], '2': prob_initial[1]})
00241         
00242         # set transition probabilities
00243         prob_transition = [[0.35, 0.65], [0.45, 0.55]]
00244         self.mm_builder.allow_transition('1', '1', prob_transition[0][0])
00245         self.mm_builder.allow_transition('1', '2', prob_transition[0][1])
00246         self.mm_builder.allow_transition('2', '1', prob_transition[1][0])
00247         self.mm_builder.allow_transition('2', '2', prob_transition[1][1])
00248 
00249         # set emission probabilities
00250         prob_emission = [[0.45, 0.55], [0.75, 0.25]]
00251         self.mm_builder.set_emission_score('1', 'A', prob_emission[0][0])
00252         self.mm_builder.set_emission_score('1', 'B', prob_emission[0][1])
00253         self.mm_builder.set_emission_score('2', 'A', prob_emission[1][0])
00254         self.mm_builder.set_emission_score('2', 'B', prob_emission[1][1])
00255 
00256         # Check all two letter sequences using a brute force calculation
00257         model = self.mm_builder.get_markov_model()
00258         for first_letter in LetterAlphabet.letters:
00259             for second_letter in LetterAlphabet.letters:
00260                 observed_emissions = [first_letter, second_letter]
00261                 viterbi = model.viterbi(observed_emissions, NumberAlphabet)
00262                 self._checkSimpleHmm(prob_initial, prob_transition,
00263                                  prob_emission, viterbi, observed_emissions)

Here is the call graph for this function:

Testing the calculation of transitions_from

Definition at line 141 of file test_HMMGeneral.py.

00141 
00142     def test_transitions_from(self):
00143         """Testing the calculation of transitions_from
00144         """
00145         self.mm_builder.allow_transition('1', '2', 1.0)
00146         self.mm_builder.allow_transition('2', '1', 0.5)
00147         self.mm_builder.allow_transition('2', '2', 0.5)
00148         self.mm_builder.set_initial_probabilities({})
00149         self.mm = self.mm_builder.get_markov_model()
00150 
00151         state_1 = self.mm.transitions_from("1")
00152         expected_state_1 = ["2"]
00153         state_1.sort()
00154         expected_state_1.sort()
00155         test_assertion("States reached by transitions from state 1",
00156                        state_1, expected_state_1)
00157 
00158         state_2 = self.mm.transitions_from("2")
00159         expected_state_2 = ["1", "2"]
00160         state_2.sort()
00161         expected_state_2.sort()
00162         test_assertion("States reached by transitions from state 2",
00163                        state_2, expected_state_2)
00164 
00165         fake_state = self.mm.transitions_from("Fake")
00166         expected_fake_state = []
00167         test_assertion("States reached by transitions from a fake transition",
00168                        fake_state, expected_fake_state)

Testing the calculation of transitions_to

Definition at line 169 of file test_HMMGeneral.py.

00169 
00170     def test_transitions_to(self):
00171         """Testing the calculation of transitions_to
00172         """
00173         self.mm_builder.allow_transition('1', '1', 0.5)
00174         self.mm_builder.allow_transition('1', '2', 0.5)
00175         self.mm_builder.allow_transition('2', '1', 1.0)
00176         self.mm_builder.set_initial_probabilities({})
00177         self.mm = self.mm_builder.get_markov_model()
00178 
00179         state_1 = self.mm.transitions_to("1")
00180         expected_state_1 = ["1", "2"]
00181         state_1.sort()
00182         expected_state_1.sort()
00183         test_assertion("States with transitions to state 1",
00184                        state_1, expected_state_1)
00185 
00186         state_2 = self.mm.transitions_to("2")
00187         expected_state_2 = ["1"]
00188         state_2.sort()
00189         expected_state_2.sort()
00190         test_assertion("States with transitions to state 2",
00191                        state_2, expected_state_2)
00192 
00193         fake_state = self.mm.transitions_to("Fake")
00194         expected_fake_state = []
00195         test_assertion("States with transitions to a fake transition",
00196                        fake_state, expected_fake_state)

Here is the call graph for this function:


Member Data Documentation

Definition at line 148 of file test_HMMGeneral.py.

Definition at line 138 of file test_HMMGeneral.py.


The documentation for this class was generated from the following file: