Exemple #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)
Exemple #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)
        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)
Exemple #4
0
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)
Exemple #5
0
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)

# -- Baum-Welch training without known state sequences
print("Training with Baum-Welch...")
training_seq = Trainer.TrainingSequence(rolls, Seq("", DiceTypeAlphabet()))

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, DiceTypeAlphabet())
#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)
#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
testseq = 'AGAGC'
profile = 'ATC'
predictedstates, prob = trainedhmm.viterbi(testseq, StateAlphabet())

print('Predicted state:' + predictedstates)
for i in range(len(predictedstates)):
    if (predictedstates[i] == 'D') or (predictedstates[i]
                                       == 'E') or (predictedstates[i] == 'F'):
        testseq = testseq[:i] + '-' + testseq[i:]

    elif (predictedstates[i] == 'I') or (predictedstates[i] == 'J') or (
            predictedstates[i] == 'K') or (predictedstates[i] == 'L'):
        profile = profile[:i] + '-' + profile[i:]