braindecode.modules.ChannelLayer#

class braindecode.modules.ChannelLayer(target, strategy, chs_info=None, *, drop_non_eeg=False, trainable=False, reference=None, **kwargs)[source]#

Map any montage onto target with one matrix per montage.

Warning

Experimental. Public API may change without a deprecation cycle.

Parameters:
  • target (list of dict) – Channels the backbone consumes (ch_name, optional loc).

  • strategy (str or callable) – "exact" (a missing target is an error), "zero", "nearest", "idw" (p), "spline" (reg=1e-3), "field" (reg=0) or "source" (n_parcels=64, lam=0.1, grid_mm=15, trainable: a zero-initialised parcel mixing on top of the physics). "wiener" (noise=0.01; call fit() first), "region" (mean within radius, default 1.5 x the median input spacing) or "latent" (d_model=64, n_freqs=8: learned cross-attention from each missing target’s position over the inputs’ positions and statistics). A callable kernel(pos_a, pos_b) -> (n_a, n_b) covariance of the potentials at two sets of positions (metres, from the centre of the sphere fitted to the inputs, as "spline"), e.g. spherical_spline_kernel(), fills them with linear_channel_map() (reg: the noise variance, default 1e-3; the recording reference is left unknown). spline and wiener are that estimate with their own covariance; match_gain=True gives each reconstructed target its prior variance.

  • chs_info (list of dict, optional) – Montage used when forward() gets none.

  • drop_non_eeg (bool) – Ignore input channels that are not electrodes instead of raising.

  • reference (str, optional) – Reference of the backbone’s single-electrode targets: None (the input’s), an electrode name ("Cz": target A is V(A) - V(Cz), with Cz copied or reconstructed like any target) or "average" (minus their mean). Bipolar targets need none.

variance#

Posterior variance of each target for the current montage: 0 when measured or exact, from the estimate (before match_gain) for a single-electrode target of spline, wiener or a kernel, nan when not computed (other strategies, or a target mixing several reconstructed channels, as with reference="average").

Type:

torch.Tensor

Notes

forward also takes one montage per sample (chs_info a list of montages, x padded to the largest and ch_mask the real channels), for the strategies without trained state (not latent or trainable); observed is then (batch, n_targets) and the buffers (weight, variance) are those of the last sample. A model with a channel_strategy builds these montages from the pos and ch_mask of pad_channels_collate(). A function strategy is not saved by save_pretrained: pass it again when loading.

Methods

fit(X, chs_info_dense)[source]#

wiener: fit the spatial covariance on X (n_samples, n_dense) recorded with chs_info_dense (matched to the montages by position).

Return type:

ChannelLayer

forward(x, chs_info=None, ch_mask=None)[source]#

Map x (B, C, T) recorded with chs_info, or with one montage per sample whose channels are the True entries of ch_mask (B, C) (default: the first ones, as pad_channels_collate() pads); return (x, observed).

Return type:

tuple[Tensor, Tensor]

Examples using braindecode.modules.ChannelLayer#

Running a Pretrained Model on Any Channel Set with the Channel Layer

Running a Pretrained Model on Any Channel Set with the Channel Layer