Beispiel #1
0
def test_whitening_on_whitened():
    data = np.random.normal(size=(1000, 50))
    from sklearn.decomposition import PCA
    data = PCA(whiten=True).fit_transform(data)
    cov = Covariance().fit(data).fetch_model()
    whitened = cov.whiten(data)
    np.testing.assert_array_almost_equal(whitened, data)
Beispiel #2
0
 def test_XX_weighted_meanconst(self):
     est = Covariance(lagtime=self.lag, compute_c0t=False, bessels_correction=False)
     cc = est.fit(self.data - self.mean_const, weights=self.data_weights).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.mx_c_wobj_lag0)
     np.testing.assert_allclose(cc.cov_00, self.Mxx_c_wobj_lag0)
     cc = est.fit(self.data - self.mean_const, weights=self.data_weights, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx_c_wobj_lag0[:, self.cols_2])
Beispiel #3
0
 def test_XX_weightobj_meanfree(self):
     # many passes
     est = Covariance(lagtime=self.lag, compute_c0t=False, remove_data_mean=True, bessels_correction=False)
     cc = est.fit(self.data, weights=self.data_weights, n_splits=10).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.mx_wobj_lag0)
     np.testing.assert_allclose(cc.cov_00, self.Mxx0_wobj_lag0)
     cc = est.fit(self.data, column_selection=self.cols_2, weights=self.data_weights).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx0_wobj_lag0[:, self.cols_2])
Beispiel #4
0
 def test_XX_with_mean(self):
     # many passes
     est = Covariance(lagtime=self.lag, compute_c0t=False, remove_data_mean=False, bessels_correction=False)
     cc = est.fit(self.data).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.mx_lag0)
     np.testing.assert_allclose(cc.cov_00, self.Mxx_lag0)
     cc = est.fit(self.data, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx_lag0[:, self.cols_2])
Beispiel #5
0
 def test_XY_sym_meanconst(self):
     est = Covariance(lagtime=self.lag, compute_c0t=True, reversible=True, bessels_correction=False)
     cc = est.fit(self.Xc_lag0).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.m_c_sym)
     np.testing.assert_allclose(cc.cov_00, self.Mxx_c_sym)
     np.testing.assert_allclose(cc.cov_0t, self.Mxy_c_sym)
     cc = est.fit(self.Xc_lag0, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx_c_sym[:, self.cols_2])
     np.testing.assert_allclose(cc.cov_0t, self.Mxy_c_sym[:, self.cols_2])
Beispiel #6
0
def test_multiple_fetch():
    # checks that the model instance does not change when the estimator was not updated
    data = np.random.normal(size=(5000, 3))
    est = Covariance(5, compute_c00=True, compute_c0t=False, compute_ctt=False)
    m1 = est.fit(data).model
    m2 = est.model
    m3 = est.partial_fit(np.random.normal(size=(50, 3))).model
    np.testing.assert_(m1 is m2)
    np.testing.assert_(m1 is not m3)
    np.testing.assert_(m2 is not m3)
Beispiel #7
0
def test_fit_from_cov():
    data = np.random.normal(size=(500, 3))
    # fitting with C0t and symmetric covariances, should pass
    TICA().fit(Covariance(1, compute_c0t=True, reversible=True).fit(data))

    with np.testing.assert_raises(ValueError):
        TICA().fit(Covariance(1, compute_c0t=True, reversible=False).fit(data))

    with np.testing.assert_raises(ValueError):
        TICA().fit(Covariance(1, compute_c0t=False, reversible=True).fit(data))
Beispiel #8
0
 def test_XXXY_meanfree(self):
     # many passes
     est = Covariance(lagtime=self.lag, remove_data_mean=True, compute_c0t=True, bessels_correction=False)
     cc = est.fit(self.data).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.mx)
     np.testing.assert_allclose(cc.mean_t, self.my)
     np.testing.assert_allclose(cc.cov_00, self.Mxx0)
     np.testing.assert_allclose(cc.cov_0t, self.Mxy0)
     cc = est.fit(self.data, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx0[:, self.cols_2])
     np.testing.assert_allclose(cc.cov_0t, self.Mxy0[:, self.cols_2])
