Optimizer.MplMonitor#

Optimizer.MplMonitor.py

migration of FullOptDialog to Jupyter Notebook

class MplMonitor(function_code=None, clear_jobs=True, debug=True)#

Bases: object

Interactive Jupyter notebook monitor for optimization processes with subprocess management.

This class provides a dashboard-based interface for running and monitoring optimization jobs in Jupyter notebooks. It manages background subprocess execution, provides real-time progress visualization, and implements robust recovery mechanisms to prevent losing control of running processes when notebook outputs are cleared.

The monitor tracks active processes through both an in-memory registry and a persistent file-based registry, allowing recovery from accidental notebook state loss.

Parameters:
  • function_code (str, optional) – Function code identifier for logging purposes.

  • clear_jobs (bool, default=True) – If True, clears existing job folders in the optimizer directory on initialization.

  • debug (bool, default=True) – If True, enables debug mode with module reloading for development.

optimizer_folder#

Path to the folder containing optimization outputs and logs.

Type:

str

logger#

Logger instance for recording monitor activities.

Type:

logging.Logger

runner#

Background process runner managing the subprocess execution.

Type:

BackRunner

dashboard#

The main dashboard widget containing plots and controls.

Type:

ipywidgets.VBox

process_id#

String representation of the current subprocess PID.

Type:

str

instance_id#

Unique identifier for this monitor instance.

Type:

int

Examples

Basic usage with automatic recovery:

from molass_legacy.Optimizer.MplMonitor import MplMonitor

# Create and configure monitor
monitor = MplMonitor(clear_jobs=True)
monitor.create_dashboard()

# Run optimization
monitor.run(optimizer, init_params, niter=20, max_trials=30)
monitor.show()
monitor.start_watching()

Recovering a lost dashboard after clearing notebook outputs:

# Retrieve the most recent active monitor
monitor = MplMonitor.get_active_monitor()
monitor.redisplay_dashboard()

Checking all active monitors:

# Display status of all running monitors
MplMonitor.show_active_monitors()

# Get all active instances
monitors = MplMonitor.get_all_active_monitors()

Cleaning up orphaned processes:

# Interactive cleanup of orphaned processes
MplMonitor.cleanup_orphaned_processes()

Notes

  • The monitor maintains two registries: an in-memory registry for quick access to active instances, and a file-based registry (active_processes.json) for subprocess tracking that persists across notebook sessions.

  • When creating a new monitor while others are active, a warning is displayed with instructions for recovery.

  • The dashboard includes real-time plot updates, status indicators, and control buttons for terminating jobs and exporting data.

  • Background processes are automatically cleaned up when the monitor detects they are orphaned or when the monitor instance is destroyed.

  • For optimal use in Jupyter notebooks, use start_watching() to run progress monitoring in a background thread, keeping the notebook interactive.

Note

Process registry and dashboard recovery features implemented with assistance from GitHub Copilot (January 2026).

See also

BackRunner

Manages subprocess execution for optimization jobs.

JobState

Tracks and parses optimization job state from callback files.

classmethod cleanup_orphaned_processes(optimizer_folder=None)#

Class method to manually clean up orphaned processes.

This can be called from a fresh notebook cell without an instance:

MplMonitor.cleanup_orphaned_processes()

Parameters:

optimizer_folder – Path to optimizer folder. If None, uses default from settings.

clear_jobs()#
create_dashboard()#
export_data(b, debug=True)#
classmethod get_active_monitor()#

Get the most recently created active monitor instance.

Returns the last MplMonitor instance that was created and is still active. Useful for recovering access to a monitor after clearing notebook outputs.

Returns:

The most recent active monitor, or None if no monitors exist.

Return type:

MplMonitor

Example

# After clearing outputs: monitor = MplMonitor.get_active_monitor() if monitor:

monitor.redisplay_dashboard()

classmethod get_all_active_monitors()#

Get all active monitor instances.

Returns:

List of all active MplMonitor instances.

Return type:

list

get_best_params(plot_info=None)#
redisplay_dashboard()#

Redisplay the dashboard after it has been cleared.

This method allows you to reconnect to a running monitor after accidentally clearing notebook outputs. Call this to get the dashboard back.

Example

# After clearing outputs, retrieve and redisplay: monitor = MplMonitor.get_active_monitor() monitor.redisplay_dashboard()

run(optimizer, init_params, niter=20, seed=1234, max_trials=30, work_folder=None, dummy=False, x_shifts=None, debug=False, devel=True)#
run_impl(optimizer, init_params, niter=20, seed=1234, work_folder=None, dummy=False, debug=False, devel=False)#
save_the_result_figure(fig_file=None)#
show(debug=False)#
classmethod show_active_monitors()#

Display information about all active monitor instances.

Shows a summary of all currently active monitors including their status, process ID, and working folder if available.

Example

MplMonitor.show_active_monitors()

start_watching()#
trigger_terminate(b)#
update_plot()#
watch_progress(interval=1.0)#