Skip to content

Instantly share code, notes, and snippets.

@jmuhlich
Created January 16, 2021 21:28
Show Gist options
  • Save jmuhlich/d72c23dcd965f39ede3811fc451e6e88 to your computer and use it in GitHub Desktop.
Save jmuhlich/d72c23dcd965f39ede3811fc451e6e88 to your computer and use it in GitHub Desktop.
import sys
import re
import importlib
import time
from pysb.simulator import ScipyOdeSimulator
import pysb.bng
import numpy as np
def benchmark(model, sim_kwargs):
tspan = np.linspace(0, 10000, 1000)
start = time.time()
sim = ScipyOdeSimulator(model, tspan, **sim_kwargs)
end = time.time()
print(" simulator init: %.3g" % (end-start))
start = time.time()
sim.run()
end = time.time()
print(" first sim: %.3g" % (end-start))
times = []
for i in range(10):
start = time.time()
sim.run()
end = time.time()
times.append(end-start)
# Discard worst two times.
times = sorted(times)[:-2]
mean = np.mean(times)
std = np.std(times)
print(" subsequent sims: %.3g +/- %.3g" % (mean, std))
model = importlib.import_module(sys.argv[1]).model
print(f"Model: {model.name}")
ge_kwargs = {}
sim_kwargs = {}
if model.name.endswith('.egfr_extended'):
print("==> egfr_extended detected")
ge_kwargs['max_iter'] = 5
elif model.name == 'MARM1':
print("==> MARM1 detected")
import pandas as pd
param_sets = pd.read_csv('parameter_sets.csv', index_col=0)
param_sets = param_sets.drop('chi2', axis=1)
params = param_sets.iloc[0].to_dict()
sim_kwargs['param_values'] = params
model.initials = [
ic for i, ic in enumerate(model.initials) if i not in (0, 1)
]
for arg in sys.argv[2:]:
k, v = arg.split("=")
if v == "True":
v = True
elif v == "False":
v = False
elif not re.search(r'[^0-9]', v):
v = int(v)
else:
try:
v = float(v)
except ValueError:
pass
sim_kwargs[k] = v
if not model.reactions:
print("Generating equations...")
pysb.bng.generate_equations(model, **ge_kwargs)
print()
print("Running benchmark...")
benchmark(model, sim_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment