Created
January 25, 2022 21:30
-
-
Save cwindolf/b552d6a563925ec17e11a8227d2fadbf to your computer and use it in GitHub Desktop.
CmdStanPy helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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