Skip to content

Instantly share code, notes, and snippets.

@cwindolf
Created January 25, 2022 21:30
Show Gist options
  • Save cwindolf/b552d6a563925ec17e11a8227d2fadbf to your computer and use it in GitHub Desktop.
Save cwindolf/b552d6a563925ec17e11a8227d2fadbf to your computer and use it in GitHub Desktop.
CmdStanPy helpers
from pathlib import Path
import cmdstanpy
import ujson
def stanc(name, code, workdir=".stan"):
Path(workdir).mkdir(exist_ok=True)
path = Path(workdir) / f"{name}.stan"
with open(path, "w") as f:
f.write(code)
model = cmdstanpy.CmdStanModel(stan_file=path, stanc_options={'warn-pedantic': True})
return model
def tojson(path, **kwargs):
out = {}
for k, v in kwargs.items():
if isinstance(v, np.ndarray):
v = v.tolist()
# print(k, v)
out[k] = v
with open(path, "w") as f:
ujson.dump(out, f)
return path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment