|
| 1 | +# Code modify by [email protected] |
| 2 | +# last update 2023.11.27 |
| 3 | +# Input : raw data |
| 4 | +# Output : connectivity map by region (N by N matrix, N is number of region) |
| 5 | + |
| 6 | +import numpy as np |
| 7 | +import mne_connectivity |
| 8 | + |
| 9 | +def get_connectivity(data, method): |
| 10 | + con = mne_connectivity.spectral_connectivity_epochs( |
| 11 | + data, |
| 12 | + method=method, |
| 13 | + mode="multitaper", |
| 14 | + sfreq=sfreq, |
| 15 | + fmin=fmin, |
| 16 | + fmax=fmax, |
| 17 | + faverage=True, |
| 18 | + tmin=tmin, |
| 19 | + mt_adaptive=False, |
| 20 | + n_jobs=1, |
| 21 | + ) |
| 22 | + return con |
| 23 | + |
| 24 | + |
| 25 | +def connectivity_mean(con): |
| 26 | + con_mean = [] |
| 27 | + con_mean = con[0].get_data("dense")[:, :, 0].copy() |
| 28 | + for sub in range(1, len(con)): |
| 29 | + con_mean = np.add(con_mean, con[sub].get_data("dense")[:, :, 0].copy()) |
| 30 | + con_mean = np.divide(con_mean, len(con)) |
| 31 | + return con_mean |
| 32 | + |
| 33 | + |
| 34 | +def ch_to_idx(ch_names, ch_list): |
| 35 | + ch_idx = [] |
| 36 | + for ch in ch_list: |
| 37 | + ch_idx.append(ch_names.index(ch)) |
| 38 | + return ch_idx |
| 39 | + |
| 40 | + |
| 41 | +def get_roi_con(con, region1, region2): |
| 42 | + roi_con = [] |
| 43 | + for i in region1: |
| 44 | + for j in region2: |
| 45 | + if i != j: |
| 46 | + if i < j: |
| 47 | + roi_con.append(con[j][i]) |
| 48 | + else: |
| 49 | + roi_con.append(con[i][j]) |
| 50 | + return np.mean(roi_con) |
| 51 | + |
| 52 | + |
| 53 | +def get_roi_map(con, ch): |
| 54 | + roi_map = np.zeros((5, 5)) |
| 55 | + roi_list = [frontal, central, temporal, parietal, occipital] |
| 56 | + for i in range(0, 5): |
| 57 | + for j in range(0, 5): |
| 58 | + roi_map[i][j] = get_roi_con( |
| 59 | + con, ch_to_idx(ch, roi_list[i]), ch_to_idx(ch, roi_list[j]) |
| 60 | + ) |
| 61 | + return roi_map |
| 62 | + |
| 63 | + |
| 64 | +# example |
| 65 | +# This example based on BrainVision data |
| 66 | +# You can modify this code for your own data |
| 67 | + |
| 68 | +sfreq = 100 # sampling frequency |
| 69 | +fmin = 8 # frequency min |
| 70 | +fmax = 12 # frequency max |
| 71 | +tmin = 0 # time min |
| 72 | + |
| 73 | +ground = "Fpz" |
| 74 | +reference = "Cz" |
| 75 | +frontal = [ |
| 76 | + "Fp1", |
| 77 | + "Fp2", |
| 78 | + "AFz", |
| 79 | + "AF3", |
| 80 | + "AF4", |
| 81 | + "AF7", |
| 82 | + "AF8", |
| 83 | + "Fz", |
| 84 | + "F1", |
| 85 | + "F2", |
| 86 | + "F3", |
| 87 | + "F4", |
| 88 | + "F5", |
| 89 | + "F6", |
| 90 | + "F7", |
| 91 | + "F8", |
| 92 | +] |
| 93 | +central = [ |
| 94 | + "FC1", |
| 95 | + "FC2", |
| 96 | + "FC3", |
| 97 | + "FC4", |
| 98 | + "FC5", |
| 99 | + "FC6", |
| 100 | + "Cz", |
| 101 | + "C1", |
| 102 | + "C2", |
| 103 | + "C3", |
| 104 | + "C4", |
| 105 | + "C5", |
| 106 | + "C6", |
| 107 | +] |
| 108 | +temporal = ["FT7", "FT8", "FT9", "FT10", "T7", "T8", "TP7", "TP8", "TP9", "TP10"] |
| 109 | +parietal = [ |
| 110 | + "CPz", |
| 111 | + "CP1", |
| 112 | + "CP2", |
| 113 | + "CP3", |
| 114 | + "CP4", |
| 115 | + "CP5", |
| 116 | + "CP6", |
| 117 | + "Pz", |
| 118 | + "P1", |
| 119 | + "P2", |
| 120 | + "P3", |
| 121 | + "P4", |
| 122 | + "P5", |
| 123 | + "P6", |
| 124 | + "P7", |
| 125 | + "P8", |
| 126 | +] |
| 127 | +occipital = ["POz", "PO3", "PO4", "PO7", "PO8", "Oz", "O1", "O2", "Iz"] |
| 128 | + |
| 129 | +baseline = [] # baseline connectivity data |
| 130 | +g1 = [] # group 1 connectivity data |
| 131 | +g2 = [] # group 2 connectivity data |
| 132 | +g3 = [] # group 3 connectivity data |
| 133 | +group_list = [baseline, g1, g2, g3] |
| 134 | +data_list = [baseline_data, g1_data, g2_data, g3_data] # raw data list |
| 135 | +method = "pli" |
| 136 | + |
| 137 | +for idx, con_value in enumerate(group_list): |
| 138 | + for sub in range(len(data_list[idx])): |
| 139 | + con_value.append(get_connectivity(data_list[idx][sub], method)) |
| 140 | + |
| 141 | +for idx, value in enumerate(group_list): |
| 142 | + mean = [] |
| 143 | + mean = connectivity_mean(value) |
| 144 | + roi = get_roi_map(mean, ch_names) # ch_names is channel name list from raw data |
0 commit comments