GraphicalVisualizer#
- class lumopt2.utils.graphical_visualizer.GraphicalVisualizer(panels: List[Panel] | None = None, layout: Tuple[int, int] | None = None, figsize: Tuple[float, float] = (12, 5), update_interval: int = 1, block_on_end: bool = False, save_plots: bool = True, save_dpi: int = 150, filename_prefix: str = 'optimization_plot', show_window: bool = True)#
Callback that visualizes optimization progress with live matplotlib plots.
The visualizer arranges a list of
Panelinstances on a grid of subplots and dispatches the standard panel lifecycle calls (setup,update,on_optimization_end) on every iteration. Users compose figures by passing any combination of built-in panels (FomPanel,GradientNormPanel,GeometryPanel,MonitorPanel) and their ownPanelsubclasses.- Parameters:
- panels
listofPanel,optional Panels to render. Panels are laid out on the grid in row-major order:
panels[0]goes in the top-left cell, the rest of the first row is filled left-to-right, then the next row, and so on untilpanels[-1]lands in the bottom-right area. WhenNone(default), a sensible default list is built aton_optimization_start():FomPanelis always included.GradientNormPanelis always included; it falls back to an “N/A” title when no gradient data flows in.GeometryPanelis included only whenproject.parametrizationexposes a callablevisualize(ax, params, initial_params)method.
Pass an empty list (
[]) to disable plotting entirely while still keeping the visualizer in the callback chain.- layout
tupleofint,optional Explicit
(rows, cols)grid. WhenNone(default), an approximately square grid is picked automatically. Must satisfyrows * cols >= len(panels).- figsize
tupleoffloat,optional Figure size in inches. When
layoutisNone, the height is multiplied by the auto-picked row count so figures with many rows stay readable. Whenlayoutis explicit,figsizeis used as-is (default:(12, 5)).- update_interval
int,optional Refresh the figure every N iterations. Set to 1 for live updates; values > 1 reduce overhead on long runs (default:
1).- block_on_endbool,
optional If
True, keep the plot window open and block at the end ofon_optimization_end()until the user closes it (default:False).- save_plotsbool,
optional If
True, save the full figure as a PNG into the project folder after every refresh (default:True).- save_dpi
int,optional DPI used for saved PNGs (default:
150).- filename_prefix
str,optional Prefix for the saved PNG filenames; the full pattern is
{filename_prefix}_iter_{iteration:04d}.png(default:"optimization_plot"). When two or moreGraphicalVisualizerinstances are active in the same Python session and resolve to the same save folder (e.g. two visualizers attached to the sameOptimizationwithout explicit prefixes), the visualizer auto-appends_2,_3, … to the second and later prefixes so the instances do not overwrite each other’s PNGs. A warning is logged whenever such auto-disambiguation kicks in. Pass an explicit unique prefix per visualizer to opt out of the warning entirely.- show_windowbool,
optional If
True(default), pop the matplotlib figure on screen and refresh it interactively as the optimization progresses. Set toFalseto keep the figure offscreen – useful when running headless / batch optimizations withsave_plots=Truewhere the user only cares about the saved PNGs. Whenshow_windowisFalse,block_on_endis silently ignored (there is no window to block on).
- panels
- Attributes:
- panels
listofPanel The panels currently registered on the visualizer. Mutating the list before
on_optimization_start()is fine; mutating it later has no effect (the figure is laid out once).- layout
tupleofintorNone - figsize
tupleoffloat - update_interval
int - block_on_endbool
- save_plotsbool
- save_dpi
int - filename_prefix
str - show_windowbool
- panels
- Raises:
ValueErrorIf
save_dpiorupdate_intervalis not positive, iflayoutis not a positive(rows, cols)tuple, or if the layout has fewer cells than panels.TypeErrorIf any element of
panelsis not aPanelinstance.
Notes
The plot window updates incrementally during optimization. Closing the window does not stop the optimization; the visualizer simply silently fails to redraw subsequent updates.
Examples
Default visualizer (FOM + gradient norm + geometry where applicable):
>>> visualizer = lmpt.GraphicalVisualizer()
Explicit panel composition:
>>> from lumopt2 import (FomPanel, GradientNormPanel, ... GeometryPanel, MonitorPanel) >>> visualizer = lmpt.GraphicalVisualizer( ... figsize=(14, 6), ... layout=(3, 2), ... panels=[ ... FomPanel(), ... GradientNormPanel(), ... GeometryPanel(), ... MonitorPanel(monitor_name='field_monitor', ... result_name='Ey', operation='real'), ... MonitorPanel(monitor_name='FDTD::ports::port_h_out', ... result_name='S', operation='abs^2'), ... ], ... save_plots=True, ... block_on_end=True, ... )
Methods
GraphicalVisualizer.on_function_eval(...[, ...])Record the initial parameter vector on the first evaluation.
GraphicalVisualizer.on_iteration_end(...[, ...])Append history, refresh the figure, and (optionally) save it.
No-op iteration-start hook (kept for symmetry with BaseCallback).
Render the final state, dispatch end hooks, optionally block.
Resolve default panels, set up the figure, configure the save dir.