Continuous Target Decoding with SPoC#

Source Power Comodulation (SPoC) [1] allows to identify the composition of orthogonal spatial filters that maximally correlate with a continuous target.

SPoC can be seen as an extension of the CSP for continuous variables.

Here, SPoC is applied to decode the (continuous) fluctuation of an electromyogram from MEG beta activity using data from Cortico-Muscular Coherence example of FieldTrip

# Author: Alexandre Barachant <alexandre.barachant@gmail.com>
#         Jean-Rémi King <jeanremi.king@gmail.com>
#
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import matplotlib.pyplot as plt
from sklearn.linear_model import Ridge
from sklearn.model_selection import KFold, cross_val_predict
from sklearn.pipeline import make_pipeline

import mne
from mne import Epochs
from mne.datasets.fieldtrip_cmc import data_path
from mne.decoding import SPoC

# Define parameters
fname = data_path() / "SubjectCMC.ds"
raw = mne.io.read_raw_ctf(fname)
raw.crop(50.0, 200.0)  # crop for memory purposes

# Filter muscular activity to only keep high frequencies
emg = raw.copy().pick(["EMGlft"]).load_data()
emg.filter(20.0, None)

# Filter MEG data to focus on beta band
raw.pick(picks=["meg", "ref_meg"]).load_data()
raw.filter(15.0, 30.0)

# Build epochs as sliding windows over the continuous raw file
events = mne.make_fixed_length_events(raw, id=1, duration=0.75)

# Epoch length is 1.5 second
meg_epochs = Epochs(raw, events, tmin=0.0, tmax=1.5, baseline=None, detrend=1, decim=12)
emg_epochs = Epochs(emg, events, tmin=0.0, tmax=1.5, baseline=None)

# Prepare classification
X = meg_epochs.get_data()
y = emg_epochs.get_data().var(axis=2)[:, 0]  # target is EMG power

# Classification pipeline with SPoC spatial filtering and Ridge Regression
spoc = SPoC(n_components=2, log=True, reg="oas", rank="full")
clf = make_pipeline(spoc, Ridge())
# Define a two fold cross-validation
cv = KFold(n_splits=2, shuffle=False)

# Run cross validation
y_preds = cross_val_predict(clf, X, y, cv=cv)

# Plot the True EMG power and the EMG power predicted from MEG data
fig, ax = plt.subplots(1, 1, figsize=[10, 4], layout="constrained")
times = raw.times[meg_epochs.events[:, 0] - raw.first_samp]
ax.plot(times, y_preds, color="b", label="Predicted EMG")
ax.plot(times, y, color="r", label="True EMG")
ax.set_xlabel("Time (s)")
ax.set_ylabel("EMG Power")
ax.set_title("SPoC MEG Predictions")
plt.legend()
plt.show()
SPoC MEG Predictions
ds directory : /home/circleci/mne_data/MNE-fieldtrip_cmc-data/SubjectCMC.ds
    res4 data read.
    hc data read.
    Separate EEG position data file not present.
    Quaternion matching (desired vs. transformed):
       0.33   78.32    0.00 mm <->    0.33   78.32    0.00 mm (orig :  -71.62   40.46 -256.48 mm) diff =    0.000 mm
      -0.33  -78.32   -0.00 mm <->   -0.33  -78.32   -0.00 mm (orig :   39.27  -70.16 -258.60 mm) diff =    0.000 mm
     114.65    0.00   -0.00 mm <->  114.65    0.00   -0.00 mm (orig :   64.35   66.64 -262.01 mm) diff =    0.000 mm
    Coordinate transformations established.
    Polhemus data for 3 HPI coils added
    Device coordinate locations for 3 HPI coils added
Picked positions of 4 EEG channels from channel info
    4 EEG locations added to Polhemus data.
    Measurement info composed.
Finding samples for /home/circleci/mne_data/MNE-fieldtrip_cmc-data/SubjectCMC.ds/SubjectCMC.meg4:
    System clock channel is available, checking which samples are valid.
    75 x 12000 = 911610 samples from 191 chs
    390 samples omitted at the end
Current compensation grade : 0
Reading 0 ... 180000  =      0.000 ...   150.000 secs...
Filtering raw data in 1 contiguous segment
Setting up high-pass filter at 20 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal highpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 20.00
- Lower transition bandwidth: 5.00 Hz (-6 dB cutoff frequency: 17.50 Hz)
- Filter length: 793 samples (0.661 s)

Reading 0 ... 180000  =      0.000 ...   150.000 secs...
Filtering raw data in 1 contiguous segment
Setting up band-pass filter from 15 - 30 Hz

FIR filter parameters
---------------------
Designing a one-pass, zero-phase, non-causal bandpass filter:
- Windowed time-domain design (firwin) method
- Hamming window with 0.0194 passband ripple and 53 dB stopband attenuation
- Lower passband edge: 15.00
- Lower transition bandwidth: 3.75 Hz (-6 dB cutoff frequency: 13.12 Hz)
- Upper passband edge: 30.00 Hz
- Upper transition bandwidth: 7.50 Hz (-6 dB cutoff frequency: 33.75 Hz)
- Filter length: 1057 samples (0.881 s)

[Parallel(n_jobs=1)]: Done  17 tasks      | elapsed:    0.1s
[Parallel(n_jobs=1)]: Done  71 tasks      | elapsed:    0.6s
[Parallel(n_jobs=1)]: Done 161 tasks      | elapsed:    1.3s
Not setting metadata
200 matching events found
No baseline correction applied
0 projection items activated
Not setting metadata
200 matching events found
No baseline correction applied
0 projection items activated
Using data from preloaded Raw for 200 events and 1801 original time points (prior to decimation) ...
1 bad epochs dropped
Using data from preloaded Raw for 200 events and 1801 original time points ...
1 bad epochs dropped
Reducing data rank from 183 -> 183
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Reducing data rank from 183 -> 183
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.

Plot the contributions to the detected components (i.e., the forward model)

CSP0, CSP1, AU
Reducing data rank from 183 -> 183
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.
Estimating covariance using OAS
Done.

References#

Total running time of the script: (0 minutes 22.330 seconds)

Gallery generated by Sphinx-Gallery