def test_fmriRDM(self): fmri_data = np.random.rand(8, 10, 13, 23, 12) rdms = fmriRDM(fmri_data, sub_result=0) self.assertEqual(rdms.shape[0], 11) rdms = fmriRDM(fmri_data, sub_result=1) self.assertEqual(rdms.shape[0], 10)
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 bhvANDfmri_corr(bhv_data, fmri_data, 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 behavioral data and fMRI data for searchlight 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. 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. 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 behavioral data and fMRI data for searchlight. If sub_result=0, the shape of corrs is [n_x, n_y, n_z, 2]. If sub_result=1, the shape of corrs is [n_subs, 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(bhv_data)) != 3 or len(np.shape(fmri_data)) != 5: return "Invalid input!" # calculate the bhv_rdm bhv_rdm = bhvRDM(bhv_data, sub_opt=0) print("****************") print("get behavior RDM") print(bhv_rdm) # calculate the fmri_rdms for searchlight fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides, sub_result=sub_result) print("****************") print("get fMRI RDM") print(np.shape(fmri_rdms)) # 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[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 # sub_result=0 if 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(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) print(corrs[i, j, k]) return corrs # sub_result=1 # initialize the corrs corrs = np.full([subs, n_x, n_y, n_z, 2], np.nan) # calculate the corrs for sub in range(subs): for i in range(n_x): for j in range(n_y): for k in range(n_z): if method == "spearman": corrs[sub, i, j, k] = rdm_correlation_spearman(bhv_rdm, fmri_rdms[sub, i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "pearson": corrs[sub, i, j, k] = rdm_correlation_pearson(bhv_rdm, fmri_rdms[sub, i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "kendall": corrs[sub, i, j, k] = rdm_correlation_kendall(bhv_rdm, fmri_rdms[sub, i, j, k], fisherz=fisherz, rescale=rescale, permutation=permutation, iter=iter) elif method == "similarity": corrs[sub, i, j, k, 0] = rdm_similarity(bhv_rdm, fmri_rdms[sub, i, j, k], rescale=rescale) elif method == "distance": corrs[sub, i, j, k, 0] = rdm_distance(bhv_rdm, fmri_rdms[sub, i, j, k], rescale=rescale) print(corrs[sub, i, j, k]) return corrs
plotting.show() """********** Section 5: Calculating the RDM for ROI and Plotting **********""" # get mask of "mask_vt" in the dataset mask_vt_filename = haxby_dataset.mask_face[0] mask_vt_data = nib.load(mask_vt_filename).get_data() # calculate the RDM for ROI rdm_roi = fmriRDM_roi(fmri_data, mask_vt_data) # plot the RDM plot_rdm(rdm_roi, rescale=True, conditions=categories) """********** Section 6: Calculating the RDM by Searchlight and Plotting **********""" # calculate the RDMs by Searchlight fmri_RDMs = fmriRDM(fmri_data) # plot one of the RDMs plot_rdm(fmri_RDMs[20, 30, 30], rescale=True, conditions=categories) """********** Section 7: Calculating the representational similarities **********""" """********** between a coding model and neural activities **********""" # Create a RDM for "animate-inanimate" coding model # which means the representations of animate matters are highly similar # and the representations of inanimate matters are highly similar model_RDM = np.array([[0, 0, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0]]) # plot the model RDM
# get mask of "mask_vt" in the dataset mask_vt_filename = haxby_dataset.mask_face[0] mask_vt_data = nib.load(mask_vt_filename).get_fdata() # calculate the RDM for ROI rdm_roi = fmriRDM_roi(fmri_data, mask_vt_data) # plot the RDM plot_rdm(rdm_roi, rescale=True, conditions=categories) """********** Section 6: Calculating the RDM by Searchlight and Plotting **********""" # calculate the RDMs by Searchlight fmri_RDMs = fmriRDM(fmri_data, sub_opt=0) # plot one of the RDMs plot_rdm(fmri_RDMs[20, 30, 30], rescale=True, conditions=categories) """********** Section 7: Calculating the representational similarities **********""" """********** between a coding model and neural activities **********""" # Create a RDM for "animate-inanimate" coding model # which means the representations of animate matters are highly similar # and the representations of inanimate matters are highly similar model_RDM = np.array([[0, 0, 1, 1, 1, 1, 1], [0, 0, 1, 1, 1, 1, 1], [1, 1, 0, 0, 0, 0, 0],
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 bhvANDfmri_corr(bhv_data, fmri_data, ksize=[3, 3, 3], strides=[1, 1, 1], method="spearman", rescale=False): """ Calculate the Similarities between behavioral data and fMRI data for searchlight 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. 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. 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 behavioral data and fMRI data for searchlight. 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. """ # calculate the bhv_rdm bhv_rdm = bhvRDM(bhv_data, sub_opt=0) print("****************") print("get behavior RDM") print(bhv_rdm) # calculate the fmri_rdms for searchlight fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides) print("****************") print("get fMRI RDM") print(np.shape(fmri_rdms)) # 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[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 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(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) print(corrs[i, j, k]) return corrs
nx, ny, nz = maskdata.shape datapath='/mnt/Data3/RfMRILab/Lihuixian/DataAnalysis/TaskAnalysis/WYSWYT/RSAanalysisWYSWYT/RSAnewprepocess/section2/SpeakEventBeta' subID=os.listdir(datapath) for sub in range(len(subID)): subname=os.path.join(datapath,subID[sub]) print(subname) speakeventbeta=glob.glob('%s/*.nii' %subname) ncon=len(speakeventbeta) fmri_data = np.full([ncon, nx, ny, nz], np.nan) for event in range(len(speakeventbeta)): fmri_data[event] = nib.load(speakeventbeta[event]).get_fdata() fmri_data = np.reshape(fmri_data, [ncon,1, nx, ny, nz]) print(fmri_data.shape) ## calculate the RDMs by Searchlight voxels=5 b=1 print('calculate fmri_RDMs...') fmri_RDMs = fmriRDM(fmri_data,ksize=[5, 5, 5],strides=[1, 1, 1],sub_opt=0,method='correlation', abs=True) print(fmri_RDMs.shape) ## save fmri_RDMs savepath=('%s/fmriRDMs_%s.npy' %(fmriRDMsOutpath,subID[sub])) np.save(savepath,fmri_RDMs) print(subID[sub]+'The fmri_RDMs was saved successfully!') """***Calculating the representational similarities***""" bhvsubpath=('%s/RDM_%s.csv' %(bhvpath,subID[sub])) bhv_RDM = np.loadtxt(bhvsubpath,delimiter=",") print('calculate RSA...'+subID[sub]) corrs=fmrirdms_corr(bhv_RDM,fmri_RDMs,method="spearman", fisherz=False, rescale=False, permutation=False, iter=5000) print('calculate ZRSA...'+subID[sub]) #fisherz_rdm;not permutation test corrsZ=fmrirdms_corr(bhv_RDM,fmri_RDMs,method="spearman", fisherz=True, rescale=False, permutation=False, iter=5000) """*** sav RSA result ***""" # get the affine info
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 bhvANDfmri_corr(bhv_data, fmri_data, bhv_data_opt=1, ksize=[3, 3, 3], strides=[1, 1, 1], method="spearman", rescale=False): # sub_opt=1 if bhv_data_opt == 0: bhv_rdm = bhvRDM(bhv_data, sub_opt=0, data_opt=0) # if bhv_data_opt=1 else: bhv_rdm = bhvRDM(bhv_data, sub_opt=0, data_opt=1) print("****************") print("get behavior RDM") print(bhv_rdm) fmri_rdms = fmriRDM(fmri_data, ksize=ksize, strides=strides) print("****************") print("get fMRI RDM") print(np.shape(fmri_rdms)) cons = np.shape(bhv_data)[0] nx = np.shape(fmri_data)[2] ny = np.shape(fmri_data)[3] nz = np.shape(fmri_data)[4] kx = ksize[0] ky = ksize[1] kz = ksize[2] sx = strides[0] sy = strides[1] sz = strides[2] n_x = int((nx - kx) / sx) + 1 n_y = int((ny - ky) / sy) + 1 n_z = int((nz - kz) / sz) + 1 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, m, n]) == 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(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "pearson": corrs[i, j, k] = rdm_correlation_pearson(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "kendall": corrs[i, j, k] = rdm_correlation_kendall(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "similarity": corrs[i, j, k, 0] = rdm_similarity(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) elif method == "distance": corrs[i, j, k, 0] = rdm_distance(bhv_rdm, fmri_rdms[i, j, k], rescale=rescale) print(corrs[i, j, k]) return corrs