0
# Configuration Management
1
2
Global and context-specific configuration management for ArviZ behavior including plotting backends, statistical defaults, and data handling preferences.
3
4
## Global Configuration
5
6
```python { .api }
7
rcParams: RcParams
8
"""
9
Global configuration parameters object for ArviZ settings.
10
11
Categories:
12
data.*: Data handling preferences
13
plot.*: Plotting backend and style settings
14
stats.*: Statistical computation defaults
15
16
Access pattern: az.rcParams['category.parameter'] = value
17
"""
18
```
19
20
## Context Management
21
22
```python { .api }
23
class rc_context:
24
"""
25
Context manager for temporary configuration changes.
26
27
Allows temporary modification of ArviZ settings within a specific scope,
28
automatically restoring previous values when exiting the context.
29
"""
30
31
def __init__(self, rc: dict = None, fname: str = None):
32
"""
33
Initialize context manager for temporary settings.
34
35
Args:
36
rc (dict, optional): Dictionary of parameter -> value mappings
37
fname (str, optional): Path to rcParams configuration file
38
"""
39
40
def __enter__(self):
41
"""Enter context with modified settings."""
42
43
def __exit__(self, exc_type, exc_val, exc_tb):
44
"""Exit context and restore original settings."""
45
```
46
47
## Configuration Categories
48
49
### Data Settings
50
51
```python
52
# Data handling preferences
53
az.rcParams["data.load"] = "lazy" # "lazy" or "eager" data loading
54
az.rcParams["data.save_warmup"] = False # Whether to save warmup samples
55
az.rcParams["data.log_likelihood"] = True # Include log likelihood by default
56
```
57
58
### Plot Settings
59
60
```python
61
# Backend configuration
62
az.rcParams["plot.backend"] = "matplotlib" # "matplotlib", "bokeh", "plotly"
63
az.rcParams["plot.max_subplots"] = 40 # Maximum subplots per figure
64
az.rcParams["plot.point_estimate"] = "mean" # "mean", "median", "mode"
65
66
# Style preferences
67
az.rcParams["plot.density_kind"] = "kde" # "kde" or "hist"
68
az.rcParams["plot.theme"] = "default" # Plot theme
69
```
70
71
### Statistical Settings
72
73
```python
74
# Statistical computation defaults
75
az.rcParams["stats.hdi_prob"] = 0.94 # Default HDI probability
76
az.rcParams["stats.information_criterion"] = "loo" # "loo" or "waic"
77
az.rcParams["stats.ic_scale"] = "log" # "log", "negative_log", "deviance"
78
```
79
80
## Usage Examples
81
82
### Basic Configuration
83
84
```python
85
import arviz as az
86
87
# View current settings
88
print(az.rcParams["plot.backend"])
89
print(az.rcParams["stats.hdi_prob"])
90
91
# Modify global settings
92
az.rcParams["plot.backend"] = "bokeh"
93
az.rcParams["stats.hdi_prob"] = 0.89
94
95
# Settings persist for the session
96
az.plot_posterior(idata) # Uses Bokeh backend
97
az.summary(idata) # Uses 89% HDI
98
```
99
100
### Temporary Configuration Changes
101
102
```python
103
# Temporary backend change
104
with az.rc_context({"plot.backend": "plotly"}):
105
az.plot_trace(idata) # Uses Plotly backend
106
az.plot_posterior(idata)
107
# Automatically reverts to previous backend
108
109
# Multiple temporary settings
110
temp_settings = {
111
"plot.backend": "bokeh",
112
"stats.hdi_prob": 0.89,
113
"plot.point_estimate": "median"
114
}
115
116
with az.rc_context(temp_settings):
117
summary = az.summary(idata) # Uses 89% HDI with median estimates
118
az.plot_forest(idata) # Uses Bokeh backend
119
# All settings revert automatically
120
```
121
122
### Configuration from File
123
124
```python
125
# Load settings from configuration file
126
with az.rc_context(fname="my_arviz_config.json"):
127
az.plot_posterior(idata)
128
results = az.compare(model_dict)
129
```
130
131
## Advanced Configuration
132
133
### Custom Plotting Defaults
134
135
```python
136
# Set publication-quality defaults
137
publication_settings = {
138
"plot.backend": "matplotlib",
139
"plot.point_estimate": "mean",
140
"plot.density_kind": "kde",
141
"stats.hdi_prob": 0.95,
142
"plot.max_subplots": 20
143
}
144
145
# Apply settings globally
146
for key, value in publication_settings.items():
147
az.rcParams[key] = value
148
149
# Or use as context
150
with az.rc_context(publication_settings):
151
# Generate publication plots
152
az.plot_posterior(idata, hdi_prob=0.95)
153
az.plot_forest(idata)
154
```
155
156
### Interactive vs Static Plotting
157
158
```python
159
# Interactive plotting setup
160
interactive_config = {
161
"plot.backend": "bokeh",
162
"plot.max_subplots": 25
163
}
164
165
# Static plotting setup
166
static_config = {
167
"plot.backend": "matplotlib",
168
"plot.max_subplots": 40
169
}
170
171
# Switch between modes
172
with az.rc_context(interactive_config):
173
az.plot_pair(idata) # Interactive plot
174
175
with az.rc_context(static_config):
176
az.plot_trace(idata) # Static plot
177
```
178
179
### Performance-Oriented Settings
180
181
```python
182
# Settings for large datasets
183
performance_config = {
184
"data.load": "lazy",
185
"plot.max_subplots": 20,
186
"stats.information_criterion": "waic" # Faster than LOO for large data
187
}
188
189
with az.rc_context(performance_config):
190
# Process large inference data efficiently
191
summary = az.summary(large_idata)
192
comparison = az.compare(large_model_dict)
193
```
194
195
## Configuration File Format
196
197
ArviZ supports JSON configuration files:
198
199
```json
200
{
201
"data.load": "lazy",
202
"data.save_warmup": false,
203
"plot.backend": "matplotlib",
204
"plot.point_estimate": "mean",
205
"plot.density_kind": "kde",
206
"stats.hdi_prob": 0.94,
207
"stats.information_criterion": "loo"
208
}
209
```
210
211
## Available Parameters
212
213
### Complete Parameter List
214
215
```python
216
# Data parameters
217
"data.load" # "lazy" or "eager"
218
"data.save_warmup" # True or False
219
"data.log_likelihood" # True or False
220
221
# Plot parameters
222
"plot.backend" # "matplotlib", "bokeh", "plotly"
223
"plot.max_subplots" # Integer (default 40)
224
"plot.point_estimate" # "mean", "median", "mode"
225
"plot.density_kind" # "kde" or "hist"
226
"plot.theme" # "default", "arviz-white", etc.
227
228
# Stats parameters
229
"stats.hdi_prob" # Float between 0 and 1 (default 0.94)
230
"stats.information_criterion" # "loo" or "waic"
231
"stats.ic_scale" # "log", "negative_log", "deviance"
232
```
233
234
### Parameter Validation
235
236
```python
237
# Valid values are enforced
238
try:
239
az.rcParams["plot.backend"] = "invalid_backend"
240
except ValueError as e:
241
print(f"Invalid backend: {e}")
242
243
try:
244
az.rcParams["stats.hdi_prob"] = 1.5 # Must be between 0 and 1
245
except ValueError as e:
246
print(f"Invalid HDI probability: {e}")
247
```
248
249
## Best Practices
250
251
### Session Management
252
253
```python
254
# Save current settings at start of analysis
255
original_settings = {
256
"plot.backend": az.rcParams["plot.backend"],
257
"stats.hdi_prob": az.rcParams["stats.hdi_prob"]
258
}
259
260
# Modify as needed during analysis
261
az.rcParams["plot.backend"] = "bokeh"
262
263
# Restore at end if needed
264
for key, value in original_settings.items():
265
az.rcParams[key] = value
266
```
267
268
### Project-Specific Configuration
269
270
```python
271
# Create project-specific configuration function
272
def setup_project_config():
273
"""Configure ArviZ for this specific project."""
274
config = {
275
"plot.backend": "matplotlib",
276
"stats.hdi_prob": 0.89,
277
"plot.point_estimate": "median",
278
"data.save_warmup": False
279
}
280
281
for key, value in config.items():
282
az.rcParams[key] = value
283
284
# Use at start of analysis
285
setup_project_config()
286
```
287
288
### Context-Aware Plotting
289
290
```python
291
def diagnostic_plots(idata):
292
"""Generate diagnostic plots with appropriate settings."""
293
diag_config = {
294
"plot.backend": "matplotlib", # Better for diagnostic plots
295
"plot.max_subplots": 50
296
}
297
298
with az.rc_context(diag_config):
299
az.plot_trace(idata)
300
az.plot_rank(idata)
301
az.plot_ess(idata)
302
303
def presentation_plots(idata):
304
"""Generate presentation plots with interactive backend."""
305
pres_config = {
306
"plot.backend": "bokeh", # Interactive for presentations
307
"plot.point_estimate": "mean",
308
"stats.hdi_prob": 0.95
309
}
310
311
with az.rc_context(pres_config):
312
az.plot_posterior(idata)
313
az.plot_forest(idata)
314
```
315
316
## Version Information
317
318
```python { .api }
319
__version__: str
320
"""
321
ArviZ version string.
322
323
Provides the current version of the ArviZ library installation.
324
Useful for debugging, compatibility checks, and version reporting.
325
326
Example: "0.22.0"
327
"""
328
```
329
330
### Usage Examples
331
332
```python
333
import arviz as az
334
335
# Check ArviZ version
336
print(f"ArviZ version: {az.__version__}")
337
338
# Version-specific compatibility
339
from packaging import version
340
341
if version.parse(az.__version__) >= version.parse("0.20.0"):
342
# Use newer features
343
az.plot_forest(idata, combined=True)
344
else:
345
# Use legacy syntax
346
az.plot_forest(idata)
347
348
# Include version in analysis reports
349
def analysis_info():
350
"""Print analysis environment information."""
351
import sys
352
import numpy as np
353
import xarray as xr
354
355
print(f"Python: {sys.version}")
356
print(f"ArviZ: {az.__version__}")
357
print(f"NumPy: {np.__version__}")
358
print(f"Xarray: {xr.__version__}")
359
360
analysis_info()
361
```