- MNE version: 1.8.0
- operating system: Windows 11
I used sLORETA to obtain STC files for two groups of subjects under the same conditions (left and right brain)
I want to use the " ‘2 samples permutation test on source data with spatio temporal clustering’" to compare the results of two groups of participants and identify clusters with significant differences
then i use code below to show the Clusters with significant differences
stc_all_cluster_vis = summarize_clusters_stc(
clu, tstep=tstep, vertices=fsave_vertices, subject=subject
)
brain = stc_all_cluster_vis.plot(
subject,
hemi="both",
views="lateral",
subjects_dir=subjects_dir,
time_label="时间范围 (ms)",
clim=dict(kind="value", lims=[0, 1, 40]),
However, the plot give me a strange result(shown in picture): half brain has no significant differences, half brain has all significant differences.
there must be something wrong with my code, but there is no Reference Example to study,
Few people use MNE for 2-sample permutation tests on source data on google scholar.
Can anyone provide corresponding tutorials (preferably published literature)?
My complete code is as follows
def run_cluster_analysis(X1, X2, src, subjects_dir, subject='fsaverage'):
"""
Perform spatiotemporal cluster analysis
Parameters:
X1, X2 : numpy.ndarray
Group data, shape (n_subjects, n_vertices, n_times)
src : SourceSpaces
Source space object
subjects_dir, subject : str
Subject information
"""
n_subjects1, n_vertices, n_times = X1.shape
n_subjects2 = X2.shape[0]
# KEY STEP 1: Transpose dimensions for cluster test
# From (n_subjects, n_vertices, n_times) to (n_subjects, n_times, n_vertices)
X1 = np.transpose(X1, [0, 2, 1])
X2 = np.transpose(X2, [0, 2, 1])
X = [X1, X2]
# KEY STEP 2: Calculate spatial adjacency matrix
adjacency = spatial_src_adjacency(src)
# KEY STEP 3: Set statistical threshold
p_threshold = 0.05
f_threshold = stats.distributions.f.ppf(
1.0 - p_threshold / 2.0, n_subjects1 - 1, n_subjects2 - 1
)
# KEY STEP 4: Perform cluster analysis
F_obs, clusters, cluster_p_values, H0 = clu = spatio_temporal_cluster_test(
X,
adjacency=adjacency,
n_permutations=1000,
threshold=f_threshold
)
# KEY STEP 5: Select significant clusters (p < 0.05)
good_cluster_inds = np.where(cluster_p_values < 0.05)[0]
if len(good_cluster_inds) > 0:
# KEY STEP 6: Prepare visualization data
tstep = 1000 / 50 # Assuming 50Hz sampling rate
fsave_vertices = [s['vertno'] for s in src]
stc_all_cluster_vis = summarize_clusters_stc(
clu, tstep=tstep, vertices=fsave_vertices, subject=subject
)
# Visualization code (simplified)
brain = stc_all_cluster_vis.plot(
subject,
hemi="both",
subjects_dir=subjects_dir,
time_label="Time range (ms)",
clim=dict(kind="value", lims=[0, 1, 40])
)
return brain, stc_all_cluster_vis
else:
return None, None
def main():
"""Main function - simplified for clarity"""
# Setup and data loading (pseudocode)
env = setup_environment()
X1 = load_group_data(group1_subjects, env['group1_dir'])
X2 = load_group_data(group2_subjects, env['group2_dir'])
# Run the analysis with loaded data
brain, clusters_stc = run_cluster_analysis(
X1, X2, env['src'], env['subjects_dir'], env['subject']
)
`