How to use allow_transition method in hypothesis

Best Python code snippet using hypothesis

test_HMMGeneral.py

Source:test_HMMGeneral.py Github

copy

Full Screen

...83 self.mm_builder.set_initial_probabilities({'1': 0.9, '2': 0.1})84 test_assertion("Set initial probabilities",85 self.mm_builder.initial_prob, {'1': 0.9, '2': 0.1})86 def test_set_equal_probabilities(self):87 self.mm_builder.allow_transition('1', '2', 0.05)88 self.mm_builder.allow_transition('2', '1', 0.95)89 self.mm_builder.set_equal_probabilities()90 test_assertion("Equal initial probabilities",91 self.mm_builder.initial_prob,92 {'1': 0.5, '2': 0.5})93 test_assertion("Equal transition probabilities",94 self.mm_builder.transition_prob,95 {('1', '2'): 0.5, ('2', '1'): 0.5})96 test_assertion("Equal emission probabilities",97 self.mm_builder.emission_prob,98 {('2', 'A'): 0.25, ('1', 'B'): 0.25,99 ('1', 'A'): 0.25, ('2', 'B'): 0.25})100 def test_set_random_probabilities(self):101 self.mm_builder.allow_transition('1', '2', 0.05)102 self.mm_builder.allow_transition('2', '1', 0.95)103 self.mm_builder.set_random_probabilities()104 test_assertion("Number of initial probabilities",105 len(self.mm_builder.initial_prob),106 len(self.mm_builder._state_alphabet.letters))107 # To test this more thoroughly, perhaps mock random.random() and108 # verify that it's being called as expected?109class HiddenMarkovModelTest(unittest.TestCase):110 def setUp(self):111 self.mm_builder = MarkovModel.MarkovModelBuilder(NumberAlphabet(),112 LetterAlphabet())113 def test_transitions_from(self):114 """Testing the calculation of transitions_from115 """116 self.mm_builder.allow_transition('1', '2', 1.0)117 self.mm_builder.allow_transition('2', '1', 0.5)118 self.mm_builder.allow_transition('2', '2', 0.5)119 self.mm_builder.set_initial_probabilities({})120 self.mm = self.mm_builder.get_markov_model()121 state_1 = self.mm.transitions_from("1")122 expected_state_1 = ["2"]123 state_1.sort()124 expected_state_1.sort()125 test_assertion("States reached by transitions from state 1",126 state_1, expected_state_1)127 state_2 = self.mm.transitions_from("2")128 expected_state_2 = ["1", "2"]129 state_2.sort()130 expected_state_2.sort()131 test_assertion("States reached by transitions from state 2",132 state_2, expected_state_2)133 fake_state = self.mm.transitions_from("Fake")134 expected_fake_state = []135 test_assertion("States reached by transitions from a fake transition",136 fake_state, expected_fake_state)137 def test_transitions_to(self):138 """Testing the calculation of transitions_to139 """140 self.mm_builder.allow_transition('1', '1', 0.5)141 self.mm_builder.allow_transition('1', '2', 0.5)142 self.mm_builder.allow_transition('2', '1', 1.0)143 self.mm_builder.set_initial_probabilities({})144 self.mm = self.mm_builder.get_markov_model()145 state_1 = self.mm.transitions_to("1")146 expected_state_1 = ["1", "2"]147 state_1.sort()148 expected_state_1.sort()149 test_assertion("States with transitions to state 1",150 state_1, expected_state_1)151 state_2 = self.mm.transitions_to("2")152 expected_state_2 = ["1"]153 state_2.sort()154 expected_state_2.sort()155 test_assertion("States with transitions to state 2",156 state_2, expected_state_2)157 fake_state = self.mm.transitions_to("Fake")158 expected_fake_state = []159 test_assertion("States with transitions to a fake transition",160 fake_state, expected_fake_state)161 def test_allow_transition(self):162 """Testing allow_transition163 """164 self.mm_builder.allow_transition('1', '2', 1.0)165 self.mm_builder.set_initial_probabilities({})166 self.mm = self.mm_builder.get_markov_model()167 state_1 = self.mm.transitions_from("1")168 expected_state_1 = ["2"]169 state_1.sort()170 expected_state_1.sort()171 test_assertion("States reached by transitions from state 1",172 state_1, expected_state_1)173 state_2 = self.mm.transitions_from("2")174 expected_state_2 = []175 state_2.sort()176 expected_state_2.sort()177 test_assertion("States reached by transitions from state 2",178 state_2, expected_state_2)179 state_1 = self.mm.transitions_to("1")180 expected_state_1 = []181 state_1.sort()182 expected_state_1.sort()183 test_assertion("States with transitions to state 1",184 state_1, expected_state_1)185 state_2 = self.mm.transitions_to("2")186 expected_state_2 = ["1"]187 state_2.sort()188 expected_state_2.sort()189 test_assertion("States with transitions to state 2",190 state_2, expected_state_2)191 def test_simple_hmm(self):192 """Test a simple model with 2 states and 2 symbols.193 """194 # set initial probabilities195 prob_initial = [0.4, 0.6]196 self.mm_builder.set_initial_probabilities(197 {'1': prob_initial[0], '2': prob_initial[1]})198 # set transition probabilities199 prob_transition = [[0.35, 0.65], [0.45, 0.55]]200 self.mm_builder.allow_transition('1', '1', prob_transition[0][0])201 self.mm_builder.allow_transition('1', '2', prob_transition[0][1])202 self.mm_builder.allow_transition('2', '1', prob_transition[1][0])203 self.mm_builder.allow_transition('2', '2', prob_transition[1][1])204 # set emission probabilities205 prob_emission = [[0.45, 0.55], [0.75, 0.25]]206 self.mm_builder.set_emission_score('1', 'A', prob_emission[0][0])207 self.mm_builder.set_emission_score('1', 'B', prob_emission[0][1])208 self.mm_builder.set_emission_score('2', 'A', prob_emission[1][0])209 self.mm_builder.set_emission_score('2', 'B', prob_emission[1][1])210 # Check all two letter sequences using a brute force calculation211 model = self.mm_builder.get_markov_model()212 for first_letter in LetterAlphabet.letters:213 for second_letter in LetterAlphabet.letters:214 observed_emissions = [first_letter, second_letter]215 viterbi = model.viterbi(observed_emissions, NumberAlphabet)216 self._checkSimpleHmm(prob_initial, prob_transition,217 prob_emission, viterbi, observed_emissions)218 def _checkSimpleHmm(self, prob_initial, prob_transition, prob_emission,219 viterbi, observed_emissions):220 max_prob = 0221 # expected first and second states in the sequence, calculated below222 seq_first_state = None223 seq_second_state = None224 # convert the observed letters 'A' or 'B' into 0 or 1225 letter1 = ord(observed_emissions[0]) - ord('A')226 letter2 = ord(observed_emissions[1]) - ord('A')227 for first_state in NumberAlphabet.letters:228 for second_state in NumberAlphabet.letters:229 # compute the probability of the state sequence first_state,230 # second_state emitting the observed_emissions231 state1 = ord(first_state) - ord('1')232 state2 = ord(second_state) - ord('1')233 prob = prob_initial[state1] * prob_emission[state1][letter1] *\234 prob_transition[state1][state2] *\235 prob_emission[state2][letter2]236 if prob > max_prob:237 seq_first_state = first_state238 seq_second_state = second_state239 max_prob = prob240 max_prob = math.log(max_prob)241 seq = viterbi[0]242 prob = viterbi[1]243 test_assertion("state sequence",244 str(seq),245 seq_first_state + seq_second_state)246 test_assertion("log probability", round(prob, 11), round(max_prob, 11))247 def test_non_ergodic(self):248 """Test a non-ergodic model (meaning that some transitions are not249 allowed).250 """251 # make state '1' the initial state252 prob_1_initial = 1.0253 self.mm_builder.set_initial_probabilities(254 {'1': prob_1_initial})255 # probabilities of transitioning from state 1 to 1, and 1 to 2256 prob_1_to_1 = 0.5257 prob_1_to_2 = 0.5258 # set up allowed transitions259 self.mm_builder.allow_transition('1', '1', prob_1_to_1)260 self.mm_builder.allow_transition('1', '2', prob_1_to_2)261 # Emission probabilities262 # In state 1 the most likely emission is A, in state 2 the most263 # likely emission is B. (Would be simpler just to use 1.0 and 0.0264 # emission probabilities here, but the algorithm blows up on zero265 # probabilities because of the conversion to log space.)266 prob_1_A = 0.95267 prob_1_B = 0.05268 prob_2_A = 0.05269 prob_2_B = 0.95270 # set emission probabilities271 self.mm_builder.set_emission_score('1', 'A', prob_1_A)272 self.mm_builder.set_emission_score('1', 'B', prob_1_B)273 self.mm_builder.set_emission_score('2', 'A', prob_2_A)274 self.mm_builder.set_emission_score('2', 'B', prob_2_B)...

