-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_hmm.py
285 lines (167 loc) · 6.66 KB
/
test_hmm.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# -*- coding: utf-8 -*-
###########
# IMPORTS #
###########
# Libraries
import numpy as _np
import numpy.testing as _npt
# Internal
from pydtmc import (
HiddenMarkovModel as _HiddenMarkovModel
)
#########
# TESTS #
#########
def test_decode(p, e, symbols, initial_status, use_scaling, value):
hmm = _HiddenMarkovModel(p, e)
initial_status = _np.array(initial_status) if isinstance(initial_status, list) else initial_status
decoding = hmm.decode(symbols, initial_status, use_scaling)
if decoding is None:
actual = decoding
expected = value
assert actual == expected
else:
actual = round(decoding[0], 8)
expected = value[0]
assert actual == expected
actual = decoding[1]
expected = _np.array(value[1])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
actual = decoding[2]
expected = _np.array(value[2])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
actual = decoding[3]
expected = _np.array(value[3])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
if use_scaling:
actual = decoding[4]
expected = _np.array(value[4])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
# noinspection DuplicatedCode, PyBroadException
def test_estimate(possible_states, possible_symbols, sequence_states, sequence_symbols, value):
try:
hmm = _HiddenMarkovModel.estimate(possible_states, possible_symbols, sequence_states, sequence_symbols)
except Exception:
hmm = None
if value is None:
assert hmm is None
else:
actual = hmm.p
expected = _np.array(value[0])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
actual = hmm.e
expected = _np.array(value[1])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
# noinspection DuplicatedCode, PyBroadException
def test_fit(fitting_type, possible_states, possible_symbols, p_guess, e_guess, symbols, initial_status, value):
p_guess = _np.array(p_guess)
e_guess = _np.array(e_guess)
try:
hmm_fit = _HiddenMarkovModel.fit(fitting_type, possible_states, possible_symbols, p_guess, e_guess, symbols, initial_status)
except Exception:
hmm_fit = None
if hmm_fit is None:
actual = hmm_fit
expected = value
assert actual == expected
else:
actual = hmm_fit.p
expected = _np.array(value[0])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
actual = hmm_fit.e
expected = _np.array(value[1])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_next(p, e, seed, initial_state, target, output_index, value):
hmm = _HiddenMarkovModel(p, e)
actual = hmm.next(initial_state, target, output_index, seed)
expected = tuple(value) if target == 'both' else value
assert actual == expected
# noinspection PyBroadException
def test_predict(p, e, algorithm, symbols, initial_distribution, output_indices, value):
hmm = _HiddenMarkovModel(p, e)
actual = hmm.predict(algorithm, symbols, initial_distribution, output_indices)
if actual is not None:
actual = (round(actual[0], 8), actual[1])
expected = value
if expected is not None:
expected = tuple(expected)
assert actual == expected
def test_probabilities(p, e):
hmm = _HiddenMarkovModel(p, e)
transition_matrix = hmm.p
states = hmm.states
for index1, state1 in enumerate(states):
for index2, state2 in enumerate(states):
actual = hmm.transition_probability(state1, state2)
expected = transition_matrix[index2, index1]
assert _np.isclose(actual, expected)
emission_matrix = hmm.e
symbols = hmm.symbols
for index1, state in enumerate(states):
for index2, symbol in enumerate(symbols):
actual = hmm.emission_probability(symbol, state)
expected = emission_matrix[index1, index2]
assert _np.isclose(actual, expected)
def test_properties(p, e, value):
hmm = _HiddenMarkovModel(p, e)
actual = hmm.is_ergodic
expected = value[0]
assert actual == expected
actual = hmm.is_regular
expected = value[1]
assert actual == expected
def test_random(seed, n, k, p_zeros, p_mask, e_zeros, e_mask, value):
states = [f'P{i:d}' for i in range(1, n + 1)]
symbols = [f'E{i:d}' for i in range(1, k + 1)]
hmm = _HiddenMarkovModel.random(n, k, states, p_zeros, p_mask, symbols, e_zeros, e_mask, seed)
actual = hmm.p
expected = _np.array(value[0])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
if p_zeros > 0 and p_mask is None:
actual = n**2 - _np.count_nonzero(hmm.p)
expected = p_zeros
assert actual == expected
if p_mask is not None:
indices = ~_np.isnan(_np.array(p_mask))
actual = hmm.p[indices]
expected = _np.array(value[0])[indices]
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
actual = hmm.e
expected = _np.array(value[1])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
if e_zeros > 0 and e_mask is None:
actual = (n * k) - _np.count_nonzero(hmm.e)
expected = e_zeros
assert actual == expected
if e_mask is not None:
indices = ~_np.isnan(_np.array(e_mask))
actual = hmm.e[indices]
expected = _np.array(value[1])[indices]
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
# noinspection DuplicatedCode, PyBroadException
def test_restrict(p, e, states, symbols, value):
hmm = _HiddenMarkovModel(p, e)
try:
hmm_restricted = hmm.restrict(states, symbols)
except Exception:
hmm_restricted = None
if hmm_restricted is None:
actual = hmm_restricted
expected = value
assert actual == expected
else:
actual = hmm_restricted.p
expected = _np.array(value[0])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
actual = hmm_restricted.e
expected = _np.array(value[1])
_npt.assert_allclose(actual, expected, rtol=1e-5, atol=1e-8)
def test_simulate(p, e, seed, steps, initial_state, final_state, final_symbol, output_indices, value):
hmm = _HiddenMarkovModel(p, e)
actual = hmm.simulate(steps, initial_state, final_state, final_symbol, output_indices, seed)
expected = tuple(value)
assert actual == expected
if initial_state is not None:
actual = actual[0][0]
expected = initial_state
assert actual == expected