Skip to content

Instantly share code, notes, and snippets.

@yiyuezhuo
Created February 16, 2020 00:34
Show Gist options
  • Save yiyuezhuo/7541dd6e629ba3ffc1fab5314b36c123 to your computer and use it in GitHub Desktop.
Save yiyuezhuo/7541dd6e629ba3ffc1fab5314b36c123 to your computer and use it in GitHub Desktop.
colab gists
import pystan
import pickle
from hashlib import md5
def StanModel_cache(model_code, model_name=None, **kwargs):
"""Use just as you would `stan`"""
code_hash = md5(model_code.encode('ascii')).hexdigest()
if model_name is None:
cache_fn = 'cached-model-{}.pkl'.format(code_hash)
else:
cache_fn = 'cached-{}-{}.pkl'.format(model_name, code_hash)
try:
sm = pickle.load(open(cache_fn, 'rb'))
except:
sm = pystan.StanModel(model_code=model_code)
with open(cache_fn, 'wb') as f:
pickle.dump(sm, f)
else:
print("Using cached StanModel")
return sm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment