OptimizationPlotter#
- class nextnano_optimizers.plotting.OptimizationPlotter(data: dict, param_names: list | None = None, fitness_names: list | None = None, exclude_failed: bool = False, fitness_limits: list | dict | None = None, progress_series: list | str | None = None, series_labels: dict | None = None, series_specs: dict | None = None, iteration_label: str | None = None, progress_title: str | None = None, population_label: str | None = None)#
Bases:
objectVisualize the progress and results of an optimization.
Provides figures showing the objective values and input variables throughout an optimization, as well as the final population in input and objective space.
The plotter can be created from an optimization log or an optimizer instance. Each plotting method returns a Matplotlib Figure for further customization or saving.
- Parameters:
- datadict
Optimization results in the JSON log format produced by the optimizers.
- param_nameslist of str, optional
Labels for the optimization variables. Defaults to
param1,param2, etc.- fitness_nameslist of str, optional
Labels for the objectives. Defaults to
fitness1,fitness2, etc.- exclude_failedbool, optional
If
True, exclude evaluations assigned thenon_execution_penaltyfrom the plots (i.e. failed evaluations).- fitness_limitslist or dict, optional
Plotting limits for each objective, specified as
(min, max)pairs. If dictionary is used, the keys should match the fitness names.- progress_seriessequence of str or str, optional
Curves to display in the optimization progress plot. Use
"all"to display all available curves. The series applicable to all optimizations: (“best”, “candidates”). The series also applicable to evolution: (“min-max”, “min”, “max”, “median”, “mean”).- series_labelsdict, optional
Custom legend labels for progress_series curves, keyed by curve tag from the
progress_series.- series_specsdict, optional
Custom plotting properties for individual curves, keyed by curve name. Each entry can specify
kindand/orstyle. See examples below.- iteration_labelstr, optional
Label used for optimization iterations.
- progress_titlestr, optional
Title of the optimization progress plot.
- population_labelstr, optional
Label used for candidates in the final iteration.
- Attributes:
- runRunData
Parsed optimization results, including input and objective dimensions, labels, evaluation counts, and the run summary.
Examples
Create a plotter from a saved optimization log and customize the labels and title of the figures:
plotter = OptimizationPlotter.from_json( "Simulation_00001.json", param_names=["Well width (nm)", "Barrier width (nm)"], fitness_names=["Energy error (eV)"], progress_title="Quantum well optimization", ) plotter.plot_all(show=True)
Customize the progress curves and objective limits for an evolutionary optimization. Include only candidates whose objective values are within the specified ranges, and customize the styles of the “best” and “mean” curves:
plotter = OptimizationPlotter.from_json( "Simulation_00001.json", fitness_names=["Energy error", "Efficiency"], fitness_limits={ "Energy error": (0, 0.5), "Efficiency": (0, 1), }, progress_series=["best", "mean", "min-max"], series_specs={ "best": {"kind": "line", "style": {"linewidth": 2}}, "mean": {"kind": "line", "style": {"linestyle": "--"}}, }, ) plotter.plot_all(show=True)
- classmethod from_json(json_path, **kwargs) OptimizationPlotter#
Create a plotter from a saved optimization log.
Examples
Create a plotter from a saved optimization log and customize the labels.
plotter = OptimizationPlotter.from_json( "Simulation_00001.json", param_names=["Well width (nm)", "Barrier width (nm)"], )
- classmethod from_optimizer(optimizer, **kwargs) OptimizationPlotter#
Create a plotter from a completed optimization.
Loads the optimization results recorded by the optimizer.
- Parameters:
- optimizerEvolution or LocalOptimizer or BayesianOptimization
An optimizer whose
runhas produced a log.- **kwargs
Additional plotting configuration passed to
OptimizationPlotter.
- Returns:
- OptimizationPlotter
Plotter initialized with the optimizer’s results.
Examples
Create a plotter from a completed local optimization and customize the figure labels and title:
from nextnano_optimizers.optimizer import LocalOptimizer from nextnano_optimizers.plotting import OptimizationPlotter ... optimizer = LocalOptimizer( nextnanoio=io, metric=metric, bounds=[(3, 10), (2, 8)], ) optimizer.run(method="Nelder-Mead") plotter = OptimizationPlotter.from_optimizer( optimizer, param_names=["Well width (nm)", "Barrier width (nm)"], fitness_names=["Energy error (eV)"], progress_title="Quantum well optimization", ) plotter.plot_all(show=True)
- plot_all(save_dir=None, show=False, progress_series=None, fig_format='png')#
Generate all available optimization figures.
Generates the progress figure and, when applicable, figures showing the final population in input and objective space.
- Parameters:
- save_dirstr, default=None
Directory in which to save the figures. Created if it does not exist. If
None, figures are not saved.- showbool, default=False
Display figures in a window.
- progress_seriessequence of str or str, default=None
Curves to display in the progress figure. Overrides the plotter’s default
progress_seriesconfiguration.- fig_formatstr, default=”png”
Format of the saved figures, such as
"png","pdf", or"svg".
- Raises:
- ValueError
If the requested figure format is not supported by Matplotlib.
- plot_inputs()#
Plot the final population in input-variable space.
Shows the final population, with a separate scatter plot for each pair of input variables. Pareto-optimal candidates are highlighted.
- Returns:
- matplotlib.figure.Figure or None
- plot_pareto()#
Plot the final population in objective space.
Shows the final population, with a separate scatter plot for each pair of objectives. Pareto-optimal candidates are highlighted.
For two objectives, the Pareto-optimal points are connected to illustrate the Pareto front.
- Returns:
- matplotlib.figure.Figure or None
Figure showing the final population in objective space, or
Noneif fewer than two objectives are defined.
- plot_progress(fig=None, series=None)#
Plot the progress of the optimization.
Shows the objective values and input variables of the evaluated candidates throughout the optimization. Each objective and input variable is displayed in a separate panel.
- Parameters:
- figmatplotlib.figure.Figure, optional
Existing figure to clear and reuse. If not provided, a new figure is created.
- serieslist of str or str, optional
Progress curves to display. Available options are
"candidates","min-max","min","max","median","mean", and"best". Use"all"to display every curve.Defaults to the curves configured through
progress_series.
- Returns:
- matplotlib.figure.Figure
Figure showing the optimization progress.
Notes
The
"best"curve shows the lowest objective value found so far and is displayed only for objectives.If evaluations are excluded through
exclude_failedorfitness_limits, an additional panel shows the number of excluded evaluations at each iteration.