decoding source space LDA patterns

Hello,

I am learning how to decode brain activity in source space, based on this tutorial Decoding source space data — MNE 0.24.0 documentation .

I can compute patterns on my data when using Logistic Regression model, as in the example. I would like however to compute patterns from an LDA model. I see that MNE creates the patterns_ attribute only for logistic regressions mne-python/base.py at main · mne-tools/mne-python · GitHub. Are you are going to implement this feature also for LDA in future versions of MNE? Or is there a particular reason why this is available for Logisitc Regression and not for LDA?

I decided to give it a try and calculate the patterns myself, by using the coef_ covariance_ attributes stored in sklearn.discriminant_analysis.LinearDiscriminantAnalysis:

coef = get_coef(time_decod, ‘coef_’, inverse_transform=True)
cov = get_coef(time_decod, ‘covariance_’, inverse_transform=True)

for i in range(timepoints):
patterns.append(cov[:,:,i].dot(coef[:,i]))

patterns = np.array(patterns).T

This runs okay, but when I then plot the resulting patterns, they do not look nice and smooth as in the logistic regressions, but are “spiky” and do not make much sense. So there is definitely something I am doing wrong. Could you help me with this?

Thanks,

Federica

are you sure it does not work with LDA?

if the model has a .coef_ attribute after fit it should work.

Alex

Hi Alex, thanks a lot for your reply.

I had the feeling that the reason why get_coef was not working for in LDA (neither filters_ nor patterns_, while I could get attributes such as coef_ and covariance_) is that logistic regression is part of sklearn.linear_model, while LDA is part of sklearn.discriminant_analysis and somehow MNE was including just the first when transforming coef_. However I might be completely wrong.

Just for clarity:

clf = make_pipeline(StandardScaler(),
SelectKBest(f_classif, k=‘all’),
LinearDiscriminantAnalysis(solver=“svd”,
store_covariance=True))

time_decod = SlidingEstimator(clf, scoring=‘roc_auc’)

time_decod.fit(X, y)

####### this works
coef = get_coef(time_decod, ‘coef_’, inverse_transform=True)

####### these don’t
patterns = get_coef(time_decod, ‘patterns_’, inverse_transform=True)
filters = get_coef(time_decod, ‘filters_’, inverse_transform=True)

####### ValueError: This estimator does not have a filters_(/patterns_) attribute: LinearDiscriminantAnalysis(store_covariance=True)

Thanks,

Federica

to have filters and patterns attributes you need to wrap your scikit-learn model
with LinearModel

see the:

csp = CSP(n_components=3, norm_trace=False)
clf_csp = make_pipeline(csp, LinearModel(LogisticRegression(solver=‘lbfgs’)))
scores = cross_val_multiscore(clf_csp, X, y, cv=5, n_jobs=1)
print(‘CSP: %0.1f%%’ % (100 * scores.mean(),))

in

https://mne.tools/stable/auto_tutorials/machine-learning/50_decoding.html#source-power-comodulation-spoc

Alex

in

1 Like