Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
MDAnalysisCookbook/examples/blocks.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
executable file
71 lines (51 sloc)
1.84 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
""" | |
MDAnalysis example: Simple blocking analysis | |
============================================ | |
Calculate the radius of gyration for increasing number of blocks over | |
a trajectory. | |
See: H. Flyvbjerg and H. G. Petersen. Error estimates on averages of | |
correlated data. 91(1):461-466, 1989. | |
""" | |
import numpy as np | |
import MDAnalysis | |
def blocked(universe, nblocks, analyze): | |
size = universe.trajectory.numframes / nblocks | |
blocks = [] | |
for block in xrange(nblocks): | |
a = [] | |
for ts in u.trajectory[block * size:(block + 1) * size]: | |
a.append(analyze(universe)) | |
blocks.append(np.average(a)) | |
blockaverage = np.average(blocks) | |
blockstd = np.std(blocks) | |
return nblocks, size, blockaverage, blockstd | |
def rgyr(universe): | |
return universe.select_atoms('protein').radius_of_gyration() | |
if __name__ == "__main__": | |
from MDAnalysis.tests.datafiles import PSF, DCD | |
try: | |
import matplotlib | |
matplotlib.use('agg') # no interactive plotting, only save figures | |
from pylab import errorbar, subplot, xlabel, ylabel, savefig | |
have_matplotlib = True | |
except ImportError: | |
have_matplotlib = False | |
u = MDAnalysis.Universe(PSF, DCD) | |
results = [] | |
for nblocks in xrange(2, 10): | |
results.append(blocked(u, nblocks, rgyr)) | |
r = np.array(results) | |
if have_matplotlib: | |
subplot(211) | |
errorbar(r[:, 0], r[:, 2], yerr=r[:, 3]) | |
xlabel("number of blocks") | |
ylabel(r"$\langle R_{\rm{gyr}} \rangle$ ($\AA$)") | |
subplot(212) | |
errorbar(r[:, 1], r[:, 2], yerr=r[:, 3]) | |
xlabel("block size") | |
ylabel(r"$\langle R_{\rm{gyr}} \rangle$ ($\AA$)") | |
savefig("./figures/blocks.pdf") | |
savefig("./figures/blocks.png") | |
print "Wrote ./figures/blocks.{{pdf,png}}".format(*vars()) |