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

Improved functions to get leadfields and LCMV weights #374

Merged
merged 4 commits into from
Jan 10, 2025
Merged

Conversation

cgohil8
Copy link
Collaborator

@cgohil8 cgohil8 commented Jan 10, 2025

Changes:

  • Added a function to extract LCMV beamformer weights.
  • Simplified the function to get leadfields.
  • Improved docs for these functions.

Tested the beamforming projection with:

import mne
import numpy as np
from osl_ephys.source_recon import beamforming

subjects_dir = "data/raw/src"
subject = "sub-CC320608_task-rest"
preproc_file = "data/raw/preproc/mf2pt2_sub-CC320608_ses-rest_task-rest_meg/mf2pt2_sub-CC320608_ses-rest_task-rest_meg_preproc_raw.fif"

raw = mne.io.read_raw_fif(preproc_file, preload=True)
raw = raw.filter(l_freq=1, h_freq=45, method="iir", iir_params={"order": 5, "ftype": "butter"})
sensor_data = raw.get_data(picks="meg", reject_by_annotation="omit")
sensor_cov = np.cov(sensor_data)

voxel_data, _ = beamforming.voxel_timeseries(
    subjects_dir=subjects_dir,
    subject=subject,
    preproc_file=preproc_file,
    chantypes="meg",
    freq_range=[1, 45],
    reject_by_annotation="omit",
)
voxel_cov = np.cov(voxel_data)

weights, _ = beamforming.get_lcmv_weights(subjects_dir, subject)  # projects from sensor to MNI voxel space
weights_inv = np.linalg.pinv(weights)

print("Going forward")
test_cov = weights @ sensor_cov @ weights.T
print(voxel_cov)
print(test_cov)
print()

print("Going backward")
test_cov = weights_inv @ voxel_cov @ weights_inv.T
print(sensor_cov)
print(test_cov)

The covariances going forward match. The covariances going backwards don't because you removed information (power, i.e. variance) when you applied the beamformer to the sensor data.

@cgohil8 cgohil8 marked this pull request as ready for review January 10, 2025 03:58
@cgohil8 cgohil8 merged commit 938a139 into main Jan 10, 2025
1 check passed
@cgohil8 cgohil8 deleted the get-bf-weights branch January 10, 2025 05:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant