def test_integral_of_constant_function(): print("yay") f1 = vectorize(f) computed_ans = integrate(f1, 0, 1, 10) #N = 10 assert abs(2.0 - computed_ans) < 1E-10 computed_ans = integrate(f1, 0, 1, 10000) #N = 10000 assert abs(2.0 - computed_ans) < 1E-10
def test_integral_of_constant_function(): # test integrate assert abs(integrate(f1,0,1,1)-2)<= 1E-15 #print ("lol",abs(integrate(f1,0,1,10000)-float(2))) assert abs(integrate(f1,0,1,10)-2)<= 1E-12 assert abs(integrate(f1,0,1,100)-2)<= 1E-12 assert abs(integrate(f1,0,1,1000)-2)<= 1E-12 assert abs(integrate(f1,0,1,10000)-2)<= 1E-12 # test numpy integrate assert abs(numpy_integrate(f1,0,1,1)-2)<= 1E-15 #print ("lol",numpy_integrate(f1,0,1,10),abs(numpy_integrate(f1,0,1,10)-float(2))) assert abs(numpy_integrate(f1,0,1,10)-2)<= 1E-12 assert abs(numpy_integrate(f1,0,1,100)-2)<= 1E-12 assert abs(numpy_integrate(f1,0,1,1000)-2)<= 1E-12 assert abs(numpy_integrate(f1,0,1,10000)-2)<= 1E-12 # test Cython assert abs(cython_integrate(f1,0,1,1)-2)<= 1E-15 #print ("lol",numpy_integrate(f1,0,1,10),abs(numpy_integrate(f1,0,1,10)-float(2))) assert abs(cython_integrate(f1,0,1,10)-2)<= 1E-12 assert abs(cython_integrate(f1,0,1,100)-2)<= 1E-12 assert abs(cython_integrate(f1,0,1,1000)-2)<= 1E-12 assert abs(cython_integrate(f1,0,1,10000)-2)<= 1E-12
def test_integral_of_linear_function(): # test integrate N1=10 #print ("here",abs(1-integrate(f2,0,1,N1)-(float(1)/float(N1)))) assert abs(1-integrate(f2,0,1,N1)-(float(1)/float(N1))) <= 1E-15 N2=100 assert abs(1-integrate(f2,0,1,N2)-(float(1)/float(N2))) <= 1E-15 N3=1000 assert abs(1-integrate(f2,0,1,N3)-(float(1)/float(N3))) <= 1E-15 N4=10000 assert abs(1-integrate(f2,0,1,N4)-(float(1)/float(N4))) <= 1E-15 # test numpy integrate N1=10 assert abs(1-numpy_integrate(f2,0,1,N1)-(float(1)/float(N1))) <= 1E-15 N2=100 assert abs(1-numpy_integrate(f2,0,1,N2)-(float(1)/float(N2))) <= 1E-15 N3=1000 assert abs(1-numpy_integrate(f2,0,1,N3)-(float(1)/float(N3))) <= 1E-15 N4=10000 assert abs(1-numpy_integrate(f2,0,1,N4)-(float(1)/float(N4))) <= 1E-15 # test Cython N1=10 assert abs(1-cython_integrate(f2,0,1,N1)-(float(1)/float(N1))) <= 1E-15 N2=100 assert abs(1-cython_integrate(f2,0,1,N2)-(float(1)/float(N2))) <= 1E-15 N3=1000 assert abs(1-cython_integrate(f2,0,1,N3)-(float(1)/float(N3))) <= 1E-15 N4=10000 assert abs(1-cython_integrate(f2,0,1,N4)-(float(1)/float(N4))) <= 1E-15
def smafeat(datXa,datYa,datZa): dataXsq1 = sqrt(numpy.array(datXa) ** 2) dataYsq1 = sqrt(numpy.array(datYa) ** 2) dataZsq1 = sqrt(numpy.array(datZa) ** 2) dataXc1 = integrator.integrate(dataXsq1) dataYc1 = integrator.integrate(dataYsq1) dataZc1 = integrator.integrate(dataZsq1) sma = (dataXc1 + dataYc1 + dataZc1) / float(len(dataXsq1)) return sma
def attractorPlot(f0, f1, w0, w1, period, name='', show=True, save=False): consts = {} consts["F0"] = f0 consts["F1"] = f1 consts["w0"] = w0 consts["w1"] = w1 consts["c"] = 1.0 consts["k"] = -1.0 consts["beta"] = 1.0 consts["phi"] = 0.0 x0 = 1.0 v0 = 0.0 plt.figure() plt.title("Poincare Plot - 2 Forcing Terms") plt.xlabel("x") plt.ylabel("v") x, v, t, f = integrator.integrate(consts, x0, v0, 10000, 5E-5, True, 2000000) N = len(x) // 2 plt.plot(x[N:], v[N:], '-r') strb_x = integrator.strobe_points(x[N:], t[N:], period) strb_v = integrator.strobe_points(v[N:], t[N:], period) lab = "$\omega_0={},F_0={},\omega_1={},F_1={}$".format(w0, f0, w1, f1) l = plt.plot(strb_x, strb_v, '.', label=lab) plt.setp(l, 'markersize', 2) plt.legend(loc="best") plt.xlabel("$x$") plt.ylabel("$\dot{x}$") #plt.savefig("Poincare4.pdf") plt.show()
def test_integral_of_constant_function(): f = lambda x: 2 computed_for = integrate(f, 0, 1, 100) computed_numpy = numpy_integrator(f, 0, 1, 100) expected = 2 success = computed_for == expected and (computed_numpy - expected) < 1e-16 assert success
def integrator_test(self): """integrator_test: this test checks the result of integreation function""" sourcelist = [ 0.9676834571, 0.9517338109, 0.9469666224, 0.9455741929, 0.937271954, 0.9756308369, 0.963667954, 0.9783003172, 0.9791772432, 0.9630688213, 0.9636879293, 0.9581131469, 0.9749188851, 0.96132287, 0.9594692316, 0.9517417142, 0.9580682981, 0.9694806605, 0.9705071034, 0.9741733322, 0.9656285119, 0.9797829703, 0.9476657225, 0.9505700439, 0.9769535092, 0.9628920433, 0.9886532632, 0.9749905082, 0.9622008273, 0.9588126694, 0.9585201885, 0.9656285119, 0.9635884074, 0.9637201077, 0.9486464305, 0.9448569633, 0.9548531757, 0.9413523349, 0.9437067169, 0.9295829852, 0.9443785543, 0.9266972306, 0.9272177018, 0.9396456813, 0.948808991, 0.9573977701, 0.970075925, 0.9793470801, 0.9840866322, 0.9765331821, 0.9656885071, 0.9629277499, 0.9614462269, 0.9738112064, 0.949794186, 0.9964832677, 0.9958061173, 0.9737453088, 0.9641612591, 0.9680686611, 0.9796823214, 0.9715538306, 0.9917106859, 0.961369319, 0.960533384, 0.9630009426, 0.9597456627, 0.9426550241, 0.9571523382, 0.9441424433, 0.9533811307, 0.9547935049, 0.9595538268, 0.9522523389, 0.9563979183, 0.9743059924, 0.963151262, 0.9664897915, 0.9625850612, 0.9878803841, 0.973709362, 0.9593169424, 0.9501199574, 0.9492373943, 0.9438450032, 0.9455317748, 0.967039832, 0.9616603865, 0.9824073673, 0.9699467778, 0.9750983573, 0.9927066538, 0.9729307196, 0.9851261483, 0.9534832401, 0.9562380523, 0.9606531787, 0.967532279, 0.9584946534, 0.9740792079 ] target = 0.962607799619 result = integrator.integrate(sourcelist) self.assertAlmostEqual(result, target)
def test_integral_of_linear_function(): f = lambda x: 2 * x computed_for = integrate(f, 0, 1, 1e7) computed_numpy = numpy_integrator(f, 0, 1, 1e7) expected = 1. success = abs(computed_for - expected) < 1e-4 and abs(computed_numpy - expected) < 1e-4 assert success
def step_checker(integrate): def f(x): return np.sin(x) a = 0 b = np.pi eps = 10**(-10) exact_solution = 2.0 N = 0 while True: N += 100 error = abs(integrate(f, a, b, N) - exact_solution) if error < eps: break print N print integrate(f, a, b, N)
def function_1(): f = lambda x: (1 / math.pi) * (math.sin(x) / x) * (math.sin(x / 3) / ( x / 3)) * (math.sin(x / 5) / (x / 5)) sum_of_integral = integrator.integrate(f, a, b, n, method="midpoint", implementation="numba") print('Function 1 - sum = {} N = {}'.format(sum_of_integral, n))
def cython_midpoint(): difference = 1 n = 90600 f = lambda x: math.sin(x) while (difference > 1E-10): n += 1 sum_of_integral = integrator.integrate(f, 0, math.pi, n, method="midpoint", implementation="numpy") difference = abs(2 - sum_of_integral) print(n) print('Cython - sum = {} difference = {} N = {}'.format(sum_of_integral, difference, n))
def test_integral_of_constant_function(): f = lambda x: 2 a = 0 b = 1 N1 = 10 N2 = 1000 tol = 1e-10 integral1 = integrate(f, a, b, N1) integral2 = integrate(f, a, b, N2) integral1_numpy = integrate_numpy(f, a, b, N1) integral2_numpy = integrate_numpy(f, a, b, N2) assert abs(integral1 - 2) < tol assert abs(integral2 - 2) < tol assert abs(integral1_numpy - 2) < tol assert abs(integral2_numpy - 2) < tol
def test_integral_of_linear_function(): f = lambda x: 2 * x a = 0 b = 1 N1 = 10 N2 = 1000 tol = 1e-10 integral1 = integrate(f, a, b, N1) integral2 = integrate(f, a, b, N2) integral1_numpy = integrate_numpy(f, a, b, N1) integral2_numpy = integrate_numpy(f, a, b, N2) assert abs(integral1 - 1) < 1.0 / N1 + tol assert abs(integral2 - 1) < 1.0 / N2 + tol assert abs(integral1_numpy - 1) < 1.0 / N1 + tol assert abs(integral2_numpy - 1) < 1.0 / N2 + tol
def test_integral_of_linear_function(): expected_answer = 1 f = lambda x: 2 * x a = 0 b = 1 N = 1000000 computed_answer = integrate(f, a, b, N) #numpy_integrate(f, a, b, N) #assert abs(computed_answer - expected_answer) < 1E-20, "Computational error to large!" if abs(computed_answer - expected_answer) < (1. / N): print("Computation ok") else: print("Computational error to large, error = %f" % (1. / N))
def run_test_problem(test_func, description, integration_params): x_exact, f_test, dfdt_test, fx_test = test_func method, test_name = description hs, x0, t0, tf = integration_params plt.figure() plt.title("{} on {}".format(*description)) for h in hs: derivatives = f_test, dfdt_test, fx_test params = x0, t0, tf, h t, x = integrator.integrate(method, derivatives, params) xe = x_exact(t) plt.plot(x.real, x.imag, label='h = {}'.format(h)) plt.plot(xe.real, xe.imag, label='exact') plt.legend()
def test_integral_of_constant_function(): #abs(computed_answer - expected_answer) < 1E-20 expected_answer = 2 f = lambda x: 2 a = 0 b = 1 N = 1000000 computed_answer = integrate(f, a, b, N) #numpy_integrate(f, a, b, N) print(computed_answer) #assert abs(computed_answer - expected_answer) < 1E-20, "Computational error to large!" if abs(computed_answer - expected_answer) < 1E-5: print("Computation ok") else: print("Computational error to large!")
def score_track_novis(track, fps=30, ppm=1000): #First, euclidify and integrate. euclidified = integrator.euclidify(track) integrated = integrator.integrate(euclidified) vid_frames = euclidified.size vid_framerate = fps #Calculate various discounts for the things that change between videos and setups time_discount = scorer.factor_time(vid_frames, vid_framerate) size_discount = scorer.factor_size(ppm) #Factor in all of the different discounts score = integrated / (time_discount * size_discount) return score
def test_a_stab(create_int): k_vals = [0.1, 1, 10] interval = numpy.arange(0, 10, 1) for k in k_vals: # integrator for A' = -kA integrator_func = create_int(lambda t, last_vals: -k * last_vals[0]) # [1] => only y-values, [0] => 1-dim. result = integrator.integrate([integrator_func], [1], interval)[1][0] # check whether values decrease for i in range(0, len(result) - 1): if result[i] < result[i + 1]: return False return True
def integrator_test(self): sourcelist = [0.9676834571,0.9517338109,0.9469666224,0.9455741929,0.937271954,0.9756308369,0.963667954,0.9783003172, 0.9791772432,0.9630688213,0.9636879293, 0.9581131469,0.9749188851,0.96132287,0.9594692316,0.9517417142, 0.9580682981,0.9694806605,0.9705071034,0.9741733322,0.9656285119,0.9797829703,0.9476657225,0.9505700439, 0.9769535092,0.9628920433,0.9886532632,0.9749905082,0.9622008273,0.9588126694,0.9585201885,0.9656285119, 0.9635884074,0.9637201077,0.9486464305,0.9448569633,0.9548531757,0.9413523349,0.9437067169,0.9295829852, 0.9443785543,0.9266972306,0.9272177018,0.9396456813,0.948808991,0.9573977701,0.970075925,0.9793470801, 0.9840866322,0.9765331821,0.9656885071,0.9629277499,0.9614462269,0.9738112064,0.949794186,0.9964832677, 0.9958061173,0.9737453088,0.9641612591,0.9680686611,0.9796823214,0.9715538306,0.9917106859,0.961369319, 0.960533384,0.9630009426,0.9597456627,0.9426550241,0.9571523382,0.9441424433,0.9533811307,0.9547935049, 0.9595538268,0.9522523389,0.9563979183,0.9743059924,0.963151262,0.9664897915,0.9625850612,0.9878803841,0.973709362, 0.9593169424,0.9501199574,0.9492373943,0.9438450032,0.9455317748,0.967039832,0.9616603865,0.9824073673, 0.9699467778,0.9750983573,0.9927066538,0.9729307196,0.9851261483,0.9534832401,0.9562380523,0.9606531787, 0.967532279,0.9584946534,0.9740792079] target = 0.962607799619 result = integrator.integrate(sourcelist) self.assertAlmostEqual(result,target)
def bifurcationPlot(f0_min, f0_max, num_pts, f1, w0, w1, period, name='', show=True, save=False): consts = {} consts["F0"] = 0 forces = list(np.linspace(f0_min, f0_max, num_pts)) consts["F1"] = f1 consts["c"] = 1.0 consts["k"] = -1.0 consts["beta"] = 1.0 consts["w0"] = w0 consts["w1"] = w1 consts["phi"] = 0 x0 = 1.0 y0 = 0.0 plt.figure() plt.title('Bifurcation - f1={}'.format(f1)) plt.xlabel("$f_0$") plt.ylabel("$x$") for force in forces: consts["F0"] = force #print("integrating for F={}".format(force)) X, V, T, F = integrator.integrate(consts, x0, y0, 400, .5 * 10**-5, True, 20000) cut = len(X) // 2 strobe_x = integrator.strobe_points(X[cut:], T[cut:], period) l = plt.plot([force] * len(strobe_x), strobe_x, 'b.') plt.setp(l, 'markersize', 2) #plt.savefig("bifurcation_f1_" + str(round(f,3)) + "w1_" + str(w)+ ".pdf") plt.show()
def integrate_data(self, processing_options, out=None): if out is None: out = sys.stdout integration_object = integrator.integrate( self.hutch, processing_options.integration ) q_low, q_mean, q_step = integration_object.get_q_arrays() self.q_range = integrator.q_range( q_low, q_mean, q_step ) for this_specimen in self.specimen_array: for this_image_name in this_specimen.image_names(): print >> out, "Integrating %s "%(this_image_name) mi,vi = integration_object.integrate_image( this_image_name ) new_radially_integrated_dataset = integrator.radially_integrated_data( self.q_range ) new_radially_integrated_dataset.load_data(mi,vi) transmission, i_tot,i_after = integration_object.get_aux_data(this_image_name) new_radially_integrated_dataset.set_aux_info(transmission, i_tot, i_after) tmp_image_name = this_image_name.split("/") tmp_image_name = tmp_image_name[ len(tmp_image_name)-1 ] #if new_radially_integrated_dataset.write_data( file_name = tmp_image_name+".raw_data.qis" ) this_specimen.add_data( new_radially_integrated_dataset ) print >> out, " ----> I1/I0: %8.5f I0: %5.3e I1: %5.3e "%(transmission, i_tot, i_after) print >> out
def pathPlots(f0, f1, w0, w1, name='', show=True, save=False): consts = {} consts["F0"] = f0 consts["F1"] = f1 consts["w0"] = w0 consts["w1"] = w1 consts["c"] = 1.0 consts["k"] = -1.0 consts["beta"] = 1.0 consts["phi"] = 0 x0 = 1.0 v0 = 0.0 x, v, t, f = integrator.integrate(consts, x0, v0, 500, 5E-5, True, 20000) if not show and not save: return N = len(x) // 2 plt.figure() plt.plot(x[N:], v[N:]) plt.axis([-2, 2, -2, 2]) plt.title('Phase Space Trajectory') plt.xlabel("x") plt.ylabel("$\dot{x}$") if save: plt.savefig("pathPlots_phase_" + name + ".pdf") plt.figure() plt.plot(t, x) plt.title('x vs t') plt.xlabel("t") plt.ylabel("x") if save: plt.savefig("pathPlots_xt_" + name + ".pdf") if show: plt.show()
from numba import jit from time import clock from integrator import integrate, f t0 = clock() integrate(f, 0, 1, 1e7) time0 = clock() - t0 @jit def numba_integrate(f, a, b, N): dx = (b - a) / float(N) s = 0 for i in range(int(N)): s += f(i * dx) return s * dx t1 = clock() numba_integrate(f, 0, 1, 1e7) time1 = clock() - t1 print(time0) print(time1) """ runfile('C:/Users/simen/INF3331-Simehaa/assignment4/numba_integrator.py', wdir='C:/Users/simen/INF3331-Simehaa/assignment4') Reloaded modules: integrator 2.9856839809117446 3.8899518532693946 """
def energyCalc(array1,array2,array3): totEnergy = integrator.integrate(numpy.array(array1) ** 2) + integrator.integrate(numpy.array(array2) ** 2) + \ integrator.integrate(numpy.array(array3) ** 2) return totEnergy
from integrator import midpoint_integrate from numpy_integrator import numpy_midpoint_integrate from numba_integrator import numba_midpoint_integrate from cython_integrator import cython_midpoint_integrate def f5(x): return np.sin(x) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10: #for i in range(1,10): #print ("here",i,integrate(f5,0, math.pi,i*1000)) approx = integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with integrate", i) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10: #for i in range(1,3): #print ("here",i,numpy_integrate(f5,0, math.pi,i*1000)) approx = numpy_integrate(f5, 0, math.pi, i * 1000) i = i + 1 print("iterations required with numpy_integrate", i) approx = 0 i = 1 while abs(approx - float(2.0)) > 10e-10:
def featurescom (fullarray, micannot,act_state): samp_rate = source_var.sampling_rate() pre_win = [0, samp_rate] #pre-window imp_win = [(samp_rate/2)-1, int(6.5 * samp_rate)] # impact window post_win = [int((2.5 * samp_rate))-1, 12 * samp_rate] #post impact window max_win = [(samp_rate/2)-1, int(1.5 * samp_rate)] # window for search max value tilt_sample_impact = [(2*samp_rate)-1, 3*samp_rate] tilt_sample_post = [(3 * samp_rate) - 1, 12 * samp_rate] # window for tilt-angle datAr = [] #for vector magnitude datXa= [] #for X values datYa= [] #for Y values datZa= [] #for Z values datXg=[] datYg=[] datZg=[] for tempdata in fullarray: datAr.append(float(tempdata[6])) datXa.append(float(tempdata[0])) datYa.append(float(tempdata[1])) datZa.append(float(tempdata[2])) datXg.append(float(tempdata[3])) datYg.append(float(tempdata[4])) datZg.append(float(tempdata[5])) #****************************************************************************************************************************** #minimum value feature minVal = min(datAr[pre_win[0]:pre_win[1]]) #****************************************************************************************************************************** #maximum value feature maxVal = max(datAr[max_win[0]:max_win[1]]) #****************************************************************************************************************************** #mean for pre-impact event meanList1 = datAr[pre_win[0]:pre_win[1]] meanVal1 = numpy.mean(meanList1) #****************************************************************************************************************************** #mean for impact meanList2 = datAr[imp_win[0]:imp_win[1]] meanVal2 = numpy.mean(meanList2) #****************************************************************************************************************************** #mean for post-impact meanList3 = datAr[post_win[0]:post_win[1]] meanVal3 = numpy.mean(meanList3) #****************************************************************************************************************************** #Root mean square for pre-impact rmsList1 = datAr[pre_win[0]:pre_win[1]] rms1 = sqrt(mean(numpy.array(rmsList1)**2)) #Root mean square for impact rmsList2 = datAr[imp_win[0]:imp_win[1]] rms2 = sqrt(mean(numpy.array(rmsList2)**2)) #Root mean square for post-impact rmsList3 = datAr[post_win[0]:post_win[1]] rms3 = sqrt(mean(numpy.array(rmsList3)**2)) #****************************************************************************************************************************** #variance for pre-impact variance1 = numpy.var(datAr[pre_win[0]:pre_win[1]],ddof=1) #variance for impact variance2 = numpy.var(datAr[imp_win[0]:imp_win[1]],ddof=1) #variance for post-impact variance3 = numpy.var(datAr[post_win[0]:post_win[1]],ddof=1) #****************************************************************************************************************************** #velocity in pre-impact velo1 = integrator.integrate(datAr[pre_win[0]:pre_win[1]]) #velocity in impact velo2 = integrator.integrate(datAr[imp_win[0]:imp_win[1]]) #velocity in post-impact velo3 = integrator.integrate(datAr[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #energy in pre-impact win1x = datXa[pre_win[0]:pre_win[1]] win1y = datYa[pre_win[0]:pre_win[1]] win1z = datZa[pre_win[0]:pre_win[1]] energy1 = energyFeat.energyCalc(win1x,win1y,win1z) #energy in impact win2x = datXa[imp_win[0]:imp_win[1]] win2y = datYa[imp_win[0]:imp_win[1]] win2z = datZa[imp_win[0]:imp_win[1]] energy2 = energyFeat.energyCalc(win2x,win2y,win2z) #energy in post-impact win3x = datXa[post_win[0]:post_win[1]] win3y = datYa[post_win[0]:post_win[1]] win3z = datZa[post_win[0]:post_win[1]] energy3 = energyFeat.energyCalc(win3x,win3y,win3z) #****************************************************************************************************************************** #signal magnitude are in pre-impact sma1 = smaFeat.smafeat(datXa[pre_win[0]:pre_win[1]],datYa[pre_win[0]:pre_win[1]],datZa[pre_win[0]:pre_win[1]]) #signal magnitude are in impact sma2 = smaFeat.smafeat(datXa[imp_win[0]:imp_win[1]],datYa[imp_win[0]:imp_win[1]],datZa[imp_win[0]:imp_win[1]]) #signal magnitude are in pre-impact sma3 = smaFeat.smafeat(datXa[post_win[0]:post_win[1]],datYa[post_win[0]:post_win[1]],datZa[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #exponential moving average in pre-impact ewma1 = emafit.ema(datAr[pre_win[0]:pre_win[1]]) #exponential moving average in impact ewma2 = emafit.ema(datAr[imp_win[0]:imp_win[1]]) #exponential moving average in post-impact ewma3 = emafit.ema(datAr[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #Tilt Angle in pre-impact all_angle_1 = mytilt.mytilt(datXa[pre_win[0]:pre_win[1]],datYa[pre_win[0]:pre_win[1]],datZa[pre_win[0]:pre_win[1]], datXg[pre_win[0]:pre_win[1]],datYg[pre_win[0]:pre_win[1]],datZg[pre_win[0]:pre_win[1]]) angle_y_1 = numpy.max(numpy.abs(numpy.array(all_angle_1[0]))) angle_z_1 = numpy.max(numpy.abs(numpy.array(all_angle_1[1]))) #Tilt Angle in impact all_angle_2 = mytilt.mytilt(datXa[tilt_sample_impact[0]:tilt_sample_impact[1]],datYa[tilt_sample_impact[0]:tilt_sample_impact[1]], datZa[tilt_sample_impact[0]:tilt_sample_impact[1]],datXg[tilt_sample_impact[0]:tilt_sample_impact[1]], datYg[tilt_sample_impact[0]:tilt_sample_impact[1]],datZg[tilt_sample_impact[0]:tilt_sample_impact[1]]) angle_y_2 = numpy.max(numpy.abs(numpy.array(all_angle_2[0]))) angle_z_2 = numpy.max(numpy.abs(numpy.array(all_angle_2[1]))) #Tilt Angle in post-impact all_angle_3 = mytilt.mytilt(datXa[tilt_sample_post[0]:tilt_sample_post[1]],datYa[tilt_sample_post[0]:tilt_sample_post[1]], datZa[tilt_sample_post[0]:tilt_sample_post[1]],datXg[tilt_sample_post[0]:tilt_sample_post[1]], datYg[tilt_sample_post[0]:tilt_sample_post[1]],datZg[tilt_sample_post[0]:tilt_sample_post[1]]) angle_y_3 = numpy.max(numpy.abs(numpy.array(all_angle_3[0]))) angle_z_3 = numpy.max(numpy.abs(numpy.array(all_angle_3[1]))) #****************************************************************************************************************************** annot = micannot #datfeat = [minVal, maxVal, meanVal1, meanVal2, meanVal3, rms1, rms2, rms3, variance1, variance2, variance3, velo1, # velo2, velo3, energy1, energy2, energy3, sma1, sma2, sma3, ewma1, ewma2, ewma3, act_state, annot] datfeat = [minVal, maxVal, meanVal1, meanVal2, meanVal3, rms1, rms2, rms3, variance1, variance2, variance3, velo1, velo2, velo3, energy1, energy2, energy3, sma1, sma2, sma3, ewma1, ewma2, ewma3, angle_y_1, angle_z_1, angle_y_2, angle_z_2,angle_y_3,angle_z_3,act_state, annot] return datfeat
def test_integrals_of_linear_function(N,c=1): #c scales error (order of 1/N) f = lambda x: 2*x computed_answer = integrate(f,0,1,N) expected_answer = float(1) error = float(c)/N assert abs(computed_answer-expected_answer) < error
def test_integral_of_linear_function(): def f(x): return 2 * x for i in range(1, 4): assert integrate(f, 0, 1, 10**i) - 2 < 1E20
import integrator #you would have to package this into a python package '''create a directory, then create a file, call file __init__.py that makes the folder a python module put in the integrator = Integrator() integrator.method("midpoint") integrator.function(f) integrator.startpoint(0) integrator.end_point(200) #working example of this on website result = integrator.integrate()
f = lambda x: x*x a=0 b=1 N = 10000 start1 = time.time() integral = numpy_integrate(f,a,b,N) end1 = time.time() report.write("time taken for N = %i using numpy is %f \n"%(N,end1 - start1)) start2 = time.time() integral = integrate(f,a,b,N) end2 = time.time() report.write("time taken for N = %i NOT using numpy is %f \n"%(N,end2 - start2)) report.write("the difference of time2-time1 if %f \n"%((end2 - start2)-(end1 - start1))) if((end2 - start2) < (end1 - start1)): report.write("using numpy arrays slowed down the integrator by %i percent \n" %((((end1 - start1)/(end2 - start2))*100)-100)) if((end2 - start2) > (end1 - start1)): report.write("using numpy arrays speeds up the integrator by %i percent \n" %(((end2 - start2)/(end1 - start1))*100)) ########################################################### report.write("------------------------------------------------- \n")
def test_integral_of_constant_function(): def f(x): return 2 for i in range(1, 4): assert integrate(f, 0, 1, 10**i) - 2 < 1E20
def calc_features(fullarray, micannot, samp_rate): #vect_c not used pre_win = [0, 100] #pre-window 1s impact_max = [50, 150] #1s imp_win = [50, 650] # impact window 6s post_win = [300, 1200] #9 s, based on the paper it should be 250-1200 datAr = [] #for vector magnitude datXa = [] #for X values datYa = [] #for Y values datZa = [] #for Z values datXg = [] datYg = [] datZg = [] for tempdata in fullarray: datAr.append(float(tempdata[6])) datXa.append(float(tempdata[0])) datYa.append(float(tempdata[1])) datZa.append(float(tempdata[2])) datXg.append(float(tempdata[3])) datYg.append(float(tempdata[4])) datZg.append(float(tempdata[5])) #if micannot == 2: # print "fall" #else: # print "non-fall" #plt.plot(datAr) #plt.show() #****************************************************************************************************************************** #minimum pre impact min_pre = min(datAr[pre_win[0]:pre_win[1]]) #minimum value impact #min_imp = min(datAr[imp_win[0]:imp_win[1]]) #minimum post imp #min_post = min(datAr[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #maximum value pre-impact #max_pre = max(datAr[pre_win[0]:pre_win[1]]) #maximum value impact max_imp = max(datAr[impact_max[0]:impact_max[1]]) #maximum value post #max_post = max(datAr[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #mean for pre-impact event meanList1 = datAr[pre_win[0]:pre_win[1]] meanVal1 = numpy.mean(meanList1) #****************************************************************************************************************************** #mean for impact meanList2 = datAr[imp_win[0]:imp_win[1]] meanVal2 = numpy.mean(meanList2) #****************************************************************************************************************************** #mean for post-impact meanList3 = datAr[post_win[0]:post_win[1]] meanVal3 = numpy.mean(meanList3) #****************************************************************************************************************************** #Root mean square for pre-impact rmsList1 = datAr[pre_win[0]:pre_win[1]] rms1 = sqrt(mean(numpy.array(rmsList1)**2)) #Root mean square for impact rmsList2 = datAr[imp_win[0]:imp_win[1]] rms2 = sqrt(mean(numpy.array(rmsList2)**2)) #Root mean square for post-impact rmsList3 = datAr[post_win[0]:post_win[1]] rms3 = sqrt(mean(numpy.array(rmsList3)**2)) #****************************************************************************************************************************** #variance for pre-impact variance1 = numpy.var(datAr[pre_win[0]:pre_win[1]], ddof=1) #variance for impact variance2 = numpy.var(datAr[imp_win[0]:imp_win[1]], ddof=1) #variance for post-impact variance3 = numpy.var(datAr[post_win[0]:post_win[1]], ddof=1) #****************************************************************************************************************************** #velocity in pre-impact velo1 = integrator.integrate(datAr[pre_win[0]:pre_win[1]]) #velocity in impact velo2 = integrator.integrate(datAr[imp_win[0]:imp_win[1]]) #velocity in post-impact velo3 = integrator.integrate(datAr[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #energy in pre-impact win1x = datXa[pre_win[0]:pre_win[1]] win1y = datYa[pre_win[0]:pre_win[1]] win1z = datZa[pre_win[0]:pre_win[1]] energy1 = energyFeat.energyCalc(win1x, win1y, win1z) #energy in impact win2x = datXa[imp_win[0]:imp_win[1]] win2y = datYa[imp_win[0]:imp_win[1]] win2z = datZa[imp_win[0]:imp_win[1]] energy2 = energyFeat.energyCalc(win2x, win2y, win2z) #energy in post-impact win3x = datXa[post_win[0]:post_win[1]] win3y = datYa[post_win[0]:post_win[1]] win3z = datZa[post_win[0]:post_win[1]] energy3 = energyFeat.energyCalc(win3x, win3y, win3z) #****************************************************************************************************************************** #signal magnitude are in pre-impact sma1 = smaFeat.smafeat(datXa[pre_win[0]:pre_win[1]], datYa[pre_win[0]:pre_win[1]], datZa[pre_win[0]:pre_win[1]]) #signal magnitude are in impact sma2 = smaFeat.smafeat(datXa[imp_win[0]:imp_win[1]], datYa[imp_win[0]:imp_win[1]], datZa[imp_win[0]:imp_win[1]]) #signal magnitude are in pre-impact sma3 = smaFeat.smafeat(datXa[post_win[0]:post_win[1]], datYa[post_win[0]:post_win[1]], datZa[post_win[0]:post_win[1]]) #****************************************************************************************************************************** #exponential moving average in pre-impact ewma1 = emafit.ema(datAr[pre_win[0]:pre_win[1]]) #exponential moving average in impact ewma2 = emafit.ema(datAr[imp_win[0]:imp_win[1]]) #exponential moving average in post-impact ewma3 = emafit.ema(datAr[post_win[0]:post_win[1]]) #****************************************************************************************************************************** datfeat = [ min_pre, max_imp, meanVal1, meanVal2, meanVal3, rms1, rms2, rms3, variance1, variance2, variance3, velo1, velo2, velo3, energy1, energy2, energy3, sma1, sma2, sma3, ewma1, ewma2, ewma3, micannot ] return datfeat
def test_integral_of_constant_function(N): f = lambda x: 2 #Test function computed_answer = integrate(f,0,1,N) expected_answer = 2 assert abs(computed_answer-expected_answer) < 1**(-20)
@contextmanager def timeit_context(name): startTime = time.time() yield elapsedTime = round((time.time() - startTime) * 1000.0, 3) print('[{}] finished in {} ms'.format(name, elapsedTime)) f1 = lambda x: 2 * x with timeit_context('Python implementation, midpoint method, n=100'): sum_of_integral1 = integrator.integrate(f1, 0, 1, 100, method="midpoint", implementation="default") print(sum_of_integral1) with timeit_context('Numpy implementation, midpoint method, n=100'): sum_of_integral1 = integrator.integrate(f1, 0, 1, 100, method="midpoint", implementation="numpy") print(sum_of_integral1)