ArviZ Base
arviz-base is a Python package that provides core ArviZ features and converters, serving as a fundamental component of the modular ArviZ 1.0 ecosystem. It focuses on data structures, primarily leveraging `xarray.DataTree`, and conversion utilities from various probabilistic programming languages (PPLs). This library, currently at version 1.0.0, is under active development as part of the broader ArviZ refactoring, aiming for increased flexibility and a minimal dependency footprint.
Common errors
-
ModuleNotFoundError: No module named 'xarray'
cause The core dependency `xarray` is not installed, which is essential for `arviz-base`'s data structures like `DataTree`.fixInstall xarray: `pip install xarray` or `pip install "arviz-base"` (which should pull `xarray` as a dependency). -
AttributeError: module 'arviz' has no attribute 'InferenceData'
cause Attempting to use the deprecated `arviz.InferenceData` object directly with an ArviZ 1.0+ installation (or `arviz-base`). The `InferenceData` object has been replaced by `xarray.DataTree`.fixUpdate your code to use `xarray.DataTree` or `arviz_base`'s conversion functions like `from_dict` or `from_cmdstanpy` to create `DataTree` objects. Refer to the ArviZ migration guide for detailed steps. -
ValueError: setting an array element with a sequence. The above exception was the direct cause of the following exception: ValueError: could not broadcast input array from shape (X,Y) into shape (Z,)
cause This error typically occurs when the input data (e.g., NumPy arrays or dictionaries) provided to conversion functions like `from_dict` or other ArviZ functions do not conform to the expected dimensionality or shape for creating a `DataTree` group.fixEnsure that the input arrays have consistent shapes and the correct number of dimensions corresponding to the `chain`, `draw`, and variable-specific dimensions expected by ArviZ. For instance, posterior samples usually require `(chain, draw, *variable_dimensions)`.
Warnings
- breaking ArviZ 1.0, of which arviz-base is a part, introduces a major refactoring. The central `arviz.InferenceData` object has been replaced by `xarray.DataTree`. Code relying on `InferenceData` attributes or methods will break.
- gotcha arviz-base is a minimal package focused on data structures and converters. For statistical computations or plotting, you will need `arviz-stats` and `arviz-plots` respectively, or install the overarching `arviz` package with necessary extras.
- gotcha I/O functionalities for formats like NetCDF, HDF5, or Zarr are provided via optional dependencies. `arviz-base` itself does not have these as hard requirements to maintain a minimal footprint.
Install
-
pip install arviz-base
Imports
- arviz_base
import arviz as az_old; az_old.InferenceData()
import arviz_base as az
- load_arviz_data
from arviz_base import load_arviz_data
- from_dict
from arviz_base import from_dict
Quickstart
import arviz_base as az
import numpy as np
# Load an example dataset provided by ArviZ Base
idata = az.load_arviz_data("centered_eight")
print(f"Loaded DataTree with groups: {list(idata.keys())}")
# Extract a specific group, e.g., 'posterior', combining chain and draw dimensions
extracted_data = az.extract(idata, group="posterior")
print(f"\nExtracted posterior data shape: {extracted_data.sizes}")
# Convert a simple dictionary to a DataTree
my_data = {
"posterior": {
"alpha": np.random.normal(0, 1, size=(4, 500)),
"beta": np.random.normal(0, 0.5, size=(4, 500))
}
}
custom_idata = az.from_dict(my_data)
print(f"\nCustom DataTree with groups: {list(custom_idata.keys())}")