stanio
stanio is a Python library providing utilities for preparing data inputs and processing outputs for Stan, a probabilistic programming language. It supports common formats like Stan's JSON and R dump (rdump). The current version is 0.5.1, with a release cadence that addresses bug fixes and minor improvements as needed.
Warnings
- gotcha Older versions of stanio (prior to v0.5.1) had issues when dumping `numpy.matrix` types to JSON, potentially leading to incorrect serialization or errors.
- gotcha Ensure Python data types align with Stan's expected types. While stanio handles many conversions, complex nested structures or unusual scalar types might not map directly, leading to Stan errors. For example, empty lists might not be interpreted as empty arrays if Stan expects a fixed-size array.
Install
-
pip install stanio
Imports
- dump_stan_json
from stanio import dump_stan_json
- load_stan_json
from stanio import load_stan_json
- dump_rdump
from stanio import dump_rdump
- load_rdump
from stanio import load_rdump
Quickstart
import stanio
import numpy as np
import os
data_to_dump = {"x": np.array([1.0, 2.0]), "y": 5, "theta": [[0.1, 0.2], [0.3, 0.4]]}
# Dump data to Stan JSON format
json_file_path = "my_data.json"
stanio.dump_stan_json(data_to_dump, json_file_path)
print(f"Data dumped to {json_file_path}")
# Load data from Stan JSON format
loaded_data = stanio.load_stan_json(json_file_path)
print(f"Loaded data: {loaded_data}")
# Clean up the created file
os.remove(json_file_path)
print(f"Removed {json_file_path}")