def test_gauss_function(self): f = tdt.GaussFunction(1, 0.5, 0.5) x = np.random.random((3,)) grad = np.zeros((3,)) grad[1] = -np.exp(-(0.5 * (0.5 - x[1]) ** 2) / 0.5) * (-0.5 + x[1]) / 0.5 self.assertEqual(f(x), np.exp(-0.5 * (x[1] - 0.5) ** 2 / 0.5)) self.assertEqual(f.partial(x, 0), 0) self.assertEqual(f.partial(x, 1), -np.exp(-(0.5 * (0.5 - x[1]) ** 2) / 0.5) * (-0.5 + x[1]) / 0.5) self.assertTrue((f.gradient(x) - grad == 0).all()) with self.assertRaises(ValueError): tdt.GaussFunction(1, 0.5, 0)
def plot_basis_functions(dimension, downsampling_rate, directory): """Plot basis functions Parameters ---------- dimension: int number of contact indices downsampling_rate: int downsampling rate for trajectory data directory: string directory data to load """ # load contact indices (sorted by relevance) contact_indices = np.load( directory + 'ntl9_contact_indices.npz')['indices'][:dimension] # load trajectories data, trajectory_lengths = load_data(directory, 1, contact_indices) # plot basis functions plt.figure(dpi=300) plt.hist(data[:10].flatten(), 10000, histtype='bar') x_values = np.arange(0, 1, 0.001)[:, None] y_values = tdt.GaussFunction(0, 0.285, 0.001)(x_values.T) plt.plot(x_values, 170000 * y_values, linewidth=2) y_values = tdt.GaussFunction(0, 0.62, 0.01)(x_values.T) plt.plot(x_values, 2500 * y_values, linewidth=2) plt.xlim([0, 1]) plt.xlabel(r"$\Delta [\mathrm{nm}]$") plt.savefig(directory + 'ntl9_basis_functions_1.pdf') plt.show() plt.figure(dpi=300) x = np.arange(0, 1, 0.001)[:, None] plt.plot(x, tdt.ConstantFunction(0)(x.T), linewidth=2) plt.plot(x, tdt.GaussFunction(0, 0.285, 0.001)(x.T), linewidth=2) plt.plot(x, tdt.GaussFunction(0, 0.62, 0.01)(x.T), linewidth=2) plt.xlim([0, 1]) plt.ylim([0, 1.2]) plt.xlabel(r"$\Delta [\mathrm{nm}]$") plt.savefig(directory + 'ntl9_basis_functions_2.pdf') plt.show()
def test_periodicgauss_function(self): f = tdt.PeriodicGaussFunction(1, 0.5, 0.5) x = np.random.random((3,)) grad = np.zeros((3,)) grad[1] = (0.5 * np.exp(-(0.5 * np.sin(0.5 * 0.5 - 0.5 * x[1]) ** 2) / 0.5) * np.cos(0.5 * 0.5 - 0.5 * x[1]) * np.sin(0.5 * 0.5 - 0.5 * x[1])) / 0.5 self.assertEqual(f(x), np.exp(-0.5 * np.sin(0.5 * (x[1] - 0.5)) ** 2 / 0.5)) self.assertEqual(f.partial(x, 0), 0) self.assertEqual(f.partial(x, 1), (0.5 * np.exp(-(0.5 * np.sin(0.5 * 0.5 - 0.5 * x[1]) ** 2) / 0.5) * np.cos(0.5 * 0.5 - 0.5 * x[1]) * np.sin(0.5 * 0.5 - 0.5 * x[1])) / 0.5) self.assertTrue((f.gradient(x) - grad == 0).all()) with self.assertRaises(ValueError): tdt.GaussFunction(1, 0.5, 0)
def tedmd_hocur(dimensions, downsampling_rate, integer_lag_times, max_rank, directory): """tEDMD using AMUSEt with HOSVD Parameters ---------- dimensions: list[int] numbers of contact indices downsampling_rate: int downsampling rate for trajectory data integer_lag_times: list[int] integer lag times for application of tEDMD max_rank: int maximum rank for HOCUR directory: string directory data to load """ # progress start_time = utl.progress('Apply AMUSEt (HOCUR)', 0) for i in range(len(dimensions)): # parameters time_step = 2e-3 lag_times = time_step * downsampling_rate * integer_lag_times # define basis list basis_list = [[ tdt.ConstantFunction(), tdt.GaussFunction(i, 0.285, 0.001), tdt.GaussFunction(i, 0.62, 0.01) ] for i in range(dimensions[i])] # progress utl.progress('Apply AMUSEt (HOCUR, p=' + str(dimensions[i]) + ')', 100 * i / len(dimensions), cpu_time=_time.time() - start_time) # load contact indices (sorted by relevance) contact_indices = np.load( directory + 'ntl9_contact_indices.npz')['indices'][:dimensions[i]] # load data data, trajectory_lengths = load_data(directory, downsampling_rate, contact_indices, progress=False) # select snapshot indices for x and y data x_indices = [] y_indices = [] for j in range(len(integer_lag_times)): x_ind, y_ind = xy_indices(trajectory_lengths, integer_lag_times[j]) x_indices.append(x_ind) y_indices.append(y_ind) # apply AMUSEt with utl.timer() as timer: eigenvalues, _ = tedmd.amuset_hocur(data, x_indices, y_indices, basis_list, max_rank=max_rank) cpu_time = timer.elapsed for j in range(len(integer_lag_times)): eigenvalues[j] = [eigenvalues[j][1]] # Save results to file: dic = {} dic["lag_times"] = lag_times dic["eigenvalues"] = eigenvalues dic["cpu_time"] = cpu_time np.savez_compressed( directory + "Results_NTL9_HOCUR_d" + str(dimensions[i]) + ".npz", **dic) utl.progress('Apply AMUSEt (HOCUR)', 100 * (i + 1) / len(dimensions), cpu_time=_time.time() - start_time)