Sklearn

class gpype.backend.transform.sklearn.Sklearn[source]

Bases: Fittable, IONode

Fits one allow-listed scikit-learn estimator, offline and online.

The allow-list is fixed: LinearDiscriminantAnalysis and LogisticRegression (both from sklearn) as classifiers, StandardScaler and PCA as transformers. Anything else is refused at construction, naming the four. Construction checks the name and imports nothing; the estimator is built where the node is set up or fitted.

A classifier fits on the (time, channel, trial) block a batch run downstream of Epochs delivers, with y taken from Constants.Keys.TRIALS – one flattened row per trial. Its output is one column: the index of the predicted class in estimator.classes_ (kept in the fitted state for reference, as a plain list of strings when the labels are not numeric – never as an array, since an artifact carries no text arrays). Deployed online, it takes one 2-D frame – the trained epoch length, flattened the same way – and emits one prediction per frame.

A transformer fits and applies on plain 2-D rows (samples x channels), in both batch and realtime. Its output channels, and a classifier’s, declare no unit: neither is in the input’s.

Parameters:
  • estimator – An unfitted instance of one of the four allow-listed classes – the ordinary, author-time form, whose own get_params() becomes part of the fitted state, and is also written into this node’s own configuration under params – so an unfitted node’s hyperparameters still survive a document, not only a fitted one’s. Or the class’s bare name as a string, which is what a document supplies when this node is rebuilt: no live estimator object can cross that boundary, so a document-rebuilt node builds an instance of the named class from params (or a bare default where none was recorded), or the one load_state() was given, when it is set up.

  • **kwargs – Additional arguments for the parent IONode.

Raises:
  • ValueError – If estimator names or is an instance of a class outside the allow-list, or if one of its hyperparameters is not a plain JSON value (str, int, float, bool, None, or a list/dict of those) – no object may enter a document.

  • ImportError – From setup() or a fit, not construction, if scikit-learn is not installed.

class Configuration[source]

Bases: Configuration

Configuration class for Sklearn parameters.

class Keys[source]

Bases: Keys

Configuration keys for Sklearn settings.

ESTIMATOR = 'estimator'

The allow-listed estimator class’s bare name.

class OptionalKeys[source]

Bases: OptionalKeys

Optional configuration keys for Sklearn settings.

PARAMS = 'params'

The unfitted estimator’s own get_params() – absent only where none was ever given (a document written before this key existed).

__init__(estimator, **kwargs)[source]

Initialize the IONode with input and output port configurations.

Parameters:
  • input_ports – List of input port configurations or None.

  • output_ports – List of output port configurations or None.

  • **kwargs – Additional arguments passed to parent classes.

setup(data, port_context_in)[source]

Declare this node’s output shape.

A classifier’s output is one column – the predicted class’s index – regardless of how many channels it was trained on. A transformer’s output keeps its input’s width, except PCA, which narrows it to the number of components it was fitted with.

Parameters:
Return type:

dict[str, dict]

Returns:

Output port contexts.

fit(block, context)[source]

Fit the estimator, as a classifier or as a transformer.

Parameters:
  • block (ndarray) – (time, channel, trial) downstream of Epochs, for a classifier; (samples, channels) for a transformer.

  • context (dict) – For a classifier, must carry Constants.Keys.TRIALS.

Return type:

dict

Returns:

The estimator’s get_params() under "params", plus its fitted array attributes.

Raises:

ValueError – If a classifier’s input is not 3-D, or the context carries no trials; if a transformer’s input is not 2-D.

load_state(state)[source]

Adopt a state; the estimator is rebuilt from it on first use.

Deferred because a document’s artifacts are loaded wherever the document is read (see _built()).

Parameters:

state (dict) – As _estimator_state() returned it, fresh or decoded from a document.

Return type:

None

apply_block(block)[source]

Predict or transform the whole block, for a batch run.

Parameters:

block (ndarray) – The complete input, the same shape fit() took.

Return type:

ndarray

Returns:

One predicted class index per trial (a classifier), or the transformed rows.

step(data)[source]

Predict or transform one realtime frame.

A classifier’s frame is one epoch – the trained epoch length, flattened the same way training flattened each trial – and this emits its single prediction.

Parameters:

data (dict[str, ndarray]) – Input frame.

Returns:

the predicted class’s index (a classifier), or the transformed frame.

Return type:

dict[str, ndarray]