Created
June 12, 2019 17:58
-
-
Save orbeckst/5220a01463540d3d7750af9b2869b324 to your computer and use it in GitHub Desktop.
Testing memory consumption of the MDAnalysis XTCReader vs MemoryReader. See https://github.com/MDAnalysis/mdanalysis/wiki/memory-profiling
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
# -*- coding: utf-8 -*- | |
# load trajectory and iterate | |
import numpy as np | |
import tqdm | |
import MDAnalysis as mda | |
import gc | |
@profile | |
def analyze(u, start=None, stop=None, step=None): | |
protein = u.select_atoms("protein") | |
traj = u.trajectory[start:stop:step] | |
results = np.empty((len(traj), 3)) | |
for i, ts in tqdm.tqdm(enumerate(traj)): | |
results[i, :] = (ts.frame, ts.time, protein.radius_of_gyration()) | |
return results | |
@profile | |
def run(*args, **kwargs): | |
start = kwargs.pop('start', None) | |
stop = kwargs.pop('stop', None) | |
step = kwargs.pop('step', None) | |
u = mda.Universe(*args, **kwargs) | |
results = analyze(u, start=start, stop=stop, step=step) | |
rgyr = results[:, 2] | |
del u | |
gc.collect() | |
return rgyr.mean(), rgyr.std() | |
if __name__ == "__main__": | |
import MDAnalysisData.datasets as data | |
sim = data.fetch_nhaa_equilibrium() | |
for repeat in range(3): | |
results = run(sim.topology, sim.trajectory, step=10) | |
print("file reader: repeat {0} (results = {1[0]} ± {1[1]} Å)".format(repeat, results)) | |
for repeat in range(3): | |
results = run(sim.topology, sim.trajectory, in_memory=True, step=10) | |
print("memory reader: repeat {0} (results = {1[0]} ± {1[1]} Å)".format(repeat, results)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment