def smooth_series(series_data, window_len=11, window='hanning'): # uses the smoothing function in functions.py to return a series with the data smoothed and the index left alone data = series_data.values indices = series_data.index smooth_data = f.smooth(data, window_len, window)[1] # uses numpy function convolve smooth_ts = pd.Series(data=smooth_data, index=indices) return smooth_ts
r_new = np.linspace(np.amin(r),np.amax(r),npts) tstamp('files loading') #setting up shorthand repeatedly used in kernel evaluation def wig_red(m1,m2,m3): '''3j symbol with upper row fixed''' return fn.wig(l_,s,l,m1,m2,m3) om = fn.omega p = (-1)**(l+l_+s) #parity of selected modes prefac = np.sqrt((2*l_+1.) * (2*s+1.) * (2*l+1.) / (4.* np.pi)) * wig_red(-m_,t,m) #EIGENFUCNTION DERIVATIVES #smoothing U,dU,d2U = fn.smooth(U,r,window,order,npts) V,dV,d2V = fn.smooth(V,r,window,order,npts) U_,dU_,d2U_ = fn.smooth(U_,r,window,order,npts) V_,dV_,d2V_ = fn.smooth(V_,r,window,order,npts) rho_sm, __, __ = fn.smooth(rho,r,window,order,npts) #re-assigning with smoothened variables r = r_new rho = rho_sm ##no smoothing #dU, dV = np.gradient(U,r), np.gradient(V,r) #dU_, dV_ = np.gradient(U_,r), np.gradient(V_,r) #d2U_,d2V = 0.,0.
def __init__(self, files, rings, cmwp_results): m = 0 # archivo con las soluciones del ajuste for i in range(0, rings.numphases): outfile = "%s%sCMWP_SOL_PF_P%d.mtex" \ % (files.pathout, files.input_file, i) fp_sol = open(outfile, "w") fp_sol.write("IDEA CMWP --- RESULT FILE --- %s\n" % time.strftime("%d/%m/%Y %I:%M:%S")) fp_sol.write("Row 2theta theta omega gamma alpha beta ") fp_sol.write("a a_err b b_err c c_err d d_err e e_err stpr stpr_err\n") fp_sol.flush() fp_sol.close() # archivo con las soluciones fisicas outfile = "%s%sCMWP_PHYSSOL_PF.mtex" % (files.pathout, files.input_file) fp_physsol = open(outfile, "w") fp_physsol.write("IDEA CMWP --- RESULT FILE --- %s\n" % time.strftime("%d/%m/%Y %I:%M:%S")) fp_physsol.write("Row 2theta theta omega gamma alpha beta ") for field in cmwp_results.header: fp_physsol.write("%s " % field) fp_physsol.write("\n") fp_physsol.flush() # archivo con los parametros de calidad de ajuste outfile = "%s%sCMWP_FITVAR_PF.mtex" % (files.pathout, files.input_file) fp_fitvar = open(outfile, "w") fp_fitvar.write("IDEA CMWP --- RESULT FIT FILE --- %s\n" % time.strftime("%d/%m/%Y %I:%M:%S")) fp_fitvar.write("Row 2theta theta omega gamma alpha beta WSSR rms redchisq\n") fp_fitvar.flush() k = 0 # contador del archivo mtex spr = rings.spr_i # indice que me marca el spr ptrn_i = rings.pattern_i + rings.delta_pattern ptrn_f = rings.pattern_f # tranformacion angular (gamma, omega) -> (alpha, beta) for omega in range(rings.omega_i, rings.omega_f + 1, rings.delta_omega): pattern = rings.pattern_i for pattern in range(rings.pattern_i, rings.pattern_f + 1, rings.delta_pattern): alpha = winkel_al(rings.dtheta[m] * 0.5, omega, pattern) beta = winkel_be(rings.dtheta[m] * 0.5, omega, pattern, alpha) if(beta < 0): beta = 360 + beta for i in range(0, rings.numphases): outfile = "%s%sCMWP_SOL_PF_P%d.mtex" \ % (files.pathout, files.input_file, i) fp_sol = open(outfile, "a") # salida al archivo con los valores del ajuste fp_sol.write("%d %8.3f %8.3f %8.1f %8.1f %8.4f %8.4f " % (k + 1, rings.dtheta[m], rings.dtheta[m] * 0.5, omega, pattern, alpha, beta)) for j in range(0, cmwp_results.sol.shape[3]): # soluciones matematicas del ajuste fp_sol.write("%8.5e %8.5e " % (cmwp_results.sol[(spr - rings.spr_i) / rings.delta_spr][(pattern - ptrn_i) / rings.delta_pattern][i][j], cmwp_results.solerr[(spr - rings.spr_i) / rings.delta_spr][(pattern - ptrn_i) / rings.delta_pattern][i][j])) fp_sol.write("\n") fp_sol.flush() fp_sol.close() # salida al archivo con las soluciones fisicas fp_physsol.write("%d %8.3f %8.3f %8.1f %8.1f %8.4f %8.4f " % (k + 1, rings.dtheta[m], rings.dtheta[m] * 0.5, omega, pattern, alpha, beta)) for i in range(0, cmwp_results.physsol.shape[2]): ini_x = (rings.spr_i - rings.spr_i) / rings.delta_spr x = (spr - rings.spr_i) / rings.delta_spr end_x = (rings.spr_f - rings.spr_i) / rings.delta_spr ini_y = (ptrn_i - ptrn_i) / rings.delta_pattern y = (pattern - ptrn_i) / rings.delta_pattern end_y = (ptrn_f - ptrn_i) / rings.delta_pattern ps = cmwp_results.physsol[x][y][i] flag = return_flag() if(ps == flag): smooth(cmwp_results.physsol, ini_x, x, end_x, ini_y, y, end_y, i, flag) fp_physsol.write("%8.5e " % (cmwp_results.physsol[x][y][i])) else: fp_physsol.write("%8.5e " % (cmwp_results.physsol[x][y][i])) fp_physsol.write("\n") # salida al archivo con los parametros de calidad de ajuste fp_fitvar.write("%d %8.3f %8.3f %8.1f %8.1f %8.4f %8.4f " % (k + 1, rings.dtheta[0], rings.dtheta[0] * 0.5, omega, pattern, alpha, beta)) for i in range(0, cmwp_results.fitvar.shape[2]): fp_fitvar.write("%8.5e " % (cmwp_results.fitvar[(spr - rings.spr_i) / rings.delta_spr][(pattern - ptrn_i) / rings.delta_pattern][i])) fp_fitvar.write("\n") # siguiente dato k += 1 spr += rings.delta_spr fp_physsol.flush() fp_physsol.close() fp_fitvar.flush() fp_fitvar.close() organize_files(files) self.exit = 0
def ret_kerns_axis_symm(self, smoothen=False, a_coeffkerns=False): n, l, m, n_, l_ = self.n, self.l, self.mm, self.n_, self.l_ m_ = m r_start, r_end = self.r_range nl = fn.find_nl(n, l) nl_ = fn.find_nl(n_, l_) len_m, len_s = np.shape(self.ss_o) #Savitsky golay filter for smoothening window = 45 #must be odd order = 3 if (nl == None or nl_ == None): print("Mode not found. Exiting.") exit() tstamp() om = np.vectorize(fn.omega) parity_fac = (-1)**(l + l_ + self.ss_o) #parity of selected modes if (a_coeffkerns == True): prefac = ((-1.)**l)/(4.* np.pi) * np.sqrt((2*l_+1.) * (2*self.ss_o+1.) * (2*l+1.) \ / (4.* np.pi)) * self.wig_red_o(-l,0,l) / l else: prefac = 1./(4.* np.pi) * np.sqrt((2*l_+1.) * (2*self.ss_o+1.) * (2*l+1.) \ / (4.* np.pi)) * self.wig_red_o(-m,0,m) tstamp('prefac computation') #EIGENFUCNTION DERIVATIVES ################################################################### #smoothing #interpolation params if (smoothen == True): npts = 300 #should be less than the len(r) in r.dat r_new = np.linspace(np.amin(self.r), np.amax(self.r), npts) self.ss_i, __ = np.meshgrid(self.s, r_new, indexing='ij') Ui, dUi, d2Ui = fn.smooth(self.Ui, self.r, window, order, npts) Vi, dVi, d2Vi = fn.smooth(self.Vi, self.r, window, order, npts) Ui_, dUi_, d2Ui_ = fn.smooth(self.Ui_, self.r, window, order, npts) Vi_, dVi_, d2Vi_ = fn.smooth(self.Vi_, self.r, window, order, npts) rho_sm, __, __ = fn.smooth(self.rho, self.r, window, order, npts) #re-assigning with smoothened variables r = r_new rho = rho_sm ###################################################################### #no smoothing else: r = self.r rho = self.rho Ui = self.Ui Vi = self.Vi Ui_ = self.Ui_ Vi_ = self.Vi_ dUi, dVi = np.gradient(Ui, r), np.gradient(Vi, r) dUi_, dVi_ = np.gradient(Ui_, r), np.gradient(Vi_, r) d2Ui_, d2Vi_ = np.gradient(dUi_, r), np.gradient(dVi_, r) ######################################################################### tstamp('load eigfiles') #making U,U_,V,V_,dU,dU_,dV,dV_,d2U,d2U_,d2V,d2V_ of same shape U = np.tile(Ui, (len_s, 1)) V = np.tile(Vi, (len_s, 1)) dU = np.tile(dUi, (len_s, 1)) dV = np.tile(dVi, (len_s, 1)) U_ = np.tile(Ui_, (len_s, 1)) V_ = np.tile(Vi_, (len_s, 1)) dU_ = np.tile(dUi_, (len_s, 1)) dV_ = np.tile(dVi_, (len_s, 1)) d2U_ = np.tile(d2Ui_, (len_s, 1)) d2V_ = np.tile(d2Vi_, (len_s, 1)) r = np.tile(r, (len_s, 1)) tstamp() #B-- EXPRESSION Bmm = self.wig_red(3, -2, -1) * om(l, 0) * om(l_, 0) * om(l_, 2) * om( l_, 3) * V * V_ Bmm += self.wig_red(0, -2, 2) * om(l, 0) * om(l, 2) * r * V * dU_ Bmm += self.wig_red(1, -2, 1) * om(l_, 0) * om( l, 0) * (-U * U_ + U * V_ + om(l_, 2)**2 * V * V_ - r * U * dV_) Bmm += self.wig_red(2, -2, 0) * om(l_, 0) * om( l_, 2) * (U * V_ + r * dU * V_ - r * U * dV_) Bmm = (((-1)**np.abs(1+m_))*prefac)[:,:,np.newaxis] \ * (Bmm/r**2)[np.newaxis,:,:] #tstamp('Bmm done') #B0- EXPRESSION B0m = self.wig_red(0, -1, 1) * om( l, 0) * (2 * U * U_ + om(l_, 2)**2 * V * U_ + om(l_, 0)**2 * (-2 * U * V_ - V * V_ + r * V * dV_) + r * (-U - V + r * dV) * dU_) B0m += self.wig_red(-1, -1, 2) * om(l, 0) * om(l_, 0) * om( l, 2) * V * (U_ - V_ + r * dV_) B0m += self.wig_red(2, -1, -1) * om(l, 0) * om(l_, 0) * om( l_, 2) * (V * U_ - 3 * V * V_ + r * V * dV_ + 2 * r * dV * V_) B0m -= self.wig_red(1, -1, 0) * om( l_, 0) * (-2 * U * U_ + om(l_, 0)**2 * V * U_ + om(l, 0)**2 * (-V * V_ + r * V * dV_) + U * (2 * V_ + r * (dU_ - 2 * dV_ + r * d2V_))) B0m = (0.5*((-1)**np.abs(m_))*prefac)[:,:,np.newaxis] \ * (B0m/r**2)[np.newaxis,:] #tstamp('B0m done') # print(np.shape(self.wig_red(-1,-0,1))) # exit() #B00 EXPRESSION B00 = -(self.wig_red(-1, 0, 1) + self.wig_red(1, 0, -1)) * om( l_, 0) * om(l, 0) * (V * (-4 * U_ + 2 * (1 + om(l_, 0)**2) * V_ + r * (dU_ - 2 * dV_)) + 2 * r * dV * (U_ - V_ + r * dV_)) B00 += self.wig_red(0, 0, 0) * ( (6 * U - 4 * om(l, 0)**2 * V - 2 * r * dU) * U_ + 2 * om(l_, 0)**2 * ((-3 * U + 2 * om(l, 0)**2 * V + r * dU) * V_ + r * U * dV_) + r * ((-4 * U + 2 * om(l, 0)**2 * V + r * dU) * dU_ + r * U * d2U_)) B00 = (0.5*((-1)**np.abs(m_))*prefac)[:,:,np.newaxis] \ * (B00/r**2)[np.newaxis,:] #tstamp('B00 done') #B+- EXPRESSION Bpm = -2 * (self.wig_red(-2, 0, 2) + self.wig_red(2, 0, -2)) * om( l_, 0) * om(l, 0) * om(l_, 2) * om(l, 2) * V * V_ Bpm += (self.wig_red(-1, 0, 1) + self.wig_red(1, 0, -1)) * om( l_, 0) * om(l, 0) * (-r * V * dU_ + U * (U_ - V_ + r * dV_)) Bpm += self.wig_red(0, 0, 0) * r * r * (-dU * dU_ + U * d2U_) Bpm = (0.5*((-1)**np.abs(m_))*prefac)[:,:,np.newaxis] \ * (Bpm/r**2)[np.newaxis,:] #tstamp('Bpm done') Bmm = Bmm.astype('float64') B0m = B0m.astype('float64') B00 = B00.astype('float64') Bpm = Bpm.astype('float64') #constructing the other two components of the kernel Bpp = parity_fac[:, :, np.newaxis] * Bmm Bp0 = parity_fac[:, :, np.newaxis] * B0m if (a_coeffkerns == True): return rho, Bmm, B0m, B00, Bpm, Bp0, Bpp else: return Bmm, B0m, B00, Bpm, Bp0, Bpp
np.random.seed(100) wl1 = 50 wl2 = 100 df_dx = pd.DataFrame() all_files = [ load_data.file14, load_data.file15, load_data.file16, load_data.file17, load_data.file19, load_data.file20, load_data.file21, load_data.file22, load_data.file23 ] for i, j in enumerate([14, 15, 16, 17, 19, 20, 21, 22, 23]): df_dx['x%(j)s' % {"j": j}] = all_files[i]['x_shifted'] df_dx['y%(j)s' % {"j": j}] = functions.smooth(all_files[i]['y_norm'], wl1) df_dx['derivative y%(j)s' %{"j": j}] = np.divide(df_dx['y%(j)s'% {"j": j}].diff(), \ df_dx['x%(j)s'% {"j": j}].diff()) df_dx['smooth derivative y%(j)s' % { "j": j }] = functions.smooth(df_dx['derivative y%(j)s' % { "j": j }], wl2) li = [] for i in ([14, 15, 16, 19, 20, 21]): crossing = df_dx[(df_dx['derivative y%(i)s' % { "i": i }] > 0) & (df_dx['x%(i)s' % { "i": i
def main_2(t=0, time=100, path="./"): t_las_begin = 86 t_begin = 80 direct = os.listdir(path) peak_loc_array = np.array([]) peak_loc_tuples = [] x1ym = np.array([]) x1y = np.array([]) # For all files in directory # for d in direct: if d.split('_')[0] == "data": dir = d dir_parse = dir.split('_') YLoc = int(dir_parse[2]) XLoc = int(dir_parse[1]) dens_variance = np.zeros(time) # Assemble Tiles # for t in range(0, time): data0 = ut.getTile(t, (0, 1), path + "/" + dir) data1 = ut.getTile(t, (0, 2), path + "/" + dir) Z0 = np.log(data0['data'][:, :, ut.data_v['rh'] + 1]) / np.log(10) Z1 = np.log(data1['data'][:, :, ut.data_v['rh'] + 1]) / np.log(10) Z0 = Z0[:, Z0.shape[1] - int(Z0.shape[1] * (1 / 3)):] Z1 = Z1[:, :int(Z1.shape[1] * (1 / 3))] Z_ry0 = ut.reduce_y(Z0) Z_ry1 = ut.reduce_y(Z1) Z_ry_rod = np.zeros((Z_ry0.size + Z_ry1.size)) Z_ry_rod[0:Z_ry0.size] = Z_ry0 Z_ry_rod[Z_ry0.size:] = Z_ry1 Y = np.arange(-Z_ry_rod.size / 2, Z_ry_rod.size / 2) * data0['L0/dxi'] dens_row_norm = Z_ry_rod / Z_ry_rod.max() dens_variance[t] = np.var(Z_ry_rod) if t == 0: peak_dens = dens_row_norm else: peak_dens = np.vstack((dens_row_norm, peak_dens)) # rho_crit_fact = ut.find_critical_dens_fact(532, Z, 6 * 10 ** 21) # min = rho_crit_fact.min() # print(rho_crit_fact.min()) if False: rho_crit_fact = ut.find_critical_dens_fact( 532, Z, 6 * 10**21) print(rho_crit_fact.min) fig, ax = plt.subplots() ax.imshow(peak_dens) im = ax.imshow(rho_crit_fact, interpolation='gaussian', cmap='Greys') # fig2, ax2 = plt.subplots() # ax2.plot(Z_ry, color='k') plt.show() # Smooth Data # smooth_variance = ut.smooth(dens_variance, window_len=7, window='hanning') smooth_var_t = np.arange(0, time, (time - 0) / smooth_variance.size) # Find Inflection Points (first one) # i = ut.find_inflect(smooth_variance, thresh=20.0) i = len(dens_variance) - i # Use this inflection point as the time lock, now find the location of the peak density # x_max, dens_max = ut.find_peak( ut.smooth(peak_dens[i, :], window_len=2, window='hanning')) peak_loc_array = np.append(peak_loc_array, x_max * data0['L0/dxi']) peak_loc_tuples.append((XLoc, YLoc, x_max)) x1y = np.append(x1y, dl * YLoc + las_loc_start) x1ym = np.append(x1ym, (x_max - Z_ry_rod.size / 2) * data0['L0/dxi']) # ax.plot(XLoc, x_max-Z_ry_rod.size/2, 'xb') # Plot Results # if True: fig, ax11 = plt.subplots() im11 = ax11.plot(1000 * Y, 255 * peak_dens[i, :], 'k') ax11.plot(1000 * Y[x_max], 255 * dens_max, 'rx') print("x_laser= %f, x_peak= %f " % (1000 * (las_loc_start + dl * YLoc), 1000 * Y[x_max])) f, (ax1, ax2) = plt.subplots(1, 2, sharey=True) ax1.imshow(255 * peak_dens[:, :], extent=[1000 * Y[0], 1000 * Y[-1], 0, time * dt0], cmap='gray', interpolation='gaussian', aspect='auto') ax1.set_title('Time v. Ion Density') ax1.axvline(x=las_loc_start * 1000 + dl * YLoc * 1000, color='red') ax1.axvline(x=1000 * Y[x_max], color='blue') tlas = (t_las_begin - t_begin) ax1.axhline(y=tlas, color='red') ax2.plot(smooth_variance, smooth_var_t * dt0, 'k') ax2.plot(smooth_variance[len(dens_variance) - i], smooth_var_t[len(dens_variance) - i] * dt0, 'xk') ax2.set_title('Ion Density Variance') plt.tight_layout() corry1, _ = pearsonr(x1y, x1ym) print(corry1) plt.show()