-
Notifications
You must be signed in to change notification settings - Fork 0
/
AEMatrices.py
225 lines (181 loc) · 6.12 KB
/
AEMatrices.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""Module for operations on matrices.
This module includes all the necessary matrix operations for HMM.
"""
import sys
from decimal import Decimal
# GLOBAL Variable
# Emission matrix
# contains two-dimensional dictionary E[i][j]
# describing transition probability from state i to j
E = dict()
# Transition matrix
# contains two-dimensional dictionary A[i][j]
# describing transition probability from state i of symbol j
A = dict()
# list of all emission symbols in E
emissionSymbols = []
# list of all states in A
allStates = []
# list of all states excluding "B" and "E"
emittingStates = []
def readMatrix(filename, flag):
"""Read emission or transition matrix from a file.
flag should be either "A" or "E"
"""
# check if the correct flag is used
if flag not in ("A", "E"):
sys.exit("The flag for readMatrix should either be \"A\" or \"E\"")
matrixFile = open(filename)
firstLine = True
# the columns in the E matrix should be the symbols and the rows should
# be the states
for line in matrixFile:
# cols: are the columns in the matrix
cols = line.split()
if firstLine:
# start reading the header of the matrix
header = cols
firstLine = False
else:
if len(cols) == len(header)+1:
# Read each row of the matrix and store each row temporariliy
# in probs
probs = {}
for i in range(1, len(cols)):
probs[header[i-1]] = float(cols[i])
# store each row in the correct matrix, according to the flag
if flag == "E":
E[cols[0]] = probs
elif flag == "A":
A[cols[0]] = probs
else:
# we are no longer reading the rows of the matrix,
# so finish reading.
break
def init(EMatrixFilename, AMatrixFilename):
"""Initialize the A and E matrices.
by reading them from files and storing the content of the file into the
correct variables.
"""
global A, E, emittingStates, emissionSymbols, allStates
# reinitialise the variables each time init is performed
E.clear()
A.clear()
emittingStates[:] = []
allStates[:] = []
emissionSymbols[:] = []
# read both matrices
readMatrix(AMatrixFilename, "A")
readMatrix(EMatrixFilename, "E")
# check if begin and end state defined, and remove from list
for x in A.keys():
emittingStates.append(x)
if('B' not in emittingStates or 'E' not in emittingStates):
print("no begin or end state defined")
exit(1)
emittingStates.remove('E')
emittingStates.remove('B')
# make a deepcopy
for x in emittingStates:
allStates.append(x)
allStates.append('E')
allStates.insert(0, 'B')
# get emitting symbols, from first row of E
for x in E[emittingStates[0]].keys():
emissionSymbols.append(x)
# Checks whether all states in E correspond to states in A, terminate the
# programme if this is not the case.
for x in E.keys():
if x not in allStates:
print("the states in the E matrix do not correspond to states in"
" A matrix")
exit(1)
def writeEMatrix(M, filename):
"""Write emission matrix M to a file.
(in tab-separated manner)
"""
f = open(filename, "w")
header = ['']
for s in emissionSymbols:
header.append(s)
print('\t'.join(sorted(header)), file=f)
for l in sorted(emittingStates):
to_print = []
to_print.append(l)
for s in sorted(header)[1:]:
to_print.append('{:.4E}'.format(Decimal(M[l][s])))
print('\t'.join(map(str, to_print)), file=f)
f.close()
print('New E matrix saved in file')
def writeAMatrix(M, filename):
"""Write transition (A) matrix M to a file.
(in tab-separated manner)
"""
f = open(filename, "w")
# writing the header
header = ['']
header.append('B')
for l in sorted(emittingStates):
header.append(l)
header.append('E')
print('\t'.join(header), file=f)
# writing rows (notice rows and columns have the same indexes for A)
for l in header[1:]:
to_print = [l]
for k in header[1:]:
to_print.append('{:.4E}'.format(Decimal(M[l][k])))
print('\t'.join(map(str, to_print)), file=f)
f.close()
print('New A matrix saved in file')
def writeForwardMatrix(forward_matrix, filename):
"""Write forward matrix to a file.
(in tab-separated manner)
"""
f = open(filename, "w")
position=['']
for i in range(len(forward_matrix['E'])):
position.append(str(i))
print('\t'.join(position), file=f)
for l in ['B', 'D', 'L', 'E']:
to_print = [l]
for i in forward_matrix[l]:
to_print.append('{:.2E}'.format(Decimal(i)))
print('\t'.join(map(str, to_print)), file=f)
f.close()
print('New Forward matrix saved in file')
def writeBackwardMatrix(backward_matrix, filename):
"""Write backward matrix to a file.
(in tab-separated manner)
"""
f = open(filename, "w")
position=['']
for i in range(len(backward_matrix['E'])):
position.append(str(i))
print('\t'.join(position), file=f)
for l in ['B', 'D', 'L', 'E']:
to_print = [l]
for i in backward_matrix[l]:
to_print.append('{:.2E}'.format(Decimal(i)))
print('\t'.join(map(str, to_print)), file=f)
f.close()
print('New Backward matrix saved in file')
def setNewA(newA):
"""Copy the value from newA to the current A matrix.
assuming that both of them have the same states
"""
for l in allStates:
A[l] = dict()
for k in allStates:
A[l][k] = newA[l][k]
return A
def setNewE(newE):
"""Copy the value from newE to the current E matrix.
assuming that both of them have the same states and emission symbols
"""
for l in emittingStates:
E[l] = dict()
for s in emissionSymbols:
E[l][s] = newE[l][s]
return E