示例#1
0
    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]

        return convolve_lambda
#-------#
# 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)



#######################
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))
示例#4
0
#-------#
# 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,
 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]
     return convolve_lambda