def test_convolution_specialized(): stimuli=np.array([0,5,15]) on_off1=np.array([0,1,0]) on_off2=np.array([1,0,1]) x=np.linspace(0,45,91) # 0, .5, 1, 1.5, 2, ... 45 HRF1=convolution_specialized(stimuli,on_off1,hrf_single,x) y1=np.array([hrf_single(x_i-5) for x_i in x]) # what it should be doing HRF2=convolution_specialized(stimuli,on_off2,hrf_single,x) y2=np.array([hrf_single(x_i)+hrf_single(x_i-15) for x_i in x]) #what it should be doing assert all(HRF1 == y1) assert all(HRF2 == y2)
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)
# for convolution_specialized cond1=np.loadtxt(condition_location+"cond001.txt") cond2=np.loadtxt(condition_location+"cond002.txt") cond3=np.loadtxt(condition_location+"cond003.txt") ######################## # X Matrix Creation # ######################## ################### # 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 #
behav = pd.read_table(path_to_data + i + behav_suffix, sep=" ") num_TR = float(behav["NumTRs"]) # Suppose that TR=2. We know this is not a good assumption. # Also need to look into the hrf function. cond1 = np.loadtxt(path_to_data + i + "/model/model001/onsets/task001_run001/cond001.txt") cond2 = np.loadtxt(path_to_data + i + "/model/model001/onsets/task001_run001/cond002.txt") cond3 = np.loadtxt(path_to_data + 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]) # 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]) cond_all = np.array(cond_all)[:, 0] delta_y = 2 * (np.arange(34)) / 34 shifted = make_shift_matrix(cond_all, delta_y) def make_convolve_lambda(hrf_function, TR, num_TRs): convolve_lambda = lambda x: np_convolve_30_cuts( x, np.ones(x.shape[0]), hrf_function, TR, np.linspace(0, (num_TRs - 1) * TR, num_TRs), 15)[0]
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))
from event_related_fMRI_functions import hrf_single,convolution_specialized one_zeros = np.zeros(40) one_zeros[4] = 1 one_zeros[16:20]=1 plt.scatter(np.arange(40),one_zeros) plt.xlim(-1,40) plt.title("Stimulus pattern") plt.savefig(location_of_created_images+"on_off_pattern.png") plt.close() plt.plot(np.linspace(0,30,200),np.array([hrf_single(x) for x in np.linspace(0,30,200)])) plt.title("Single HRF, started at t=0") plt.savefig(location_of_created_images+"hrf_pattern.png") plt.close() convolved=convolution_specialized(np.arange(40),one_zeros,hrf_single,np.linspace(0,60,300)) plt.plot(np.linspace(0,60,300),convolved) plt.title("Convolution") plt.savefig(location_of_created_images+"initial_convolved.png") plt.close()
sys.path.append(functions) from event_related_fMRI_functions import hrf_single, convolution_specialized one_zeros = np.zeros(40) one_zeros[4] = 1 one_zeros[16:20] = 1 plt.scatter(np.arange(40), one_zeros) plt.xlim(-1, 40) plt.title("Stimulus pattern") plt.savefig(location_of_created_images + "on_off_pattern.png") plt.close() plt.plot(np.linspace(0, 30, 200), np.array([hrf_single(x) for x in np.linspace(0, 30, 200)])) plt.title("Single HRF, started at t=0") plt.savefig(location_of_created_images + "hrf_pattern.png") plt.close() convolved = convolution_specialized(np.arange(40), one_zeros, hrf_single, np.linspace(0, 60, 300)) plt.plot(np.linspace(0, 60, 300), convolved) plt.title("Convolution") plt.savefig(location_of_created_images + "initial_convolved.png") plt.close() colors = [ "#CCCCFF", "#C4C3D0", "#92A1CF", "#2A52BE", "#003399", "#120A8F", "#000080", "#002366" ]