################## 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,
################## 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)
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))
################## # a. np.convolve # ################## 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 convolved = convolved[:N] ################################ # b. convolution (my function) # ################################ my_convolved = convolution(np.linspace(0, 432.5, 173), neural_prediction, hrf_single) ####################### # c. Plot Comparision # ####################### plt.plot(np.linspace(0, 432.5, 173), convolved, label="np.convolved") plt.plot(np.linspace(0, 432.5, 173), my_convolved, label="my convolution function") plt.title("Examining if 'convolution' can do the same thing as 'np.convolve' ") plt.xlabel("Time") plt.ylabel("Predicted Hemoglobin response") plt.legend(loc='lower right', shadow=True, fontsize="smaller") plt.savefig(location_of_images + 'test_comparision.png') plt.close()
################## # a. np.convolve # ################## 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 convolved=convolved[:N] ################################ # b. convolution (my function) # ################################ my_convolved=convolution(np.linspace(0,432.5,173),neural_prediction,hrf_single) ####################### # c. Plot Comparision # ####################### plt.plot(np.linspace(0,432.5,173),convolved,label="np.convolved") plt.plot(np.linspace(0,432.5,173),my_convolved,label="my convolution function") plt.title("Examining if 'convolution' can do the same thing as 'np.convolve' ") plt.xlabel("Time") plt.ylabel("Predicted Hemoglobin response") plt.legend(loc='lower right', shadow=True,fontsize="smaller") plt.savefig(location_of_images+'test_comparision.png') plt.close()
####################### # user convolution # ####################### # note: np.linspace(0,239*2-2,239) ==all_tr_times cond_all = np.array( sorted(list(cond2[:, 0]) + list(cond3[:, 0]) + list(cond1[:, 0]))) # could also just x_s_array #--------# # second # #--------# conv_2 = convolution(cond_all, np.ones(len(cond_all)), hrf_single) scaled_2 = (conv_2 - np.mean(conv_2)) / (2 * np.std(conv_2)) + .4 #-------# # third # #-------# conv_3 = convolution_specialized(cond_all, np.ones(len(cond_all)), hrf_single, np.linspace(0, 239 * 2 - 2, 239)) scaled_3 = (conv_3 - np.mean(conv_3)) / (2 * np.std(conv_3)) + .4 #--------# # fourth # #--------# real_times, on_off = cond_all, np.ones(len(cond_all))
####################### # user convolution # ####################### # note: np.linspace(0,239*2-2,239) ==all_tr_times cond_all=np.array(sorted(list(cond2[:,0])+list(cond3[:,0])+list(cond1[:,0]))) # could also just x_s_array #--------# # second # #--------# conv_2 = convolution(cond_all,np.ones(len(cond_all)),hrf_single) scaled_2=(conv_2-np.mean(conv_2))/(2*np.std(conv_2)) +.4 #-------# # third # #-------# conv_3 = convolution_specialized(cond_all,np.ones(len(cond_all)),hrf_single,np.linspace(0,239*2-2,239)) scaled_3=(conv_3-np.mean(conv_3))/(2*np.std(conv_3)) +.4 #--------# # fourth # #--------#