def test_its_estimated_with_only_ts(self): its = ImpliedTimescales(estimator=self.estimator, lags=[1, 2, 3], only_timescales=True, n_jobs=1) its.estimate(self.dtraj) plot_implied_timescales(its)
def test_its_estimated_with_only_ts_samples(self): from pyemma.msm import BayesianMSM its = ImpliedTimescales(estimator=BayesianMSM(nsamples=2), lags=[1, 2, 3], only_timescales=True) its.estimate(self.dtraj) plot_implied_timescales(its)
def plotTimescales(self, lags=None, errors=None, nits=None, results=False, plot=True): """ Plot the implied timescales of MSMs of various lag times Parameters ---------- lags : list The lag times at which to compute the timescales. By default it spreads out 25 lag times linearly from lag 10 until the mode length of the trajectories. errors : errors Calculate errors using Bayes (Refer to pyEMMA documentation) nits : int Number of implied timescales to calculate. Default: all results : bool If the method should return the calculated implied timescales plot : bool If the method should display the plot of implied timescales Returns ------- If given `results`=True this method will return the following data its : np.ndarray The calculated implied timescales. 2D array with dimensions (len(`lags`), `nits`) lags : np.ndarray A list of the lag times that were used to calculate the implied timescales Examples -------- >>> model = Model(data) >>> model.plotTimescales() >>> model.plotTimescales(lags=list(range(1,100,5))) """ import pyemma.plots as mplt import pyemma.msm as msm self._integrityCheck() if lags is None: lags = self._defaultLags() if nits is None: nits = np.min((self.data.K, 20)) from htmd.config import _config its = msm.its(self.data.St.tolist(), lags=lags, errors=errors, nits=nits, n_jobs=_config['ncpus']) if plot: plt.ion() plt.figure() mplt.plot_implied_timescales(its, dt=self.data.fstep, units='ns') plt.show() if results: return its.get_timescales(), its.lags
def plotTimescales(self, maxlag, numstates): import pyemma.msm as msm import pyemma.plots as mplt its = msm.timescales_hmsm(self.data.St.tolist(), numstates, lags=maxlag) mplt.plot_implied_timescales(its, ylog=True, units='ns', dt=self.data.fstep, linewidth=2)
def plotTimescales(self, lags=None, units='frames', errors=None, nits=None, results=False, plot=True): """ Plot the implied timescales of MSMs of various lag times Parameters ---------- lags : list The lag times at which to compute the timescales. By default it spreads out 25 lag times linearly from lag 10 until the mode length of the trajectories. units : str The units of lag. Can be 'frames' or any time unit given as a string. errors : errors Calculate errors using Bayes (Refer to pyEMMA documentation) nits : int Number of implied timescales to calculate. Default: all results : bool If the method should return the calculated implied timescales plot : bool If the method should display the plot of implied timescales Returns ------- If given `results`=True this method will return the following data its : np.ndarray The calculated implied timescales. 2D array with dimensions (len(`lags`), `nits`) lags : np.ndarray A list of the lag times that were used to calculate the implied timescales Examples -------- >>> model = Model(data) >>> model.plotTimescales() >>> model.plotTimescales(lags=list(range(1,100,5))) """ import pyemma.plots as mplt import pyemma.msm as msm self._integrityCheck() if lags is None: lags = self._defaultLags() else: lags = unitconvert(units, 'frames', lags, fstep=self.data.fstep).tolist() if nits is None: nits = np.min((self.data.K, 20)) from htmd.config import _config its = msm.its(self.data.St.tolist(), lags=lags, errors=errors, nits=nits, n_jobs=_config['ncpus']) if plot: from matplotlib import pylab as plt plt.ion() plt.figure() mplt.plot_implied_timescales(its, dt=self.data.fstep, units='ns') plt.show() if results: return its.get_timescales(), its.lags
def _calculateITS(self): is_converged = False # its print(("Calculating implied time-scales, when it's done will prompt " "for confirmation on the validity of the lagtimes...")) while not is_converged: if not self.error: itsErrors = None elif self.error: itsErrors = "bayes" if self.lagtimes and self.lagtimes is not None: # workaround to get new its plot at each iteration, the # plot_implied_timescales function is calling plt.gca() and # recovers the previous plot's axes, by creating a new figure # gca gets a set of empty axes and plots are fine plt.figure() its_object = msm.its(self.dtrajs, lags=self.lagtimes, errors=itsErrors) mplt.plot_implied_timescales(its_object, outfile=self.itsOutput, nits=self.numberOfITS) plt.savefig("its.png") if self.lagtime is not None: return self.lagtime while True: plt.show() convergence_answer = raw_input("Has the ITS plot converged?[y/n] ") convergence_answer.rstrip() convergence_answer = convergence_answer or "y" # Making yes the default answer if convergence_answer.lower() == "y" or convergence_answer.lower() == "yes": is_converged = True lagtime_str = raw_input("Please input the lagtime to construct the MSM: ") lagtime = int(lagtime_str.rstrip()) break elif convergence_answer.lower() == "n" or convergence_answer.lower() == "no": break else: print("Answer not valid. Please answer yes or no") if not is_converged: new_lagtimes = raw_input("Do you want to define new lagtimes or add to the previous?[add(a)/new(n)] ") new_lagtimes.rstrip() if new_lagtimes.lower() == "add" or new_lagtimes.lower() == "a": lag_list = raw_input("Please input the lagtimes you want to add separated by a space: ") lag_list.rstrip() self.lagtimes.extend(map(int, lag_list.split(" "))) elif new_lagtimes.lower() == "new" or new_lagtimes.lower() == "n": lag_list = raw_input("Please input the new lagtimes separated by a space: ") lag_list.rstrip() self.lagtimes = map(int, lag_list.split(" ")) self.lagtimes.sort() return lagtime
def _calculateITS(self): print("Calculating implied time-scales") if not self.error: itsErrors = None elif self.error: itsErrors = "bayes" if self.lagtimes and self.lagtimes is not None: # workaround to get new its plot at each iteration, the # plot_implied_timescales function is calling plt.gca() and # recovers the previous plot's axes, by creating a new figure # gca gets a set of empty axes and plots are fine plt.figure() its_object = msm.its(self.dtrajs, lags=self.lagtimes, errors=itsErrors) mplt.plot_implied_timescales(its_object, outfile=self.itsOutput, nits=self.numberOfITS) plt.savefig("its.png") if self.lagtime is not None: return self.lagtime
def plot_implied_timescales(dtrajs, nits=15, model_name=""): """ Compute and plot implied timescales. Parameters ---------- dtrajs nits model_name Returns ------- its : :class:`ImpliedTimescales <pyemma.msm.estimators.implied_timescales.ImpliedTimescales>` object """ its = msm.its(dtrajs, lags=lags, nits=nits, errors="bayes") mplt.plot_implied_timescales(its, dt=0.25, units="ns") plt.title(model_name) return its
def plot_implied_timescales(dtrajs,nits=15,model_name=''): ''' Compute and plot implied timescales. Parameters ---------- dtrajs nits model_name Returns ------- its : :class:`ImpliedTimescales <pyemma.msm.estimators.implied_timescales.ImpliedTimescales>` object ''' its = msm.its(dtrajs, lags=lags, nits=nits)#,errors='bayes') mplt.plot_implied_timescales(its,dt=0.25,units='ns') plt.title(model_name) plt.savefig('{0}_implied_timescales.pdf'.format(model_name)) plt.close() return its
elapsed_time = final_time - initial_time print('Elapsed time %.3f s' % elapsed_time) # Save cluster centers np.save('clustercenters', clustering.clustercenters) # Save discrete trajectories. dtrajs = clustering.dtrajs dtrajs_dir = 'dtrajs' clustering.save_dtrajs(output_dir=dtrajs_dir, output_format='npy', extension='.npy') ################################################################################ # Make timescale plots ################################################################################ import matplotlib as mpl mpl.use('Agg') # Don't use display import matplotlib.pyplot as plt from pyemma import msm from pyemma import plots lags = [1, 2, 5, 10, 20, 50] #its = msm.its(dtrajs, lags=lags, errors='bayes') its = msm.its(dtrajs, lags=lags) plots.plot_implied_timescales(its) plt.savefig('plot.pdf')
plt.contourf(F.T, 50, cmap=plt.cm.hot, extent=extent) plt.xlabel('tic1') plt.ylabel('tic2') plt.plot(clustering.clustercenters[:,0],clustering.clustercenters[:,1], linewidth=0, marker='o') plt.savefig('tic1_tic2.pdf') ################################################################################ # Make eigenvalues plot ################################################################################ plt.clf() eigenvalues = (running_tica.eigenvalues)**2 plt.plot(eigenvalues) plt.savefig('eigenvalues.pdf') ################################################################################ # Make timescale plot ################################################################################ plt.clf() lags = [1,2,5,10,20,50] its = pyemma.msm.its(dtrajs, lags=lags, errors='bayes') mplt.plot_implied_timescales(its) plt.savefig('implied_timescales.pdf')
def plotTimescales(self, maxlag): import pyemma.msm as msm import pyemma.plots as mplt its = msm.timescales_hmsm(self.hmm.discrete_trajectories_full, self.macronum, lags=maxlag) mplt.plot_implied_timescales(its, ylog=True, units='ns', dt=self.data.fstep, linewidth=2)
elapsed_time = final_time - initial_time print('Elapsed time %.3f s' % elapsed_time) # Save cluster centers np.save('clustercenters', clustering.clustercenters) # Save discrete trajectories. dtrajs = clustering.dtrajs dtrajs_dir = 'dtrajs' clustering.save_dtrajs(output_dir=dtrajs_dir, output_format='npy', extension='.npy') ################################################################################ # Make timescale plots ################################################################################ import matplotlib as mpl mpl.use('Agg') # Don't use display import matplotlib.pyplot as plt from pyemma import msm from pyemma import plots lags = [1,2,5,10,20,50] #its = msm.its(dtrajs, lags=lags, errors='bayes') its = msm.its(dtrajs, lags=lags) plots.plot_implied_timescales(its) plt.savefig('plot.pdf')
if args.save: pp.savefig(os.path.join(args.save_destination, 'msm_tica_all.png')) if args.display: pp.show() pp.clf() pp.close() ### #actually generate MSM from data msm_from_data = msm.estimate_markov_model(dtrajs=mapped_data, lag=lagtime) #plot and/or save implied timescales, if specified if args.timescales: its = msm.timescales_msm(dtrajs=mapped_data, lags=500) mplt.plot_implied_timescales(its, show_mean=False, ylog=True, dt=25, units='ps', linewidth=2) if args.save: pp.savefig(os.path.join(args.save_destination, 'msm_its.png')) if args.display: pp.show() pp.clf() pp.close() #### #pcca cluster using specified n_sets msm_from_data.pcca(n_sets) pcca_return = msm_from_data.pcca(n_sets) pcca_return.metastable_sets pcca_return.metastable_assignment
def plotITS(self, its_object, its_plot_file=None, nits=-1): its_plot = mplt.plot_implied_timescales(its_object, outfile=its_plot_file, nits=nits) plt.savefig("its.eps") return its_plot
def plotTimescales(self, lags=None, units='frames', errors=None, nits=None, results=False, plot=True, save=None): """ Plot the implied timescales of MSMs of various lag times Parameters ---------- lags : list The lag times at which to compute the timescales. By default it spreads out 25 lag times linearly from lag 10 until the mode length of the trajectories. units : str The units of lag. Can be 'frames' or any time unit given as a string. errors : errors Calculate errors using Bayes (Refer to pyEMMA documentation) nits : int Number of implied timescales to calculate. Default: all results : bool If the method should return the calculated implied timescales plot : bool If the method should display the plot of implied timescales save : str Path of the file in which to save the figure Returns ------- If given results=True this method will return the following data its : np.ndarray The calculated implied timescales. 2D array with dimensions (len(`lags`), `nits`) lags : np.ndarray A list of the lag times that were used to calculate the implied timescales Examples -------- >>> model = Model(data) >>> model.plotTimescales() >>> model.plotTimescales(lags=list(range(1,100,5))) """ import pyemma.plots as mplt import pyemma.msm as msm self._integrityCheck() if lags is None: lags = self._defaultLags() else: lags = unitconvert(units, 'frames', lags, fstep=self.data.fstep).tolist() if nits is None: nits = np.min((self.data.K, 20)) from htmd.config import _config its = msm.its(self.data.St.tolist(), lags=lags, errors=errors, nits=nits, n_jobs=_config['ncpus']) if plot or (save is not None): from matplotlib import pylab as plt plt.ion() plt.figure() try: mplt.plot_implied_timescales(its, dt=self.data.fstep, units='ns') except ValueError as ve: plt.close() raise ValueError( '{} This is probably caused by badly set fstep in the data ({}). ' .format(ve, self.data.fstep) + 'Please correct the model.data.fstep to correspond to the simulation frame step in nanoseconds.' ) if save is not None: plt.savefig(save, dpi=300, bbox_inches='tight', pad_inches=0.2) if plot: plt.show() if results: return its.get_timescales(), its.lags
def test_plot(self): plot_implied_timescales(self.its, refs=self.refs)
def test_process(self): plot_implied_timescales(self.its, refs=self.refs, process=[1, 2], dt=0.01, units='ns')
ax1.scatter(cc_x, cc_y, marker='o', color='black') ax2 = mplt.plot_free_energy(np.vstack(Y)[:,0], np.vstack(Y)[:,1], cbar_label=None) if args.save: pp.savefig(os.path.join(args.save_destination, 'msm_tica_all.png')) if args.display: pp.show() pp.clf() pp.close() ### #actually generate MSM from data msm_from_data = msm.estimate_markov_model(dtrajs=mapped_data, lag=lagtime) #plot and/or save implied timescales, if specified if args.timescales: its = msm.timescales_msm(dtrajs=mapped_data, lags=500) mplt.plot_implied_timescales(its, show_mean=False, ylog=True, dt=25, units='ps', linewidth=2) if args.save: pp.savefig(os.path.join(args.save_destination, 'msm_its.png')) if args.display: pp.show() pp.clf() pp.close() #### #pcca cluster using specified n_sets msm_from_data.pcca(n_sets) pcca_return = msm_from_data.pcca(n_sets) pcca_return.metastable_sets pcca_return.metastable_assignment pcca_return.transition_matrix pcca_dist = msm_from_data.metastable_distributions
def test_process(self): plot_implied_timescales(self.its, refs=self.refs, process=[1, 2])
if not dontsavemsm: dtraj_info = { dirs[x]:dtrajs[x] for x in range(len(dirs)) } dtraj_info["dirs"] = dirs with open("dtrajs.pkl", 'wb') as fhandle: pickle.dump(dtraj_info, fhandle) else: os.chdir("msm") with open("dtrajs.pkl", 'rb') as fhandle: dtraj_pkl = pickle.load(fhandle) dirs = dtraj_pkl["dirs"] dtrajs = [ dtraj_pkl[x] for x in dirs ] # estimate MSM's at different lagtimes lags = [1,2,5,10,20,50,100,200,300,400,500,600,700,800,900,1000] its = msm.its(dtrajs, lags=lags) if not dontsavemsm: util.save_markov_state_models(T, its.models) mplt.plot_implied_timescales(its, ylog=False) plt.title("T = " + str(T)) plt.savefig("its_tanh_cont_features.pdf") plt.savefig("its_tanh_cont_features.png") os.chdir("..") if display: plt.show()
plt.ylabel("IC 2") plt.title("FES IC1-2") plt.savefig("fes_IC1-2.png") plt.figure(figsize=(8, 5)) mplt.plot_free_energy(xall, np.vstack(Y)[:, 2], cmap="Spectral") plt.plot(cc_x, cc_z, linewidth=0, marker='o', markersize=5, color='black') plt.xlabel("IC 1") plt.ylabel("IC 3") plt.title("FES IC1-3") plt.savefig("fes_IC1-3.png") lags = None plt.figure(figsize=(8, 5)) its = msm.timescales_msm(dtrajs, lags=lags, nits=10) mplt.plot_implied_timescales(its, ylog=True, units='steps', linewidth=2) plt.savefig("its.png") # its = msm.timescales_msm(dtrajs, lags=lags, nits=10, errors='bayes', n_jobs=-1) # plt.figure(figsize=(8, 5)) # mplt.plot_implied_timescales(its, show_mean=False, ylog=False, units='steps', linewidth=2) # plt.savefig("its_errors.png") M = msm.estimate_markov_model(dtrajs, msm_lag) print('fraction of states used = ', M.active_state_fraction) print('fraction of counts used = ', M.active_count_fraction) f = plt.figure(figsize=(8, 5)) pi = M.stationary_distribution ax = mplt.scatter_contour(cc_x[M.active_set], cc_y[M.active_set], pi, fig=f) plt.xlabel("IC 1")
lags = [ 1, 5, 10, 20, 35, 50, 75, 100, 150, 200, 300, 400, 500, 600, 700, 800, 900, 100 ] implied_ts = pyemma.msm.its(dtrajs=dtrajs, lags=lags, nits=5) pyemma.plots.plot_implied_timescales(implied_ts, units='time-steps', ylog=False) #plt.vlines(2,ymin=0,ymax=350,linestyles='dashed') #plt.annotate("selected model", xy=(lags[-3], implied_ts.timescales[-3][0]), xytext=(15,250), # arrowprops=dict(facecolor='black', shrink=0.001, width=0.1,headwidth=8)) plt.figure(figsize=(10, 10), dpi=600) plt.ylim([0, 150]) print(implied_ts) its = msm.timescales_msm(dtrajs, lags=50, nits=10) print(its) mplt.plot_implied_timescales(its, ylog=False, units='steps', linewidth=2) #plt.xlim(0, 40); plt.ylim(0, 50) its = msm.timescales_msm(dtrajs, lags=50, nits=10, errors='bayes', n_jobs=-1) plt.figure(figsize=(8, 5)) mplt.plot_implied_timescales(its, show_mean=False, ylog=False, dt=0.1, units='ns', linewidth=2) #plt.xlim(0, 5); plt.ylim(0.1,60);
if i == 3: for j in range(4): axes[i][j].set_xlabel("TIC " + str(j + 2), fontsize=20) axes[0][0].annotate("TICA " + f_str, fontsize=24, xy=(0, 0), xytext=(1.8, 1.1), xycoords="axes fraction", textcoords="axes fraction") fig.savefig(msm_savedir + "/tic_hist_grid.pdf") n_clusters = 300 msm_lags = [1, 10, 20, 50, 100, 200] cluster = coor.cluster_kmeans(k=n_clusters) coor.pipeline([reader, tica, cluster]) its = msm.its(cluster.dtrajs, lags=msm_lags) plt.figure() mplt.plot_implied_timescales(its) plt.title(msm_savedir) plt.savefig(msm_savedir + "/its_vs_lag_ylog.pdf") #plt.figure() #plt.plot(np.arange(1,21), M.timescales()[:20], 'o') #ymin, ymax = plt.ylim() #plt.ylim(0, ymax) #plt.savefig("msm_ti.pdf")
import pyemma.coordinates as coor import numpy as np import pyemma.msm as msm import pyemma.plots as pyemma_plots import matplotlib.pyplot as plt sys = 'fdis' n_clusters = 100 dtrajs = coor.load(f'cluster_data/{sys}_{n_clusters}_cluster_dtrajs.h5') max_lag = 80 dt2 = [i.astype(np.int_) for i in dtrajs] dt3 = [i.reshape((i.shape[0])) for i in dt2] its = msm.its(dt3, lags=max_lag, nits=8, errors='bayes', nsamples=200) fig, ax = plt.subplots() pyemma_plots.plot_implied_timescales(its, units='ns', ax=ax) fig.savefig(f'{sys}_implied_timescale_{max_lag}.pdf')
def test_nits(self): plot_implied_timescales(self.its, refs=self.refs, nits=2)
"_lagt" + "{:d}".format(lagtime) + ".pickle", "wb") pickle.dump(MSM, pickle_out) pickle_out.close() print("\nGenerated all MSMs.") # Generate implied timescales plots (draft verision) if plotImpliedTimescalesDraft: print("Generating implied timescales plots (draft version)") maxlagtime = 100 #100 #200 #300 its = pyemma.msm.its(finalTrajs, maxlagtime, reversible=reversible) nits = 20 fig, ax = plt.subplots(figsize=(10, 7)) mplt.plot_implied_timescales(its, ax=ax, nits=nits, ylog=True, units='steps', linewidth=2, dt=1) plt.ylabel(r"log(timescale/steps)", fontsize=18) plt.xlabel(r"lag time/steps", fontsize=18) plt.savefig(MSMdirectory + 'its_draft_dimer4trimer' + "{:.2E}".format(totalTimeSteps) + '.pdf') # No log version fig, ax = plt.subplots(figsize=(10, 7)) mplt.plot_implied_timescales(its, ax=ax, nits=nits, ylog=False, units='steps', linewidth=2,
def plotTimescales(self, lags=None, units='frames', errors=None, nits=None, results=False, plot=True, save=None): """ Plot the implied timescales of MSMs of various lag times Parameters ---------- lags : list The lag times at which to compute the timescales. By default it spreads out 25 lag times linearly from lag 10 until the mode length of the trajectories. units : str The units of lag. Can be 'frames' or any time unit given as a string. errors : errors Calculate errors using Bayes (Refer to pyEMMA documentation) nits : int Number of implied timescales to calculate. Default: all results : bool If the method should return the calculated implied timescales plot : bool If the method should display the plot of implied timescales save : str Path of the file in which to save the figure Returns ------- If given results=True this method will return the following data its : np.ndarray The calculated implied timescales. 2D array with dimensions (len(`lags`), `nits`) lags : np.ndarray A list of the lag times that were used to calculate the implied timescales Examples -------- >>> model = Model(data) >>> model.plotTimescales() >>> model.plotTimescales(lags=list(range(1,100,5))) """ import pyemma.plots as mplt import pyemma.msm as msm self._integrityCheck() if lags is None: lags = self._defaultLags() else: lags = unitconvert(units, 'frames', lags, fstep=self.data.fstep).tolist() if nits is None: nits = np.min((self.data.K, 20)) from htmd.config import _config its = msm.its(self.data.St.tolist(), lags=lags, errors=errors, nits=nits, n_jobs=_config['ncpus']) if plot or (save is not None): from matplotlib import pylab as plt plt.ion() plt.figure() try: mplt.plot_implied_timescales(its, dt=self.data.fstep, units='ns') except ValueError as ve: plt.close() raise ValueError('{} This is probably caused by badly set fstep in the data ({}). '.format(ve, self.data.fstep) + 'Please correct the model.data.fstep to correspond to the simulation frame step in nanoseconds.') if save is not None: plt.savefig(save, dpi=300, bbox_inches='tight', pad_inches=0.2) if plot: plt.show() if results: return its.get_timescales(), its.lags
reader = coor.source(trajnames, features=feat) transform = coor.tica(lag=lagtime, stride=stride) cluster_pipe = [reader, transform, cluster] pipeline = coor.pipeline(cluster_pipe) dtrajs = cluster.dtrajs #lags = [1,2,5,10,20,50,100,200,300,400,500,600,700,800,900,1000] lags = [10, 25, 50, 100, 200, 500, 1000] its = msm.its(dtrajs, lags=lags) T = 300 save_markov_state_models(T, its.models) # save name should have n_clusters saveas = "msm_its_vs_lag_{}".format(n_clusters) mplt.plot_implied_timescales(its, ylog=False) #plt.title("T = " + str(T)) plt.savefig(msm_savedir + "/" + saveas + ".pdf") plt.savefig(msm_savedir + "/" + saveas + ".png") plt.figure() mplt.plot_implied_timescales(its) #plt.title("T = " + str(T)) plt.savefig(msm_savedir + "/" + saveas + "_ylog.pdf") plt.savefig(msm_savedir + "/" + saveas + "_ylog.png") raise SystemExit dt = 0.2 # ps stride = 10