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) ####################### # c. Plot Comparision # ####################### plt.plot(all_tr_times,testconv_np,label="conv_np") plt.plot(all_tr_times,testconv_2,label="user 2") plt.plot(all_tr_times,testconv_3,label="user 3") plt.plot(np.linspace(0,432.5,174),testconv_4_1,label="user 4, cut = 1") plt.plot(np.linspace(0,432.5,174),testconv_4_15,label="user 4, cut = 15") plt.plot(np.linspace(0,432.5,174),testconv_4_30,label="user 4, cut = 30 (standard)") plt.plot(all_tr_times,testconv_5,label="user 5") plt.title("User-made functions matching np.convolve in np.convolve territory")
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) ####################### # c. Plot Comparision # ####################### plt.plot(all_tr_times, testconv_np, label="conv_np") plt.plot(all_tr_times, testconv_2, label="user 2") plt.plot(all_tr_times, testconv_3, label="user 3") plt.plot(np.linspace(0, 432.5, 174), testconv_4_1, label="user 4, cut = 1") plt.plot(np.linspace(0, 432.5, 174), testconv_4_15, label="user 4, cut = 15") plt.plot(np.linspace(0, 432.5, 174), testconv_4_30, label="user 4, cut = 30 (standard)") plt.plot(all_tr_times, testconv_5, label="user 5") plt.title("User-made functions matching np.convolve in np.convolve territory")
cuts=15) scaled_4_15 = (conv_4_15 - np.mean(conv_4_15)) / (2 * np.std(conv_4_15)) + .4 conv_4_30 = np_convolve_30_cuts(real_times, on_off, hrf_function, TR, record_cuts, cuts=30) scaled_4_30 = (conv_4_30 - np.mean(conv_4_30)) / (2 * np.std(conv_4_30)) + .4 #-------# # fifth # #-------# conv_5 = fast_convolution(cond_all, np.ones(len(cond_all)), fast_hrf, np.linspace(0, 239 * 2 - 2, 239)) scaled_5 = (conv_5 - np.mean(conv_5)) / (2 * np.std(conv_5)) + .4 ############################ # Plot for comparisions # ############################ plt.scatter(cond_all, np.zeros(len(cond_all)), color=colors, label="stimuli instances") plt.plot(all_tr_times, scaled_np, label="np naive approach scaled") plt.plot(cond_all, scaled_2, label="user 2 scaled") plt.plot(all_tr_times, scaled_3, "-o", label="user 3 scaled") plt.plot(all_tr_times, scaled_4_30,
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))
real_times,on_off = cond_all,np.ones(len(cond_all)) hrf_function,TR,record_cuts= hrf_single, 2 ,np.linspace(0,239*2-2,239) conv_4_15 = np_convolve_30_cuts(real_times,on_off,hrf_function,TR,record_cuts,cuts=15) scaled_4_15=(conv_4_15-np.mean(conv_4_15))/(2*np.std(conv_4_15)) +.4 conv_4_30 = np_convolve_30_cuts(real_times,on_off,hrf_function,TR,record_cuts,cuts=30) scaled_4_30=(conv_4_30-np.mean(conv_4_30))/(2*np.std(conv_4_30)) +.4 #-------# # fifth # #-------# conv_5 = fast_convolution(cond_all,np.ones(len(cond_all)),fast_hrf,np.linspace(0,239*2-2,239)) scaled_5=(conv_5-np.mean(conv_5))/(2*np.std(conv_5)) +.4 ############################ # Plot for comparisions # ############################ plt.scatter(cond_all,np.zeros(len(cond_all)),color=colors,label="stimuli instances") plt.plot(all_tr_times,scaled_np,label="np naive approach scaled") plt.plot(cond_all,scaled_2,label="user 2 scaled") plt.plot(all_tr_times,scaled_3,"-o",label="user 3 scaled") plt.plot(all_tr_times,scaled_4_30,"-*",label="user 4 scaled (30 cuts)",color="k") plt.plot(all_tr_times,scaled_4_15,"-*",label="user 4 scaled (15 cuts)")