Machine Learning with g.Pype

Training happens offline, on a recording; deployment happens live, on every epoch or frame. g.Pype has the first half built in for scikit-learn, and leaves the second half open to any library through a node of your own.

What is built in

  • Epochs cuts one epoch per marker of a condition in a batch run, stacked into (time, channel, trial).

  • Standardize learns each channel’s mean and standard deviation from a whole recording and applies them, unchanged, to every live frame.

  • Sklearn fits one of four scikit-learn estimators offline and runs it live: LinearDiscriminantAnalysis and LogisticRegression as classifiers, StandardScaler and PCA as transformers. A classifier takes one epoch, flattened, and emits the index of the predicted class; a transformer takes plain rows.

  • Pipeline.fit() drives a batch run in which every such node fits on the whole recording first, then applies the result; the fitted state is saved with the serialized pipeline and loaded with it.

Season 4, Episode 10 cuts epochs, checks separability with cross-validation, fits with shrinkage LDA and deploys through step(); Offline Processing explains the batch run underneath.

Why an allow-list

A serialized pipeline is rebuilt wherever it is read, on a server a reader may not control, and a fitted estimator travels inside it. Only an estimator that can be rebuilt from its class name and plain parameters, and whose behaviour is known, is accepted; anything else is refused at construction, naming the four. The fitted state holds the estimator’s parameters as plain JSON, never code.

Any library, offline

A Result hands a recording to any library as a NumPy array, and an epochs block becomes a feature matrix with one transpose:

block = result.data                                   # (time, channel, trial)
x = block.transpose(2, 0, 1).reshape(block.shape[2], -1)   # (trial, features)
y = [label for _, label in result.trials]

From there scikit-learn, PyTorch, TensorFlow or anything else trains as it would on any array. EEG Data in Python lists the ways to the array, and the MNE workflow the way into MNE’s decoding tools.

A model outside the list, live

A model the allow-list does not cover runs live as a node of your own: load it once in setup(), infer in step() on each epoch or frame, return the decision. The frame budget applies, a frame of 4 samples at 250 Hz is 16 ms, so a model that needs longer runs downstream of a Trigger, once per epoch, or on a decimated stream. Measure with p.get_load(): a node’s worst_cycle_ms near the frame length is already late. Load the model and warm it up in setup(), not in the first step(), so the first epoch pays nothing extra. A node holding a model object cannot be serialized, so ship it as a module the pipeline’s allow-list permits, as Season 8, Episode 2 shows. Season 7 is the way to write the node. No PyTorch example ships with g.Pype.