Esempio n. 1
0
    def test_baum_welch_training_without(self):
        """Baum-Welch training without known state sequences."""
        training_seq = Trainer.TrainingSequence(self.rolls, ())

        def stop_training(log_likelihood_change, num_iterations):
            """Tell the training model when to stop."""
            if VERBOSE:
                print(f"ll change: {log_likelihood_change:f}")
            if log_likelihood_change < 0.01:
                return 1
            elif num_iterations >= 10:
                return 1
            else:
                return 0

        baum_welch_mm = self.mm_builder.get_markov_model()
        trainer = Trainer.BaumWelchTrainer(baum_welch_mm)
        trained_mm = trainer.train([training_seq], stop_training)
        if VERBOSE:
            print(trained_mm.transition_prob)
            print(trained_mm.emission_prob)
        test_rolls, test_states = generate_rolls(300)
        predicted_states, prob = trained_mm.viterbi(test_rolls,
                                                    dice_type_alphabet)
        if VERBOSE:
            print(f"Prediction probability: {prob:f}")
            Utilities.pretty_print_prediction(self.test_rolls, test_states,
                                              predicted_states)
Esempio n. 2
0
 def test_baum_welch_training_standard(self):
     """Standard Training with known states."""
     known_training_seq = Trainer.TrainingSequence(self.rolls, self.states)
     standard_mm = self.mm_builder.get_markov_model()
     trainer = Trainer.KnownStateTrainer(standard_mm)
     trained_mm = trainer.train([known_training_seq])
     if VERBOSE:
         print(trained_mm.transition_prob)
         print(trained_mm.emission_prob)
     test_rolls, test_states = generate_rolls(300)
     predicted_states, prob = trained_mm.viterbi(test_rolls, DiceTypeAlphabet())
     if VERBOSE:
         print("Prediction probability: %f" % prob)
         Utilities.pretty_print_prediction(test_rolls, test_states, predicted_states)
Esempio n. 3
0
    def setUp(self):
        # set up our Markov Model
        mm_builder = MarkovModel.MarkovModelBuilder(number_alphabet, letter_alphabet)
        mm_builder.allow_all_transitions()
        mm_builder.set_equal_probabilities()

        mm = mm_builder.get_markov_model()

        # now set up a test sequence
        emission_seq = Seq("ABB")
        state_seq = ()
        training_seq = Trainer.TrainingSequence(emission_seq, state_seq)

        # finally set up the DP
        self.dp = DynamicProgramming.ScaledDPAlgorithms(mm, training_seq)
        states.append(random.choice('123'))
    

    sequence = MutableSeq('',DNA())
    for i in range(num):
        sequence.append(random.choice('ACTG'))
    
    return states.toseq(),sequence.toseq()

n_seq = 30
states = []
sequence = []
seq = []

for i in range(n_seq):
    num = 10
    state2, sequence2 = random_generator(num)
    states.append(state2)
    sequence.append(sequence2)
    seq.append(Trainer.TrainingSequence(sequence[i],states[i]))
    
trainer = Trainer.BaumWelchTrainer(baum_welch)
trained = trainer.train(seq, stop_training)

print('\n\nProbabilitas Transisi: ',trained.transition_prob)
print('\nProbabilitas Emisi: ',trained.emission_prob)

prediction, prob = trained.viterbi(sequence[0], state())
print('\nProbabilitas Prediksi: ', prob)
Utilities.pretty_print_prediction(sequence[0], states[0], prediction)
Esempio n. 5
0
builder.set_emission_score('O', 'A', (1 / 3))
builder.set_emission_score('O', 'T', (2 / 3))

#building hmm
hmm = builder.get_markov_model()

#list sequence & state utk training
tseq = [Seq('ACSA', DNA()), Seq('AST', DNA()), Seq('ACCST', DNA())]
tstate = [
    MutableSeq('MJNO', State()),
    MutableSeq('MNO', State()),
    MutableSeq('MJJNO', State())
]

#training dengan menggunakan sequence ACSA, AST, ACCST pada tstate dan tseq
trainer = Trainer.KnownStateTrainer(hmm)
#hmm dilatih satupersatu
for i in range(len(tseq)):
    trainseq = Trainer.TrainingSequence(tseq[i], tstate[i])
    trainhmm = trainer.train([trainseq])

#Query yang akan dicocokan beserta dengan perhitungan state awalnya
seq = Seq('ACCCSA', DNA())
state = MutableSeq('MJJJNO', State())

#algoritma viterbi akan mencari state yang terbaik untuk sequence
predicted_states, prob = trainhmm.viterbi(seq, State())

#mengeluarkan hasil probabilitas dari sequence, emission, statenya, dan predicted statenya
print("Prediction probability: %f" % prob)
Utilities.pretty_print_prediction(seq, state, predicted_states)
Esempio n. 6
0
 def setUp(self):
     # set up a bogus HMM and our trainer
     hmm = MarkovModel.HiddenMarkovModel({}, {}, {}, {}, {})
     self.test_trainer = Trainer.AbstractTrainer(hmm)
Esempio n. 7
0
def stop_training(log_likelihood_change, num_iterations):
    """Tell the training model when to stop.
    """
    if VERBOSE:
        print("ll change: %f" % log_likelihood_change)
    if log_likelihood_change < 0.01:
        return 1
    elif num_iterations >= 10:
        return 1
    else:
        return 0


