It looks like one function not tested by pytest
Robert
rxjwg98 at gmail.com
Tue Jan 12 15:35:41 EST 2016
Hi,
I modify a test suite, and simplified to the below content.
I am interested in function:
test_bad_covariance_type()
I think that it tests for wrong type input, i.e.
'badcovariance_type'
will generate error, which is expected. Thus, it passes test.
When a correct type, such as 'diag', is given, there will be no error
generated. pytest will declare a failure.
Unfortunately, it will always pass the pytest for both cases.
What is wrong with my code or my understanding?
Thanks,
//////////////
from __future__ import absolute_import
from unittest import TestCase
import numpy as np
import pytest
from hmmlearn import hmm
from . import log_likelihood_increasing, make_covar_matrix, normalized
class GaussianHMMTestMixin(object):
covariance_type = None # set by subclasses
def setUp(self):
self.prng = prng = np.random.RandomState(10)
self.n_components = n_components = 3
self.n_features = n_features = 3
self.startprob = prng.rand(n_components)
self.startprob = self.startprob / self.startprob.sum()
self.transmat = prng.rand(n_components, n_components)
self.transmat /= np.tile(self.transmat.sum(axis=1)[:, np.newaxis],
(1, n_components))
self.means = prng.randint(-20, 20, (n_components, n_features))
self.covars = dict(
(cv_type, make_covar_matrix(cv_type, n_components, n_features))
for cv_type in ["spherical", "tied", "diag", "full"])
self.expanded_covars = {
'spherical': [np.eye(n_features) * cov
for cov in self.covars['spherical']],
'diag': [np.diag(cov) for cov in self.covars['diag']],
'tied': [self.covars['tied']] * n_components,
'full': self.covars['full'],
}
def test_bad_covariance_type(self):
with pytest.raises(ValueError):
#h = hmm.GaussianHMM(20, covariance_type='badcovariance_type')
h = hmm.GaussianHMM(covariance_type='diag')
h.means_ = self.means
h.covars_ = []
h.startprob_ = self.startprob
h.transmat_ = self.transmat
h._check()
def test_fit(self, params='stmc', n_iter=5, **kwargs):
h = hmm.GaussianHMM(self.n_components, self.covariance_type)
h.startprob_ = self.startprob
h.transmat_ = normalized(
self.transmat + np.diag(self.prng.rand(self.n_components)), 1)
h.means_ = 20 * self.means
h.covars_ = self.covars[self.covariance_type]
lengths = [10] * 10
X, _state_sequence = h.sample(sum(lengths), random_state=self.prng)
# Mess up the parameters and see if we can re-learn them.
# TODO: change the params and uncomment the check
h.fit(X, lengths=lengths)
class TestGaussianHMMWithSphericalCovars(GaussianHMMTestMixin, TestCase):
covariance_type = 'spherical'
def test_fit_startprob_and_transmat(self):
self.test_fit('st')
def test_bad_covariance_type0(self):
self.test_bad_covariance_type()
More information about the Python-list
mailing list