Exemplo n.º 1
0
def test_8():
    # "Sample from a VMHMM and then fit to it"
    n_states = 2
    vm = VonMisesHMM(n_states=n_states)
    means = np.array([[0, 0, 0], [np.pi, np.pi, np.pi]])
    kappas = np.array([[1, 2, 3], [2, 3, 4]])
    transmat = np.array([[0.9, 0.1], [0.1, 0.9]])
    vm.means_ = means
    vm.kappas_ = kappas
    vm.transmat_ = transmat
    x, s = vm.sample(1000)

    vm = VonMisesHMM(n_states=2)
    vm.fit([x])

    mappingcost = np.zeros((n_states, n_states))
    for i in range(n_states):
        for j in range(n_states):
            mappingcost[i, j] = np.sum(
                circwrap(vm.means_[i, :] - means[j, :]) ** 2)

    mapping = Munkres().compute(mappingcost)
    means_ = np.array([vm.means_[j, :] for i, j in mapping])
    kappas_ = np.array([vm.kappas_[j, :] for i, j in mapping])
    transmat_ = vm.transmat_

    print('means\n', means, '\nmeans_\n', means_)
    print('kappas\n', kappas, '\nkappas_\n', kappas_)
    print('transmat\n', transmat, '\ntransmat_\n', transmat_)

    #vm.score(x)

    assert np.all(np.abs(kappas - kappas_) < 0.5)
    assert np.all(circwrap(means - means_) < 0.25)
Exemplo n.º 2
0
def test_3():
    transmat = np.array([[0.2, 0.3, 0.5], [0.4, 0.4, 0.2], [0.8, 0.2, 0.0]])
    means = np.array([[0.0], [2.0], [4.0]])
    kappas = np.array([[8.0], [8.0], [6.0]])
    X = [create_timeseries(means, kappas, transmat) for i in range(20)]
    
    # For each value of various options, create a 3 state HMM and see if it is correct.
    
    for reversible_type in ('mle', 'transpose'):
        model = VonMisesHMM(n_states=3, reversible_type=reversible_type, thresh=1e-4, n_iter=30)
        model.fit(X)
        validate_timeseries(means, kappas, transmat, model, 0.1, 0.5, 0.1)
        assert abs(model.fit_logprob_[-1]-model.score(X)) < 0.5
Exemplo n.º 3
0
def test_2_state():
    transmat = np.array([[0.7, 0.3], [0.4, 0.6]])
    means = np.array([[0.0], [2.0]])
    kappas = np.array([[4.0], [8.0]])
    X = [create_timeseries(means, kappas, transmat) for i in range(10)]

    # For each value of various options,
    # create a 2 state HMM and see if it is correct.

    for reversible_type in ('mle', 'transpose'):
        model = VonMisesHMM(n_states=2, reversible_type=reversible_type,
                            thresh=1e-4, n_iter=30)
        model.fit(X)
        validate_timeseries(means, kappas, transmat, model, 0.1, 0.5, 0.05)
        assert abs(model.fit_logprob_[-1] - model.score(X)) < 0.5
Exemplo n.º 4
0
def test_code_works():
    # creates a 4-state HMM on the ALA2 data. Nothing fancy, just makes
    # sure the code runs without erroring out
    trajectories = AlanineDipeptide().get_cached().trajectories
    topology = trajectories[0].topology

    indices = topology.select('symbol C or symbol O or symbol N')
    featurizer = DihedralFeaturizer(['phi', 'psi'], trajectories[0][0])

    sequences = featurizer.transform(trajectories)

    hmm = VonMisesHMM(n_states=4, n_init=1)
    hmm.fit(sequences)

    assert len(hmm.timescales_ == 3)
    assert np.any(hmm.timescales_ > 50)
Exemplo n.º 5
0
def test_pipeline():
    trajs = AlanineDipeptide().get_cached().trajectories
    p = Pipeline([('diheds', DihedralFeaturizer(['phi', 'psi'], sincos=False)),
                  ('hmm', VonMisesHMM(n_states=4))])

    predict = p.fit_predict(trajs)
    p.named_steps['hmm'].summarize()
Exemplo n.º 6
0
def test_code_works():
    # creates a 4-state HMM on the ALA2 data. Nothing fancy, just makes
    # sure the code runs without erroring out
    trajectories = AlanineDipeptide().get_cached().trajectories
    topology = trajectories[0].topology

    indices = topology.select('symbol C or symbol O or symbol N')
    featurizer = DihedralFeaturizer(['phi', 'psi'], trajectories[0][0])

    sequences = featurizer.transform(trajectories)

    hmm = VonMisesHMM(n_states=4, n_init=1)
    hmm.fit(sequences)

    assert len(hmm.timescales_ == 3)
    assert np.any(hmm.timescales_ > 50)
Exemplo n.º 7
0
def test_pickle():
    """Test pickling an HMM"""
    trajectories = AlanineDipeptide().get_cached().trajectories
    topology = trajectories[0].topology
    indices = topology.select('symbol C or symbol O or symbol N')
    featurizer = DihedralFeaturizer(['phi', 'psi'], trajectories[0][0])
    sequences = featurizer.transform(trajectories)
    hmm = VonMisesHMM(n_states=4, n_init=1)
    hmm.fit(sequences)
    logprob, hidden = hmm.predict(sequences)

    with tempfile.TemporaryFile() as savefile:
        pickle.dump(hmm, savefile)
        savefile.seek(0, 0)
        hmm2 = pickle.load(savefile)

    logprob2, hidden2 = hmm2.predict(sequences)
    assert (logprob == logprob2)
Exemplo n.º 8
0
def test_log_likelihood():
    n_samples, n_states, n_features = 1000, 27, 16
    obs = np.random.rand(n_samples, n_features)
    vm = VonMisesHMM(n_states=n_states)
    vm.fit([obs])

    t0 = time.time()
    from scipy.stats.distributions import vonmises

    reference = np.array(
        [np.sum(vonmises.logpdf(obs, vm.kappas_[i], vm.means_[i]), axis=1)
         for i in range(n_states)]).T
    t1 = time.time()
    value = _vmhmm._compute_log_likelihood(obs, vm.means_, vm.kappas_)
    t2 = time.time()

    print("Log likeihood timings")
    print('reference time ', t1 - t0)
    print('c time         ', t2 - t1)
    np.testing.assert_array_almost_equal(reference, value)
Exemplo n.º 9
0
def test_6():
    # Test that _c_fitkappa is consistent with the two-step python implementation
    np.random.seed(42)
    vm = VonMisesHMM(n_states=13)
    kappas = np.random.randn(13, 7)
    posteriors = np.random.randn(100, 13)
    obs = np.random.randn(100, 7)
    means = np.random.randn(13, 7)

    vm.kappas_ = kappas
    vm._c_fitkappas(posteriors, obs, means)
    c_kappas = np.copy(vm._kappas_)

    vm._py_fitkappas(posteriors, obs, means)
    py_kappas = np.copy(vm._kappas_)
    np.testing.assert_array_almost_equal(py_kappas, c_kappas)
Exemplo n.º 10
0
def test_1():
    vm = VonMisesHMM(n_states=5)
    gm = GaussianHMM(n_components=5)
    X1 = np.random.randn(100, 2)
    yield lambda: vm.fit([X1])
    yield lambda: gm.fit([X1])