Beispiel #9
0
 def test_XXXY_sym_withmean(self):
     # many passes
     est = Covariance(lagtime=self.lag, remove_data_mean=False, compute_c0t=True, reversible=True,
                      bessels_correction=False)
     cc = est.fit(self.data).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.m_sym)
     np.testing.assert_allclose(cc.cov_00, self.Mxx_sym)
     np.testing.assert_allclose(cc.cov_0t, self.Mxy_sym)
     cc = est.fit(self.data, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx_sym[:, self.cols_2])
     np.testing.assert_allclose(cc.cov_0t, self.Mxy_sym[:, self.cols_2])
Beispiel #10
0
def test_weights():
    weights = np.concatenate([np.ones((1001,)) * 1e-16, np.ones((3999,))])
    np.testing.assert_equal(len(weights), 5000)
    data = np.random.normal(size=(5000, 2))
    cov = Covariance(lagtime=5, compute_c00=True, compute_c0t=True, compute_ctt=False)
    model = cov.fit(data, weights=weights, n_splits=64).fetch_model()
    model2 = cov.fit(data[1002:], weights=weights[1002:], n_splits=55).fetch_model()
    np.testing.assert_array_almost_equal(model.cov_00, model2.cov_00, decimal=2)
    np.testing.assert_array_almost_equal(model.cov_0t, model2.cov_0t, decimal=2)
    np.testing.assert_array_almost_equal(model.mean_0, model2.mean_0, decimal=2)
    np.testing.assert_array_almost_equal(model.mean_t, model2.mean_t, decimal=2)
Beispiel #11
0
 def test_XXXY_weightobj_sym_meanfree(self):
     # many passes
     est = Covariance(lagtime=self.lag, remove_data_mean=True, compute_c0t=True, reversible=True,
                      bessels_correction=False)
     cc = est.fit(self.data, weights=self.data_weights).fetch_model()
     np.testing.assert_allclose(cc.mean_0, self.m_sym_wobj)
     np.testing.assert_allclose(cc.cov_00, self.Mxx0_sym_wobj)
     np.testing.assert_allclose(cc.cov_0t, self.Mxy0_sym_wobj)
     cc = est.fit(self.data, weights=self.data_weights, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx0_sym_wobj[:, self.cols_2])
     np.testing.assert_allclose(cc.cov_0t, self.Mxy0_sym_wobj[:, self.cols_2])
Beispiel #12
0
    def test_non_matching_length(self):
        n = 100
        data = [np.random.random(size=(n, 2)) for n in range(3)]
        data = (data[:-1], data[1:])
        weights = [np.random.random(n) for _ in range(3)]
        weights[0] = weights[0][:-3]
        with self.assertRaises(ValueError):
            Covariance(1, compute_c0t=True).fit(data, weights=weights)

        with self.assertRaises(ValueError):
            Covariance(1, compute_c0t=True).fit(data, weights=weights[:10])
Beispiel #13
0
 def test_XXXY_withmean(self):
     # many passes
     est = Covariance(lagtime=self.lag, remove_data_mean=False, compute_c0t=True, bessels_correction=False)
     cc = est.fit(self.data, n_splits=1).fetch_model()
     assert not cc.bessels_correction
     np.testing.assert_allclose(cc.mean_0, self.mx)
     np.testing.assert_allclose(cc.mean_t, self.my)
     np.testing.assert_allclose(cc.cov_00, self.Mxx)
     np.testing.assert_allclose(cc.cov_0t, self.Mxy)
     cc = est.fit(self.data, n_splits=1, column_selection=self.cols_2).fetch_model()
     np.testing.assert_allclose(cc.cov_00, self.Mxx[:, self.cols_2])
     np.testing.assert_allclose(cc.cov_0t, self.Mxy[:, self.cols_2])
Beispiel #14
0
 def test_XXXY_weightobj_meanfree(self):
     for n_splits in [1, 2, 3, 4, 5, 6, 7]:
         est = Covariance(lagtime=self.lag, remove_data_mean=True, compute_c0t=True, bessels_correction=False)
         cc = est.fit(self.data, weights=self.data_weights, n_splits=n_splits).fetch_model()
         np.testing.assert_allclose(cc.mean_0, self.mx_wobj)
         np.testing.assert_allclose(cc.mean_t, self.my_wobj)
         np.testing.assert_allclose(cc.cov_00, self.Mxx0_wobj)
         np.testing.assert_allclose(cc.cov_0t, self.Mxy0_wobj)
         cc = est.fit(self.data, weights=self.data_weights, column_selection=self.cols_2,
                      n_splits=n_splits).fetch_model()
         np.testing.assert_allclose(cc.cov_00, self.Mxx0_wobj[:, self.cols_2])
         np.testing.assert_allclose(cc.cov_0t, self.Mxy0_wobj[:, self.cols_2])
