if we create a forward solution with surface ico down sampling parameters = 5 , which means i select 20484 brain locations to put dipoles. And after source reconstruction , typically we will get a data matix 20484 * timepoints (each element is electrical magnitude) for different conditions. And we will do decoding analysis, which means the final matrix is 20484*timepoints (each element is decoding value).
it seems that if i want to flexibly understand my data in whole brain level, it is useful to get a label vector in format like 20484 (spatial location) *with each element showing label(brain area) with different brain template. And i can see that the folder label in the folder fsaverage do have a lot of anatomy maps , however with the vector size in 163842(maybe for freesurfer’s typical resolution?) So how do you guys handle this ? can i find brain anatomy vector in size 20484 or can i know the relation between the two spatial dimension 20484 and 163842?
it sounds like what you want is to get a vector of anatomical label names, that is the same size as the number of vertices in your STC, and in the same order as the STC vertices. You can probably construct this using a combination of mne.read_labels_from_annot() and label.get_vertices_used() I think.
This gets you pretty close:
from collections import defaultdict
import mne
data_path = mne.datasets.sample.data_path()
meg_path = data_path / "MEG" / "sample"
subjects_dir = data_path / "subjects"
fname_stc = meg_path / "sample_audvis-meg"
stc = mne.read_source_estimate(fname_stc, subject="sample")
labels = mne.read_labels_from_annot("sample", "aparc.a2009s")
label_verts = dict()
for label in labels:
hemi = 0 if label.name.endswith("-lh") else 1
label_verts[label.name] = label.get_vertices_used(stc.vertices[hemi])
results = defaultdict(list)
for vert in stc.vertices[0]:
for label, lverts in label_verts.items():
if vert in lverts:
results[vert].append(label)
Sure!
I only changed a few points.
I saved the anatomical label vector into size of 20484*1, so that it directly match the STC 's data format.
Below are my codes.
from collections import defaultdict
import mne
import numpy as np
import scipy.io
ana_name = 'HCPMMP1_combined'
stc = mne.read_source_estimate('G:\\MEG_analysis\\source_megdata\\tempstc-lh.stc', subject="fsaverage")
labels = mne.read_labels_from_annot('fsaverage', ana_name)
label_verts = dict()
for label in labels:
hemi = 0 if label.name.endswith("-lh") else 1
label_verts[label.name] = label.get_vertices_used(stc.vertices[hemi])
results = np.empty([len(stc.vertices[0])*2], dtype=object)
for vert in stc.vertices[0]:
for label, lverts in label_verts.items():
if vert in lverts and label.endswith("-lh"):
results[vert]=label
for vert in stc.vertices[1]:
for label, rverts in label_verts.items():
if vert in rverts and label.endswith("-rh"):
results[vert+len(stc.vertices[0])]=label
mat_file_name = r'G:\MEG_analysis\fsaverage_anatomy_stc\{}.mat'.format(ana_name)
scipy.io.savemat(mat_file_name, {'anat': results})