def test_diagmatrix(self): traj = pt.iterload("./data/tz2.nc", "./data/tz2.parm7") state = pt.load_batch(traj, ''' #matrix covar @CA name mymat matrix dist @CA name mymat diagmatrix mymat vecs 6''') state.run() mat = state.data['mymat'] cpp_evals, cpp_evecs = state.data[-1].eigenvalues, state.data[ -1].eigenvectors # test triu_indices mat2 = mat.__class__() indices = np.triu_indices(mat.n_cols) mat2._set_data_half_matrix(mat._to_cpptraj_sparse_matrix(), mat.size, mat.n_cols) # OK data = matrix.diagonalize(mat.values, n_vecs=6, dtype='dataset')[-1] data2 = matrix.diagonalize(mat, n_vecs=6, dtype='dataset')[0] aa_eq(data.eigenvalues, cpp_evals, decimal=10) aa_eq(data2.eigenvalues, cpp_evals, decimal=10) # try numpy np_vals, np_vecs = np.linalg.eigh(mat2.values, UPLO='U') np_vals = np_vals[::-1][:6] np_vecs = np_vecs[:, ::-1].T[:6] # at least the absolute values are equal aa_eq(np.abs(data.eigenvectors), np.abs(cpp_evecs)) aa_eq(np.abs(np_vecs), np.abs(cpp_evecs)) # test raise if not having supported dtype self.assertRaises(ValueError, lambda: pt.matrix.diagonalize(mat, 3, dtype='ndarray')) # plot def plot_(): try: from pytraj.plot import plot_matrix from matplotlib import pyplot as plt fig, ax, bar = plot_matrix(np.abs(data.eigenvectors) - np.abs( cpp_evecs)) fig.colorbar(bar) plt.title('data vs cpp_evecs') fig, ax, bar = plot_matrix(np.abs(np_vecs) - np.abs(cpp_evecs)) fig.colorbar(bar) plt.title('np vs cpp_evecs') fig, ax, bar = plot_matrix(np.abs(np_vecs) - np.abs( data.eigenvectors)) fig.colorbar(bar) plt.title('np vs data') pt.show() except ImportError: pass
def test_diagmatrix(self): traj = pt.iterload(fn('tz2.nc'), fn('tz2.parm7')) state = pt.load_batch( traj, ''' #matrix covar @CA name mymat matrix dist @CA name mymat diagmatrix mymat vecs 6''') state.run() mat = state.data['mymat'] cpp_evals, cpp_evecs = state.data[-1].eigenvalues, state.data[ -1].eigenvectors # test triu_indices mat2 = mat.__class__() np.triu_indices(mat.n_cols) mat2._set_data_half_matrix(mat._to_cpptraj_sparse_matrix(), mat.size, mat.n_cols) # OK data = matrix.diagonalize(mat.values, n_vecs=6, dtype='dataset')[-1] data2 = matrix.diagonalize(mat, n_vecs=6, dtype='dataset')[0] aa_eq(data.eigenvalues, cpp_evals, decimal=10) aa_eq(data2.eigenvalues, cpp_evals, decimal=10) # try numpy np_vals, np_vecs = np.linalg.eigh(mat2.values, UPLO='U') np_vals = np_vals[::-1][:6] np_vecs = np_vecs[:, ::-1].T[:6] # at least the absolute values are equal aa_eq(np.abs(data.eigenvectors), np.abs(cpp_evecs)) aa_eq(np.abs(np_vecs), np.abs(cpp_evecs)) # test raise if not having supported dtype self.assertRaises( ValueError, lambda: pt.matrix.diagonalize(mat, 3, dtype='ndarray'))
def nh_order_parameters(traj, vector_pairs, order=2, tstep=1., tcorr=10000., n_cores=1, **kwargs): '''compute NH order parameters Parameters ---------- traj : Trajectory-like vector_pairs : 2D array-like, shape (n_pairs, 2) order : default 2 tstep : default 1. tcorr : default 10000. kwargs : additional keyword argument Returns ------- S2 : 1D array, order parameters Examples -------- >>> import pytraj as pt >>> traj = pt.datafiles.load_tz2_ortho() >>> h_indices = pt.select('@H', traj.top) >>> n_indices = h_indices - 1 >>> nh_pairs = list(zip(n_indices, h_indices)) >>> data = pt.nh_order_parameters(traj, nh_pairs) ''' from pytraj import matrix # compute N-H vectors and ired matrix if n_cores == 1: vecs_and_mat = ired_vector_and_matrix( traj, vector_pairs, order=order, dtype='tuple') else: # use _pmap to avoid cicular import from pytraj import _pmap vecs_and_mat = _pmap( ired_vector_and_matrix, traj, vector_pairs, order=order, dtype='tuple', n_cores=n_cores) state_vecs = vecs_and_mat[0] mat_ired = vecs_and_mat[1] # get eigenvalues and eigenvectors modes = matrix.diagonalize( mat_ired, n_vecs=len(state_vecs), dtype='dataset')[0] evals, evecs = modes.eigenvalues, modes.eigenvectors data = _ired( state_vecs, modes=(evecs, evals), NHbond=True, tcorr=tcorr, tstep=tstep, **kwargs) order = [d.values.copy() for d in data if 'S2' in d.key][0] return order
def NH_order_parameters(traj, vector_pairs, order=2, tstep=1., tcorr=10000., n_cores=1): '''compute NH order parameters Parameters ---------- traj : Trajectory-like vector_pairs : 2D array-like, shape (n_pairs, 2) order : default 2 tstep : default 1. tcorr : default 10000. Returns ------- S2 : 1D array, order parameters Examples -------- >>> import pytraj as pt >>> traj = pt.datafiles.load_tz2_ortho() >>> h_indices = pt.select('@H', traj.top) >>> n_indices = h_indices - 1 >>> nh_pairs = list(zip(n_indices, h_indices)) >>> data = pt.NH_order_parameters(traj, nh_pairs) ''' from pytraj import matrix # compute N-H vectors and ired matrix if n_cores == 1: vecs_and_mat = ired_vector_and_matrix(traj, vector_pairs, order=order, dtype='tuple') else: # use _pmap to avoid cicular import from pytraj import _pmap vecs_and_mat = _pmap(ired_vector_and_matrix, traj, vector_pairs, order=order, dtype='tuple', n_cores=n_cores) state_vecs = vecs_and_mat[0] mat_ired = vecs_and_mat[1] # get eigenvalues and eigenvectors modes = matrix.diagonalize(mat_ired, n_vecs=len(state_vecs), dtype='dataset')[0] evals, evecs = modes.eigenvalues, modes.eigenvectors data = _ired(state_vecs, modes=(evals, evecs), NHbond=True, tcorr=tcorr, tstep=tstep) order = [d.values.copy() for d in data if 'S2' in d.key][0] return order