How to use test_assertion method in pytest-play

Best Python code snippet using pytest-play_python

test_HMMGeneral.py

Source:test_HMMGeneral.py Github

copy

Full Screen

...22 """Letters as the emissions of the model.23 """24 letters = ['A', 'B']25# -- helper functions26def test_assertion(name, result, expected):27 """Helper function to test an assertion and print out a reasonable error.28 """29 assert result == expected, "Expected %s, got %s for %s" \30 % (expected, result, name)31class MarkovModelBuilderTest(unittest.TestCase):32 def setUp(self):33 self.mm_builder = MarkovModel.MarkovModelBuilder(NumberAlphabet(),34 LetterAlphabet())35 def test_test_initialize(self):36 """Making sure MarkovModelBuilder is initialized correctly.37 """38 expected_transition_prob = {}39 expected_transition_pseudo = {}40 expected_emission_prob = {('2', 'A'): 0, ('1', 'A'): 0,41 ('1', 'B'): 0, ('2', 'B'): 0}42 expected_emission_pseudo = {('2', 'A'): 1, ('1', 'A'): 1,43 ('1', 'B'): 1, ('2', 'B'): 1}44 assertions = []45 test_assertion("Transition prob", self.mm_builder.transition_prob,46 expected_transition_prob)47 test_assertion("Transition pseudo",48 self.mm_builder.transition_pseudo,49 expected_transition_pseudo)50 test_assertion("Emission prob", self.mm_builder.emission_prob,51 expected_emission_prob)52 test_assertion("Emission pseudo", self.mm_builder.emission_pseudo,53 expected_emission_pseudo)54 def test_allow_all_transitions(self):55 """Testing allow_all_transitions.56 """57 self.mm_builder.allow_all_transitions()58 expected_prob = {('2', '1'): 0, ('1', '1'): 0,59 ('1', '2'): 0, ('2', '2'): 0}60 expected_pseudo = {('2', '1'): 1, ('1', '1'): 1,61 ('1', '2'): 1, ('2', '2'): 1}62 test_assertion("Probabilities", self.mm_builder.transition_prob,63 expected_prob)64 test_assertion("Pseudo counts", self.mm_builder.transition_pseudo,65 expected_pseudo)66 def test_set_initial_probabilities(self):67 self.mm_builder.set_initial_probabilities({})68 test_assertion("Equal initial probabilities by default",69 self.mm_builder.initial_prob, {'1': 0.5, '2': 0.5})70 # initial probability sum > 1, should raise an exception71 self.assertRaises(72 Exception,73 self.mm_builder.set_initial_probabilities,74 {'1': 0.6, '2': 0.5})75 # referencing invalid states should raise an exception76 self.assertRaises(77 Exception,78 self.mm_builder.set_initial_probabilities,79 {'666': 0.1})80 self.mm_builder.set_initial_probabilities({'1': 0.2})81 test_assertion("One default initial probability",82 self.mm_builder.initial_prob, {'1': 0.2, '2': 0.8})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)275 # run the Viterbi algorithm to find the most probable state path276 model = self.mm_builder.get_markov_model()277 observed_emissions = ['A', 'B']278 viterbi = model.viterbi(observed_emissions, NumberAlphabet)279 seq = viterbi[0]280 prob = viterbi[1]281 # the most probable path must be from state 1 to state 2282 test_assertion("most probable path", str(seq), '12')283 # The probability of that path is the probability of starting in284 # state 1, then emitting an A, then transitioning 1 -> 2, then285 # emitting a B.286 # Note that probabilities are converted into log space.287 expected_prob = math.log(prob_1_initial)\288 + math.log(prob_1_A)\289 + math.log(prob_1_to_2)\290 + math.log(prob_2_B)291 test_assertion("log probability of most probable path",292 prob, expected_prob)293class ScaledDPAlgorithmsTest(unittest.TestCase):294 def setUp(self):295 # set up our Markov Model296 mm_builder = MarkovModel.MarkovModelBuilder(NumberAlphabet(),297 LetterAlphabet())298 mm_builder.allow_all_transitions()299 mm_builder.set_equal_probabilities()300 mm = mm_builder.get_markov_model()301 # now set up a test sequence302 emission_seq = Seq("ABB", LetterAlphabet())303 state_seq = Seq("", NumberAlphabet())304 training_seq = Trainer.TrainingSequence(emission_seq, state_seq)305 # finally set up the DP...

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 pytest-play 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