MLTrainer
Namespace: MLLIB · Library: Machine Learning Library
#include <ml/ml_trainer.h>
class MLLIB::MLTrainer
Launches Python training scripts via PythonRunner with automatic venv handling and prerequisite checks.
Training logic (PyTorch, scikit-learn, MNE-Python, …) lives entirely in the .py scripts under scripts/ml/training/ and runs in a child process; no Python embedding or libpython linkage is required. If the configured PythonRunnerConfig carries a venvDir, the runner creates and updates the venv before each script invocation so dependencies are pinned per training job.
QTemporaryDir dir;
QFile script(dir.filePath("train.py"));
const bool written = script.open(QIODevice::WriteOnly) && script.write("import sys\nprint('trained on', sys.argv[1], 'epochs')\n") > 0;
script.close();
MLTrainer trainer; // runs python3; a venv and pip install are configured via UTILSLIB::PythonRunnerConfig
const UTILSLIB::PythonRunnerResult result = trainer.run(script.fileName(), {"5"});
Public Methods
MLTrainer()
Construct an MLTrainer with default PythonRunner configuration.
MLTrainer(config)
Construct an MLTrainer with explicit PythonRunner configuration.
Parameters:
- config : const PythonRunnerConfig & PythonRunner configuration to use.
runner()
Access the underlying PythonRunner for callback/config changes.
Returns:
- PythonRunner & — Reference to the PythonRunner instance.
run(scriptPath, args)
Run a training script.
If the PythonRunner is configured with a venvDir and packageDir, the venv is created/updated automatically before the script runs.
Parameters:
-
scriptPath : const QString & Path to the .py training script.
-
args : const QStringList & Arguments forwarded to the script.
Returns:
- PythonRunnerResult — PythonRunnerResult with exit code, output, and progress.
checkPrerequisites(packages)
Check that the required Python packages are importable.
Parameters:
- packages : const QStringList & List of package names (e.g. {"torch", "mne"}).
Returns:
- QStringList — List of packages that could NOT be imported (empty = all OK).
Example
- Full source:
src/examples/ex_ml/main.cpp - Build target:
ex_ml(runs under CTest)
Used in examples
ex_ml
ML library: tensors, ONNX inference through the MlModel interface, and the Python trainer.
QFile script(dir.filePath("train.py"));
const bool written = script.open(QIODevice::WriteOnly) && script.write("import sys\nprint('trained on', sys.argv[1], 'epochs')\n") > 0;
script.close();
MLTrainer trainer; // runs python3; a venv and pip install are configured via UTILSLIB::PythonRunnerConfig
const UTILSLIB::PythonRunnerResult result = trainer.run(script.fileName(), {"5"});
if (trainer.runner().isPythonAvailable()) {
ok &= expect(written && result.success && result.stdOut.trimmed() == "trained on 5 epochs" && trainer.checkPrerequisites({"json", "no_such_package_xyz"}) == QStringList({"no_such_package_xyz"}),
"MLTrainer runs the script with its arguments and reports the missing package");
} else {
qInfo().noquote() << " skip MLTrainer (python3 not found)";
}
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>