Skip to content

Instantly share code, notes, and snippets.

@JohannesBuchner
Created May 18, 2020 11:32
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 JohannesBuchner/5a301588e9baacbda4ac8a165260b188 to your computer and use it in GitHub Desktop.
Save JohannesBuchner/5a301588e9baacbda4ac8a165260b188 to your computer and use it in GitHub Desktop.
Build and cache Stan models smartly (ignoring changes in comments and white spaces)
import re
import pystan
import hashlib
import pickle
import os
def build_model(code):
lines = code.split("\n")
lines = [re.sub('//.*$', '', line).strip() for line in lines]
lines = [line.replace(' ', ' ').replace(' ', ' ').replace(' ', ' ')
for line in lines if len(line) > 0]
slimcode = '\n'.join(lines).strip()
slimbytes = slimcode.encode(encoding="ascii", errors="ignore")
slimcode = slimbytes.decode(encoding="ascii")
hexcode = hashlib.sha224(slimbytes).hexdigest()[:6]
#print("Stan code[%s]:\n%s" % (hexcode, '\n'.join(['%3d: %s' % (i+1, line) for i, line in enumerate(code.split("\n"))])))
print("Slimmed Stan code[%s]:\n%s" % (hexcode, '\n'.join(['%3d: %s' % (i+1, line) for i, line in enumerate(slimcode.split("\n"))])))
if not os.path.exists('.modelcache'):
os.mkdir('.modelcache')
filename = '.modelcache/%s.pkl' % hexcode
try:
return pickle.load(open(filename, 'rb'))
except IOError:
pass
sm = pystan.StanModel(model_code=slimcode, model_name='m' + hexcode)
with open(filename, 'wb') as f:
pickle.dump(sm, f)
return sm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment