Equation

class gpype.backend.transform.equation.Equation[source]

Bases: IONode

Mathematical expression evaluation node for data transformation.

Applies custom mathematical expressions to input data using SymPy. Automatically creates input ports from expression variables and compiles to optimized NumPy functions. Handles ‘in’ keyword via internal aliasing.

The output records no unit unless unit names one: an expression can square, divide or mix its inputs, and nothing here can tell which.

INPUT_PORTS_FROM = 'expression'

The input ports are the free variables of the expression, so there is no port list to declare: a+b has two inputs and eeg1*2 - eeg2 has two differently named ones. Naming the parameter that decides them is the whole truth. Publishing one expression’s answer as the class’s port set – which is what building an instance to find out did – is correct for exactly one expression out of infinitely many.

class Configuration[source]

Bases: Configuration

Configuration class for Equation parameters.

class Keys[source]

Bases: Keys

Configuration key constants for the Equation.

EXPRESSION = 'expression'

Configuration key for mathematical expression string

class OptionalKeys[source]

Bases: OptionalKeys

Optional configuration keys.

UNIT = 'unit'

Physical unit of every output channel

__init__(expression, unit=None, **kwargs)[source]

Initialize Equation node with mathematical expression.

Parses expression using SymPy, extracts variables to create input ports, and compiles to optimized NumPy function.

Parameters:
  • expression (str) – Mathematical expression string. Must be valid SymPy expression. Variables become input port names. ‘in’ keyword handled via internal aliasing.

  • unit (Optional[str]) – Physical unit of every output channel, e.g. "uV". None records no unit.

  • **kwargs – Additional configuration parameters for IONode.

Raises:
  • ValueError – If expression is None or empty, or unit is not a non-empty string.

  • SymPy parsing errors – If expression cannot be parsed.

setup(data, port_context_in)[source]

Setup Equation node and determine output dimensionality.

Creates pseudo input data based on input context, runs the computation to determine output shape, and builds output context with correct channel count. This handles dimensionality changes from matrix operations.

Parameters:
  • data (dict[str, ndarray]) – Initial data dictionary for port configuration.

  • port_context_in (dict[str, dict]) – Input port context with channel counts, sampling rates, and frame sizes.

Return type:

dict[str, dict]

Returns:

Output port context with validated configuration and computed channel count based on expression output shape.

step(data)[source]

Apply mathematical expression to input data.

Evaluates compiled function on current frame of input data in the order of sorted variable names from expression.

Parameters:

data (dict[str, ndarray]) – Dictionary with input data arrays for each expression variable. Keys are variable names, values are NumPy arrays.

Return type:

dict[str, ndarray]

Returns:

Dictionary with expression evaluation result on output port.