<commit_before>"""
===========================================================================
Motor imagery decoding from EEG data using the Common Spatial Pattern (CSP)
===========================================================================

Decoding of motor imagery applied to EEG data decomposed using CSP.
Here the classifier is applied to features extracted on CSP filtered signals.

See http://en.wikipedia.org/wiki/Common_spatial_pattern and [1]

[1] <NAME>. The quantitative extraction and topographic mapping
    of the abnormal components in the clinical EEG. Electroencephalography
    and Clinical Neurophysiology, 79(6):440--447, December 1991.

"""
# Authors: <NAME> <<EMAIL>>
#
# License: BSD (3-clause)

print(__doc__)
import numpy as np

import mne
from mne import fiff
from mne.io.edf import read_raw_edf
from mne.datasets import eegbci
from mne.event import find_events
from mne.decoding import CSP
from mne.layouts import read_layout
import matplotlib.pyplot as plt


class LogCSP(CSP):
    """This class replaces the CSP's normalized bandpower features with
    log-bandpower features.

    Averaged band power is chi-square distributed. Taking the logarithm brings
    it closer to the normal distribution, which improves LDA classification.
    """
    def __init__(self, *args, **kwargs):
        CSP.__init__(self, *args, **kwargs)

    def transform(self, epochs_data, y=None):
        X = CSP.transform(self, epochs_data, y)
        # undo normalization
        X *= self.std_
        X += self.mean_
        # calculate log-bandpower features
        X = np.log(X)
        return X


###############################################################################
## Set parameters and read data

# avoid classification of evoked responses by using epochs that start 1s after
# cue onset.
tmin, tmax = 1, 2
event_id = dict(hands=2, feet=3)
subject = 1
#runs = [3, 7, 11]  # motor execution: left hand vs right hand
#runs = [4, 8, 12]  # motor imagery: left hand vs right hand
#runs = [5, 9, 13]  # motor execution: hands vs feet
runs = [6, 10, 14]  # motor imagery: hands vs feet

raw_fnames = eegbci.load_data(subject, runs)

# Read raw data from EDF files
raw = None
for fname in raw_fnames:
    raw0 = read_raw_edf(fname, tal_channel=-1, preload=True)
    try:
        raw.append(raw0)
    except AttributeError:
        raw = raw0

# Apply band-pass filter
# Hack: fake filter information so raw.filter won't throw an exception at us.
raw.info['lowpass'] = raw.info['sfreq'] * 0.5
raw.info['highpass'] = 0
raw.filter(7, 30, method='iir')

layout = read_layout('../../examples/decoding/EEG1005.lay')

# make sure channel names are in the same case
raw.info['ch_names'] = [chn.strip('.') for chn in raw.info['ch_names']]
#layout.names = [chn.upper() for chn in layout.names]

events = find_events(raw, output='onset', shortest_event=0,
                     stim_channel='STI 014')

#raw.info['bads'] = ['T8']  # set bad channels
picks = fiff.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
                        exclude='bads')

# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                    baseline=None, preload=True, add_eeg_ref=False)

# larger epochs for running classifier testing
epochs2 = mne.Epochs(raw, events, event_id, -1, 4, proj=True, picks=picks,
                     baseline=None, preload=True, add_eeg_ref=False)

labels = epochs.events[:, -1] - 2
evoked = epochs.average()


###############################################################################
# Classification with linear discrimant analysis

from sklearn.lda import LDA<commit_msg>more cosmetics<commit_after>"""
===========================================================================
Motor imagery decoding from EEG data using the Common Spatial Pattern (CSP)
===========================================================================

Decoding of motor imagery applied to EEG data decomposed using CSP.
Here the classifier is applied to features extracted on CSP filtered signals.

See http://en.wikipedia.org/wiki/Common_spatial_pattern and [1]

[1] <NAME>. The quantitative extraction and topographic mapping
    of the abnormal components in the clinical EEG. Electroencephalography
    and Clinical Neurophysiology, 79(6):440--447, December 1991.

"""
# Authors: <NAME> <<EMAIL>>
#
# License: BSD (3-clause)

print(__doc__)
import numpy as np

import mne
from mne import fiff
from mne.io import concatenate_raws
from mne.io.edf import read_raw_edf
from mne.datasets import eegbci
from mne.event import find_events
from mne.decoding import CSP
from mne.layouts import read_layout
import matplotlib.pyplot as plt


class LogCSP(CSP):
    """This class replaces the CSP's normalized bandpower features with
    log-bandpower features.

    Averaged band power is chi-square distributed. Taking the logarithm brings
    it closer to the normal distribution, which improves LDA classification.
    """
    def __init__(self, *args, **kwargs):
        CSP.__init__(self, *args, **kwargs)

    def transform(self, epochs_data, y=None):
        X = CSP.transform(self, epochs_data, y)
        # undo normalization
        X *= self.std_
        X += self.mean_
        # calculate log-bandpower features
        X = np.log(X)
        return X


###############################################################################
## Set parameters and read data

# avoid classification of evoked responses by using epochs that start 1s after
# cue onset.
tmin, tmax = 1, 2
event_id = dict(hands=2, feet=3)
subject = 1
#runs = [3, 7, 11]  # motor execution: left hand vs right hand
#runs = [4, 8, 12]  # motor imagery: left hand vs right hand
#runs = [5, 9, 13]  # motor execution: hands vs feet
runs = [6, 10, 14]  # motor imagery: hands vs feet

raw_fnames = eegbci.load_data(subject, runs)
raw_files = [read_raw_edf(f, tal_channel=-1, preload=True) for f in raw_fnames]
raw = concatenate_raws(raw_files)

# strip channel names
raw.info['ch_names'] = [chn.strip('.') for chn in raw.info['ch_names']]

# Apply band-pass filter
# Hack: fake filter information so raw.filter won't throw an exception at us.
raw.info['lowpass'] = raw.info['sfreq'] * 0.5
raw.info['highpass'] = 0
raw.filter(7, 30, method='iir')

layout = read_layout('../../examples/decoding/EEG1005.lay')

events = find_events(raw, output='onset', shortest_event=0,
                     stim_channel='STI 014')

picks = fiff.pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
                        exclude='bads')

# Read epochs
epochs = mne.Epochs(raw, events, event_id, tmin, tmax, proj=True, picks=picks,
                    baseline=None, preload=True, add_eeg_ref=False)

# larger epochs for running classifier testing
epochs2 = mne.Epochs(raw, events, event_id, -1, 4, proj=True, picks=picks,
                     baseline=None, preload=True, add_eeg_ref=False)

labels = epochs.events[:, -1] - 2
evoked = epochs.average()


###############################################################################
# Classification with linear discrimant analysis

from sklearn.lda import LDA
{'fraction_non_alphanumeric': 0.1822756499860218, 'fraction_numerical': 0.017053396701146212, 'mean_word_length': 4.067280453257791, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4812129', 'n_tokens_mistral': 2330, 'n_tokens_neox': 2116, 'n_words': 709}