def fit(hm, t=None): X, Y = np.meshgrid(np.arange(0.0, hm.shape[1], 1.0), np.arange(0.0, hm.shape[0], 1.0)) if t is not None: return interpolate.bisplrep(X, Y, hm, kx=3, ky=3, task=-1, tx=t[0], ty=t[1]) else: return interpolate.bisplrep(X, Y, hm, kx=3, ky=3)
def interpolate(self, logtauLim=[-1, 2.5], Tlim=[5000, 50000], isNewStyle=True, field='HI'): N = 100 # N = 10 # yT = np.logspace(*np.log10(np.array(Tlim)), N) yT = np.logspace(np.log10(Tlim[0]), np.log10(Tlim[1]), N) ylogT = np.log10(yT) xtau = np.logspace(logtauLim[0], logtauLim[1], N) xlogtau = np.log10(xtau) xtau, yT = np.meshgrid(xtau, yT) xlogtau, ylogT = np.meshgrid(xlogtau, ylogT) # print(self.logef(1, 1000)) # logefV = np.vectorize(self.logef()) # zlogef = logefV(xtau, yT) nx, ny = xtau.shape # zlogef = np.array([[self.logef(xtau[i, j], yT[i, j]) # for i in range(nx)] for j in range(ny)]) zlogef = self.logefV(xtau, yT, field) if isNewStyle: # return logtau, logT --> logef / tau return interpolate.bisplrep(xlogtau, ylogT, zlogef / xtau) else: # return logtau, logT --> logef return interpolate.bisplrep(xlogtau, ylogT, zlogef)
def __init__(self, fname='data/coords.npy'): coord_table = np.load('data/coords.npy') self.t = t = coord_table lat, lon, x, z = t self.f0 = interp.bisplrep(lat, lon, x) self.f1 = interp.bisplrep(lat, lon, z) self.g0 = interp.bisplrep(x, z, lat) self.g1 = interp.bisplrep(x, z, lon)
def interpolation(noisy, SNR, number_of_pilot, interp): noisy_image = np.zeros((40000, 72, 14, 2)) noisy_image[:, :, :, 0] = np.real(noisy) noisy_image[:, :, :, 1] = np.imag(noisy) if number_of_pilot == 48: idx = [14 * i for i in range(1, 72, 6)] + [4 + 14 * i for i in range(4, 72, 6)] + [7 + 14 * i for i in range(1, 72, 6)] + [ 11 + 14 * i for i in range(4, 72, 6)] elif number_of_pilot == 16: idx = [4 + 14 * i for i in range(1, 72, 9)] + [9 + 14 * i for i in range(4, 72, 9)] elif number_of_pilot == 24: idx = [14 * i for i in range(1, 72, 9)] + [6 + 14 * i for i in range(4, 72, 9)] + [11 + 14 * i for i in range(1, 72, 9)] elif number_of_pilot == 8: idx = [4 + 14 * i for i in range(5, 72, 18)] + [9 + 14 * i for i in range(8, 72, 18)] elif number_of_pilot == 36: idx = [14 * i for i in range(1, 72, 6)] + [6 + 14 * i for i in range(4, 72, 6)] + [11 + 14 * i for i in range(1, 72, 6)] r = [x // 14 for x in idx] c = [x % 14 for x in idx] interp_noisy = np.zeros((40000, 72, 14, 2)) for i in range(len(noisy)): z = [noisy_image[i, j, k, 0] for j, k in zip(r, c)] if (interp == 'rbf'): f = interpolate.Rbf(np.array(r).astype(float), np.array(c).astype(float), z, function='gaussian') X, Y = np.meshgrid(range(72), range(14)) z_intp = f(X, Y) interp_noisy[i, :, :, 0] = z_intp.T elif (interp == 'spline'): tck = interpolate.bisplrep(np.array(r).astype(float), np.array(c).astype(float), z) z_intp = interpolate.bisplev(range(72), range(14), tck) interp_noisy[i, :, :, 0] = z_intp z = [noisy_image[i, j, k, 1] for j, k in zip(r, c)] if (interp == 'rbf'): f = interpolate.Rbf(np.array(r).astype(float), np.array(c).astype(float), z, function='gaussian') X, Y = np.meshgrid(range(72), range(14)) z_intp = f(X, Y) interp_noisy[i, :, :, 1] = z_intp.T elif (interp == 'spline'): tck = interpolate.bisplrep(np.array(r).astype(float), np.array(c).astype(float), z) z_intp = interpolate.bisplev(range(72), range(14), tck) interp_noisy[i, :, :, 1] = z_intp interp_noisy = np.concatenate((interp_noisy[:, :, :, 0], interp_noisy[:, :, :, 1]), axis=0).reshape(80000, 72, 14, 1) return interp_noisy
def get_tck(x,y,z,kk_default=3,logger=None): logger = logger or logging.getLogger(__name__) # estimate max number of allowed spline order mm = len(x) kk_max = np.int(np.floor(np.sqrt(mm/2.0)-1)) kk = np.min([kk_default,kk_max]) result = bisplrep(x=x,y=y,z=z,kx=kk,ky=kk,full_output=1) if result[2]>0: logger.info("Interpolation problem:%s" % result[-1]) logger.info("Now, try to adjust s") result = bisplrep(x=x,y=y,z=z,kx=kk,ky=kk,s=result[1],full_output=1) if result[2]>0: raise ValueError("Interpolation problem:%s" % result[-1]) return result[0]
def onUpdateSpline(self): p,ro,rmin,rmax=self.pts[np.where(self.bool_pnt)],self.RefOutline,self.RefMin,self.RefMax self.ui.statLabel.setText("Fitting . . .") QtWidgets.qApp.processEvents() #read input parameters self.gx=float(self.ui.numEdit1.text()) self.gy=float(self.ui.numEdit2.text()) kx=self.ui.numEdit3.value() ky=self.ui.numEdit4.value() tx=np.linspace(rmin[0],rmax[0],int((rmax[0]-rmin[0])/self.gx)) ty=np.linspace(rmin[1],rmax[1],int((rmax[1]-rmin[1])/self.gy)) #make sure both x & y have enough values in either direction if len(tx)<3 or len(ty)<3: self.ui.statLabel.setText("Grid too large . . .") self.ui.updateButton.setEnabled(True) return tx=np.insert(tx,0,[rmin[0]] * kx) #to make sure knots are repeated at edges tx=np.insert(tx,-1,[rmax[0]] * kx) ty=np.insert(ty,0,[rmin[1]] * ky) #to make sure knots are repeated at edges ty=np.insert(ty,-1,[rmax[1]] * ky) try: self.tck = bisplrep(p[:,0], p[:,1], p[:,2], kx=kx, ky=ky, tx=tx, ty=ty, task=-1) #get spline representation except ValueError as ve: self.ui.statLabel.setText("Last fit failed.") return self.DisplayFit()
def test_scipy_approx(): """ Test SciPy approximation of B-Spline surface :return: None """ terrain_data = [ [0.0, 0.0, 0.0], [0.0, 0.5, 0.4], [0.0, 1.0, 0.0], [0.5, 0.0, 0.2], [0.5, 0.5, 0.8], [0.5, 1.0, 0.2], [1.0, 0.0, 0.0], [1.0, 0.5, 0.4], [1.0, 1.0, 0.0]] tX = [item[0] for item in terrain_data] tY = [item[1] for item in terrain_data] tZ = [item[2] for item in terrain_data] from scipy import interpolate print('SciPy approximation ...') start_time = time.time() tck, fp, ior, msg = interpolate.bisplrep(tX, tY, tZ, kx=2, ky=2, full_output=1) end_time = time.time() print('Computed in {0} seconds.'.format(end_time - start_time)) occ_bspline = convert.bspline.scipy_to_occ(tck) # Compute difference between original terrain data and B-Spline surface u_num = v_num = 50 points = [[float(i)/u_num, float(j)/u_num, 0.0] for i in range(v_num+1) for j in range(u_num+1)] points = [(it[0], it[1], interpolate.bisplev(it[0], it[1], tck)) for it in points] # points = terrain_data display_results(occ_bspline, points)
def fit(self, data, poldegree, swidth, sheight, threshold): if int(threshold) == -1: threshold = (int(data.mean()) * 10) / 7 dims = data.shape xList = [] yList = [] zList = [] for y in xrange(0, dims[0] - 1, sheight): for x in xrange(0, dims[1] - 1, swidth): view = data[y:y + sheight, x:x + swidth] flatIndex = numpy.argmax(view) yIdx, xIdx = numpy.unravel_index(flatIndex, view.shape) zValue = view[yIdx, xIdx] if zValue <= threshold: xList.append(x + xIdx) yList.append(y + yIdx) zList.append(zValue) if len(xList) < (poldegree + 1) * (poldegree + 1): raise ValueError("Not enough reference points.") tck = interpolate.bisplrep(yList, xList, zList, kx=poldegree, ky=poldegree, xb=0, yb=0, xe=int(dims[0]), ye=int(dims[1])) clipmin, clipmax = data.min(), threshold return interpolate.bisplev(range(dims[0]), range(dims[1]), tck).clip(clipmin, clipmax)
def plotSomething(m): fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) for matrix in m: # Make data. X = np.arange(matrix.shape[1]) Y = np.arange(matrix.shape[0]) X, Y = np.meshgrid(X, Y) xnew, ynew = np.mgrid[0:6:80j, 0:3:80j] tck = interpolate.bisplrep(X, Y, matrix, s=0) znew = interpolate.bisplev(xnew[:, 0], ynew[0, :], tck) # Plot the surface. # surf = ax.plot_surface(X, Y, matrix, cmap=cm.coolwarm, # linewidth=0, antialiased=False) # fig.clear() # Plot the surface. surf = ax.plot_surface(xnew, ynew, znew, cmap=cm.coolwarm, linewidth=0, antialiased=True) plt.pause(0.05) time.sleep(0.5) plt.show()
def interpolate(self, xi, yi): from scipy import interpolate # Need to write a norm function that calculates distance from a rib... """ def interp(x1,x2,x3, x1i, x2i): spline = interpolate.Rbf(x1, x2, x3, function='thin-plate', smooth=0) return spline(x1i,x2i) try: zi = interp(self.points.x, self.points.y, self.points.z, xi, yi) except np.linalg.linalg.LinAlgError: zi = interp(self.points.y, self.points.x, self.points.z, yi, xi) """ # Segfaults... Problems with the way scipy is compiled? tck = interpolate.bisplrep(self.points.x, self.points.y, self.points.z) zi = interpolate.bisplev(yi, xi, tck) """ spline = interpolate.Rbf(self.points.x, self.points.y, self.points.z, function='thin-plate', smooth=0) zi = spline(xi,yi) """ return zi
def threshold_by_blocks(a, num_blocks, threshold_function, *args): a_dims = a.shape block_dims = [0, 0] block_dims[0] = int(round(a_dims[0] / \ round(np.sqrt(num_blocks * a_dims[0] / a_dims[1])))) block_dims[1] = int(round(a_dims[1] / \ round(np.sqrt(num_blocks * a_dims[1] / a_dims[0])))) x = [] y = [] th = [] for row in xrange(0, a_dims[0], block_dims[0]): for col in xrange(0, a_dims[1], block_dims[1]): x.append(float(row) + (block_dims[0] - 1) / 2) y.append(float(col) + (block_dims[1] - 1) / 2) threshold = threshold_function(a[row:row + block_dims[0], \ col:col + block_dims[1]], *args) th.append(threshold) x = np.asarray(x) y = np.asarray(y) th = np.asarray(th) #fit = bisplrep(x,y,th,xb=0,xe=a_dims[0]-1,yb=0,ye=a_dims[1]-1) fit = bisplrep(x, y, th) th_new = bisplev(np.arange(a_dims[0]), np.arange(a_dims[1]), fit) return th_new
def fitspline(xy, z, smoothing=None): """ Return a tuple (t, c, k) describing the spline as described in the Numpy documentation. """ return bisplrep(xy[...,0].ravel(), xy[...,1].ravel(), z.ravel(), s=smoothing)
def get_bspline_mtx(coord_eval_x, coord_eval_y, num_cp_x, num_cp_y, kx=4, ky=4): coord_tmp = get_coord_tmp(num_cp_x, num_cp_y) num_eval_x = coord_eval_x.shape[0] num_eval_y = coord_eval_y.shape[0] num_tmp = coord_tmp.shape[0] num_eval = num_eval_x * num_eval_y num_cp = num_cp_x * num_cp_y tx = np.linspace(0, 1, num_cp_x + kx + 1) ty = np.linspace(0, 1, num_cp_y + ky + 1) nxest = num_cp_x + kx + 1 nyest = num_cp_y + ky + 1 tmp = np.ones(num_tmp) tck = bisplrep( coord_tmp[:, 0], coord_tmp[:, 1], coord_tmp[:, 0], task=-1, kx=kx, ky=ky, tx=tx, ty=ty, nxest=nxest, nyest=nyest, xb=0., xe=1., yb=0., ye=1.) h = 1e-3 mtx = np.zeros((num_eval, num_cp)) out0 = bisplev(coord_eval_x, coord_eval_y, tck).flatten() for ind in range(num_cp): tck[2][ind] += h out = bisplev(coord_eval_x, coord_eval_y, tck).flatten() tck[2][ind] -= h mtx[:, ind] = (out - out0) / h return mtx
def interpolate_gradient(grad, x_pos, y_pos, magn=5): size_y, size_x = grad.shape x, y = np.mgrid[0:size_x, 0:size_y] xnew, ynew = np.mgrid[0:(size_x * magn), 0:(size_y * magn)] tck = interpolate.bisplrep(x, y, grad, s=0) znew = interpolate.bisplev(xnew[:, 0], ynew[0, :], tck) return znew
def __init__(self, flux_file=None, smooth=0.05, **params): logging.info("Loading atmospheric flux table %s" % flux_file) #Load the data table table = np.loadtxt(open_resource(flux_file)).T #columns in Honda files are in the same order cols = ['energy'] + primaries flux_dict = dict(zip(cols, table)) for key in flux_dict.iterkeys(): #There are 20 lines per zenith range flux_dict[key] = np.array(np.split(flux_dict[key], 20)) if not key == 'energy': flux_dict[key] = flux_dict[key].T #Set the zenith and energy range flux_dict['energy'] = flux_dict['energy'][0] flux_dict['coszen'] = np.linspace(0.95, -0.95, 20) #Now get a spline representation of the flux table. logging.debug('Make spline representation of flux') # do this in log of energy and log of flux (more stable) logE, C = np.meshgrid(np.log10(flux_dict['energy']), flux_dict['coszen']) self.spline_dict = {} for nutype in primaries: #Get the logarithmic flux log_flux = np.log10(flux_dict[nutype]).T #Get a spline representation spline = bisplrep(logE, C, log_flux, s=smooth) #and store self.spline_dict[nutype] = spline
def ranDataCreate(step, no, window=5, dim=100, low=0, factor=2, s=1, amplitude=1, seed=123456, save=0): # random generator ranGen = np.random.RandomState() ranGen.seed(seed) x, y, z = buildSpace(no, -1, 1, step, window, low, factor, ranGen) tck = interpolate.bisplrep(x, y, z, s=s) xynew = np.linspace(-1, 1, step) Z = interpolate.bisplev(xynew, xynew, tck) Z = setAmplitude(amplitude, Z) x = np.linspace(0, step - 1, step) X, Y = np.meshgrid(x, x) if (save == 0): np.savetxt('data/random_2d_data.csv', Z.ravel(order='C'), delimiter=',') return ranDataParams(X, Y, Z)
def __init__(self, tables, smooth=0.05, **params): logging.info("Loading atmospheric flux table %s" %tables) #Load the data table table = np.loadtxt(open_resource(tables)).T #columns in Honda files are in the same order cols = ['energy']+primaries flux_dict = dict(zip(cols, table)) for key in flux_dict.iterkeys(): #There are 20 lines per zenith range flux_dict[key] = np.array(np.split(flux_dict[key], 20)) if not key=='energy': flux_dict[key] = flux_dict[key].T #Set the zenith and energy range flux_dict['energy'] = flux_dict['energy'][0] flux_dict['coszen'] = np.linspace(0.95, -0.95, 20) #Now get a spline representation of the flux table. logging.debug('Make spline representation of flux') # do this in log of energy and log of flux (more stable) logE, C = np.meshgrid(np.log10(flux_dict['energy']), flux_dict['coszen']) self.spline_dict = {} for nutype in primaries: #Get the logarithmic flux log_flux = np.log10(flux_dict[nutype]).T #Get a spline representation spline = bisplrep(logE, C, log_flux, s=smooth) #and store self.spline_dict[nutype] = spline
def ipspline(ipparams, position, etc = []): """ This function fits the intra-pixel sensitivity effect using a bicubic spline. Parameters ---------- k#: Knot coefficient x,y: Array of x,y pixel positions and quadrant locations etx: Knot locations Returns ------- This function returns an array of y values... Revisions --------- 2010-06-08 Kevin Stevenson, UCF [email protected] Original Version """ y, x, q = position yknots, xknots = etc tck = spi.bisplrep(xknots.flatten(), yknots.flatten(), ipparams, kx=3, ky=3) #print tck #tck = [yknots, xknots, ipparams, 3, 3] #func = spi.interp2d(xknots, yknots, ipparams, kind='cubic' output = np.ones(y.size) for i in range(y.size): output[i] = spi.bisplev(x[i], y[i], tck, dx=0, dy=0) return output
def Y_p(omega_b, Nnu): """Calculate BBN-standard nucleon number fraction by interpolating results from AlterBBN v1.4""" _omega_b, _delta_Nnu, _Yp = np.loadtxt(context.bbn_table, unpack=True, usecols=[0, 2, 4]) intp = interpolate.bisplrep(_omega_b, _delta_Nnu, _Yp) return interpolate.bisplev(omega_b, Nnu - 3.046, intp)
def aproximate_terrain(self): """ Try to aproximate terrain with bspline surface """ tck,fp,ior,msg = interpolate.bisplrep(self.tX, self.tY, self.tZ, kx=5, ky=5, full_output=1) self.tck[(self.min_x, self.min_y, self.max_x, self.max_y)] = tck # Compute difference between original terrain data and b-spline surface self.tW = [abs(it[2] - interpolate.bisplev(it[0], it[1], tck)) for it in self.terrain_data]
def interpROI(self, s=None, plotFlag=False): # Set dimension dim = self.ROI.data.shape self.ROI.dim = [dim[0], dim[1], dim[0] * dim[1]] newDim = self.ROI.dimInterp[0].astype( int) # Force to int for linspace etc. # Set value for s, if not passed - choose very roughly by ROI size. If s is too small, interp tends to hang. if s is None: s = self.ROI.dim[2] * 1E-4 print('Interpolating, s = ', s) self.ROI.s = s # Set original gridding # dims = self.ROI.data.shape # Arb axes # x, y = np.mgrid[-1:1:(dims[0]*1j), -1:1:(dims[1]*1j)] # Real axes # x, y = np.meshgrid(self.ROI.wavelengths, self.ROI.fs) x, y = np.meshgrid(self.ROI.fs, self.ROI.wavelengths) # Interp to newDim # Arb units # xnew, ynew = np.mgrid[-1:1:(newDim*1j), -1:1:(newDim*1j)] # Weird - apparently using complex here provides grid inclusive of stop value, see https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.mgrid.html # Real units # TODO: this currently assumes linear sampling... may not be totally accurate self.ROI.fsInterp = np.linspace(self.ROI.fsLim[0], self.ROI.fsLim[1], num=newDim) self.ROI.waveInterp = np.linspace(self.ROI.waveLim[0], self.ROI.waveLim[1], num=newDim) #xnew, ynew = np.meshgrid(self.ROI.waveInterp, self.ROI.fsInterp) xnew, ynew = np.meshgrid(self.ROI.fsInterp, self.ROI.waveInterp) tck = interpolate.bisplrep(x, y, self.ROI.data / self.ROI.data.max(), s=s) # , s=0) self.ROI.dataInterp = interpolate.bisplev(xnew[0, :], ynew[:, 0], tck) self.ROI.dataInterp = self.ROI.dataInterp.T # Plot interp data if flag is set if plotFlag: plt.figure() plt.subplot(121) plt.pcolor(x, y, self.ROI.data) plt.colorbar() plt.title("Original data.") plt.subplot(122) plt.pcolor(xnew, ynew, self.ROI.dataInterp) plt.colorbar() plt.title("Interpolated data.") plt.show()
def cor_graph(self,name,Zv): z=np.asarray(Zv) b=self.Zrho a=self.Ztheta+np.pi/2#self.Zphi bn=self.Zrho_new an=self.Ztheta_new+np.pi/2#self.Zphi_new Bng=[] for i in z: f=interp1d(b, i, kind='cubic') Bng.append(f(bn)) z=np.array(Bng).T x,y=np.meshgrid(a,bn) m=len(x) val_nxest = int(max(3+np.sqrt(m/2),2*3+3)) tck = interpolate.bisplrep(y, x, z, s=3000, kx=3, ky=3, nxest=val_nxest) xn,yn=np.meshgrid(an,bn) zn = interpolate.bisplev(yn[:,0], xn[0,:], tck) for i in range(len(zn)): for j in range(len(zn[i])): if zn[i][j]>1.0: zn[i][j]=1 elif zn[i][j]<-1: zn[i][j]=-1 zn=zn.T for a in range(3): for i in range(-int(len(zn)/2),int(len(zn)/2)): zn[i]=0.5*(zn[int(len(zn)/2)-np.sign(i)*i]+zn[i]) for i in range(-int(len(zn)/2),int(len(zn)/2)): zn[i]=0.5*(zn[-i]+zn[i]) a+=1 zn=zn.T zn_min=1 zn_max=-1 for i in z: for j in i: if j<zn_min: zn_min=j if j>zn_max: zn_max=j matplotlib.use('Agg') plt.figure() xn,yn=np.meshgrid(an,bn) plt.subplots(subplot_kw=dict(projection='polar')) matplotlib.rcParams.update({'font.size': 15, 'font.family': 'serif'}) plt.pcolor(xn, yn, zn, vmin=0, vmax=1) plt.ioff() plt.savefig('cor_pic/{}.jpg'.format(name), dpi=600, bbox_inches="tight", format="jpg")
def interpgrid(x,y, xlist,ylist, xmap, ymap, kx=3, ky=3, s=50): ''' for position x,y and a 2-D mapping map(list), i.e., xmap[xlist,ylist],ymap[xlist,ylist] given on a grid xlist,ylist; the nearest xlist, ylist positions to each x,y pair are found and interpolated to yield mapx(x,y),mapy(x,y) x,y : rank-1 arrays of data points xlist, ylist, xmap, ymap: rank-1 arrays of data points + 2008-08-24 NPMK (MSSL) ''' from scipy import interpolate # check if the input is right data type # ... TBD # compute the Bivariate-spline coefficients # kx = ky = 3 # cubic splines (smoothing) task = 0 # find spline for given smoothing factor # s = 50 # spline goes through the given points # eps = 1.0e-6 (0 < eps < 1) #(tck_x, ems1) tck_x = interpolate.bisplrep(xlist,ylist,xmap,kx=kx,ky=ky,s=s) #(fp1, ier1, msg1) = ems1 #if ier1 in [1,2,3]: # print 'an error occurred computing the bivariate spline (xmap) ' # print ier1, msg1 # # raise error # return None tck_y = interpolate.bisplrep(xlist,ylist,ymap,kx=kx,ky=ky,s=s) #(fp2, ier2, msg2) = ems2 #if ier2 in [1,2,3]: # print 'an error occurred computing the bivariate spline (ymap) ' # print ier2, msg2 # # raise error # return None # compute the spline xval = interpolate.bisplev(x, y, tck_x) yval = interpolate.bisplev(x, y, tck_y) return xval,yval
def plt(n=25): x=[] y=[] qx=[] qy=[] for i in range(n): x.append(r()) y.append(r()) qx.append(sin(x[-1])) qy.append(cos(y[-1])) qxb=bisplrep(x,y,qx,s=0) qyb=bisplrep(x,y,qy,s=0) X=arange(-2,2,0.4) Y=arange(-2,2,0.4) cla() hold(True) quiver(x,y,qx,qy,pivot='tail',color='b') quiver2(X,Y,bisplev(X, Y,qxb),bisplev(X, Y,qyb),pivot='tail',color='r') hold(False)
def interpgrid(x, y, xlist, ylist, xmap, ymap, kx=3, ky=3, s=50): ''' for position x,y and a 2-D mapping map(list), i.e., xmap[xlist,ylist],ymap[xlist,ylist] given on a grid xlist,ylist; the nearest xlist, ylist positions to each x,y pair are found and interpolated to yield mapx(x,y),mapy(x,y) x,y : rank-1 arrays of data points xlist, ylist, xmap, ymap: rank-1 arrays of data points + 2008-08-24 NPMK (MSSL) ''' from scipy import interpolate # check if the input is right data type # ... TBD # compute the Bivariate-spline coefficients # kx = ky = 3 # cubic splines (smoothing) task = 0 # find spline for given smoothing factor # s = 50 # spline goes through the given points # eps = 1.0e-6 (0 < eps < 1) #(tck_x, ems1) tck_x = interpolate.bisplrep(xlist, ylist, xmap, kx=kx, ky=ky, s=s) #(fp1, ier1, msg1) = ems1 #if ier1 in [1,2,3]: # print 'an error occurred computing the bivariate spline (xmap) ' # print ier1, msg1 # # raise error # return None tck_y = interpolate.bisplrep(xlist, ylist, ymap, kx=kx, ky=ky, s=s) #(fp2, ier2, msg2) = ems2 #if ier2 in [1,2,3]: # print 'an error occurred computing the bivariate spline (ymap) ' # print ier2, msg2 # # raise error # return None # compute the spline xval = interpolate.bisplev(x, y, tck_x) yval = interpolate.bisplev(x, y, tck_y) return xval, yval
def bulid_data(data): # bulid_data(np.array([ips.data[1]['body'],ips.data[1]['z']])) z = data[1].astype(np.float64) data = np.array([i for i in data[0]]) x, y, weight = data[:, 0], data[:, 1], data[:, 2] tck = interpolate.bisplrep(x, y, z, w=weight, kx=1, ky=2) xnew, ynew = np.mgrid[0:500:500j, 0:500:500j] print('xynew', xnew, ynew) znew = interpolate.bisplev(xnew[:, 0], ynew[0, :], tck) return znew
def processing(filename,x_u,y_u,x_l,y_l,s_val,x_new_res,y_new_res,coord_opt,contour_lim): #load in data as a 2D matrix try: with open(filename): pass except IOError: return -1 values = np.loadtxt(filename,delimiter=',') #Check if 95% limit will exist flag = False for row in values: for element in row: if element >= contour_lim: flag = True break if (flag == False): return -2 #define data co-ordinates #TODO: take into account irregularly spaced data values if coord_opt == 'd': x = np.mgrid[x_l:x_u:len(values[0])*1j] y = np.mgrid[y_l:y_u:len(values)*1j] elif coord_opt == 'n': #request to read in co-ordinates noted in data file try: with open(filename+"_coord"): pass except IOError: return -3 else: filename_coord = filename+"_coord" data_coord=open(filename_coord) x=((data_coord.readline()).strip()).split(',') x = [float(i) for i in x ] y=((data_coord.readline()).strip()).split(',') y = [float(i) for i in y ] x,y = np.meshgrid(x,y) #interpolate using quadratic splines #Quadratic are used to better preserve asymptotic nature of plots #TODO:What value of s is optimal? tck = interp.bisplrep(x,y,values,kx=2,ky=2,s=s_val) #define points to interpolate over xnew,ynew = np.mgrid[x_l:x_u:(x_new_res*1j),y_l:y_u:(y_new_res*1j)] values_new = interp.bisplev(xnew[:,0],ynew[0,:],tck) #plot only the cls_level line v=np.linspace(contour_lim,contour_lim,2) cs = plt.contour(xnew,ynew,values_new,v) #Extract data of cls_level line #TODO: investigate syntax of this line #TODO: catch error where there is data below 95% but not enough to generate a contour return (cs.collections[0].get_paths()[0]).vertices
def bivariate_spline(flux_dict, cz_centers, en_centers, smooth=0.02): """Spline the flux.""" logging.debug('Entering mceq.bivariate_spline') Cz, logE = np.meshgrid(cz_centers, np.log10(en_centers)) spline_dict = OrderedDict() for nu in flux_dict.iterkeys(): log_flux = np.log10(flux_dict[nu]).T spline = interpolate.bisplrep(Cz, logE, log_flux, s=smooth) spline_dict[nu] = spline return spline_dict
def get_deformation_field(self, field_lowres_x, field_lowres_y, image_shape): ''' :param field_lowres_x: np.array of low resolution velocity field (possibly random) :param field_lowres_y: np.array low resolution velocity field (possibly random) :param image_shape: tuple of (rows, cols) :return: flow_x, flow_y I'll be using X and Y as cartesian coordinates (i.e: Y=rows, X=columns). However, b-Splines functions use X=rows, Y=columns, that's why I change the parameters ''' lowres_shape = field_lowres_x.shape ratio_shape = tuple( [np.ceil(1.0 * a / b) for a, b in zip(image_shape, lowres_shape)]) Y, X = np.arange(0, image_shape[0]), np.arange(0, image_shape[1]) Ylowres, Xlowres = np.arange(0, image_shape[0], ratio_shape[0]), np.arange( 0, image_shape[1], ratio_shape[1]) YYlowres, XXlowres = np.meshgrid(Ylowres, Xlowres, indexing='ij') #bisplrep uses x as rows and y as columns r_x = bisplrep(YYlowres, XXlowres, field_lowres_x.flatten(), tx=np.arange(0, Ylowres.shape[0]), ty=np.arange(0, Xlowres.shape[0]), task=-1) #, s=np.prod(Xlowres.shape)) r_y = bisplrep(YYlowres, XXlowres, field_lowres_y.flatten(), tx=np.arange(0, Ylowres.shape[0]), ty=np.arange(0, Xlowres.shape[0]), task=-1) #, s=np.prod(Xlowres.shape)) flow_x = bisplev(Y, X, r_x) flow_y = bisplev(Y, X, r_y) # (X=rows,Y=col) return flow_x, flow_y
def init_spline(self,dhalo,psi,z): """Compute knots and coefficients of an interpolating spline given a grid of points in halo distance (dhalo) and offset angle (psi) at which the LoS integral has been computed. """ kx = 2 ky = 2 self._psi_min = psi.min() self._tck = bisplrep(dhalo,psi,np.log10(z),s=0.0,kx=kx,ky=ky, nxest=int(kx+np.sqrt(len(z.flat))), nyest=int(ky+np.sqrt(len(z.flat))))
def _data_from_ndvar(self, ndvar): v = ndvar.get_data(('sensor', )) locs = ndvar.sensor.get_locs_2d(self._proj, frame=SENSORMAP_FRAME) if self._visible_data is not None: v = v[self._visible_data] locs = locs[self._visible_data] if self._method is None: # interpolate data xi, yi = self._mgrid # code adapted from mne-python topmap _griddata() xy = locs[:, 0] + locs[:, 1] * -1j d = np.abs(xy - xy[:, None]) diagonal_step = len(locs) + 1 d.flat[::diagonal_step] = 1. g = (d * d) * (np.log(d) - 1.) g.flat[::diagonal_step] = 0. weights = linalg.solve(g, v.ravel()) m, n = xi.shape out = np.empty_like(xi) g = np.empty(xy.shape) for i in range(m): for j in range(n): d = np.abs(xi[i, j] + -1j * yi[i, j] - xy) mask = np.where(d == 0)[0] if len(mask): d[mask] = 1. np.log(d, out=g) g -= 1. g *= d * d if len(mask): g[mask] = 0. out[i, j] = g.dot(weights) return out elif self._method == 'spline': k = int(floor(sqrt(len(locs)))) - 1 tck = interpolate.bisplrep(locs[:, 1], locs[:, 0], v, kx=k, ky=k) return interpolate.bisplev(self._grid, self._grid, tck) else: isnan = np.isnan(v) if np.any(isnan): nanmap = interpolate.griddata(locs, isnan, self._mgrid, self._method) mask = nanmap > 0.5 v = np.where(isnan, 0, v) vmap = interpolate.griddata(locs, v, self._mgrid, self._method) np.place(vmap, mask, np.NaN) return vmap return interpolate.griddata(locs, v, self._mgrid, self._method)
def _data_from_ndvar(self, ndvar): v = ndvar.get_data(('sensor',)) locs = ndvar.sensor.get_locs_2d(self._proj, frame=SENSORMAP_FRAME) if self._visible_data is not None: v = v[self._visible_data] locs = locs[self._visible_data] if self._method is None: # interpolate data xi, yi = self._mgrid # code adapted from mne-python topmap _griddata() xy = locs[:, 0] + locs[:, 1] * -1j d = np.abs(xy - xy[:, None]) diagonal_step = len(locs) + 1 d.flat[::diagonal_step] = 1. g = (d * d) * (np.log(d) - 1.) g.flat[::diagonal_step] = 0. weights = linalg.solve(g, v.ravel()) m, n = xi.shape out = np.empty_like(xi) g = np.empty(xy.shape) for i in range(m): for j in range(n): d = np.abs(xi[i, j] + -1j * yi[i, j] - xy) mask = np.where(d == 0)[0] if len(mask): d[mask] = 1. np.log(d, out=g) g -= 1. g *= d * d if len(mask): g[mask] = 0. out[i, j] = g.dot(weights) return out elif self._method == 'spline': k = int(floor(sqrt(len(locs)))) - 1 tck = interpolate.bisplrep(locs[:, 1], locs[:, 0], v, kx=k, ky=k) return interpolate.bisplev(self._grid, self._grid, tck) else: isnan = np.isnan(v) if np.any(isnan): nanmap = interpolate.griddata(locs, isnan, self._mgrid, self._method) mask = nanmap > 0.5 v = np.where(isnan, 0, v) vmap = interpolate.griddata(locs, v, self._mgrid, self._method) np.place(vmap, mask, np.NaN) return vmap return interpolate.griddata(locs, v, self._mgrid, self._method)
def process_functions(functions, UV_data, nodes): """ Processes coefficient functions of the DE/problem to create directly callable functions from Python. """ default_lambda = "lambda x,y:" global_vars = None for name in functions: if functions[name] == '?': functions[name] = '0' elif functions[name] == "x" or functions[name] == "y": #If it is indicated that the provided U & V values to be used if not global_vars: x, y = [0] * nodes.__len__(), [0] * nodes.__len__() for i, node in enumerate(nodes): x[i] = node[0] y[i] = node[1] from scipy.interpolate import bisplrep, bisplev # Fit a bivariate B-spline to U and V values to calculate # values that are not on the nodes This "global_vars" # dictionary is provided to eval for the lambda's to work # properly global_vars = { "x_tck": bisplrep(x, y, UV_data[0]), "y_tck": bisplrep(x, y, UV_data[1]), "bisplev": bisplev } functions[name] = eval( "lambda x,y: bisplev(x, y, {0}_tck)".format(functions[name]), global_vars ) continue functions[name] = default_lambda + functions[name] functions[name] = eval(functions[name]) return functions
def get_splined_2d_dist(fname, islog=False, num_spline_points=100): data = np.loadtxt(fname, skiprows=1) if islog: lnL = data[:, 2] else: lnL = np.log(data[:, 2]) lnL = -2* (lnL - np.max(lnL)) tck = interpolate.bisplrep(data[:, 0], data[:, 1], lnL, s=1) num_spline_points = complex(0, num_spline_points) Q_args, N_args = np.mgrid[data[0, 0]:data[-1, 0]:num_spline_points, data[0, 1]:data[-1, 1]:num_spline_points] lnL_splined = interpolate.bisplev(Q_args[:, 0], N_args[0, :], tck) return Q_args, N_args, lnL_splined
def interpolateAWS(data, newPoint): X = data["latitude"].values Y = data["longitude"].values value = data["value"].values m = len(X) #number in [(m - sqrt(2 * m) , m + sqrt(2 * m))] tck = interpolate.bisplrep(x=X, y=Y, z=value, s=m) Xnew = newPoint[0] Ynew = newPoint[1] znew = interpolate.bisplev(x=Xnew, y=Ynew, tck=tck) #print(znew) return (znew)
def interpolate(self): # # ПРоинтерполированное поле # # tck = interpolate.interp2d( self._lon, self._lat, self._val, kind='linear' ) # tck = interpolate.bisplrep(self._lon, self._lat, self._val, s=28733) tck = interpolate.bisplrep(self._lon, self._lat, self._val, s=28733) self._lap = csgraph.laplacian(interpolate.bisplev( self._xi_l, self._yi_l, tck), normed=False) # self._lap = csgraph.laplacian(tck( self._xi_l, self._yi_l), normed=False) # print(tck(56.3,5.0)) return self
def _fit(self, X, y): """The function call to fit the spline model on the given data. This function is not supposed to be called directly. """ # fitting the curve # bisplrep returns details of the fitted curve # read bisplrep docs for more info about it's return values. self.tck = bisplrep(X[:, 0], X[:, 1], y, kx=self.kx, ky=self.ky, s=self.s) return self
def get_splined_2d_dist(fname, islog=False, num_spline_points=100): data = np.loadtxt(fname, skiprows=1) if islog: lnL = data[:, 2] else: lnL = np.log(data[:, 2]) lnL = -2 * (lnL - np.max(lnL)) tck = interpolate.bisplrep(data[:, 0], data[:, 1], lnL, s=1) num_spline_points = complex(0, num_spline_points) Q_args, N_args = np.mgrid[data[0, 0]:data[-1, 0]:num_spline_points, data[0, 1]:data[-1, 1]:num_spline_points] lnL_splined = interpolate.bisplev(Q_args[:, 0], N_args[0, :], tck) return Q_args, N_args, lnL_splined
def off_pol_eval_linear_test(n_max, beta_0, beta_hi, n_trials, n_treatments, n_spacing, n_0, **sub_params): ''' Systematically evaluate over a treatment space defined by a linear treatment policy ''' treatment_space = np.linspace(beta_0, beta_hi, n_treatments) off_pol_evals = np.zeros([n_treatments, n_spacing, n_trials]) oracle_evals = np.zeros([n_treatments, n_spacing, n_trials]) discrete_off_pol_evals = np.zeros([n_treatments, n_spacing, n_trials]) t_lo = sub_params['t_lo'] t_hi = sub_params['t_hi'] spl_x = sub_params['z'][:, 0] spl_t = sub_params['z'][:, 1] # f is positive splined_f_tck = interpolate.bisplrep(spl_x, spl_t, sub_params['f']) sub_params['spline'] = splined_f_tck oracle_func = sub_params['oracle_func'] n = sub_params['n'] m = sub_params['m'] for i, n_sub in enumerate(np.linspace(n_0, n_max, n_spacing)): n_rnd = int(np.floor(n_sub)) print "testing n = " + str(n_rnd) for k in np.arange(n_trials): for beta_ind, beta in enumerate(treatment_space): subsamples_pm = evaluate_subsample(n_rnd, evaluation=False, cross_val=False, **sub_params) tau = np.clip(np.dot(subsamples_pm['x_samp'], beta), t_lo, t_hi) subsamples_pm['tau'] = tau oracle_evals[beta_ind, i, k] = np.mean(oracle_func(**subsamples_pm)) # oracle_evals[beta_ind, i, k] = np.mean(evaluate_oracle_interpolated_outcomes(splined_f_tck,m,n_rnd, subsamples_pm['f'], beta_0, beta_hi, tau, subsamples_pm['x_samp'])) # off_pol_evals[beta_ind, i, k] = off_policy_evaluation(**subsamples_pm) off_pol_evals[beta_ind, i, k] = off_policy_evaluation(**subsamples_pm) discrete_off_pol_evals[beta_ind, i, k] = off_pol_disc_evaluation( discretize_tau_policy, **subsamples_pm) off_pol_evals.dump( str(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")) + 'off_pol_linear_vals.np') oracle_evals.dump( str(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M")) + 'off_pol_linear_oracles.np') return [oracle_evals, off_pol_evals, discrete_off_pol_evals]
def scan_interpolation(f, x_min, x_max, y_min, y_max, f_init=10**6, gridSize=5, delta=1): x_list = np.linspace(x_min, x_max, num=gridSize, endpoint=True) y_list = np.linspace(y_min, y_max, num=gridSize, endpoint=True) f_min = f_init grid = [] counter = 0 for x in x_list: for y in y_list: print("Scan progress: ", floor(counter / gridSize**2 * 1000) / 10, " %", end='\r') counter += 1 z = f(x, y) time.sleep(0.1) grid.append([x, y, z]) if (z < f_min): f_min = z point = [x, y] sys.stdout.write("\033[K") grid = np.array(grid) tck = interpolate.bisplrep(grid[:, 0], grid[:, 1], grid[:, 2]) def inter(x, y): return interpolate.bisplev(x, y, tck) m = Minuit(inter, *point) m.migrad() # run optimiser minimum = np.append(np.array(m.values), inter(*(m.values))) # minimum = GD_adaptive(inter, *point) return minimum
def precompute_efficiencies(self, n_pts=1e3): # Generate a LUT for efficiencies print("Precomputing efficiencies...") directions = util.fibonacci_sphere(n_pts) self.fluos = [flu.Fluorophore(*x) for x in directions] excite_effs = [ self.illuminator.calc_excitation_efficiency(fluo) for fluo in self.fluos ] collect_effs = [ self.detector.calc_collection_efficiency(fluo) for fluo in self.fluos ] self.excite_bspl = interpolate.bisplrep(directions[:, 0], directions[:, 1], excite_effs, s=0) self.collect_bspl = interpolate.bisplrep(directions[:, 0], directions[:, 1], collect_effs, s=0) self.precompute_flag = True
def get_optical_path_map(self,size=(20, 20), mask=None): """Return the optical path of the rays hitting the detector. This method uses the optical path of the rays hitting the surface, to create a optical path map. The returned value is an interpolation of the values obtained by the rays. Warning: If the rays hitting the surface are produced by more than one optical source, the returned map migth not be valid. *Atributes* *size* Tuple (nx,ny) containing the number of samples of the returned map. The map size will be the same as the CCD *mask* Shape instance containig the mask of the apperture. If not given, the mask will be automatically calculated. *Return value* A masked array as defined in the numpy.ma module, containig the optical paths """ X,Y,Z=self.get_optical_path_data() rv=bisplrep(X,Y,Z) nx, ny=size xs, ys=self.size xi=-xs/2. xf=-xi yi=-ys/2. yf=-yi xd=linspace(xi, xf,nx) yd=linspace(yi, yf,ny) data=bisplev(xd,yd,rv) if mask!=None: assert(isinstance(mask, Shape)) X, Y=meshgrid(xd, yd) m= ~mask.hit((X, Y, 0)) retval= ma.array(data, mask=m) else: retval=data return retval
def contourpk(x,y,f, levels=None,xb=None,xe=None,yb=None,ye=None,s=60,kx=1,ky=1,dolabels=True, **kwargs): '''Make a contour plot with 1-D array inputs for X, Y and F. This is a wrapper to convert lists of points (X,Y,Z) in 2-D arrays, then calls contour() Parameters ---------- X, Y: ndarrays[:], 1D on a 2D plane coordinates X, Y Z : ndarray[:], 1D function on X,Y kwargs : dict ------------- - **xb,xe,yb,ye** : float limits x,y for bispline interpolation valid region - **s** : float smoothing parameter for bisplrep - **kx, ky** : int order for the interpolation - **dolabels** : bool labels on the contours if true - **levels** : list contour levels Note ---- warning: X, Y axis may have been interchanged ''' import numpy from scipy import interpolate from pylab import contour, plt x1, x2, y1, y2 = min(x), max(x), min(y), max(y) xx = numpy.linspace(x1, x2) yy = numpy.linspace(y1, y2) X, Y = numpy.meshgrid(xx, yy) shp = X.shape task = 0 tck = interpolate.bisplrep(x,y,f,kx=kx,ky=ky,s=s,xb=xb,xe=xe,yb=yb,ye=ye) Z = interpolate.bisplev(xx, yy, tck) if levels == None: C = contour(Y, X, Z,**kwargs) else: C = contour(Y, X, Z, levels=levels,**kwargs) if dolabels: plt.clabel(C, inline=1,fontsize=10) return Y,X,Z,tck, C
def _data_from_ndvar(self, ndvar): v = ndvar.get_data(('sensor',)) locs = ndvar.sensor.get_locs_2d(self._proj) if self._interpolation == 'spline': tck = interpolate.bisplrep(locs[:, 1], locs[:, 0], v, kx=5, ky=5) return interpolate.bisplev(self._grid, self._grid, tck) else: isnan = np.isnan(v) if np.any(isnan): nanmap = interpolate.griddata(locs, isnan, self._mgrid, method=self._interpolation) mask = nanmap > 0.5 v = np.where(isnan, 0, v) vmap = interpolate.griddata(locs, v, self._mgrid, method=self._interpolation) np.place(vmap, mask, np.NaN) return vmap return interpolate.griddata(locs, v, self._mgrid, method=self._interpolation)
def plot_splined_contours(fname, label=None, colors=None, islog=False, linestyle='-', num_spline_points=100): data = np.loadtxt(fname, skiprows=1) if islog: lnL = data[:, 2] else: lnL = np.log(data[:, 2]) lnL = -2* (lnL - np.max(lnL)) #tck = interpolate.bisplrep(data[:, 0], data[:, 1], lnL, s=0) tck = interpolate.bisplrep(data[:, 0], data[:, 1], lnL, s=1) num_spline_points = complex(0, num_spline_points) Q_args, N_args = np.mgrid[data[0, 0]:data[-1, 0]:num_spline_points, data[0, 1]:data[-1, 1]:num_spline_points] lnL_splined = interpolate.bisplev(Q_args[:, 0], N_args[0, :], tck) my_levels = np.array([0.1, 2.3, 6.17, 11.8]) a = plt.contour(Q_args, N_args, lnL_splined, my_levels, colors=colors, linestyles=linestyle) if label is not None: plt.plot(Q_args[int(abs(num_spline_points))/2, 0], N_args[0, int(abs(num_spline_points))/2], linestyle, color=colors, label=label) return a
def createDatasetForGene(self, gene_ind, plot = False): if gene_ind not in [3,4,5,6,7]: raise Exception("Wrong gene") '''use only wt data for now''' data = self.dp.normData[:,:,0,:] x_range = np.linspace(0, data.shape[2]-1, data.shape[2]) t_range = np.linspace(0, data.shape[0]-1, data.shape[0]) xv, tv = np.meshgrid(x_range, t_range) x = xv.flatten() t = tv.flatten() z = data[:,gene_ind,:].flatten() spdat = ip.bisplrep(x,t,z,s=5) t_der = ip.bisplev(x_range, t_range, spdat, dx=0, dy=1) x_der2 = ip.bisplev(x_range, t_range, spdat, dx=2, dy=0) input_list = [] for g in xrange(7): input_list.append(data[:,g,:].flatten()) input_list.append(x_der2.T.flatten()) input_list = np.rollaxis(np.array(input_list), 1, 0) output_list, self.omax, self.omin = self.normalize(t_der.T.flatten(), -0.9, 0.9) if plot is True: fig = plt.figure() ax = fig.add_subplot(221, projection='3d') ax.plot_surface(xv, tv, t_der.T) ax = fig.add_subplot(222, projection='3d') ax.plot_surface(xv, tv, x_der2.T) ax = fig.add_subplot(223, projection='3d') x_range = np.linspace(0, data.shape[2]-1, 200) t_range = np.linspace(0, data.shape[0]-1, 200) xv, tv = np.meshgrid(x_range, t_range) plt_data = ip.bisplev(x_range, t_range, spdat) ax.plot_surface(xv, tv, plt_data.T) ax = fig.add_subplot(224) ax.hist(t_der.flatten(), bins=40) plt.show() exit() return input_list, output_list
def contour_logL(xvals, yvals, logL, resample=False): """ draw 1, 2, and 3-sigma contours from a gridded log-likelihood """ #resample logL if resample: x,y = np.meshgrid(xvals, yvals) x.resize(x.size) y.resize(y.size) logL = logL.copy() logL.resize(logL.size) tck = interpolate.bisplrep(x, y, logL) xvals = np.linspace(xvals[0], xvals[-1], 20) yvals = np.linspace(yvals[0], yvals[-1], 20) logL = interpolate.bisplev(xvals, yvals, tck) #normalize logL: start by making the max logL=0 L = np.exp(logL - logL.max()) L /= L.sum() #assume a well-behaved peak. Find 1-, 2-, and 3-sigma values Llist = L.copy().reshape(L.size) Llist.sort() levels = Llist.cumsum() i1 = levels.searchsorted(1 - 0.63) i2 = levels.searchsorted(1 - 0.95) i3 = levels.searchsorted(1 - 0.997) v1 = Llist[i1] v2 = Llist[i2] v3 = Llist[i3] pylab.contour(xvals, yvals, L, [v1,v2,v3])
def __calc_surface(self, kx, ky): self.tck = interpolate.bisplrep(self.Knots[:,0],self.Knots[:,1],self.Knots[:,2], kx=kx, ky=ky)
# Modell in Array ueberfuehren: for step in model: stepcat = [] thislayer = step.split() thislayer = stringList2intList(thislayer) thislayer = np.array(thislayer) layered = np.hstack((layered,thislayer)) layered = layered.reshape(int(numlayers), int(numsteps)) X,Z = np.meshgrid(horizontal, depths) X = np.ravel(X ) Z = np.ravel(Z) print 'splineinterp ... rep' Vsplinerep = bisplrep(Z,X,layered, s=1, kx=3, ky=3) print 'splineinterp ... rev' # spline auf grosse Matrix: splined = bisplev(newz, newx, tck=Vsplinerep) # Gradientenfeld berechnen # 50, 30 sampling interval xgradfield, zgradfield = np.gradient(splined) ################################# receiver = [1800, 0] ######## ODE solver ######## # Anfangswert:
# INTERPOLATE SURFACE ONTO REGULAR GRID AND SMOOTH # ------------------------------------------------ if args.int: print "Smoothing surface..." xx_interpolated, yy_interpolated = np.mgrid[args.w[0]:args.w[1]:args.i[0], args.w[2]:args.w[3]:args.i[1]] zz_interpolated = griddata((x, y), d, (xx_interpolated, yy_interpolated), method='linear') xx_interpolated = xx_interpolated[1:-1, 1:-1] yy_interpolated = yy_interpolated[1:-1, 1:-1] zz_interpolated = zz_interpolated[1:-1, 1:-1] zz_interpolated = medfilt(zz_interpolated, args.m) it = interpolate.bisplrep(xx_interpolated, yy_interpolated, zz_interpolated, kx=args.o, ky=args.o, s=args.s) fitted_s = copy.deepcopy(zz_interpolated) for idx_j, row in enumerate(xx_interpolated): for idx_i, col in enumerate(row): this_x = xx_interpolated[idx_j, idx_i] this_y = yy_interpolated[idx_j, idx_i] fitted_s[idx_j, idx_i] = interpolate.bisplev(this_x, this_y, it) print_stats(zz_interpolated) fig = plt.figure() ax = fig.gca(projection='3d') ax.set_zlim(-20,20) ax.plot_surface(xx_interpolated, yy_interpolated, zz_interpolated, cmap=cm.Reds) plt.show()
def align_volume(self,vidx=0,rad=5): self.logger.info('align_volume: Starting') self.logger.info('align_volume: Getting volume from data store.') avol = np.abs(self.h5.get(self.data_block)[vidx,:,:,:]) avol = np.swapaxes(avol,0,1) self.logger.info('align_volume: Smoothing volume with smoothing kernel of size %d pixels.'%rad) if rad: print time.time() avol = lateral_smooth_3d(avol,rad) print time.time() sys.exit() ndepth,nslow,nfast = avol.shape offset_submatrix = np.zeros((nslow,nfast)) goodness_submatrix = np.zeros((nslow,nfast)) profile = self.profile if len(profile)>ndepth: profile = profile[:ndepth] if ndepth>len(profile): avol = avol[:len(profile),:,:] ndepth,nslow,nfast = avol.shape x = [] y = [] z = [] w = [] for islow in range(nslow): pct_done = int(round(100*float(islow)/float(nslow))) if islow%10==0: self.logger.info('align_volume: Aligning A-scans, volume %d is %d percent done.'%(vidx,pct_done)) for ifast in range(nfast): test = avol[:,islow,ifast] offset,goodness = translation1(profile,test,debug=False) x.append(ifast) y.append(islow) z.append(offset) w.append(goodness) offset_submatrix[islow,ifast] = offset goodness_submatrix[islow,ifast] = goodness fitting = True if fitting: # revisit this later; may be of use ptile = 75 goodness_threshold=np.percentile(w,ptile) self.logger.info('align_volume: Goodness %dth percentile %0.3f used as threshold.'%(ptile,goodness_threshold)) valid = np.where(w>goodness_threshold)[0] x0 = x y0 = y self.logger.info('align_volume: Using %d of %d points (%d percent) for fit.'%(len(valid),len(w),float(len(valid))/float(len(w))*100)) x = np.array(x) y = np.array(y) z = np.array(z) w = np.array(w) x = x[valid] y = y[valid] z = z[valid] w = w[valid] mode='median_filter' if mode=='spline': self.logger.info('Spline fitting surface to A-line axial positions.') tck = bisplrep(x,y,z,w=w,xb=0,xe=nfast-1,yb=0,ye=nslow-1) self.logger.info('Evaluating spline function at A-line coordinates.') fit_surface = bisplev(np.arange(nslow),np.arange(nfast),tck) if mode=='polyfit2d': self.logger.info('Polynomial fitting surface to A-line axial positions.') p = polyfit2d(x,y,z,order=2) self.logger.info('Evaluating polynomial function at A-line coordinates.') xx,yy = np.meshgrid(np.arange(nfast),np.arange(nslow)) fit_surface = polyval2d(xx,yy,p) if mode=='median_filter': # This is a dumb way to fit. Use the goodness matrix, dummy, perhaps with 2D splines! self.logger.info('Median filtering to create a smoothed offset surface.') slow_height = np.median(offset_submatrix,axis=1) plt.figure() plt.plot(slow_height) plt.figure() debias = (offset_submatrix.T-slow_height).T plt.figure() plt.imshow(debias) plt.colorbar() fit_surface_1 = median_filter(offset_submatrix,(3,3)) plt.figure() plt.imshow(fit_surface_1) plt.title('straight fit') plt.colorbar() fit_surface_2 = (median_filter(debias,(3,3)).T+slow_height).T plt.figure() plt.imshow(fit_surface_2) plt.title('debiased fit') plt.colorbar() plt.show() sys.exit() if mode=='interp2d': self.logger.info('Using interp2d to create a smoothed offset surface.') interpolation_function = interp2d(x,y,z) fit_surface = interpolation_function(x0,y0) print fit_surface print fit_surface.shape # print fit_surface # print fit_surface.shape if True: clim = np.min(offset_submatrix),np.max(offset_submatrix) plt.figure() plt.imshow(offset_submatrix,interpolation='none',clim=clim) plt.colorbar() plt.figure() plt.imshow(fit_surface,interpolation='none',clim=clim) plt.colorbar() plt.figure() plt.imshow(offset_submatrix-fit_surface,interpolation='none') plt.colorbar() plt.show() #goodness_used = np.zeros(goodness_submatrix.shape) #goodness_used[np.where(goodness_submatrix>goodness_threshold)] = 1.0 else: fit_surface = offset_submatrix return offset_submatrix,goodness_submatrix,fit_surface
def aproximate_terrain(self): """ Try to aproximate terrain with bspline surface """ # dx = (self.size_x / 2) * self.dx # dy = (self.size_y / 2) * self.dy # tot_fp = 0 # X = [] # Y = [] # Z = [] # for i in range(0, self.size_x/2 + 2): # for j in range(0, self.size_y/2 + 2): # X.append(self.grid[(i,j)][0]) # Y.append(self.grid[(i,j)][1]) # Z.append(self.grid[(i,j)][2]) # # kx an ky are degrees of polynoms # # s is smoothness # tck,fp,ior,msg = interpolate.bisplrep(X, Y, Z, kx=5, ky=5, full_output=1) # self.tck[(self.min_x, self.min_y, self.min_x + dx, self.min_y + dy)] = tck # tot_fp += fp # print tck, fp # X = [] # Y = [] # Z = [] # for i in range(self.size_x/2 - 2, self.size_x): # for j in range(0, self.size_y/2 + 2): # X.append(self.grid[(i,j)][0]) # Y.append(self.grid[(i,j)][1]) # Z.append(self.grid[(i,j)][2]) # tck,fp,ior,msg = interpolate.bisplrep(X, Y, Z, kx=5, ky=5, full_output=1) # self.tck[(self.min_x + dx, self.min_y, self.max_x, self.min_y + dy)] = tck # tot_fp += fp # print tck, fp # X = [] # Y = [] # Z = [] # for i in range(0, self.size_x/2 + 2): # for j in range(self.size_y/2 - 2, self.size_y): # X.append(self.grid[(i,j)][0]) # Y.append(self.grid[(i,j)][1]) # Z.append(self.grid[(i,j)][2]) # tck,fp,ior,msg = interpolate.bisplrep(X, Y, Z, kx=5, ky=5, full_output=1) # self.tck[(self.min_x, self.min_y + dy, self.min_x + dx, self.max_y)] = tck # tot_fp += fp # print tck, fp # X = [] # Y = [] # Z = [] # for i in range(self.size_x/2 - 2, self.size_x): # for j in range(self.size_y/2 - 2, self.size_y): # X.append(self.grid[(i,j)][0]) # Y.append(self.grid[(i,j)][1]) # Z.append(self.grid[(i,j)][2]) # tck,fp,ior,msg = interpolate.bisplrep(X, Y, Z, kx=5, ky=5, full_output=1) # self.tck[(self.min_x + dx, self.min_y + dy, self.max_x, self.max_y)] = tck # tot_fp += fp # print tck, fp tck,fp,ior,msg = interpolate.bisplrep(self.tX, self.tY, self.tZ, kx=5, ky=5, full_output=1) self.tck[(self.min_x, self.min_y, self.max_x, self.max_y)] = tck # Compute difference between original terrain data and b-spline surface self.tW = [abs(it[2] - interpolate.bisplev(it[0], it[1], tck)) for it in self.terrain_data] print tck print fp
def SplineGen(beta,lambda_,C,name): beta_shift = np.min(beta) lambda_shift = np.min(lambda_) beta -= beta_shift lambda_ -= lambda_shift betagrid, lambdagrid = MakeGrid(beta,lambda_) tck = interpolate.bisplrep(betagrid,lambdagrid,C,s=1e-2) #spline order knots_x = tck[0] knots_y = tck[1] checkpoints = tck[2] p = tck[3] q = tck[4] assert(p==3) assert(q==3) #P is size mxn, row major representation m = len(knots_x)-p-1 n = len(knots_y)-q-1 P = tck[2].reshape(m,n) Pline = tck[2] Px, Ux = Dx(P,knots_x,m,n,p) Py, Uy = Dy(P,knots_y,m,n,q) Pxx, Uxx = Dx(Px.reshape(m-1,n), Ux, m-1 ,n , p-1) Pyy, Uyy = Dy(Py.reshape(m,n-1), Uy, m ,n-1 , q-1) Pxy, _ = Dy(Px.reshape(m-1,n), knots_y, m-1 ,n , q) #### Write SplineData.h #### fileobj = open(name+'.h','w') ## write variables ## varDictionary = {'n': [n, 'int'], 'm': [m, 'int'], 'p': [p, 'int'], 'q': [q, 'int'], 'x_shift': [beta_shift, 'float'], 'y_shift': [lambda_shift, 'float'], 'knots_x': [list(knots_x),'float'], 'length_knots_x': [len(knots_x), 'int'], 'knots_y': [list(knots_y),'float'], 'length_knots_y': [len(knots_y), 'int'], 'P': [list(Pline), 'float'], 'length_P': [len(Pline), 'int'], 'Px': [list(Px), 'float'], 'length_Px': [len(Px), 'int'], 'Py': [list(Py), 'float'], 'length_Py': [len(Py), 'int'], 'Ux': [list(Ux), 'float'], 'length_Ux': [len(Ux), 'int'], 'Uy': [list(Uy), 'float'], 'length_Uy': [len(Uy), 'int'], 'Uxx': [list(Uxx), 'float'], 'length_Uxx': [len(Uxx), 'int'], 'Uyy': [list(Uyy), 'float'], 'length_Uyy': [len(Uyy), 'int'], 'Pxx': [list(Pxx), 'float'], 'length_Pxx': [len(Pxx), 'int'], 'Pyy': [list(Pyy), 'float'], 'length_Pyy': [len(Pyy), 'int'], 'Pxy': [list(Pxy), 'float'], 'length_Pxy': [len(Pxy), 'int']} for key in varDictionary.keys(): writeData(key, varDictionary[key][0], varDictionary[key][1], fileobj) fileobj.close() return tck, betagrid, lambdagrid
#lambda_ /= np.max(lambda_) Cp = np.maximum(-0.0,mat['Cp_Data']) beta = beta.reshape(beta.shape[1],) lambda_ = lambda_.reshape(lambda_.shape[1],) def MakeGrid(argx,argy): gridx = [[argx[i] for j in range(len(argy))] for i in range(len(argx))] gridy = [[argy[i] for j in range(len(argx))] for i in range(len(argy))] return np.array(gridx).T, np.array(gridy) betagrid, lambdagrid = MakeGrid(beta,lambda_) tck = interpolate.bisplrep(betagrid,lambdagrid,Cp,s=1e-2) #spline order knots_x = tck[0] knots_y = tck[1] checkpoints = tck[2] p = tck[3] q = tck[4] assert(p==3) assert(q==3) #P is size mxn, row major representation m = len(knots_x)-p-1
data1 = [] sn_list = [SNe[band][id] for id in randints] masks = [list0[band] == sn for sn in sn_list] data1 = concatenate([compress(m, data0[band], axis=1) for m in masks], axis=1) list1 = concatenate([list0[band][m] for m in masks]) list1 = list1.tolist() CSPtemp.dm15tempc.set_data(band, data1, list1) z,ez,mask = temp.eval(band, ts, mag=0) z_mat[band].append(z) ez_mat[band].append(ez) x = ravel(array(t_mat)) y = ravel(array(dm15_mat)) smooths = {} for band in bands: smooths[band] = 0.1 z = ravel(array(z_mat[band])) ez = ravel(array(ez_mat[band])) print "fitting band ",band print inter.__file__ try: tck[band].append(inter.bisplrep(x, y, z, w=1.0/ez, s=smooths[band]*len(x))) except: continue tck["e_"+band].append(inter.bisplrep(x, y, ez, kx=1, ky=1, s=0.0*len(x))) f = open(tck_file, 'w') pickle.dump(tck, f) f.close()
def list_plot3d_tuples(v, interpolation_type, texture, **kwds): r""" A 3-dimensional plot of a surface defined by the list `v` of points in 3-dimensional space. INPUT: - ``v`` - something that defines a set of points in 3 space, for example: - a matrix This will be if using an interpolation type other than 'linear', or if using ``num_points`` with 'linear'; otherwise see :func:`list_plot3d_matrix`. - a list of 3-tuples - a list of lists (all of the same length, under same conditions as a matrix) - ``texture`` - (default: "automatic", a solid light blue) OPTIONAL KEYWORDS: - ``interpolation_type`` - 'linear', 'nn' (natural neighbor), 'spline' 'linear' will perform linear interpolation The option 'nn' will interpolate by using natural neighbors. The value for an interpolation point is estimated using weighted values of the closest surrounding points in the triangulation. The option 'spline' interpolates using a bivariate B-spline. When v is a matrix the default is to use linear interpolation, when v is a list of points the default is nearest neighbor. - ``degree`` - an integer between 1 and 5, controls the degree of spline used for spline interpolation. For data that is highly oscillatory use higher values - ``point_list`` - If point_list=True is passed, then if the array is a list of lists of length three, it will be treated as an array of points rather than a `3\times n` array. - ``num_points`` - Number of points to sample interpolating function in each direction. By default for an `n\times n` array this is `n`. - ``**kwds`` - all other arguments are passed to the surface function OUTPUT: a 3d plot EXAMPLES: All of these use this function; see :func:`list_plot3d` for other list plots:: sage: pi = float(pi) sage: m = matrix(RDF, 6, [sin(i^2 + j^2) for i in [0,pi/5,..,pi] for j in [0,pi/5,..,pi]]) sage: list_plot3d(m, texture='yellow', interpolation_type='linear', num_points=5) # indirect doctest Graphics3d Object :: sage: list_plot3d(m, texture='yellow', interpolation_type='spline', frame_aspect_ratio=[1, 1, 1/3]) Graphics3d Object :: sage: show(list_plot3d([[1, 1, 1], [1, 2, 1], [0, 1, 3], [1, 0, 4]], point_list=True)) :: sage: list_plot3d([(1, 2, 3), (0, 1, 3), (2, 1, 4), (1, 0, -2)], texture='yellow', num_points=50) Graphics3d Object """ from matplotlib import tri, delaunay import numpy import scipy from random import random from scipy import interpolate from plot3d import plot3d if len(v)<3: raise ValueError("We need at least 3 points to perform the interpolation") x = [float(p[0]) for p in v] y = [float(p[1]) for p in v] z = [float(p[2]) for p in v] # If the (x,y)-coordinates lie in a one-dimensional subspace, the # matplotlib Delaunay code segfaults. Therefore, we compute the # correlation of the x- and y-coordinates and add small random # noise to avoid the problem if needed. corr_matrix = numpy.corrcoef(x, y) if corr_matrix[0, 1] > 0.9 or corr_matrix[0, 1] < -0.9: ep = float(.000001) x = [float(p[0]) + random()*ep for p in v] y = [float(p[1]) + random()*ep for p in v] # If the list of data points has two points with the exact same # (x,y)-coordinate but different z-coordinates, then we sometimes # get segfaults. The following block checks for this and raises # an exception if this is the case. # We also remove duplicate points (which matplotlib can't handle). # Alternatively, the code in the if block above which adds random # error could be applied to perturb the points. drop_list = [] nb_points = len(x) for i in range(nb_points): for j in range(i+1, nb_points): if x[i] == x[j] and y[i] == y[j]: if z[i] != z[j]: raise ValueError("Two points with same x,y coordinates and different z coordinates were given. Interpolation cannot handle this.") elif z[i] == z[j]: drop_list.append(j) x = [x[i] for i in range(nb_points) if i not in drop_list] y = [y[i] for i in range(nb_points) if i not in drop_list] z = [z[i] for i in range(nb_points) if i not in drop_list] xmin = float(min(x)) xmax = float(max(x)) ymin = float(min(y)) ymax = float(max(y)) num_points = kwds['num_points'] if 'num_points' in kwds else int(4*numpy.sqrt(len(x))) #arbitrary choice - assuming more or less a nxn grid of points # x should have n^2 entries. We sample 4 times that many points. if interpolation_type == 'linear': T = tri.Triangulation(x, y) f = tri.LinearTriInterpolator(T, z) j = numpy.complex(0, 1) from parametric_surface import ParametricSurface def g(x, y): z = f(x, y) return (x, y, z) G = ParametricSurface(g, (list(numpy.r_[xmin:xmax:num_points*j]), list(numpy.r_[ymin:ymax:num_points*j])), texture=texture, **kwds) G._set_extra_kwds(kwds) return G if interpolation_type == 'nn' or interpolation_type =='default': T=delaunay.Triangulation(x,y) f=T.nn_interpolator(z) f.default_value=0.0 j=numpy.complex(0,1) vals=f[ymin:ymax:j*num_points,xmin:xmax:j*num_points] from parametric_surface import ParametricSurface def g(x,y): i=round( (x-xmin)/(xmax-xmin)*(num_points-1) ) j=round( (y-ymin)/(ymax-ymin)*(num_points-1) ) z=vals[int(j),int(i)] return (x,y,z) G = ParametricSurface(g, (list(numpy.r_[xmin:xmax:num_points*j]), list(numpy.r_[ymin:ymax:num_points*j])), texture=texture, **kwds) G._set_extra_kwds(kwds) return G if interpolation_type == 'spline': from plot3d import plot3d kx = kwds['kx'] if 'kx' in kwds else 3 ky = kwds['ky'] if 'ky' in kwds else 3 if 'degree' in kwds: kx = kwds['degree'] ky = kwds['degree'] s = kwds['smoothing'] if 'smoothing' in kwds else len(x)-numpy.sqrt(2*len(x)) s = interpolate.bisplrep(x, y, z, [int(1)]*len(x), xmin, xmax, ymin, ymax, kx=kx, ky=ky, s=s) f = lambda x,y: interpolate.bisplev(x, y, s) return plot3d(f, (xmin, xmax), (ymin, ymax), texture=texture, plot_points=[num_points, num_points], **kwds)