Some questions ablout Python mne-csp plot_patterns/filters on eeg recordings

If you have a question or issue with MNE-Python, please include the following info:

  • MNE-Python version: 0.20.7
  • operating system: windows 10

i have some questions about the spatial patterns drawn by csp.plot_pattern/filters. Here are my results:


why its number was too high in the colorbar? it seemed abnormal. I only used a 4-order butterworth filter befor.Here are part of my codes. Look forward to your reply sincerely.

# defination of bandpass filter 

def butter_bandpass(lowcut,highcut,fs,order):
    nyq=0.5*fs#采样频率:奈奎斯特抽样定理指若频带宽度有限的,要从抽样信号中无失真地恢复原信号,抽样频率应大于2倍信号最高频率(脑电最高频率)
    low=lowcut/nyq
    high=highcut/nyq
    b,a=signal.butter(4,[low,high],'bandpass')#阶数为4
    return b,a
def butter_bandpass_filter(data,lowcut,highcut,fs,order):
    b,a=butter_bandpass(lowcut,highcut,fs,order)
    #y=signal.filtfilt(b,a,data,axis=2)
    y=signal.filtfilt(b,a,data)
    return y

raw_fname1='E:\\实验数据\\2020.1.6上午before\\03.fif'
raw=mne.io.read_raw_fif(raw_fname1)
raw.load_data()
#raw_temp=raw.copy()
raw.drop_channels({'TP10', 'ECG', 'TP9'})
print(raw.info)
<Info | 10 non-empty values
 bads: []
 ch_names: C3, C4, CP1, CP2, CP5, CP6, Cz, F3, F4, F7, F8, FC1, FC2, FC5, ...
 chs: 29 EEG
 custom_ref_applied: False
 dig: 31 items (31 EEG)
 file_id: 4 items (dict)
 highpass: 0.0 Hz
 lowpass: 500.0 Hz
 meas_date: 2020-01-06 10:25:47 UTC
 meas_id: 4 items (dict)
 nchan: 29
 projs: []
 sfreq: 1000.0 Hz
>

csp=CSP(n_components=6, reg=None, log=True, norm_trace=False)

from mne.decoding import CSP

#acquire and combine features of different fequency bands
features_train=[]
#features_test=[]
#freq=[4,8,14,20]
#freq=[0.5,4]
#freq=[4,8]
#freq=[8,14]
#freq=[14,20]
freq=[4,20]
#freq=[4,8,12,16,20,24,28,32,36,40]
#freq=[4,8,12,16,20,24,28]
#freq=[2,3,4,5,6,7,8]#p300主要在2-8hz
#freq=[2,4,6,8]
#freq=[2,5,8];

for freq_count in range(len(freq)):
#loop for freqency
    lower=freq[freq_count]
    if lower==freq[-1]:#到最后一个时停止
        break
    higher=freq[freq_count+1]
    #print(lower,higher)
    X_train_filt=butter_bandpass_filter(X_train,lowcut=lower,highcut=higher,fs=250,order=4)
    print(X_train_filt.shape)

    tmp_train=csp.fit_transform(X_train_filt,y_train)
    vmax=np.argmax(tmp_train)
    vmin=np.argmin(tmp_train)
    csp.plot_patterns(raw.info, ch_type='eeg',units=None, size=1.5)
    csp.plot_filters(raw.info, ch_type='eeg', units='Patterns (AU)', size=1.5)
    if freq_count==0:
        features_train=X_train_filt 
    else:
        features_train=np.concatenate((features_train,X_train_filt),axis=1)
       
np.set_printoptions(threshold=np.inf) 
print(features_train.shape)

What is the range of your raw data? Also, note that the MNE version you’re using had a bug regarding plotting CSP filters - please update to 0.22 where this should be fixed.

1 Like

thank you for your reply, i would update it and see it again :smiley:

 tmp_train=csp.fit_transform(X_train_filt,y_train)
    #tmp_train=csp.fit_transform(X_train,y_train)
    #print(tmp_train.shape)
    vmax_pos = np.unravel_index(np.argmax(tmp_train),tmp_train.shape)
    vmin_pos = np.unravel_index(np.argmin(tmp_train),tmp_train.shape)
    vmax=tmp_train[vmax_pos];
    vmin=tmp_train[vmin_pos];
    #v_mean=np.mean(tmp_train) 
    print(vmax,vmin)
results:
![image|350x29](upload://f9vO1pnPrvr8J6qUWrHWYdv9i5Y.png) 

i updated the toolkit,and printed the max/min value of my data.however,the result was also a little bit confusing.I thought maybe it recognized the input data in “Volt” unit defaulty?
and think about that, i have multipied my input data with “0.000001”

tmp_train=csp.fit_transform(X_train_filt*0.000001,y_train)

and the results of the 'plot_patterns’seemed a bit normal, but the filters seemed much stranger. You can see them as follows. Should it be transformed by 10^6?

csp=CSP(n_components=6, reg=None, log=True, norm_trace=False)

Now I have some questions about:
(1)what does the axis on the color bar mean? Is it a power pattern? And also whether it’s right or not to enlarge the data by 10^6?
(2)what are the components represented for? I have read the papers related roughly, but I’m not sure whether the fist one of the compoments (like csp0) and the last one of the compoments (like csp5) (if there were only 6 of ‘n_components’ was chosen )represented the one class(like the left hand) and the other class(like the right hand) respectively in a two-classification task?
I sincerely hope for your reply.

the orignal max and min values are as followings:
1.8701044029396205 -2.580324175783792

and i have saw other instances on kaggle like this:

 # plot first pattern
    pattern = csp.patterns_[0,:]
    pattern -= pattern.mean()
    ix = np.argmax(abs(pattern))
    # the parttern is sign invariant.
    # invert it for display purpose
    if pattern[ix]>0:
        sign = 1.0
    else:
        sign = -1.0

should i also reproduce it for my patterns’ show?
Thank you