Beispiel #15
0
 def test_re_estimate_weight_types(self):
     # check different types are allowed and re-estimation works
     x = np.random.random((100, 2))
     c = Covariance(lagtime=1, compute_c0t=True)
     c.fit(x, weights=np.ones((len(x),))).fetch_model()
     c.fit(x, weights=np.ones((len(x),))).fetch_model()
     c.fit(x, weights=None).fetch_model()
     c.fit(x, weights=x[:, 0]).fetch_model()
Beispiel #16
0
def test_eig_corr(epsilon, method, canonical_signs, return_rank,
                  hermitian_ctt):
    data = np.random.normal(size=(5000, 3))
    from deeptime.covariance import Covariance
    covariances = Covariance(lagtime=10, compute_c00=True,
                             compute_ctt=True).fit(data).fetch_model()
    if not hermitian_ctt:
        covariances.cov_tt[0, 1] += 1e-6
        assert_(not np.allclose(covariances.cov_tt, covariances.cov_tt.T))
    out = eig_corr(covariances.cov_00,
                   covariances.cov_tt,
                   epsilon=epsilon,
                   method=method,
                   canonical_signs=canonical_signs,
                   return_rank=return_rank)
    eigenvalues = out[0]
    eigenvectors = out[1]
    if return_rank:
        rank = out[2]
        assert_equal(rank, len(eigenvalues))

    for r in range(len(out[0])):
        assert_array_almost_equal(covariances.cov_00 @ eigenvectors[r] *
                                  eigenvalues[r],
                                  covariances.cov_tt @ eigenvectors[r],
                                  decimal=2)
Beispiel #17
0
    def test_weights_close_to_zero(self):
        n = 1000
        data = [np.random.random(size=(n, 2)) for _ in range(5)]

        # create some artificial correlations
        data[0][:, 0] *= np.random.randint(n)
        data = np.asarray(data)

        weights = [np.ones(n, dtype=np.float32) for _ in range(5)]
        # omit the first trajectory by setting a weight close to zero.
        weights[0][:] = 1E-44
        weights = np.asarray(weights)

        est = Covariance(lagtime=1, compute_c0t=True)
        for data_traj, weights_traj in zip(data, weights):
            est.partial_fit((data_traj[:-3], data_traj[3:]), weights=weights_traj[:-3])
        cov = est.fetch_model()
        # cov = covariance_lagged(data, lag=3, weights=weights, chunksize=10)
        assert np.all(cov.cov_00 < 1)
Beispiel #18
0
def test_sanity(fixed_seed, two_state_hmm, model):
    traj, traj_rot, loader = two_state_hmm
    tae = setup_tae() if model == 'tae' else setup_tvae()
    tae.fit(loader, n_epochs=40)
    out = tae.transform(traj_rot).reshape((-1, 1))
    out = Covariance().fit(out).fetch_model().whiten(out)
    dtraj = dt.clustering.Kmeans(2).fit(out).transform(out)
    msm = dt.markov.msm.MaximumLikelihoodMSM().fit_from_discrete_timeseries(
        dtraj, 1).fetch_model()
    assert_array_almost_equal(msm.transition_matrix, [[.9, .1], [.1, .9]],
                              decimal=1)
Beispiel #19
0
def test_weights_incompatible():
    data = np.random.normal(size=(5000, 3))
    est = Covariance(5)
    with np.testing.assert_raises(ValueError):
        est.fit(data, weights=np.arange(10))  # incompatible shape

    with np.testing.assert_raises(ValueError):
        est.fit(data, weights=np.ones((len(data), 2)))  # incompatible shape
Beispiel #20
0
def test_whitening():
    data = np.random.normal(size=(5000, 50))
    data = Covariance().fit(data).fetch_model().whiten(data)
    cov = Covariance().fit(data).fetch_model()
    np.testing.assert_array_almost_equal(cov.cov_00, np.eye(50), decimal=2)
    np.testing.assert_array_almost_equal(cov.mean_0, np.zeros_like(cov.mean_0))