def test_bisplev(): from fluids.numerics import bisplev as my_bisplev from scipy.interpolate import bisplev tck = [np.array([0.0, 0.0, 0.0, 0.0, 0.0213694, 0.0552542, 0.144818, 0.347109, 0.743614, 0.743614, 0.743614, 0.743614]), np.array([0.0, 0.0, 0.25, 0.5, 0.75, 1.0, 1.0]), np.array([1.0001228445490002, 0.9988161050974387, 0.9987070557919563, 0.9979385859402731, 0.9970983069823832, 0.96602540121758, 0.955136014969614, 0.9476842472211648, 0.9351143114374392, 0.9059649602818451, 0.9218915266550902, 0.9086000082864022, 0.8934758292610783, 0.8737960765592091, 0.83185251064324, 0.8664296734965998, 0.8349705397843921, 0.809133298969704, 0.7752206120745123, 0.7344035693011536, 0.817047920445813, 0.7694560150930563, 0.7250979336267909, 0.6766754605968431, 0.629304180420512, 0.7137237030611423, 0.6408238328161417, 0.5772000233279148, 0.504889627280836, 0.440579886434288, 0.6239736474980684, 0.5273646894226224, 0.43995388722059986, 0.34359277007615313, 0.26986439252143746, 0.5640689738382749, 0.4540959882735219, 0.35278120580740957, 0.24364672351604122, 0.1606942128340308]), 3, 1] my_tck = [tck[0].tolist(), tck[1].tolist(), tck[2].tolist(), tck[3], tck[4]] xs = np.linspace(0, 1, 10) zs = np.linspace(0, 1, 10) ys_scipy = bisplev(xs, zs, tck) ys = my_bisplev(xs, zs, my_tck) assert_allclose(ys, ys_scipy) ys_scipy = bisplev(0.5, .7, tck) ys = my_bisplev(.5, .7, my_tck) assert_allclose(ys, ys_scipy)
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 make_pixel_lut(self, dims): """ Generate an x and y image which maps the array indices into floating point array indices (to be corrected for pixel size later) returns FIXME - check they are the right way around add some sort of known splinefile testcase """ # Cache the value in case of multiple calls if self.pixel_lut is None: x_im = numpy.outer(numpy.arange(dims[0]), numpy.ones(dims[1])) y_im = numpy.outer(numpy.ones(dims[0]), numpy.arange(dims[1])) # xcor is tck2 x_im = numpy.add( x_im, bisplev( numpy.arange(dims[1]), numpy.arange(dims[0]), self.tck2 ).T, x_im) # ycor is tck1 y_im = numpy.add( y_im, bisplev( numpy.arange(dims[1]), numpy.arange(dims[0]), self.tck1 ).T, y_im) self.pixel_lut = x_im, y_im return self.pixel_lut
def get_emission(self, beam, ne, mass_b, Ti, file_number): """ Get the emission coefficient for a given density, beam energy, and temperature. The ADAS database store the data as two array, for putting them together, we do a first interpolation for the 2D array (as a function of density and beam energy) and after we do a scaling with the temperature. :param float beam: Beam energy (eV) :param float or np.array[N] ne: Electron density density :param float mass_b: mass of a neutral particle in the beam (amu) :param float or np.array[N] Ti: Ion temperature (should be of the same lenght than ne) :param int file_number: File number wanted (choosen by beam.py) :returns: Emission coefficient :rtype: np.array[ne.shape] """ beam = np.log(beam / mass_b) ne = np.log(ne) Ti = np.log(Ti) if not isinstance(ne, float): coef = np.zeros(len(ne)) for i in range(len(ne)): coef[i] = interpolate.bisplev(beam, ne[i], self.emis_tck_dens[file_number]) else: coef = interpolate.bisplev(beam, ne, self.emis_tck_dens[file_number]) coef = coef * interpolate.splev(Ti, self.emis_tck_temp[file_number]) return coef
def __interpolateParameters__(self, height, latitude, tkcDeep, tkcUpper): # Preallocate result array and start looping through all values isScalar = not util.isArray(height) results = [] if isScalar: results = [0] height = [height] latitude = [latitude] else: results = np.zeros(height.shape) for i in range(0, len(height)): # Check where the height is with respect to the interpolation limits if height[i] <= tkcDeep[0][-1]: results[i] = scp_ip.splev(height[i], tkcDeep) elif height[i] >= tkcUpper[0][0]: results[i] = scp_ip.bisplev(height[i], latitude[i], tkcUpper) else: # Interpolate between the lower and upper interpolating functions (do # so linearly for now) low = scp_ip.splev(tkcDeep[0][-1], tkcDeep) high = scp_ip.bisplev(tkcUpper[0][0], latitude[i], tkcUpper) results[i] = low + (high - low) * (height[i] - tkcDeep[0][-1]) / \ (tkcUpper[0][0] - tkcDeep[0][-1]) if isScalar: return results[0] return results
def make_pixel_lut(self, dims): """ Generate an x and y image which maps the array indices into floating point array indices (to be corrected for pixel size later) returns FIXME - check they are the right way around add some sort of known splinefile testcase """ # Cache the value in case of multiple calls if self.pixel_lut is None: x_im = numpy.outer(range(dims[0]), numpy.ones(dims[1])) y_im = numpy.outer(numpy.ones(dims[0]), range(dims[1])) # xcor is tck2 x_im = numpy.add( x_im, bisplev( range(dims[1]), range(dims[0]), self.tck2 ).T, x_im) # ycor is tck1 y_im = numpy.add( y_im, bisplev( range(dims[1]), range(dims[0]), self.tck1 ).T, y_im) self.pixel_lut = x_im, y_im return self.pixel_lut
def __call__(self, **kwargs): r""" Parameters ---------- diameter : float or array, shape (N) cylinder (axon) diameter in meters. Returns ------- Pgamma : float or array, shape (N) probability of cylinder diameter for given alpha and beta. """ alpha = kwargs.get('alpha', self.alpha) beta = kwargs.get('beta', self.beta) gamma_dist = stats.gamma(alpha, scale=beta) start_point = interpolate.bisplev(alpha, beta, self.start_interpolator) end_point = interpolate.bisplev(alpha, beta, self.end_interpolator) start_point = max(start_point, 1e-8) radii = np.linspace(start_point, end_point, self.Nsteps) normalization = self.norm_func(radii) radii_pdf = gamma_dist.pdf(radii) radii_pdf_area = radii_pdf * normalization radii_pdf_normalized = (radii_pdf_area / np.trapz(x=radii, y=radii_pdf_area)) return radii, radii_pdf_normalized
def apar_intrp(x,y,z): dx = dx_xy[x,y] dy = dy_xy[x,y] # Interpolating down the y-axis (along the field) y_vals = np.array(range(ny), dtype=float) for j in range(len(y_vals)): y_vals[j] = interpolate.bisplev(x,z,tck[j]) # print y_vals, 'sdf' dy_coeffs = interpolate.splrep(range(ny), y_vals, k=3) # Interpolating along the slices of data # intrp = interpolate.RectBivariateSpline(range(nx),range(nz),data[:,y,:],kx=3,ky=3) # tx,ty = intrp.get_knots() # tck = (tx,ty,intrp.get_coeffs(),3,3) # print y, int(y), np.shape(data[:,y,:]) # From the cubic spline coefficients, returns derivatives # print 's', int(np.rint(y)), int(y) dervs = ( interpolate.bisplev(x,z,tck[int(np.rint(y))], dx=1, dy=0)/dx, interpolate.splev(y,dy_coeffs,der=1)/dy, interpolate.bisplev(x,z,tck[int(np.rint(y))], dx=0, dy=1)/dz ) return dervs
def _get_spline_coords_interpolation(self, approx=None): if type(approx) == type(None): approx = self.approx tx = np.clip(np.arange(self.n_spl + 3 + 1) - 3, 0, self.n_spl - 3) intact_x = si.bisplev( np.linspace(0, self.n_spl - 3, approx), np.linspace(0, self.n_spl - 3, approx), (tx, tx, self.norm_matrix[:, :, :].ravel(), 3, 3)) intact_y = si.bisplev( np.linspace(0, self.n_spl - 3, approx), np.linspace(0, self.n_spl - 3, approx), (tx, tx, np.transpose(self.norm_matrix[::-1, :, :], (0, 2, 1)).ravel(), 3, 3)) #print(intact_x) interp_intact_x = si.interp1d(intact_x[0], np.linspace(0, self.n_spl - 3, approx), kind='cubic', fill_value="extrapolate") interp_intact_y = si.interp1d(intact_y[0], np.linspace(0, self.n_spl - 3, approx), kind='cubic', fill_value="extrapolate") return interp_intact_x, interp_intact_y
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_z(self, pres, temp, dp=False, dt=False): if hasattr(pres, "__iter__") is False: pres = [pres] temp = [temp] result = np.zeros(len(pres), np.float32) for i, (p, t) in enumerate(zip(np.array(pres, dtype=np.float32), np.array(temp, dtype=np.float32))): if dp: result[i] = interpolate.bisplev(p, t, self.tck, dx=1) elif dt: result[i] = interpolate.bisplev(p, t, self.tck, dy=1) else: result[i] = interpolate.bisplev(p, t, self.tck) return result
def constraint(optimizer, xi, eta, c): ''' Return a vector of Jacobian Determinant function evaluations over the tensor-product points resulting from ``xi`` and ``eta``. The knotvector(s) are taken from ``g`` and the control points are given by ``c`` (hence not necessarily g.x). ''' g = optimizer._g assert len(g) == g.targetspace == 2, NotImplementedError x, y = np.array_split(c, 2) _bsplev = lambda c, **kwargs: bisplev(xi, eta, optimizer._tck(c), **kwargs ).ravel() x_xi = _bsplev(x, dx=1) x_eta = _bsplev(x, dy=1) y_xi = _bsplev(y, dx=1) y_eta = _bsplev(y, dy=1) return x_xi * y_eta - x_eta * y_xi
def evaluatePartialDerivativeV(self, x, y): result = np.empty(self.coeffElems) for i in range(self.coeffElems): result[i] = interpolate.bisplev(x, y, self.tcks[i], dy=1) return result
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 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 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 queryDepth(self, xq, yq): #return 1 data point depth = bisplev(xq, yq, self.tck) if np.isnan(depth): print "NaN returned for depth" else: return max(self.minz,min(depth, self.maxz))
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 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 fct(x_eval, y_eval): if k == 0: vec_x = np.maximum( np.ceil( np.array(x_eval, ndmin=1, copy=False) * nSpline1d) - 1, 0).astype(int) vec_y = np.maximum( np.ceil( np.array(y_eval, ndmin=1, copy=False) * nSpline1d) - 1, 0).astype(int) return (theta_t.reshape( (nSpline1d, nSpline1d))[vec_x][:, vec_y]) if k == 1: x_eval_order = np.argsort(x_eval) y_eval_order = np.argsort(y_eval) fct_eval_order = interpolate.bisplev( x=np.array(x_eval, ndmin=1, copy=False)[x_eval_order], y=np.array(y_eval, ndmin=1, copy=False)[y_eval_order], tck=(t, t, theta_t, k, k), dx=0, dy=0) return (eval('fct_eval_order' + (('[np.argsort(x_eval_order)]' + ('[:,' if len(y_eval_order) > 1 else '') ) if len(x_eval_order) > 1 else ('[' if len(y_eval_order) > 1 else '')) + ('np.argsort(y_eval_order)]' if len(y_eval_order) > 1 else '')))
def __call__(self, c, **kwargs): """ Call is overloaded to simply yield the mapping evaluated in c (or its derivatives) """ return interpolate.bisplev(*self._quad, self._tck(c), **kwargs)
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 evaluate(self, x, y): result = np.empty(self.coeffElems) for i in range(self.coeffElems): result[i] = interpolate.bisplev(x, y, self.tcks[i]) return result
def transferFactorCalculator (rho, pt): # Scipy bi-linear spline representation data object. # Cf. [https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.interpolate.bisplrep.html] tck = [ np.array([ 1.25, 1.25, 1.25, 1.25, 6.75, 6.75, 6.75, 6.75]), # Knots, x np.array([ 450., 450., 450., 450., 1950., 1950., 1950., 1950.]), # Knots, y np.array([ 0.18261346, 0.2174425 , 0.37762574, 0.10284952, 0.26108651, # Spline coefficients -0.14541588, 0.05701827, 0.27361263, 0.54531852, 0.77814774, 0.2479843 , 0.23509468, -0.04597834, -0.47218929, 0.01928886, -0.09066243]), 3, # Spline degree, x 3] # Spline degree, y # Limits for the transfer factor map. Return zero if outside. limits = { 'rho': ( 1., 7.), 'pt': (400., 2000.) } # Check limits. if (limits['rho'][0] <= rho <= limits['rho'][1]) and \ (limits['pt'] [0] <= pt <= limits['pt'] [1]): # Calculate and return transfer factor. return interpolate.bisplev(rho, pt, tck) # Return fallback value. return 0.
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 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 get_zdata(self, xydata): return_value = [] for each_xy in xydata: each_z = si.bisplev(each_xy[0], each_xy[1], self._tck) return_value.append(each_z) return np.array(return_value) # return si.bisplev(xydata[:, 0], xydata[:, 1], self._tck)
def bivariate_evaluate(spline_dict, czvals, evals): """Evaluate the bivariate spline to get the flux.""" fluxes = OrderedDict() for nu in spline_dict.iterkeys(): fluxes[nu] = np.power( 10., interpolate.bisplev(czvals, np.log10(evals), spline_dict[nu])) return fluxes
def derivatives(self, alpha, Re): # note: direct call to bisplev will be unnecessary with latest scipy update (add derivative method) tck_cl = self.cl_spline.tck[:3] + self.cl_spline.degrees # concatenate lists tck_cd = self.cd_spline.tck[:3] + self.cd_spline.degrees dcl_dalpha = bisplev(alpha, Re, tck_cl, dx=1, dy=0) dcd_dalpha = bisplev(alpha, Re, tck_cd, dx=1, dy=0) if self.one_Re: dcl_dRe = 0.0 dcd_dRe = 0.0 else: dcl_dRe = bisplev(alpha, Re, tck_cl, dx=0, dy=1) dcd_dRe = bisplev(alpha, Re, tck_cd, dx=0, dy=1) return dcl_dalpha, dcl_dRe, dcd_dalpha, dcd_dRe
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 loggtracks(masslimit,location,fileroot, metalstr, plot=True): #This is an array of all the masses that are part of the filenames massarrstr=['120.0', '85.0', '60.0', '40.0', '25.0', '20.0', '15.0', '12.0', '9.0', '7.0', '5.0', '4.0', '3.0', '2.5', '2.0', '1.7', '1.5', '1.25', '1.0', '0.9'] #this is for each mass, read in the file and plot the evolutionary tracks for imass in range(masslimit): filemass = massarrstr[imass] filename = location+fileroot+filemass DataIn = np.genfromtxt(filename, dtype="float", unpack=True) time = DataIn[3,:] timesec = time*365*24*60*60 Teff = 10**DataIn[ 6,:] logTeff = [math.log10(jj) for jj in Teff] L = 10**DataIn[ 5,:]*Lsun Rsquared = (L/(4*math.pi*sigma*Teff**4)) Mass = DataIn[4,:]*Msun surfaceGrav = Mass*G/(Rsquared) logsurfaceGrav = [math.log10(ii) for ii in surfaceGrav] if plot == True: fadeplot(Teff, logsurfaceGrav, time) q0s = np.zeros(len(Teff)) for timestep in range(len(Teff)): q0s[timestep] = interpolate.bisplev(Teff[timestep],logsurfaceGrav[timestep],gridq0s) for kk in range(len(q0s)): if q0s[kk] < 0: q0s[kk] = -q0s[kk] q0s[kk] = np.log10(q0s[kk]) totalQ0s = np.trapz(q0s,timesec) logtotalQ0s = np.log10(totalQ0s) #print "Mass of: " + massarr[imass]+" Produces "+str(totalQ0s)+" Photons" logq0ints[imass] = totalQ0s #plt.plot(timesec/timesec[-1], q0s, 'k') titlestr= "Z= "+metalstr #plt.title(titlestr) #plt.xlabel("Stellar Lifetime") #plt.ylabel("Photons / Second") #plt.ylim([0,2e50]) #plt.show() return;
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 loggtracks(masslimit, location, fileroot, metalstr, plot=True): #This is an array of all the masses that are part of the filenames massarrstr = [ '120.0', '85.0', '60.0', '40.0', '25.0', '20.0', '15.0', '12.0', '9.0', '7.0', '5.0', '4.0', '3.0', '2.5', '2.0', '1.7', '1.5', '1.25', '1.0', '0.9' ] #this is for each mass, read in the file and plot the evolutionary tracks for imass in range(masslimit): filemass = massarrstr[imass] filename = location + fileroot + filemass DataIn = np.genfromtxt(filename, dtype="float", unpack=True) time = DataIn[3, :] timesec = time * 365 * 24 * 60 * 60 Teff = 10**DataIn[6, :] logTeff = [math.log10(jj) for jj in Teff] L = 10**DataIn[5, :] * Lsun Rsquared = (L / (4 * math.pi * sigma * Teff**4)) Mass = DataIn[4, :] * Msun surfaceGrav = Mass * G / (Rsquared) logsurfaceGrav = [math.log10(ii) for ii in surfaceGrav] if plot == True: fadeplot(Teff, logsurfaceGrav, time) q0s = np.zeros(len(Teff)) for timestep in range(len(Teff)): q0s[timestep] = interpolate.bisplev(Teff[timestep], logsurfaceGrav[timestep], gridq0s) for kk in range(len(q0s)): if q0s[kk] < 0: q0s[kk] = -q0s[kk] q0s[kk] = np.log10(q0s[kk]) totalQ0s = np.trapz(q0s, timesec) logtotalQ0s = np.log10(totalQ0s) #print "Mass of: " + massarr[imass]+" Produces "+str(totalQ0s)+" Photons" logq0ints[imass] = totalQ0s #plt.plot(timesec/timesec[-1], q0s, 'k') titlestr = "Z= " + metalstr #plt.title(titlestr) #plt.xlabel("Stellar Lifetime") #plt.ylabel("Photons / Second") #plt.ylim([0,2e50]) #plt.show() return
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 correct(self, xin, yin): """ Transform x,y in raw image coordinates into x,y of an idealised image. Returns a tuple (x,y), expects a pair of floats as arguments """ if self.orientation == "edf": xcor = xin + bisplev(yin, xin, self.tck2) ycor = yin + bisplev(yin, xin, self.tck1) else: # fit2d does a flip raise Exception("Spline orientations must be edf, convert " "your image to edf and remake the spline") # Unreachable code - we no longer accept this complexity # it means the spline file for ImageD11 bruker images # is not the same as for fit2d. xpos = self.xmax - xin xcor = xin - bisplev(yin, xpos, self.tck2) ycor = yin + bisplev(yin, xpos, self.tck1) return xcor, ycor
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 sample(sgridxy, im, dd=(0,0), hscaling=1. ): xs = sgridxy[0].flatten() ys = sgridxy[1].flatten() assert(xs.size == ys.size) v = np.zeros(xs.size) for i in np.arange(v.size): v[i] = (1./(hscaling**np.sum(dd)))*flt(interpolate.bisplev(xs[i],ys[i], im, dd[0], dd[1]) ) #return v.reshape(sgridxy[0].shape) return v
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 correct(self, xin, yin): """ Transform x,y in raw image coordinates into x,y of an idealised image. Returns a tuple (x,y), expects a pair of floats as arguments """ if self.orientation == "edf": xcor = xin + bisplev(yin, xin, self.tck2) ycor = yin + bisplev(yin, xin, self.tck1) else: # fit2d does a flip raise Exception("Spline orientations must be edf, convert " "your image to edf and remake the spline") # Unreachable code - we no longer accept this complexity # it means the spline file for ImageD11 bruker images # is not the same as for fit2d. # xpos = self.xmax - xin # xcor = xin - bisplev(yin, xpos, self.tck2) # ycor = yin + bisplev(yin, xpos, self.tck1) return xcor, ycor
def _predict(self, X): """The function to predict using the BSpline interpolation. This function is not supposed to be called directly. """ results = [] for ix in range(X.shape[0]): interpolated_y = bisplev(X[ix, 0], X[ix, 1], self.tck).item() # one value returned results.append(interpolated_y) return np.array(results)
def distort(self, xin, yin): """ Distort a pair of points xnew, ynew to find where they would be in a raw image Iterative algorithm... """ yold = yin - bisplev(yin, xin, self.tck1) xold = xin - bisplev(yin, xin, self.tck2) # First guess, assumes distortion is constant ytmp = yin - bisplev(yold, xold, self.tck1) xtmp = xin - bisplev(yold, xold, self.tck2) # Second guess should be better error = math.sqrt((xtmp - xold) * (xtmp - xold) + (ytmp - yold) * (ytmp - yold)) ntries = 0 while error > self.tolerance: ntries = ntries + 1 xold = xtmp yold = ytmp ytmp = yin - bisplev(yold, xold, self.tck1) xtmp = xin - bisplev(yold, xold, self.tck2) error = math.sqrt((xtmp - xold) * (xtmp - xold) + (ytmp - yold) * (ytmp - yold)) # print error,xold,x,yold,y if ntries == 10: raise Exception("Error getting the inverse spline to converge") return xtmp, ytmp
def finterp(band, t, p, param, gen, extrap=False): '''interpolate at time t and param p for param,gen combo.''' load_data(band,param,gen) if param == 'dm15': f = dm15_flux[(band,gen)] ef = dm15_eflux[(band,gen)] else: f = st_flux[(band,gen)] ef = st_eflux[(band,gen)] if len(num.shape(t)) == 0: scalar = 1 else: scalar = 0 t = num.atleast_1d(t) # First the evaluation mtarix: Z = num.atleast_2d(bisplev(t, p, f))[:,0] eZ = num.atleast_2d(bisplev(t, p, ef))[:,0] if not extrap: mask = num.greater_equal(t,f[0][0])*num.less_equal(t,f[0][-1]) mask = mask*num.greater(Z, 0) Z = num.where(mask, Z, 1) eZ = num.where(mask, eZ, -1) else: t1,t2 = get_t_lim(band, param, gen) mask = -num.isnan(Z) # extrapolate lower with t^2 law if num.sometrue(num.less(t,t1)): Tp = bisplev(t1, p, f, dx=1) T = bisplev(t1, p, f) eT = bisplev(t, p, ef) t0 = t1 - 2*T/Tp; a = T/(t1-t0)**2 Z = num.where(num.less(t, t1), a*num.power(t-t0,2), Z) eZ = num.where(num.less(t, t1), eT, eZ) mask = mask*num.greater(Z,0)*num.greater(t, t0) if num.sometrue(num.greater(t, t2)): # extrapolate with m = a*(t-t2)+b Tp = bisplev(t2, p, f, dx=1) T = bisplev(t2, p, f) eT = bisplev(t2, p, ef) b = -2.5*num.log10(T) a = -2.5/num.log(10)/T*Tp f = num.power(10, -0.4*(a*(t-t2)+b)) Z = num.where(num.greater(t, t2), f, Z) eZ = num.where(num.greater(t, t2), eT, eZ) Z = num.where(mask, Z, 1) if scalar: return Z[0],eZ[0],mask[0] else: return Z,eZ,mask
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 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 __call__(self,dhalo,psi,rho=1,rs=1): """Compute the LoS integral using a 2D spline table. Returns ------- vals: LoS amplitude per steradian. """ dhalo = np.asarray(dhalo) psi = np.asarray(psi) if dhalo.ndim == 0: dhalo = np.array([dhalo]) if psi.ndim == 0: psi = np.array([psi]) if psi.ndim == 2 and dhalo.ndim == 2: v = np.power(10,bisplev(dhalo[:,0],psi[0,:],self._tck)) else: v = np.power(10,bisplev(dhalo,psi,self._tck)) v *= rho*rho*rs return v
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 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 display_terrain(self): """ Try to display terrain """ fig = plt.figure() ax = fig.gca(projection='3d') plt.hold(True) if self.tW is not None: terrain_points = ax.scatter(self.tX, self.tY, self.tZ, c=self.tW) fig.colorbar(terrain_points, shrink=0.5, aspect=5) else: terrain_points = ax.scatter(self.tX, self.tY, self.tZ) # Draw rivers for river_id,river in self.rivers_data_3d.items(): rx = [item[0] for item in river] ry = [item[1] for item in river] rz = [item[2] for item in river] ax.plot(rx, ry, rz, label=str(river_id)) # Draw borders for border_id,border in self.area_borders_3d.items(): bx = [item[0] for item in border] by = [item[1] for item in border] bz = [item[2] for item in border] # Make sure border is displayed as cyclic polyline bx.append(bx[0]) by.append(by[0]) bz.append(bz[0]) ax.plot(bx, by, bz) # Draw bspline patches for limit,tck in self.tck.items(): min_x = limit[0] min_y = limit[1] max_x = limit[2] max_y = limit[3] XB = np.arange(min_x, max_x + self.dx / 2.0, self.dx) YB = np.arange(min_y, max_y + self.dy / 2.0, self.dy) XG,YG = np.meshgrid(XB, YB) ZB = interpolate.bisplev(XB, YB, tck) surf = ax.plot_surface(XG.transpose(), YG.transpose(), ZB, color='gray', shade=True, alpha=0.5, antialiased=False, rstride=1, cstride=1) surf.set_linewidth(0) plt.show()
def loggtracks(masslimit,location,fileroot, metalstr, plot=True): #This is an array of all the masses that are part of the filenames massarrstr=['120.0', '85.0', '60.0', '40.0', '25.0', '20.0', '15.0', '12.0', '9.0', '7.0', '5.0', '4.0', '3.0', '2.5', '2.0', '1.7', '1.5', '1.25', '1.0', '0.9'] #this is for each mass, read in the file and plot the evolutionary tracks for imass in range(masslimit): print "Using mass: "+str(massarrstr[imass]) #Producing the location of the file print "Reading in evolutionary track data." filemass = massarrstr[imass] filename = location+fileroot+filemass #Read in the data DataIn = np.genfromtxt(filename, dtype="float", unpack=True) #Set the variables from the "DataIn" array to more meaningful terms, then use them to calculate some basic paramaters time = DataIn[3,:] timesec = time*365*24*60*60 Teff = 10**DataIn[ 6,:] logTeff = [math.log10(jj) for jj in Teff] L = 10**DataIn[ 5,:]*Lsun Rsquared = (L/(4*math.pi*sigma*Teff**4)) Mass = DataIn[4,:]*Msun surfaceGrav = Mass*G/(Rsquared) logsurfaceGrav = [math.log10(ii) for ii in surfaceGrav] #Create the q0s array then fill it along the evolutionary track from the interpolated grid print "Interpolating the time snapshot on the grid." q0s = np.zeros(len(Teff)) for timestep in range(len(Teff)): q0s[timestep] = np.power(10,interpolate.bisplev(Teff[timestep],logsurfaceGrav[timestep],gridq0s)) #For some reason, some values are negative, this fixes that for kk in range(len(q0s)): if q0s[kk] < 0: q0s[kk] = -q0s[kk] q0s[kk] = np.log10(q0s[kk]) #This is the integration of the integrated Q0. It then takes the log of it and sets the value to an array. print "Integrating the Q0 production over a lifetime." totalQ0s = np.trapz(q0s,timesec) logtotalQ0s = np.log10(totalQ0s) logq0ints[imass] = totalQ0s return;
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