import numpy as np import matplotlib.pyplot as plt from scipy import interpolate x = np.linspace(0,np.pi*4,20) y = np.sin(x) plt.scatter(x,y) # plt.show() #f = interpolate.interp1d(x,y,kind="linear") f = interpolate.UnivariateSpline(x,y,s=1) x2 = np.linspace(x.min(), x.max(), 1000) f2 = f(x2) plt.plot(x2,f2) plt.show() # polynomial=scipy.interpolate.lagrange(x, y) # xn = scipy.linspace(0,np.pi/2,100) # plt.plot(xn,polynomial(xn)) # plt.plot(x,y,'or') # plt.show()
print particleID.keys() print galaxyID.keys() print haloID.keys() print particleDist.keys() print galaxyDist.keys() print haloDist.keys() # =============================================================== print "-----------------------------------" print "-------CALCULATING REDSHIFTS-------" print "-----------------------------------" # create linear interpolation function using zr.txt to get Mpcs/H -> Z points = [line.split(" ") for line in open("zr.txt")] zrFunc = sip.UnivariateSpline(map(float, zip(*points)[1]), map(float, zip(*points)[2]), s=0) z = zrFunc(maxDistance) print "Max redshift is", z # =============================================================== # SEARCH THE TREE IN PARALLEL # =============================================================== print "-----------------------------------" print "--------- RM CALCULATIONS ---------" print "-----------------------------------" rotationMeasure = [] galaxyRM = [] haloRM = [] numberHit = []
import matplotlib as mpl import matplotlib.pyplot as plt from scipy import interpolate as intp # # read input file with P(k) output from CAMB # fname = './test_matterpower_logintk1000.dat' k, Pk = np.loadtxt(fname, usecols=(0, 1), unpack=True) lk = np.log10(k) lPk = np.log10(Pk) # # construct fitted spline of a given smoothing s # slPk = intp.UnivariateSpline(lk, lPk, s=0.1) # play with smoothing! slPk0 = intp.UnivariateSpline( lk, lPk, s=0.0) # effectively standard interpolated spline # # output first derivatives of the fitted spline # ders = [] ders0 = [] for i in range(len(lk)): dummy = slPk.derivatives(lk[i]) dummy0 = slPk0.derivatives(lk[i]) ders.append(dummy[1]) ders0.append(dummy0[1]) # take first derivative only # plot derivative approximations
#https://www.hackerrank.com/challenges/missing-stock-prices/problem import numpy as np from scipy import interpolate n = int(input()) prices = [] for i in range(n): time, price = input().split("\t") prices.append(price) X = [] Y = [] X_missing = [] for i in range(n): if not "Missing" in prices[i]: X.append(i) Y.append(float(prices[i])) else: X_missing.append(i) Y = np.array(Y) f = interpolate.UnivariateSpline(X, Y, s=1) for i in X_missing: print(f(i))
by_nearest.extend( (a, b) for gene, a, b in zip(emb1.index, emb1.ix[:, slice1], emb2.ix[:, slice2]) if (np.isfinite(a) and np.isfinite(b) and gene in both_expr)) for gene in emb1.index: if (gene not in both_expr or sum(np.isfinite(emb1.ix[gene])) < 5 or sum(np.isfinite(emb2.ix[gene])) < 5 or False): continue yvals = pd.rolling_mean(emb1.ix[gene], 3, center=True, min_periods=1) gx = np.isfinite(yvals) spline = interpolate.UnivariateSpline( emb1_xs[gx], yvals[gx], bbox=[0, 1], ) gx2 = (np.isfinite(emb2.ix[gene]) & (emb2_xs > emb1_xs[gx][0]) & (emb2_xs < emb1_xs[gx][-1])) smoothed_vals = spline(emb2_xs[gx2]) svnm = np.nanmean(smoothed_vals) if np.isnan(svnm): print(emb1.columns[0].split('_sl')[0], emb2.columns[0].split('_sl')[0], gene) #assert False smoothed_ase_vals[gene].extend(smoothed_vals) actual_ase_vals[gene].extend(emb2.ix[gene, gx2])
l = len(t) print((" sample length ", l, '\n')) # this depends on cabling of the PicoScope ! vI = data[2] # proportional to Current vB = data[1] # proportional to B-Field # if length is too large, resample by appropriate factor if l > 400: nr = int(l / 150) print(('** resampling by factor ', nr)) vI, t = resample(vI, t, n=nr) vB = resample(vB, n=nr) print('** spline interpolation') cs_I = interpolate.UnivariateSpline(t, vI, s=0) cs_B = interpolate.UnivariateSpline(t, vB, s=0) tplt = np.linspace(t[0], t[-1], 150) # take derivative of channel a # -- this is used to determine the branch of the hysteresis curve cs_Ideriv = cs_I.derivative() # for increasing current Ip = [] Bp = [] # for decreasing current Im = [] Bm = [] # tlist = np.linspace(t[0], t[-1], 200) # for i, ti in enumerate(t): if (cs_Ideriv(ti) > 0.):
def plotLayer(fig, ax, asciiHeader, meta, subtitle, onlyOnce, fontsize = 10, axlabelpad = None, axtickpad = None) : # Read in the ascii data array ascii_data_array = np.loadtxt(asciiHeader.ascii_path, dtype=np.float, skiprows=6) colorM = None # set min color if given if len(meta.minColor) > 0 and not meta.cMap: newColorMap = matplotlib.cm.get_cmap(meta.colormap, 256) newcolors = newColorMap(np.linspace(0, 1, 256)) for idC in range(256) : if idC == 0 : alpha = meta.mintransparent * meta.transparencyfactor rgba = matplotlib.cm.colors.to_rgba(meta.minColor, alpha=alpha) minColorVal = np.array([rgba]) newcolors[:1, :] = minColorVal else : newcolors[idC:idC+1, 3:4] = meta.transparencyfactor colorM = ListedColormap(newcolors) # Get the img object in order to pass it to the colorbar function elif meta.cMap : if meta.transparencyfactor < 1.0 or meta.mintransparent < 1.0: newColorMap = ListedColormap(meta.cMap) newcolors = newColorMap(np.linspace(0, 1, len(meta.cMap))) for idC in range(len(meta.cMap)) : alpha = meta.transparencyfactor if idC == 0 : alpha = meta.mintransparent * meta.transparencyfactor rgba = matplotlib.cm.colors.to_rgba(meta.cMap[idC], alpha=alpha) newcolors[idC:idC+1, :] = np.array([rgba]) colorM = ListedColormap(newcolors) else : colorM = ListedColormap(meta.cMap) else : # use color map name newColorMap = matplotlib.cm.get_cmap(meta.colormap, 256) newcolors = newColorMap(np.linspace(0, 1, 256)) for idC in range(256) : alpha = meta.transparencyfactor if idC == 0 : alpha = meta.mintransparent * meta.transparencyfactor newcolors[idC:idC+1, 3:4] = alpha colorM = ListedColormap(newcolors) if meta.renderAs == "heatmap" : # Set the nodata values to nan ascii_data_array[ascii_data_array == asciiHeader.ascii_nodata] = np.nan if meta.removeEmptyColumns : ascii_data_array = ascii_data_array[:,~np.isnan(ascii_data_array).all(axis=0)] rowcol = ascii_data_array.shape image_extent = [ asciiHeader.ascii_xll, asciiHeader.ascii_xll + rowcol[1] * asciiHeader.ascii_cs, asciiHeader.ascii_yll, asciiHeader.ascii_yll + asciiHeader.ascii_rows * asciiHeader.ascii_cs] # data is stored as an integer but scaled by a factor ascii_data_array *= meta.factor if meta.minLoaded and meta.maxLoaded: img_plot = ax.imshow(ascii_data_array, cmap=colorM, extent=image_extent, interpolation='none', vmin=meta.minValue, vmax=meta.maxValue) elif meta.minLoaded : img_plot = ax.imshow(ascii_data_array, cmap=colorM, extent=image_extent, interpolation='none', vmax=meta.minValue) elif meta.maxLoaded : img_plot = ax.imshow(ascii_data_array, cmap=colorM, extent=image_extent, interpolation='none', vmax=meta.maxValue) else : img_plot = ax.imshow(ascii_data_array, cmap=colorM, extent=image_extent, interpolation='none') if meta.showbars : axins = inset_axes(ax, width="5%", # width = 5% of parent_bbox width height="90%", # height : 50% loc='lower left', bbox_to_anchor=(1.05, 0., 1, 1), bbox_transform=ax.transAxes, borderpad=0, ) if meta.ticklist : # Place a colorbar next to the map cbar = fig.colorbar(img_plot, ticks=meta.ticklist, orientation='vertical', shrink=0.5, aspect=14, cax=axins) else : # Place a colorbar next to the map cbar = fig.colorbar(img_plot, orientation='vertical', shrink=0.5, aspect=14, cax=axins) if len(meta.label) > 0 : #cbar.ax.set_label(meta.label) cbar.ax.set_title(meta.label, loc='left') if meta.cbarLabel : cbar.ax.set_yticklabels(meta.cbarLabel) if len(meta.title) > 0 : ax.set_title(meta.title, y=meta.yTitle, x=meta.xTitle) if len(subtitle) > 0 : ax.set_title(subtitle) #ax.set_axis_off() ax.grid(True, alpha=0.5) ax.axes.xaxis.set_visible(False) ax.axes.yaxis.set_visible(False) if meta.renderAs == "densitySpread" : if onlyOnce : ax.axes.invert_yaxis() ascii_data_array[ascii_data_array == asciiHeader.ascii_nodata] = np.nan arithemticMean = np.nanmean(ascii_data_array, axis=1) arithemticMean = np.nan_to_num(arithemticMean) arithemticMean *= meta.densityFactor maxV = np.max(arithemticMean) minV = np.min(arithemticMean) if meta.densityReduction > 0 : y = np.linspace(0, len(arithemticMean)-1, len(arithemticMean)) spl = spy.UnivariateSpline(y, arithemticMean) ys = np.linspace(0, len(arithemticMean), meta.densityReduction) y_new = np.linspace(0, len(arithemticMean), 500) a_BSpline = spy.interpolate.make_interp_spline(ys, spl(ys)) x_new = a_BSpline(y_new) x_new[x_new < minV] = minV x_new[x_new > maxV] = maxV if len(meta.lineColor) > 0 : ax.plot(x_new,y_new, label=meta.lineLabel, color=meta.lineColor) else : ax.plot(x_new,y_new, label=meta.lineLabel) else : y = np.linspace(0, len(arithemticMean)-1, len(arithemticMean)) if len(meta.lineColor) > 0 : ax.plot(arithemticMean, y, label=meta.lineLabel, color=meta.lineColor) else : ax.plot(arithemticMean, y, label=meta.lineLabel) if len(meta.lineLabel) > 0 : ax.legend(fontsize=6, handlelength=1) #ax.legend(fontsize=fontsize, handlelength=1, bbox_to_anchor=(1.05, 1), loc='upper left') if onlyOnce : # do this only once def update_ticks(val, pos): val *= (1/meta.densityFactor) val *= meta.factor return str(val) ax.xaxis.set_major_formatter(mticker.FuncFormatter(update_ticks)) if meta.yTicklist : ax.set_yticks(meta.yTicklist) if meta.xTicklist : ax.set_xticks(meta.xTicklist) if axtickpad != None : ax.yaxis.set_tick_params(which='major', pad=axtickpad) ax.xaxis.set_tick_params(which='major', pad=axtickpad) def applyTickLabelMapping(file, ref, tar, textformat, axis): if len(file) > 0 and len(ref) > 0 and len(tar) > 0 : lookup = readAxisLookup(file, ref, tar) def update_ticks_fromLookup(val, pos): if val in lookup : if len(textformat) > 0 : newVal = lookup[val] return textformat.format(newVal) return str(lookup[val]) return '' axis.set_major_formatter(mticker.FuncFormatter(update_ticks_fromLookup)) applyTickLabelMapping(meta.YaxisMappingFile, meta.YaxisMappingRefColumn, meta.YaxisMappingTarColumn, meta.YaxisMappingFormat, ax.yaxis) applyTickLabelMapping(meta.XaxisMappingFile, meta.XaxisMappingRefColumn, meta.XaxisMappingTarColumn, meta.XaxisMappingFormat, ax.xaxis) if len(meta.yLabel) > 0 : ax.set_ylabel(meta.yLabel, labelpad=axlabelpad) if len(meta.xLabel) > 0 : ax.set_xlabel(meta.xLabel, labelpad=axlabelpad) if len(meta.title) > 0 : ax.set_title(meta.title, y=meta.yTitle, x=meta.xTitle) for item in ([ax.xaxis.label, ax.yaxis.label] + ax.get_xticklabels() + ax.get_yticklabels()): item.set_fontsize(fontsize)
def sple_grad(x, y, par, isteps=100): """ NAME: sple_grad PURPOSE: compute the deflection of an SPLE mass distribution (singular power-law ellipsoid) USAGE: (xg, yg) = sple_grad(x, y, par) ARGUMENTS: x, y: vectors or images of coordinates; should be matching numpy ndarrays. Currently there is no testing for size matching! par: vector of parameters with 1 to 6 elements, defined as follows: par[0]: lens strength, or 'Einstein radius' par[1]: (optional) x-center (default = 0.0) par[2]: (optional) y-center (default = 0.0) par[3]: (optional) axis ratio (default=1.0) par[4]: (optional) major axis Position Angle in degrees c.c.w. of x axis. (default = 0.0) par[5]: power-law index 'gamma': surface density propto R^-gamma. gamma = 1 is isothermal (r^-2 in 3D); gamma > 1 is steeper than isothermal gamma < i is shallower than isothermal. isteps: number of steps in 90deg of azimuth over which to compute, for subsequent interpolation. RETURNS: tuple (xg, yg) of gradients at the positions (x, y) NOTES: This routine implements an 'intermediate-axis' convention. The parameter-order convention in this routine differs from that of a previous IDL routine of the similar name by ASB. This routine is not efficient for small numbers (less than ~100) of image-plane points. It is written with the intention of working efficiently for evaluation over a large number of image plane points during the course of lens-model optimization, so it evaluates the expensive integrals over a baseline gridded by 'isteps' and interpolates. If one had fewer than 'isteps' points in the image plane, it would make more sense just to integrate directly for their associated values, but that would require additional logic. I *think* this is originally based upon Barkana 1998 ApJ, 502, 531, with the simplification of zero core radius. It is translated from an IDL code written 6 years ago by teh translator, so memory is hazy. WRITTEN: Adam S. Bolton, U of Utah, 2009 """ # Extract parameters: bpl = n.abs(par[0]) # lens strength, can't be negative! xzero = 0. if (len(par) < 2) else par[1] yzero = 0. if (len(par) < 3) else par[2] q = 1. if (len(par) < 4) else n.abs(par[3]) phiq = 0. if (len(par) < 5) else par[4] gpl = 1. if (len(par) < 6) else par[5] # Handle q > 1 gracefully: if (q > 1.): q = 1.0 / q phiq = phiq + 90.0 # Don't let gpl exceed the bounds [0., 2.]: gpl = 2.0 if gpl > 2.0 else gpl gpl = 0.0 if gpl < 0.0 else gpl # Go into shifted coordinates of the potential: phirad = n.deg2rad(phiq) xpl = (x - xzero) * n.cos(phirad) + (y - yzero) * n.sin(phirad) ypl = (y - yzero) * n.cos(phirad) - (x - xzero) * n.sin(phirad) # Store quadrant info and reduce to quadrant 1: xnegative = xpl < 0. ynegative = ypl < 0. xpl = n.abs(xpl) ypl = n.abs(ypl) # Compute azimuth and radial coordinate. # No need to use generalized radial coordinate, # since we can just scale it later. theta = n.asarray(n.arctan2(ypl, xpl)) r = n.asarray(n.hypot(xpl, ypl)) # Compute potential gradient around a ring at unit radius: rfid = 1. thbase = 0.5 * n.pi * (n.arange(float(isteps + 3)) - 1.) / float(isteps) xgbase = n.zeros(float(isteps + 3)) ygbase = n.zeros(float(isteps + 3)) for i in range(isteps + 3): xpl_this = rfid * n.cos(thbase[i]) ypl_this = rfid * n.sin(thbase[i]) rhomax = n.sqrt(xpl_this**2 + ypl_this**2 / q**2) umax = rhomax**(2. - gpl) def pl_integrand_x_2(u): rho = u**(1. / (2. - gpl)) delta = n.sqrt(( (1. - q**2) * rho**2 + ypl_this**2 - xpl_this**2)**2 + 4. * xpl_this**2 * ypl_this**2) omega = n.sqrt( (delta + xpl_this**2 + ypl_this**2 + (1. - q**2) * rho**2) / (delta + xpl_this**2 + ypl_this**2 - (1. - q**2) * rho**2)) integrand = 2. * xpl_this * q * 0.5 * ( bpl / n.sqrt(q))**gpl * omega / (xpl_this**2 + omega**4 * ypl_this**2) return integrand xgbase[i] = ig.romberg(pl_integrand_x_2, 0., umax, vec_func=True) def pl_integrand_y_2(u): rho = u**(1. / (2. - gpl)) delta = n.sqrt(( (1. - q**2) * rho**2 + ypl_this**2 - xpl_this**2)**2 + 4. * xpl_this**2 * ypl_this**2) omega = n.sqrt( (delta + xpl_this**2 + ypl_this**2 + (1. - q**2) * rho**2) / (delta + xpl_this**2 + ypl_this**2 - (1. - q**2) * rho**2)) integrand = 2. * ypl_this * q * 0.5 * ( bpl / n.sqrt(q))**gpl * omega**3 / (xpl_this**2 + omega**4 * ypl_this**2) return integrand ygbase[i] = ig.romberg(pl_integrand_y_2, 0., umax, vec_func=True) # Compute interpolating splines: xgb2func = ip.UnivariateSpline(thbase, xgbase, s=0., k=3) ygb2func = ip.UnivariateSpline(thbase, ygbase, s=0., k=3) # Evaluate splines for deflection values at r=rfid # (gotta flatten, because I think there's some FORTRAN under the hood.) xtg = (xgb2func(theta.flatten())).reshape(theta.shape) ytg = (ygb2func(theta.flatten())).reshape(theta.shape) # Scale to the appropriate values for the actual radii: xtg = xtg * ((r + (r == 0)) / rfid)**(1. - gpl) * (r != 0) ytg = ytg * ((r + (r == 0)) / rfid)**(1. - gpl) * (r != 0) # Restore quadrant-appropriate signs: xtg = xtg * (-1.)**xnegative ytg = ytg * (-1.)**ynegative # Take gradient back into the un-rotated system and return: xg = xtg * n.cos(phirad) - ytg * n.sin(phirad) yg = ytg * n.cos(phirad) + xtg * n.sin(phirad) return (xg, yg)
for band in range(len(n_list)): print('frequency band %i/%i' % ((band + 1), len(n_list))) #loop over each pressure (each individual data file) for i in range(len(time_table)): #read data data0 = pd.read_table(good_files[i][band]) print('n = %i, timestep = %i, RH = %i' % (n_list[band], i, rh_list[i])) #get frequency and conductance (G) freq0, g0 = data0['f_res'], data0['g'] #freq0 *= 1e-6 #fit conductance to spline for smoothing spline_fit = inter.UnivariateSpline(freq0, g0, s=5e-9) g0_spline = spline_fit(freq0) #G0 -= np.min(G0) #G0 /= np.max(G0) #G0 *= 1e3 #save resonant frequency and maximum conductance dic['f_res'][i, band] = freq0[np.argmax(g0_spline)] dic['G_max'][i, band] = np.max(g0_spline) #set guess for BVD fitting #guess params: Gp, Cp, Gmax00, D00, f00 if i == 0: guess0 = [ np.min(g0), 0, dic['G_max'][i, band], 0.0008, dic['f_res'][i, band]
Y = np.cumsum(B) X = np.cumsum(C) # Rho Yhat = lowess.lowess(Y, X, 0.5)[:, 1] # Now sample it with indices yhat = interp.interp1d(X, Yhat) # Correct the first value x = np.linspace(0, X[-1], len(X)) yhat = yhat(x) yhat[0] = 2 * yhat[1] - yhat[2] # Rho : put some splines through Yhat and take the derivative rho = interp.UnivariateSpline(x, yhat).derivative()(x) """ reg = linear_model.BayesianRidge(fit_intercept=False, compute_score=True) # Compute the R^2 for a range of polynomials from degree-1 to degree-7 # The fit score has a penalty proportional to the square of the degree of the polynomial Ns = range(2, 8) #scores = [] for n in Ns : reg.fit(np.vander(X, n), Y) scores.append(reg.score(np.vander(X, n), Y) - penalty * n**2) # Use the polynomial that maximised R^2 to compute Yhat Yhat = reg.fit(np.vander(X, 4), Y).predict(np.vander(X, 4))
u_pres=np.zeros((len(plev_std),ni[2]),'float') v_pres=np.zeros((len(plev_std),ni[2]),'float') relh_pres=np.zeros((len(plev_std),ni[2]),'float') dwpo_pres=np.zeros((len(plev_std),ni[2]),'float') for j in range(0,ni[2]): yt=temp[~np.isnan(temp[:,j]),j] ym=mixr[~np.isnan(mixr[:,j]),j] yw=u[~np.isnan(u[:,j]),j] yd=v[~np.isnan(v[:,j]),j] yr=relh[~np.isnan(relh[:,j]),j] yp=dwpo[~np.isnan(dwpo[:,j]),j] xp=pres[~np.isnan(temp[:,j]),j] temp_interp_pres=si.UnivariateSpline(xp[::-1],yt[::-1],k=5) mixr_interp_pres=si.UnivariateSpline(xp[::-1],ym[::-1],k=5) u_interp_pres=si.UnivariateSpline(xp[::-1],yw[::-1],k=5) v_interp_pres=si.UnivariateSpline(xp[::-1],yd[::-1],k=5) relh_interp_pres=si.UnivariateSpline(xp[::-1],yr[::-1],k=5) dwpo_interp_pres=si.UnivariateSpline(xp[::-1],yp[::-1],k=5) for ind in range(0,len(plev_std)): temp_pres[ind,j]=temp_interp_pres(plev_std[ind]) mixr_pres[ind,j]=mixr_interp_pres(plev_std[ind]) u_pres[ind,j]=u_interp_pres(plev_std[ind]) v_pres[ind,j]=v_interp_pres(plev_std[ind]) relh_pres[ind,j]=relh_interp_pres(plev_std[ind]) dwpo_pres[ind,j]=dwpo_interp_pres(plev_std[ind])
def calc_plot_and_save_fitted_data(cmd_line_arg): ''' Import data file into a pandas dataframe. Calc fitted data with module interpolate.UnivariateSpline, plot resulting data and print mean squared error of fitted data. Plot is saved as 'fit_syield.png', resulting data as 'syield.pp'. syield.pp includes spline derivatives additional to original data. Keyword arguments: cmd_line_arg -- list of cmd line arguments: [1] name of dat file, if keyword 'test' is included different smoothing factors are tried and compared. ''' # name of data file dat_name = cmd_line_arg[1] testing = 0 for arg in cmd_line_arg: if arg=='test': testing=1 # read data from .dat file dat_table = pd.read_table(dat_name, sep='\s+', skiprows=1, names=['tilt', 'yield', 'error']) # set x and y values x = dat_table.loc[:,'tilt'] y = dat_table.loc[:,'yield'] # calc standard deviation from error # w = np.sqrt(1/dat_table.loc[:,'error']**2) w = dat_table.loc[:,'error'] w[w==0]=0.00001 w=1/w # std_dev = np.std(dat_table.loc[:,'error']) # w = 1/(std_dev-dat_table.loc[:,'error']) # plotting original data plt.plot(x, y, 'bo', ms=5, label='original') plt.title('Compared fitted and original data') plt.xlabel('tilt') plt.ylabel('yield') # plot fitted parameters if testing: s_index = np.arange(0,2,0.02) results = pd.DataFrame(index=s_index, columns=['knots', 'mse']) for s in s_index: spl = interpolate.UnivariateSpline(x, y, s=s, w=w) results.loc[s] = [len(spl.get_coeffs()), mse(y, spl(x))] plot_fitted_data(spl, x, y, s) # plt.legend(loc=2, borderaxespad=0.) plt.show() plot_results(results) else: s = 0.57 spl = interpolate.UnivariateSpline(x, y, s=s, w=w) plot_fitted_data(spl, x, y, s) # show legend plt.legend(loc='upper left') # save figure plt.savefig(os.path.join(os.path.dirname(__file__), '..', 'work', 'Aufgabe14_spline', 'fit_syield.png')) # copy dat table for pp table pp_table = dat_table.copy() # fill missing entries with NaN pp_table['derivative'] = np.NaN # iterate over all knots for i in spl.get_knots(): # calc derivatives pp_table.loc[pp_table['tilt']==i, 'derivative'] = spl.derivatives(i)[1] # drop rows with NaN (unnecessarily rows) pp_table = pp_table.dropna() # # delete error column pp_table = pp_table.drop('error', axis=1) # save data in pp file (into tables folder) pp_table.to_csv(os.path.join(os.path.dirname(__file__), '..', 'tables', 'syield.pp'), sep=' ', index=False)
def muscleThickness(start, end, calibV, calibH, spl1=None, spl2=None, points1=None, points2=None): """ Function that calculates muscle thickness in mm on the interval of columns [start, end] of a US muscle image I. Args: spl1 (scipy spline): spline modeling one aponeurosis in I. spl2 (scipy spline): spline modeling the other aponeurosis in I. points1, points2 (optional): are array of dimension (n,2). they are aponeuroses' points. Default value is None for each array (they are calculated from spl1 and spl2 in this case) start (int) : starting column to calculate muscle thickness. Its value should be >= 0 and <I.shape[1] end (int): ending column to calculate muscle thickness. Its value should be > start, > 0 and <I.shape[1] calibV (float) : vertical calibration factor calibH (float) : horizontal calibration factor Outputs: absc (list) : list of abscissa representing distances from the beginning of the image I mt (list) : muscle thickness in mm at the respecting abscissa spl (tuple): spline that interpolates the previous points (x,y) """ start = int(start) end = int(end) mt = [] absc = [] #generate aponeuroses' points coordinates if necessary if (spl1 is None and points1 is None) or (spl2 is None and points2 is None): raise ValueError( 'Missing value. Spline or array of points should be input for each aponeurosis.' ) if points1 is None: points1, spl1 = pointsCoordinates('spline', spl1, [start, end]) if points2 is None: points2, spl2 = pointsCoordinates('spline', spl2, [start, end]) for col in range(start, end + 1): #search if there exist points in each aponeurosis with abscissa col search1 = [pt for pt in points1 if pt[1] == col] search2 = [pt for pt in points2 if pt[1] == col] if search1 and search2: #if there exist a point whith abscissa col #in each list points1, points2, then calculates MT mt.append(abs(search1[0][0] - search2[0][0]) * calibV) absc.append(col * calibH) #interpolation of MT curve import scipy.interpolate as interpolate spl = interpolate.UnivariateSpline(absc, mt, k=5, ext=0) return absc, mt, spl
def pblVPT(pot, rvv, vpt, hi): # The Virtual Potential Temperature (VPT) method looks for the height at which VPT is equal to the VPT at surface level # NOTE: The VPT may equal VPT[0] in several places, so the function is coded to return the highest height where # these are equal # Supoort for this method can be found at the following link: # https://www.mdpi.com/2073-4433/6/9/1346/pdf global groot, grootVal vert_ln = [vpt[1]] * 2 #Defines the starting value of VPT as reference negXlim = vert_ln[1] - 15 #Creates variable for -15 than vertical line VPT posXlim = vert_ln[1] + 15 #Creates variable for +15 than vertical line VPT vptCutOff = vpt[1] g = interpolate.UnivariateSpline(hi, vpt - vptCutOff, s=0) #Smoothing function plt.plot(vpt, hi, color='red') #Plots VPT values against height plt.plot(g(hi) + vptCutOff, hi) plt.plot(vert_ln, plt.ylim()) #Plots a vertical line at the X-value of VPT[1] axes = plt.gca() axes.set_xlim([negXlim, posXlim ]) #Centers X-bounds about Vertical line, easy to view axes.set_ylim([0, 3000]) #Average range to see values plt.xlabel("VPT") plt.ylabel("Height above ground [m]") plt.title('VPT PBL Determination \n %.20s' % (file), fontsize=12) if saveData: plt.savefig('%s/VPT_PBL_%.20s.jpg' % (savePath, file)) plt.show() print('Vertical Line for VPT:') print(vert_ln) #Feeds back the vertical line created by VPT[1] rootdos = [] #The following section finds locations where VPT crosses the value of VPT[1] if len(g.roots()) == 0: rootdos = "Error in Calculating VPT Method" return rootdos else: groot = pd.DataFrame(g.roots()) groot.columns = ['Roots'] if len(groot) >= 1: grootVal = groot['Roots'].iloc[-1] grootVal = round(grootVal, 3) grootVal = int(grootVal) if grootVal < 100: rootdos = "VPT Method Inconclusive-No Value Output in " return rootdos if grootVal < 500 and grootVal >= 100: rootdos = ("VPT Method Inconclusive-Estimated Value: %.5s" % (grootVal)) return rootdos if grootVal >= 500 and grootVal <= 3000: rootdos = ("%.5s" % (grootVal)) rootdos = int(rootdos) return rootdos if grootVal > 3000: rootdos = ( "VPT Method Inconclusive-Estimated Value Beyond Nominal Range: %.5s" % (grootVal)) return rootdos return rootdos #Returns the highest ALT where VPT = VPT[1]
xy_selector = S.logical_and( S.logical_and(new_xs >= b_box_x[0], new_xs <= b_box_x[1]), S.logical_and(new_ys >= b_box_y[0], new_ys <= b_box_y[1])) selected_data_xs = new_xs[xy_selector] selected_data_ys = new_ys[xy_selector] selected_data = d[xy_selector, :] x_sort = selected_data_xs.argsort() selected_data_xs = selected_data_xs[x_sort] selected_data_ys = selected_data_ys[x_sort] selected_data = selected_data[x_sort] val_spline_y = selected_data[:, colnum] a_spline = INTERP.UnivariateSpline(selected_data_xs, val_spline_y, ext=3, s=3) spline_xs = S.linspace(0.0, 8.0, 100) ax2 = fig.add_subplot(2, 1, 2, sharex=ax) ax2.scatter(selected_data_xs, val_spline_y) ax2.plot(spline_xs, a_spline(spline_xs), c="r") ax2.set_xlabel("Natural A/P axis.") fig.tight_layout() plt.show()
def linear_schedule(t, y): """ Piecewise linear function y(t) """ return interpolate.UnivariateSpline(t, y, s=0, k=1, ext='const')
def volwmag(z): #if __name__ == "__main__": if (len(sys.argv)>1): lensmodels = sys.argv[1] else: # lensmodels='lensmodels.lis' lensmodels='vp_lensmodels.lis' if not (os.path.isfile(lensmodels)): print 'missing startup file' sys.exit() clist=asciitable.read(lensmodels, names=['alias','clusterdir','deflxfile','deflyfile','segfile','zcluster','zmodel']) # Observed F160W AB magnitude is 25.7. Its restframe B-band magnitude # is approximately -22.4, uncorrected for the magnification of ~15.5. # So the corrected absolute magnitude is -22.4+2.5*log(15.5) = -19.4. # a reasonable grid of magnifications #mu=np.linspace(0.2,120,100) mu=np.linspace(1,100,100) # representative magnifications ###magref=[5.4, 8.0, 13.5, 14.5, 18.7, 57.7] magref=[1, 2, 3, 4, 5, 5.4, 6, 6.6, 7, 7.5, 8.0, 9, 10, 11, 12, 13, 13.5, 14, 14.5, 15, 16, 17, 18, 18.7, 20, 25, 30, 35, 40, 45, 50, 57.7, 60] # set the anchor point of Mref <-> muref ### Mref, muref = -19.5, 14.5 Mref, muref = -19.5, 9.0 Mlim = Mref+2.5*np.log10(mu/muref) def getmu(Mag): return muref*10**(0.4*(Mag-Mref)) TotalVolarrayWithSeg = np.zeros(mu.shape) TotalVolarray = np.zeros(mu.shape) # target source redshift for rescaling model deflection maps # *** #ztarget = 9.5 #ztarget = 9.6 #ztarget = 5.5 ztarget = z # Plot stuff fig=plt.figure() MFig=fig.add_subplot(111) MFig.set_xlabel('Magnification [$\mu$]') MFig.set_ylabel('Effective Volume (>$\mu$) [Mpc$^3$]') MFig.set_xlim(0.2,100) MFig.set_ylim(1.0,2e5) MFig.set_xscale('log') MFig.set_yscale('log') # some annotations # *** MFig.text(30,2e4,'z=[9,10]',size=13) #MFig.text(30,2e4,'z=[5,6]',size=13) ytext = 1e5 # *** #MFig.text(0.22,ytext,'Unlensed M$_{B}$ AB limit:',size=10) ## including some absolute magnitudes that correspond to magnifications #plotmus=[-18.0,Mref,-21.0,-22.0] #for pp in plotmus: MFig.text(getmu(pp)/1.2,ytext,'%.1f' % pp,size=10) outdata = open('volumedata_a1689.dat','w') for ii in np.arange(clist.size): #for ii in [0]: alias = clist['alias'][ii] clusterdir = clist['clusterdir'][ii] deflxfile = clist['deflxfile'][ii] deflyfile = clist['deflyfile'][ii] segfile = clist['segfile'][ii] zcluster = clist['zcluster'][ii] zmodel = clist['zmodel'][ii] rescale = \ (cdad(ztarget, zcluster, **cosmo) / cdad(ztarget, **cosmo)) * \ (cdad(zmodel, **cosmo) / cdad(zmodel, zcluster, **cosmo)) # read in the deflections from Adi's lens models axlist = pyfits.open(clusterdir+'/'+deflxfile) aylist = pyfits.open(clusterdir+'/'+deflyfile) ax, ay = rescale*axlist[0].data, rescale*aylist[0].data # read in the segmentation map, which we are implicitly assuming has # already been wregistered to the model try: segfile = pyfits.open(clusterdir+'/'+segfile) segmap=segfile[0].data segflag = 1 except: segflag = 0 segmap=ax*0.0 # do some initializations etc Unity = np.zeros(ax.shape)+1.0 header = axlist[0].header try: cdelt1, cdelt2 = header['CDELT1'], header['CDELT2'] header['CDELT1'], header['CDELT2'] = cdelt1, cdelt2 pxsc = np.abs(cdelt1)*3600.0 # in arcseconds per linear pixel dimension except: cd1_1, cd2_2 = header['CD1_1'], header['CD2_2'] pxsc = np.abs(cd1_1)*3600.0 # in arcseconds per linear pixel dimension # Calculate the magnification from the Jacobian. Note that it first # calculates the gradient with respect to the 'y' axis, and then wrt # the 'x' axis. axy, axx = np.gradient(ax) ayy, ayx = np.gradient(ay) Jxx = Unity -axx Jxy = -axy Jyx = -ayx Jyy = Unity -ayy Mu = Unity / (Jxx*Jyy - Jxy*Jyx) AbsMu = np.abs(Mu) # define the total wfc3ir outline mask # regionfile = clusterdir+'/'+'wfc3ir_outline.reg' regionfile = clusterdir+'/'+'acs_outline.reg' rwcs=pyregion.open(regionfile) rcoo=pyregion.open(regionfile).as_imagecoord(header) maskboo=rwcs.get_mask(hdu=axlist[0]) # rmask=np.zeros(ax.shape) # rmask[(maskboo)]=1.0 # The diff_comoving_volume is the differential comoving volume per # unit redshift per unit solid angle, in units of Mpc**3 ster**-1. So # we convert that to the Volume per (unlensed) pixel, for a source # plane at ztarget. VolPix = (pxsc/206265.0)**2 * cd.diff_comoving_volume(ztarget, **cosmo) # PixMap will now be the Mpc**3 volume that each pixel corresponds to # in the z=9-10 source plane. PixMap = VolPix / AbsMu # Now let's zap the areas of the mosaic that are covered by objects # via the IR-detected segmentation map. # /Volumes/Archive/CLASH/archive.stsci.edu/pub/clash/outgoing/macs1149/HST/catalogs/mosaicdrizzle_image_pipeline/IR_detection/SExtractor # Now let us calculate the source-plane volume for each of the # magnification lower limits we established. VolarrayWithSeg = np.zeros(mu.shape) Volarray = np.zeros(mu.shape) for jj in np.arange(mu.size): # VolarrayWithSeg[jj] = np.sum( PixMap[((AbsMu>mu[jj]) & (rmask==1.0) & (segmap==0) )] ) # Volarray[jj] = np.sum( PixMap[((AbsMu>mu[jj]) & (rmask==1.0))] ) VolarrayWithSeg[jj] = np.sum( PixMap[((AbsMu>mu[jj]) & (maskboo) & (segmap==0) )] ) Volarray[jj] = np.sum( PixMap[((AbsMu>mu[jj]) & (maskboo))] ) TotalVolarrayWithSeg += VolarrayWithSeg TotalVolarray += Volarray s=si.UnivariateSpline(np.log10(mu),np.log10(VolarrayWithSeg),s=5) print alias,clusterdir,10**s(np.log10(muref)) outstring = '%s %s %.1f ' % (alias,clusterdir,10**s(np.log10(muref))) outdata.write(outstring+'\n') # need to write the mu, Volarray data to a file.... MFig.plot(mu,Volarray,label=alias) MFig.plot(mu,VolarrayWithSeg,'--') MFig.legend(loc=3,prop={'size':8}) # no error checking here yet... sws = si.UnivariateSpline(np.log10(mu),np.log10(TotalVolarrayWithSeg),s=5) s = si.UnivariateSpline(np.log10(mu),np.log10(TotalVolarray),s=5) volwithseg = (np.asarray(10**sws(np.log10(magref)))).reshape(len(magref), 1) volnoseg = (np.asarray(10**s(np.log10(magref)))).reshape(len(magref),1) magref = (np.asarray(magref)).reshape(len(magref),1) MVolArray = np.hstack((magref, volnoseg, volwithseg)) return MVolArray
def process_grid(rz_grid, mesh, output=None, poorquality=None, gui=None, parent=None, reverse_bt=None, curv=None, smoothpressure=None, smoothhthe=None, smoothcurv=None, settings=None): if settings == None: # Create an empty structure settings = Bunch(dummy=0) # Check settings settings.calcp = -1 settings.calcbt = -1 settings.calchthe = -1 settings.calcjpar = -1 # ;CATCH, err # ;IF err NE 0 THEN BEGIN # ; PRINT, "PROCESS_GRID failed" #; PRINT, " Error message: "+!ERROR_STATE.MSG # ; CATCH, /cancel # ; RETURN # ;ENDIF MU = 4.e-7 * numpy.pi poorquality = 0 if output == None: output = "bout.grd.nc" # Size of the mesh nx = numpy.int(numpy.sum(mesh.nrad)) ny = numpy.int(numpy.sum(mesh.npol)) # Find the midplane ymid = 0 status = gen_surface(mesh=mesh) # Start generator while True: period, yi, xi, last = gen_surface(period=None, last=None, xi=None) if period: rm = numpy.max(mesh.Rxy[xi, yi]) ymidindx = numpy.argmax(mesh.Rxy[xi, yi]) ymid = yi[ymidindx] break if last == 1: break Rxy = numpy.asarray(mesh.Rxy) Zxy = numpy.asarray(mesh.Zxy) psixy = mesh.psixy * mesh.fnorm + mesh.faxis # Non-normalised psi pressure = numpy.zeros((nx, ny)) # Use splines to interpolate pressure profile status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=period, last=last, xi=xi) if period: # Pressure only given on core surfaces # pressure[xi,yi] = SPLINE(rz_grid.npsigrid, rz_grid.pres, mesh.psixy[xi,yi[0]], /double) sol = interpolate.UnivariateSpline(rz_grid.npsigrid, rz_grid.pres, s=1) pressure[xi, yi] = sol(mesh.psixy[xi, yi[0]]) else: pressure[xi, yi] = rz_grid.pres[numpy.size(rz_grid.pres) - 1] if last == 1: break # Add a minimum amount if numpy.min(pressure) < 1.0e-2 * numpy.max(pressure): print("****Minimum pressure is very small:", numpy.min(pressure)) print("****Setting minimum pressure to 1% of maximum") pressure = pressure + 1e-2 * numpy.max(pressure) if smoothpressure != None: p0 = pressure[:, ymid] # Keep initial pressure for comparison while True: #!P.multi=[0,0,2,0,0] fig = figure() plot(p0, xtitle="X index", ytitle="pressure at y=" + numpy.strip(numpy.str(ymid), 2) + " dashed=original", color=1, lines=1) plot(pressure[:, ymid], color=1) plot(deriv(p0), xtitle="X index", ytitle="DERIV(pressure)", color=1, lines=1) plot(deriv(pressure[:, ymid]), color=1) sm = query_yes_no( "Smooth pressure profile?") #, gui=gui, dialog_parent=parent) if sm: # Smooth the pressure profile p2 = pressure for i in range(6): status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=period, last=last, xi=xi) if (xi > 0) and (xi < (nx - 1)): for j in range(numpy.size(yi)): p2[xi, yi[j]] = (0.5 * pressure[xi, yi[j]] + 0.25 * (pressure[xi - 1, yi[j]] + pressure[xi + 1, yi[j]])) # Make sure it's still constant on flux surfaces p2[xi, yi] = numpy.mean(p2[xi, yi]) if last != None: break pressure = p2 if sm == 0: break if numpy.min(pressure) < 0.0: print("") print("============= WARNING ==============") print("Poor quality equilibrium: Pressure is negative") print("") poorquality = 1 dpdpsi = DDX(psixy, pressure) #;IF MAX(dpdpsi)*mesh.fnorm GT 0.0 THEN BEGIN #; PRINT, "" #; PRINT, "============= WARNING ==============" #; PRINT, "Poor quality equilibrium: Pressure is increasing radially" #; PRINT, "" #; poorquality = 1 #;ENDIF # Grid spacing dx = numpy.zeros((nx, ny)) for y in range(ny): dx[0:(nx - 1), y] = psixy[1::, y] - psixy[0:(nx - 1), y] dx[nx - 1, y] = dx[nx - 2, y] # Sign bpsign = 1. xcoord = psixy if numpy.min(dx) < 0.: bpsign = -1. dx = -dx # dx always positive xcoord = -xcoord dtheta = 2. * numpy.pi / numpy.float(ny) dy = numpy.zeros((nx, ny)) + dtheta # B field components # Following signs mean that psi increasing outwards from # core to edge results in Bp clockwise in the poloidal plane # i.e. in the positive Grad Theta direction. Brxy = old_div(mesh.dpsidZ, Rxy) Bzxy = old_div(-mesh.dpsidR, Rxy) Bpxy = numpy.sqrt(Brxy**2 + Bzxy**2) # Determine direction (dot B with grad y vector) dot = (Brxy[0, ymid] * (Rxy[0, ymid + 1] - Rxy[0, ymid - 1]) + Bzxy[0, ymid] * (Zxy[0, ymid + 1] - Zxy[0, ymid - 1])) if dot < 0.: print( "**** Poloidal field is in opposite direction to Grad Theta -> Bp negative" ) Bpxy = -Bpxy if bpsign > 0: sys.exit() # Should be negative bpsign = -1.0 else: if bpsign < 0: sys.exit() # Should be positive bpsign = 1. # Get toroidal field from poloidal current function fpol Btxy = numpy.zeros((nx, ny)) fprime = numpy.zeros((nx, ny)) fp = deriv(rz_grid.npsigrid * (rz_grid.sibdry - rz_grid.simagx), rz_grid.fpol) status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=period, last=period, xi=xi) if period: # In the core #fpol = numpy.interp(rz_grid.fpol, rz_grid.npsigrid, mesh.psixy[xi,yi], /spline) sol = interpolate.UnivariateSpline(rz_grid.npsigrid, rz_grid.fpol, s=1) # fpol = SPLINE(rz_grid.npsigrid, rz_grid.fpol, mesh.psixy[xi,yi[0]], 'double') fpol = sol(mesh.psixy[xi, yi[0]]) sol = interpolate.UnivariateSpline(rz_grid.npsigrid, fp, s=1) # fprime[xi,yi] = SPLINE(rz_grid.npsigrid, fp, mesh.psixy[xi,yi[0]], 'double') fprime[xi, yi] = sol(mesh.psixy[xi, yi[0]]) else: # Outside core. Could be PF or SOL fpol = rz_grid.fpol[numpy.size(rz_grid.fpol) - 1] fprime[xi, yi] = 0. Btxy[xi, yi] = old_div(fpol, Rxy[xi, yi]) if last == 1: break # Total B field Bxy = numpy.sqrt(Btxy**2 + Bpxy**2) #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; # Go through the domains to get a starting estimate # of hthe hthe = numpy.zeros((nx, ny)) # Pick a midplane index status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=period, last=last, xi=xi) if period: # In the core rmax = numpy.argmax(Rxy[xi, yi]) ymidplane = yi[rmax] break if last == 1: break status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=period, last=last, xi=xi) n = numpy.size(yi) # Get distance along this line if period: # Periodic, so can use FFT #drdi = REAL_PART(fft_deriv(Rxy[xi, yi])) #dzdi = REAL_PART(fft_deriv(Zxy[xi, yi])) line = numpy.append(Rxy[xi, yi[n - 1::]], Rxy[xi, yi]) line = numpy.append(line, Rxy[xi, yi[0:1]]) drdi = deriv(line)[1:n + 1] line = numpy.append(Zxy[xi, yi[n - 1::]], Zxy[xi, yi]) line = numpy.append(line, Zxy[xi, yi[0:1]]) dzdi = deriv(line)[1:n + 1] else: # Non-periodic drdi = numpy.gradient(Rxy[xi, yi]) dzdi = numpy.gradient(Zxy[xi, yi]) dldi = numpy.sqrt(drdi**2 + dzdi**2) if 0: # Need to smooth to get sensible results if period: n = numpy.size(dldi) line = numpy.append(dldi[(n - 2)::], dldi) # once line = numpy.append(line, dldi[0:2]) dldi = SMOOTH(line, 5)[4:(n + 4)] line = numpy.append(dldi[(n - 2)::], dldi) #twice line = numpy.append(line, dldi[0:2]) dldi = SMOOTH(line, 5)[4:(n + 4)] line = numpy.append(dldi[(n - 2)::], dldi) # three line = numpy.append(line, dldi[0:2]) dldi = SMOOTH(line, 5)[4:(n + 4)] else: line = dldi dldi = SMOOTH(line, 5)[2:n + 2] line = dldi dldi = SMOOTH(line, 5)[2:n + 2] line = dldi dldi = SMOOTH(dldi, 5)[2:n + 2] hthe[xi, yi] = old_div(dldi, dtheta) # First estimate of hthe # Get outboard midplane if period and xi == 0: m = numpy.argmax(Rxy[0, yi]) ymidplane = yi[m] if last == 1: break print("Midplane index ", ymidplane) fb0 = force_balance(psixy, Rxy, Bpxy, Btxy, hthe, pressure) print("Force imbalance: ", numpy.mean(numpy.abs(fb0)), numpy.max(numpy.abs(fb0))) #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; # Correct pressure using hthe print("Calculating pressure profile from force balance") try: # Calculate force balance dpdx = old_div((-Bpxy * DDX(xcoord, Bpxy * hthe) - Btxy * hthe * DDX(xcoord, Btxy) - (Btxy * Btxy * hthe / Rxy) * DDX(xcoord, Rxy)), (MU * hthe)) # Surface average dpdx2 = surface_average(dpdx, mesh) pres = numpy.zeros((nx, ny)) # Integrate to get pressure for i in range(ny): pres[:, i] = int_func(psixy[:, i], dpdx2[:, i]) pres[:, i] = pres[:, i] - pres[nx - 1, i] status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=None, last=None, xi=None) ma = numpy.max(pres[xi, yi]) for i in range(numpy.size(yi)): pres[:, yi[i]] = pres[:, yi[i]] - pres[xi, yi[i]] + ma if last == 1: break pres = pres - numpy.min(pres) # Some sort of smoothing here? fb0 = force_balance(psixy, Rxy, Bpxy, Btxy, hthe, pres) print("Force imbalance: ", numpy.mean(numpy.abs(fb0)), numpy.max(numpy.abs(fb0))) #!P.MULTI=[0,0,2,0,0] fig = figure(figsize=(7, 11)) subplots_adjust(left=.07, bottom=.07, right=0.95, top=0.95, wspace=.3, hspace=.25) SURFACE(pressure, fig, xtitle="X", ytitle="Y", var='Pa', sub=[2, 1, 1]) title("Input pressure") SURFACE(pres, fig, xtitle="X", ytitle="Y", var='Pa', sub=[2, 1, 2]) title("New pressure") # arrange the plot on the screen # mngr = get_current_fig_manager() # geom = mngr.window.geometry() # x,y,dx,dy = geom.getRect() # mngr.window.setGeometry(0, 0, dx, dy) # show(block=False) calcp = settings.calcp if calcp == -1: calcp = query_yes_no( "Keep new pressure?") #, gui=gui, dialog_parent=parent) else: time.sleep(2) if calcp == 1: pressure = pres dpdpsi = dpdx2 except Exception: print("WARNING: Pressure profile calculation failed: " ) #, !ERROR_STATE.MSG pass #CATCH, /cancel #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; # Correct f = RBt using force balance calcbt = settings.calcbt if calcbt == -1: calcbt = query_yes_no("Correct f=RBt using force balance?" ) #, gui=gui, dialog_parent=parent) if calcbt == 1: new_Btxy = newton_Bt(psixy, Rxy, Btxy, Bpxy, pres, hthe, mesh) fb0 = force_balance(psixy, Rxy, Bpxy, new_Btxy, hthe, pressure) print("force imbalance: ", numpy.mean(numpy.abs(fb0)), numpy.max(numpy.abs(fb0))) fig = figure(figsize=(7, 11)) subplots_adjust(left=.07, bottom=.07, right=0.95, top=0.95, wspace=.3, hspace=.25) subplot(211) SURFACE(Btxy, fig, xtitle="X", ytitle="Y", var='T', sub=[2, 1, 1]) title("Input Bt") subplot(212) SURFACE(new_Btxy, fig, xtitle="X", ytitle="Y", var='T', sub=[2, 1, 2]) title("New Bt") # arrange the plot on the screen #mngr = get_current_fig_manager() #geom = mngr.window.geometry() #x,y,dx,dy = geom.getRect() #mngr.window.setGeometry(600, 0, dx, dy) show(block=False) calcbt = settings.calcbt if calcbt == -1: calcbt = query_yes_no( "Keep new Bt?") #, gui=gui, dialog_parent=parent) if calcbt == 1: Btxy = new_Btxy Bxy = numpy.sqrt(Btxy**2 + Bpxy**2) #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; # CALCULATE HTHE # Modify hthe to fit force balance using initial guess # Does not depend on signs #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; calchthe = settings.calchthe if calchthe == -1: calchthe = query_yes_no("Adjust hthe using force balance?" ) #, gui=gui, dialog_parent=parent) if calchthe == 1: # This doesn't behave well close to the x-points fixhthe = numpy.int(old_div(nx, 2)) nh = correct_hthe(Rxy, psixy, Btxy, Bpxy, hthe, pressure, fixhthe=fixhthe) fb0 = force_balance(psixy, Rxy, Bpxy, Btxy, nh, pressure) print("Force imbalance: ", numpy.mean(numpy.abs(fb0)), numpy.max(numpy.abs(fb0))) print("numpy.maximum difference in hthe: ", numpy.max(numpy.abs(hthe - nh))) print("numpy.maximum percentage difference: ", 100. * numpy.max(numpy.abs(old_div((hthe - nh), hthe)))) #!P.multi=[0,0,1,0,0] fig = figure(figsize=(7, 4)) title("Poloidal arc length at midplane. line is initial estimate") plot(hthe[:, 0], '-') plot(nh[:, 0], 'r-+') # arrange the plot on the screen #mngr = get_current_fig_manager() #geom = mngr.window.geometry() #x,y,dx,dy = geom.getRect() #mngr.window.setGeometry(0, 1150, dx, dy) show(block=False) if query_yes_no( "Keep new hthe?") == 1: #, gui=gui, dialog_parent=parent) : hthe = nh if smoothhthe != None: # Smooth hthe to prevent large jumps in X or Y. This # should be done by creating a better mesh in the first place # Need to smooth in Y and X otherwise smoothing in X # produces discontinuities in Y hold = hthe if 1: # Nonlinear smoothing. Tries to smooth only regions with large # changes in gradient hthe = 0. # smooth_nl(hthe, mesh) else: # Just use smooth in both directions for i in range(ny): hthe[:, i] = SMOOTH(SMOOTH(hthe[:, i], 10), 10) status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=None, last=None, xi=None) n = numpy.size(yi) if period: hthe[xi, yi] = (SMOOTH([ hthe[xi, yi[(n - 4):(n - 1)]], hthe[xi, yi], hthe[xi, yi[0:3]] ], 4))[4:(n + 3)] else: hthe[xi, yi] = SMOOTH(hthe[xi, yi], 4) if last == 1: break # Calculate field-line pitch pitch = hthe * Btxy / (Bpxy * Rxy) # derivative with psi dqdpsi = DDX(psixy, pitch) qinty, qloop = int_y(pitch, mesh, loop=0, nosmooth='nosmooth', simple='simple') qinty = qinty * dtheta qloop = qloop * dtheta sinty = int_y(dqdpsi, mesh, nosmooth='nosmooth', simple='simple') * dtheta # NOTE: This is only valid in the core pol_angle = numpy.zeros((nx, ny)) for i in range(nx): pol_angle[i, :] = 2.0 * numpy.pi * qinty[i, :] / qloop[i] #;;;;;;;;;;;;;;;;;;; THETA_ZERO ;;;;;;;;;;;;;;;;;;;;;; # re-set zshift to be zero at the outboard midplane print("MIDPLANE INDEX = ", ymidplane) status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=None, last=None, xi=None) w = numpy.size(numpy.where(yi == ymidplane)) if w > 0: # Crosses the midplane qinty[xi, yi] = qinty[xi, yi] - qinty[xi, ymidplane] sinty[xi, yi] = sinty[xi, yi] - sinty[xi, ymidplane] else: # Doesn't include a point at the midplane qinty[xi, yi] = qinty[xi, yi] - qinty[xi, yi[0]] sinty[xi, yi] = sinty[xi, yi] - sinty[xi, yi[0]] if last == 1: break print("") print("==== Calculating curvature ====") #;;;;;;;;;;;;;;;;;;; CURVATURE ;;;;;;;;;;;;;;;;;;;;;;; # Calculating b x kappa if curv == None: print("*** Calculating curvature in toroidal coordinates") thetaxy = numpy.zeros((nx, ny)) status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=None, last=None, xi=None) thetaxy[xi, yi] = numpy.arange(numpy.size(yi)).astype(float) * dtheta if last == 1: break bxcv = curvature(nx, ny, Rxy, Zxy, Brxy, Bzxy, Btxy, psixy, thetaxy, hthe, mesh=mesh) bxcvx = bpsign * bxcv.psi bxcvy = bxcv.theta bxcvz = bpsign * (bxcv.phi - sinty * bxcv.psi - pitch * bxcv.theta) # x borders bxcvx[0, :] = bxcvx[1, :] bxcvx[nx - 1, :] = bxcvx[nx - 2, :] bxcvy[0, :] = bxcvy[1, :] bxcvy[nx - 1, :] = bxcvy[nx - 2, :] bxcvz[0, :] = bxcvz[1, :] bxcvz[nx - 1, :] = bxcvz[nx - 2, :] elif curv == 1: # Calculate on R-Z mesh and then interpolate onto grid # ( cylindrical coordinates) print("*** Calculating curvature in cylindrical coordinates") bxcv = rz_curvature(rz_grid) # DCT methods cause spurious oscillations # Linear interpolation seems to be more robust bxcv_psi = numpy.interp(bxcv.psi, mesh.Rixy, mesh.Zixy) bxcv_theta = old_div(numpy.interp(bxcv.theta, mesh.Rixy, mesh.Zixy), hthe) bxcv_phi = numpy.interp(bxcv.phi, mesh.Rixy, mesh.Zixy) # If Bp is reversed, then Grad x = - Grad psi bxcvx = bpsign * bxcv_psi bxcvy = bxcv_theta bxcvz = bpsign * (bxcv_phi - sinty * bxcv_psi - pitch * bxcv_theta) elif curv == 2: # Curvature from Curl(b/B) bxcvx = bpsign * (Bpxy * Btxy * Rxy * DDY(old_div(1., Bxy), mesh) / hthe) bxcvy = -bpsign * Bxy * Bpxy * DDX(xcoord, Btxy * Rxy / Bxy ^ 2) / (2. * hthe) bxcvz = Bpxy ^ 3 * DDX(xcoord, old_div( hthe, Bpxy)) / (2. * hthe * Bxy) - Btxy * Rxy * DDX( xcoord, old_div(Btxy, Rxy)) / (2. * Bxy) - sinty * bxcvx else: # calculate in flux coordinates. print("*** Calculating curvature in flux coordinates") dpb = numpy.zeros((nx, ny)) # quantity used for y and z components for i in range(ny): dpb[:, i] = MU * dpdpsi / Bxy[:, i] dpb = dpb + DDX(xcoord, Bxy) bxcvx = bpsign * (Bpxy * Btxy * Rxy * DDY(old_div(1., Bxy), mesh) / hthe) bxcvy = bpsign * (Bpxy * Btxy * Rxy * dpb / (hthe * Bxy ^ 2)) bxcvz = -dpb - sinty * bxcvx if smoothcurv: # Smooth curvature to prevent large jumps # Nonlinear smoothing. Tries to smooth only regions with large # changes in gradient bz = bxcvz + sinty * bxcvx print("Smoothing bxcvx...") bxcvx = 0. #smooth_nl(bxcvx, mesh) print("Smoothing bxcvy...") bxcvy = 0. #smooth_nl(bxcvy, mesh) print("Smoothing bxcvz...") bz = 0. #smooth_nl(bz, mesh) bxcvz = bz - sinty * bxcvx #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; # CALCULATE PARALLEL CURRENT # # Three ways to calculate Jpar0: # 1. From fprime and pprime # 2. From Curl(B) in field-aligned coords # 3. From the curvature # # Provides a way to check if Btor should be reversed # #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; print("") print("==== Calculating parallel current ====") jpar0 = -Bxy * fprime / MU - Rxy * Btxy * dpdpsi / Bxy # Set to zero in PF and SOL status = gen_surface(mesh=mesh) # Start generator while True: # Get the next domain period, yi, xi, last = gen_surface(period=None, last=None, xi=None) if period == None: jpar0[xi, yi] = 0.0 if last == 1: break # Curl(B) expression for Jpar0 (very noisy usually) j0 = (bpsign * ((Bpxy * Btxy * Rxy / (Bxy * hthe)) * (DDX(xcoord, Bxy**2 * hthe / Bpxy) - bpsign * Btxy * Rxy * DDX(xcoord, Btxy * hthe / (Rxy * Bpxy))) - Bxy * DDX(xcoord, Btxy * Rxy)) / MU) # Create a temporary mesh structure to send to adjust_jpar tmp_mesh = Bunch(mesh, bxcvx=bxcvx, bxcvy=bxcvy, bxcvz=bxcvz, Bpxy=Bpxy, Btxy=Btxy, Bxy=Bxy, dx=dx, dy=dy, hthe=hthe, jpar0=jpar0, pressure=pressure) tmp_mesh.psixy = psixy jpar = adjust_jpar(tmp_mesh, noplot='noplot') #!P.multi=[0,2,2,0,0] fig = figure(figsize=(15, 11)) subplots_adjust(left=.07, bottom=.07, right=0.95, top=0.95, wspace=.3, hspace=.25) subplot(221) SURFACE(jpar0, fig, xtitle="X", ytitle="Y", var='A', sub=[2, 2, 1]) title("Jpar from F' and P'") subplot(222) SURFACE(jpar, fig, xtitle="X", ytitle="Y", var='A', sub=[2, 2, 2]) title("Jpar from curvature") subplot(223) plot(jpar0[0, :], '-', jpar[0, :], '+') ylim([ numpy.min([jpar0[0, :], jpar[0, :]]), numpy.max([jpar0[0, :], jpar[0, :]]) ]) title("jpar at x=0. Solid from f' and p'") subplot(224) plot(jpar0[:, ymidplane], '-', jpar[:, ymidplane], '+') ylim([ numpy.min([jpar0[:, ymidplane], jpar[:, ymidplane]]), numpy.max([jpar0[:, ymidplane], jpar[:, ymidplane]]) ]) title("Jpar at y=" + numpy.str(ymidplane) + " Solid from f' and p'") # arrange the plot on the screen #mngr = get_current_fig_manager() #geom = mngr.window.geometry() #x,y,dx,dy = geom.getRect() #mngr.window.setGeometry(1350, 0, dx, dy) show(block=False) # !P.multi=0 calcjpar = settings.calcjpar if calcjpar == -1: calcjpar = query_yes_no( "Use Jpar from curvature?") #, gui=gui, dialog_parent=parent) if calcjpar == True: jpar0 = jpar if 0: # Try smoothing jpar0 in psi, preserving zero points and maxima jps = jpar0 for y in range(ny): j = jpar0[:, y] js = j ma = numpy.max(numpy.abs(j)) ip = numpy.argmax(numpy.abs(j)) if (ma < 1.e-4) or (ip == 0): jps[:, y] = j level = 1. #i0 = MAX(WHERE(ABS(j[0:ip]) LT level)) i1 = numpy.min(numpy.where(numpy.abs(j[ip::]) < level)) #IF i0 LE 0 THEN i0 = 1 i0 = 1 if i1 == -1: i1 = nx - 2 else: i1 = i1 + ip if (ip <= i0) or (ip >= i1): # Now preserve starting and end points, and peak value div = numpy.int(old_div( (i1 - i0), 10)) + 1 # reduce number of points by this factor inds = [i0] # first point for i in [i0 + div, ip - div, div]: inds = [inds, i] inds = [inds, ip] # Put in the peak point # Calculate spline interpolation of inner part js[0:ip] = spline_mono(inds, j[inds], numpy.arange(ip + 1), yp0=(j[i0] - j[i0 - 1]), ypn_1=0.0) inds = [ip] # peak point for i in [ip + div, i1 - div, div]: inds = [inds, i] inds = [inds, i1] # Last point js[ip:i1] = spline_mono(inds, j[inds], ip + numpy.arange(i1 - ip + 1), yp0=0.0, ypn_1=(j[i1 + 1] - j[i1])) jps[:, y] = js #;;;;;;;;;;;;;;;;;;; TOPOLOGY ;;;;;;;;;;;;;;;;;;;;;;; # Calculate indices for backwards-compatibility nr = numpy.size(mesh.nrad) np = numpy.size(mesh.npol) if (nr == 2) and (np == 3): print("Single null equilibrium") ixseps1 = mesh.nrad[0] ixseps2 = nx jyseps1_1 = mesh.npol[0] - 1 jyseps1_2 = mesh.npol[0] + numpy.int(old_div(mesh.npol[1], 2)) ny_inner = jyseps1_2 jyseps2_1 = jyseps1_2 jyseps2_2 = ny - mesh.npol[2] - 1 elif (nr == 3) and (np == 6): print("Double null equilibrium") ixseps1 = mesh.nrad[0] ixseps2 = ixseps1 + mesh.nrad[1] jyseps1_1 = mesh.npol[0] - 1 jyseps2_1 = jyseps1_1 + mesh.npol[1] ny_inner = jyseps2_1 + mesh.npol[2] + 1 jyseps1_2 = ny_inner + mesh.npol[3] - 1 jyseps2_2 = jyseps1_2 + mesh.npol[4] elif (nr == 1) and (np == 1): print("Single domain") ixseps1 = nx ixseps2 = nx jyseps1_1 = -1 jyseps1_2 = numpy.int(old_div(ny, 2)) jyseps2_1 = numpy.int(old_div(ny, 2)) ny_inner = numpy.int(old_div(ny, 2)) jyseps2_2 = ny - 1 else: print("***************************************") print("* WARNING: Equilibrium not recognised *") print("* *") print("* Check mesh carefully! *") print("* *") print("* Contact Ben Dudson *") print("* [email protected] *") print("***************************************") ixseps1 = -1 ixseps2 = -1 jyseps1_1 = -1 jyseps1_2 = numpy.int(old_div(ny, 2)) jyseps2_1 = numpy.int(old_div(ny, 2)) ny_inner = numpy.int(old_div(ny, 2)) jyseps2_2 = ny - 1 print("Generating plasma profiles:") print(" 1. Flat temperature profile") print(" 2. Flat density profile") print(" 3. Te proportional to density") while True: opt = input("Profile option:") if eval(opt) >= 1 and eval(opt) <= 3: break if eval(opt) == 1: # flat temperature profile print("Setting flat temperature profile") while True: Te_x = eval(input("Temperature (eV):")) # get density Ni = old_div(pressure, (2. * Te_x * 1.602e-19 * 1.0e20)) print("numpy.maximum density (10^20 m^-3):", numpy.max(Ni)) done = query_yes_no("Is this ok?") if done == 1: break Te = numpy.zeros((nx, ny)) + Te_x Ti = Te Ni_x = numpy.max(Ni) Ti_x = Te_x elif eval(opt) == 2: print("Setting flat density profile") while True: Ni_x = eval(input("Density [10^20 m^-3]:")) # get temperature Te = old_div(pressure, (2. * Ni_x * 1.602e-19 * 1.0e20)) print("numpy.maximum temperature (eV):", numpy.max(Te)) if query_yes_no("Is this ok?") == 1: break Ti = Te Ni = numpy.zeros((nx, ny)) + Ni_x Te_x = numpy.max(Te) Ti_x = Te_x else: print("Setting te proportional to density") while True: Te_x = eval(input("Maximum temperature [eV]:")) Ni_x = old_div(numpy.max(pressure), (2. * Te_x * 1.602e-19 * 1.0e20)) print("Maximum density [10^20 m^-3]:", Ni_x) Te = Te_x * pressure / numpy.max(pressure) Ni = Ni_x * pressure / numpy.max(pressure) if query_yes_no("Is this ok?") == 1: break Ti = Te Ti_x = Te_x rmag = numpy.max(numpy.abs(Rxy)) print("Setting rmag = ", rmag) bmag = numpy.max(numpy.abs(Bxy)) print("Setting bmag = ", bmag) #;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; # save to file # open a new netCDF file for writing. handle = file_open(output) print("Writing grid to file " + output) # Size of the grid s = file_write(handle, "nx", nx) s = file_write(handle, "ny", ny) # Topology for original scheme s = file_write(handle, "ixseps1", ixseps1) s = file_write(handle, "ixseps2", ixseps2) s = file_write(handle, "jyseps1_1", jyseps1_1) s = file_write(handle, "jyseps1_2", jyseps1_2) s = file_write(handle, "jyseps2_1", jyseps2_1) s = file_write(handle, "jyseps2_2", jyseps2_2) s = file_write(handle, "ny_inner", ny_inner) # Grid spacing s = file_write(handle, "dx", dx) s = file_write(handle, "dy", dy) s = file_write(handle, "ShiftAngle", qloop) s = file_write(handle, "zShift", qinty) s = file_write(handle, "pol_angle", pol_angle) s = file_write(handle, "ShiftTorsion", dqdpsi) s = file_write(handle, "Rxy", Rxy) s = file_write(handle, "Zxy", Zxy) s = file_write(handle, "Bpxy", Bpxy) s = file_write(handle, "Btxy", Btxy) s = file_write(handle, "Bxy", Bxy) s = file_write(handle, "hthe", hthe) s = file_write(handle, "sinty", sinty) s = file_write(handle, "psixy", psixy) # Topology for general configurations s = file_write(handle, "yup_xsplit", mesh.yup_xsplit) s = file_write(handle, "ydown_xsplit", mesh.ydown_xsplit) s = file_write(handle, "yup_xin", mesh.yup_xin) s = file_write(handle, "yup_xout", mesh.yup_xout) s = file_write(handle, "ydown_xin", mesh.ydown_xin) s = file_write(handle, "ydown_xout", mesh.ydown_xout) s = file_write(handle, "nrad", mesh.nrad) s = file_write(handle, "npol", mesh.npol) # plasma profiles s = file_write(handle, "pressure", pressure) s = file_write(handle, "Jpar0", jpar0) s = file_write(handle, "Ni0", Ni) s = file_write(handle, "Te0", Te) s = file_write(handle, "Ti0", Ti) s = file_write(handle, "Ni_x", Ni_x) s = file_write(handle, "Te_x", Te_x) s = file_write(handle, "Ti_x", Ti_x) s = file_write(handle, "bmag", bmag) s = file_write(handle, "rmag", rmag) # Curvature s = file_write(handle, "bxcvx", bxcvx) s = file_write(handle, "bxcvy", bxcvy) s = file_write(handle, "bxcvz", bxcvz) # Psi range s = file_write(handle, "psi_axis", mesh.faxis) psi_bndry = mesh.faxis + mesh.fnorm s = file_write(handle, "psi_bndry", psi_bndry) file_close, handle print("DONE")
def histogram_peaks(data, bins=100, smoothing=.1, weights=None, plot=False, use_spline=True): ''' A function to bin data, fit a spline to the histogram, and return the peaks of that spline. Parameters ----------- data: (n,) data bins: int, number of bins in histogram smoothing: float, fraction to smooth spline (out of 1.0) weights: (n,) float, weight for each data point plot: bool, if True plot the histogram and spline use_spline: bool, if True fit a spline to the histogram Returns ----------- peaks: (m,) float, ordered list of peaks (largest are at the end). ''' data = np.asanyarray(data).reshape(-1) # (2,) float, start and end of histogram bins # round to two signifigant figures edges = [ trimesh.util.round_sigfig(i, 2) for i in np.percentile(data, [.1, 99.9]) ] h, b = np.histogram(data, weights=weights, bins=np.linspace(*edges, num=bins), range=edges, density=False) # set x to center of histogram bins x = b[:-1] + (b[1] - b[0]) / 2.0 if not use_spline: return x[h.argsort()] norm = weights.sum() / bins normalized = h / norm from scipy import interpolate # create an order 4 spline representing the radii histogram # note that scipy only supports root finding of order 3 splines # and we want to find peaks using the derivate, so start with order 4 spline = interpolate.UnivariateSpline(x, normalized, k=4, s=smoothing) roots = spline.derivative().roots() roots_value = spline(roots) peaks = roots[roots_value.argsort()] if plot: import matplotlib.pyplot as plt x_plt = np.linspace(x[1], x[-2], 500) y_plt = spline(x_plt) plt.hist(data, weights=weights / norm, bins=b) plt.plot(x_plt, y_plt) y_max = y_plt.max() * 1.2 for peak in peaks[-5:]: plt.plot([peak, peak], [0, y_max]) plt.show() return peaks
def cross_interpolate(cross): return interpolate.UnivariateSpline(cross.theta, cross.sigma, s=0)
psi0 = 0.9854333 pnts = np.genfromtxt('pnts_DIIID_psi09854.txt') q_gene = 4.4762795 R = pnts[:, 0] Z = pnts[:, 1] psirz_spl0, psiax, psisep, zmag, F, psip_n, ffprime, pprime, R0 = \ psirzSpl(efit_file_name) theta_fs = theta_grid_old(R, Z, q_gene, psirz_spl0, F, psip_n, psi0) theta_fs = theta_fs - np.pi ntheta = 401 zmag, R_fs00, Z_fs00, dPsidZ0, dPsidR0, Bp_fs00, Bt_fs00, q_fs00, jtor0, psip_n0 = originalSurf( efit_file_name, psi0, ntheta) jtor_spl = interpolate.UnivariateSpline(psip_n0, jtor0) gradPsi20 = dPsidZ0**2 + dPsidR0**2 qtheta_fs00, q_new0, R_new0, Z_new0, Bp_new0, Bt_new0 = theta_grid( zmag, R_fs00, Z_fs00, Bp_fs00, Bt_fs00, np.sqrt(gradPsi20), psi0) if 1 == 1: plt.plot(theta_fs / np.pi, Z, '.', label='gene') plt.plot(qtheta_fs00 / q_new0 / np.pi, Z_new0, '.', label='x') plt.xlabel('theta/pi') plt.ylabel('Z (m)') plt.legend() plt.show() if 1 == 1: plt.plot(R, Z, '.', label='gene') plt.plot(R_new0, Z_new0, '.', label='x') plt.axis('equal')
def get_response(min_w, max_w, response_function): root = rootdir + "/response_functions/" # Standard response functions: if response_function.lower() == 'kphires': response_file = root + "standard/kepler_response_hires1.txt" elif response_function.lower() == 'kplowres': response_file = root + "standard/kepler_response_lowres1.txt" elif response_function.lower() == 'irac1': response_file = root + "standard/IRAC1_subarray_response_function.txt" elif response_function.lower() == 'irac2': response_file = root + "standard/RAC2_subarray_response_function.txt" elif response_function.lower() == 'wfc3': response_file = root + "standard/WFC3_response_function.txt" # User-defined response functions: else: if os.path.exists(root + response_function): response_file = root + response_function elif os.path.exists(response_function): # RF not in RF folder: response_file = response_function else: print("Error: '{:s}' is not valid.".format(response_function)) sys.exit() # Open the response file, which we assume has as first column wavelength # and second column the response: w, r = np.loadtxt(response_file, unpack=True) if ('kepler' in response_file): w = 10 * w if min_w is None: min_w = min(w) if max_w is None: max_w = max(w) print('\t > Kepler response file detected. Switch from ' 'nanometers to Angstroms.') print('\t > Minimum wavelength: {} A.\n' '\t > Maximum wavelength: {} A.'.format(min(w), max(w))) elif ('IRAC' in response_file): w = 1e4 * w if min_w is None: min_w = min(w) if max_w is None: max_w = max(w) print('\t > IRAC response file detected. Switch from microns to ' 'Angstroms.') print('\t > Minimum wavelength: {} A.\n' '\t > Maximum wavelength: {} A.'.format(min(w), max(w))) else: if min_w is None: min_w = min(w) if max_w is None: max_w = max(w) # Fit a univariate linear spline (k=1) with s=0 (a node in each data-point): S = si.UnivariateSpline(w, r, s=0, k=1) if type(min_w) is list: S_wav = [] S_res = [] for i in range(len(min_w)): c_idx = np.where((w > min_w[i]) & (w < max_w[i]))[0] c_S_wav = np.append(np.append(min_w[i], w[c_idx]), max_w[i]) c_S_res = np.append(np.append(S(min_w[i]), r[c_idx]), S(max_w[i])) S_wav.append(np.copy(c_S_wav)) S_res.append(np.copy(c_S_res)) else: idx = np.where((w > min_w) & (w < max_w))[0] S_wav = np.append(np.append(min_w, w[idx]), max_w) S_res = np.append(np.append(S(min_w), r[idx]), S(max_w)) return min_w, max_w, S_wav, S_res
# Interpolates sigmas on equidistant mesh! for c in cixs: fsig = open('sig.inp' + str(c) + options.m_ext, 'r') data = fsig.readlines() fsig.close() om0 = [] sig0 = [] for line in data: dd = map(float, line.split()) om0.append(dd[0]) sig0.append(dd[1:]) sig0 = array(sig0).transpose() sig1 = [] for i in range(len(sig0)): fs = interpolate.UnivariateSpline(om0, sig0[i], s=0) sig1.append(fs(eq_om)) #print 'sig1=', sig1 #tck = interpolate.splrep(om0, sig0[i]) #sig1.append( interpolate.splev(eq_om, tck) ) sig1 = array(sig1).transpose() osig = open('sig.inp' + str(c) + options.m_ext + '_band', 'w') for i in range(len(eq_om)): print >> osig, '%15.8f ' % eq_om[i], ( '%15.8f ' * len(sig1[i])) % tuple(sig1[i]) osig.close() if not options.def_only: runExternal('dmft' + idmf, dmfe.ROOT, dmfe.MPI2, options.nom, options.ntail, 'dmft', options.m_ext, False)
def _interpolate_scipy_wrapper(x, y, new_x, method, fill_value=None, bounds_error=False, order=None, **kwargs): """ passed off to scipy.interpolate.interp1d. method is scipy's kind. Returns an array interpolated at new_x. Add any new methods to the list in _clean_interp_method """ try: from scipy import interpolate # TODO: Why is DatetimeIndex being imported here? from pandas import DatetimeIndex # noqa except ImportError: raise ImportError('{0} interpolation requires Scipy'.format(method)) new_x = np.asarray(new_x) # ignores some kwargs that could be passed along. alt_methods = { 'barycentric': interpolate.barycentric_interpolate, 'krogh': interpolate.krogh_interpolate, 'from_derivatives': _from_derivatives, 'piecewise_polynomial': _from_derivatives, } if getattr(x, 'is_all_dates', False): # GH 5975, scipy.interp1d can't hande datetime64s x, new_x = x._values.astype('i8'), new_x.astype('i8') if method == 'pchip': try: alt_methods['pchip'] = interpolate.pchip_interpolate except AttributeError: raise ImportError("Your version of Scipy does not support " "PCHIP interpolation.") elif method == 'akima': try: from scipy.interpolate import Akima1DInterpolator # noqa alt_methods['akima'] = _akima_interpolate except ImportError: raise ImportError("Your version of Scipy does not support " "Akima interpolation.") interp1d_methods = ['nearest', 'zero', 'slinear', 'quadratic', 'cubic', 'polynomial'] if method in interp1d_methods: if method == 'polynomial': method = order terp = interpolate.interp1d(x, y, kind=method, fill_value=fill_value, bounds_error=bounds_error) new_y = terp(new_x) elif method == 'spline': # GH #10633 if not order: raise ValueError("order needs to be specified and greater than 0") terp = interpolate.UnivariateSpline(x, y, k=order, **kwargs) new_y = terp(new_x) else: # GH 7295: need to be able to write for some reason # in some circumstances: check all three if not x.flags.writeable: x = x.copy() if not y.flags.writeable: y = y.copy() if not new_x.flags.writeable: new_x = new_x.copy() method = alt_methods[method] new_y = method(x, y, new_x, **kwargs) return new_y
#URL: https://www.hackerrank.com/contests/foxtradingsolutions-internship-assessment/challenges/missing-stock-prices/submissions import numpy as np from scipy import interpolate N = int(input()) prices = [] for i in range(N): date_time, price = input().split("\t") prices.append(price) x_known = [] prices_float = [] x_unknown = [] #print("f",prices[6]) for i in range(N): if prices[i][0]!="M" and prices[i][0]!="m": x_known.append(i) prices_float.append(float(prices[i])) else: x_unknown.append(i) prices_known = np.array(prices_float) pred_model = interpolate.UnivariateSpline(x_known, prices_known, s=1) for i in x_unknown: print(pred_model(i))
def approximateFasc(typeapprox, listF, d): """ Realizes the interpolation of a list of fascicles, either with polynomial fitting or with B-splines. Inputs: listF is the list of fascicles (same format as output from function contourAverage). Each fascicle is an array of points defining its line. Each point = (row, column). d (integer) : degree of the curve that will be interpolated typeapprox (string): either 'Bspline' or 'polyfit', depending on the type of approximation that you want to realize (spline or fitting with a polynom respectively) Outputs: a list of splines. """ approx_fasc = [] if typeapprox == 'Bspline': import scipy.interpolate as interpolate #transform each fascicle format in list for n in range(len(listF)): if type(listF[n]) == list: f = listF[n] else: f = list(listF[n]) #sort all fascicle points in increasing columns order f.sort(key=lambda x: x[1]) #remove potential double points to_remove = [] for pt in range(len(f) - 1): if f[pt][1] == f[pt + 1][1]: to_remove.append(pt + 1) fa = [f[ind] for ind in range(len(f)) if ind not in to_remove] ycoord = [fa[i][1] for i in range(len(fa))] xcoord = [fa[i][0] for i in range(len(fa))] #interpolation spline = interpolate.UnivariateSpline(ycoord, xcoord, k=d, ext=0) approx_fasc.append(spline) if typeapprox == 'polyfit': for n in range(len(listF)): #change format to list if necessary if type(listF[n]) == list: f = listF[n] else: f = list(listF[n]) #sort fascicle points in increasing columns order f.sort(key=lambda x: x[1]) #remove potential double points to_remove = [] for pt in range(len(f) - 1): if f[pt][1] == f[pt + 1][1]: to_remove.append(pt + 1) fa = [f[ind] for ind in range(len(f)) if ind not in to_remove] ycoord = [fa[i][1] for i in range(len(fa))] xcoord = [fa[i][0] for i in range(len(fa))] #interpolation and conversion to spline p = np.polyfit(ycoord, xcoord, deg=d) spline = np.poly1d(p) approx_fasc.append(spline) return approx_fasc
# error plot is a little different from regular if errplt[i] =='y': plt.errorbar( x[i], y[i], yerr=e[i], label=labels[i], linewidth=linewidth[i], linestyle=linestyle[i], color=color[i], fmt=marker[i], capthick=linewidth[i], barsabove=False, elinewidth=linewidth[i], ecolor=ecolor[i], markersize=markersize[i], capsize=2.5 ) elif histplt =='y': plt.bar( x[i], y[i], color=color[i], edgecolor=ecolor[i], align='center', width=abs(x[i][0]-x[i][1]), label=labels[i] ) else: plt.plot( x[i], y[i], label=labels[i], linewidth=linewidth[i], linestyle=linestyle[i], color=color[i], marker=marker[i], markersize=markersize[i] ) if fit[i] >= 0: # order, must be for spline to work order = np.argsort(x[i]) spl = interpolate.UnivariateSpline(x[i][order],y[i][order], s=fit[i]) xs = np.linspace( x[i].min(), x[i].max(), num=100 ) plt.plot( xs, spl(xs), linewidth=linewidth[i], color=color[i], linestyle='-' ) # edit the plots with the appropriate labels and stuff plt.xlabel( xlabels[0], fontsize=10 ) plt.ylabel( ylabels[0], fontsize=10) # adjust the tick size and turn on minor ticks plt.tick_params(axis='both',which='major',labelsize=10, direction='in', top='on',bottom='on', left='on', right='on') plt.minorticks_on() plt.tick_params(axis='both',which='minor',labelsize=6, direction='in', top='on',bottom='on', left='on', right='on')
######################### ## ## ## Irving Gomez Mendez ## ## April 02, 2021 ## ## ## ######################### import numpy as np import matplotlib.pyplot as plt import scipy.interpolate as interpolate x = np.linspace(-3, 3, 50) y = np.exp(-x**2) + 0.1 * np.random.randn(50) xs = np.linspace(-3, 3, 1000) spl_1 = interpolate.UnivariateSpline(x, y, s=0) spl_2 = interpolate.UnivariateSpline(x, y, s=0.5) spl_3 = interpolate.UnivariateSpline(x, y, s=3) plt.figure(figsize=(7.5, 5)) plt.plot(xs, spl_1(xs), 'g', lw=2) plt.plot(xs, spl_2(xs), 'b', lw=2) plt.plot(xs, spl_3(xs), 'y', lw=2) plt.plot(x, y, 'ro', ms=5)
def calc_fall_flush_durations_2(filter_data, date): """Left side sharp""" der_percent_threshold_left = 50 # Slope of rising limb (i.e. derivative) must be "sharp" flow_percent_threshold_left = 80 """Right side mellow""" der_percent_threshold_right = 30 # Slope of falling limb (i.e. derivative) has lower requirement to be part of flush duration flow_percent_threshold_right = 80 duration = None left = 0 right = 0 if date or date == 0: date = int(date) _, left_minarray = peakdet(filter_data[:date], 0.01) _, right_minarray = peakdet(filter_data[date:], 0.01) if not list(left_minarray): left = 0 else: left = int(left_minarray[-1][0]) if not list(right_minarray): right = 0 else: right = int(date - 2 + right_minarray[0][0]) if date - left > 10: """create spline, and find derivative""" x_axis_left = list(range(len(filter_data[left:date]))) spl_left = ip.UnivariateSpline( x_axis_left, filter_data[left:date], k=3, s=3) spl_first_left = spl_left.derivative(1) """check if derivative value falls below certain threshold""" spl_first_left_median = np.nanpercentile( spl_first_left(x_axis_left), der_percent_threshold_left) """check if actual value falls below threshold, avoiding the rounded peak""" median_left = np.nanpercentile( list(set(filter_data[left:date])), flow_percent_threshold_left) for index_left, der in enumerate(reversed(spl_first_left(x_axis_left))): # print(der < spl_first_left_median, filter_data[date - index_left] < median_left) if der < spl_first_left_median and filter_data[date - index_left] < median_left: left = date - index_left break if right - date > 10: x_axis_right = list(range(len(filter_data[date:right]))) spl_right = ip.UnivariateSpline( x_axis_right, filter_data[date:right], k=3, s=3) spl_first_right = spl_right.derivative(1) spl_first_right_median = abs(np.nanpercentile( spl_first_right(x_axis_right), der_percent_threshold_right)) median_right = np.nanpercentile( list(set(filter_data[date:right])), flow_percent_threshold_right) for index_right, der in enumerate(spl_first_right(x_axis_right)): # print(date+index_right, der < spl_first_right_median, filter_data[date + index_right] < median_right) if abs(der) < spl_first_right_median and filter_data[date + index_right] < median_right: right = date + index_right break if left: duration = int(date - left) elif not left and right: duration = int(right - date) else: duration = 0 return duration, left, right
shifts = np.zeros([M, 2]) for J in xrange(0, M): shifts[J, 1] = np.random.normal( loc=velocity[1] * tx, scale=(D_charge * Drift_decay[J] + D_environ) * np.sqrt(tx), size=1) shifts[J, 0] = np.random.normal( loc=velocity[0] * tx, scale=(D_charge * Drift_decay[J] + D_environ) * np.sqrt(tx), size=1) trans = np.cumsum(shifts, axis=0) centroid = np.mean(trans, axis=0) trans -= centroid splineX = intp.UnivariateSpline(t_axis, trans[:, 1], s=0.0) splineY = intp.UnivariateSpline(t_axis, trans[:, 0], s=0.0) t_display = np.linspace(np.min(t_axis), np.max(t_axis), 2048) plt.figure() plt.plot(trans[:, 1], trans[:, 0], 'k.', label='trans') plt.plot(splineX(t_display), splineY(t_display), label='spline') plt.legend(loc='best') # motion-blur velocity vectors can be computed from numerical derivative of the splines print("TODO: compute instantaneous velocity vectors") # Object is a few spheres # Make a short array of object positions objectCount = 50 objectPositions = np.random.uniform(low=-N / 2 + objRadius,