{"id":1728,"library":"stanio","title":"stanio","description":"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.","status":"active","version":"0.5.1","language":"en","source_language":"en","source_url":"https://github.com/stan-dev/stanio","tags":["stan","bayesian","data-science","io","serialization","numpy"],"install":[{"cmd":"pip install stanio","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for handling numerical arrays and matrices, which are fundamental data structures for Stan models.","package":"numpy","optional":false}],"imports":[{"symbol":"dump_stan_json","correct":"from stanio import dump_stan_json"},{"symbol":"load_stan_json","correct":"from stanio import load_stan_json"},{"symbol":"dump_rdump","correct":"from stanio import dump_rdump"},{"symbol":"load_rdump","correct":"from stanio import load_rdump"}],"quickstart":{"code":"import stanio\nimport numpy as np\nimport os\n\ndata_to_dump = {\"x\": np.array([1.0, 2.0]), \"y\": 5, \"theta\": [[0.1, 0.2], [0.3, 0.4]]}\n\n# Dump data to Stan JSON format\njson_file_path = \"my_data.json\"\nstanio.dump_stan_json(data_to_dump, json_file_path)\nprint(f\"Data dumped to {json_file_path}\")\n\n# Load data from Stan JSON format\nloaded_data = stanio.load_stan_json(json_file_path)\nprint(f\"Loaded data: {loaded_data}\")\n\n# Clean up the created file\nos.remove(json_file_path)\nprint(f\"Removed {json_file_path}\")","lang":"python","description":"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."},"warnings":[{"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.","message":"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.","severity":"gotcha","affected_versions":"<0.5.1"},{"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`.","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}