v2.0.0
Loading...
Searching...
No Matches
UTILSLIB::PythonTestHelper Class Reference

#include <python_test_helper.h>

Public Member Functions

 PythonTestHelper ()
bool isAvailable () const
bool isPythonAvailable () const
bool hasPackage (const QString &packageName) const
PythonRunnerResult eval (const QString &code, int timeoutMs=30000) const
double evalDouble (const QString &code, bool *ok=nullptr, int timeoutMs=30000) const
Eigen::VectorXd evalVector (const QString &code, bool *ok=nullptr, int timeoutMs=60000) const
Eigen::MatrixXd evalMatrix (const QString &code, bool *ok=nullptr, int timeoutMs=60000) const
PythonRunnerResult runScript (const QString &scriptPath, const QStringList &args={}, int timeoutMs=120000) const
Eigen::MatrixXd evalMatrixViaFile (const QString &code, const QString &outputFilePath, bool *ok=nullptr, int timeoutMs=120000) const

Static Public Member Functions

static QString testDataPath ()
static bool isPythonRequired ()
static bool writeMatrix (const QString &filePath, const Eigen::MatrixXd &mat)
static Eigen::MatrixXd readMatrix (const QString &filePath, bool *ok=nullptr)

Detailed Description

Definition at line 128 of file python_test_helper.h.

Constructor & Destructor Documentation

◆ PythonTestHelper()

PythonTestHelper::PythonTestHelper ( )

Constructs a PythonTestHelper with auto-detected Python.

Definition at line 59 of file python_test_helper.cpp.

Member Function Documentation

◆ eval()

PythonRunnerResult PythonTestHelper::eval ( const QString & code,
int timeoutMs = 30000 ) const

Run inline Python code and return the result.

Parameters
[in]codePython code to execute via python -c.
[in]timeoutMsTimeout in milliseconds (-1 = no limit).
Returns
PythonRunnerResult with captured stdout/stderr.

Definition at line 86 of file python_test_helper.cpp.

◆ evalDouble()

double PythonTestHelper::evalDouble ( const QString & code,
bool * ok = nullptr,
int timeoutMs = 30000 ) const

Run inline Python code that prints a single double value, and parse it.

Parameters
[in]codePython code whose stdout is a single number.
[in]okSet to true on success, false on parse failure.
[in]timeoutMsTimeout in milliseconds.
Returns
The parsed double value, or 0.0 on failure.

Definition at line 94 of file python_test_helper.cpp.

◆ evalMatrix()

MatrixXd PythonTestHelper::evalMatrix ( const QString & code,
bool * ok = nullptr,
int timeoutMs = 60000 ) const

Run inline Python code that prints a matrix (one row per line, space-separated values), and parse it into an Eigen MatrixXd.

Parameters
[in]codePython code whose stdout is a space-separated matrix.
[in]okSet to true on success.
[in]timeoutMsTimeout in milliseconds.
Returns
Eigen MatrixXd, or empty matrix on failure.

Definition at line 153 of file python_test_helper.cpp.

◆ evalMatrixViaFile()

MatrixXd PythonTestHelper::evalMatrixViaFile ( const QString & code,
const QString & outputFilePath,
bool * ok = nullptr,
int timeoutMs = 120000 ) const

Run Python code that writes a matrix to outputFilePath (e.g. via numpy.savetxt), then read the result back as an Eigen MatrixXd.

This is preferred over evalMatrix() for large matrices or when full double precision is required, because it avoids stdout parsing.

Example:

QString code = QString(
"import numpy as np\n"
"cov = np.eye(3)\n"
"np.savetxt('%1', cov, fmt='%%.17e')\n"
).arg(outPath);
bool ok;
auto mat = helper.evalMatrixViaFile(code, outPath, &ok);
Parameters
[in]codePython code to execute.
[in]outputFilePathPath where Python writes the matrix.
[out]okSet to true on success.
[in]timeoutMsTimeout in milliseconds.
Returns
Parsed MatrixXd, or empty matrix on failure.

Definition at line 319 of file python_test_helper.cpp.

◆ evalVector()

VectorXd PythonTestHelper::evalVector ( const QString & code,
bool * ok = nullptr,
int timeoutMs = 60000 ) const

Run inline Python code that prints a flat array of doubles (one per line or space-separated), and parse it into an Eigen VectorXd.

Parameters
[in]codePython code whose stdout is numeric values.
[in]okSet to true on success.
[in]timeoutMsTimeout in milliseconds.
Returns
Eigen VectorXd, or empty vector on failure.

Definition at line 110 of file python_test_helper.cpp.

◆ hasPackage()

bool PythonTestHelper::hasPackage ( const QString & packageName) const

Check if a specific Python package is importable.

Parameters
[in]packageNamePackage name (e.g. "numpy", "scipy", "mne").
Returns
True if the import succeeds.

Definition at line 79 of file python_test_helper.cpp.

◆ isAvailable()

bool PythonTestHelper::isAvailable ( ) const

Check if Python is available and MNE-Python is importable.

Returns
True if both python3 and the 'mne' package are available.

Definition at line 65 of file python_test_helper.cpp.

◆ isPythonAvailable()

bool PythonTestHelper::isPythonAvailable ( ) const

Check if Python is available (without checking for mne package).

Returns
True if python3 is reachable.

Definition at line 72 of file python_test_helper.cpp.

◆ isPythonRequired()

bool PythonTestHelper::isPythonRequired ( )
static

Check whether the environment demands Python availability.

When MNE_REQUIRE_PYTHON=true is set, test guards should QFAIL instead of QSKIP, ensuring skips are never silent in CI.

Returns
True if MNE_REQUIRE_PYTHON environment variable is "true" (or "1").

Definition at line 229 of file python_test_helper.cpp.

◆ readMatrix()

MatrixXd PythonTestHelper::readMatrix ( const QString & filePath,
bool * ok = nullptr )
static

Read an Eigen matrix from a text file (space-separated values, one row per line). Compatible with numpy.savetxt() output.

Parameters
[in]filePathInput file path.
[out]okSet to true on success, false on failure.
Returns
Parsed MatrixXd, or empty matrix on failure.

Definition at line 262 of file python_test_helper.cpp.

◆ runScript()

PythonRunnerResult PythonTestHelper::runScript ( const QString & scriptPath,
const QStringList & args = {},
int timeoutMs = 120000 ) const

Run a Python script file and return the result.

Parameters
[in]scriptPathPath to the .py file.
[in]argsArguments forwarded to the script.
[in]timeoutMsTimeout in milliseconds.
Returns
PythonRunnerResult.

Definition at line 212 of file python_test_helper.cpp.

◆ testDataPath()

QString PythonTestHelper::testDataPath ( )
static

Get the standard path to mne-cpp-test-data from the test binary location.

Returns
Absolute path to the test data directory.

Definition at line 222 of file python_test_helper.cpp.

◆ writeMatrix()

bool PythonTestHelper::writeMatrix ( const QString & filePath,
const Eigen::MatrixXd & mat )
static

Write an Eigen matrix to a text file with full double precision. The format is one row per line, space-separated values, using %.17e notation — compatible with Python's numpy.loadtxt().

Parameters
[in]filePathOutput file path.
[in]matMatrix to write.
Returns
True on success.

Definition at line 237 of file python_test_helper.cpp.


The documentation for this class was generated from the following files: