def test_eegRDM(self): eeg_data = np.random.rand(8, 10, 15, 32, 50) rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=0, time_opt=0) self.assertEqual(rdms.shape[0], 8) rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=0, time_opt=1) self.assertEqual(rdms.shape[0], 10) rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=1, time_opt=0) self.assertEqual(rdms.shape[0], 32) rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=1, time_opt=1) self.assertEqual(rdms.shape[0], 32) rdms = eegRDM(eeg_data, sub_opt=1, chl_opt=0, time_opt=0) self.assertEqual(rdms.shape[0], 10) rdms = eegRDM(eeg_data, sub_opt=1, chl_opt=0, time_opt=1) self.assertEqual(rdms.shape[0], 10) rdms = eegRDM(eeg_data, sub_opt=1, chl_opt=1, time_opt=0) self.assertEqual(rdms.shape[0], 10) rdms = eegRDM(eeg_data, sub_opt=1, chl_opt=1, time_opt=1) self.assertEqual(rdms.shape[0], 10) eeg_data = np.random.rand(8, 10, 15, 32) output = bhvRDM(eeg_data) self.assertEqual(output, "Invalid input!")
nps = nps(nps_data, time_win=10, time_step=10) # Plot the NPS results plot_nps_hotmap(nps[:, :, 0], time_unit=[0, 0.01]) # Smooth the results and plot plot_nps_hotmap(nps[:, :, 0], time_unit=[0, 0.01], smooth=True) # 加上轮廓 rsa_plot.plot_stats_hotmap(nps, time_unit=[0, 0.01], smooth=True, outline=True) #%% """********** Section 4: Calculating single RDM and Plotting **********""" # Calculate the RDM based on the data during 190ms-210ms rdm = eegRDM(megdata[:, :, :, :, 290:310]) # Plot this RDM plot_rdm(rdm, rescale=True) #%% """********** Section 5: Calculating RDMs and Plotting **********""" # Calculate the RDMs by a 10ms time-window # (raw sampling requency is 1000Hz, so here time_win=10ms/(1s/1000Hz)/1000=10) rdms = eegRDM(megdata, time_opt=1, time_win=10, time_step=10) # Plot the RDM of 0ms, 50ms, 100ms, 150ms, 200ms times = [10, 20, 30, 40, 50] for t in times: plot_rdm(rdms[t], rescale=True)
def eegANDfmri_corr(eeg_data, fmri_data, chl_opt=0, ksize=[3, 3, 3], strides=[1, 1, 1], sub_result=1, method="spearman", fisherz=False, rescale=False, permutation=False, iter=5000): """ 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. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating along the x, y, z axis. sub_result: int 0 or 1. Default is 1. Return the subject-result or average-result. If sub_result=0, return the average result. If sub_result=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. fisherz : bool True or False. Default is False. Do the Fisher-Z transform of the RDMs or not. 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 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_result=sub_result, ksize=ksize, strides=strides) print(fmri_rdms.shape) # calculate the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=sub_result, chl_opt=chl_opt) # chl_opt=1 if chl_opt == 1: # sub_result=1 if sub_result == 1: # chl_opt=1 & sub_result=1 # initialize the corrs corrs = np.full([subs, chls, n_x, n_y, n_z, 2], np.nan) # 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): if method == "spearman": corrs[sub, i, j, k, l] = rdm_correlation_spearman(eeg_rdms[sub, i], fmri_rdms[sub, j, k, l], fisherz=fisherz, 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], fisherz=fisherz, 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], fisherz=fisherz, 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) return np.abs(corrs) # sub_result=0 # chl_opt=1 & sub_result=0 # initialize the corrs corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan) # 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): if method == "spearman": corrs[i, j, k, l] = rdm_correlation_spearman(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz, 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], fisherz=fisherz, 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], fisherz=fisherz, 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) return np.abs(corrs) # chl_opt=0 # sub_result=1 if sub_result == 1: # chl_opt=0 & sub_result=1 # initialize the corrs corrs = np.full([subs, n_x, n_y, n_z, 2], np.nan) # 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): if method == "spearman": corrs[i, j, k, l] = rdm_correlation_spearman(eeg_rdms[i], fmri_rdms[j, k, l], fisherz=fisherz, 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], fisherz=fisherz, 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], fisherz=fisherz, 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) return corrs # sub_result=0 # chl_opt=0 & sub_result=0 # initialize the corrs corrs = np.full([n_x, n_y, n_z, 2], np.nan) # calculate the corrs for i in range(n_x): for j in range(n_y): for k in range(n_z): if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(eeg_rdms, fmri_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(eeg_rdms, fmri_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(eeg_rdms, fmri_rdms[i, j, k], fisherz=fisherz, 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) return np.abs(corrs)
def bhvANDeeg_corr(bhv_data, eeg_data, sub_opt=1, chl_opt=0, time_opt=0, time_win=5, time_step=5, method="spearman", fisherz=False, 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. fisherz : bool True or False. Default is False. Do the Fisher-Z transform of the RDMs or not. 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 # 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], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i], fisherz=fisherz, 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) return corrs # sub_opt=1 & chl_opt=0 & time_opt=1 # 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) # calculate the corrs for i in range(subs): for j in range(ts): if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, 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) return corrs if time_opt == 1: # sub_opt=1 & chl_opt=1 & time_opt=1 # 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) # calculate the corrs for i in range(subs): for j in range(chls): for k in range(ts): if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j, k], fisherz=fisherz, 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) return corrs # sub_opt=1 & chl_opt=1 & time_opt=0 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], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], fisherz=fisherz, 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) 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 # 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) # calculate the corrs for i in range(chls): for j in range(ts): if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i, j], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i, j], fisherz=fisherz, 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 # 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], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], fisherz=fisherz, 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) return corrs # if chl_opt=0 if time_opt == 1: # sub_opt=0 & chl_opt=0 & time_opt=1 # 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], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], fisherz=fisherz, 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) return corrs # sub_opt=0 & chl_opt=0 & time_opt=0 # 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, fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corr = rdm_correlation_pearson(bhv_rdm, eeg_rdm, fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corr = rdm_correlation_kendall(bhv_rdm, eeg_rdm, fisherz=fisherz, 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) return corr
for sub in subs: print('Loading data of sub' + sub) f = h5py.File(data_dir + 'data_for_RSA/ERP/' + sub + '.h5', 'r') ori_subdata = np.array(f['ori']) pos_subdata = np.array(f['pos']) f.close() data_ori_ERP[:, subindex] = ori_subdata data_pos_ERP[:, subindex] = pos_subdata subindex = subindex + 1 # calculate the RDMs print("\nCalculate the Orientation RDMs!") RDM_ori_ERP = eegRDM(data_ori_ERP, sub_opt=1, chl_opt=0, time_opt=1, time_win=5, time_step=5) print("\nCalculate the Position RDMs!") RDM_pos_ERP = eegRDM(data_pos_ERP, sub_opt=1, chl_opt=0, time_opt=1, time_win=5, time_step=5) # shape of RDMs: [5, 100, 16, 16] # establish a Coding RDM model_RDM = np.zeros([16, 16], dtype=np.float) for i in range(16): for j in range(16):
# -*- coding: utf-8 """ @File : rdm_cal.py @Author : Zitong Lu @Contact : [email protected] @License : MIT License """ import numpy as np from neurora.rdm_cal import eegRDM for sub in range(6): data = np.loadtxt("../eegdata/alpha_power_po/sub" + str(sub + 1) + ".txt") data = np.reshape(data, [18, 1, 1, 17, 875]) eegrdms = eegRDM(data, time_opt=1, time_win=25, time_step=1) eegrdms = np.reshape(eegrdms, [851, 18 * 18]) np.savetxt("../eegrdm/alpha_power_po/sub" + str(sub + 1) + ".txt", eegrdms)
def eegANDfmri_corr(eeg_data, fmri_data, chl_opt=0, ksize=[3, 3, 3], strides=[1, 1, 1], method="spearman", rescale=False): """ 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, n_chls, nx, ny, nz]. n_cons, n_chls, nx, ny, nz represent the number of conidtions, the number of channels & 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 fMRI-img. nx, ny, nz represent the number of voxels along the x, y, z axis. strides : array or list [sx, sy, sz]. Default is [1, 1, 1]. The strides for calculating 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. Returns ------- corrs : array The similarities between EEG/MEG/fNIRS data and fMRI data for searchlight. If chl_opt=1, the shape of RDMs 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, 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. """ # 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 # calculate the fmri_rdms for searchlight fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides) # chl_opt=1 if chl_opt == 1: # get the number of channels chls = np.shape(eeg_data)[3] # get the eeg_rdms eeg_rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=1, time_opt=0) # initialize the corrs corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan) # 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): if method == "spearman": corrs[i, j, k, l] = rdm_correlation_spearman( eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) elif method == "pearson": corrs[i, j, k, l] = rdm_correlation_pearson(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) elif method == "kendall": corrs[i, j, k, l] = rdm_correlation_kendall(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) 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) return np.abs(corrs) # chl_opt=0 # get the eeg_rdms eeg_rdm = eegRDM(eeg_data, sub_opt=0, chl_opt=0, time_opt=0) # initialize the corrs corrs = np.full([n_x, n_y, n_z, 2], np.nan) # calculate the corrs for i in range(n_x): for j in range(n_y): for k in range(n_z): if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) return np.abs(corrs)
def eegANDfmri_corr(eeg_data, fmri_data, chl_opt=0, ksize=[3, 3, 3], strides=[1, 1, 1], method="spearman", rescale=False): # sub_opt=0, time_opt=0 nx = np.shape(fmri_data)[2] ny = np.shape(fmri_data)[3] nz = np.shape(fmri_data)[4] cons = np.shape(eeg_data)[0] kx = ksize[0] ky = ksize[1] kz = ksize[2] sx = strides[0] sy = strides[1] sz = strides[0] n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides) if chl_opt == 1: chls = np.shape(eeg_data)[3] eeg_rdms = eegRDM(eeg_data, sub_opt=0, chl_opt=1, time_opt=0) corrs = np.full([chls, n_x, n_y, n_z, 2], np.nan) for j in range(n_x): for k in range(n_y): for l in range(n_z): """index = 0 for m in range(cons): for n in range(cons): if math.isnan(fmri_rdms[j, k, l]) == True: index = index + 1""" for i in range(chls): """if index != 0: corrs[i, j, k, l, 0] = 0 corrs[i, j, k, l, 1] = 1""" if method == "spearman": corrs[i, j, k, l] = rdm_correlation_spearman( eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) elif method == "pearson": corrs[i, j, k, l] = rdm_correlation_pearson(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) elif method == "kendall": corrs[i, j, k, l] = rdm_correlation_kendall(eeg_rdms[i], fmri_rdms[j, k, l], rescale=rescale) 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) return np.abs(corrs) # if chl_opt=0 eeg_rdm = eegRDM(eeg_data, sub_opt=0, chl_opt=0, time_opt=0) corrs = np.full([n_x, n_y, n_z, 2], np.nan) for i in range(n_x): for j in range(n_y): for k in range(n_z): """index = 0 for m in range(cons): for n in range(cons): if math.isnan(fmri_rdms[i, j, k]) == True: index = index + 1 if index != 0: corrs[i, j, k, 0] = 0 corrs[i, j, k, 1] = 1""" if method == "spearman": corrs[i, j, k] = rdm_correlation_spearman(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(eeg_rdm, fmri_rdms[i, j, k], rescale=rescale) return np.abs(corrs)
def bhvANDeeg_corr(bhv_data, EEG_data, sub_opt=0, bhv_data_opt=1, time_win=5, chl_opt=0, time_opt=0, method="spearman", rescale=False): subs = np.shape(bhv_data)[1] chls = np.shape(EEG_data)[3] ts = int(np.shape(EEG_data)[4] / time_win) if sub_opt == 1: if bhv_data_opt == 0: return None # if bhv_data_opt=1 bhv_rdms = bhvRDM(bhv_data, sub_opt=sub_opt, data_opt=bhv_data_opt) if chl_opt == 0: if time_opt == 0: eeg_rdms = eegRDM(EEG_data, time_win=time_win, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) corrs = np.zeros([subs, 2], dtype=np.float64) for i in range(subs): if method == "spearman": corrs[i] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i], rescale=rescale) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i], rescale=rescale) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i], rescale=rescale) 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) return corrs # if time_opt=1 eeg_rdms = eegRDM(EEG_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) corrs = np.zeros([subs, ts, 2], dtype=np.float64) for i in range(subs): for j in range(ts): if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) 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) return corrs # chl_opt=1 if time_opt == 1: return None # time_opt=0 eeg_rdms = eegRDM(EEG_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) corrs = np.zeros([subs, chls], 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) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdms[i], eeg_rdms[i, j], rescale=rescale) 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) return corrs # if sub_opt=0 bhv_rdm = bhvRDM(bhv_data, sub_opt=sub_opt, data_opt=bhv_data_opt) if chl_opt == 1: if time_opt == 1: eeg_rdms = eegRDM(EEG_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) corrs = np.zeros([chls, ts, 2], dtype=np.float64) for i in range(chls): for j in range(ts): if method == "spearman": corrs[i, j] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i, j], rescale=rescale) elif method == "pearson": corrs[i, j] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i, j], rescale=rescale) elif method == "kendall": corrs[i, j] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i, j], rescale=rescale) 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 # if time_opt=0 eeg_rdms = eegRDM(EEG_data, sub_opt=sub_opt, chl_opt=chl_opt, time_opt=time_opt) corrs = np.zeros([chls, 2], dtype=np.float64) for i in range(chls): if method == "spearman": corrs[i] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "similarity": corrs[i, 0] = rdm_similarity(bhv_rdm, eeg_rdms[i]) elif method == "distance": corrs[i, 0] = rdm_distance(bhv_rdm, eeg_rdms[i]) return corrs # if chl_opt=0 if time_opt == 1: eeg_rdms = eegRDM(EEG_data, sub_opt=0, chl_opt=0, time_opt=1) corrs = np.zeros([ts, 2], dtype=np.float64) for i in range(ts): if method == "spearman": corrs[i] = rdm_correlation_spearman(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "pearson": corrs[i] = rdm_correlation_pearson(bhv_rdm, eeg_rdms[i], rescale=rescale) elif method == "kendall": corrs[i] = rdm_correlation_kendall(bhv_rdm, eeg_rdms[i], rescale=rescale) 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) return corrs # if time_opt=0 eeg_rdm = eegRDM(EEG_data, sub_opt=0, chl_opt=0, time_opt=0) corr = np.zeros([2], dtype=np.float64) if method == "spearson": corr = rdm_correlation_spearman(bhv_rdm, eeg_rdm, rescale=rescale) elif method == "pearson": corr = rdm_correlation_pearson(bhv_rdm, eeg_rdm, rescale=rescale) elif method == "kendall": corr = rdm_correlation_kendall(bhv_rdm, eeg_rdm, rescale=rescale) 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) return corr
megdata[subindex] = subdata subindex = subindex + 1 # the shape of MEG data: megdata is [3, 92, 306, 1101] # n_subs = 3, n_conditions = 92, n_channels = 306, n_timepoints = 1101 (-100ms to 1000ms) """********** Section 2: Calculating single RDM and Plotting **********""" # shape of megdata: [n_subs, n_cons, n_chls, n_ts] -> [n_cons, n_subs, n_chls, n_ts] megdata = np.transpose(megdata, (1, 0, 2, 3)) # shape of megdata: [n_cons, n_subs, n_chls, n_ts] -> [n_cons, n_subs, n_trials, n_chls, n_ts] # here data is averaged, so set n_trials = 1 megdata = np.reshape(megdata, [92, 3, 1, 306, 1101]) # Calculate the RDM based on the data during 190ms-210ms rdm = eegRDM(megdata[:, :, :, :, 290:310]) # Plot this RDM plot_rdm(rdm, rescale=True) """********** Section 3: Calculating RDMs and Plotting **********""" # Calculate the RDMs by a 10ms time-window # (raw sampling requency is 1000Hz, so here time_win=10ms/(1s/1000Hz)/1000=10) rdms = eegRDM(megdata, time_win=10, time_opt=1) # Plot the RDM of 0ms, 50ms, 100ms, 150ms, 200ms times = [0, 10, 20, 30, 40, 50] for t in times: plot_rdm(rdms[t], rescale=True) """********** Section 4: Calculating the Similarity between two RDMs **********"""