Plotting
This module contains functions for plotting results related to model fitting and parameter estimation.
plotting
Functions:
-
generate_placeholder_param_names–Utility function. Generates a list of placeholder
-
plot_best_models–Generate a scatter plot grid showing the model with the highest/lowest
-
plot_matrices–Plot confusion and inversion matrices as heatmaps.
-
plot_parameter_dists–Plot parameter estimate distributions as histograms.
-
plot_pp–Probability-probability plot. Plots the proportion of observations with
-
plot_recovery–Plots recovered parameter values against true ones. Used to determine how
-
plot_recovery_matrix–Plots a matrix of the correlation coefficients between true and recovered
-
plot_waic–Creates a bar plot of WAIC values with standard error for given models.
generate_placeholder_param_names
Utility function. Generates a list of placeholder parameter names.
Parameters:
-
(n_paramsint) –Number of parameters.
Returns:
Source code in model_fit_tools/plotting.py
11 12 13 14 15 16 17 18 19 20 21 22 | |
plot_best_models
plot_best_models(df: DataFrame, metric: str = 'waic', highest_best: bool = True, subjects_per_row: int = 40, fig_width_scale: float = 0.15, fig_height_scale: float = 0.15, marker_size: int = 20) -> None
Generate a scatter plot grid showing the model with the highest/lowest model fit metric per subject.
Parameters:
-
(dfDataFrame) –DataFrame containing the data. Expected columns are 'subject', 'model', and an additional column containing the model fit metric.
-
(metricstr, default:'waic') –The model fit metric to use. Defaults to
'waic'. -
(highest_bestbool, default:True) –Whether the highest value of the metric is the best. Defaults to
True. -
(subjects_per_rowint, default:40) –The number of subjects per row in the grid. Defaults to
40. -
(fig_width_scalefloat, default:0.15) –Scaling factor for figure width. Defaults to
0.15. -
(fig_height_scalefloat, default:0.15) –Scaling factor for figure height. Defaults to
0.15. -
(marker_sizeint, default:20) –Size of the scatter plot markers. Defaults to
20.
Source code in model_fit_tools/plotting.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 | |
plot_matrices
plot_matrices(confusion_matrix: array, inversion_matrix: array, model_names: list, format_names: bool = False, scale: float = 1.0, cmap: str = 'viridis') -> None
Plot confusion and inversion matrices as heatmaps.
Parameters:
-
(confusion_matrixarray) –The confusion matrix, indicating how often each model is estimated to be the best when each model is true.
-
(inversion_matrixarray) –The inversion matrix normalized, indicating the proportion of times each model is selected as best given each true model.
-
(model_nameslist) –List of strings indicating the name of each model, used for axis labels.
-
(format_namesbool, default:False) –(bool): Whether to format the model names. If True, replaces underscores with spaces and capitalizes the first letter. and replaces 'Mf' with 'MF' and 'Mb' with 'MB'. Defaults to False.
-
(scalefloat, default:1.0) –Scaling factor for the matrices.
-
(cmapstr, default:'viridis') –The colormap to use for the heatmaps.
Returns:
-
None(None) –Plots the matrices as heatmaps.
Example
plot_matrices(confusion_matrix, inversion_matrix, ['Model1', 'Model2', 'Model3'])
Source code in model_fit_tools/plotting.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 | |
plot_parameter_dists
plot_parameter_dists(estimated: ndarray, param_names: Union[List[str], None] = None, scale: float = 1.0, save_path: Union[str, None] = None, save_fname: Union[str, None] = None) -> None
Plot parameter estimate distributions as histograms.
Can be used with either point estimates or posterior samples.
Parameters:
-
(estimatedndarray) –Estimated parameter values. Can either be provided as a 2D array of shape shape
(n_observations, n_params), or as a 3D array of shape(n_samples, n_observations, n_params), in which case the mean of the samples is plotted. -
(param_namesList[str], default:None) –List of parameter names. Defaults to
None. -
(scalefloat, default:1.0) –Scale of the figure. Defaults to
1.0. -
(save_pathstr, default:None) –Path to save the figure to. Defaults to
None. -
(save_fnamestr, default:None) –Filename to use when saving. Defaults to
None.
Source code in model_fit_tools/plotting.py
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 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | |
plot_pp
plot_pp(true: ndarray, estimated: ndarray, param_names: Union[List[str], None] = None, scale: float = 1.0, save_path: Union[str, None] = None, save_fname: Union[str, None] = None) -> None
Probability-probability plot. Plots the proportion of observations with values that fall within a given credible interval against the credible interval probability. Used for assessing how well-calibrated the posterior is. Perfectly calibrated posteriors should result in points lying on the diagonal.
NOTE: Designed for use with posterior distributions, cannot be used with point estimates.
Parameters:
-
(truendarray) –True parameter values, shape
(n_observations, n_params). -
(estimatedndarray) –Samples from posterior, shape
(n_samples, n_observations, n_params). -
(param_namesList[str], default:None) –List of parameter names. Defaults to
None. -
(save_pathstr, default:None) –Path to save the plot to. Defaults to
None. -
(scalefloat, default:1.0) –Scale of the plot. Defaults to
1.0. -
(save_pathstr, default:None) –Path to save the plot to. Defaults to None.
-
(save_fnamestr, default:None) –File name for the saved plot, if
save_pathis not None. If None, the file name is generated automatically, including the current time and date. Defaults toNone.
Source code in model_fit_tools/plotting.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 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 | |
plot_recovery
plot_recovery(true: ndarray, estimated: ndarray, param_names: Union[List[str], None] = None, show_correlation: bool = True, scale: float = 1.0, save_path: Union[str, None] = None, save_fname: Union[str, None] = None, colour_by: Union[str, None] = None, scatter_kwargs: Dict[str, Any] = {}) -> None
Plots recovered parameter values against true ones. Used to determine how well the model has recovered the true parameter values.
Can be used with either point estimates or posterior samples.
Parameters:
-
(truendarray) –True parameter values, shape
(n_observations, n_params). -
(estimatedndarray) –Estimated parameter values. Can either be provided as a 2D array of shape shape
(n_observations, n_params), or as a 3D array of shape(n_samples, n_observations, n_params), in which case the mean of the samples is plotted. -
(param_namesList[str], default:None) –List of parameter names. Defaults to
None. -
(show_correlationbool, default:True) –Whether to show the correlation coefficient in the title of the plot. Defaults to
True. -
(scalefloat, default:1.0) –Scale of the plot. Defaults to
1.0. -
(save_pathstr, default:None) –Path to save the plot to. Defaults to
None. -
(save_fnamestr, default:None) –File name for the saved plot, if
save_pathis notNone. IfNone, the file name is generated automatically, including the current time and date. Defaults toNone. -
(colour_bystr, default:None) –Parameter name to colour the points by. Defaults to
None. -
(scatter_kwargsDict[str, Any], default:{}) –Optional keyword arguments for customizing scatter plot appearance. Defaults to
{}.
Source code in model_fit_tools/plotting.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 | |
plot_recovery_matrix
plot_recovery_matrix(true: ndarray, estimated: ndarray, param_names: Union[List[str], None] = None, scale: float = 1.0, colorbar_scale: float = 1.0, xtick_rotation: float = 0, cmap: str = 'viridis', vmin: Union[float, None] = None, vmax: Union[float, None] = None, save_path: Union[str, None] = None, save_fname: Union[str, None] = None, ax: Union[Axes, None] = None) -> None
Plots a matrix of the correlation coefficients between true and recovered parameter values. Used to determine how well the model has recovered the true parameter values.
Parameters:
-
(truendarray) –True parameter values, shape
(n_observations, n_params). -
(estimatedndarray) –Estimated parameter values. Can either be provided as a 2D array of shape shape
(n_observations, n_params), or as a 3D array of shape(n_samples, n_observations, n_params), in which case the mean of the samples is plotted. -
(param_namesList[str], default:None) –List of parameter names. Defaults to None.
-
(scalefloat, default:1.0) –Scale of the plot. Defaults to
1.0. colorbar_scale (float, optional): Scale for the colorbar. Defaults to1.0. -
(xtick_rotationfloat, default:0) –Degrees by which to rotate x tick labels. Defaults to
0. -
(cmapstr, default:'viridis') –Colormap to use. Defaults to
"viridis". -
(vminfloat, default:None) –Minimum value. Defaults to
None. -
(vmaxfloat, default:None) –Maximum value. Defaults to
None. -
(save_pathstr, default:None) –Path to save the plot to. Defaults to
None. -
(save_fnamestr, default:None) –File name for the saved plot, if
save_pathis notNone. IfNone, the file name is generated automatically, including the current time and date. Defaults toNone. -
(axAxes, default:None) –Axes object to plot on. Defaults to
None.
Source code in model_fit_tools/plotting.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 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 | |
plot_waic
plot_waic(waic_data: Union[Dict[str, Any], DataFrame], best_model_idx: int, bar_kwargs: Dict[str, Any] = {}, ylim_kwargs: Dict[str, Any] = {}, fig_kwargs: Dict[str, Any] = {}, rotate_xticks: bool = False, ax: Optional[Axes] = None, colours: Optional[List[str]] = None, model_rename_dict: Optional[Dict[str, str]] = None) -> None
Creates a bar plot of WAIC values with standard error for given models.
This function visualizes WAIC values along with their respective standard error for various models, highlighting the "best" model with a different color.
Parameters:
-
(waic_dataUnion[Dict[str, Any], DataFrame]) –A dictionary where keys are strings representing model names and values are objects with
elpd_waicandseattributes, or a DataFrame with columns"model","waic", and"se". -
(best_model_idxint) –Index of the model considered as the best, which will be highlighted with a different color in the plot.
-
(bar_kwargsDict[str, Any], default:{}) –Optional keyword arguments for customizing bar appearance.
-
(ylim_kwargsDict[str, Any], default:{}) –Optional keyword arguments for customizing y-axis limits.
-
(fig_kwargsDict[str, Any], default:{}) –Optional keyword arguments for customizing figure attributes.
-
(rotate_xticksbool, default:False) –Whether to rotate x-axis tick labels by 45 degrees. Defaults to
False. -
(axOptional[Axes], default:None) –Matplotlib axis to plot on. If
None, a new figure and axis are created. colours (Optional[List[str]]): List of colors to use for each bar. If None, default Matplotlib colors are used. Defaults toNone -
(model_rename_dictOptional[Dict[str, str]], default:None) –Dictionary mapping model names to their display names. Defaults to
None.
Source code in model_fit_tools/plotting.py
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 | |