Skip to content

Instantly share code, notes, and snippets.

@orbeckst
Created June 12, 2019 17:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orbeckst/5220a01463540d3d7750af9b2869b324 to your computer and use it in GitHub Desktop.
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
# -*- 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