Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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'):
filename = '.modelcache/%s.pkl' % hexcode
return pickle.load(open(filename, 'rb'))
except IOError:
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