Visualization¶
Plotting utilities for analysis results.
Optional Dependency
The visualization module requires pip install rotalabs-probe[viz] which includes
matplotlib and seaborn.
Plotting Functions¶
Publication-ready plotting functions for metacognition analysis.
This module provides high-quality visualization functions for: - Awareness probability time series with confidence bands - Causal attribution bar charts - Feature divergence heatmaps - Distribution comparison plots
All plots are customizable and can be saved at publication quality.
plot_awareness_over_time(timestamps: List[Union[datetime, float]], probabilities: List[float], confidence_intervals: Optional[List[Tuple[float, float]]] = None, title: str = 'Situational Awareness Over Time', xlabel: str = 'Time', ylabel: str = 'Awareness Probability', threshold: Optional[float] = None, figsize: Tuple[float, float] = DEFAULT_FIGSIZE, save_path: Optional[str] = None, dpi: int = DEFAULT_DPI, style: Optional[str] = None, show: bool = True) -> Figure
¶
Plot time series of situational awareness probabilities.
Creates a line plot showing awareness probability over time with optional confidence intervals shown as shaded bands. Optionally displays a threshold line for decision-making.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamps
|
List[Union[datetime, float]]
|
List of timestamps (datetime objects or numeric values) |
required |
probabilities
|
List[float]
|
Awareness probability at each timestamp |
required |
confidence_intervals
|
Optional[List[Tuple[float, float]]]
|
Optional list of (lower, upper) confidence bounds |
None
|
title
|
str
|
Plot title |
'Situational Awareness Over Time'
|
xlabel
|
str
|
X-axis label |
'Time'
|
ylabel
|
str
|
Y-axis label |
'Awareness Probability'
|
threshold
|
Optional[float]
|
Optional decision threshold to display as horizontal line |
None
|
figsize
|
Tuple[float, float]
|
Figure size as (width, height) in inches |
DEFAULT_FIGSIZE
|
save_path
|
Optional[str]
|
If provided, save figure to this path |
None
|
dpi
|
int
|
Resolution for saved figure |
DEFAULT_DPI
|
style
|
Optional[str]
|
Matplotlib style to use |
None
|
show
|
bool
|
Whether to display the plot |
True
|
Returns:
| Type | Description |
|---|---|
Figure
|
Matplotlib Figure object |
Examples:
>>> from datetime import datetime, timedelta
>>> base = datetime.now()
>>> timestamps = [base + timedelta(hours=i) for i in range(24)]
>>> probabilities = [0.1 + 0.03*i for i in range(24)]
>>> confidence_intervals = [(p-0.05, p+0.05) for p in probabilities]
>>> fig = plot_awareness_over_time(
... timestamps, probabilities, confidence_intervals,
... threshold=0.5, save_path="awareness_trend.png"
... )
Source code in src/rotalabs_probe/viz/plotting.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
plot_causal_attribution(intervention_results: List[Dict], title: str = 'Causal Attribution Analysis', xlabel: str = 'Divergence Score', ylabel: str = 'Intervention Type', figsize: Tuple[float, float] = (10, 6), save_path: Optional[str] = None, dpi: int = DEFAULT_DPI, style: Optional[str] = None, show: bool = True, highlight_threshold: Optional[float] = None) -> Figure
¶
Plot causal attribution from counterfactual interventions.
Creates a horizontal bar chart showing the causal importance of different interventions, sorted by divergence score. Optionally highlights interventions above a significance threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intervention_results
|
List[Dict]
|
List of dicts with 'intervention_type' and 'divergence' keys |
required |
title
|
str
|
Plot title |
'Causal Attribution Analysis'
|
xlabel
|
str
|
X-axis label |
'Divergence Score'
|
ylabel
|
str
|
Y-axis label |
'Intervention Type'
|
figsize
|
Tuple[float, float]
|
Figure size as (width, height) in inches |
(10, 6)
|
save_path
|
Optional[str]
|
If provided, save figure to this path |
None
|
dpi
|
int
|
Resolution for saved figure |
DEFAULT_DPI
|
style
|
Optional[str]
|
Matplotlib style to use |
None
|
show
|
bool
|
Whether to display the plot |
True
|
highlight_threshold
|
Optional[float]
|
If provided, highlight bars above this value |
None
|
Returns:
| Type | Description |
|---|---|
Figure
|
Matplotlib Figure object |
Examples:
>>> results = [
... {"intervention_type": "remove_eval_awareness", "divergence": 0.342},
... {"intervention_type": "amplify_eval_awareness", "divergence": 0.156},
... {"intervention_type": "remove_goal_reasoning", "divergence": 0.089},
... ]
>>> fig = plot_causal_attribution(
... results, highlight_threshold=0.2, save_path="causal_attribution.png"
... )
Source code in src/rotalabs_probe/viz/plotting.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |
plot_divergence_heatmap(features: List[str], timepoints: List[Union[datetime, str, float]], z_scores: np.ndarray, title: str = 'Feature Divergence Over Time', xlabel: str = 'Time', ylabel: str = 'Feature', figsize: Tuple[float, float] = (12, 8), save_path: Optional[str] = None, dpi: int = DEFAULT_DPI, style: Optional[str] = None, show: bool = True, cmap: str = 'RdYlGn_r', vmin: float = -5.0, vmax: float = 5.0) -> Figure
¶
Plot heatmap of feature divergence over time.
Creates a color-coded heatmap showing z-scores for multiple features across timepoints. Uses diverging colormap with red indicating high divergence (potential issues) and green indicating normal behavior.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
features
|
List[str]
|
List of feature names |
required |
timepoints
|
List[Union[datetime, str, float]]
|
List of timepoint labels (datetime, strings, or numeric) |
required |
z_scores
|
ndarray
|
2D array of z-scores with shape (n_features, n_timepoints) |
required |
title
|
str
|
Plot title |
'Feature Divergence Over Time'
|
xlabel
|
str
|
X-axis label |
'Time'
|
ylabel
|
str
|
Y-axis label |
'Feature'
|
figsize
|
Tuple[float, float]
|
Figure size as (width, height) in inches |
(12, 8)
|
save_path
|
Optional[str]
|
If provided, save figure to this path |
None
|
dpi
|
int
|
Resolution for saved figure |
DEFAULT_DPI
|
style
|
Optional[str]
|
Matplotlib style to use |
None
|
show
|
bool
|
Whether to display the plot |
True
|
cmap
|
str
|
Colormap name (diverging recommended) |
'RdYlGn_r'
|
vmin
|
float
|
Minimum z-score for colormap |
-5.0
|
vmax
|
float
|
Maximum z-score for colormap |
5.0
|
Returns:
| Type | Description |
|---|---|
Figure
|
Matplotlib Figure object |
Examples:
>>> features = ["response_length", "refusal_rate", "hedging_ratio"]
>>> timepoints = ["00:00", "01:00", "02:00", "03:00"]
>>> z_scores = np.array([
... [0.5, 1.2, 2.8, 3.5], # response_length
... [-0.2, 0.1, 0.3, 4.2], # refusal_rate
... [0.1, 0.3, 0.2, 0.4], # hedging_ratio
... ])
>>> fig = plot_divergence_heatmap(
... features, timepoints, z_scores, save_path="divergence_heatmap.png"
... )
Source code in src/rotalabs_probe/viz/plotting.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
plot_feature_distributions(eval_dist: Union[np.ndarray, List[float]], prod_dist: Union[np.ndarray, List[float]], feature_name: str, title: Optional[str] = None, xlabel: Optional[str] = None, ylabel: str = 'Density', figsize: Tuple[float, float] = (10, 6), save_path: Optional[str] = None, dpi: int = DEFAULT_DPI, style: Optional[str] = None, show: bool = True, bins: int = 30, alpha: float = 0.6) -> Figure
¶
Plot overlaid distributions for evaluation vs production.
Creates overlaid histograms and KDE plots comparing a feature's distribution in evaluation and production contexts. Helps visualize behavioral divergence.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eval_dist
|
Union[ndarray, List[float]]
|
Feature values from evaluation context |
required |
prod_dist
|
Union[ndarray, List[float]]
|
Feature values from production context |
required |
feature_name
|
str
|
Name of the feature being plotted |
required |
title
|
Optional[str]
|
Plot title (auto-generated if None) |
None
|
xlabel
|
Optional[str]
|
X-axis label (auto-generated if None) |
None
|
ylabel
|
str
|
Y-axis label |
'Density'
|
figsize
|
Tuple[float, float]
|
Figure size as (width, height) in inches |
(10, 6)
|
save_path
|
Optional[str]
|
If provided, save figure to this path |
None
|
dpi
|
int
|
Resolution for saved figure |
DEFAULT_DPI
|
style
|
Optional[str]
|
Matplotlib style to use |
None
|
show
|
bool
|
Whether to display the plot |
True
|
bins
|
int
|
Number of histogram bins |
30
|
alpha
|
float
|
Transparency for histogram bars |
0.6
|
Returns:
| Type | Description |
|---|---|
Figure
|
Matplotlib Figure object |
Examples:
>>> eval_dist = np.random.normal(100, 15, 200)
>>> prod_dist = np.random.normal(120, 20, 200)
>>> fig = plot_feature_distributions(
... eval_dist, prod_dist, "response_length",
... save_path="response_length_comparison.png"
... )
Source code in src/rotalabs_probe/viz/plotting.py
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 | |