def faster_correlation(fname, tr): # Load the ds114_sub009_t2r1.nii image img = nib.load(fname + ".nii") # Get the number of volumes in ds114_sub009_t2r1.nii n_trs = img.shape[-1] # Time between 3D volumes in seconds TR = tr # Get on-off timecourse time_course = events2neural(fname + "_cond.txt", TR, n_trs) # Drop the first 4 volumes, and the first 4 on-off values data = img.get_data() data = data[..., 4:] time_course = time_course[4:] # Calculate the number of voxels (number of elements in one volume) n_voxels = np.prod(data.shape[:-1]) # Reshape 4D array to 2D array n_voxels by n_volumes data_2d = np.reshape(data, (n_voxels, data.shape[-1])) # Transpose 2D array to give n_volumes, n_voxels array data_2d_T = data_2d.T # Calculate 1D vector length n_voxels of correlation coefficients correlation_1d = pearson.pearson_2d(time_course, data_2d_T) # Reshape the correlations array back to 3D correlation_3d = correlation_1d.reshape(data.shape[:-1]) return correlation_3d
def corr(nii_file, cond_file): """Find the correlations between time course and each voxel Parameters: ---------- nii_file: bold.nii file cond_file: condition file Return: ------- correlations: an array """ img = nib.load(nii_file) n_trs = img.shape[-1] TR = 2 #The TR (time between scans) is 2 seconds time_course = events2neural(cond_file, 2, n_trs) # Call the events2neural function to generate the on-off values for each volume data = img.get_data() # Using slicing, drop the first 4 volumes, and the first 4 on-off values data = data[..., 4:] time_course = time_course[4:] n_voxels = np.prod(data.shape[:-1]) # Calculate the number of voxels (number of elements in one volume) data_2d = np.reshape(data, (n_voxels, data.shape[-1])) # Reshape 4D array to 2D array n_voxels by n_volumes correlations_1d = np.zeros((n_voxels,)) # Make a 1D array of size (n_voxels,) to hold the correlation values for i in range(n_voxels): # Loop over voxels filling in correlation at this voxel correlations_1d[i] = np.corrcoef(time_course, data_2d[i, :])[0, 1] correlations = np.reshape(correlations_1d, data.shape[:-1]) # Reshape the correlations array back to 3D plt.imshow(correlations[:, :, 14]) # Plot the middle slice of the third axis from the correlations array return correlations #get correlations of two value
def test_convolve(): TR = 2 n_vols = 240 duration = 1.5 neural = events2neural(".././cond001.txt", TR, n_vols) conv = convolve(neural, TR, n_vols, 1.5) assert len(conv)==n_vols
def predict_bold_signal(filename, duration, by, TR, n_vols): """return neural prediction and predicted BOLD signal """ tr_times = np.arange(0, duration, by) hrf_at_trs = hrf(tr_times) neural_prediction = events2neural(filename, TR, n_vols) convolved = np.convolve(neural_prediction, hrf_at_trs) assert len(neural_prediction) + len(hrf_at_trs) - 1 == len(convolved) n_to_remove = len(hrf_at_trs) - 1 convolved = convolved[:-n_to_remove] return neural_prediction, convolved
def test_corr(): corr_act = corr(nii_file,cond_file) img = nib.load(nii_file) n_trs = img.shape[-1] TR = 2 time_course = events2neural(cond_file, 2, n_trs) data = img.get_data() data = data[..., 4:] time_course = time_course[4:] n_voxels = np.prod(data.shape[:-1]) assert corr_act.shape == data.shape[:-1]
def test_events2neural(): # test events2neural function neural = stimuli.events2neural('cond_test1.txt', 2, 16) # cond_test1.txt file is: """ 10 5.0 1 20 4.0 2 24 3.0 0.1 """ # Expected values for tr=2, n_trs=16 expected = np.zeros(16) expected[5:7] = 1 expected[10:12] = 2 expected[12] = 0.1 npt.assert_array_equal(neural, expected) neural = stimuli.events2neural('cond_test1.txt', 1, 30) # Expected values for tr=1, n_trs=30 expected = np.zeros(30) expected[10:15] = 1 expected[20:24] = 2 expected[24:27] = 0.1 npt.assert_array_equal(neural, expected)
def test_events2neural(): # test events2neural function cond_test1 = np.loadtxt(data_location+'cond_test1.txt') neural = stimuli.events2neural(cond_test1, 2, 16) # cond_test1.txt file is: """ 10 5.0 1 20 4.0 2 24 3.0 0.1 """ # Expected values for tr=2, n_trs=16 expected = np.zeros(16) expected[5:7] = 1 expected[10:12] = 2 expected[12] = 0.1 npt.assert_array_equal(neural, expected)
def test_events2neural(): # test events2neural function cond_test1 = np.loadtxt(data_location + 'cond_test1.txt') neural = stimuli.events2neural(cond_test1, 2, 16) # cond_test1.txt file is: """ 10 5.0 1 20 4.0 2 24 3.0 0.1 """ # Expected values for tr=2, n_trs=16 expected = np.zeros(16) expected[5:7] = 1 expected[10:12] = 2 expected[12] = 0.1 npt.assert_array_equal(neural, expected)
def test_time_shift(): # Intialize values for class data. TR = 2.5 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) # Load class data. n_vols = 173 neural_prediction = events2neural(pathtoclassdata+'ds114_sub009_t2r1_cond.txt', TR, n_vols) # Get np.convolve time course. convolved = np.convolve(neural_prediction, hrf_at_trs) N = len(neural_prediction) # N == n_vols == 173 # Compare shifted time courses by hand and using function. actual_shifted = convolved[5:(5+N)] exp_convolved2, exp_shifted = time_shift(convolved, neural_prediction, 5) assert_almost_equal(actual_shifted, exp_shifted)
def correlations(fname, tr): img = nib.load(fname + ".nii") TR = tr n_vol = img.shape[-1] time_course = events2neural(fname + "_cond.txt", TR, n_vol) time_course = time_course[4:] data = img.get_data()[...,4:] n_voxels = np.prod(data.shape[:-1]) data_2d = np.reshape(data, (n_voxels, data.shape[-1])) correlations_1d = np.zeros((n_voxels, )) for i in range(n_voxels): correlations_1d[i] = np.corrcoef(time_course, data_2d[i, :])[0,1] correlations = np.reshape(correlations_1d, data.shape[:-1]) return correlations
def run_model(func_fname, cond_file, TR, n_dropped, sigma_thresh): img = nib.load(func_fname) data = img.get_data() n_vols = data.shape[-1] vol_shape = data.shape[:3] n_voxels = np.prod(vol_shape) data_2d = np.reshape(data, (-1, n_vols)).T neural = events2neural(cond_file, TR, n_vols + n_dropped)[n_dropped:] times = np.arange(0, 24, TR) hrf = spm_hrf(times) hemo = np.convolve(neural, hrf)[:n_vols] X = np.ones((n_vols, 3)) X[:, 0] = hemo X[:, 1] = np.linspace(-1, 1, n_vols) betas = npl.pinv(X).dot(data_2d) fitted = X.dot(betas) residuals = data_2d - fitted df = n_vols - npl.matrix_rank(X) MRSS = (residuals ** 2).sum(axis=0) / df # Mask out voxels with tiny variance mask = MRSS > sigma_thresh c = np.array([1, 0, 0]) design_variance = c.dot(npl.pinv(X.T.dot(X)).dot(c)) con_1d = c.dot(betas)[mask] var_1d = np.sqrt(MRSS[mask] * design_variance) t_1d = con_1d / var_1d con_data = np.zeros(n_voxels) con_data[mask] = con_1d t_data = np.zeros(n_voxels) t_data[mask] = t_1d pth = dirname(func_fname) con_img = nib.Nifti1Image(con_data.reshape(vol_shape), img.affine, img.header) nib.save(con_img, pjoin(pth, 'con_task2.nii')) t_img = nib.Nifti1Image(t_data.reshape(vol_shape), img.affine, img.header) nib.save(t_img, pjoin(pth, 't_task2.nii'))
def run_model(func_fname, cond_file, TR, n_dropped, sigma_thresh): img = nib.load(func_fname) data = img.get_data() n_vols = data.shape[-1] vol_shape = data.shape[:3] n_voxels = np.prod(vol_shape) data_2d = np.reshape(data, (-1, n_vols)).T neural = events2neural(cond_file, TR, n_vols + n_dropped)[n_dropped:] times = np.arange(0, 24, TR) hrf = spm_hrf(times) hemo = np.convolve(neural, hrf)[:n_vols] X = np.ones((n_vols, 3)) X[:, 0] = hemo X[:, 1] = np.linspace(-1, 1, n_vols) betas = npl.pinv(X).dot(data_2d) fitted = X.dot(betas) residuals = data_2d - fitted df = n_vols - npl.matrix_rank(X) MRSS = (residuals**2).sum(axis=0) / df # Mask out voxels with tiny variance mask = MRSS > sigma_thresh c = np.array([1, 0, 0]) design_variance = c.dot(npl.pinv(X.T.dot(X)).dot(c)) con_1d = c.dot(betas)[mask] var_1d = np.sqrt(MRSS[mask] * design_variance) t_1d = con_1d / var_1d con_data = np.zeros(n_voxels) con_data[mask] = con_1d t_data = np.zeros(n_voxels) t_data[mask] = t_1d pth = dirname(func_fname) con_img = nib.Nifti1Image(con_data.reshape(vol_shape), img.affine, img.header) nib.save(con_img, pjoin(pth, 'con_task2.nii')) t_img = nib.Nifti1Image(t_data.reshape(vol_shape), img.affine, img.header) nib.save(t_img, pjoin(pth, 't_task2.nii'))
################# #np.convolve ################ # initial needed values TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols = data.shape[-1] # creating the .txt file for the events2neural function cond_all = np.row_stack((cond1, cond2, cond3)) cond_all = sorted(cond_all, key=lambda x: x[0]) np.savetxt(condition_location + "cond_all.txt", cond_all) neural_prediction = events2neural(condition_location + "cond_all.txt", TR, n_vols) convolved = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample data N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 np_hrf = convolved[:N] ################### # From GLM function ################### np_B, np_X = glm(data, np_hrf) #################################### # GLM Diagnostics (to get residuals) ###################################
################## # Suppose that TR=2. We know this is not a good assumption. # Also need to look into the hrf function. # initial needed values TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols=data.shape[-1] # creating the .txt file for the events2neural function cond_all=np.row_stack((cond1,cond2,cond3)) cond_all=sorted(cond_all,key= lambda x:x[0]) np.savetxt(condition_location+"cond_all.txt",cond_all) neural_prediction = events2neural(condition_location+"cond_all.txt",TR,n_vols) convolved = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample data N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 np_hrf=convolved[:N] #================================================= """ Run hypothesis testing script""" B_my,t_my,df,p_my = t_stat(data, my_hrf, np.array([0,1])) print("'my' convolution single regression (t,p):") print(t_my,p_my) print("means of (t,p) for 'my' convolution: (" +str(np.mean(t_my))+str(np.mean(p_my)) +")")
cond_all = np.loadtxt(condition_location + "cond_all.txt") ############## ############## ############################################ # i. Comparing convolution and np.convolve # ############################################ ############## ############## # i. Can the user-created functions match np.convolve in np.convolve territory TR = 2.5 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols = 173 neural_prediction = events2neural( location_to_class_data + 'ds114_sub009_t2r1_cond.txt', TR, n_vols) all_tr_times = np.arange(173) * TR ################## # a. np.convolve # ################## testconv_np = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample data N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 testconv_np = testconv_np[:N] ##################### # b. user functions # #####################
four conditions """ from __future__ import absolute_import, division, print_function import numpy as np import matplotlib.pyplot as plt from stimuli import events2neural from convolution import convolve TR = 2 n_vols = 240 duration = 3/TR all_tr_times = np.arange(240)*2 neural1 = events2neural(".././cond001.txt", TR, n_vols) neural2 = events2neural(".././cond002.txt", TR, n_vols) neural3 = events2neural(".././cond003.txt", TR, n_vols) neural4 = events2neural(".././cond004.txt", TR, n_vols) convolved1 = convolve(neural1, TR, n_vols, duration) np.savetxt("conv001.txt", convolved1) convolved2 = convolve(neural2, TR, n_vols, duration) np.savetxt("conv002.txt", convolved2) convolved3 = convolve(neural3, TR, n_vols, duration) np.savetxt("conv003.txt", convolved3) convolved4 = convolve(neural4, TR, n_vols, duration) np.savetxt("conv004.txt", convolved4) plt.subplot(221) plt.plot(all_tr_times, convolved1)
def test_convolution(): ################# # i. Can the user-created functions match np.convolve in np.convolve territory TR = 2.5 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols = 173 neural_prediction = events2neural(location_to_class_data+'ds114_sub009_t2r1_cond.txt',TR,n_vols) all_tr_times = np.arange(173) * TR ################## # a. np.convolve # ################## testconv_np = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample data N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 testconv_np=testconv_np[:N] ##################### # b. user functions # ##################### #--------# # second # testconv_2 = convolution(all_tr_times,neural_prediction,hrf_single) #-------# # third # testconv_3 = convolution_specialized(all_tr_times,neural_prediction, hrf_single,all_tr_times) #--------# # fourth # on_off = np.zeros(174) real_times,on_off[:-1] = np.linspace(0,432.5,173+1),neural_prediction hrf_function,TR,record_cuts= hrf_single, 2.5 ,np.linspace(0,432.5,173+1) # testconv_4_1 = np_convolve_30_cuts(real_times,on_off,hrf_function,TR,record_cuts,cuts=1) testconv_4_15 = np_convolve_30_cuts(real_times,on_off,hrf_function,TR,record_cuts,cuts=15) testconv_4_30 = np_convolve_30_cuts(real_times,on_off,hrf_function,TR,record_cuts,cuts=30) #-------# # fifth # testconv_5 = fast_convolution(all_tr_times,neural_prediction,fast_hrf,all_tr_times) additional_runs=[testconv_np,testconv_2,testconv_3,testconv_4_1,testconv_4_15,testconv_4_30,testconv_5] names=["testconv_np","testconv_2","testconv_3","testconv_4_1","testconv_4_15","testconv_4_30","testconv_5"] print("Max difference between model and testconv_np:") for i,my_convolved in enumerate(additional_runs): if my_convolved.shape[0]==testconv_np.shape[0]: print(names[i],max(abs(testconv_np-my_convolved))) else: print(names[i],max(abs(testconv_np-my_convolved[:-1]))) # Actual asserts for i,my_convolved in enumerate(additional_runs): if my_convolved.shape[0]==testconv_np.shape[0]: assert (max(abs(testconv_np-my_convolved) < .0001)) else: assert (max(abs(testconv_np-my_convolved[:-1]) < .0001))
# np.convolve # ################## # initial needed values TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) # creating the .txt file for the events2neural function cond_all=np.row_stack((cond1,cond2,cond3)) cond_all=sorted(cond_all,key= lambda x:x[0]) np.savetxt(condition_location+"cond_all.txt",cond_all) neural_prediction=events2neural(condition_location+"cond_all.txt",2,239) # 1s are non special events # doing the np.convolve conv_np=np.convolve(neural_prediction,hrf_at_trs) conv_np=conv_np[:-(len(hrf_at_trs)-1)] #shorting convolution vector all_tr_times = np.arange(data.shape[-1]) * TR scaled_np=(conv_np-np.mean(conv_np))/(2*np.std(conv_np)) +.4 ####################### # user convolution #
################## # np.convolve # ################## # initial needed values TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) # creating the .txt file for the events2neural function cond_all = np.row_stack((cond1, cond2, cond3)) cond_all = sorted(cond_all, key=lambda x: x[0]) np.savetxt(condition_location + "cond_all.txt", cond_all) neural_prediction = events2neural(condition_location + "cond_all.txt", 2, 239) # 1s are non special events # doing the np.convolve conv_np = np.convolve(neural_prediction, hrf_at_trs) conv_np = conv_np[:-(len(hrf_at_trs) - 1)] #shorting convolution vector all_tr_times = np.arange(data.shape[-1]) * TR scaled_np = (conv_np - np.mean(conv_np)) / (2 * np.std(conv_np)) + .4 ####################### # user convolution # ####################### # note: np.linspace(0,239*2-2,239) ==all_tr_times
######################## ################### # np.convolve # ################### TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols = data.shape[-1] # time slices X_np = np.ones((n_vols,4)) cond_string = ["cond001.txt","cond002.txt","cond003.txt"] for i,name in enumerate(cond_string): nueral_prediction = events2neural(condition_location+name,TR,n_vols) hrf_long = np.convolve(nueral_prediction, hrf_at_trs) X_np[:,i+1] = hrf_long[:-(len(hrf_at_trs)-1)] all_tr_times = np.arange(n_vols) * TR ############################### # convolution_specialized # ############################### X_my = np.ones((n_vols,4)) conds = [cond1[:,0],cond2[:,0],cond3[:,0]] for i,cond in enumerate(conds): X_my[:,i+1]=convolution_specialized(cond,np.ones(len(cond)),hrf_single,all_tr_times)
"/model/model001/onsets/task001_run001/cond003.txt") TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols = data.shape[-1] # creating the .txt file for the events2neural function cond_all = np.row_stack((cond1, cond2, cond3)) cond_all = sorted(cond_all, key=lambda x: x[0]) np.savetxt( pathtodata + i + "/model/model001/onsets/task001_run001/cond_all.txt", cond_all) neural_prediction = events2neural( pathtodata + i + "/model/model001/onsets/task001_run001/cond_all.txt", TR, n_vols) convolved = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 np_hrf = convolved[:N] B, t, df, p = t_stat(data, np_hrf, np.array([0, 1])) #Simple mask function mask = nib.load(pathtodata + i + '/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() t_mean[..., int(i[-1])] = make_mask(np.reshape(t, (64, 64, 34)), mask_data,
import numpy as np import matplotlib.pyplot as plt # %matplotlib import nibabel as nib img = nib.load('bold.nii') data = img.get_data() data = data[..., 1:] data.shape vol0 = data[..., 0] vol0.shape mean_data = np.mean(data, axis=-1) mean_data[42, 32, 19] = np.max(mean_data) plt.imshow(mean_data[:, :, 19], cmap='gray', interpolation='nearest') voxel_time_course = data[42, 32, 19] plt.plot(voxel_time_course) img.shape from stimuli import events2neural TR = 2 # time between volumes n_trs = img.shape[-1] # The original number of TRs neural = events2neural('cond002.txt', TR, n_trs) plt.plot(neural) neural = neural[1:] plt.plot(neural, voxel_time_course, 'o') np.corrcoef(neural, voxel_time_course)
############## ############## ############################################ # i. Comparing convolution and np.convolve # ############################################ ############## ############## # i. Can the user-created functions match np.convolve in np.convolve territory TR = 2.5 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols = 173 neural_prediction = events2neural(location_to_class_data+'ds114_sub009_t2r1_cond.txt',TR,n_vols) all_tr_times = np.arange(173) * TR ################## # a. np.convolve # ################## testconv_np = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample data N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 testconv_np=testconv_np[:N] ##################### # b. user functions #
# Also need to look into the hrf function. cond1=np.loadtxt(pathtodata+ i+ "/model/model001/onsets/task001_run001/cond001.txt") cond2=np.loadtxt(pathtodata+ i+ "/model/model001/onsets/task001_run001/cond002.txt") cond3=np.loadtxt(pathtodata+ i+ "/model/model001/onsets/task001_run001/cond003.txt") TR = 2 tr_times = np.arange(0, 30, TR) hrf_at_trs = np.array([hrf_single(x) for x in tr_times]) n_vols=data.shape[-1] # creating the .txt file for the events2neural function cond_all=np.row_stack((cond1,cond2,cond3)) cond_all=sorted(cond_all,key= lambda x:x[0]) np.savetxt(pathtodata+ i+ "/model/model001/onsets/task001_run001/cond_all.txt",cond_all) neural_prediction = events2neural(pathtodata+ i+ "/model/model001/onsets/task001_run001/cond_all.txt",TR,n_vols) convolved = np.convolve(neural_prediction, hrf_at_trs) # hrf_at_trs sample N = len(neural_prediction) # N == n_vols == 173 M = len(hrf_at_trs) # M == 12 np_hrf=convolved[:N] B,t,df,p = t_stat(data, np_hrf, np.array([0,1])) #Simple mask function mask = nib.load(pathtodata+i+'/anatomy/inplane001_brain_mask.nii.gz') mask_data = mask.get_data() t_mean[...,int(i[-1])] = make_mask(np.reshape(t,(64,64,34)), mask_data, fit=True)
# TR is 2,5 second TR = 2.5 tr_times = np.arange(0, 30, TR) # The number of the voxel is 121 n_vols = 121 all_tr_times = np.arange(121) * TR for i in list_every_cond('sub001', 'task001_run001'): neural = events2neural(cond_path('sub001', 'task001_run001', i), TR, n_vols) convolved = convolve(neural, hrf(tr_times)) plt.plot(all_tr_times, neural) plt.plot(all_tr_times, convolved) plt.close() # There are 8 categories, and 121 features each categories. X = np.ones((n_vols, 8 + 1)) #There are 8 categories, so the data matrix has 9 colomns. #The order of the column is house, scrambledpix, cat, shoe, bottle, scissors, chair, face. # Let the first column starts from index 0 col = 0
data = load_img(1,1) data = gaussian_filter(data, [2, 2, 2, 0]) # Get the number of volumes n_trs = data.shape[-1] #nice map nice_cmap_values = np.loadtxt('actc.txt') nice_cmap = colors.ListedColormap(nice_cmap_values, 'actc') # Identify the TR (time between scans) TR = 2 # Call the events2neural function to generate the on-off values for each volume task_name = location_of_data +"sub001/model/model001/onsets/task001_run001/cond002.txt" task = np.loadtxt(task_name) time_course = events2neural(task, TR, n_trs) # Make a single brain volume-size array of all zero to hold the correlations correlations = np.zeros(data.shape[:-1]) # Loop over all voxel indices on the first, then second, then third dimension # Extract the voxel time courses at each voxel coordinate in the image # Get the correlation between the voxel time course and neural prediction # Fill in the value in the correlations array for i in range(data.shape[0]): for j in range(data.shape[1]): for k in range(data.shape[2]): vox_values = data[i, j, k] correlations[i, j, k] = np.corrcoef(time_course, vox_values)[1, 0]