Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Project CSP patterns to source #950

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
32 changes: 32 additions & 0 deletions mne_bids_pipeline/steps/sensor/_05_decoding_csp.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ def _fmt_contrast(cond1, cond2, fmin, fmax, freq_range_name, tmin=None, tmax=Non
epochs_filt, y = prepare_epochs_and_y(
epochs=epochs, contrast=contrast, fmin=fmin, fmax=fmax, cfg=cfg
)

# Get the data for all time points
X = epochs_filt.get_data()

Expand All @@ -253,6 +254,21 @@ def _fmt_contrast(cond1, cond2, fmin, fmax, freq_range_name, tmin=None, tmax=Non
freq_decoding_table.loc[idx, "mean_crossval_score"] = cv_scores.mean()
freq_decoding_table.at[idx, "scores"] = cv_scores

# PATTERNS
csp.fit_transform(X, y)
sensor_pattern_csp = csp.patterns_

# COEFS
clf.fit(X, y)
weights_csp = mne.decoding.get_coef(clf, "patterns_", inverse_transform=True)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically this is all I use. Why also take the csp.patterns_? csp.fit(X, y) results anywhere else so it's a bit weird to me to add them here. It seems like we should just use these weights_csp. And maybe we should call them clf_patterns_ because they're really the patterns inverse transformed from the CSP all the way back through the other steps (e.g., PCA, sensor scaling)?

Copy link
Collaborator Author

@SophieHerbst SophieHerbst May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is exactly where I am lost in the terminology..

my approach was to save the weights in this step to pick them up in a separate source projection step later

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to save both, because in @britta-wstnr's code they are combined during beamforming:

stc_csp = beamform_components(weights_csp, sensor_pattern_csp, spat_filter,
                                fwd, multipliers=multiplier)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea here is to combine the coefficients and the CSP components. The (Haufe et al.) patterns are not easily beamform-able as they do not represent classical sink-source patterns anymore but refer to the decoding classes. Using the CSP components is a work-around for that.


# save scores
# XXX right now this saves in working directory
csp_fname = cond1 + cond2 + str(fmin) + str(fmax)

np.save(csp_fname + "_patterns", sensor_pattern_csp)
np.save(csp_fname + "_weights", weights_csp)

# Loop over times x frequencies
#
# Note: We don't support varying time ranges for different frequency
Expand Down Expand Up @@ -306,6 +322,7 @@ def _fmt_contrast(cond1, cond2, fmin, fmax, freq_range_name, tmin=None, tmax=Non
# Crop data to the time window of interest
if tmax is not None: # avoid warnings about outside the interval
tmax = min(tmax, epochs_filt.times[-1])

X = epochs_filt.crop(tmin, tmax).get_data()
del epochs_filt
cv_scores = cross_val_score(
Expand All @@ -323,6 +340,21 @@ def _fmt_contrast(cond1, cond2, fmin, fmax, freq_range_name, tmin=None, tmax=Non
msg += f": {cfg.decoding_metric}={score:0.3f}"
logger.info(**gen_log_kwargs(msg))

# PATTERNS
csp.fit_transform(X, y)
sensor_pattern_csp = csp.patterns_

# COEFS
clf.fit(X, y)
weights_csp = mne.decoding.get_coef(clf, "patterns_", inverse_transform=True)

# save scores
# XXX right now this saves in working directory
csp_fname = cond1 + cond2 + str(fmin) + str(fmax) + str(tmin) + str(tmax)

np.save(csp_fname + "_patterns", sensor_pattern_csp)
np.save(csp_fname + "_weights", weights_csp)

# Write each DataFrame to a different Excel worksheet.
a_vs_b = f"{condition1}+{condition2}".replace(op.sep, "")
processing = f"{a_vs_b}+CSP+{cfg.decoding_metric}"
Expand Down
Loading