Skip to content

Instantly share code, notes, and snippets.

@davipatti
Created May 16, 2023 17:42
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 davipatti/3e9de14255869bf9f52120ded93ba288 to your computer and use it in GitHub Desktop.
Save davipatti/3e9de14255869bf9f52120ded93ba288 to your computer and use it in GitHub Desktop.
Try to load a pymc trace from a path. If the path doesn't exist then generate the trace by calling fun.
import arviz as az
def make_trace(path: str, fun: Callable, *args, **kwargs):
"""
Try to load a pymc trace from a path. If the path doesn't exist then generate
the trace by calling fun.
Args:
path: Path to Netcdf file.
fun: Callable that returns a PyMC trace.
*args, **kwargs: Passed to fun.
"""
try:
trace = az.from_netcdf(path)
except FileNotFoundError:
trace = fun(*args, **kwargs)
trace.to_netcdf(path)
return trace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment