def render(self, mode='human'): from gym.envs.classic_control import rendering from pyglet.gl import glRotatef, glPushMatrix def draw_lasting_circle(Viewer, radius=10, res=30, filled=True, **attrs): geom = rendering.make_circle(radius=radius, res=res, filled=filled) rendering._add_attrs(geom, attrs) Viewer.add_geom(geom) return geom def draw_lasting_line(Viewer, start, end, **attrs): geom = rendering.Line(start, end) rendering._add_attrs(geom, attrs) Viewer.add_geom(geom) return geom def make_ellipse(major=10, minor=5, res=30, filled=True): points = [] for i in range(res): ang = 2*np.pi*i / res points.append((np.cos(ang)*major, np.sin(ang)*minor)) if filled: return rendering.FilledPolygon(points) else: return rendering.PolyLine(points, True) def draw_ellipse(Viewer, major=10, minor=5, res=30, **attrs): geom = make_ellipse(major=major, minor=minor, res=res, filled=True) rendering._add_attrs(geom, attrs) Viewer.add_onetime(geom) return geom a,b,c = self.a,self.b,self.c x,y,theta = self.pos alpha_1, alpha_2 = self.shape theta = np.array([0, -alpha_1, alpha_2]) + theta x1 = x - np.cos(theta[0])*a[0] - np.cos(theta[1])*a[1] y1 = y - np.sin(theta[0])*a[0] - np.sin(theta[1])*a[1] x2 = x + np.cos(theta[0])*a[0] + np.cos(theta[2])*a[2] y2 = y + np.sin(theta[0])*a[0] + np.sin(theta[2])*a[2] x = np.array([x,x1,x2]) y = np.array([y,y1,y2]) from gym.envs.classic_control import rendering if self.viewer is None: self.viewer = rendering.Viewer(1000,200) background = draw_lasting_circle(self.viewer,radius=100, res=10) # background.set_color(.8,.9,.99) background.set_color(1.,1.,1.) bound = 4 self.viewer.set_bounds(-bound+2,bound+4,-bound/4,bound/4) axisX = self.viewer.draw_line((-1000., 0), (1000., 0)) axisY = self.viewer.draw_line((0,-1000.), (0,1000.)) axisX.set_color(.5,.5,.5) axisY.set_color(.5,.5,.5) for i in range(3): link = draw_ellipse(self.viewer,major=self.a[i], minor=self.b[i], res=30, filled=True) lkTrans = rendering.Transform(rotation=theta[i],translation=(x[i],y[i])) link.add_attr(lkTrans) if i%3 == 0: link.set_color(.7, .1, .1) if i%3 == 1: link.set_color(.1, .7, .1) if i%3 == 2: link.set_color(.1, .1, .7) for i in range(2): eye = draw_ellipse(self.viewer,major=self.a[2]/8, minor=self.a[2]/8, res=30, filled=True) eyngle = theta[2]+np.pi/5.5*(i-.5)*2; eyeTrans = rendering.Transform(translation=(x[2]+np.cos(eyngle)*self.a[2]/2,y[2]+np.sin(eyngle)*self.a[2]/2)) eye.add_attr(eyeTrans) eye.set_color(.6,.3,.4) Npts = 7*2 headl = 1.3 facel = .6 facew = 2.4 headw = 2.6 neckw = 2.3 bodyw = 2.2 waist = 2 tailw = 1.7 taill = 2.2 referenceX = np.zeros((Npts,)) referenceY = np.zeros((Npts,)) referenceX[7], referenceY[7] = x2 + np.cos(theta[2])*a[2]*headl, y2 + np.sin(theta[2])*a[2]*headl referenceX[6], referenceY[6] = x2 + np.cos(theta[2])*a[2]*facel - np.sin(theta[2])*b[2]*facew, y2 + np.sin(theta[2])*a[2]*facel + np.cos(theta[2])*b[2]*facew referenceX[-6], referenceY[-6] = x2 + np.cos(theta[2])*a[2]*facel + np.sin(theta[2])*b[2]*facew, y2 + np.sin(theta[2])*a[2]*facel - np.cos(theta[2])*b[2]*facew referenceX[5], referenceY[5] = x2 - np.sin(theta[2])*b[2]*headw, y2 + np.cos(theta[2])*b[2]*headw referenceX[-5], referenceY[-5] = x2 + np.sin(theta[2])*b[2]*headw, y2 - np.cos(theta[2])*b[2]*headw referenceX[4], referenceY[4] = x[0] + np.cos(theta[0])*a[0] - np.sin((theta[0]+theta[2])/2)*(b[2]+b[0])*neckw/2, y[0] + np.sin(theta[0])*a[0] + np.cos((theta[0]+theta[2])/2)*(b[2]+b[0])*neckw/2 referenceX[-4], referenceY[-4] = x[0] + np.cos(theta[0])*a[0] + np.sin((theta[0]+theta[2])/2)*(b[2]+b[0])*neckw/2, y[0] + np.sin(theta[0])*a[0] - np.cos((theta[0]+theta[2])/2)*(b[2]+b[0])*neckw/2 referenceX[3], referenceY[3] = x[0] - np.sin(theta[0])*b[0]*bodyw, y[0] + np.cos(theta[0])*b[0]*bodyw referenceX[-3], referenceY[-3] = x[0] + np.sin(theta[0])*b[0]*bodyw, y[0] - np.cos(theta[0])*b[0]*bodyw referenceX[2], referenceY[2] = x[0] - np.cos(theta[0])*a[0] - np.sin((theta[0]+theta[1])/2)*(b[1]+b[0])*waist/2, y[0] - np.sin(theta[0])*a[0] + np.cos((theta[0]+theta[1])/2)*(b[1]+b[0])*waist/2 referenceX[-2], referenceY[-2] = x[0] - np.cos(theta[0])*a[0] + np.sin((theta[0]+theta[1])/2)*(b[1]+b[0])*waist/2, y[0] - np.sin(theta[0])*a[0] - np.cos((theta[0]+theta[1])/2)*(b[1]+b[0])*waist/2 referenceX[1], referenceY[1] = x1 - np.sin(theta[1])*b[1]*tailw, y1 + np.cos(theta[1])*b[1]*tailw referenceX[-1], referenceY[-1] = x1 + np.sin(theta[1])*b[1]*tailw, y1 - np.cos(theta[1])*b[1]*tailw referenceX[0], referenceY[0] = x1 - np.cos(theta[1])*a[1]*taill, y1 - np.sin(theta[1])*a[1]*taill referenceX = np.append(referenceX,referenceX[0]) referenceY = np.append(referenceY,referenceY[0]) from scipy.interpolate import CubicSpline p = np.linspace(0,1,num=Npts+1) cs = CubicSpline(p, np.stack([referenceX,referenceY]).T,bc_type='periodic') pnew = np.linspace(0,1,num=200) outout = cs(pnew) reference = [] for i in range(np.size(outout,0)): reference.append((outout[i,0],outout[i,1])) # from scipy.interpolate import splprep, splev # tck, u = splprep([referenceX, referenceY], s=0.05) # out = splev(pnew, tck) # for i in range(out[0].size): # reference.append((out[0][i],out[1][i])) fish = self.viewer.draw_polygon(reference, filled=False) fish.set_linewidth(2) fish.set_color(.5,.5,.5) """trail""" if self.oldpos is not None: # trail = draw_lasting_line(self.viewer, (self.oldpos[0],self.oldpos[1]), (self.pos[0],self.pos[1])) trail = draw_lasting_circle(self.viewer, radius=0.02, res = 5) trTrans = rendering.Transform(translation=(np.sum(x)/3,np.sum(y)/3)) trail.add_attr(trTrans) dx = self.pos[0]-self.oldpos[0] dy = self.pos[1]-self.oldpos[1] # print((dx**2+dy**2)**0.1) trail.set_color(2.2-(dx**2+dy**2)**0.1*4, 2.2-(dx**2+dy**2)**0.1*4, 2.2-(dx**2+dy**2)**0.1*4) # trail.linewidth.stroke = 10 return self.viewer.render(return_rgb_array = mode=='rgb_array')
def Mu(t, T, mu): cs = CubicSpline([0, 0.5 * T, T], mu) return cs(t) # use when mu a 3-point cubic spline
def cspline_antideriv(x, y, axis=0): return CubicSpline(x, y, axis).antiderivative()
import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import CubicSpline global G, c G = 6.67e-8 c = 3e10 #Interpolating the EOS sly = np.genfromtxt("SLy.txt", delimiter=" ") #nbs=sly[:,1] Es = sly[:, 2] Ps = sly[:, 3] cPs = CubicSpline(Es, Ps) crs = CubicSpline(Ps, Es) #cns=CubicSpline(Ps,nbs) fps = np.genfromtxt("FPS.txt", delimiter=" ") #nbf=fps[:,1] Ef = fps[:, 2] Pf = fps[:, 3] cPf = CubicSpline(Ef, Pf) crf = CubicSpline(Pf, Ef) #cnf=CubicSpline(Pf,nbf) apr = np.genfromtxt("apr.txt", delimiter=" ") #nba=apr[:,0]*1e14*c*c Ea = apr[:, 1] * 1e14
def horizontal_geodesic(curve_a, curve_b, n_times=10, threshold=1e-3): """Compute the horizontal geodesic between curve_a and curve_b in the fiber bundle induced by the action of reparameterizations. """ dim = curve_a.shape[1] Rdim = Euclidean(dim) curves = DiscreteCurves(ambient_manifold=Rdim) n_points = curve_a.shape[0] - 1 t_space = np.linspace(0., 1., n_points + 1) t_time = np.linspace(0., 1., n_times + 1) spline_a = CubicSpline(t_space, curve_a, axis=0) spline_b = CubicSpline(t_space, curve_b, axis=0) initial_curve = curve_a.copy() end_curve = curve_b.copy() gap = 1. while (gap > threshold): # Compute geodesic path of curves srv_geod_fun = curves.square_root_velocity_metric.geodesic( initial_curve=initial_curve, end_curve=end_curve) geod = srv_geod_fun(t_time) M, K, cs_ver, cs_hor = hvsplit(geod) # Compute path of reparameterizations phi = np.zeros((n_times + 1, n_points + 1)) phi_t = np.zeros((n_times + 1, n_points)) phi_s = np.zeros((n_times, n_points)) test_phi = np.zeros(n_times) phi[0, :] = np.linspace(0., 1., n_points + 1) phi[:, -1] = np.ones(n_times + 1) for j in range(n_times): phi_t[j, 0] = n_points * (phi[j, 1] - phi[j, 0]) phi_s[j, 0] = phi_t[j, 0] * M[j, 0] / (n_points * K[j, 0]) phi[j + 1, 0] = phi[j, 0] + 1 / n_times * phi_s[j, 0] for k in range(1, n_points): # Matlab k = 2 : n if M[j, k] > 0: phi_t[j, k] = n_points * (phi[j, k + 1] - phi[j, k]) else: phi_t[j, k] = n_points * (phi[j, k] - phi[j, k - 1]) phi_s[j, k] = phi_t[j, k] * M[j, k] / (n_points * K[j, k]) phi[j + 1, k] = phi[j, k] + 1 / n_times * phi_s[j, k] test_phi[j] = np.sum(phi[j + 1, 2:] - phi[j + 1, 1:-1] < 0) if np.any(test_phi): print(test_phi) print( 'Warning: phi(s) is non increasing for at least one time s.' ) # Compute horizontal path of curves horizontal_path = np.zeros(geod.shape) horizontal_path[0, :, :] = curve_a for j in range(1, n_times): spline_j = CubicSpline(t_space, geod[j, :, :], axis=0) phi_inverse = CubicSpline(phi[j, :], t_space) horizontal_path[j, :, :] = spline_j(phi_inverse(t_space)) phi_inverse = CubicSpline(phi[-1, :], t_space) horizontal_path[-1, :, :] = spline_b(phi_inverse(t_space)) new_end_curve = horizontal_path[-1, :, :] gap = (np.sum(np.linalg.norm(new_end_curve - end_curve, axis=-1)**2))**(1 / 2) end_curve = new_end_curve.copy() print(gap) return horizontal_path
bq_t[:,i] = row[8:12] bp_t[:,i] = row[12:15] # clean up temporary CSV os.system('rm ./' + csvfn) # assume orientation of plate is constant Rot_board = Rotation.from_quat(bq_t.T)[0] rot_t = Rot_board.inv() * Rotation.from_quat(q_t.T) p_t = Rot_board.inv().apply(p_t.T - bp_t.T).T t = t-t[0] # calculate derivatives with spline interpolation rspline = RotationSpline(t, rot_t) pspline = CubicSpline(t, p_t.T) quat_t = rspline(t).as_quat().T w_t = rspline(t, 1).T dp_t = pspline(t, 1).T # butterworth filter of order 2 to smooth velocity states # sampling frequency fs = 40. # Cut-off frequency of angular velocity filter. < 20.0 (Nyquist) fc_w = 5. # Cut-off frequency of linear velocity filter. < 20.0 (Nyquist)
def generateTrajectory(self, **kwargs): from scipy.interpolate import CubicSpline # Load parameters of the trajectory self.setParametersFromDict(**kwargs) t_crouch = self.getParameter('t_crouch') t_jump = self.getParameter('t_jump') t_air = self.getParameter('t_air') dt = self.getParameter('dt') # Define trajectory for return traj = ActuatorsTrajectory() # Define time of the trajectory t_traj = np.arange(0, t_air, dt) # Define the different configurations of the jump pos_crouch = np.array([[0, pi/2, -pi], \ [0, pi/2, -pi], \ [0, -pi/2, pi], \ [0, -pi/2, pi]]) q_stand = self.getParameter('q_start') q_crouch = np.zeros(12) q_air = np.zeros(12) for leg in range(4): for art in range(3): q_crouch[3 * leg + art] = 0.7 * pos_crouch[leg, art] q_air[3 * leg + art] = 0.4 * pos_crouch[leg, art] # interpolation with a cubic spline # part 1 : a smooth trajectory for the crouching x = np.array([0, t_crouch]) y = np.zeros((2, 12)) y[0, :] = q_stand y[1, :] = q_crouch xnew0 = np.arange(0, t_crouch, dt) traj0 = np.zeros((len(xnew0), 12)) qtraj0 = np.zeros((len(xnew0), 12)) for art in range(12): cs = CubicSpline(x, y[:, art], bc_type='clamped') traj0[:, art] = cs(xnew0) qtraj0[:, art] = cs(xnew0, 1) # part 2 : the jump x = np.array([t_crouch, t_jump]) y = np.zeros((2, 12)) y[0, :] = q_crouch y[1, :] = q_stand xnew1 = np.arange(t_crouch, t_jump, dt) traj1 = np.zeros((len(xnew1), 12)) qtraj1 = np.zeros((len(xnew1), 12)) for art in range(12): cs = CubicSpline(x, y[:, art], bc_type='clamped') traj1[:, art] = cs(xnew1) qtraj1[:, art] = cs(xnew1, 1) # part 3 : reaching a good position for the reception x = np.array([t_jump, t_air]) y = np.zeros((2, 12)) y[0, :] = q_stand y[1, :] = q_air xnew2 = np.arange(t_jump, t_air, dt) traj2 = np.zeros((len(xnew2), 12)) qtraj2 = np.zeros((len(xnew2), 12)) for art in range(12): cs = CubicSpline(x, y[:, art], bc_type='clamped') traj2[:, art] = cs(xnew2) qtraj2[:, art] = cs(xnew2, 1) # part 4 : building the whole trajectory xnew = np.concatenate((xnew0, xnew1, xnew2)) q_traj = np.concatenate((traj0, traj1, traj2), axis=0) qdot_traj = np.concatenate((qtraj0, qtraj1, qtraj2), axis=0) # let's set the gain during the trajectory gains = np.zeros((len(xnew), 2)) for i in range(len(xnew)): if xnew[i] < t_crouch: gains[i, :] = np.array([0.1, 0.01]) if xnew[i] > t_crouch and xnew[i] < t_jump: gains[i, :] = np.array([0.01, 0.001]) else: gains[i, :] = np.array([0.1, 0.01]) # Define trajectory for return traj = ActuatorsTrajectory() traj.addElement('t', t_traj) traj.addElement('q', q_traj) traj.addElement('q_dot', qdot_traj) traj.addElement('gains', gains) return traj
def trajectoryIK(self, start, end): dur = self.config["trajectory_duration"] joints = np.array( [self.positions[joint] for joint in self.joint_names], dtype=np.int32 ).reshape(9, 1) constraints = list() # Constrain the start position start_pose = np.concatenate(start).reshape(9, 1) start_constraint = ik.PostureConstraint( self.robot, np.array([0.0, 0.0]).reshape([2,1]) # Active time ) start_constraint.setJointLimits( joints, start_pose - self.config['pose_tol'], start_pose + self.config['pose_tol'] ) constraints.append(start_constraint) # Constrain the end position end_pose = np.concatenate(end).reshape(9, 1) end_constraint = ik.PostureConstraint( self.robot, np.array([dur, dur]).reshape([2,1]) # Active time ) end_constraint.setJointLimits( joints, end_pose - self.config['pose_tol'], end_pose + self.config['pose_tol'] ) constraints.append(end_constraint) # Constrain against collisions constraints.append( ik.MinDistanceConstraint ( self.robot, self.config['collision_min_distance'], # Minimum distance between bodies list(), # Active bodies (empty set means all bodies) set() # Active collision groups (not filter groups! Empty set means all) )) # Prepare times of interest for trajectory solve times = np.linspace(0, dur, num=self.config['trajectory_count'], endpoint=True) # Compute cubic interpolation as seed trajectory x = np.array([0, dur]) y = np.hstack([start_pose, end_pose]) f = CubicSpline(x, y, axis=1, bc_type='clamped') q_seed = f(times) # print "q_seed:", q_seed # Solve the IK problem options = ik.IKoptions(self.robot) options.setMajorIterationsLimit(400) options.setFixInitialState(True) # print "Iterations limit:", options.getMajorIterationsLimit() result = ik.InverseKinTraj(self.robot, times, q_seed, q_seed, constraints, options) if result.info[0] != 1: print "Could not find safe trajectory!" print "Start pose:", start_pose.flatten() print "End pose: ", end_pose.flatten() print "Result:", result.info return None else: # Clean up: Pack solutions as a matrix, with time as first column q_sol = np.vstack(result.q_sol) q_sol = np.insert(q_sol, [0], times[:, None], axis=1) return q_sol
def main(): # Location of APS 2018-1 data prj_fld = '/mnt/r/X-ray Radiography/APS 2018-1/' # Save location for the plots plots_folder = create_folder('{0}/Figures/Jet_HorizSumm/'.format(prj_fld)) # Scintillator scintillators = ['LuAG', 'YAG'] # KI % KI_conc = [0, 1.6, 3.4, 5.3, 8, 10, 11.1] # Test matrix test_matrix = pd.read_csv('{0}/APS White Beam.txt'.format(prj_fld), sep='\t+', engine='python') # Crop down the test matrix test_matrix = test_matrix[['Test', 'Nozzle Diameter (um)', 'KI %']].copy() for scint in scintillators: # Processed data sets location prc_fld = '{0}/Processed/{1}/Summary/'.format(prj_fld, scint) # Groups rpkT = 'Ratio Peak T' relT = 'Ratio Ellipse T' # Horizontal variation horiz_matrix = test_matrix[test_matrix['Test'].str.contains( 'mm')].copy() horiz_matrix['X Position'] = [ get_xpos('{0}/{1}_{2}.pckl'.format(prc_fld, scint, x)) for x in horiz_matrix['Test'] ] # Sort horiz_matrix by X position, re-index, and drop the outliers horiz_matrix.sort_values(by=['X Position'], inplace=True) horiz_matrix.reset_index(inplace=True) horiz_matrix.drop([0, len(horiz_matrix) - 1], inplace=True) # Get horizontal values horiz_matrix[relT] = [ get_mean_elpsT('{0}/{1}_{2}.pckl'.format(prc_fld, scint, x)) for x in horiz_matrix['Test'] ] horiz_matrix[rpkT] = [ get_mean_peakT('{0}/{1}_{2}.pckl'.format(prc_fld, scint, x)) for x in horiz_matrix['Test'] ] breakpoint() # Create a fit to the horizontal variation xData = horiz_matrix['X Position'] yData = horiz_matrix[relT] yData = savgol_filter(yData, 55, 3) XtoCF = CubicSpline(xData, yData) # Horizontal plot plt.figure() plt.plot(horiz_matrix['X Position'], horiz_matrix[rpkT], color='olivedrab', marker='s', label=rpkT) plt.plot(xData, XtoCF(xData), color='cornflowerblue', label='Fit') plt.plot(horiz_matrix['X Position'], horiz_matrix[relT], fillstyle='none', color='olivedrab', marker='s', label=relT) plt.legend() plt.title('{0} - Horizontal Variation - 700 um, 10% KI'.format(scint)) plt.savefig('{0}/{1}_horiz.png'.format(plots_folder, scint)) plt.close() # Save the linear fitted correction factors with open('{0}/Processed/{1}/{1}_peakT_cf.txt'.format(prj_fld, scint), 'wb') as f: np.savetxt(f, peakT_combi_fit['function'](KI_conc)) with open('{0}/Processed/{1}/{1}_elpsT_cf.txt'.format(prj_fld, scint), 'wb') as f: np.savetxt(f, elpsT_combi_fit['function'](KI_conc))
def random_curve_generator(ts, magnitude=.1, order=4, noise=None): seq_len = ts.shape[-1] x = np.linspace(-seq_len, 2 * seq_len - 1, 3 * (order - 1) + 1, dtype=int) x2 = np.random.normal(loc=1.0, scale=magnitude, size=len(x)) f = CubicSpline(x, x2, axis=-1) return f(np.arange(seq_len))
from scipy.interpolate import CubicSpline os.chdir(DIR + 'fits') FILE = glob.glob('*fits') N = 3 * len(FILE) v = np.linspace(-20, 20, 401) CCF = np.zeros([401, N]) v_new = np.linspace(-10, 10, 201) CCF_new = np.zeros([201, N]) for n in range(N): i = n % 25 hdulist = fits.open(FILE[i]) CCF[:, n] = hdulist[0].data v_planet = Model(**truth).get_value(n) cs = CubicSpline(v + v_planet, CCF[:, n]) CCF_new[:, n] = cs(v_new) #plt.plot(np.arange(N), CCF_new[70, :]) plt.plot(v_new, CCF[100:301, n], v_new, CCF_new[:, n], '--', v_new, CCF[100:301, n] - CCF_new[:, n], '-.') plt.plot(v_new[-50], CCF[301 - 50, n], 'ro') plt.plot(v_new[-60], CCF[301 - 60, n], 'bo') plt.plot(v_new[-70], CCF[301 - 70, n], 'mo') plt.plot(v_new[-70], CCF[301 - 70, n] - CCF_new[-70, n], 'mo') plt.plot(v_new[-60], CCF[301 - 60, n] - CCF_new[-60, n], 'bo') plt.plot(v_new[-50], CCF[301 - 50, n] - CCF_new[-50, n], 'ro') plt.ylabel("Normalized flux") plt.xlabel("Wavelength [km/s]") plt.legend(['static line profile', 'shifted line profile', 'variation'])
# Genero una matriz con los niveles isoeléctricos, no tomo un solo punto, si no # que voy a promediar determinada cantidad L de puntos que se encuentren en el intervalo PQ L = 30 dt = 70 nIso = [] for i in posQRS: nIso.append(ecg_one_lead[i - dt - (L // 2):i - dt + (L // 2)]) nIso = np.array(nIso) # Promedio los segmentos nIso = np.transpose(np.mean(nIso, axis=1)) # Interpolo los puntos para obtener el estimador de la señal interferente cs = CubicSpline(posQRS - dt, nIso) estBni = cs(np.arange(N)) # Obtengo el estimador x del ecg estXni = ecg_one_lead - estBni # ACA ARRANCA EL EJERCICIO 6 # Grafico los patrones ''' plt.figure(1) plt.plot(hb_1,label = "Patron latido normal") plt.plot(hb_2,label = "Patron latido ventricular") plt.plot(qrs_1,label = "Patron qrs") plt.legend(bbox_to_anchor=(0.85, 0.98), loc=2, borderaxespad=0.) plt.grid() '''
def MW_unred_fitzpatrick99(wave, flux, ebv): verbose = 'no' #'yes' R_V = 3.1 c2 = -0.824 + 4.171 * (R_V**(-1.0)) c1 = 2.030 - 3.007 * c2 x0 = 4.596 #um-1 (bump position) gamma = 0.99 #um-1 (bump width) c3 = 3.23 #(bump strength) c4 = 0.41 #(FUV curvature) x = 10000. / wave #Convert to inverse microns curve = x * 0. #Create a list of zeros with the same x size xcutuv = 10000.0 / 2700.0 xspluv = np.zeros(2) xspluv[0] = 10000.0 / 2700.0 xspluv[1] = 10000.0 / 2600.0 #iuv => UV index: iuv = (x >= xcutuv) N_UV_where = np.where(iuv == True) N_UV = len(iuv[N_UV_where]) #number of elements if verbose == 'yes': print('iuv: ', iuv, 'N_UV: ', N_UV) #Optical and infrared index: iopir = x < xcutuv N_comp_where = np.where(iopir == True) iopir_xval = wave[N_comp_where] N_opir = len(iopir[N_comp_where]) #Number of elements, N_comp=N_opir if N_UV > 0.0: xuv = np.concatenate((xspluv, np.array(x[iuv]))) else: xuv = xspluv if verbose == 'yes': print('xuv: ', xuv) xuv = np.array(xuv) yuv = c1 + c2 * xuv yuv = yuv + c3 * xuv**2 / ((xuv**2 - x0**2)**2 + (xuv * gamma)**2) yuv = yuv + c4 * (0.5392 * ((xuv > 5.9) - 5.9)**2 + 0.05644 * ((xuv > 5.9) - 5.9)**3) yuv = yuv + R_V if verbose == 'yes': print('yuv: ', yuv) #save spline points in UV: yspluv = yuv[0:2] if verbose == 'yes': print('yspluv: ', yspluv) if N_UV > 0.0: curve[iuv] = yuv[2:] values1 = [26500.0, 12200.0, 6000.0, 5470.0, 4670.0, 4110.0] xsplopir = [] initial_val = 0. xsplopir.append(initial_val) for i in values1: xsplopir_val = 10000.0 / i xsplopir.append(xsplopir_val) if verbose == 'yes': print('xsplopir: ', xsplopir) #save spline points in IR: values2 = [0.0, 0.26469, 0.82925] ysplir = [] for j in values2: ysplir_val = j * R_V / 3.1 ysplir.append(ysplir_val) if verbose == 'yes': print('ysplir: ', ysplir) #save spline points in VIS: ysplop = [] ysplop1 = np.polynomial.polynomial.polyval( R_V, [-4.22809e-01, 1.00270, 2.13572e-04], tensor=True) ysplop.append(ysplop1) ysplop2 = np.polynomial.polynomial.polyval( R_V, [-5.13540e-02, 1.00216, -7.35778e-05], tensor=True) ysplop.append(ysplop2) ysplop3 = np.polynomial.polynomial.polyval( R_V, [7.00127e-01, 1.00184, -3.32598e-05], tensor=True) ysplop.append(ysplop3) ysplop4 = np.polynomial.polynomial.polyval( R_V, [1.19456, 1.01707, -5.46959e-03, 7.97809e-04, -4.45636e-05], tensor=True) ysplop.append(ysplop4) #print 'ysplop1: ', ysplop1, 'ysplop2: ', ysplop2, 'ysplop3: ', ysplop3, 'ysplop4: ', ysplop4, 'ysplop: ', ysplop #save spline points in VIS and IR together: ysplopir = np.concatenate((ysplir, ysplop), axis=0) if verbose == 'yes': print('ysplopir: ', ysplopir) #save spline points in UV, VIS and IR together: x_splval = np.concatenate((xsplopir, xspluv), axis=0) y_splval = np.concatenate((ysplopir, yspluv), axis=0) if verbose == 'yes': print('x: ', len(x_splval), 'y: ', len(y_splval)) if N_opir > 0: cs = CubicSpline( x_splval, y_splval) #Create the function to use in the extindtion correction #--Plot the extiction law function (MW): curve[iopir] = cs(x[iopir]) plotthis = 0 if plotthis == 1: plt.figure(figsize=(6.5, 4)) plt.plot(x_splval, y_splval, '--r') #plt.plot(x_splval, curve(x_splval), 'ok') plt.title('Extinction law for MW (Fitzpatrick et al. 1999)') plt.xlabel('(1/Wavelength)(um^-1)') plt.ylabel('A(Wavelength)/E(B-V)') plt.show() #Now apply extinction correction to input flux vector: #Derive unreddened flux: funred = flux * 10.**(0.4 * ebv * curve) return funred
import sys sys.path.append( '/cvmfs/icecube.opensciencegrid.org/py3-v4.0.1/RHEL_7_x86_64/lib/python3.6/site-packages' ) import numpy as np import pandas as pd import os from scipy.interpolate import CubicSpline import random import warnings #get QE function from HamamatsuQE.txt for QE function further down, should work on init module_dir = os.path.dirname(__file__) QE_file = np.loadtxt(module_dir + '/HamamatsuQE.txt', delimiter="\t") QE_func = CubicSpline(QE_file.T[0], QE_file.T[1], extrapolate=False) del QE_file #if numpy version > 1.16 need rng for random number generator but as of Aug 2020 Illume uses numpy1.14 try: rng = np.random.default_rng() except AttributeError: rng = np.random #return the QE weight for a given energy def QE(energy): ''' Returns the Quantum efficiency (QE) value for the input energy Parameters:
def Prediction_expressions(self): x, y, tx, ty, qp, dz, p, bx = sym.symbols( 'x y tx ty qp dz p bx') #qp denotes q/p #Magnetic Field Integrals S_x = 0.5 * bx * dz**2 S_y = 0.5 * by * dz**2 S_xx = bx**2 * dz**3 / 6 S_yy = by**2 * dz**3 / 6 S_xy = bx * by * dz**3 / 6 S_yx = S_xy R_x = bx * dz R_y = by * dz R_xy = 0.5 * bx * by * dz**2 R_xx = 0.5 * bx * bx * dz**2 R_yy = 0.5 * by * by * dz**2 R_yx = R_xy def h(): return (K * qp * (1 + (tx)**2 + (ty)**2)**0.5) #Symbolic definitions of Prediction equations(for x, y, tx, ty) def f_x(): return (x + tx * dz + h() * (tx * ty * S_x - (1 + tx**2) * S_y) + h()**2 * (tx * (3 * ty**2 + 1) * S_xx - ty * (3 * tx**2 + 1) * S_xy - ty * (3 * tx**2 + 1) * S_yx + tx * (3 * tx**2 + 3) * S_yy)) def f_y(): return (y + ty * dz + h() * ((1 + ty**2) * S_x - tx * ty * S_y) + h()**2 * (ty * (3 * ty**2 + 3) * S_xx - tx * (3 * ty**2 + 1) * S_xy - tx * (3 * ty**2 + 1) * S_yx + ty * (3 * tx**2 + 1) * S_yy)) def f_tx(): return (tx + h() * (tx * ty * R_x - (1 + tx**2) * R_y) + h()**2 * (tx * (3 * ty**2 + 1) * R_xx - ty * (3 * tx**2 + 1) * R_xy - ty * (3 * tx**2 + 1) * R_yx + tx * (3 * tx**2 + 3) * R_yy)) def f_ty(): return (ty + h() * ((1 + ty**2) * R_x - tx * ty * R_y) + h()**2 * (ty * (3 * (ty**2) + 3) * R_xx - tx * (3 * ty**2 + 1) * R_xy - tx * (3 * ty**2 + 1) * R_yx + ty * (3 * tx**2 + 1) * R_yy)) #Converting symbolic to mathematical self.Prediction_x = sym.lambdify((x, y, tx, ty, qp, dz, bx), f_x(), "numpy") self.Prediction_y = sym.lambdify((x, y, tx, ty, qp, dz, bx), f_y(), "numpy") self.Prediction_tx = sym.lambdify((x, y, tx, ty, qp, dz, bx), f_tx(), "numpy") self.Prediction_ty = sym.lambdify((x, y, tx, ty, qp, dz, bx), f_ty(), "numpy") #**************************************************************************** #Derivatives of the prediction equations for Propagator Matrix Elements #Row 1 def Prediction_xprimex(): return f_x().diff(x) def Prediction_xprimey(): return f_x().diff(y) def Prediction_xprimetx(): return f_x().diff(tx) def Prediction_xprimety(): return f_x().diff(ty) def Prediction_xprimeqp(): return f_x().diff(qp) #Row 2 def Prediction_yprimex(): return f_y().diff(x) def Prediction_yprimey(): return f_y().diff(y) def Prediction_yprimetx(): return f_y().diff(tx) def Prediction_yprimety(): return f_y().diff(ty) def Prediction_yprimeqp(): return f_y().diff(qp) #Row 3 def Prediction_txprimex(): return f_tx().diff(x) def Prediction_txprimey(): return f_tx().diff(y) def Prediction_txprimetx(): return f_tx().diff(tx) def Prediction_txprimety(): return f_tx().diff(ty) def Prediction_txprimeqp(): return f_tx().diff(qp) #Row 4 def Prediction_typrimex(): return f_ty().diff(x) def Prediction_typrimey(): return f_ty().diff(y) def Prediction_typrimetx(): return f_ty().diff(tx) def Prediction_typrimety(): return f_ty().diff(ty) def Prediction_typrimeqp(): return f_ty().diff(qp) #Converting symbolic to mathematical self.dx_dx = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_xprimex(), "numpy") self.dx_dy = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_xprimey(), "numpy") self.dx_dtx = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_xprimetx(), "numpy") self.dx_dty = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_xprimety(), "numpy") self.dx_dqp = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_xprimeqp(), "numpy") self.dy_dx = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_yprimex(), "numpy") self.dy_dy = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_yprimey(), "numpy") self.dy_dtx = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_yprimetx(), "numpy") self.dy_dty = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_yprimety(), "numpy") self.dy_dqp = sym.lambdify((x, y, tx, ty, qp, dz, bx), Prediction_yprimeqp(), "numpy") self.dtx_dx = sym.lambdify((tx, ty, qp, dz, bx), Prediction_txprimex(), "numpy") self.dtx_dy = sym.lambdify((tx, ty, qp, dz, bx), Prediction_txprimey(), "numpy") self.dtx_dtx = sym.lambdify((tx, ty, qp, dz, bx), Prediction_txprimetx(), "numpy") self.dtx_dty = sym.lambdify((tx, ty, qp, dz, bx), Prediction_txprimety(), "numpy") self.dtx_dqp = sym.lambdify((tx, ty, qp, dz, bx), Prediction_txprimeqp(), "numpy") self.dty_dx = sym.lambdify((tx, ty, qp, dz, bx), Prediction_typrimex(), "numpy") self.dty_dy = sym.lambdify((tx, ty, qp, dz, bx), Prediction_typrimey(), "numpy") self.dty_dtx = sym.lambdify((tx, ty, qp, dz, bx), Prediction_typrimetx(), "numpy") self.dty_dty = sym.lambdify((tx, ty, qp, dz, bx), Prediction_typrimety(), "numpy") self.dty_dqp = sym.lambdify((tx, ty, qp, dz, bx), Prediction_typrimeqp(), "numpy") #**************************************************************************** #Range-momentum relation for Error Propagation of q/p cb = pd.read_csv("muon-iron-energyLossTable3.txt", sep=" ") cb1 = np.array(cb) pp = cb1[:, 3] * 10**-3 #converting MeV/c into GeV/c l_r = cb1[:, 10] * 10 / 7.874 #in q/cm^2 to mm (/iron density) #Alternate equation for Energy-loss in Iron #ene_r=cb1[:,9]*7.874 #self.EnergylossIron_CubicSpline = CubicSpline(pp,ene_r,bc_type='natural') self.range_l = CubicSpline(pp, l_r, bc_type='natural') #l=f(p) fl = CubicSpline(l_r, pp, bc_type='natural') #p=f(l) self.fl__1 = CubicSpline.derivative(fl, nu=1) #f'(l) self.fl__2 = CubicSpline.derivative(fl, nu=2) #f''(l) self.fl__3 = CubicSpline.derivative(fl, nu=3) #f'''(l) #**************************************************************************** def Prediction_xprimep(): def f_x(): #Prediction(x) with p explicitly defined h = K * q / p * (1 + (tx)**2 + (ty)**2)**0.5 return x + tx * dz + h * ( tx * ty * S_x - (1 + tx**2) * S_y) + h**2 * (tx * (3 * ty**2 + 1) * S_xx - ty * (3 * tx**2 + 1) * S_xy - ty * (3 * tx**2 + 1) * S_yx + tx * (3 * tx**2 + 3) * S_yy) return f_x().diff(p) #dx/dp def Prediction_yprimep(): def f_y(): #Prediction(y) with p explicitly defined h = K * q / p * (1 + (tx)**2 + (ty)**2)**0.5 return y + ty * dz + h * ( (1 + ty**2) * S_x - tx * ty * S_y) + h**2 * (ty * (3 * ty**2 + 3) * S_xx - tx * (3 * ty**2 + 1) * S_xy - tx * (3 * ty**2 + 1) * S_yx + ty * (3 * tx**2 + 1) * S_yy) return f_y().diff(p) #dy/dp def Prediction_txprimep(): def f_tx(): #Prediction(tx) with p explicitly defined h = K * q / p * (1 + (tx)**2 + (ty)**2)**0.5 return (tx + h * (tx * ty * R_x - (1 + tx**2) * R_y) + h**2 * (tx * (3 * ty**2 + 1) * R_xx - ty * (3 * tx**2 + 1) * R_xy - ty * (3 * tx**2 + 1) * R_yx + tx * (3 * tx**2 + 3) * R_yy)) return f_tx().diff(p) #d(tx)/dp def Prediction_typrimep(): def f_ty(): #Prediction(ty) with p explicitly defined h = K * q / p * (1 + (tx)**2 + (ty)**2)**0.5 return (ty + h * ((1 + ty**2) * R_x - tx * ty * R_y) + h**2 * (ty * (3 * (ty**2) + 3) * R_xx - tx * (3 * ty**2 + 1) * R_xy - tx * (3 * ty**2 + 1) * R_yx + ty * (3 * tx**2 + 1) * R_yy)) return f_ty().diff(p) #d(ty)/dp #Symbolic to mathematical self.dx_dp = sym.lambdify((x, y, tx, ty, qp, dz, p, bx), Prediction_xprimep(), "numpy") self.dy_dp = sym.lambdify((x, y, tx, ty, qp, dz, p, bx), Prediction_yprimep(), "numpy") self.dtx_dp = sym.lambdify((x, y, tx, ty, qp, dz, p, bx), Prediction_txprimep(), "numpy") self.dty_dp = sym.lambdify((x, y, tx, ty, qp, dz, p, bx), Prediction_typrimep(), "numpy")
def read_profiles_file(pfpath,setParam={}): #Developed by Ehab Hassan on 2019-02-17 from scipy.interpolate import CubicSpline if not os.path.isfile(pfpath): print('Fatal: file %s not found.' % pfpath) sys.exit() ofh = open(pfpath,'r') profiles = {} units = {} while True: recs = ofh.readline().split() if len(recs)>4: nrec = int(recs[0]) ary0=npy.zeros(nrec) ary1=npy.zeros(nrec) ary2=npy.zeros(nrec) var0 = str(recs[1]).lower() var1 = str(recs[2]).lower() var2 = str(recs[3]).lower() for i in range(nrec): recs = ofh.readline().split() ary0[i] = float(recs[0]) ary1[i] = float(recs[1]) ary2[i] = float(recs[2]) profiles[var0]=ary0 profiles[var1]=ary1 profiles[var2]=ary2 elif len(recs)==4: nrec = int(recs[0]) ary0=npy.zeros(nrec) ary1=npy.zeros(nrec) ary2=npy.zeros(nrec) var0 = str(recs[1]).lower() temp = str(recs[2]).lower() var1 = temp[:temp.index("(")] if var1.strip() in ['ne','ni','nb','nz1']: powr = int(temp[temp.index("^")+1:temp.index("/")]) unit = 'm^{-3}' elif var1.strip() in ['te','ti']: unit = 'eV' elif var1.strip() in ['pb','ptot']: unit = 'Pa' elif var1.strip() in ['vtor1','vpol1']: unit = 'm/s' else: unit = temp[temp.index("(")+1:temp.index(")")] var2 = str(recs[3]).lower() for i in range(nrec): recs = ofh.readline().split() ary0[i] = float(recs[0]) ary1[i] = float(recs[1]) ary2[i] = float(recs[2]) if var0 in profiles.keys(): CS = CubicSpline(ary0,ary1) units[var1] = unit profiles[var1] = CS(profiles[var0]) CS = CubicSpline(ary0,ary2) profiles[var2] = CS(profiles[var0]) else: profiles[var0] = ary0 units[var1] = unit profiles[var1] = ary1 profiles[var2] = ary2 if var1.strip() in ['ne','ni','nb','nz1']: profiles[var1] *= 10.0**powr elif var1.strip() in ['te','ti','pb','ptot','vtor1','vpol1']: profiles[var1] *= 1.0e3 else: break ofh.close() if 'rhotor' in setParam: if 'eqdskfpath' not in setParam: eqdskfpath = raw_input('Path to EQDSK file: ') elif 'eqdskfpath' in setParam: eqdskfpath = setParam['eqdskfpath'] eqdskdata = read_efit_file(eqdskfpath) qpsifn = interp1d(eqdskdata['PSIN'],eqdskdata['qpsi']) qpsi = qpsifn(profiles['psinorm']) qpsifn = interp1d(profiles['psinorm'],qpsi) psinorm = npy.linspace(profiles['psinorm'][0],profiles['psinorm'][-1],setParam['rhotor']) qpsi = qpsifn(psinorm) phinorm = npy.zeros_like(psinorm) for i in range(1,npy.size(qpsi)): x = psinorm[:i+1] y = qpsi[:i+1] phinorm[i] = npy.trapz(y,x) profiles['rhotor'] = npy.sqrt(phinorm) return profiles,units
min_cnt = np.unique(weather_monthly[col])[1] month_min = weather_monthly.loc[weather_monthly[col] == min_cnt, :].index[0][0] site_id_min = weather_monthly.loc[weather_monthly[col] == min_cnt, :].index[0][1] time_plt = pd.to_datetime( weather_train.loc[(weather_train['month'] == month_min) & (weather_train['site_id'] == site_id_min) & (weather_train[col].isnull() == False), 'timestamp']) col_plt = weather_train.loc[(weather_train['month'] == month_min) & (weather_train['site_id'] == site_id_min) & (weather_train[col].isnull() == False), col] cs = CubicSpline(time_plt, col_plt) time_interp = pd.date_range(time_plt.min(), time_plt.max(), freq='H') col_interp = cs(time_interp) if plotFig: plt.figure() plt.plot(time_plt, col_plt, 'o-') plt.plot(time_interp, col_interp, '.-') plt.title('{:} interpolation'.format(col)) del weather_monthly # Merging building metadata and train/test sets meter_data = meter_data.merge(building, on='building_id', how='left') del building # Align timestamps
def read_efit_file(eqdskfpath,setParam={}): #Developed by Ehab Hassan on 2019-02-27 if os.path.isfile(eqdskfpath) == False: errorFunc = traceback.extract_stack(limit=2)[-2][3] errorLine = traceback.extract_stack(limit=2)[-2][1] errorFile = traceback.extract_stack(limit=2)[-2][2] errMSG = 'Call %s line %5d in file %s Failed.\n' errMSG += 'Fatal: file %s not found.' raise IOError(errMSG %(errorFunc,errorLine,errorFile,eqdskfpath)) ofh = open(eqdskfpath,'r') eqdskdata = {} cline = ofh.readline() eqdskdata['idum'] = int(cline[48:52]) eqdskdata['RDIM'] = int(cline[52:56]) eqdskdata['ZDIM'] = int(cline[56:61]) cline = ofh.readline() eqdskdata['RLEN'] = float(cline[0:16]) eqdskdata['ZLEN'] = float(cline[16:32]) eqdskdata['RCTR'] = float(cline[32:48]) eqdskdata['RLFT'] = float(cline[48:64]) eqdskdata['ZMID'] = float(cline[64:80]) cline = ofh.readline() eqdskdata['RMAX'] = float(cline[0:16]) eqdskdata['ZMAX'] = float(cline[16:32]) eqdskdata['PSIMAX'] = float(cline[32:48]) eqdskdata['PSIBND'] = float(cline[48:64]) eqdskdata['BCTR'] = float(cline[64:80]) cline = ofh.readline() eqdskdata['CURNT'] = float(cline[0:16]) eqdskdata['PSIMAX'] = float(cline[16:32]) eqdskdata['XDUM'] = float(cline[32:48]) eqdskdata['RMAX'] = float(cline[48:64]) eqdskdata['XDUM'] = float(cline[64:80]) cline = ofh.readline() eqdskdata['ZMAX'] = float(cline[0:16]) eqdskdata['XDUM'] = float(cline[16:32]) eqdskdata['PSIBND'] = float(cline[32:48]) eqdskdata['XDUM'] = float(cline[48:64]) eqdskdata['XDUM'] = float(cline[64:80]) nlines1D = int(npy.ceil(eqdskdata['RDIM']/5.0)) eqdskdata['fpol'] = npy.zeros(eqdskdata['RDIM']) for iline in range(nlines1D): cline = ofh.readline() try: eqdskdata['fpol'][iline*5+0] = float(cline[0:16]) eqdskdata['fpol'][iline*5+1] = float(cline[16:32]) eqdskdata['fpol'][iline*5+2] = float(cline[32:48]) eqdskdata['fpol'][iline*5+3] = float(cline[48:64]) eqdskdata['fpol'][iline*5+4] = float(cline[64:80]) except: error = 'empty records' eqdskdata['pressure'] = npy.zeros(eqdskdata['RDIM']) for iline in range(nlines1D): cline = ofh.readline() try: eqdskdata['pressure'][iline*5+0] = float(cline[0:16]) eqdskdata['pressure'][iline*5+1] = float(cline[16:32]) eqdskdata['pressure'][iline*5+2] = float(cline[32:48]) eqdskdata['pressure'][iline*5+3] = float(cline[48:64]) eqdskdata['pressure'][iline*5+4] = float(cline[64:80]) except: error = 'empty records' eqdskdata['ffprime'] = npy.zeros(eqdskdata['RDIM']) for iline in range(nlines1D): cline = ofh.readline() try: eqdskdata['ffprime'][iline*5+0] = float(cline[0:16]) eqdskdata['ffprime'][iline*5+1] = float(cline[16:32]) eqdskdata['ffprime'][iline*5+2] = float(cline[32:48]) eqdskdata['ffprime'][iline*5+3] = float(cline[48:64]) eqdskdata['ffprime'][iline*5+4] = float(cline[64:80]) except: error = 'empty records' eqdskdata['pprime'] = npy.zeros(eqdskdata['RDIM']) for iline in range(nlines1D): cline = ofh.readline() try: eqdskdata['pprime'][iline*5+0] = float(cline[0:16]) eqdskdata['pprime'][iline*5+1] = float(cline[16:32]) eqdskdata['pprime'][iline*5+2] = float(cline[32:48]) eqdskdata['pprime'][iline*5+3] = float(cline[48:64]) eqdskdata['pprime'][iline*5+4] = float(cline[64:80]) except: error = 'empty records' nlines2D = int(npy.ceil(eqdskdata['RDIM']*eqdskdata['ZDIM']/5.0)) eqdskdata['psiRZ'] = npy.zeros(eqdskdata['RDIM']*eqdskdata['ZDIM']) for iline in range(nlines2D): cline = ofh.readline() try: eqdskdata['psiRZ'][iline*5+0] = float(cline[0:16]) eqdskdata['psiRZ'][iline*5+1] = float(cline[16:32]) eqdskdata['psiRZ'][iline*5+2] = float(cline[32:48]) eqdskdata['psiRZ'][iline*5+3] = float(cline[48:64]) eqdskdata['psiRZ'][iline*5+4] = float(cline[64:80]) except: error = 'empty records' eqdskdata['psiRZ'] = npy.reshape(eqdskdata['psiRZ'],(eqdskdata['ZDIM'],eqdskdata['RDIM'])) eqdskdata['qpsi'] = npy.zeros(eqdskdata['RDIM']) for iline in range(nlines1D): cline = ofh.readline() try: eqdskdata['qpsi'][iline*5+0] = float(cline[0:16]) eqdskdata['qpsi'][iline*5+1] = float(cline[16:32]) eqdskdata['qpsi'][iline*5+2] = float(cline[32:48]) eqdskdata['qpsi'][iline*5+3] = float(cline[48:64]) eqdskdata['qpsi'][iline*5+4] = float(cline[64:80]) except: error = 'empty records' cline = ofh.readline() eqdskdata['nbound'] = int(cline[0:5]) eqdskdata['nlimit'] = int(cline[5:10]) if eqdskdata['nbound'] > 0: nlines1D = int(npy.ceil(2*eqdskdata['nbound']/5.0)) Ary1D = npy.zeros(2*eqdskdata['nbound']) for iline in range(nlines1D): cline = ofh.readline() try: Ary1D[iline*5+0] = float(cline[0:16]) Ary1D[iline*5+1] = float(cline[16:32]) Ary1D[iline*5+2] = float(cline[32:48]) Ary1D[iline*5+3] = float(cline[48:64]) Ary1D[iline*5+4] = float(cline[64:80]) except: error = 'empty records' eqdskdata['rbound'] = Ary1D[0::2] eqdskdata['zbound'] = Ary1D[1::2] if eqdskdata['nlimit'] > 0: nlines1D = int(npy.ceil(2*eqdskdata['nlimit']/5.0)) Ary1D = npy.zeros(2*eqdskdata['nlimit']) for iline in range(nlines1D): cline = ofh.readline() try: Ary1D[iline*5+0] = float(cline[0:16]) Ary1D[iline*5+1] = float(cline[16:32]) Ary1D[iline*5+2] = float(cline[32:48]) Ary1D[iline*5+3] = float(cline[48:64]) Ary1D[iline*5+4] = float(cline[64:80]) except: error = 'empty records' eqdskdata['rlimit'] = Ary1D[0::2] eqdskdata['zlimit'] = Ary1D[1::2] eqdskdata['ZR1D'] = npy.arange(eqdskdata['ZDIM'],dtype=float)*eqdskdata['ZLEN']/(eqdskdata['ZDIM']-1.0) eqdskdata['ZR1D'] += eqdskdata['ZMID']-eqdskdata['ZMID']/2.0 eqdskdata['RR1D'] = npy.arange(eqdskdata['RDIM'],dtype=float)*eqdskdata['RLEN']/(eqdskdata['RDIM']-1.0) eqdskdata['RR1D'] += eqdskdata['RLFT'] eqdskdata['psiRZ'] = (eqdskdata['psiRZ']-eqdskdata['PSIMAX'])/(eqdskdata['PSIBND']-eqdskdata['PSIMAX']) eqdskdata['PSI'] = (eqdskdata['PSIBND']-eqdskdata['PSIMAX'])*npy.arange(eqdskdata['RDIM'])/(eqdskdata['RDIM']-1.0) eqdskdata['PSIN'] = (eqdskdata['PSI']-eqdskdata['PSI'][0])/(eqdskdata['PSI'][-1]-eqdskdata['PSI'][0]) eqdskdata['rhopsi'] = npy.sqrt(eqdskdata['PSIN']) extendPSI = npy.linspace(eqdskdata['PSI'][0],eqdskdata['PSI'][-1],10*npy.size(eqdskdata['PSI'])) extendPHI = npy.empty_like(extendPSI) extendPHI[0] = 0.0 qfunc = CubicSpline(eqdskdata['PSI'],eqdskdata['qpsi']) for i in range(1,npy.size(extendPSI)): x = extendPSI[:i+1] y = qfunc(x) extendPHI[i]= npy.trapz(y,x) eqdskdata['PHI'] = npy.empty_like(eqdskdata['PSI']) phifunc = CubicSpline(extendPSI,extendPHI) for i in range(npy.size(eqdskdata['PSI'])): eqdskdata['PHI'][i] = phifunc(eqdskdata['PSI'][i]) eqdskdata['PHIN'] = (eqdskdata['PHI']-eqdskdata['PHI'][0])/(eqdskdata['PHI'][-1]-eqdskdata['PHI'][0]) eqdskdata['rhotor'] = npy.sqrt(eqdskdata['PHIN']) return eqdskdata
def from_endf(cls, photoatomic, relaxation=None): """Generate incident photon data from an ENDF evaluation Parameters ---------- photoatomic : str or endf.Evaluation ENDF photoatomic data evaluation to read from. If given as a string, it is assumed to be the filename for the ENDF file. relaxation : str or endf.Evaluation, optional ENDF atomic relaxation data evaluation to read from. If given as a string, it is assumed to be the filename for the ENDF file. Returns ------- IncidentPhoton Photon interaction data """ if isinstance(photoatomic, Evaluation): ev = photoatomic else: ev = Evaluation(photoatomic) Z = ev.target['atomic_number'] data = cls(Z) # Read each reaction for mf, mt, nc, mod in ev.reaction_list: if mf == 23: data.reactions[mt] = PhotonReaction.from_endf(ev, mt) # Add atomic relaxation data if it hasn't been added already if relaxation is not None: data.atomic_relaxation = AtomicRelaxation.from_endf(relaxation) # If Compton profile data hasn't been loaded, do so if not _COMPTON_PROFILES: filename = os.path.join(os.path.dirname(__file__), 'compton_profiles.h5') with h5py.File(filename, 'r') as f: _COMPTON_PROFILES['pz'] = f['pz'].value for i in range(1, 101): group = f['{:03}'.format(i)] num_electrons = group['num_electrons'].value binding_energy = group['binding_energy'].value * EV_PER_MEV J = group['J'].value _COMPTON_PROFILES[i] = { 'num_electrons': num_electrons, 'binding_energy': binding_energy, 'J': J } # Add Compton profile data pz = _COMPTON_PROFILES['pz'] profile = _COMPTON_PROFILES[Z] data.compton_profiles['num_electrons'] = profile['num_electrons'] data.compton_profiles['binding_energy'] = profile['binding_energy'] data.compton_profiles['J'] = [ Tabulated1D(pz, J_k) for J_k in profile['J'] ] # Load stopping power data if it has not yet been loaded if not _STOPPING_POWERS: filename = os.path.join(os.path.dirname(__file__), 'stopping_powers.h5') with h5py.File(filename, 'r') as f: # Units are in MeV; convert to eV _STOPPING_POWERS['energy'] = f['energy'].value * EV_PER_MEV for i in range(1, 99): group = f['{:03}'.format(i)] # Units are in MeV cm^2/g; convert to eV cm^2/g _STOPPING_POWERS[i] = { 'I': group.attrs['I'], 's_collision': group['s_collision'].value * EV_PER_MEV, 's_radiative': group['s_radiative'].value * EV_PER_MEV } # Add stopping power data if Z < 99: data.stopping_powers['energy'] = _STOPPING_POWERS['energy'] data.stopping_powers.update(_STOPPING_POWERS[Z]) # Load bremsstrahlung data if it has not yet been loaded if not _BREMSSTRAHLUNG: filename = os.path.join(os.path.dirname(__file__), 'BREMX.DAT') brem = open(filename, 'r').read().split() # Incident electron kinetic energy grid in eV _BREMSSTRAHLUNG['electron_energy'] = np.logspace(3, 9, 200) log_energy = np.log(_BREMSSTRAHLUNG['electron_energy']) # Get number of tabulated electron and photon energy values n = int(brem[37]) k = int(brem[38]) # Index in data p = 39 # Get log of incident electron kinetic energy values, used for # cubic spline interpolation in log energy. Units are in MeV, so # convert to eV. logx = np.log(np.fromiter(brem[p:p + n], float, n) * EV_PER_MEV) p += n # Get reduced photon energy values _BREMSSTRAHLUNG['photon_energy'] = np.fromiter( brem[p:p + k], float, k) p += k for i in range(1, 101): dcs = np.empty([len(log_energy), k]) # Get the scaled cross section values for each electron energy # and reduced photon energy for this Z. Units are in mb, so # convert to b. y = np.reshape(np.fromiter(brem[p:p + n * k], float, n * k), (n, k)) * 1.0e-3 p += k * n for j in range(k): # Cubic spline interpolation in log energy and linear DCS cs = CubicSpline(logx, y[:, j]) # Get scaled DCS values (millibarns) on new energy grid dcs[:, j] = cs(log_energy) _BREMSSTRAHLUNG[i] = {'dcs': dcs} # Add bremsstrahlung DCS data data.bremsstrahlung['electron_energy'] = _BREMSSTRAHLUNG[ 'electron_energy'] data.bremsstrahlung['photon_energy'] = _BREMSSTRAHLUNG['photon_energy'] data.bremsstrahlung['dcs'] = _BREMSSTRAHLUNG[Z]['dcs'] return data
def time_adjust_func(t_offset, t_adjust, t_sim, obs_sim, t_exp, obs_exp, weights, obs_bounds=[], loss_alpha=2, loss_c=1, scale='Linear', DoF=1, opt_type='Residual', verbose=False): def calc_exp_bounds(t_sim, t_exp): t_bounds = [max([t_sim[0], t_exp[0]])] # Largest initial time in SIM and Exp t_bounds.append(min([t_sim[-1], t_exp[-1] ])) # Smallest final time in SIM and Exp # Values within t_bounds exp_bounds = np.where( np.logical_and((t_exp >= t_bounds[0]), (t_exp <= t_bounds[1])))[0] return exp_bounds t_sim_shifted = t_sim + t_offset + t_adjust # Compare SIM Density Grad vs. Experimental exp_bounds = calc_exp_bounds(t_sim_shifted, t_exp) t_exp, obs_exp, weights = t_exp[exp_bounds], obs_exp[ exp_bounds], weights[exp_bounds] if opt_type == 'Bayesian': obs_bounds = obs_bounds[exp_bounds] f_interp = CubicSpline(t_sim_shifted.flatten(), obs_sim.flatten()) obs_sim_interp = f_interp(t_exp) if scale == 'Linear': resid = np.subtract(obs_exp, obs_sim_interp) elif scale == 'Log': ind = np.argwhere(((obs_exp != 0.0) & (obs_sim_interp != 0.0))) exp_bounds = exp_bounds[ind] weights = weights[ind].flatten() m = np.divide(obs_exp[ind], obs_sim_interp[ind]) resid = np.log10(np.abs(m)).flatten() if verbose and opt_type == 'Bayesian': obs_exp = np.log10(np.abs( obs_exp[ind])).squeeze() # squeeze to remove extra dim obs_sim_interp = np.log10(np.abs( obs_sim_interp[ind])).squeeze() obs_bounds = np.log10(np.abs(obs_bounds[ind])).squeeze() resid_outlier = outlier(resid, a=loss_alpha, c=loss_c, weights=weights) loss = generalized_loss_fcn(resid, a=loss_alpha, c=resid_outlier) loss = rescale_loss_fcn(np.abs(resid), loss, resid_outlier, weights) loss_sqr = loss**2 wgt_sum = weights.sum() N = wgt_sum - DoF if N <= 0: N = wgt_sum stderr_sqr = (loss_sqr * weights).sum() / N chi_sqr = loss_sqr / stderr_sqr std_resid = chi_sqr**(0.5) #loss_scalar = (chi_sqr*weights).sum() loss_scalar = weighted_quantile(std_resid, 0.5, weights=weights) # median value if verbose: output = { 'chi_sqr': chi_sqr, 'resid': resid, 'resid_outlier': resid_outlier, 'loss': loss_scalar, 'weights': weights, 'obs_sim_interp': obs_sim_interp, 'obs_exp': obs_exp } if opt_type == 'Bayesian': # need to calculate aggregate weights to reduce outliers in bayesian SSE = generalized_loss_fcn(resid) SSE = rescale_loss_fcn(np.abs(resid), SSE, resid_outlier, weights) loss_weights = loss / SSE # comparison is between selected loss fcn and SSE (L2 loss) output['aggregate_weights'] = weights * loss_weights output['obs_bounds'] = obs_bounds return output else: # needs to return single value for optimization return loss_scalar
def trajFeet_jump1(self, **kwargs): from scipy.interpolate import CubicSpline from matplotlib import pyplot as plt t0 = kwargs.get("traj_t0", 1) # Duration of initialisation step t1 = kwargs.get("traj_t1", 0.25) # Duration of first step (extension of legs) t2 = kwargs.get("traj_t2", 0.15) # Duration of second step (spreading legs) # Initialization of the variables traj_x0 = 0.190 + kwargs.get( "traj_dx0", 0.05) # initial distance on the x axis from strait traj_y0 = 0.147 + kwargs.get( "traj_dy0", 0) # initial distance on the y axis from strait traj_z0 = kwargs.get("traj_z0", -0.25) # initial distance on the z axis from body traj_zf = kwargs.get("traj_zf", -0.2) # initial distance on the z axis from body dz = kwargs.get("traj_dz", -0.2) # displacement amplitude by z dy = kwargs.get("traj_dy", 0.05) # displacement amplitude by y dx = kwargs.get("traj_dx", dy) # displacement amplitude by x # Gains parameters param_kps = kwargs.get("kps", [1.5, 1.5]) # default parameter of kps param_kds = kwargs.get("kds", [0.05, 0.05]) # default parameter of kds # Definition of the feet trajectory self.setParameter('tf', t0 + t1 + t2 * 2) times = [0, t0, t0 + t1, t0 + t1 + t2, self.getParameter('tf')] x_pos = [traj_x0, traj_x0, traj_x0, traj_x0 + dx, traj_x0 + dx] y_pos = [traj_y0, traj_y0, traj_y0, traj_y0 + dy, traj_y0 + dy] z_pos = [traj_z0, traj_z0 - dz, traj_z0, traj_zf, traj_zf] kp_pos = [ param_kps[1], param_kps[1], param_kps[1], param_kps[0], param_kps[0] ] kd_pos = [ param_kds[1], param_kds[1], param_kds[1], param_kds[0], param_kds[0] ] time = [] x_feet = [] y_feet = [] z_feet = [] kp_feet = [] kd_feet = [] for i in range(len(times) - 1): t = np.arange(times[i], times[i + 1], self.getParameter('dt')) cur_times = [times[i], times[i + 1]] cs_x = CubicSpline(cur_times, [x_pos[i], x_pos[i + 1]], bc_type='clamped') cs_y = CubicSpline(cur_times, [y_pos[i], y_pos[i + 1]], bc_type='clamped') cs_z = CubicSpline(cur_times, [z_pos[i], z_pos[i + 1]], bc_type='clamped') cs_kp = CubicSpline(cur_times, [kp_pos[i], kp_pos[i]], bc_type='clamped') cs_kd = CubicSpline(cur_times, [kd_pos[i], kd_pos[i]], bc_type='clamped') time = np.concatenate((time, t)) x_feet = np.concatenate((x_feet, cs_x(t))) y_feet = np.concatenate((y_feet, cs_y(t))) z_feet = np.concatenate((z_feet, cs_z(t))) kp_feet = np.concatenate((kp_feet, cs_kp(t))) kd_feet = np.concatenate((kd_feet, cs_kd(t))) foot_FL = np.array([+x_feet, +y_feet, z_feet]) foot_FR = np.array([+x_feet, -y_feet, z_feet]) foot_HL = np.array([-x_feet, +y_feet, z_feet]) foot_HR = np.array([-x_feet, -y_feet, z_feet]) feet_traj = np.array([foot_FL, foot_FR, foot_HL, foot_HR]) feet_traj = np.swapaxes(feet_traj, 0, 2) feet_traj = np.swapaxes(feet_traj, 1, 2) gains = np.array([kp_feet, kd_feet]) gains = np.swapaxes(gains, 0, 1) plt.plot(foot_FL[0, :], label='x') plt.plot(foot_FL[1, :], label='y') plt.plot(foot_FL[2, :], label='z') plt.grid(True) plt.legend() plt.show() return time, feet_traj, gains
def generate_kappa(kappa_params=None): """ Returns the dust emissivity in [m^2/kg] at a frequency and in cell ID. TODO: Make the input directly file paths or floats? Args: kappa_params (str): A string decribing the dust parameters. For the use of Ossenkopf & Henning (1994) opacities it must take the form of: kappa_params = 'jena, TYPE, COAG' where TYPE must be 'bare', 'thin' or 'thick' and COAG must be 'no', 'e5', 'e6', 'e7' or 'e8'. Otherwise a power law profile can be included. Alternatively, a simple power law can be used where the parameters are given by: kappa_params = 'powerlaw, freq0, kappa0, beta' where freq0 is in [Hz], kappa0 in [cm^2/g], and beta is the frequency index. If nothing is given, we assume no opacity. Returns: kappa: A callable function returning the dust emissivity in [m^2/kg]. Raises: ValueError: If the parameters cannot be parsed. """ # If no parameters are provided, assume no dust opacity. if kappa_params is None: def kappa(idx, freq): return 0.0 return kappa # Parse the input parameters. params = kappa_params.replace(' ', '').lower().split(',') # Ossenkopf & Henning (199X) dust opacities. if params[0] == 'jena': from scipy.interpolate import CubicSpline filename = "kappa/jena_" + params[1] + "_" + params[2] + ".tab" table = np.loadtxt(filename) lamtab = np.log10(table[:, 0]) - 6.0 kaptab = np.log10(table[:, 1]) interp_func = CubicSpline(lamtab, kaptab, extrapolate=True) # The 0.1 converts [cm^2/g] to [m^2/kg]. def kappa(idx, freq): lam_lookup = np.log10(sc.c / freq) return 0.1 * 10**interp_func(lam_lookup) return kappa # Simple power law opacity. elif params[0] == 'powerlaw': freq0 = float(params[1]) kappa0 = float(params[2]) beta = float(params[3]) # The 0.1 converts [cm^2/g] to [m^2/kg]. def kappa(idx, freq): return 0.1 * kappa0 * (freq / freq0)**beta return kappa else: raise ValueError("Invalid kappa_params.")
for o in obstacles: (get_points_around_ob(0.02, (0.05, 0.07), (0.02, 0.07), (0.08, 0.07))) #insert intermediate waypoints from navigating around the obstacle #slicing x = [0.01, 0.02, 0.03, 0.06, 0.07] y = [0.02, 0.05, 0.07, 0.04, 0.02] #inputted waypts plt.scatter(x, y, color='blue', label='given') ''' if an obstacle exists on the interpolated path within some error margin [RADIUS OF OBJECT], route around it within those 2 way pts, use them as start and end ''' # In[35]: from scipy.interpolate import * from scipy.interpolate import CubicSpline for i in range(0, len(x) - 1): cs = CubicSpline(x[i:i + 2], y[i:i + 2]) #plt.plot(x[i:i+2], cs(x[i:i+2]), label="S") print((x[i:i + 2], cs(x[i:i + 2]))) plt.show() # In[ ]:
def colorMod(): from scipy.interpolate import CubicSpline # Load the Filters JBandFilterWave, JBandFilterThru = np.loadtxt( '2MASS-2MASS.J.dat').T # load the J filter HBandFilterWave, HBandFilterThru = np.loadtxt( '2MASS-2MASS.H.dat').T # load the H filter KBandFilterWave, KBandFilterThru = np.loadtxt( '2MASS-2MASS.Ks.dat').T # load the K filter # Interpolate the Filters onto the Stellar Model Wavelength Grid tempModel = S.Icat( 'phoenix', 5500, 0.0, 4.5 ) # we are going to use this to setup the filters in the SYNPHOT framework JBandFilterSpline = CubicSpline(JBandFilterWave, JBandFilterThru) HBandFilterSpline = CubicSpline(HBandFilterWave, HBandFilterThru) KBandFilterSpline = CubicSpline(KBandFilterWave, KBandFilterThru) # For some reason pysnphot stores the Vega spectrum with different number of flux points JWaveVRange = (S.Vega.wave >= JBandFilterWave.min()) * ( S.Vega.wave <= JBandFilterWave.max()) # isolate model region in JBand HWaveVRange = (S.Vega.wave >= HBandFilterWave.min()) * ( S.Vega.wave <= HBandFilterWave.max()) # isolate model region in HBand KWaveVRange = (S.Vega.wave >= KBandFilterWave.min()) * ( S.Vega.wave <= KBandFilterWave.max()) # isolate model region in KBand VegaJFlux = np.trapz(S.Vega.flux[JWaveVRange] * JBandFilterSpline(S.Vega.wave[JWaveVRange])) VegaHFlux = np.trapz(S.Vega.flux[HWaveVRange] * HBandFilterSpline(S.Vega.wave[HWaveVRange])) VegaKFlux = np.trapz(S.Vega.flux[KWaveVRange] * KBandFilterSpline(S.Vega.wave[KWaveVRange])) # Now to compare the JHK filters to the PHOENIX models JWaveRange = (tempModel.wave >= JBandFilterWave.min()) * ( tempModel.wave <= JBandFilterWave.max() ) # isolate model region in JBand HWaveRange = (tempModel.wave >= HBandFilterWave.min()) * ( tempModel.wave <= HBandFilterWave.max() ) # isolate model region in HBand KWaveRange = (tempModel.wave >= KBandFilterWave.min()) * ( tempModel.wave <= KBandFilterWave.max() ) # isolate model region in KBand # Loop over the 30 models nModels = 30 Jmags = np.zeros(nModels) Hmags = np.zeros(nModels) Kmags = np.zeros(nModels) teff_list = [x for x in range(2800, 5500 + 100, 100)] + [5800] + [6000] # check if I did that write assert (len(teff_list) == nModels) for kt, teff in enumerate(teff_list): modelTeff = S.Icat('phoenix', teff, 0.0, 4.5) # load the model Jmags[kt] = -2.5 * np.log10( np.trapz(modelTeff.flux[JWaveRange] * JBandFilterSpline(tempModel.wave[JWaveRange]) / VegaJFlux)) # integrate the flux in the J bandpass Hmags[kt] = -2.5 * np.log10( np.trapz(modelTeff.flux[HWaveRange] * HBandFilterSpline(tempModel.wave[HWaveRange]) / VegaHFlux)) # integrate the flux in the H bandpass Kmags[kt] = -2.5 * np.log10( np.trapz(modelTeff.flux[KWaveRange] * KBandFilterSpline(tempModel.wave[KWaveRange]) / VegaKFlux)) # integrate the flux in the K bandpass jhmod = Jmags - Hmags hkmod = Hmags - Kmags return jhmod, hkmod, teff_list
def evaluate(**kwargs): torch.set_default_dtype(torch.float32) conf = ConfigFactory.parse_file(kwargs['conf']) exps_folder_name = kwargs['exps_folder_name'] evals_folder_name = kwargs['evals_folder_name'] timestamp = '2020' checkpoint = '2000' expname = conf.get_string('train.expname') geometry_id = kwargs['geometry_id'] appearance_id = kwargs['appearance_id'] utils.mkdir_ifnotexists(os.path.join('../', evals_folder_name)) expdir_geometry = os.path.join('../', exps_folder_name, expname + '_{0}'.format(geometry_id)) expdir_appearance = os.path.join('../', exps_folder_name, expname + '_{0}'.format(appearance_id)) evaldir = os.path.join( '../', evals_folder_name, expname + '_{0}_{1}'.format(geometry_id, appearance_id)) utils.mkdir_ifnotexists(evaldir) model = utils.get_class( conf.get_string('train.model_class'))(conf=conf.get_config('model')) if torch.cuda.is_available(): model.cuda() # Load geometry network model old_checkpnts_dir = os.path.join(expdir_geometry, timestamp, 'checkpoints') saved_model_state = torch.load( os.path.join(old_checkpnts_dir, 'ModelParameters', checkpoint + ".pth")) model.load_state_dict(saved_model_state["model_state_dict"]) # Load rendering network model model_fake = utils.get_class( conf.get_string('train.model_class'))(conf=conf.get_config('model')) if torch.cuda.is_available(): model_fake.cuda() old_checkpnts_dir = os.path.join(expdir_appearance, timestamp, 'checkpoints') saved_model_state = torch.load( os.path.join(old_checkpnts_dir, 'ModelParameters', checkpoint + ".pth")) model_fake.load_state_dict(saved_model_state["model_state_dict"]) model.rendering_network = model_fake.rendering_network dataset_conf = conf.get_config('dataset') dataset_conf['scene_id'] = geometry_id eval_dataset = utils.get_class(conf.get_string('train.dataset_class'))( False, **dataset_conf) eval_dataloader = torch.utils.data.DataLoader( eval_dataset, batch_size=1, shuffle=True, collate_fn=eval_dataset.collate_fn) total_pixels = eval_dataset.total_pixels img_res = eval_dataset.img_res #################################################################################################################### print("evaluating...") model.eval() gt_pose = utils.to_cuda(eval_dataset.get_gt_pose(scaled=True)) gt_quat = rend_util.rot_to_quat(gt_pose[:, :3, :3]) gt_pose_vec = torch.cat([gt_quat, gt_pose[:, :3, 3]], 1) indices_all = [11, 16, 34, 28, 11] pose = gt_pose_vec[indices_all, :] t_in = np.array([0, 2, 3, 5, 6]).astype(np.float32) n_inter = 5 t_out = np.linspace(t_in[0], t_in[-1], n_inter * t_in[-1]).astype(np.float32) scales = np.array([4.2, 4.2, 3.8, 3.8, 4.2]).astype(np.float32) s_new = CubicSpline(t_in, scales, bc_type='periodic') s_new = s_new(t_out) q_new = CubicSpline(t_in, pose[:, :4].detach().cpu().numpy(), bc_type='periodic') q_new = q_new(t_out) q_new = q_new / np.linalg.norm(q_new, 2, 1)[:, None] q_new = utils.to_cuda(torch.from_numpy(q_new)).float() images_dir = '{0}/novel_views_rendering'.format(evaldir) utils.mkdir_ifnotexists(images_dir) indices, model_input, ground_truth = next(iter(eval_dataloader)) for i, (new_q, scale) in enumerate(zip(q_new, s_new)): if torch.cuda.is_available(): torch.cuda.empty_cache() new_q = new_q.unsqueeze(0) new_t = -rend_util.quat_to_rot(new_q)[:, :, 2] * scale new_p = utils.to_cuda(torch.eye(4).float()).unsqueeze(0) new_p[:, :3, :3] = rend_util.quat_to_rot(new_q) new_p[:, :3, 3] = new_t sample = { "object_mask": utils.to_cuda(torch.zeros_like(model_input['object_mask'])).bool(), "uv": utils.to_cuda(model_input['uv']), "intrinsics": utils.to_cuda(model_input['intrinsics']), "pose": new_p } split = utils.split_input(sample, total_pixels) res = [] for s in split: out = model(s) res.append({ 'rgb_values': out['rgb_values'].detach(), }) batch_size = 1 model_outputs = utils.merge_output(res, total_pixels, batch_size) rgb_eval = model_outputs['rgb_values'] rgb_eval = rgb_eval.reshape(batch_size, total_pixels, 3) rgb_eval = (rgb_eval + 1.) / 2. rgb_eval = plt.lin2img(rgb_eval, img_res).detach().cpu().numpy()[0] rgb_eval = rgb_eval.transpose(1, 2, 0) img = Image.fromarray((rgb_eval * 255).astype(np.uint8)) img.save('{0}/eval_{1}.png'.format(images_dir, '%03d' % i))
def path_planner(start=(0, 2), end=(5, 0), obstacle_coords=[]): # Road split into sections road_map = [ [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], ] # Road sections as rectangles [x0,y0,x1,y1] sections = [ [[0, 0, 10, 10], [10, 0, 20, 10], [20, 0, 30, 10]], [[0, 10, 10, 20], [10, 10, 20, 20], [20, 10, 30, 20]], [[0, 20, 10, 30], [10, 20, 20, 30], [20, 20, 30, 30]], [[0, 30, 10, 40], [10, 30, 20, 40], [20, 30, 30, 40]], [[0, 40, 10, 50], [10, 40, 20, 50], [20, 40, 30, 50]], [[0, 50, 10, 60], [10, 50, 20, 60], [20, 50, 30, 60]], ] # Ostacles coordinates as points (x,y) obstacle_coords = [ (0, 5), (1, 1), (25, 33), (9, 45), (15, 15), ] # Check for obstacles in each road section and mark on road map for obstacle in obstacle_coords: for x, rectangles in enumerate(sections): for y, rectangle in enumerate(rectangles): if rect_contains(rectangle, obstacle): road_map[x][y] = 1 # Find path from start to end path = astar(road_map, start, end) # Mark path on road map for i in path: if road_map[i[0]][i[1]] is not 1: road_map[i[0]][i[1]] = 2 # Print some test data print('\nStart_section: ' + str(start) + '\nEnd_section: ' + str(end)) print('\nObstacle_coordinates: \n' + str(obstacle_coords)) print('\nPath: \n' + str(path)) print('\nRectangle_grid: (1=contain_obstacle, 2=path)') for section in road_map: print(section) # Plot path import numpy as np from scipy.interpolate import CubicSpline import matplotlib.pyplot as plt x = [] y = [] for i, j in path: x.append(i) y.append(j) xo = [] yo = [] for k, sections in enumerate(road_map): for l, section in enumerate(sections): if road_map[k][l] == 1: xo.append(k) yo.append(l) print('\n') cs = CubicSpline(x, y) xs = np.arange(-0.0, 5.1, 0.1) plt.figure(figsize=(6.5, 3)) plt.plot(x, y, 'o', label='Waypoint') plt.plot(xs, cs(xs), label="Path") plt.plot(xo, yo, 'o', label='Obstacle') plt.xlim(-0.5, 5.5) plt.ylim(-1, 4) plt.legend(loc='upper left', ncol=2) plt.grid(True) plt.title('A* path planner') plt.xlabel('Road') plt.ylabel('Lanes') plt.show() return path
def getDiffArraysForNumPrincpalComponents( nComponents, nSamples=100 ): ''' New method, test the GPR for the default cosmology and then lineraly interpolate this to a new cosmology ''' #hubble interpolator over a small number o hubbleInterpolator = \ hubbleModel.hubbleInterpolator( nPrincipalComponents=nComponents ) hubbleInterpolator.getTrainingData('exactPDFpickles/trainingDataWithMass.pkl') hubbleInterpolator.getTimeDelayModel() allDistributions = \ pkl.load(open(hubbleInterpolator.allDistributionsPklFile,'rb')) #set up an array #This one is truth - predictedCDF diffArray = np.zeros((nSamples, len(hubbleInterpolator.timeDelays))) #this one is truth - true pca with shifted cosmology diffPCA= np.zeros((nSamples, len(hubbleInterpolator.timeDelays))) #Cosmology labels cosmoKeys = hubbleInterpolator.fiducialCosmology.keys() doneInts = [] for i in np.arange(nSamples): #Cant do all of them so randomly select one randInt = np.random.randint(0, len(allDistributions)) #Makes sure i dont re-do one if randInt in doneInts: continue doneInts.append(randInt) #Get the raw distriubtion iDist = allDistributions[randInt] #and determine the cdf truth = iDist['y'][ iDist['x'] > \ hubbleInterpolator.logMinimumTimeDelay] truthCumSum = np.cumsum(truth)/np.sum(truth) #Get the params of this distribution params = iDist['cosmology'] fileName = iDist['fileNames'][0] zLensStr = fileName.split('/')[-2] zLens = np.float(zLensStr.split('_')[1]) densityProfile = \ getDensity.getDensityProfileIndex(fileName)[0] totalMassForHalo = getMass.getTotalMass( fileName ) defaultDistributionIndex = \ ( hubbleInterpolator.features['densityProfile'] == densityProfile ) &\ ( hubbleInterpolator.features['zLens'] == zLens ) & \ ( hubbleInterpolator.features['totalMass'] == totalMassForHalo ) params['zLens'] = zLens params['densityProfile'] = densityProfile params['totalMass'] = totalMassForHalo params['H0'] /= 100. #and get the principal components that describe this truePrincpalComponents = \ hubbleInterpolator.principalComponents[defaultDistributionIndex,:] #and then the distriubtion described by the PCA in the default cosmology pcaCDFinDefaultCosmology = hubbleInterpolator.pca.inverse_transform( truePrincpalComponents[0] ) #Get the cosmological interpolated shift and shift it interpolateThisCosmology = \ np.array([ iDist['cosmology'][i] for i in \ hubbleInterpolator.fiducialCosmology.keys()]) cosmoShift = \ hubbleInterpolator.interpolatorFunction.predict(interpolateThisCosmology.reshape(1,-1)) spline = CubicSpline( hubbleInterpolator.timeDelays+cosmoShift, pcaCDFinDefaultCosmology) #So this is the PDF of the true components, interpolated to the new cosmology pcaPDFinShiftedCosmology = spline( hubbleInterpolator.timeDelays) diffPCA[i,:] = pcaPDFinShiftedCosmology - truthCumSum #This is the predicted CDF from GPR interpolated to the new cosmology predictCDF = hubbleInterpolator.predictCDF( hubbleInterpolator.timeDelays, params) diffArray[i,:] = predictCDF - truthCumSum return {'x': hubbleInterpolator.timeDelays, 'diffPredict': diffArray, 'diffPCA':diffPCA}
from initial_condition import InitialConditionOtherTenors x = np.linspace(0, 1, 10, endpoint=True) y = norm.cdf(x) left_deriv = (y[1] - y[0]) / (x[1] - x[0]) right_deriv = (y[-1] - y[-2]) / (x[-1] - x[-2]) #f = interpolate.interp1d(x, y, kind = 'cubic', fill_value='extrapolate') f1 = CubicSpline(x, y, bc_type='natural', extrapolate=True) f2 = CubicSpline(x, y, bc_type=((1, left_deriv), (1, right_deriv)), extrapolate=True) f3 = CubicSpline_LinearExtrp(x, y) f4 = InitialConditionOtherTenors(x, y) xnew = np.linspace(-1, 2, 50, endpoint=True) ynew1 = f1(xnew) ynew2 = f2(xnew) ynew3 = f3.interpolate(xnew) ynew4 = f4.compute(xnew) #plt.plot(x, y, 'o', xnew, ynew1, 'r-', xnew, ynew3, 'b-', xnew, ynew4, 'g-') plt.plot(x, y, 'o', xnew, ynew4, 'g-') plt.show()
def test_periodic_eval(self): x = np.linspace(0, 2 * np.pi, 10) y = np.cos(x) S = CubicSpline(x, y, bc_type='periodic') assert_almost_equal(S(1), S(1 + 2 * np.pi), decimal=15)
def prediction_one_point(state, tau, predicting_ind, E, interpolate=False, plotting=False): """ Visualize how nearest neighbors look like using SIMPLEX projection method E: embedding dimension predicting_ind: which row in Mx that we aim to predict its future values """ ## Obtain data disease = 'rubella' t, locations, tot_data, data_dic = preprocess_Mexico_disease_data(disease) x = data_dic[state] # causal variable, to be constructed start_year = 1986 start_year_num = start_year - 1985 - 1 start_month = 1 start_time_ind = start_year_num * 12 + start_month - 1 end_year = 2008 end_year_num = end_year - 1985 - 1 end_month = 12 end_time_ind = end_year_num * 12 + end_month - 1 t = t[start_time_ind:end_time_ind:1] X = x[start_time_ind:end_time_ind:1] # chop the series at month/year # interpolate data using cubic spline if interpolate: dt = 0.4 # finer time step size t_finer = np.arange(0, len(X), dt) # finer time array cs = CubicSpline(t, X) X = cs(t_finer) t = t_finer Tp = 1 # how many time steps to forecast in the future # generate embedding using the 1D time series eX = Embed(X) Mx = eX.embed_vectors(tau, E) # shadow manifold of x simplex_pred = simplex(E, X, Mx, tau) cache = simplex_pred.one_forecasting(Tp, predicting_ind) y_pred = cache['prediction'] y_actual_ind = cache['actual data indices'] y_actual = X[y_actual_ind] t_pred = t[y_actual_ind] # time plotted with y_actual and y_pred y = Mx[predicting_ind, :] # targeted data to be predicted Tp ahead ty = t[predicting_ind:predicting_ind + (E - 1) * tau + 1:tau] # time plotted with y nn_ind = cache['nearest neighbors indices'] nn_y = Mx[nn_ind, :] # y's nearest neighbors t_nn = np.array([t[ind:ind + (E - 1) * tau + 1:tau] for ind in nn_ind]) # plotting if plotting: plt.figure(0) plt.plot(t, X) plt.plot(ty, y, 'bo', markersize=10) for i in range(nn_y.shape[0]): plt.plot(t_nn[i, :], nn_y[i, :], '*', markersize=15) plt.figure(1) plt.plot(y, 'bo-', markersize=10) plt.plot(np.transpose(nn_y), '*-') plt.plot(np.array(range(Tp)) + len(y), y_pred, 'r*', markersize=20) plt.plot(np.array(range(Tp)) + len(y), y_actual, 'ro', markersize=20) return y_actual, y_pred