Debian Codegraph @

src/hmmlearn/tests/test_gmm_hmm.py

raw symbols SCIP
1 import numpy as np
2 from numpy.testing import assert_allclose
3 import pytest
4 from sklearn.mixture import GaussianMixture
5 from sklearn.utils import check_random_state
6
7 from hmmlearn import hmm
8 from . import assert_log_likelihood_increasing, make_covar_matrix, normalized
9
10 pytestmark = pytest.mark.xfail()
11
12
13 def create_random_gmm(n_mix, n_features, covariance_type, prng=0):
14 prng = check_random_state(prng)
15 g = GaussianMixture(n_mix, covariance_type=covariance_type)
16 g.means_ = prng.randint(-20, 20, (n_mix, n_features))
17 g.covars_ = make_covar_matrix(covariance_type, n_mix, n_features)
18 g.weights_ = normalized(prng.rand(n_mix))
19 return g
20
21
22 class GMMHMMTestMixin:
23 def setup_method(self, method):
24 self.prng = np.random.RandomState(9)
25 self.n_components = 3
26 self.n_mix = 2
27 self.n_features = 2
28 self.startprob = normalized(self.prng.rand(self.n_components))
29 self.transmat = normalized(
30 self.prng.rand(self.n_components, self.n_components), axis=1)
31 self.gmms = []
32 for state in range(self.n_components):
33 self.gmms.append(create_random_gmm(
34 self.n_mix, self.n_features, self.covariance_type,
35 prng=self.prng))
36
37 def test_score_samples_and_decode(self):
38 h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type)
39 h.startprob_ = self.startprob
40 h.transmat_ = self.transmat
41 h.gmms_ = self.gmms
42
43 # Make sure the means are far apart so posteriors.argmax()
44 # picks the actual component used to generate the observations.
45 for g in h.gmms_:
46 g.means_ *= 20
47
48 refstateseq = np.repeat(np.arange(self.n_components), 5)
49 n_samples = len(refstateseq)
50 X = [h.gmms_[x].sample(1).flatten() for x in refstateseq]
51
52 _ll, posteriors = h.score_samples(X)
53
54 assert posteriors.shape == (n_samples, self.n_components)
55 assert_allclose(posteriors.sum(axis=1), np.ones(n_samples))
56
57 _log_prob, stateseq = h.decode(X)
58 assert_allclose(stateseq, refstateseq)
59
60 def test_sample(self, n_samples=1000):
61 h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type)
62 h.startprob_ = self.startprob
63 h.transmat_ = self.transmat
64 h.gmms_ = self.gmms
65 X, state_sequence = h.sample(n_samples)
66 assert X.shape == (n_samples, self.n_features)
67 assert len(state_sequence) == n_samples
68
69 @pytest.mark.parametrize("params", ["stmwc", "wt", "m"])
70 def test_fit(self, params, n_iter=5):
71 h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type,
72 covars_prior=1.0)
73 h.startprob_ = self.startprob
74 h.transmat_ = normalized(
75 self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
76 h.gmms_ = self.gmms
77
78 lengths = [10] * 10
79 X, _state_sequence = h.sample(sum(lengths), random_state=self.prng)
80
81 # Mess up the parameters and see if we can re-learn them.
82 h.n_iter = 0
83 h.fit(X, lengths=lengths)
84 h.transmat_ = normalized(self.prng.rand(self.n_components,
85 self.n_components), axis=1)
86 h.startprob_ = normalized(self.prng.rand(self.n_components))
87
88 assert_log_likelihood_increasing(h, X, lengths, n_iter)
89
90 def test_fit_works_on_sequences_of_different_length(self):
91 lengths = [3, 4, 5]
92 X = self.prng.rand(sum(lengths), self.n_features)
93
94 h = hmm.GMMHMM(self.n_components, covariance_type=self.covariance_type)
95 # This shouldn't raise
96 # ValueError: setting an array element with a sequence.
97 h.fit(X, lengths=lengths)
98
99
100 class TestGMMHMMWithDiagCovars(GMMHMMTestMixin):
101 covariance_type = 'diag'
102
103
104 @pytest.mark.xfail
105 class TestGMMHMMWithTiedCovars(GMMHMMTestMixin):
106 covariance_type = 'tied'
107
108
109 @pytest.mark.xfail
110 class TestGMMHMMWithFullCovars(GMMHMMTestMixin):
111 covariance_type = 'full'