# -- Standard Training with known states
print("Training with the Standard Trainer...")
known_training_seq = Trainer.TrainingSequence(rolls, states)

trainer = Trainer.KnownStateTrainer(standard_mm)
trained_mm = trainer.train([known_training_seq])

if VERBOSE:
    print(trained_mm.transition_prob)
    print(trained_mm.emission_prob)

test_rolls, test_states = generate_rolls(300)

predicted_states, prob = trained_mm.viterbi(test_rolls, DiceTypeAlphabet())
if VERBOSE:
    print("Prediction probability: %f" % prob)
    Utilities.pretty_print_prediction(test_rolls, test_states, predicted_states)
Esempio n. 8
0
 def test_invalid_training_sequence(self):
     emission_seq = Seq("AB")
     state_seq = ("1", )
     with self.assertRaises(ValueError):
         Trainer.TrainingSequence(emission_seq, state_seq)
Esempio n. 9
0
 def test_valid_training_sequence(self):
     emission_seq = Seq("AB")
     state_seq = ("1", "2")
     training_seq = Trainer.TrainingSequence(emission_seq, state_seq)
     self.assertEqual(training_seq.emissions, emission_seq)
     self.assertEqual(training_seq.states, state_seq)
Esempio n. 10
0
 def test_empty_state_training_sequence(self):
     emission_seq = Seq("AB")
     state_seq = ()
     training_seq = Trainer.TrainingSequence(emission_seq, state_seq)
     self.assertEqual(training_seq.emissions, emission_seq)
     self.assertEqual(training_seq.states, state_seq)
 def test_invalid_training_sequence(self):
     emission_seq = Seq('AB', LetterAlphabet())
     state_seq = Seq('1', NumberAlphabet())
     with self.assertRaises(ValueError):
         Trainer.TrainingSequence(emission_seq, state_seq)
 def test_valid_training_sequence(self):
     emission_seq = Seq('AB', LetterAlphabet())
     state_seq = Seq('12', NumberAlphabet())
     training_seq = Trainer.TrainingSequence(emission_seq, state_seq)
     assert training_seq.emissions == emission_seq
     assert training_seq.states == state_seq
Esempio n. 13
0
 def test_empty_state_training_sequence(self):
     emission_seq = Seq("AB", LetterAlphabet())
     state_seq = Seq("", NumberAlphabet())
     training_seq = Trainer.TrainingSequence(emission_seq, state_seq)
     assert training_seq.emissions == emission_seq
     assert training_seq.states == state_seq
markovModel = markovBuilder.get_markov_model()

#3 sequence yang akan dialign
seq1 = Seq('ATGA', arrayDNA())
seq2 = Seq('A CCA', arrayDNA())
seq3 = Seq('ACAST', arrayDNA())

#state untuk tiap sequence
seq1State = MutableSeq('MNOP', arrayState())
seq2State = MutableSeq('MDIOP', arrayState())
seq3State = MutableSeq('MNIOP', arrayState())

seq = [seq1, seq2, seq3]
states = [seq1State, seq2State, seq3State]

#training Hidden Markov Model dengan sequence di atas
trainer = Trainer.KnownStateTrainer(markovModel)
for i in range(len(seq)):
    trainingseq = Trainer.TrainingSequence(seq[i], states[i])
    trainedhmm = trainer.train([trainingseq])

#contoh query yang lain
testSeq = Seq('ATSA', arrayDNA())
testState = MutableSeq('MNOP', arrayState())

#mencari state terbaik untuk sequence dengan Viterbi Algorithm
predictedstates, prob = trainedhmm.viterbi(testSeq, arrayState())

#mengeluarkan hasil probabilitas dari sequence, emission, statenya, dan predicted statenya
print("Probabilitas Prediksi: %f" % prob)
Utilities.pretty_print_prediction(testSeq, testState, predictedstates)
cat = Seq('AGAAG', generic_dna)
gnat = Seq('GAAAC', generic_dna)
goat = Seq('AGC', generic_dna)

#list state untuk masing2 sekuen
catst = MutableSeq('MNJJO', StateAlphabet())
batst = MutableSeq('MNO', StateAlphabet())
ratst = MutableSeq('MNJJO', StateAlphabet())
gnatst = MutableSeq('DNJJO', StateAlphabet())
goatst = MutableSeq('MNO', StateAlphabet())

seq = [cat, bat, rat, gnat, goat]
states = [catst, batst, ratst, gnatst, goatst]

#untuk training HMM
trainer = Trainer.KnownStateTrainer(hmm)
for i in range(len(seq)):
    trainingseq = Trainer.TrainingSequence(seq[i], states[i])
    trainedhmm = trainer.train([trainingseq])

#mengetest HMM yang sudah di train
teststates = MutableSeq('', StateAlphabet())

for testseq in seq:
    #digunakan Viterbi algorithm
    predictedstates, prob = trainedhmm.viterbi(testseq, StateAlphabet())
    print("Prediction probability: %f" % prob)
    #untuk mengeprint test sequence dan predictednya
    Utilities.pretty_print_prediction(testseq, teststates, predictedstates)

#contoh untuk print alignment antara 2 DNA sekuen