Registry / data / stanio

stanio

JSON →
library0.5.1pypypi✓ verified 25d ago

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.

pip install stanio
INSTALL
IMPORT
SIG · STANIO
S
stanio
datapythonv0.5.1
Install
3.5s avg
Import
260ms
Disk
89MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.262s · 89.4MB
glibc
py 3.103.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}")
Debug
Known issues
gotchaOlder versions of stanio (prior to v0.5.1) had issues when dumping `numpy.matrix` types to JSON, potentially leading to incorrect serialization or errors.
fix
Upgrade to stanio version 0.5.1 or newer to ensure correct handling of `numpy.matrix`. Alternatively, convert `numpy.matrix` objects to standard `numpy.ndarray` before dumping.
affects: <0.5.1
gotchaEnsure 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.
fix
Carefully review Stan's data block requirements. For potentially ambiguous types (e.g., empty collections, mixed-type lists), explicitly convert them to `numpy.ndarray` with appropriate dtypes before passing to `stanio.dump_stan_json` or `stanio.dump_rdump`.
affects: All versions
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.
fix
Ensure 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.
fix
Review 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.
fix
Correct 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.
fix
Consult 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'`).
fix
Open 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.
Agent activity
14 hits · last 30 days
node
10
OpenAI (training)
1
Resources
stanio — pip install stanio · libregistry