Пример #1
0
    def do_test(self, dim, rank, test_partial_fit=False):
        # setup
        N_frames = [123, 456, 789]
        N_trajs = len(N_frames)
        A = random_matrix(dim, rank)
        trajs = []
        mean = np.random.randn(dim)
        for i in range(N_trajs):
            # set up data
            white = np.random.randn(N_frames[i], dim)
            brown = np.cumsum(white, axis=0)
            correlated = np.dot(brown, A)
            trajs.append(correlated + mean)

        # test
        tau = 50
        vamp = VAMP(scaling=None, lagtime=tau).fit(trajs).fetch_model()

        assert vamp.output_dimension <= rank

        atol = np.finfo(np.float32).eps * 10.0
        rtol = np.finfo(np.float32).resolution
        phi_trajs = [vamp.backward(X, propagate=False)[tau:, :] for X in trajs]
        phi = np.concatenate(phi_trajs)
        mean_right = phi.sum(axis=0) / phi.shape[0]
        cov_right = phi.T.dot(phi) / phi.shape[0]
        np.testing.assert_allclose(mean_right, 0.0, rtol=rtol, atol=atol)
        np.testing.assert_allclose(cov_right, np.eye(vamp.output_dimension), rtol=rtol, atol=atol)

        vamp.right = False
        # vamp = estimate_vamp(trajs, lag=tau, scaling=None, right=False)
        psi_trajs = [vamp.forward(X, propagate=False)[0:-tau, :] for X in trajs]
        psi = np.concatenate(psi_trajs)
        mean_left = psi.sum(axis=0) / psi.shape[0]
        cov_left = psi.T.dot(psi) / psi.shape[0]
        np.testing.assert_allclose(mean_left, 0.0, rtol=rtol, atol=atol)
        np.testing.assert_allclose(cov_left, np.eye(vamp.output_dimension), rtol=rtol, atol=atol)

        # compute correlation between left and right
        assert phi.shape[0] == psi.shape[0]
        C01_psi_phi = psi.T.dot(phi) / phi.shape[0]
        n = max(C01_psi_phi.shape)
        C01_psi_phi = C01_psi_phi[0:n, :][:, 0:n]
        np.testing.assert_allclose(C01_psi_phi, np.diag(vamp.singular_values[0:vamp.output_dimension]), rtol=rtol,
                                   atol=atol)

        if test_partial_fit:
            vamp2 = VAMP(lagtime=tau).fit(trajs).fetch_model()

            atol = 1e-14
            rtol = 1e-5

            np.testing.assert_allclose(vamp.singular_values, vamp2.singular_values)
            np.testing.assert_allclose(vamp.mean_0, vamp2.mean_0, atol=atol, rtol=rtol)
            np.testing.assert_allclose(vamp.mean_t, vamp2.mean_t, atol=atol, rtol=rtol)
            np.testing.assert_allclose(vamp.cov_00, vamp2.cov_00, atol=atol, rtol=rtol)
            np.testing.assert_allclose(vamp.cov_0t, vamp2.cov_0t, atol=atol, rtol=rtol)
            np.testing.assert_allclose(vamp.cov_tt, vamp2.cov_tt, atol=atol, rtol=rtol)
            np.testing.assert_allclose(vamp.epsilon, vamp2.epsilon, atol=atol, rtol=rtol)
            np.testing.assert_allclose(vamp.output_dimension, vamp2.output_dimension, atol=atol, rtol=rtol)
            np.testing.assert_equal(vamp.scaling, vamp2.scaling)
            assert_allclose_ignore_phase(vamp.singular_vectors_left, vamp2.singular_vectors_left, atol=atol)
            assert_allclose_ignore_phase(vamp.singular_vectors_right, vamp2.singular_vectors_right, atol=rtol)

            # vamp2.singular_values # trigger diagonalization
            for t, ref in zip(trajs, phi_trajs):
                assert_allclose_ignore_phase(vamp2.backward(t[tau:], propagate=False), ref, rtol=rtol, atol=atol)

            for t, ref in zip(trajs, psi_trajs):
                assert_allclose_ignore_phase(vamp2.transform(t[0:-tau], propagate=False), ref, rtol=rtol, atol=atol)
from deeptime.data import position_based_fluids
from deeptime.decomposition import VAMP

pbf_simulator = position_based_fluids(n_burn_in=500, n_jobs=8)
trajectory = pbf_simulator.simulate_oscillatory_force(n_oscillations=3,
                                                      n_steps=400)
n_grid_x = 20
n_grid_y = 10
kde_trajectory = pbf_simulator.transform_to_density(trajectory,
                                                    n_grid_x=n_grid_x,
                                                    n_grid_y=n_grid_y,
                                                    n_jobs=8)
tau = 100
model = VAMP(lagtime=100).fit(kde_trajectory).fetch_model()
projection_left = model.forward(kde_trajectory, propagate=False)
projection_right = model.backward(kde_trajectory, propagate=False)

f, ax = plt.subplots(1, 1, figsize=(5, 5))
start = 400
stop = len(kde_trajectory) - tau  # 5000
left = projection_left[:-tau][start:stop, 0]
right = projection_right[tau:][start:stop, 0]
lw = 4
ax.plot(np.arange(start, stop), left, label="left", linewidth=lw)
ax.plot(np.arange(start, stop)[::50],
        right[::50],
        '--',
        label="right",
        linewidth=3,
        markersize=12)