Full Screen

Full Screen

HiddenMarkovModel.py

Source:HiddenMarkovModel.py Github

copy

Full Screen

...24 25#membangun HMM26hmmbuild = MarkovModel.MarkovModelBuilder(StateAlphabet(),DNAAlphabet())27#transisi dari match state28hmmbuild.allow_transition('M','N')29hmmbuild.allow_transition('N','O')30hmmbuild.allow_transition('M','I')31hmmbuild.allow_transition('N','J')32hmmbuild.allow_transition('O','K')33hmmbuild.allow_transition('M','E')34hmmbuild.allow_transition('N','F')35#transisi dari insert state36hmmbuild.allow_transition('L','M')37hmmbuild.allow_transition('I','N')38hmmbuild.allow_transition('J','O')39hmmbuild.allow_transition('I','I')40hmmbuild.allow_transition('J','J')41hmmbuild.allow_transition('K','K')42hmmbuild.allow_transition('L','L')43#transisi dari delete state 44hmmbuild.allow_transition('D','N')45hmmbuild.allow_transition('E','O')46hmmbuild.allow_transition('D','E')47hmmbuild.allow_transition('E','F')48hmmbuild.allow_transition('N','M')49hmmbuild.allow_transition('O','N')50hmmbuild.allow_transition('I','M')51hmmbuild.allow_transition('J','N')52hmmbuild.allow_transition('K','O')53hmmbuild.allow_transition('M','L')54hmmbuild.allow_transition('N','I')55hmmbuild.allow_transition('O','J')56hmmbuild.allow_transition('E','M')57hmmbuild.allow_transition('F','N')58hmmbuild.allow_transition('N','D')59hmmbuild.allow_transition('O','E')60hmmbuild.allow_transition('E','D')61hmmbuild.allow_transition('F','E')62#initial probability63hmmbuild.set_initial_probabilities({'M': 0.8, 'D': 0.2,'L':0})64#probabilitas untuk transisi dari satu state ke state lain65hmmbuild.set_transition_score('M', 'N', 1)66hmmbuild.set_transition_score('N', 'O', .4)67hmmbuild.set_transition_score('N', 'J', .6)68hmmbuild.set_transition_score('J', 'J', (4/6))69hmmbuild.set_transition_score('J', 'O', (2/6))70hmmbuild.set_transition_score('D', 'N', 1)71#emission probability dari state72hmmbuild.set_emission_score('M', 'A', 1)73hmmbuild.set_emission_score('N', 'G', 1)74hmmbuild.set_emission_score('O', 'C', .8)75hmmbuild.set_emission_score('O', 'G', .2)...

