Dear MNE experts:
I have one issue while computing an XDAWN. I have tried XDAWN on ERP datasets. I thought the problem is due to gbk encoding. But I can not add it in the fname = os.path.join(eeg_path,‘010101_1.set’).
May I ask how to solve this ‘gbk’ problem?
Best wishes
YuTong`
type or paste code here
import mne
import os
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import StratifiedKFold
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report, confusion_matrix
from sklearn.preprocessing import MinMaxScaler
from mne import io, pick_types, read_events, Epochs, EvokedArray, create_info
from mne.preprocessing import Xdawn
from mne.decoding import Vectorizer
print(__doc__)
data_path = data_path = 'C:\WM_DATA\DATA\DATA'
eeg_path = data_path
fname = os.path.join(eeg_path,'010101_1.set')
tmin, tmax = -0.1, 0.3
event_id = {'B9(size4_left_noc)/71': 46, 'B5(size2_left_noc)/61': 55, 'B3(size1_right_noc)/53': 56, 'B2(size1_left_c)/52': 59, 'B1(size1_left_noc)/51': 59, 'B8(size2_right_c)/64': 48, 'B4(size1_right_c)/54': 56, 'B6(size2_left_c)/62': 56, 'B12(size4_right_c)/74': 19, 'B10(size4_left_c)/72': 49}
n_filter = 3
# Setup for reading the raw data
raw = mne.io.read_epochs_eeglab(fname)
raw.filter(1, 20, fir_design='firwin')
events = read_events(fname)
picks = pick_types(raw.info, meg=False, eeg=True, stim=False, eog=False,
exclude='bads')
epochs = Epochs(raw, events, event_id, tmin, tmax, proj=False,
picks=picks, baseline=None, preload=True,
verbose=False)
# Create classification pipeline
clf = make_pipeline(Xdawn(n_components=n_filter),
Vectorizer(),
MinMaxScaler(),
LogisticRegression(penalty='l1', solver='liblinear',
multi_class='auto'))
# Get the labels
labels = epochs.events[:, -1]
# Cross validator
cv = StratifiedKFold(n_splits=10, shuffle=True, random_state=42)
# Do cross-validation
preds = np.empty(len(labels))
for train, test in cv.split(epochs, labels):
clf.fit(epochs[train], labels[train])
preds[test] = clf.predict(epochs[test])
# Classification report
target_names = ['aud_l', 'aud_r', 'vis_l', 'vis_r']
report = classification_report(labels, preds, target_names=target_names)
print(report)
# Normalized confusion matrix
cm = confusion_matrix(labels, preds)
cm_normalized = cm.astype(float) / cm.sum(axis=1)[:, np.newaxis]
# Plot confusion matrix
fig, ax = plt.subplots(1)
im = ax.imshow(cm_normalized, interpolation='nearest', cmap=plt.cm.Blues)
ax.set(title='Normalized Confusion matrix')
fig.colorbar(im)
tick_marks = np.arange(len(target_names))
plt.xticks(tick_marks, target_names, rotation=45)
plt.yticks(tick_marks, target_names)
fig.tight_layout()
ax.set(ylabel='True label', xlabel='Predicted label')
Module created for script run in IPython
Extracting parameters from C:\WM_DATA\DATA\DATA\010101_1.set…
Not setting metadata
613 matching events found
c:\users\yyt.spyder-py3\xdawn.py:27: RuntimeWarning: At least one epoch has multiple events. Only the latency of the first event will be retained.
raw = mne.io.read_epochs_eeglab(fname)
C:\Users\yyt\mne-python\1.1.1_0\lib\site-packages\mne\io\eeglab\eeglab.py:149: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use array.size > 0
to check that an array is not empty.
if d.get(“type”, None) != ‘FID’:
C:\Users\yyt\mne-python\1.1.1_0\lib\site-packages\mne\io\eeglab\eeglab.py:149: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Use array.size > 0
to check that an array is not empty.
if d.get(“type”, None) != ‘FID’:
No baseline correction applied
0 projection items activated
Ready.
Setting up band-pass filter from 1 - 20 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: 1.00
- Lower transition bandwidth: 1.00 Hz (-6 dB cutoff frequency: 0.50 Hz)
- Upper passband edge: 20.00 Hz
- Upper transition bandwidth: 5.00 Hz (-6 dB cutoff frequency: 22.50 Hz)
- Filter length: 3301 samples (3.301 sec)
c:\users\yyt.spyder-py3\xdawn.py:28: RuntimeWarning: filter_length (3301) is longer than the signal (1400), distortion is likely. Reduce filter length or filter a longer signal.
raw.filter(1, 20, fir_design=‘firwin’)
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1 concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 3 out of 3 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 4 out of 4 | elapsed: 0.0s remaining: 0.0s
[Parallel(n_jobs=1)]: Done 17777 out of 17777 | elapsed: 7.4s finished
c:\users\yyt.spyder-py3\xdawn.py:29: RuntimeWarning: This filename (C:\WM_DATA\DATA\DATA\010101_1.set) does not conform to MNE naming conventions. All events files should end with .eve, -eve.fif, -eve.fif.gz, -eve.lst, -eve.txt, _eve.fif, _eve.fif.gz, _eve.lst, _eve.txt or -annot.fif
events = read_events(fname)
Traceback (most recent call last):
File “C:\Users\yyt\mne-python\1.1.1_0\lib\site-packages\spyder_kernels\py3compat.py”, line 356, in compat_exec
exec(code, globals, locals)
File “c:\users\yyt.spyder-py3\xdawn.py”, line 29, in
events = read_events(fname)
File “”, line 12, in read_events
File “C:\Users\yyt\mne-python\1.1.1_0\lib\site-packages\mne\event.py”, line 266, in read_events
lines = np.loadtxt(filename, dtype=np.float64).astype(int)
File “C:\Users\yyt\mne-python\1.1.1_0\lib\site-packages\numpy\lib\npyio.py”, line 1098, in loadtxt
first_line = next(fh)
UnicodeDecodeError: ‘gbk’ codec can’t decode byte 0xca in position 188: illegal multibyte sequence