<commit_before>
"""
This example shows how to decompose EEG signals into source activations with MVARICA, and subsequently extract single-trial connectivity as features for LDA.
"""
import numpy as np

import scot
import scot.backend.sklearn     # use scikit-learn backend
import scot.xvschema

from sklearn.lda import LDA

import matplotlib.pyplot as plt

"""
The example data set contains a continuous 45 channel EEG recording of a motor
imagery experiment. The data was preprocessed to reduce eye movement artifacts
and resampled to a sampling rate of 100 Hz.
With a visual cue the subject was instructed to perform either hand of foot
motor imagery. The the trigger time points of the cues are stored in 'tr', and
'cl' contains the class labels (hand: 1, foot: -1). Duration of the motor 
imagery period was approximately 6 seconds.
"""

from motorimagery import data as midata

raweeg = midata.eeg
triggers = midata.triggers
classes = midata.classes
fs = midata.samplerate
locs = midata.locations

"""
Set up the analysis object

We simply choose a VAR model order of 30, and reduction to 4 components.
"""
ws = scot.Workspace(30, reducedim=4, fs=fs)


"""
Prepare the data

Here we cut segments from 3s to 4s following each trigger out of the EEG. This
is right in the middle of the motor imagery period.
"""
data = scot.datatools.cut_segments(raweeg, triggers, 3*fs, 4*fs)

"""
Perform MVARICA
"""
ws.setData(data)
ws.doMVARICA()

"""
Find optimal regularization parameter for single-trial fitting
"""
ws.optimizeRegularization(scot.xvschema.singletrial, 30)

freq = np.linspace(0,fs,ws.nfft_)

"""
Single-Trial Fitting and feature extraction
"""
features = np.zeros((len(triggers), 32))
for t in range(len(triggers)):
    print('Trial: %d   '%t, end='\r')
    ws.setData(data[:,:,t])
    ws.fitVAR()

    con = ws.getConnectivity('ffPDC')
    
    alpha = np.mean(con[:,:,np.logical_and(7<freq, freq<13)], axis=2)
    beta = np.mean(con[:,:,np.logical_and(15<freq, freq<25)], axis=2)
    
    features[t,:] = np.array([alpha, beta]).flatten()
print('')
    
lda = LDA( )
lda.fit(features, classes)

llh = lda.transform(features)
    <commit_msg>Changed Feature example to use CSPVARICA (it rocks!)<commit_after>
"""
This example shows how to decompose EEG signals into source activations with MVARICA, and subsequently extract single-trial connectivity as features for LDA.
"""
import numpy as np

import scot
import scot.backend.sklearn     # use scikit-learn backend
import scot.xvschema

from sklearn.lda import LDA

import matplotlib.pyplot as plt

"""
The example data set contains a continuous 45 channel EEG recording of a motor
imagery experiment. The data was preprocessed to reduce eye movement artifacts
and resampled to a sampling rate of 100 Hz.
With a visual cue the subject was instructed to perform either hand of foot
motor imagery. The the trigger time points of the cues are stored in 'tr', and
'cl' contains the class labels (hand: 1, foot: -1). Duration of the motor 
imagery period was approximately 6 seconds.
"""

from motorimagery import data as midata

raweeg = midata.eeg
triggers = midata.triggers
classes = midata.classes
fs = midata.samplerate
locs = midata.locations

"""
Set up the analysis object

We simply choose a VAR model order of 30, and reduction to 4 components.
"""
ws = scot.Workspace(30, reducedim=4, fs=fs)


"""
Prepare the data

Here we cut segments from 3s to 4s following each trigger out of the EEG. This
is right in the middle of the motor imagery period.
"""
data = scot.datatools.cut_segments(raweeg, triggers, 3*fs, 4*fs)

"""
Perform MVARICA
"""
ws.setData(data, classes)
ws.doCSPVARICA()

"""
Find optimal regularization parameter for single-trial fitting
"""
ws.optimizeRegularization(scot.xvschema.singletrial, 30)

freq = np.linspace(0,fs,ws.nfft_)

"""
Single-Trial Fitting and feature extraction
"""
features = np.zeros((len(triggers), 32))
for t in range(len(triggers)):
    print('Trial: %d   '%t, end='\r')
    ws.setData(data[:,:,t])
    ws.fitVAR()

    con = ws.getConnectivity('ffPDC')
    
    alpha = np.mean(con[:,:,np.logical_and(7<freq, freq<13)], axis=2)
    beta = np.mean(con[:,:,np.logical_and(15<freq, freq<25)], axis=2)
    
    features[t,:] = np.array([alpha, beta]).flatten()
print('')
    
lda = LDA( )
lda.fit(features, classes)

llh = lda.transform(features)
    
{'fraction_non_alphanumeric': 0.1078971533516988, 'fraction_numerical': 0.014692378328741965, 'mean_word_length': 4.372379778051788, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '101956', 'n_tokens_mistral': 1515, 'n_tokens_neox': 1407, 'n_words': 512}