Install & Compatibility
Where this runs
tested against v0.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.262s · 89.4MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 3.5s · import 0.258s · 86MB
89MB installed
● package 89MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
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
This quickstart demonstrates how to dump Python data structures, including NumPy arrays, into Stan's JSON format and then load them back. It covers the primary use case of `stanio` for preparing input data for Stan models.
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}")
Errors
Common errors & fixes
TypeError: argument of type 'str' is not a `Mapping` object
The `stanio.dump_rdump` or `stanio.dump_stan_json` function received a data argument that was not a dictionary-like object (a `Mapping`), but rather a different type like a string or list.
fixEnsure the data argument passed to the dump function is a dictionary or a compatible mapping object.
stanio.rdump_parser.RDumpSyntaxError: Expected `"` but got `eof` at line 1, column 19.
The R dump file (`.R` data file) provided to `stanio.read_rdump` contains malformed syntax, preventing the `stanio` parser from correctly interpreting its content.
fixReview and correct the syntax of the R dump file to ensure it conforms to the expected R dump format, fixing issues like missing quotes, unbalanced parentheses, or incorrect assignments.
ModuleNotFoundError: No module named 'stanio.Rump'
The import statement is attempting to access a submodule or class (e.g., `Rump`) that does not exist within the `stanio` library, likely due to a typo or misunderstanding of the library's structure.
fixCorrect the import statement to refer to the actual functions available directly under `stanio`, such as `dump_rdump`, `read_rdump`, `dump_stan_json`, or `read_stan_json`.
AttributeError: module 'stanio' has no attribute 'some_non_existent_func'
The code is attempting to call a function or access an attribute (e.g., `some_non_existent_func`) that does not exist directly within the `stanio` module's public API.
fixConsult the `stanio` documentation or use IDE introspection (tab-completion) to verify the correct function names and their usage, calling only existing attributes.
TypeError: write() argument must be str, not bytes
When using `stanio.dump_rdump` or `stanio.dump_stan_json` with a file object, the file was opened in text mode (`'w'`), but the library attempts to write binary data to it, which requires the file to be opened in binary write mode (`'wb'`).
fixOpen the output file in binary write mode (`'wb'`) when providing a file object to `stanio.dump_rdump` or `stanio.dump_stan_json`.
Upgrade
Version history
0.5.1latest on PyPI · released Jul 8, 2024
Audit
Dependencies
numpyrequiredRequired for handling numerical arrays and matrices, which are fundamental data structures for Stan models.