def eegANDfmri_corr(eeg_data, fmri_data, chl_opt=0, ksize=[3, 3, 3], strides=[1, 1, 1], sub_opt=1, method="spearman", rescale=False, permutation=False, iter=1000): """ Calculate the Similarities between EEG/MEG/fNIRS data and fMRI data for searchligt Parameters ---------- eeg_data : array The EEG/MEG/fNIRS data. The shape of EEGdata must be [n_cons, n_subs, n_trials, n_chls, n_ts]. n_cons, n_subs, n_trials, n_chls & n_ts represent the number of conidtions, the number of subjects, the number of trials, the number of channels & the number of time-points, respectively. fmri_data : array The fmri data. The shape of fmri_data must be [n_cons, n_subs, nx, ny, nz]. n_cons, nx, ny, nz represent the number of conditions, the number of subs & the size of fMRI-img, respectively. chl_opt : int 0 or 1. Default is 0. Calculate the RDM & similarities for each channel or not. If chl_opt=0, calculating based on all channels' data. If chl_opt=1, calculating based on each channel's data respectively. ksize : array or list [kx, ky, kz]. Default is [3, 3, 3]. The size of the calculation unit for searchlight. kx, ky, kz represent the number of voxels along the x, y, z axis. kx, ky, kz should be odd. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating along the x, y, z axis. sub_opt: int 0 or 1. Default is 1. Return the subject-result or average-result. If sub_opt=0, return the average result. If sub_opt=1, return the results of each subject. method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'. The method to calculate the similarities. If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the Cosine Similarities. If method='distance', calculate the Euclidean Distances. rescale : bool True or False. Rescale the values in RDM or not. Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal. permutation : bool True or False. Default is False. Use permutation test or not. iter : int. Default is 1000. The times for iteration. Returns ------- corrs : array The similarities between EEG/MEG/fNIRS data and fMRI data for searchlight. If chl_opt=1 & sub_result=1, the shape of corrs is [n_subs, n_chls, n_x, n_y, n_z, 2]. n_subs, n_x, n_y, n_z represent the number of subjects, the number of calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value. If chl_opt=1 & sub_result=0, the shape of corrs is [n_chls, n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value. If chl_opt=0 & sub_result=1, the shape of RDMs is [n_subs, n_x, n_y, n_z, 2]. n_subs, n_x, n_y, n_z represent the number of subjects, the number of calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value. If chl_opt=0 & sub_result=0, the shape of corrs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value. """ if len(np.shape(eeg_data)) != 5 or len(np.shape(fmri_data)) != 5: return "Invalid input!" # get the number of subjects subs = np.shape(fmri_data)[1] # get the size of the fMRI-img nx = np.shape(fmri_data)[2] ny = np.shape(fmri_data)[3] nz = np.shape(fmri_data)[4] # the size of the calculation units for searchlight kx = ksize[0] ky = ksize[1] kz = ksize[2] # strides for calculating along the x, y, z axis sx = strides[0] sy = strides[1] sz = strides[0] # calculate the number of the calculation units in the x, y, z directions n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 # get the number of channels chls = np.shape(eeg_data)[3] # calculate the fmri_rdms for searchlight fmri_rdms = fmriRDM(fmri_data, sub_opt=sub_opt, ksize=ksize, strides=strides) # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt) # chl_opt=1 if chl_opt == 1: # sub_opt=1 if sub_opt == 1: # chl_opt=1 & sub_result=1 print("\nComputing similarities") # initialize the corrs corrs = np.full([subs, chls, n_x, n_y, n_z, 2], np.nan) total = subs * n_x * n_y * n_z # calculate the corrs for sub in range(subs): for j in range(n_x): for k in range(n_y): for l in range(n_z): for i in range(chls): # show the progressbar percent = (sub * n_x * n_y * n_z + i * n_y * n_z + j * n_z + k) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[sub, i, j, k, l] = rdm_correlation_spearman(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[sub, i, j, k, l] = rdm_correlation_pearson(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[sub, i, j, k, l] = rdm_correlation_kendall(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[sub, i, j, k, l, 0] = rdm_similarity(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], rescale=rescale) elif method == "distance": corrs[sub, i, j, k, l, 0] = rdm_distance(eeg_rdms[sub, i], fmri_rdms[sub, i, j, k], rescale=rescale) print("\nComputing finished!") return corrs # sub_opt=0 # chl_opt=1 & sub_opt=0 print("\nComputing similarities") # initialize the corrs corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan) total = n_x * n_y * n_z # calculate the corrs for j in range(n_x): for k in range(n_y): for l in range(n_z): for i in range(chls): # show the progressbar percent = (i * n_y * n_z + j * n_z + k) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j, k, l] = rdm_correlation_spearman(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k, l] = rdm_correlation_pearson(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k, l] = rdm_correlation_kendall(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, l, 0] = rdm_similarity(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) elif method == "distance": corrs[i, j, k, l, 0] = rdm_distance(eeg_rdms[i], fmri_rdms[i, j, k], rescale=rescale) print("\nComputing finished!") return corrs # chl_opt=0 # sub_opt=1 if sub_opt == 1: # chl_opt=0 & sub_opt=1 print("\nComputing similarities") # initialize the corrs corrs = np.full([subs, n_x, n_y, n_z, 2], np.nan) total = subs * n_x * n_y * n_z # calculate the corrs for i in range(subs): for j in range(n_x): for k in range(n_y): for l in range(n_z): # show the progressbar percent = (i * n_x * n_y * n_z + j * n_y * n_z + k * n_z + l) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j, k, l] = rdm_correlation_spearman(eeg_rdms[i], fmri_rdms[i, j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k, l] = rdm_correlation_pearson(eeg_rdms[i], fmri_rdms[i, j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k, l] = rdm_correlation_kendall(eeg_rdms[i], fmri_rdms[i, j, k, l], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, l, 0] = rdm_similarity(eeg_rdms[i], fmri_rdms[i, j, k, l], rescale=rescale) elif method == "distance": corrs[i, j, k, l, 0] = rdm_distance(eeg_rdms[i], fmri_rdms[i, j, k, l], rescale=rescale) print("\nComputing finished!") return corrs # sub_opt=0 # chl_opt=0 & sub_opt=0 print("\nComputing similarities") # initialize the corrs corrs = np.full([n_x, n_y, n_z, 2], np.nan) total = n_x * n_y * n_z # calculate the corrs for i in range(n_x): for j in range(n_y): for k in range(n_z): # show the progressbar percent = (i * n_y * n_z + j * n_z + k) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(eeg_rdms, fmri_rdms[i, j, k], rescale=rescale) print("\nComputing finished!") return corrs
def fmriRDM(fmri_data, ksize=[3, 3, 3], strides=[1, 1, 1], sub_opt=1, method="correlation", abs=False): """ Calculate the Representational Dissimilarity Matrices (RDMs) based on fMRI data (searchlight) Parameters ---------- fmri_data : array The fmri data. The shape of fmri_data must be [n_cons, n_subs, nx, ny, nz]. n_cons, nx, ny, nz represent the number of conditions, the number of subs & the size of fMRI-img, respectively. ksize : array or list [kx, ky, kz]. Default is [3, 3, 3]. The size of the calculation unit for searchlight. kx, ky, kz represent the number of voxels along the x, y, z axis. kx, ky, kz should be odd. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating along the x, y, z axis. sub_opt: int 0 or 1. Default is 1. Return the subject-result or average-result. If sub_opt=0, return the average result. If sub_opt=1, return the results of each subject. method : string 'correlation' or 'euclidean'. Default is 'correlation'. The method to calculate the dissimilarities. If method='correlation', the dissimilarity is calculated by Pearson Correlation. If method='euclidean', the dissimilarity is calculated by Euclidean Distance, the results will be normalized. abs : boolean True or False. Default is True. Calculate the absolute value of Pearson r or not. Returns ------- RDM : array The fMRI-Searchlight RDM. If sub_opt=0, the shape of RDMs is [n_x, n_y, n_z, n_cons, n_cons]. If sub_opt=1, the shape of RDMs is [n_subs, n_x, n_y, n_cons, n_cons] n_subs, n_x, n_y, n_z represent the number of subjects & the number of calculation units for searchlight along the x, y, z axis. """ if len(np.shape(fmri_data)) != 5: print( "\nThe shape of input for fmriRDM() function must be [n_cons, n_subs, nx, ny, nz].\n" ) return "Invalid input!" # get the number of conditions, subjects and the size of the fMRI-img cons, subs, nx, ny, nz = np.shape(fmri_data) # the size of the calculation units for searchlight kx = ksize[0] ky = ksize[1] kz = ksize[2] # strides for calculating along the x, y, z axis sx = strides[0] sy = strides[1] sz = strides[2] # calculate the number of the calculation units in the x, y, z directions n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 # initialize the data for calculating the RDM data = np.full([n_x, n_y, n_z, cons, kx * ky * kz, subs], np.nan) print("\nComputing RDMs") # assignment for x in range(n_x): for y in range(n_y): for z in range(n_z): for i in range(cons): index = 0 for k1 in range(kx): for k2 in range(ky): for k3 in range(kz): for j in range(subs): data[x, y, z, i, index, j] = fmri_data[i, j, x * sx + k1, y * sy + k2, z * sz + k3] index = index + 1 # shape of data: [n_x, n_y, n_z, cons, kx*ky*kz, subs] # ->[subs, n_x, n_y, n_z, cons, kx*ky*kz] data = np.transpose(data, (5, 0, 1, 2, 3, 4)) # flatten the data for different calculating conditions data = np.reshape(data, [subs, n_x, n_y, n_z, cons, kx * ky * kz]) # initialize the RDMs subrdms = np.full([subs, n_x, n_y, n_z, cons, cons], np.nan) total = subs * n_x * n_y * n_z for sub in range(subs): for x in range(n_x): for y in range(n_y): for z in range(n_z): # show the progressbar percent = (sub * n_x * n_y * n_z + x * n_y * n_z + y * n_z + z + 1) / total * 100 show_progressbar("Calculating", percent) for i in range(cons): for j in range(cons): # no NaN if (np.isnan(data[:, x, y, z, i]).any() == False) and \ (np.isnan(data[:, x, y, z, j]).any() == False): if method == 'correlation': # calculate the Pearson Coefficient r = pearsonr(data[sub, x, y, z, i], data[sub, x, y, z, j])[0] # calculate the dissimilarity if abs == True: subrdms[sub, x, y, z, i, j] = limtozero(1 - np.abs(r)) else: subrdms[sub, x, y, z, i, j] = limtozero(1 - r) elif method == 'euclidean': subrdms[sub, x, y, z, i, j] = np.linalg.norm( data[sub, x, y, z, i] - data[sub, x, y, z, j]) """elif method == 'mahalanobis': X = np.transpose(np.vstack((data[sub, x, y, z, i], data[sub, x, y, z, j])), (1, 0)) X = np.dot(X, np.linalg.inv(np.cov(X, rowvar=False))) subrdms[sub, x, y, z, i, j] = np.linalg.norm(X[:, 0] - X[:, 1])""" if method == 'euclidean': max = np.max(subrdms[sub, x, y, z]) min = np.min(subrdms[sub, x, y, z]) subrdms[sub, x, y, z] = (subrdms[sub, x, y, z] - min) / (max - min) # average the RDMs rdms = np.average(subrdms, axis=0) print("\nRDMs computing finished!") if sub_opt == 0: return rdms if sub_opt == 1: return subrdms
def bhvANDeeg_corr(bhv_data, eeg_data, sub_opt=1, chl_opt=0, time_opt=0, time_win=5, time_step=5, method="spearman", rescale=False, permutation=False, iter=5000): """ Calculate the Similarities between behavioral data and EEG/MEG/fNIRS data Parameters ---------- bhv_data : array The behavioral data. The shape of bhv_data must be [n_cons, n_subs, n_trials]. n_cons, n_subs & n_trials represent the number of conidtions, the number of subjects & the number of trials, respectively. eeg_data : array The EEG/MEG/fNIRS data. The shape of EEGdata must be [n_cons, n_subs, n_trials, n_chls, n_ts]. n_cons, n_subs, n_trials, n_chls & n_ts represent the number of conidtions, the number of subjects, the number of trials, the number of channels & the number of time-points, respectively. sub_opt : int 0 or 1. Default is 0. Calculate the RDM & similarities for each subject or not. If sub_opt=0, calculating based on all data. If sub_opt=1, calculating based on each subject's data, respectively. chl_opt : int 0 or 1. Default is 0. Calculate the RDM & similarities for each channel or not. If chl_opt=0, calculating based on all channels' data. If chl_opt=1, calculating based on each channel's data respectively. time_opt : int 0 or 1. Default is 0. Calculate the RDM & similarities for each time-point or not If time_opt=0, calculating based on whole time-points' data. If time_opt=1, calculating based on each time-points respectively. time_win : int. Default is 5. Set a time-window for calculating the RDM & similarities for different time-points. Only when time_opt=1, time_win works. If time_win=5, that means each calculation process based on 5 time-points. time_step : int. Default is 5. The time step size for each time of calculating. Only when time_opt=1, time_step works. method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'. The method to calculate the similarities. If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the Cosine Similarities. If method='distance', calculate the Euclidean Distances. rescale : bool True or False. Rescale the values in RDM or not. Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal. permutation : bool True or False. Default is False. Use permutation test or not. iter : int. Default is 5000. The times for iteration. Returns ------- corrs : array The similarities between behavioral data and EEG/MEG/fNIRS data. If sub_opt=0 & chl_opt=0 & time_opt=0, return one corr result. The shape of corrs is [2], a r-value and a p-value. If method='similarity' or method='distance', the p-value is 0. If sub_opt=0 & chl_opt=0 & time_opt=1, return int((n_ts-time_win)/time_step)+1 corrs result. The shape of corrs is [int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. If sub_opt=0 & chl_opt=1 & time_opt=0, return n_chls corrs result. The shape of corrs is [n_chls, 2]. 2 represents a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. If sub_opt=0 & chl_opt=1 & time_opt=1, return n_chls*(int((n_ts-time_win)/time_step)+1) corrs result. The shape of corrs is [n_chls, int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. If sub_opt=1 & chl_opt=0 & time_opt=0, return n_subs corr result. The shape of corrs is [n_subs, 2], a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. If sub_opt=1 & chl_opt=0 & time_opt=1, return n_subs*(int((n_ts-time_win)/time_step)+1) corrs result. The shape of corrs is [n_subs, int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. If sub_opt=1 & chl_opt=1 & time_opt=0, return n_subs*n_chls corrs result. The shape of corrs is [n_subs, n_chls, 2]. 2 represents a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. If sub_opt=1 & chl_opt=1 & time_opt=1, return n_subs*n_chls*(int((n_ts-time_win)/time_step)+1) corrs result. The shape of corrs is [n_subs, n_chls, int((n_ts-time_win)/time_step)+1, 2]. 2 represents a r-value and a p-value. If method='similarity' or method='distance', the p-values are all 0. """ if len(np.shape(bhv_data)) != 3 or len(np.shape(eeg_data)) != 5: return "Invalid input!" # get the number of subjects subs = np.shape(bhv_data)[1] # get the number of channels chls = np.shape(eeg_data)[3] # get the number of time-points for calculating ts = np.shape(eeg_data)[4] ts = int((ts-time_win)/time_step)+1 if sub_opt == 1: # calculate the bhv_rdm bhv_rdms = bhvRDM(bhv_data, sub_opt=sub_opt) if chl_opt == 0: if time_opt == 0: # sub_opt=1 & chl_opt=0 & time_opt=0 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) # initialize the corrs corrs = np.zeros([subs, 2], dtype=np.float64) # calculate the corrs for i in range(subs): if method == "spearman": corrs[i] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i], rescale=rescale) elif method == "distance": corrs[i, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i], rescale=rescale) print("\nComputing finished!") return corrs # sub_opt=1 & chl_opt=0 & time_opt=1 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step) # initialize the corrs corrs = np.zeros([subs, ts, 2], dtype=np.float64) total = subs * ts # calculate the corrs for i in range(subs): for j in range(ts): # show the progressbar percent = (i * ts + j) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) elif method == "distance": corrs[i, j, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) print("\nComputing finished!") return corrs if time_opt == 1: # sub_opt=1 & chl_opt=1 & time_opt=1 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step) # initialize the corrs corrs = np.zeros([subs, chls, ts, 2], dtype=np.float64) total = subs * chls * ts # calculate the corrs for i in range(subs): for j in range(chls): for k in range(ts): # show the progressbar percent = (i * chls * ts + j * ts + k) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i, j, k], rescale=rescale) print("\nComputing finished!") return corrs # sub_opt=1 & chl_opt=1 & time_opt=0 print("\nComputing similarities") eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) corrs = np.zeros([subs, chls, 2], dtype=np.float64) for i in range(subs): for j in range(chls): if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, 0] = rdm_similarity(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) elif method == "distance": corrs[i, j, 0] = rdm_distance(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) print("\nComputing finished!") return corrs # if sub_opt=0 bhv_rdm = bhvRDM(bhv_data, sub_opt=sub_opt) if chl_opt == 1: if time_opt == 1: # sub_opt = 0 & chl_opt = 1 & time_opt = 1 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step) # initialize the corrs corrs = np.zeros([chls, ts, 2], dtype=np.float64) total = chls * ts # calculate the corrs for i in range(chls): for j in range(ts): # show the progressbar percent = (i * ts + j) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i, j], rescale=rescale) elif method == "distance": corrs[i, j, 0] = rdm_distance(bhv_rdm, eeg_rdms[i, j], rescale=rescale) return corrs # sub_opt=0 & chl_opt=1 & time_opt=0 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) # initialize the corrs corrs = np.zeros([chls, 2], dtype=np.float64) # calculate the corrs for i in range(chls): if method == "spearman": corrs[i] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "distance": corrs[i, 0] = rdm_distance(bhv_rdm, eeg_rdms[i], rescale=rescale) print("\nComputing finished!") return corrs # if chl_opt=0 if time_opt == 1: # sub_opt=0 & chl_opt=0 & time_opt=1 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt, time_win=time_win, time_step=time_step) # initialize the corrs corrs = np.zeros([ts, 2], dtype=np.float64) # calculate the corrs for i in range(ts): if method == "spearman": corrs[i] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "distance": corrs[i, 0] = rdm_distance(bhv_rdm, eeg_rdms[i], rescale=rescale) print("\nComputing finished!") return corrs # sub_opt=0 & chl_opt=0 & time_opt=0 print("\nComputing similarities") # calculate the eeg_rdms eeg_rdm = eegRDM(eeg_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) # initialize the corrs corr = np.zeros([2], dtype=np.float64) # calculate the corrs if method == "spearson": corr = rdm_correlation_spearman(bhv_rdm, eeg_rdm, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corr = rdm_correlation_pearson(bhv_rdm, eeg_rdm, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corr = rdm_correlation_kendall(bhv_rdm, eeg_rdm, rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corr[0] = rdm_similarity(bhv_rdm, eeg_rdm, rescale=rescale) elif method == "distance": corr[0] = rdm_distance(bhv_rdm, eeg_rdm, rescale=rescale) print("\nComputing finished!") return corr
def fmrirdms_corr(demo_rdm, fmri_rdms, method="spearman", rescale=False, permutation=False, iter=1000): """ Calculate the similarity between fMRI searchlight RDMs and a demo RDM Parameters ---------- demo_rdm : array [n_cons, n_cons] A demo RDM. fmri_rdms : array The fMRI-Searchlight RDMs. The shape of RDMs is [n_x, n_y, n_z, n_cons, n_cons]. n_x, n_y, n_z represent the number of calculation units for searchlight along the x, y, z axis. method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'. The method to calculate the similarities. If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the Cosine Similarities. If method='distance', calculate the Euclidean Distances. rescale : bool True or False. Rescale the values in RDM or not. Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal. permutation : bool True or False. Default is False. Use permutation test or not. iter : int. Default is 1000. The times for iteration. Returns ------- corrs : array The similarities between fMRI searchlight RDMs and a demo RDM The shape of RDMs is [n_x, n_y, n_z, 2]. n_x, n_y, n_z represent the number of calculation units for searchlight along the x, y, z axis and 2 represents a r-value and a p-value. Notes ----- The demo RDM could be a behavioral RDM, a hypothesis-based coding model RDM or a computational model RDM. """ if len(np.shape(demo_rdm)) != 2 or np.shape(demo_rdm)[0] != np.shape(demo_rdm)[1]: print("\nThe shape of the demo RDM should be [n_cons, n_cons].\n") return "Invalid input!" if len(np.shape(fmri_rdms)) != 5 or np.shape(fmri_rdms)[3] != np.shape(fmri_rdms)[4]: print("\nThe shape of the fMRI RDMs should be [n_x, n_y, n_z, n_cons, n_cons].\n") return "Invalid input!" # calculate the number of the calculation units in the x, y, z directions n_x = np.shape(fmri_rdms)[0] n_y = np.shape(fmri_rdms)[1] n_z = np.shape(fmri_rdms)[2] print("\nComputing similarities") # initialize the corrs corrs = np.full([n_x, n_y, n_z, 2], np.nan) total = n_x * n_y * n_z # calculate the corrs for i in range(n_x): for j in range(n_y): for k in range(n_z): # show the progressbar percent = (i * n_y * n_z + j * n_z + k + 1) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(demo_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(demo_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(demo_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(demo_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(demo_rdm, fmri_rdms[i, j, k], rescale=rescale) print("\nComputing finished!") return corrs
def eegRDM(EEG_data, sub_opt=1, chl_opt=0, time_opt=0, time_win=5, time_step=5, method="correlation", abs=False): """ Calculate the Representational Dissimilarity Matrix(Matrices) - RDM(s) based on EEG-like data Parameters ---------- EEG_data : array The EEG/MEG/fNIRS data. The shape of EEGdata must be [n_cons, n_subs, n_trials, n_chls, n_ts]. n_cons, n_subs, n_trials, n_chls & n_ts represent the number of conidtions, the number of subjects, the number of trials, the number of channels & the number of time-points, respectively. sub_opt: int 0 or 1. Default is 1. Return the subject-result or average-result. If sub_opt=0, return the average result. If sub_opt=1, return the results of each subject. chl_opt : int 0 or 1. Default is 0. Calculate the RDM for each channel or not. If chl_opt=0, calculate the RDM based on all channels'data. If chl_opt=1, calculate the RDMs based on each channel's data respectively. time_opt : int 0 or 1. Default is 0. Calculate the RDM for each time-point or not If time_opt=0, calculate the RDM based on whole time-points' data. If time_opt=1, calculate the RDMs based on each time-points respectively. time_win : int. Default is 5. Set a time-window for calculating the RDM for different time-points. Only when time_opt=1, time_win works. If time_win=5, that means each calculation process based on 5 time-points. time_step : int. Default is 5. The time step size for each time of calculating. Only when time_opt=1, time_step works. method : string 'correlation' or 'euclidean'. Default is 'correlation'. The method to calculate the dissimilarities. If method='correlation', the dissimilarity is calculated by Pearson Correlation. If method='euclidean', the dissimilarity is calculated by Euclidean Distance, the results will be normalized. abs : boolean True or False. Default is True. Calculate the absolute value of Pearson r or not. Returns ------- RDM(s) : array The EEG/MEG/fNIR/other EEG-like RDM. If sub_opt=0 & chl_opt=0 & time_opt=0, return only one RDM. The shape is [n_cons, n_cons]. If sub_opt=0 & chl_opt=0 & time_opt=1, return int((n_ts-time_win)/time_step)+1 RDM. The shape is [int((n_ts-time_win)/time_step)+1, n_cons, n_cons]. If sub_opt=0 & chl_opt=1 & time_opt=0, return n_chls RDM. The shape is [n_chls, n_cons, n_cons]. If sub_opt=0 & chl_opt=1 & time_opt=1, return n_chls*(int((n_ts-time_win)/time_step)+1) RDM. The shape is [n_chls, int((n_ts-time_win)/time_step)+1, n_cons, n_cons]. If sub_opt=1 & chl_opt=0 & time_opt=0, return n_subs RDM. The shape is [n_subs, n_cons, n_cons]. If sub_opt=1 & chl_opt=0 & time_opt=1, return n_subs*(int((n_ts-time_win)/time_step)+1) RDM. The shape is [n_subs, int((n_ts-time_win)/time_step)+1, n_cons, n_cons]. If sub_opt=1 & chl_opt=1 & time_opt=0, return n_subs*n_chls RDM. The shape is [n_subs, n_chls, n_cons, n_cons]. If sub_opt=1 & chl_opt=1 & time_opt=1, return n_subs*n_chls*(int((n_ts-time_win)/time_step)+1) RDM. The shape is [n_subs, n_chls, int((n_ts-time_win)/time_step)+1, n_cons, n_cons]. Notes ----- Sometimes, the numbers of trials under different conditions are not same. In NeuroRA, we recommend users to average the trials under a same condition firstly in this situation. Thus, the shape of input (EEG_data) should be [n_cons, n_subs, 1, n_chls, n_ts]. """ if len(np.shape(EEG_data)) != 5: print( "The shape of input for eegRDM() function must be [n_cons, n_subs, n_trials, n_chls, n_ts].\n" ) return "Invalid input!" # get the number of conditions, subjects, trials, channels and time points cons, subs, trials, chls, ts = np.shape(EEG_data) if time_opt == 1: print("\nComputing RDMs") # the time-points for calculating RDM ts = int((ts - time_win) / time_step) + 1 # initialize the data for calculating the RDM data = np.zeros([subs, chls, ts, cons, time_win], dtype=np.float64) # assignment for i in range(subs): for j in range(chls): for k in range(ts): for l in range(cons): for m in range(time_win): # average the trials data[i, j, k, l, m] = np.average(EEG_data[l, i, :, j, k * time_step + m]) if chl_opt == 1: total = subs * chls * ts # initialize the RDMs rdms = np.zeros([subs, chls, ts, cons, cons], dtype=np.float64) # calculate the values in RDMs for i in range(subs): for j in range(chls): for k in range(ts): # show the progressbar percent = (i * chls * ts + j * ts + k + 1) / total * 100 show_progressbar("Calculating", percent) for l in range(cons): for m in range(cons): if method is 'correlation': # calculate the Pearson Coefficient r = pearsonr(data[i, j, k, l], data[i, j, k, m])[0] # calculate the dissimilarity if abs == True: rdms[i, j, k, l, m] = limtozero(1 - np.abs(r)) else: rdms[i, j, k, l, m] = limtozero(1 - r) elif method == 'euclidean': rdms[i, j, k, l, m] = np.linalg.norm(data[i, j, k, l] - data[i, j, k, m]) """elif method == 'mahalanobis': X = np.transpose(np.vstack((data[i, j, k, l], data[i, j, k, m])), (1, 0)) X = np.dot(X, np.linalg.inv(np.cov(X, rowvar=False))) rdms[i, j, k, l, m] = np.linalg.norm(X[:, 0] - X[:, 1])""" if method == 'euclidean': max = np.max(rdms[i, j, k]) min = np.min(rdms[i, j, k]) rdms[i, j, k] = (rdms[i, j, k] - min) / (max - min) # time_opt=1 & chl_opt=1 & sub_opt=1 if sub_opt == 1: print("\nRDMs computing finished!") return rdms # time_opt=1 & chl_opt=1 & sub_opt=0 if sub_opt == 0: rdms = np.average(rdms, axis=0) print("\nRDMs computing finished!") return rdms # if chl_opt = 0 data = np.transpose(data, (0, 2, 3, 4, 1)) data = np.reshape(data, [subs, ts, cons, time_win * chls]) rdms = np.zeros([subs, ts, cons, cons], dtype=np.float64) total = subs * ts # calculate the values in RDMs for i in range(subs): for k in range(ts): # show the progressbar percent = (i * ts + k + 1) / total * 100 show_progressbar("Calculating", percent) for l in range(cons): for m in range(cons): if method == 'correlation': # calculate the Pearson Coefficient r = pearsonr(data[i, k, l], data[i, k, m])[0] # calculate the dissimilarity if abs is True: rdms[i, k, l, m] = limtozero(1 - np.abs(r)) else: rdms[i, k, l, m] = limtozero(1 - r) elif method == 'euclidean': rdms[i, k, l, m] = np.linalg.norm(data[i, k, l] - data[i, k, m]) if method == 'euclidean': max = np.max(rdms[i, k]) min = np.min(rdms[i, k]) rdms[i, k] = (rdms[i, k] - min) / (max - min) # time_opt=1 & chl_opt=0 & sub_opt=1 if sub_opt == 1: print("\nRDMs computing finished!") return rdms # time_opt=1 & chl_opt=0 & sub_opt=0 if sub_opt == 0: rdms = np.average(rdms, axis=0) print("\nRDM computing finished!") return rdms # if time_opt = 0 if chl_opt == 1: print("\nComputing RDMs") # average the trials data = np.average(EEG_data, axis=2) # initialize the RDMs rdms = np.zeros([subs, chls, cons, cons], dtype=np.float64) total = subs * chls # calculate the values in RDMs for i in range(subs): for j in range(chls): # show the progressbar percent = (i * chls + j + 1) / total * 100 show_progressbar("Calculating", percent) for k in range(cons): for l in range(cons): if method == 'correlation': # calculate the Pearson Coefficient r = pearsonr(data[k, i, j], data[l, i, j])[0] # calculate the dissimilarity if abs == True: rdms[i, j, k, l] = limtozero(1 - np.abs(r)) else: rdms[i, j, k, l] = limtozero(1 - r) elif method == 'euclidean': rdms[i, j, k, l] = np.linalg.norm(data[k, i, j] - data[k, i, j]) if method == 'euclidean': max = np.max(rdms[i, j]) min = np.min(rdms[i, j]) rdms[i, j] = (rdms[i, j] - min) / (max - min) # time_opt=0 & chl_opt=1 & sub_opt=1 if sub_opt == 1: print("\nRDM computing finished!") return rdms # time_opt=0 & chl_opt=1 & sub_opt=0 if sub_opt == 0: rdms = np.average(rdms, axis=0) print("\nRDM computing finished!") return rdms # if chl_opt = 0 if sub_opt == 1: print("\nComputing RDMs") else: print("\nComputing RDM") # average the trials data = np.average(EEG_data, axis=2) # flatten the data for different calculating conditions data = np.reshape(data, [cons, subs, chls * ts]) # initialize the RDMs rdms = np.zeros([subs, cons, cons], dtype=np.float64) # calculate the values in RDMs for i in range(subs): for j in range(cons): for k in range(cons): if method == 'correlation': # calculate the Pearson Coefficient r = pearsonr(data[j, i], data[k, i])[0] # calculate the dissimilarity if abs == True: rdms[i, j, k] = limtozero(1 - np.abs(r)) else: rdms[i, j, k] = limtozero(1 - r) elif method == 'euclidean': rdms[i, j, k] = np.linalg.norm(data[j, i] - data[k, i]) """elif method == 'mahalanobis': X = np.transpose(np.vstack((data[j, i], data[k, i])), (1, 0)) X = np.dot(X, np.linalg.inv(np.cov(X, rowvar=False))) rdms[i, j, k] = np.linalg.norm(X[:, 0] - X[:, 1])""" if method == 'euclidean': max = np.max(rdms[i]) min = np.min(rdms[i]) rdms[i] = (rdms[i] - min) / (max - min) if sub_opt == 1: print("\nRDMs computing finished!") return rdms if sub_opt == 0: rdms = np.average(rdms, axis=0) print("\nRDM computing finished!") return rdms
def nps(data, time_win=5, time_step=5, sub_opt=1): """ Calculate the Neural Representational Similarity (NPS) for EEG-like data Parameters ---------- data : array The EEG-like neural data. The shape of data must be [2, n_subs, n_trials, n_chls, n_ts]. 2 presents 2 different conditions. n_subs, n_trials, n_chls & n_ts represent the number of subjects, the number of trials, the number of channels & the number of time-points, respectively. time_win : int. Default is 5. Set a time-window for calculating the NPS for different time-points. If time_win=5, that means each calculation process based on 5 time-points. time_step : int. Default is 5. The time step size for each time of calculating. sub_opt : int 0 or 1. Default is 1. Calculate the NPS for each subject or not. If sub_opt=0, calculate the NPS based on all data. If sub_opt=1, calculate the NPS based on each subject's data Returns ------- nps : array The EEG-like NPS. If sub_opt=0, the shape of NPS is [n_chls, int((n_ts-time_win)/time_step)+1, 2]. If sub_opt=1, the shape of NPS is [n_subs, n_chls, int((n_ts-time_win)/time_step)+1, 2]. 2 representation a r-value and a p-value. """ if len(np.shape(data)) != 5 or np.shape(data)[0] != 2: print( "\nThe shape of input should be [2, n_subs, n_trials, n_chls, n_ts].\n" ) return "Invalid input!" print("\nComputing NPS") # get the number of subjects, trials, channels & time-points nsubs, ntrials, nchls, nts = data.shape[1:] # the time-points for calculating NPS ts = int((nts - time_win) / time_step) + 1 # initialize the NPS nps = np.zeros([nsubs, nchls, ts, 2]) total = nsubs * nchls * ts # [2, n_subs, n_trials, n_chls, n_ts] # calculate the NPS for sub in range(nsubs): for i in range(nchls): for j in range(ts): # show the progressbar percent = (sub * nchls * ts + i * ts + j + 1) / total * 100 show_progressbar("Calculating", percent) data1 = data[0, sub, :, i, j * time_step:j * time_step + time_win] data2 = data[1, sub, :, i, j * time_step:j * time_step + time_win] data1 = np.reshape(data1, [ntrials * time_win]) data2 = np.reshape(data2, [ntrials * time_win]) # calculate the Pearson Coefficient nps[sub, i, j] = pearsonr(data1, data2) # sub_opt=1 if sub_opt == 1: print("\nComputing finished!") return nps elif sub_opt == 0: nps = np.average(nps, axis=0) print("\nComputing finished!") return nps
def rdms_corr(demo_rdm, eeg_rdms, method="spearman", rescale=False, permutation=False, iter=1000): """ Calculate the similarity between RDMs based on RDMs of EEG-like data and a demo RDM Parameters ---------- demo_rdm : array [n_cons, n_cons] A demo RDM. eeg_rdms : array The EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDM(s). The shape can be [n_cons, n_cons] or [n1, n_cons, n_cons] or [n1, n2, n_cons, n_cons] or [n1, n2, n3, n_cons, n_cons]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs. method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'. The method to calculate the similarities. If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the Cosine Similarities. If method='distance', calculate the Euclidean Distances. rescale : bool True or False. Rescale the values in RDM or not. Here, the maximum-minimum method is used to rescale the values except for the values on the diagonal. permutation : bool True or False. Default is False. Use permutation test or not. iter : int. Default is 1000. The times for iteration. Returns ------- corrs : array The similarities between EEG/MEG/fNIRS/ECoG/sEEG/electrophysiological RDMs and a demo RDM If the shape of eeg_rdms is [n_cons, n_cons], the shape of corrs will be [2]. If the shape of eeg_rdms is [n1, n_cons, n_cons], the shape of corrs will be [n1, 2]. If the shape of eeg_rdms is [n1, n2, n_cons, n_cons], the shape of corrs will be [n1, n2, 2]. If the shape of eeg_rdms is [n1, n2, n3, n_cons, n_cons], the shape of corrs will be [n1, n2, n3, 2]. ni(i=1, 2, 3) can be int(n_ts/timw_win), n_chls, n_subs. 2 represents a r-value and a p-value. Notes ----- The demo RDM could be a behavioral RDM, a hypothesis-based coding model RDM or a computational model RDM. """ if len(np.shape(demo_rdm)) != 2 or np.shape(demo_rdm)[0] != np.shape(demo_rdm)[1]: print("\nThe shape of the demo RDM should be [n_cons, n_cons].\n") return "Invalid input!" if len(np.shape(eeg_rdms)) < 2 or len(np.shape(eeg_rdms)) > 5 or np.shape(eeg_rdms)[-1] != np.shape(eeg_rdms)[-2]: print("\nThe shape of the EEG-like RDMs should be [n_cons, n_cons] or [n1, n_cons, n_cons] or " "[n1, n2, n_cons, n_cons] or [n1, n2, n3, n_cons, n_cons].\n") return "Invalid input!" if len(eeg_rdms.shape) == 5: print("\nComputing similarities") n1, n2, n3 = eeg_rdms.shape[:3] # initialize the corrs corrs = np.zeros([n1, n2, n3, 2], dtype=np.float64) total = n1 * n2 * n3 # calculate the corrs for i in range(n1): for j in range(n2): for k in range(n3): # show the progressbar percent = (i * n2 * n2 + j * n3 + k + 1) / total * 100 show_progressbar("Calculating", percent) if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(demo_rdm, eeg_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(demo_rdm, eeg_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(demo_rdm, eeg_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(demo_rdm, eeg_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(demo_rdm, eeg_rdms[i, j, k], rescale=rescale) print("\nComputing finished!") return corrs if len(eeg_rdms.shape) == 4: print("\nComputing similarities") n1, n2 = eeg_rdms.shape[:2] # initialize the corrs corrs = np.zeros([n1, n2, 2], dtype=np.float64) total = n1 * n2 # calculate the corrs for i in range(n1): for j in range(n2): if method == "spearman": corrs[i, j] = rdm_correlation_spearman(demo_rdm, eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(demo_rdm, eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(demo_rdm, eeg_rdms[i, j], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, 0] = rdm_similarity(demo_rdm, eeg_rdms[i, j], rescale=rescale) elif method == "distance": corrs[i, j, 0] = rdm_distance(demo_rdm, eeg_rdms[i, j], rescale=rescale) print("\nComputing finished!") return corrs if len(eeg_rdms.shape) == 3: print("\nComputing similarities") n1 = eeg_rdms.shape[0] # initialize the corrs corrs = np.zeros([n1, 2], dtype=np.float64) # calculate the corrs for i in range(n1): if method == "spearman": corrs[i] = rdm_correlation_spearman(demo_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(demo_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(demo_rdm, eeg_rdms[i], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, 0] = rdm_similarity(demo_rdm, eeg_rdms[i], rescale=rescale) elif method == "distance": corrs[i, 0] = rdm_distance(demo_rdm, eeg_rdms[i], rescale=rescale) print("\nComputing finished!") return corrs print("\nComputing the similarity") # initialize the corrs corr = np.zeros([2], dtype=np.float64) # calculate the corrs if method == "spearman": corr = rdm_correlation_spearman(demo_rdm, eeg_rdms, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corr = rdm_correlation_pearson(demo_rdm, eeg_rdms, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corr = rdm_correlation_kendall(demo_rdm, eeg_rdms, rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corr[0] = rdm_similarity(demo_rdm, eeg_rdms, rescale=rescale) elif method == "distance": corr[0] = rdm_distance(demo_rdm, eeg_rdms, rescale=rescale) print("\nComputing finished!") return corr
def stps_fmri(fmri_data, label_item, label_rf, ksize=[3, 3, 3], strides=[1, 1, 1]): """ Calculate the spatiotemporal pattern similarities (STPS) for fMRI (searchlight) Parameters ---------- fmri_data : array The fMRI data. The shape of fmri_data must be [n_subs, n_trials, nx, ny, nz]. n_subs, n_trials, nx, ny, nz represent the number of subjects, the number of trials & the size of fMRI-img, respectively. label_item : array or list. The label of trials. The shape of label_item must be [n_trials]. n_trials represents the number of trials. label_rf : array or list. The label of trials: Remembered (0) or Forgot (1). The shape of label_rf must be [n_trials]. n_trials represents the number of trials. If the trial i is a remembered trial, label_rf[i]=0. If the trial j is a forgot trial, label_rf[j]=0. ksize : array or list [kx, ky, kz]. Default is [3, 3, 3]. The size of the calculation unit for searchlight. kx, ky, kz represent the number of voxels along the x, y, z axis. kx, ky, kz should be odd. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating along the x, y, z axis. Returns ------- stps : array. The STPS. The shape of stps is [n_subs, 8, n_x, n_y, n_z]. 8 represents eight different conditions: 0: Within-Item, 1: Between-Item, 2: Remembered, 3: Forgot, 4: Within-Item&Remembered, 5: Within-Item&Forgot, 6: Between-Item&Remembered, 7: Between-Item&Forgot. n_x, n_y, n_z represent the number of calculation units for searchlight along the x, y, z axis. Notes ----- The size of the calculation units should at least be [3, 3, 3]. """ if len(np.shape(fmri_data)) != 5: print( "\nThe shape of input should be [n_subs, n_trials, nx, ny, nz].\n") return "Invalid input!" print("\nComputing NPS") # get the number of subjects, trials and the size of the fMRI-img subs, trials, nx, ny, nz = np.shape(fmri_data) # the size of the calculation units for searchlight kx = ksize[0] ky = ksize[1] kz = ksize[2] if kx + ky + kz < 9: print("\nThe size of the calculation units is too small.\n") return "Invalid size of ksize!" # strides for calculating along the x, y, z axis sx = strides[0] sy = strides[1] sz = strides[2] # calculate the number of the calculation units n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 # initialize the STPS stps = np.zeros([subs, 8, n_x, n_y, n_z], dtype=np.float) total = subs * n_x * n_y * n_z for sub in range(subs): # initialize the STPS for each subject sub_stps = np.zeros([8, n_x, n_y, n_z], dtype=np.float) for x in range(n_x): for y in range(n_y): for z in range(n_z): # show the progressbar percent = (sub * n_x * n_y * n_z + x * n_y * n_z + y * n_z + z) / total * 100 show_progressbar("Calculating", percent) trials_data = fmri_data[sub, :, x * sx:x * sx + kx, y * sy:y * sy + ky, z * sz:z * sz + kz] trials_data = np.reshape(trials_data, [trials, kx * ky * kz]) corr_mat = np.zeros([trials, trials], dtype=np.float) index = np.zeros([8], dtype=np.int) for k in range(trials): for l in range(trials): corr_mat[k, l] = pearsonr(trials_data[k], trials_data[l])[0] if k < l: if label_item[k] == label_item[l]: index[0] = index[0] + 1 if label_rf[k] == 0 and label_rf[l] == 0: index[4] = index[4] + 1 if label_rf[k] == 1 and label_rf[l] == 1: index[5] = index[5] + 1 if label_item[k] != label_item[l]: index[1] = index[1] + 1 if label_rf[k] == 0 and label_rf[l] == 0: index[6] = index[6] + 1 if label_rf[k] == 1 and label_rf[l] == 1: index[7] = index[7] + 1 if label_rf[k] == 0 and label_rf[l] == 0: index[2] = index[2] + 1 if label_rf[k] == 1 and label_rf[l] == 1: index[3] = index[3] + 1 r0 = np.zeros([index[0]], dtype=np.float) r1 = np.zeros([index[1]], dtype=np.float) r2 = np.zeros([index[2]], dtype=np.float) r3 = np.zeros([index[3]], dtype=np.float) r4 = np.zeros([index[4]], dtype=np.float) r5 = np.zeros([index[5]], dtype=np.float) r6 = np.zeros([index[6]], dtype=np.float) r7 = np.zeros([index[7]], dtype=np.float) index = np.zeros([8], dtype=np.int) for k in range(trials): for l in range(trials): if k < l: if label_item[k] == label_item[l]: r0[index[0]] = corr_mat[k, l] index[0] = index[0] + 1 if label_rf[k] == 0 and label_rf[l] == 0: r4[index[4]] = corr_mat[k, l] index[4] = index[4] + 1 if label_rf[k] == 1 and label_rf[l] == 1: r5[index[5]] = corr_mat[k, l] index[5] = index[5] + 1 if label_item[k] != label_item[l]: r1[index[1]] = corr_mat[k, l] index[1] = index[1] + 1 if label_rf[k] == 0 and label_rf[l] == 0: r6[index[6]] = corr_mat[k, l] index[6] = index[6] + 1 if label_rf[k] == 1 and label_rf[l] == 1: r7[index[7]] = corr_mat[k, l] index[7] = index[7] + 1 if label_rf[k] == 0 and label_rf[l] == 0: r2[index[2]] = corr_mat[k, l] index[2] = index[2] + 1 if label_rf[k] == 1 and label_rf[l] == 1: r3[index[3]] = corr_mat[k, l] index[3] = index[3] + 1 sub_stps[0, x, y, z] = np.average(r0) sub_stps[1, x, y, z] = np.average(r1) sub_stps[2, x, y, z] = np.average(r2) sub_stps[3, x, y, z] = np.average(r3) sub_stps[4, x, y, z] = np.average(r4) sub_stps[5, x, y, z] = np.average(r5) sub_stps[6, x, y, z] = np.average(r6) sub_stps[7, x, y, z] = np.average(r7) stps[sub] = sub_stps print("\nComputing finished!") return stps
def nps_fmri(fmri_data, ksize=[3, 3, 3], strides=[1, 1, 1]): """ Calculate the Neural Representational Similarity (NPS) for fMRI data (searchlight) Parameters ---------- fmri_data : array The fmri data. The shape of fmri_data must be [2, n_subs, nx, ny, nz]. 2 presents 2 different conditions. nx, ny, nz represent the size of fMRI-img, respectively. ksize : array or list [kx, ky, kz]. Default is [3, 3, 3]. The size of the calculation unit for searchlight. kx, ky, kz represent the number of voxels along the x, y, z axis. kx, ky, kz should be odd. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating along the x, y, z axis. Returns ------- nps : array The fMRI NPS for searchlight. The shape of NPS is [n_subs, n_x, n_y, n_z, 2]. n_subs, n_x, n_y, n_z represent the number of subjects, the number of calculation units for searchlight along the x, y, z axis. 2 represent a r-value and a p-value. Notes ----- The size of the calculation units should at least be [3, 3, 3]. """ if len(np.shape(fmri_data)) != 5 or np.shape(fmri_data)[0] != 2: print("\nThe shape of input should be [2, n_subs, nx, ny, nz].\n") return "Invalid input!" # get the number of subjects and the size of the fMRI-img nsubs, nx, ny, nz = np.shape(fmri_data)[1:] # the size of the calculation units for searchlight kx = ksize[0] ky = ksize[1] kz = ksize[2] if kx + ky + kz < 9: print("\nThe size of the calculation units is too small.\n") return "Invalid size of ksize!" print("\nComputing NPS") # strides for calculating along the x, y, z axis sx = strides[0] sy = strides[1] sz = strides[2] # calculate the number of the calculation units n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 # initialize the data for calculating the NPS data = np.full([n_x, n_y, n_z, 2, kx * ky * kz, nsubs], np.nan) # assignment for x in range(n_x): for y in range(n_y): for z in range(n_z): for i in range(2): # record the index in a calculation unit index = 0 for k1 in range(kx): for k2 in range(ky): for k3 in range(kz): for j in range(nsubs): data[x, y, z, i, index, j] = fmri_data[i, j, x * sx + k1, y * sy + k2, z * sz + k3] index = index + 1 # shape of data: [n_x, n_y, n_z, cons, kx*ky*kz, subs] # ->[subs, n_x, n_y, n_z, cons, kx*ky*kz] data = np.transpose(data, (5, 0, 1, 2, 3, 4)) # flatten the data for different calculating conditions data = np.reshape(data, [nsubs, n_x, n_y, n_z, 2, kx * ky * kz]) # initialize the NPS subnps = np.full([nsubs, n_x, n_y, n_z, 2], np.nan) total = nsubs * n_x * n_y * n_z # calculate the NPS for sub in range(nsubs): for x in range(n_x): for y in range(n_y): for z in range(n_z): # show the progressbar percent = (sub * n_x * n_y * n_z + x * n_y * n_z + y * n_z + z + 1) / total * 100 show_progressbar("Calculating", percent) # no NaN if (np.isnan(data[:, x, y, z, 0]).any() == False) and (np.isnan(data[:, x, y, z, 1]).any() == False): # calculate the Pearson Coefficient and absolute the result subnps[sub, x, y, z] = pearsonr(data[sub, x, y, z, 0], data[sub, x, y, z, 1]) print("\nComputing finished!") return subnps
def stps(data, label_item, label_rf, time_win=20, time_step=1): """ Calculate the spatiotemporal pattern similarities (STPS) for EEG-like data Parameters ---------- data : array The neural data. The shape of data must be [n_subs, n_trials, n_chls, n_ts]. n_subs, n_trials, n_chls and n_ts represent the number of subjects, the number of trials, the number of channels or regions and the number of time-points. label_item : array or list. The label of trials. The shape of label_wibi must be [n_trials]. n_trials represents the number of trials. label_rf : array or list. The label of trials: Remembered (0) or Forgot (1). The shape of label_rf must be [n_trials]. n_trials represents the number of trials. If the trial i is a remembered trial, label_rf[i]=0. If the trial j is a forgot trial, label_rf[j]=0. time_win : int. Default is 20. Set a time-window for calculating the STPS for different time-points. If time_win=20, that means each calculation process based on 20 time-points. time_step : int. Default is 1. The time step size for each time of calculating. Returns ------- stps : array. The STPS. The shape of stps is [n_subs, 8, n_chls, int((n_ts-time_win)/time_step)+1]. 8 represents eight different conditions: 0: Within-Item, 1: Between-Item, 2: Remembered, 3: Forgot, 4: Within-Item&Remembered, 5: Within-Item&Forgot, 6: Between-Item&Remembered, 7: Between-Item&Forgot. """ if len(np.shape(data)) != 4: print( "\nThe shape of input should be [n_subs, n_trials, n_chls, n_ts].\n" ) return "Invalid input!" print("\nComputing NPS") # get the number of subjects, trials, channels/regions & time-points subs, trials, chls, ts = np.shape(data) # the time-points for calculating STPS ts = int((ts - time_win) / time_step) + 1 # initialize the STPS stps = np.zeros([subs, 8, chls, ts], dtype=np.float) total = subs * chls * ts for sub in range(subs): # initialize the STPS for each subject sub_stps = np.zeros([8, chls, ts], dtype=np.float) for i in range(chls): for j in range(ts): # show the progressbar percent = (sub * chls * ts + i * ts + j) / total * 100 show_progressbar("Calculating", percent) trials_data = data[sub, :, i, ts * time_step:ts * time_step + time_win] corr_mat = np.zeros([trials, trials], dtype=np.float) index = np.zeros([8], dtype=np.int) for k in range(trials): for l in range(trials): corr_mat[k, l] = pearsonr(trials_data[k], trials_data[l])[0] if k < l: if label_item[k] == label_item[l]: index[0] = index[0] + 1 if label_rf[k] == 0 and label_rf[l] == 0: index[4] = index[4] + 1 if label_rf[k] == 1 and label_rf[l] == 1: index[5] = index[5] + 1 if label_item[k] != label_item[l]: index[1] = index[1] + 1 if label_rf[k] == 0 and label_rf[l] == 0: index[6] = index[6] + 1 if label_rf[k] == 1 and label_rf[l] == 1: index[7] = index[7] + 1 if label_rf[k] == 0 and label_rf[l] == 0: index[2] = index[2] + 1 if label_rf[k] == 1 and label_rf[l] == 1: index[3] = index[3] + 1 r0 = np.zeros([index[0]], dtype=np.float) r1 = np.zeros([index[1]], dtype=np.float) r2 = np.zeros([index[2]], dtype=np.float) r3 = np.zeros([index[3]], dtype=np.float) r4 = np.zeros([index[4]], dtype=np.float) r5 = np.zeros([index[5]], dtype=np.float) r6 = np.zeros([index[6]], dtype=np.float) r7 = np.zeros([index[7]], dtype=np.float) index = np.zeros([8], dtype=np.int) for k in range(trials): for l in range(trials): if k < l: if label_item[k] == label_item[l]: r0[index[0]] = corr_mat[k, l] index[0] = index[0] + 1 if label_rf[k] == 0 and label_rf[l] == 0: r4[index[4]] = corr_mat[k, l] index[4] = index[4] + 1 if label_rf[k] == 1 and label_rf[l] == 1: r5[index[5]] = corr_mat[k, l] index[5] = index[5] + 1 if label_item[k] != label_item[l]: r1[index[1]] = corr_mat[k, l] index[1] = index[1] + 1 if label_rf[k] == 0 and label_rf[l] == 0: r6[index[6]] = corr_mat[k, l] index[6] = index[6] + 1 if label_rf[k] == 1 and label_rf[l] == 1: r7[index[7]] = corr_mat[k, l] index[7] = index[7] + 1 if label_rf[k] == 0 and label_rf[l] == 0: r2[index[2]] = corr_mat[k, l] index[2] = index[2] + 1 if label_rf[k] == 1 and label_rf[l] == 1: r3[index[3]] = corr_mat[k, l] index[3] = index[3] + 1 sub_stps[0, i, j] = np.average(r0) sub_stps[1, i, j] = np.average(r1) sub_stps[2, i, j] = np.average(r2) sub_stps[3, i, j] = np.average(r3) sub_stps[4, i, j] = np.average(r4) sub_stps[5, i, j] = np.average(r5) sub_stps[6, i, j] = np.average(r6) sub_stps[7, i, j] = np.average(r7) stps[sub] = sub_stps print("\nComputing finished!") return stps
def ctRDM(data, sub_opt=1, chl_opt=0, time_win=5, time_step=5): """ Calculate CTRDMs for EEG-like data Parameters ---------- data : array EEG/MEG data from a time-window. The shape of data must be [n_cons, n_subs, n_chls, n_ts]. n_cons, n_subs, n_chls & n_ts represent the number of conditions, the number of subjects, the number of channels and the number of time-points, respectively. sub_opt : int 0 or 1. Default is 1. Return the subject-result or average-result. If sub_opt=0, return the average result. If sub_opt=1, return the results of each subject. chl_opt : int 0 or 1. Default is 0. Caculate the CTRDMs for each channel or not. If chl_opt=1, calculate the CTRDMs for each channel. If chl_opt=0, calculate the CTRDMs after averaging the channels. time_win : int. Default is 5. Set a time-window for calculating the CTRDM for different time-points. If time_win=10, that means each calculation process based on 10 time-points. time_step : int. Default is 5. The time step size for each time of calculating. Returns ------- CTRDMs : array Cross-Temporal RDMs. if chl_opt=1, the shape of CTRDMs is [n_subs, n_chls, int((n_ts-time_win)/time_step)+1, int((n_ts-time_win)/time_step)+1, n_cons, n_cons] if chl_opt=0, the shape of CTRDMs is [n_subs, int((n_ts-time_win)/time_step)+1, int((n_ts-time_win)/time_step)+1, n_cons, n_cons] """ n_cons, n_subs, n_chls, n_ts = np.shape(data) nts = int((n_ts - time_win) / time_step) + 1 data_for_cal = np.zeros([n_cons, n_subs, nts, n_chls, time_win], dtype=np.float) for con in range(n_cons): for sub in range(n_subs): for t in range(nts): for chl in range(n_chls): data_for_cal[con, sub, t, chl] = data[con, sub, chl, t * time_step:t * time_step + time_win] # chl_opt=0 if chl_opt == 0: data_for_cal = np.reshape(data_for_cal, [n_cons, n_subs, nts, n_chls * time_win]) ctrdms = np.zeros([n_subs, nts, nts, n_cons, n_cons], dtype=np.float) total = n_subs * nts * nts for sub in range(n_subs): for t1 in range(nts): for t2 in range(nts): # show the progressbar percent = (sub * nts * nts + t1 * nts + t2 + 1) / total * 100 show_progressbar("Calculating", percent) for con1 in range(n_cons): for con2 in range(n_cons): if con1 != con2: r = pearsonr(data_for_cal[con1, sub, t1], data_for_cal[con2, sub, t2])[0] ctrdms[sub, t1, t2, con1, con2] = 1 - r if con1 == con2: ctrdms[sub, t1, t2, con1, con2] = 0 # chl_opt=0 & sub_opt=0 if sub_opt == 0: print("\nCross-temporal RDMs computing finished!") return np.average(ctrdms, axis=0) # chl_opt=0 & sub_opt=1 else: print("\nCross-temporal RDMs computing finished!") return ctrdms # chl_opt=1 else: ctrdms = np.zeros([n_subs, n_chls, nts, nts, n_cons, n_cons], dtype=np.float) total = n_subs * n_chls * nts * nts for sub in range(n_subs): for chl in range(n_chls): for t1 in range(nts): for t2 in range(nts): # show the progressbar percent = (sub * nts * nts + t1 * nts + t2 + 1) / total * 100 show_progressbar("Calculating", percent) for con1 in range(n_cons): for con2 in range(n_cons): if con1 != con2: r = pearsonr( data_for_cal[con1, sub, t1, chl], data_for_cal[con2, sub, t2, chl])[0] ctrdms[sub, chl, t1, t2, con1, con2] = 1 - r if con1 == con2: ctrdms[sub, chl, t1, t2, con1, con2] = 0 # chl_opt=1 & sub_opt=0 if sub_opt == 0: print("\nCross-temporal RDMs computing finished!") return np.average(ctrdms, axis=0) # chl_opt=1 & sub_opt=1 else: print("\nCross-temporal RDMs computing finished!") return ctrdms
def isc_fmri(fmri_data, ksize=[3, 3, 3], strides=[1, 1, 1]): """ Calculate the inter subject correlation (ISC) for fMRI (searchlight) Parameters ---------- fmri_data : array The fmri data. The shape of fmri_data must be [n_ts, n_subs, nx, ny, nz]. n_ts, nx, ny, nz represent the number of time-points, the number of subs & the size of fMRI-img, respectively. ksize : array or list [kx, ky, kz]. Default is [3, 3, 3]. The size of the calculation unit for searchlight. kx, ky, kz represent the number of voxels along the x, y, z axis. kx, ky, kz should be odd. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating along the x, y, z axis. Returns ------- isc : array The ISC. The shape of isc is [n_ts, n_subs!/(2!*(n_subs-2)!), n_x, n_y, n_z, 2]. n_ts, n_subs, n_x, n_y, n_z represent the number of time-points, the number of subjects, the number of calculation units for searchlight along the x, y, z axis. 2 represent a r-value and a p-value. Notes ----- The size of the calculation units should at least be [3, 3, 3]. In ISC, correlation computing process will be done for each pair of subjects. """ if len(np.shape(fmri_data)) != 5: print("\nThe shape of input should be [n_ts, n_subs, nx, ny, nz].\n") return "Invalid input!" # get the number of time-points, subjects and the size of the fMRI-img nts, nsubs, nx, ny, nz = np.shape(fmri_data) # the size of the calculation units for searchlight kx = ksize[0] ky = ksize[1] kz = ksize[2] if kx+ky+kz < 9: print("\nThe size of the calculation units is too small.\n") return "Invalid size of ksize!" # strides for calculating along the x, y, z axis sx = strides[0] sy = strides[1] sz = strides[2] # calculate the number of the calculation units n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 print("\nISC starts") # initialize the data for calculating the ISC data = np.full([nts, nsubs, n_x, n_y, n_z, kx * ky * kz], np.nan) # assignment for t in range(nts): for sub in range(nsubs): for x in range(n_x): for y in range(n_y): for z in range(n_z): # record the index in a calculation unit index = 0 for k1 in range(kx): for k2 in range(ky): for k3 in range(kz): data[t, sub, x, y, z, index] = fmri_data[t, sub, x*sx + k1, y*sy + k2, z*sz + k3] index = index + 1 # the number of pairs among n_subs if nsubs > 2: n = int(math.factorial(nsubs) / (2 * math.factorial(nsubs - 2))) if nsubs == 2: n = 1 # initialize the ISC subisc = np.full([nts, n, n_x, n_y, n_z, 2], np.nan) total = nts * n * n_x * n_y * n_z # calculate the ISC for t in range(nts): nindex = 0 for i in range(nsubs): for j in range(nsubs): if i < j: for x in range(n_x): for y in range(n_y): for z in range(n_z): # show the progressbar percent = (t * n * n_x * n_y * n_z + nindex * n_x * n_y * n_z + x * n_y * n_z + y * n_z + z + 1) / total * 100 show_progressbar("Calculating", percent) # no NaN if (np.isnan(data[t, i, x, y, z]).any() == False) and \ (np.isnan(data[t, j, x, y, z]).any() == False): # calculate the Pearson Coefficient and absolute the result subisc[t, nindex, x, y, z] = pearsonr(data[t, i, x, y, z], data[t, j, x, y, z]) nindex = nindex + 1 print("\nComputing finished!") return subisc
def isc(data, time_win=5, time_step=5): """ Calculate the inter subject correlation (ISC) for EEG-like data Parameters ---------- data : array The neural data. The shape of data must be [n_subs, n_chls, n_ts]. n_subs, n_chls, n_ts represent the number of subjects, the number of channels and the number of time-points. time_win : int. Default is 5. Set a time-window for calculating the STPS for different time-points. If time_win=5, that means each calculation process based on 5 time-points. time_step : int. Default is 5. The time step size for each time of calculating. Returns ------- isc : array The ISC. The shape of isc is [n_subs!/(2!*(n_subs-2)!), n_chls, int((n_ts-time_win)/time_step)+1, 2]. n_subs, n_chls, n_ts represent the number of subjects, the number of channels and the number of time-points. 2 represents a r-value and a p-value. Notes ----- In ISC, correlation computing process will be done for each pair of subjects. """ if len(np.shape(data)) != 3: print("\nThe shape of input should be [n_subs, n_chls, n_ts].\n") return "Invalid input!" print("\nISC starts") # get the number of subjects, channels, time-points subs, chls, ts = np.shape(data) # the time-points for calculating the ISC ts = int((ts - time_win) / time_step) + 1 # the number of pairs among n_subs if subs > 2: n = int(math.factorial(subs)/(2*math.factorial(subs-2))) if subs == 2: n = 1 # initialize the corrs isc = np.zeros([n, chls, ts, 2], dtype=np.float) total = n * chls * ts nindex = 0 # calculate the ISC for i in range(subs): for j in range(subs): if i < j: for k in range(chls): for l in range(ts): # show the progressbar percent = (nindex * chls * ts + k * ts + l + 1) / total * 100 show_progressbar("Calculating", percent) rp = pearsonr(data[i, k, l*time_step:l*time_step+time_win], data[j, k, l*time_step:l*time_step+time_win]) isc[nindex, k, l] = rp nindex = nindex + 1 print("\nComputing finished!") return isc
def ctsim_ctrdms_cal(CTRDMs, Model_RDM, method='spearman'): """ Calculate the Cross-Temporal Similarities between CTRDMs and a Coding Model RDM Parameters ---------- CTRDMs : array The Cross-Temporal Representational Dissimilarity Matrices. The shape could be [n_ts, n_ts, n_cons, n_cons] or [n_subs, n_ts, n_ts, n_cons, n_cons] or [n_chls, n_ts, n_ts, n_cons, n_cons] or [n_subs, n_chls, n_ts, n_ts, n_cons, n_cons]. n_ts, n_cons, n_subs, n_chls represent the number of time-points, the number of conditions, the number of subjects and the number of channels, respectively. Model_RDM : array [n_cons, n_cons]. The Coding Model RDM. method : string 'spearman' or 'pearson' or 'kendall' or 'similarity' or 'distance'. Default is 'spearman'. The method to calculate the similarities. If method='spearman', calculate the Spearman Correlations. If method='pearson', calculate the Pearson Correlations. If methd='kendall', calculate the Kendall tau Correlations. If method='similarity', calculate the Cosine Similarities. If method='distance', calculate the Euclidean Distances. Returns ------- CTSimilarities : array Cross-temporal similarities. If method='spearman' or 'pearson' or 'kendall': If the shape of CTRDMs is [n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_ts, n_ts, 2]. If the shape of CTRDMs is [n_subs, n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_subs, n_ts, n_ts, 2]. If the shape of CTRDMs is [n_channels, n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_channels, n_ts, n_ts, 2]. If the shape of CTRDMs is [n_subs, n_channels, n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_subs, n_channels, n_ts, n_ts, 2]. If method='similarity' or 'distance': If the shape of CTRDMs is [n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_ts, n_ts]. If the shape of CTRDMs is [n_subs, n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_subs, n_ts, n_ts]. If the shape of CTRDMs is [n_channels, n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_channels, n_ts, n_ts]. If the shape of CTRDMs is [n_subs, n_channels, n_ts, n_ts, n_cons, n_cons], the shape of CTSimilarities will be [n_subs, n_channels, n_ts, n_ts]. """ n = len(np.shape(CTRDMs)) if n == 4: n_ts, n_cons = np.shape(CTRDMs)[1:3] CTSimilarities = np.zeros([n_ts, n_ts, 2], dtype=np.float) total = n_ts * n_ts for t1 in range(n_ts): for t2 in range(n_ts): percent = (t1 * n_ts + t2) / total * 100 show_progressbar("Calculating", percent) if method == 'spearman': CTSimilarities[t1, t2] = ctrdm_correlation_spearman( CTRDMs[t1, t2], Model_RDM) if method == 'pearson': CTSimilarities[t1, t2] = ctrdm_correlation_pearson( CTRDMs[t1, t2], Model_RDM) if method == 'kendall': CTSimilarities[t1, t2] = ctrdm_correlation_kendall( CTRDMs[t1, t2], Model_RDM) if method == 'similarity': CTSimilarities[t1, t2, 0] = ctrdm_similarity( CTRDMs[t1, t2], Model_RDM) if method == 'distance': CTSimilarities[t1, t2, 0] = ctrdm_distance(CTRDMs[t1, t2], Model_RDM) if method == 'spearman' or method == 'pearson' or method == 'kendall': return CTSimilarities if method == 'similarity' or method == 'distance': return CTSimilarities[:, :, 0] if n == 5: n1 = np.shape(CTRDMs)[0] n_ts, n_cons = np.shape(CTRDMs)[2:4] CTSimilarities = np.zeros([n1, n_ts, n_ts, 2], dtype=np.float) total = n1 * n_ts * n_ts for i in range(n1): for t1 in range(n_ts): for t2 in range(n_ts): percent = (i * n_ts * n_ts + t1 * n_ts + t2) / total * 100 show_progressbar("Calculating", percent) if method == 'spearman': CTSimilarities[i, t1, t2] = ctrdm_correlation_spearman( CTRDMs[i, t1, t2], Model_RDM) #print(CTSimilarities[i, t1, t2]) if method == 'pearson': CTSimilarities[i, t1, t2] = ctrdm_correlation_pearson( CTRDMs[i, t1, t2], Model_RDM) if method == 'kendall': CTSimilarities[i, t1, t2] = ctrdm_correlation_kendall( CTRDMs[i, t1, t2], Model_RDM) if method == 'similarity': CTSimilarities[i, t1, t2, 0] = ctrdm_similarity( CTRDMs[i, t1, t2], Model_RDM) if method == 'distance': CTSimilarities[i, t1, t2, 0] = ctrdm_distance( CTRDMs[i, t1, t2], Model_RDM) if method == 'spearman' or method == 'pearson' or method == 'kendall': return CTSimilarities if method == 'similarity' or method == 'distance': return CTSimilarities[:, :, :, 0] if n == 6: n1, n2 = np.shape(CTRDMs)[:2] n_ts, n_cons = np.shape(CTRDMs)[3:5] CTSimilarities = np.zeros([n1, n2, n_ts, n_ts, 2], dtype=np.float) total = n1 * n2 * n_ts * n_ts for i in range(n1): for j in range(n2): for t1 in range(n_ts): for t2 in range(n_ts): percent = (i * n2 * n_ts * n_ts + j * n_ts * n_ts + t1 * n_ts + t2) / total * 100 show_progressbar("Calculating", percent) if method == 'spearman': CTSimilarities[i, j, t1, t2] = ctrdm_correlation_spearman( CTRDMs[i, j, t1, t2], Model_RDM) if method == 'pearson': CTSimilarities[i, j, t1, t2] = ctrdm_correlation_pearson( CTRDMs[i, j, t1, t2], Model_RDM) if method == 'kendall': CTSimilarities[i, j, t1, t2] = ctrdm_correlation_kendall( CTRDMs[i, j, t1, t2], Model_RDM) if method == 'similarity': CTSimilarities[i, j, t1, t2, 0] = ctrdm_similarity( CTRDMs[i, j, t1, t2], Model_RDM) if method == 'distance': CTSimilarities[i, j, t1, t2, 0] = ctrdm_distance( CTRDMs[i, j, t1, t2], Model_RDM) if method == 'spearman' or method == 'pearson' or method == 'kendall': return CTSimilarities if method == 'similarity' or method == 'distance': return CTSimilarities[:, :, :, :, 0]