def __init__(self, m=None, alpha=0., sr=0.0, smoothing_interval=0.0, n=None, l=0.5, smoothing_interval_sat=0.0): self._alpha = alpha self._sr = sr self._l = l if n is None: self._m = m self._n = 1.0 / (1.0 - m) else: self._m = 1 - 1.0 / n self._n = n self.s_r = self._sr # smoothing for kr self._s0 = 1.0 - smoothing_interval if self._s0 < 1.: self._spline = spline.Spline(self._s0, self.k_relative(self._s0), self.d_k_relative(self._s0), 1.0, 1.0, 0.) # smoothing for sat self._pc0 = smoothing_interval_sat if self._pc0 > 0.: self._spline_sat = spline.Spline(0., 1., 0., self._pc0, self.saturation(self._pc0), self.d_saturation(self._pc0))
def test_optimal_n(): center = [0.1, -0.475, 0.425, 1.2, -1.2, 1.2] current_point = center print("Current: {}".format(current_point)) # Create a point a bit to the right of center goal_point = list(center) goal_point[0] = center[0] + 0.2 print("Goal: {}".format(goal_point)) # Create a point to the top of goal point next_point = list(goal_point) next_point[2] = goal_point[2] + 0.1 print("Next: {}".format(next_point)) spliner = spline.Spline(order=2) path = spliner.get_path(current_point[:3], goal_point[:3], next_point[:3], bezier=True) print("Optimal number of points") assert (len(path) == 3261) print(len(path))
def make_te_shear_filt(l_vec, cl_te, cl_te_lens, cl_ee_lens, n_l_ee, cl_tt_lens, n_l_tt): a = 1 / ((cl_te_lens)**2 + (cl_ee_lens + n_l_ee) * (cl_tt_lens + n_l_tt)) lnl = np.log(l_vec) dcdl = diffSpectraManual(l_vec, cl_te) #should it be cl_ee_lens?? dcdlnl = dcdl * l_vec #cl_ee_lens?? d = dcdlnl g = a * d N_integrand = 0.5 * g * d * l_vec / (2 * pi) N = np.trapz(N_integrand) filt_vec = g / N filt_vec[l_vec.size - 1] = 0. filt_vec[l_vec.size - 2] = 0. filt_fun = spline.Spline(l_vec, filt_vec) plt.plot(l_vec[0:lmax], filt_vec[0:lmax]) plt.title('Shear Filter in Harmonic Space') plt.xlabel('l') plt.ylabel('$K^{TE}_{\gamma}(l)$') plt.savefig(working_dir + '/Filters/HS_filter_1d' + '_' + spec_print + '_shear_' + exp_print + '.png') plt.show() return (filt_vec, filt_fun)
def interpolCef(self): s = spline.Spline(self.t, self.f) self.cef = s.write_cef() self.showMessageBox( 'Успешно', 'К-ты интерполяции функции ' + self.fName + '\n Вычислены') self.buttonT.setEnabled(True)
def make_ee_shear_filt(l_vec, cl_ee, cl_ee_lens, n_l_ee): a = (cl_ee / (cl_ee_lens + n_l_ee)**2) lnCl = np.log(cl_ee) lnl = np.log(l_vec) d = np.zeros(shape=(l_vec.size)) dx = np.zeros(shape=(l_vec.size)) dy = np.zeros(shape=(l_vec.size)) dcdl = diffSpectraManual(l_vec, cl_ee) #should it be cl_ee_lens?? d = dcdl * l_vec / cl_ee #gives dlncdlnl # use cl_ee_lens?? #filt_vec=a*d g = a * d N_integrand = 0.5 * g * cl_ee * d * l_vec / (2 * pi) N = np.trapz(N_integrand) filt_vec = g / N filt_vec[l_vec.size - 1] = 0. filt_vec[l_vec.size - 2] = 0. filt_fun = spline.Spline(l_vec, filt_vec) plt.plot(l_vec[0:lmax], filt_vec[0:lmax]) plt.title('Shear Filter in Harmonic Space') plt.xlabel('l') plt.ylabel('$K^{EE}_{\gamma}(l)$') plt.savefig(working_dir + '/Filters/HS_filter_1d' + '_' + spec_print + '_shear_' + exp_print + '.png') plt.show() return (filt_vec, filt_fun)
def make_ee_conv_filt(l_vec, cl_ee, cl_ee_lens, n_l_ee): a = (cl_ee / (cl_ee_lens + n_l_ee)**2) lnCl = np.log(cl_ee) lnl = np.log(l_vec) d = np.zeros(shape=(l_vec.size)) dx = np.zeros(shape=(l_vec.size)) dy = np.zeros(shape=(l_vec.size)) for k in range(l_vec.size - 1): if cl_ee[k] < 0: lnCl[k] = 0. dcdl = diffSpectraManual(l_vec, cl_ee) #should it be cl_ee_lens?? dlncdlnl = dcdl * l_vec / cl_ee #cl_ee_lens?? d = dlncdlnl + 2. g = a * d N_integrand = g * cl_ee * d * l_vec / (2 * pi) N = np.trapz(N_integrand) filt_vec = g / N filt_vec[l_vec.size - 1] = 0. filt_vec[l_vec.size - 2] = 0. filt_fun = spline.Spline(l_vec, filt_vec) plt.plot(l_vec[0:lmax], filt_vec[0:lmax]) plt.title('Convergence Filter in Harmonic Space') plt.xlabel('l') plt.ylabel('$K^{EE}_{\kappa_0}(l)$') plt.savefig(working_dir + '/Filters/HS_filter_1d' + '_' + spec_print + '_conv_' + exp_print + '.png') plt.show() return (filt_vec, filt_fun)
def make_tt_conv_filt(l_vec, cl_tt, cl_tt_lens, n_l_tt): a = (cl_tt / (cl_tt_lens + n_l_tt)**2) print 'type of thing giving overflow', type(a[0]) lnCl = np.log(cl_tt) lnl = np.log(l_vec) d = np.zeros(shape=(l_vec.size)) dx = np.zeros(shape=(l_vec.size)) dy = np.zeros(shape=(l_vec.size)) dcdl = diffSpectraManual(l_vec, cl_tt) #should it be cl_tt_lens?? dlncdlnl = dcdl * l_vec / cl_tt #cl_tt_lens?? d = dlncdlnl + 2. g = a * d #for normalisation: #are we assuming radial symmetry (DALIAN) N_integrand = g * cl_tt * d * l_vec / (2 * pi) N = np.trapz(N_integrand) print N filt_vec = g / N filt_vec[l_vec.size - 1] = 0. filt_fun = spline.Spline(l_vec, filt_vec) plt.figure() plt.plot(l_vec[0:lmax], filt_vec[0:lmax]) plt.title('Convergence Filter in Harmonic Space') plt.xlabel('l') plt.ylabel('$K^{TT}_{\kappa_0}(l)$') plt.savefig(working_dir + '/Filters/HS_filter_1d' + '_' + spec_print + '_conv_' + exp_print + '.png') plt.show() return (filt_vec, filt_fun)
def get_Ms(T): Ts = [ 0, 5.659811, 88.011299, 196.820557, 323.276550, 434.977448, 517.180664, 587.483459, 634.108887, 657.131226, 668.426147, 673.925354, 679.424500 ] Ms = [ .995, 0.995475, 0.995475, 0.990950, 0.986425, 0.963801, 0.909502, 0.805430, 0.647059, 0.461538, 0.289593, 0.149321, 0.009050 ] for k in range(len(Ms)): Ms[k] = Ms[k] / .995 Curve = spline.Spline(Ts, Ms) return Curve(T)
def test_spline(): # Define 3 points cur_pt = [5, 7, 13] goal_pt = [1, 9, 0] next_pt = [15, 2, 11] # Create our spliner object spliner = spline.Spline(order=2) # Given the 3 points, generate the spline path as a bezier curve path = spliner.get_path(cur_pt, goal_pt, next_pt, n=30, bezier=True) # Print the points in the path for axis in range(len(path)): print(path[axis])
def make_tb_shear_filt(l_vec, cl_te, cl_tt_lens, cl_bb_lens, n_l_tt, n_l_bb): filt_vec = cl_te / ((cl_tt_lens + n_l_tt) * (cl_bb_lens + n_l_bb)) N_integrand = filt_vec * cl_te * l_vec / (2 * pi) N = np.trapz(N_integrand) filt_vec /= N filt_fun = spline.Spline(l_vec, filt_vec) plt.plot(l_vec[0:lmax], filt_vec[0:lmax]) plt.title('Shear Filter in Harmonic Space') plt.xlabel('l') plt.ylabel('$K^{TB}_{\gamma}(l)$') plt.savefig(working_dir + '/Filters/HS_filter_1d' + '_' + spec_print + '_shear_' + exp_print + '.png') plt.show() return (filt_vec, filt_fun)
def plotit(file): f = open(file, 'rU') T, Ms = [], [] for line in f.readlines(): rec = line.split() T.append(float(rec[0])) Ms.append(float(rec[1])) pylab.scatter(T, Ms, marker='o', c='r', s=20) pylab.draw() Curve = spline.Spline(T, Ms) Tpred, Mspred = [], [] t, MsP = 0, 1 while 1: MsP = Curve(t) if MsP > .2: Mspred.append(MsP) Tpred.append(t) t += 1 else: break pylab.plot(Tpred, Mspred, 'r--') pylab.draw()
def getUspline(knots, rcut, rs, MaxPairEnekBT=20, kB=1, TempSet=1): """calculate spline potential and adjust the hard core region to match the tabulated pair potentials generated by sim (PotentialTablePair class in sim/export/lammps.py)""" myspline = spline.Spline(rcut, knots) u_spline = [] du_spline = [] for r in rs: u_spline.append(myspline.Val(r)) du_spline.append(myspline.DVal(r)) u_spline = np.array(u_spline) #get the maximum pair energy MaxPairEne = kB * TempSet * MaxPairEnekBT #indices where energy is greater ind = np.where(u_spline > MaxPairEne)[0] if len(ind): #find the first index where energy is valid i = ind[-1] + 1 #do a linear extrapolation in the hard core region u_spline[:i] = (rs[i] - rs[:i]) * -du_spline[i] + u_spline[i] for j in ind: du_spline[j] = du_spline[i] return u_spline, du_spline
def run(self): print("### main flow start ###") cap = cv2.VideoCapture(0) cap.set(3, self.width) cap.set(4, self.height) cap.set(6, 30) # Initialize serial communication txt = self.ser.readline() # clear serial buffer start_msg = self.encodeStartMsg( [self.q_angle, self.q_bias, self.r_measure], self.angvel) self.ser.write(start_msg) txt = self.ser.readline().decode('utf-8') while (len(txt) < 2): print("Reopen") self.ser.close() self.ser.open() time.sleep(1) self.ser.write(start_msg) txt = self.ser.readline().decode('utf-8') print("Q_angle, Q_bias, R_measure:" ) # ensure covariances to be successfully set print(txt) # Populate data: IMU i = 0 while i < (self.s_sample * 2): msg = self.encodeMsg(self.relay) self.ser.write(msg) txt = self.ser.readline() if (txt): values = txt.decode('utf-8').split(',') self.theta[i] = float(values[0]) % 180 self.roll[i] = float(values[1]) self.pitch[i] = float(values[2]) ret, frame = cap.read() if i == 0: self.matchFeaturesInit(frame) if i >= self.s_sample: self.bench[i - self.s_sample] = frame self.matchFeatures(frame) i = i + 1 # Proceed if txt successfully received; else retry t = time.time() # Initialize spline objects & targets pitch_spline = spline.Spline(self.s_sample, self.s_alpha) roll_spline = spline.Spline(self.s_sample, self.s_alpha) pitch_tar = pitch_spline.findSpline(self.pitch[0], self.pitch[0], self.pitch[self.s_sample], self.pitch[2 * self.s_sample - 1]) roll_tar = roll_spline.findSpline(self.roll[0], self.roll[0], self.roll[self.s_sample], self.roll[2 * self.s_sample - 1]) # Start main flow nth = 0 fcount = 0 while 1: msg = self.encodeMsg(self.relay) self.ser.write(b'1') txt = self.ser.readline() if (txt): #(1) Collect data & Store into waiting line values = txt.decode('utf-8').split(',') self.t = float(values[0]) % 180 self.r = float(values[1]) self.p = float(values[2]) self.theta[2 * self.s_sample + nth] = float(values[0]) % 180 self.roll[2 * self.s_sample + nth] = float(values[1]) self.pitch[2 * self.s_sample + nth] = float(values[2]) ret, frame = cap.read() self.bench[self.s_sample + nth] = frame #(2) Warp based on spline targets roll_bias = -self.roll[self.s_sample + nth] pitch_bias = self.pitch[ self.s_sample + nth] * 0.5 #pitch_tar[nth] #self.pitch[self.s_sample + nth] - dz = self.findZ(self.theta[self.s_sample + nth]) H = self.findHomography( roll_bias, pitch_bias, 0., np.array([[0.], [(dz - 13.5) * 1.1], [0.]])) warped = cv2.warpPerspective(self.bench[nth], H, (self.width, self.height)) ### Save screenshot ### fname = '../data/0630/unstab/' + str(fcount) + '.jpg' #cv2.imwrite(fname, self.bench[nth]) cv2.imwrite(fname, warped) #(3) Find & track features self.matchFeatures(warped) self.window[0] = self.window[0] + self.dispField[1] * 1.0 #y self.window[1] = self.window[1] + self.dispField[0] * 1.0 #(4) cover inherent motion in pixelwise # pitch self.window[0] = self.window[0] - self.d * self.sin(3) #(5) Check if exceeds boundary exc = [0, 0, 0, 0] if self.window[0] - 0.5 * self.dist_height <= 0: #up self.window[0] = 0.5 * self.dist_height exc[0] = 1 elif self.window[ 0] + 0.5 * self.dist_height >= self.height: #down self.window[0] = self.height - (0.5 * self.dist_height) exc[1] = 1 if (self.window[1] - 0.5 * self.dist_width) <= 0: #left self.window[1] = 0.5 * self.dist_width exc[2] = 1 elif (self.window[1] + 0.5 * self.dist_width) >= self.width: #right self.window[1] = self.width - (0.5 * self.dist_width) exc[3] = 1 self.vb = [ int(self.window[1] - 0.5 * self.dist_width), int(self.window[1] - 0.5 * self.dist_width) + self.dist_width ] self.hb = [ int(self.window[0] - 0.5 * self.dist_height), int(self.window[0] - 0.5 * self.dist_height) + self.dist_height ] #(6) Final crop crop = warped[self.hb[0]:(self.hb[0] + self.dist_height), self.vb[0]:(self.vb[0] + self.dist_width), :] ### Save screenshot ### fname = '../data/0630/stab/' + str(fcount) + '.jpg' cv2.imwrite(fname, crop) #(7) Final display self.plotBound(warped) if self.displayOpt == False: dispImage = cv2.cvtColor(warped, cv2.COLOR_BGR2RGB) else: dispImage = cv2.cvtColor(crop, cv2.COLOR_BGR2RGB) dispImage = cv2.resize(dispImage, (960, 720)) convertToQtFormat = QImage( dispImage.data, dispImage.shape[1], dispImage.shape[0], QImage.Format_RGB888) #QImage.Format_Indexed8 self.changePixmap.emit(convertToQtFormat) #(8) Update & push bench frames & sensor data if nth == self.s_sample - 1: # Spline nodes pitch_tar = pitch_spline.findSpline( self.pitch[0], self.pitch[self.s_sample], self.pitch[self.s_sample * 2], self.pitch[self.s_sample * 3 - 1]) roll_tar = roll_spline.findSpline( self.roll[0], self.roll[self.s_sample], self.roll[self.s_sample * 2], self.roll[self.s_sample * 3 - 1]) for i in range(self.s_sample * 2): self.theta[i] = self.theta[i + self.s_sample] self.roll[i] = self.roll[i + self.s_sample] self.pitch[i] = self.pitch[i + self.s_sample] for i in range(self.s_sample): self.bench[i] = self.bench[i + self.s_sample] nth = 0 else: nth = nth + 1 fcount = fcount + 1 if cv2.waitKey(1) & 0xFF == ord('q'): cap.release()
l_vec, cl_tt, cl_ee, cl_te, cl_tt_lens, cl_ee_lens, cl_bb_lens, cl_te_lens, cl_kk = spectra( ) #to get deflection spec without messing with other code cl_dd = cl_kk * 4 / (l_vec * (l_vec + 1.0)) cl_pp = cl_dd / (l_vec * (l_vec + 1.0)) rms_tt = np.sqrt(cl_tt) rms_e1 = cl_te / rms_tt cl_e2 = cl_ee - (cl_te**2 / cl_tt) rms_e2 = np.sqrt(cl_e2) cl_tt_func = spline.Spline(l_vec, cl_tt) cl_ee_func = spline.Spline(l_vec, cl_ee) cl_te_func = spline.Spline(l_vec, cl_te) cl_tt_lens_func = spline.Spline(l_vec, cl_tt_lens) cl_ee_lens_func = spline.Spline(l_vec, cl_ee_lens) cl_te_lens_func = spline.Spline(l_vec, cl_te_lens) cl_kk_func = spline.Spline(l_vec, cl_kk) cl_dd_func = spline.Spline(l_vec, cl_dd) cl_pp_func = spline.Spline(l_vec, cl_pp) rms_tt_func = spline.Spline(l_vec, rms_tt) rms_e1_func = spline.Spline(l_vec, rms_e1) rms_e2_func = spline.Spline(l_vec, rms_e2)
def make_null_filt(l_vec): zero = np.zeros(shape=(l_vec.size)) filt_vec = zero filt_fun = spline.Spline(l_vec, filt_vec) return (filt_vec, filt_fun)
import math, matplotlib, sys, spline matplotlib.use("TkAgg") import pylab pylab.ion() Tc = 1. file = sys.argv[2] f = open(file, 'rU') T, Ms = [], [] for line in f.readlines(): rec = line.split() T.append(float(rec[0])) Ms.append(float(rec[1])) #pylab.plot(T,Ms,'r') pylab.scatter(T, Ms, marker='s', c='b', s=20) pylab.draw() Curve = spline.Spline(T, Ms) Tpred, Mspred = [], [] for k in range(100): t = float(k) / 100. MsP = Curve(t) Mspred.append(MsP) Tpred.append(t) #pylab.plot(Tpred,Mspred,'r') #pylab.draw() while 1: Te, Me = [], [] x = raw_input("x [0.43]") if x == "": x = 0.43 else: x = float(x)