Full Screen

Full Screen

phmm.py

Source:phmm.py Github

copy

Full Screen

...19 letters = ['M', 'N','O','D','E','F','I','J','K','L']20#inisialisasi hmm builder21builder = MarkovModel.MarkovModelBuilder(State(),DNA())22#transisi dari match state23builder.allow_transition('M','N')24builder.allow_transition('M','E')25builder.allow_transition('M','J')26builder.allow_transition('N','O')27builder.allow_transition('N','K')28builder.allow_transition('O','L')29#transisi dari insert state30builder.allow_transition('I','M')31builder.allow_transition('I','I')32builder.allow_transition('I','D')33builder.allow_transition('J','N')34builder.allow_transition('J','J')35builder.allow_transition('J','E')36builder.allow_transition('K','O')37builder.allow_transition('K','K')38builder.allow_transition('K','F')39builder.allow_transition('L','L')40#transisi dari delete state 41builder.allow_transition('D','E')42builder.allow_transition('D','J')43builder.allow_transition('D','N')44builder.allow_transition('E','F')45builder.allow_transition('E','K')46builder.allow_transition('E','O')47builder.allow_transition('F','L')48#initial probability49builder.set_initial_probabilities({'M': 1})50#probabilitas untuk transisi dari satu state ke state lain51builder.set_transition_score('M', 'N', (1/3))52builder.set_transition_score('M', 'J', (2/3))53builder.set_transition_score('J', 'J', (1/3))54builder.set_transition_score('J', 'N', (2/3))55builder.set_transition_score('N', 'O', (1))56#emission probability dari state57builder.set_emission_score('M', 'A', 1)58builder.set_emission_score('J', 'C', 1)59builder.set_emission_score('N', 'S', 1)60builder.set_emission_score('O', 'A', (1/3))61builder.set_emission_score('O', 'T', (2/3))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run hypothesis automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful