openscm_calibration.scipy_plotting#
Support for plotting during scipy optimisation
Modules:
| Name | Description |
|---|---|
base |
Base implementation of support for plotting during scipy optimisation |
scmdata |
Support for plotting during scipy optimisation with [ |
Classes:
| Name | Description |
|---|---|
CallbackProxy |
Callback helper |
NoSuccessfulRunsError |
Raised when no runs completed successfully i.e. there is nothing to plot |
OptPlotter |
Optimisation progress plotting helper |
Functions:
| Name | Description |
|---|---|
get_optimisation_mosaic |
Get optimisation mosaic |
get_runs_to_plot |
Get runs to plot |
get_ymax_default |
Get y-max based on costs |
plot_costs |
Plot cost function |
plot_parameters |
Plot parameters |
CallbackProxy #
Bases: Generic[DataContainer]
Callback helper
This class acts as a proxy and decides whether the real callback should actually be called. If provided, it also keeps track of the number of model calls via a progress bar.
Methods:
| Name | Description |
|---|---|
callback_differential_evolution |
Update the plots |
callback_minimize |
Update the plots |
time_to_call_real_callback |
Check whether it is time to call the real callback |
update_progress_bar |
Update the progress bar |
Attributes:
| Name | Type | Description |
|---|---|---|
last_callback_val |
int
|
Number of model calls at last callback |
progress_bar |
tqdm[Any] | None
|
Progress bar to track iterations |
real_callback |
SupportsScipyOptCallback
|
Callback to be called if a sufficient number of runs have been done |
store |
OptResStore[DataContainer]
|
Optimisation result store |
update_every |
int
|
Update the plots every X calls to the model |
Source code in src/openscm_calibration/scipy_plotting/base.py
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 | |
last_callback_val
class-attribute
instance-attribute
#
last_callback_val: int = 0
Number of model calls at last callback
progress_bar
class-attribute
instance-attribute
#
progress_bar: tqdm[Any] | None = None
Progress bar to track iterations
real_callback
instance-attribute
#
real_callback: SupportsScipyOptCallback
Callback to be called if a sufficient number of runs have been done
update_every
class-attribute
instance-attribute
#
update_every: int = 50
Update the plots every X calls to the model
callback_differential_evolution #
callback_differential_evolution(
xk: NDArray[number[Any]],
convergence: float | None = None,
) -> None
Update the plots
Intended to be used as the callback argument to
scipy.optimize.differential_evolution
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xk
|
NDArray[number[Any]]
|
Parameter vector with best solution found so far |
required |
convergence
|
float | None
|
Received from :func: |
None
|
Source code in src/openscm_calibration/scipy_plotting/base.py
callback_minimize #
Update the plots
Intended to be used as the callback argument to
scipy.optimize.minimize
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xk
|
NDArray[number[Any]]
|
Last used parameter vector |
required |
Source code in src/openscm_calibration/scipy_plotting/base.py
time_to_call_real_callback #
time_to_call_real_callback() -> bool
Check whether it is time to call the real callback
Returns:
| Type | Description |
|---|---|
``True`` if the real callback should be called
|
|
Source code in src/openscm_calibration/scipy_plotting/base.py
update_progress_bar #
update_progress_bar(n_model_calls: int) -> None
Update the progress bar
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_model_calls
|
int
|
Number of model calls in total |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
Source code in src/openscm_calibration/scipy_plotting/base.py
NoSuccessfulRunsError #
Bases: ValueError
Raised when no runs completed successfully i.e. there is nothing to plot
OptPlotter #
Bases: Generic[DataContainer]
Optimisation progress plotting helper
This class is an adapter between interfaces required by Scipy's callback arguments and updating the plots. The class holds all the information required to make useful plots. It is intended to be used in interactive Python i.e. to make updating plots.
Methods:
| Name | Description |
|---|---|
callback_differential_evolution |
Update the plots |
callback_minimize |
Update the plots |
from_autogenerated_figure |
Create plotter with automatic figure generation |
update_plots |
Update all the plots |
Attributes:
| Name | Type | Description |
|---|---|---|
axes |
dict[str, Axes]
|
Dictionary storing axes on which to plot |
convert_results_to_plot_dict |
ResultsToDictConverter[DataContainer]
|
Callable which converts results into a dictionary |
cost_key |
str
|
Key for the axes on which the cost function should be plotted |
fig |
Figure
|
Figure on which to plot |
get_timeseries |
Callable[[DataContainer], DataFrame]
|
Function which converts data into timeseries. |
holder |
SupportsFigUpdate
|
Figure updater, typically [ |
parameters |
tuple[str, ...]
|
Parameters to be optimised |
plot_costs |
PlotCostsLike | None
|
Function that plots our costs |
plot_parameters |
PlotParametersLike | None
|
Function that plots our parameters |
plot_timeseries |
PlotTimeseriesLike[DataContainer]
|
Function that plots our timeseries |
store |
OptResStore[DataContainer]
|
Optimisation result store |
target |
DataContainer
|
Target used for optimisation |
thin_ts_to_plot |
int
|
Thinning to apply to the timeseries to plot |
timeseries_axes |
tuple[str, ...]
|
Axes on which to plot timeseries |
Source code in src/openscm_calibration/scipy_plotting/base.py
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 400 401 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 522 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 | |
axes
instance-attribute
#
Dictionary storing axes on which to plot
The plot of the cost function over time will be plotted on the axes
with key given by cost_key.
Each parameter will be plotted on the axes with the same key as the parameter
(as defined in parameters).
The timeseries will be plotted on the axes specified by timeseries_axes.
See docstring of timeseries_axes for rules about its values.
convert_results_to_plot_dict
instance-attribute
#
convert_results_to_plot_dict: ResultsToDictConverter[
DataContainer
]
Callable which converts results into a dictionary
in which the keys are a subset of the values in timeseries_axes
cost_key
instance-attribute
#
cost_key: str
Key for the axes on which the cost function should be plotted
get_timeseries
instance-attribute
#
Function which converts data into timeseries.
holder
instance-attribute
#
holder: SupportsFigUpdate
Figure updater, typically [IPython.core.display_functions.DisplayHandle][IPython.core.display_functions.DisplayHandle]
parameters
class-attribute
instance-attribute
#
Parameters to be optimised
This must match the order in which the parameters are handled by the optimiser, i.e. it is used to translate the unlabeled array of parameter values onto the desired axes.
plot_costs
class-attribute
instance-attribute
#
plot_costs: PlotCostsLike | None = None
Function that plots our costs
If not supplied, we use
plot_costs.
plot_parameters
class-attribute
instance-attribute
#
plot_parameters: PlotParametersLike | None = None
Function that plots our parameters
If not supplied, we use
plot_parameters.
plot_timeseries
instance-attribute
#
plot_timeseries: PlotTimeseriesLike[DataContainer]
Function that plots our timeseries
thin_ts_to_plot
class-attribute
instance-attribute
#
thin_ts_to_plot: int = 20
Thinning to apply to the timeseries to plot
In other words, only plot every thin_ts_to_plot runs on the timeseries plots.
Plotting all runs can be very expensive.
timeseries_axes
class-attribute
instance-attribute
#
timeseries_axes: tuple[str, ...] = field(
validator=[
_all_in_axes,
_compatible_with_convert_and_target,
]
)
Axes on which to plot timeseries
The timeseries in target and store.res
are converted into dictionaries using convert_results_to_plot_dict.
The keys of the result of convert_results_to_plot_dict
must match the values in timeseries_axes.
callback_differential_evolution #
callback_differential_evolution(
xk: NDArray[number[Any]],
convergence: float | None = None,
) -> None
Update the plots
Intended to be used as the callback argument to
scipy.optimize.differential_evolution
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xk
|
NDArray[number[Any]]
|
Parameter vector with best solution found so far |
required |
convergence
|
float | None
|
Received from [ |
None
|
Source code in src/openscm_calibration/scipy_plotting/base.py
callback_minimize #
from_autogenerated_figure
classmethod
#
from_autogenerated_figure(
cost_key: str,
params: tuple[str],
convert_results_to_plot_dict: ResultsToDictConverter[
DataContainer
],
target: DataContainer,
store: OptResStore[DataContainer],
kwargs_create_mosaic: dict[str, Any] | None = None,
kwargs_get_fig_axes_holder: dict[str, Any]
| None = None,
**kwargs: Any,
) -> OptPlotter[DataContainer]
Create plotter with automatic figure generation
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_key
|
str
|
Name to use for the cost axis |
required |
params
|
tuple[str]
|
Parameters that are being optimised This is used to generate the plotting axes. It must also match the order in which the parameters are handled by the optimiser, i.e. it is used to translate the unlabeled array of parameter values onto the desired axes. |
required |
convert_results_to_plot_dict
|
ResultsToDictConverter[DataContainer]
|
Callable which converts results into a dictionary |
required |
target
|
DataContainer
|
Target to which we are optimising |
required |
store
|
OptResStore[DataContainer]
|
Optimisation result store |
required |
kwargs_create_mosaic
|
dict[str, Any] | None
|
Passed to |
None
|
kwargs_get_fig_axes_holder
|
dict[str, Any] | None
|
Passed to |
None
|
**kwargs
|
Any
|
Passed to the initialiser of |
{}
|
Returns:
| Type | Description |
|---|---|
OptPlotter[DataContainer]
|
Initialised instance with generated figure, axes and holder |
Source code in src/openscm_calibration/scipy_plotting/base.py
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 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
update_plots #
Update all the plots
Source code in src/openscm_calibration/scipy_plotting/base.py
get_optimisation_mosaic #
get_optimisation_mosaic(
cost_key: str,
params: tuple[str, ...],
timeseries: tuple[str, ...],
cost_col_relwidth: int = 1,
n_parameters_per_row: int = 1,
n_timeseries_per_row: int = 1,
) -> list[list[str]]
Get optimisation mosaic
This gives back the grid of axes to use for plotting. It can be understood by matplotlib but in theory could be used with any tool that understands such mosaics/grids.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cost_key
|
str
|
Name to use for the cost axis |
required |
params
|
tuple[str, ...]
|
Parameters axes to generate |
required |
timeseries
|
tuple[str, ...]
|
Timeseries axes to generate |
required |
cost_col_relwidth
|
int
|
Width of the cost axis, relative to the width of each parameter axis |
1
|
n_parameters_per_row
|
int
|
Number of parameters to plot per row (as many rows as are needed to plot all the parameters are created) |
1
|
n_timeseries_per_row
|
int
|
Number of timeseries to plot per row (as many rows as are needed to plot all the timeseries are created) |
1
|
Returns:
| Type | Description |
|---|---|
Mosaic
|
|
Source code in src/openscm_calibration/scipy_plotting/base.py
get_runs_to_plot #
get_runs_to_plot(
costs: tuple[float, ...],
res: tuple[DataContainer, ...],
thin_ts_to_plot: int,
) -> tuple[DataContainer, tuple[DataContainer, ...]]
Get runs to plot
This retrieves the run which best matches the target (has lowest cost) and then a series of others to plot.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
costs
|
tuple[float, ...]
|
Cost function value for each run (used to determine the best result) |
required |
res
|
tuple[DataContainer, ...]
|
Results of each run. It is assumed that the elements in |
required |
thin_ts_to_plot
|
int
|
Thinning to apply to the timeseries to plot In other words, only plot every |
required |
Returns:
| Type | Description |
|---|---|
tuple[DataContainer, tuple[DataContainer, ...]]
|
Best iteration and other runs to plot |
Raises:
| Type | Description |
|---|---|
ValueError
|
No successful runs are included in |
Source code in src/openscm_calibration/scipy_plotting/base.py
get_ymax_default #
get_ymax_default(
costs: tuple[float, ...],
min_scale_factor: float = 10.0,
min_v_median_scale_factor: float = 2.0,
) -> float
Get y-max based on costs
This is the default function used by
plot_costs.
The algorithm is
.. math::
\text{ymax} = min(
\text{min_scale_factor} \times min(costs),
max(
median(costs),
\text{min_v_median_scale_factor} \times min(costs)
)
)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
costs
|
tuple[float, ...]
|
Cost function values |
required |
min_scale_factor
|
float
|
Value by which the minimum value is scaled when determining the plot limits |
10.0
|
min_v_median_scale_factor
|
float
|
Value by which the minimum value is scaled when comparing to the median as part of determining the plot limits |
2.0
|
Returns:
| Type | Description |
|---|---|
Maximum value to use on the y-axis
|
|
Source code in src/openscm_calibration/scipy_plotting/base.py
plot_costs #
plot_costs(
ax: Axes,
ylabel: str,
costs: tuple[float, ...],
ymin: float = 0.0,
get_ymax: Callable[[tuple[float, ...]], float]
| None = None,
alpha: float = 0.7,
**kwargs: Any,
) -> None
Plot cost function
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ax
|
Axes
|
Axes on which to plot |
required |
ylabel
|
str
|
y-axis label |
required |
costs
|
tuple[float, ...]
|
Costs to plot |
required |
ymin
|
float
|
Minimum y-axis value |
0.0
|
get_ymax
|
Callable[[tuple[float, ...]], float] | None
|
Function which gets the y-max based on the costs. If not provided,
:func: |
None
|
alpha
|
float
|
Alpha to apply to plotted points |
0.7
|
**kwargs
|
Any
|
Passed to :meth: |
{}
|
Source code in src/openscm_calibration/scipy_plotting/base.py
plot_parameters #
plot_parameters(
axes: dict[str, Axes],
para_vals: dict[str, NDArray[number[Any]]],
alpha: float = 0.7,
**kwargs: Any,
) -> None
Plot parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
axes
|
dict[str, Axes]
|
Axes on which to plot. The keys should match the keys in |
required |
para_vals
|
dict[str, NDArray[number[Any]]]
|
Parameter values. Each key should be the name of a parameter |
required |
alpha
|
float
|
Alpha to use when calling :meth: |
0.7
|
**kwargs
|
Any
|
Passed to each call to :meth: |
{}
|