Optimization manager — napari_easytrack.analysis.optim_manager
Optimization manager for running btrack parameter optimization in background.
This module handles: - Starting optimization in a separate thread - Monitoring progress by polling Optuna study database - Cancelling running optimizations - Retrieving best trials after completion
- class napari_easytrack.analysis.optim_manager.OptimizationManager(db_path='btrack.db')
Manages background optimisation and progress monitoring.
This class uses Optuna for optimisation and runs the process in a separate thread.
- Parameters:
db_path (str)
- db_path
Path to SQLite database for Optuna studies
- db_url
SQLAlchemy database URL
- signals
Qt signals for communication
- current_thread
Thread object for running optimisation
- current_study_name
Name of the current study
- is_running
Whether an optimisation is currently running
- should_cancel
Flag to indicate cancellation request
- start_time
Timestamp when optimisation started
- study
Optuna Study object after completion
- start_optimization()
Start optimisation in background thread
- Parameters:
study_name (str)
n_trials (int)
timeout (int)
timeout_penalty (float)
sampler (str)
use_parallel_backend (bool)
on_progress (Callable | None)
on_finished (Callable | None)
on_error (Callable | None)
- get_progress()
Get current progress of optimisation
- Return type:
Tuple[int, float, int] | None
- cancel_current()
Cancel currently running optimisation
- is_complete()
Check if optimisation has completed
- Return type:
bool
- get_best_trials()
Retrieve best trials from completed study
- Parameters:
study_name (str | None)
max_trials (int)
- Return type:
List[Dict]
- study_exists()
Check if a study exists in the database
- Parameters:
study_name (str)
- Return type:
bool
- get_study_summary()
Get summary statistics for a study
- Parameters:
study_name (str | None)
- Return type:
Dict | None
- cleanup()
Clean up connections and state
- cancel_current()
Cancel currently running optimization.
Note: This is a soft cancel - the current trial will complete but no new trials will start.
- cleanup()
Clean up connections and state.
- get_best_trials(study_name=None, max_trials=15)
Get best trials from a completed study.
- Parameters:
study_name (str | None) – Name of study to load (uses current if None)
max_trials (int) – Maximum number of best trials to return
- Returns:
List of dicts containing trial info (params, AOGM, number)
- Return type:
List[Dict]
- get_progress()
Get current optimization progress by querying study database.
- Returns:
Tuple of (trial_number, best_aogm, elapsed_seconds) or None if no progress
- Return type:
Tuple[int, float, int] | None
- get_study_summary(study_name=None)
Get summary statistics for a study.
- Parameters:
study_name (str | None) – Name of study (uses current if None)
- Returns:
Dict with study summary or None if study not found
- Return type:
Dict | None
- is_complete()
Check if optimization has completed.
- Return type:
bool
- start_optimization(dataset, gt_data, study_name, n_trials=128, timeout=60, timeout_penalty=100000, sampler='tpe', use_parallel_backend=True, on_progress=None, on_finished=None, on_error=None)
Start optimization in background thread.
- Parameters:
dataset – CellTrackingChallengeDataset object
gt_data – Ground truth TrackingGraph
study_name (str) – Name for Optuna study
n_trials (int) – Number of optimization trials
timeout (int) – Timeout per trial in seconds
timeout_penalty (float) – Penalty value for timeouts
sampler (str) – Sampler type (‘tpe’ or ‘random’)
use_parallel_backend (bool) – Whether to use parallel processing
on_progress (Callable | None) – Callback(trial_num, best_aogm, elapsed_time)
on_finished (Callable | None) – Callback(study) when complete
on_error (Callable | None) – Callback(error_msg) on failure
- study_exists(study_name)
Check if a study with given name exists in database.
- Parameters:
study_name (str) – Name of study to check
- Returns:
True if study exists, False otherwise
- Return type:
bool