def test_skip(self): for skip in [0, 3, 13]: r1 = FeatureReader(self.trajfile, self.topfile) out_with_skip = r1.get_output(skip=skip)[0] r2 = FeatureReader(self.trajfile, self.topfile) out = r2.get_output()[0] np.testing.assert_almost_equal(out_with_skip, out[skip::], err_msg="The first %s rows were skipped, but that did not " "match the rows with skip=0 and sliced by [%s::]" % (skip, skip))
def test_skip_input_list(self): for skip in [0, 3, 13]: r1 = FeatureReader([self.trajfile, self.trajfile2], self.topfile) out_with_skip = r1.get_output(skip=skip) r2 = FeatureReader([self.trajfile, self.trajfile2], self.topfile) out = r2.get_output() np.testing.assert_almost_equal(out_with_skip[0], out[0][skip::], err_msg="The first %s rows of the first file were skipped, but that did not " "match the rows with skip=0 and sliced by [%s::]" % (skip, skip)) np.testing.assert_almost_equal(out_with_skip[1], out[1][skip::], err_msg="The first %s rows of the second file were skipped, but that did not" " match the rows with skip=0 and sliced by [%s::]" % (skip, skip))
def test_covariances_and_eigenvalues(self): reader = FeatureReader(self.trajnames, self.temppdb) trans = TICA(lag=1, output_dimension=self.dim, force_eigenvalues_le_one=True) trans.data_producer = reader for tau in [1, 10, 100, 1000, 2000]: log.info('number of trajectories reported by tica %d' % trans.number_of_trajectories()) trans.lag = tau trans.parametrize() data = trans.get_output() # print '@@cov', trans.cov # print '@@cov_tau', trans.cov_tau log.info('max. eigenvalue: %f' % np.max(trans.eigenvalues)) self.assertTrue(np.all(trans.eigenvalues <= 1.0)) # check ICs check = tica(data=data, lag=tau, dim=self.dim, force_eigenvalues_le_one=True) check.parametrize() self.assertTrue(np.allclose(np.eye(self.dim), check.cov)) ic_cov_tau = np.zeros((self.dim, self.dim)) ic_cov_tau[np.diag_indices(self.dim)] = trans.eigenvalues self.assertTrue(np.allclose(ic_cov_tau, check.cov_tau))
def testTimeLaggedIterator(self): lag = 10 reader = FeatureReader(self.trajfile, self.topfile) frames = 0 data = [] lagged = [] for _, X, Y in reader.iterator(lag=lag): frames += X.shape[0] data.append(X) lagged.append(Y) assert len(data) == len(lagged) # .reshape(self.xyz.shape) merged_lagged = np.concatenate(lagged, axis=0) # reproduce outcome xyz_s = self.xyz.shape fake_lagged = self.xyz.reshape((xyz_s[0], -1))[lag:] self.assertTrue(np.allclose(merged_lagged, fake_lagged)) # restore shape of input data = np.vstack(data).reshape(self.xyz[lag:].shape) self.assertEqual(frames, reader.trajectory_lengths()[0] - lag) self.assertTrue(np.allclose(data, self.xyz[:len(self.xyz) - lag]))
def test_covariances_and_eigenvalues(self): reader = FeatureReader(self.trajnames, self.temppdb) for tau in [1, 10, 100, 1000, 2000]: trans = tica(lag=tau, dim=self.dim, kinetic_map=False) trans.data_producer = reader log.info('number of trajectories reported by tica %d' % trans.number_of_trajectories()) trans.parametrize() data = trans.get_output() # print '@@cov', trans.cov # print '@@cov_tau', trans.cov_tau log.info('max. eigenvalue: %f' % np.max(trans.eigenvalues)) self.assertTrue(np.all(trans.eigenvalues <= 1.0)) # check ICs check = tica(data=data, lag=tau, dim=self.dim, force_eigenvalues_le_one=True) check.parametrize() np.testing.assert_allclose(np.eye(self.dim), check.cov, atol=1e-8) np.testing.assert_allclose(check.mean, 0.0, atol=1e-8) ic_cov_tau = np.zeros((self.dim, self.dim)) ic_cov_tau[np.diag_indices(self.dim)] = trans.eigenvalues np.testing.assert_allclose(ic_cov_tau, check.cov_tau, atol=1e-8)
def test_partial_fit(self): reader = FeatureReader(self.trajnames, self.temppdb, chunksize=10000) output = reader.get_output() params = {'dim': self.dim, 'lag': 1001} ref = api.tica(reader, **params) partial = api.tica(**params) for traj in output: partial.partial_fit(traj) np.testing.assert_allclose(partial.eigenvalues, ref.eigenvalues, atol=1e-3) # only compare first two eigenvectors, because we only have two metastable processes np.testing.assert_allclose(np.abs(partial.eigenvectors[:2]), np.abs(ref.eigenvectors[:2]), rtol=1e-3, atol=1e-3)
def testIteratorAccess2(self): reader = FeatureReader([self.trajfile, self.trajfile2], self.topfile) reader.chunksize = 100 data = {itraj: [] for itraj in range(reader.number_of_trajectories())} for i, X in reader: data[i].append(X) # restore shape of input data[0] = np.vstack(data[0]).reshape(-1, 9) data[1] = np.vstack(data[1]).reshape(-1, 9) np.testing.assert_equal(data[0], self.xyz.reshape(-1, 9)) np.testing.assert_equal(data[1], self.xyz2.reshape(-1, 9))
def testIteratorAccess2(self): reader = FeatureReader([self.trajfile, self.trajfile], self.topfile) reader.chunksize = 100 frames = 0 data = [] for i, X in reader: frames += X.shape[0] data.append(X) self.assertEqual(frames, reader.trajectory_lengths()[0] * 2) # restore shape of input data = np.array(data[0:reader.trajectory_lengths()[0] / reader.chunksize]).reshape(self.xyz.shape) self.assertTrue(np.allclose(data, self.xyz))
def test_covariances_and_eigenvalues(self): reader = FeatureReader(self.trajnames, self.temppdb, chunksize=10000) for lag in [1, 11, 101, 1001, 2001]: # avoid cos(w*tau)==0 trans = api.tica(data=reader, dim=self.dim, lag=lag) log.info('number of trajectories reported by tica %d' % trans.number_of_trajectories()) log.info('tau = %d corresponds to a number of %f cycles' % (lag, self.w*lag/(2.0*np.pi))) # analytical solution for C_ij(lag) is 0.5*A[i]*A[j]*cos(phi[i]-phi[j])*cos(w*lag) ana_cov = 0.5*self.A[:, np.newaxis]*self.A*np.cos(self.phi[:, np.newaxis]-self.phi) ana_cov_tau = ana_cov*np.cos(self.w*lag) self.assertTrue(np.allclose(ana_cov, trans.cov, atol=1.E-3)) self.assertTrue(np.allclose(ana_cov_tau, trans.cov_tau, atol=1.E-3)) log.info('max. eigenvalue: %f' % np.max(trans.eigenvalues)) self.assertTrue(np.all(trans.eigenvalues <= 1.0))
def test_covariances_and_eigenvalues(self): reader = FeatureReader(self.trajnames, self.temppdb) for tau in [1, 10, 100, 1000, 2000]: trans = tica(lag=tau, dim=self.dim, kinetic_map=False) trans.estimate(reader) data = trans.get_output() log.info('max. eigenvalue: %f' % np.max(trans.eigenvalues)) self.assertTrue(np.all(trans.eigenvalues <= 1.0)) # check ICs check = tica(data=data, lag=tau, dim=self.dim) np.testing.assert_allclose(np.eye(self.dim), check.cov, atol=1e-8) np.testing.assert_allclose(check.mean, 0.0, atol=1e-8) ic_cov_tau = np.zeros((self.dim, self.dim)) ic_cov_tau[np.diag_indices(self.dim)] = trans.eigenvalues np.testing.assert_allclose(ic_cov_tau, check.cov_tau, atol=1e-8)
def test_featurereader_xtc(self): # cause cache failures with settings(use_trajectory_lengths_cache=False): reader = FeatureReader(xtcfiles, pdbfile) results = {} for f in xtcfiles: traj_info = self.db[f, reader] results[f] = traj_info.ndim, traj_info.length, traj_info.offsets expected = {} for f in xtcfiles: with mdtraj.open(f) as fh: length = len(fh) ndim = fh.read(1)[0].shape[1] offsets = fh.offsets if hasattr(fh, 'offsets') else [] expected[f] = ndim, length, offsets np.testing.assert_equal(results, expected)