def test_convolve2d_king(self): gfn = lambda r, s: np.power(2*np.pi*s**2,-1)*np.exp(-r**2/(2*s**2)) kfn = lambda r, s, g: np.power(2*np.pi*s**2,-1)*(1.-1./g)* \ np.power(1+0.5/g*(r/s)**2,-g) kfn0 = lambda x, y, mux, muy, s, g: kfn(np.sqrt((x-mux)**2+(y-muy)**2),s,g) xaxis = Axis.create(-3,3,501) yaxis = Axis.create(-3,3,501) x, y = np.meshgrid(xaxis.center,yaxis.center) xbin, ybin = np.meshgrid(xaxis.width,yaxis.width) r = np.sqrt(x**2+y**2) # Scalar Input mux = 0.5 muy = -0.2 mur = (mux**2+muy**2)**0.5 gsig = 0.1 ksig = 0.2 kgam = 4.0 fval0 = np.sum(kfn0(x,y,mux,muy,ksig,kgam)*gfn(r,gsig)*xbin*ybin) fval1 = convolve2d_king(lambda t: gfn(t,gsig),mur,ksig,kgam,3.0, nstep=10000) # fval2 = convolve2d_gauss(lambda t: kfn(t,ksig,kgam),mur,gsig,3.0, # nstep=1000) # print fval0, fval1, fval2, fval1/fval0 assert_almost_equal(fval0,fval1,4)
def __init__(self, Lx=600.0, Ly=600.0, nx=31, ny=31, dt=0.2, epsilon=0.02, h0=10.0, t0=0.0): """Initialization with grid parameters and initial conditions""" # store the parameters self.Lx, self.Ly = float(Lx), float(Ly) self.nx, self.ny = nx, ny self.dt = float(dt) self.dx = self.Lx / (self.nx - 1) self.dy = self.Ly / (self.ny - 1) self.h0 = h0 self.epsilon = float(epsilon) self.t0 = float(t0) self.t = float(t0) self.i = 0 # make axes self.xeta = np.linspace(0, self.Lx, self.nx) self.yeta = np.linspace(0, self.Ly, self.ny) self.xxeta, self.yyeta = np.meshgrid(self.xeta, self.yeta) self.xu = self.xeta + self.dx / 2.0 self.yu = self.yeta + self.dy / 2.0 self.xxu, self.yyu = np.meshgrid(self.xu, self.yu) # initialize the arrays shape = np.array((nx, ny)) + 2 self.uold = np.zeros(shape) self.vold = np.zeros(shape) self.unew = np.zeros(shape) self.vnew = np.zeros(shape) self.etaold = np.zeros(shape) self.etaint = np.zeros(shape) self.etanew = np.zeros(shape) self.h = np.ones(shape, dtype=float) * h0 self.gridargs = (self.nx, self.ny, self.dx, self.dy, self.dt)
def allowed_region( V_nj, ave_j ): # read PCs PC1 = V_nj[0] PC2 = V_nj[1] n_band = len( PC1 ) band_ticks = np.arange( n_band ) x_ticks = np.linspace(-0.4,0.2,RESOLUTION) y_ticks = np.linspace(-0.2,0.4,RESOLUTION) x_mesh, y_mesh, band_mesh = np.meshgrid( x_ticks, y_ticks, band_ticks, indexing='ij' ) vec_mesh = x_mesh * PC1[ band_mesh ] + y_mesh * PC2[ band_mesh ] + ave_j[ band_mesh ] x_grid, y_grid = np.meshgrid( x_ticks, y_ticks, indexing='ij' ) prohibited_grid = np.zeros_like( x_grid ) for ii in xrange( len( x_ticks ) ) : for jj in xrange( len( y_ticks ) ) : if np.any( vec_mesh[ii][jj] < 0. ) : prohibited_grid[ii][jj] = 1 if np.any( vec_mesh[ii][jj] > 1. ) : prohibited_grid[ii][jj] = 3 elif np.any( vec_mesh[ii][jj] > 1. ) : prohibited_grid[ii][jj] = 2 else : prohibited_grid[ii][jj] = 0 return x_grid, y_grid, prohibited_grid
def test_kernel_error(): """ """ for xx2d in [np.array(np.meshgrid(np.arange(-100, 100, 5), np.arange(-100, 100, 5))), np.array(np.meshgrid(np.arange(-100, 100, 5), np.arange(-100, 100, 5))).reshape(2, -1)]: mean1 = [20, 20] sigma1 = [10, 10] params1 = np.hstack([mean1, sigma1]) mean2 = [30, 40] sigma2 = [10, 50] params2 = np.hstack([mean2, sigma2]) params = [params1, params2] betas = [0.3, 0.7] y = ebp.mixture_of_kernels(xx2d, betas, params, ebp.gaussian_kernel) err = ebp.kernel_err(y, xx2d, betas, params, ebp.gaussian_kernel) npt.assert_almost_equal(err, np.zeros(err.shape))
def get_gauss_points(self, ng): if isinstance(ng, int): return self.gauss_points.get(ng) elif isinstance(ng, list): if len(ng) > 3: raise Exception('Gauss points for 4 dimensions' + 'and above not supported') if len(ng) == 2: Xi1, W1 = self.get_gauss_points(ng[0]) Xi2, W2 = self.get_gauss_points(ng[1]) Xi1g, Xi2g = numpy.meshgrid(Xi1.flatten(), Xi2.flatten()) Xi1 = numpy.array([Xi1g.flatten(), Xi2g.flatten()]).T W1g, W2g = numpy.meshgrid(W1.flatten(), W2.flatten()) W1 = W1g.flatten() * W2g.flatten() return Xi1, W1 elif len(ng) == 3: Xi1, W1 = self.get_gauss_points(ng[0]) Xi2, W2 = self.get_gauss_points(ng[1]) Xi3, W3 = self.get_gauss_points(ng[2]) gindex = numpy.mgrid[0:ng[0], 0:ng[1], 0:ng[2]] gindex = numpy.array([ gindex[2, :, :].flatten(), gindex[1, :, :].flatten(), gindex[0, :, :].flatten()]).T Xi = numpy.array([ Xi1[gindex[:,0]], Xi2[gindex[:,1]], Xi3[gindex[:,2]]])[:,:,0].T W = numpy.array([ W1[gindex[:,0]], W2[gindex[:,1]], W3[gindex[:,2,]]]).T.prod(1) return Xi, W raise Exception('Invalid number of gauss points') return None, None
def getBoxbyX(X, grid=50, padding=True): ''' Get the meshgrid X,Y given data set X :param X: dataset :param grid: meshgrid step size :param padding: if add extra padding around :return: X,Y ''' if X.shape[1] > 2: print 'We can only get the grid in 2-d dimension!' return None else: minx = min(X[:,0]) maxx = max(X[:,0]) miny = min(X[:,1]) maxy = max(X[:,1]) padding_x = 0.05*(maxx-minx) padding_y = 0.05*(maxy-miny) if padding: X,Y = np.meshgrid(np.linspace(minx-padding_x, maxx+padding_x, grid), np.linspace(miny-padding_y, maxy+padding_y, grid)) else: X,Y = np.meshgrid(np.linspace(minx, maxx, grid), np.linspace(miny, maxy, grid)) return (X, Y)
def logLikelihood(self, data): assert self.numImages == data.numImages which = [np.nonzero(data.id == i)[0]\ for i in xrange(0, self.numImages)] # Mean vector m = np.empty(data.t.size) for i in xrange(0, self.numImages): m[which[i]] = self.mag[i] # Covariance matrix [t1, t2] = np.meshgrid(data.t, data.t) lags = np.abs(t2 - t1) plt.imshow(lags) plt.show() ids = np.meshgrid(data.id, data.id) equal = ids[0] == ids[1] ## try: ## L = la.cholesky(C) ## except: ## return -np.inf # y = data.y - m # logDeterminant = 2.0*np.sum(np.log(np.diag(L))) # solution = la.cho_solve((L, True), y) # exponent = np.dot(y, solution) # logL = -0.5*data.t.size*np.log(2.0*np.pi) - 0.5*logDeterminant - 0.5*exponent return 0.
def makePathAndGrid(data, xsteps, ysteps, x_col='x_m', y_col='y_m'): x = data[x_col] y = data[y_col] xmin = min(x) xmax = max(x) ymin = min(y) ymax = max(y) # make grid dx = (xmax - xmin)/xsteps dy = (ymax - ymin)/ysteps XXraw, YYraw = np.meshgrid(np.arange(xmin, xmax, dx) + dx/2, np.arange(ymin, ymax, dy) + dy/2) x = ((x - xmin)/(xmax-xmin) * xsteps).astype(int) y = ((y - ymin)/(ymax-ymin) * ysteps).astype(int) xx = np.arange(0, xsteps) yy = np.arange(0, ysteps) M = np.zeros((len(xx),len(yy))) N = len(xx) * len(yy) idx = np.arange(N) np.random.shuffle(idx) XX, YY = np.meshgrid(xx, yy) XX = XX.reshape(N) YY = YY.reshape(N) XXraw = XXraw.reshape(N) YYraw = YYraw.reshape(N) return idx, np.c_[XXraw, YYraw].T, np.c_[XX, YY].T, M
def check_cross_amplification(self, max_dist): pop_set = set() for pos in self.pairs_pos: p1,p2 = pos c1,a1,s1 = zip(*self.alignments_dict[p1]) c2,a2,s2 = zip(*self.alignments_dict[p2]) chrs_x, chrs_y = na.meshgrid(c1, c2) chrs_eq = chrs_x==chrs_y orient_x,orient_y = na.meshgrid(s1, s2) orient_fr = orient_x==na.logical_not(orient_y) pos_x,pos_y = na.meshgrid(a1, a2) # Filter by minimum distance. # And if orientation is forward/reverse # Amps is [Alignments i X Alignments j ] amps = na.logical_and(na.logical_and(na.abs(pos_x-pos_y)<max_dist, na.logical_and(orient_fr, pos_x<pos_y)), # Forward reverse if one position is less than the other and is forward, while the other is reverse chrs_eq) number_of_amps = na.sum(amps) #assert number_of_amps!=0 if number_of_amps>1: pop_set.add(pos) self.pairs_pos.difference_update(pop_set)
def P1(mode="a"): if mode=="a": fig = plt.figure() for N,p in [(8,221),(16,222),(32,223),(64,224)]: u = OneD(0.035,2.5,60,20,20,N,0.01,5) x = [i*2.5/N for i in range(N)] y = [j*0.01 for j in range(int(5/0.01))] X,Y = np.meshgrid(x,y) ax = fig.add_subplot(p, projection='3d') surf = ax.plot_surface(X,Y,u,rstride=1, cstride=1,linewidth=0, cmap=cm.coolwarm) ax.set_zlim(20,60.01) ax.set_xlabel('X Position (cm)') ax.set_ylabel('Time (s)') ax.set_zlabel('Temperature (C)') ax.set_title('N='+str(N)) fig.colorbar(surf,ticks=[t for t in range(20,61,5)]) plt.show() if mode == "b": fig = plt.figure() for t,p in [(0.001,221),(0.01,222),(0.1,223)]: u = OneD(0.035,2.5,60,20,20,32,t,5) x = [i*2.5/32 for i in range(32)] y = [j*t for j in range(int(5/t))] X,Y = np.meshgrid(x,y) ax = fig.add_subplot(p, projection='3d') surf = ax.plot_surface(X,Y,u,rstride=1, cstride=1,linewidth=0, cmap=cm.coolwarm) if t!=0.1: ax.set_zlim(20,60.01) ax.set_xlabel('X Position (cm)') ax.set_ylabel('Time (s)') ax.set_zlabel('Temperature (C)') ax.set_title('Timestep:'+str(t)) if t != 0.1: fig.colorbar(surf,ticks=[t for t in range(20,61,5)]) plt.show()
def polyval2d_general(c, x, y, function="polynomial", minx=None, maxx=None, miny=None, maxy=None): if function == "polynomial": xx, yy = np.meshgrid(x, y) return np.polynomial.polynomial.polyval2d(xx, yy, c) elif function in ["legendre", "chebyshev"]: # Scale x-direction if minx is None or maxx is None: if np.size(x) == 1: xmin, xmax = -1.0, 1.0 else: xmin, xmax = np.min(x), np.max(x) else: xmin, xmax = minx, maxx xv = 2.0 * (x-xmin)/(xmax-xmin) - 1.0 # Scale y-direction if miny is None or maxy is None: if np.size(y) == 1: ymin, ymax = -1.0, 1.0 else: ymin, ymax = np.min(y), np.max(y) else: ymin, ymax = miny, maxy yv = 2.0 * (y-ymin)/(ymax-ymin) - 1.0 xx, yy = np.meshgrid(xv, yv) if function == "legendre": return np.polynomial.legendre.legval2d(xx, yy, c) elif function == "chebyshev": return np.polynomial.chebyshev.chebval2d(xx, yy, c) else: msgs.error("Function {0:s} has not yet been implemented".format(function)) return None
def _calc_partials(self): """Calculate the partial derivatives Uses sympy.utilities.lambdify to convert equation into a function that can be easily computed. Raises: MissingEquationError: if method is called before equation is attached. """ if not self.equation: raise self.MissingEquationError('No equation attached') x, y = sympy.symbols('x,y') compute_func = sympy.utilities.lambdify((x, y), self.equation) X, Y = np.meshgrid(self.xrange, self.yrange) DX, DY = np.meshgrid(np.zeros(len(self.xrange)), np.zeros(len(self.yrange))) # Iterate through grid and compute function value at each point # If value cannot be computed, default to 0 # If value can be computed, scale by sqrt of the magnitude for i, a in enumerate(self.xrange): for j, b in enumerate(self.yrange): dx = 1 try: dy = compute_func(a, b) n = sqrt(dx**2 + dy**2) dy /= sqrt(n) dx /= sqrt(n) DX[j][i] = dx DY[j][i] = dy except (ValueError, ZeroDivisionError): pass return X, Y, DX, DY
def mask_polar_to_cart(mask, center, min_radius, max_radius, output_shape, zoom_factor=1): '''Converts a polar binary mask to Cartesian and places in an image of zeros''' # Account for upsampling if zoom_factor != 1: center = (center[0]*zoom_factor + zoom_factor/2, center[1]*zoom_factor + zoom_factor/2) min_radius = min_radius * zoom_factor max_radius = max_radius * zoom_factor output_shape = map(lambda a: a * zoom_factor, output_shape) # new image image = np.zeros(output_shape) # coordinate conversion theta, r = np.meshgrid(np.linspace(0, 2*np.pi, mask.shape[1]), np.arange(0, max_radius)) x, y = coord_polar_to_cart(r, theta, center) x, y = np.round(x), np.round(y) x, y = x.astype(int), y.astype(int) x = np.clip(x, 0, image.shape[0]-1) y = np.clip(y, 0, image.shape[1]-1) ix,iy = np.meshgrid(np.arange(0,mask.shape[1]), np.arange(0,mask.shape[0])) image[x,y] = mask # downsample image if zoom_factor != 1: zf = 1/float(zoom_factor) image = zoom(image, (zf, zf), order=4) # ensure image remains a filled binary mask image = (image > 0.5).astype(int) image = binary_fill_holes(image) return image
def stmDatToArray(filename): with open(filename, 'r') as f: f.readline() delta = [] for i in range(2): line = map(float, [entry for entry in f.readline().strip().split(' ') if entry!=''][:2]) delta.append(line) shape = [entry for entry in f.readline().strip().split(' ') if len(entry)!=0] shape = map(int, shape[:2]) arrays = [] xm, ym = [delta[i][0] + np.arange(shape[i])*delta[i][1] for i in range(2)] xo, yo = [delta[i][0] - 0.5*delta[i][1]+ np.arange(shape[i] + 1)*delta[i][1] for i in range(2)] Xm, Ym = np.meshgrid(xm, ym) Xo, Yo = np.meshgrid(xo, yo) collect = [] for line in f: try: line = [entry for entry in line.strip().split(' ') if len(entry)>0] line = map(float, line) except ValueError: break else: collect.extend(line) Z = np.reshape(collect, shape) return Xm, Ym, Z
def test_get_decomposition(self): # Test a stable entry to ensure that it's zero in the stable region entry = self.test_data['Zn'][12] # Should correspond to mp-2133 self.assertAlmostEqual(self.pbx.get_decomposition_energy(entry, 10, 1), 0.0, 5, "Decomposition energy of ZnO is not 0.") # Test an unstable entry to ensure that it's never zero entry = self.test_data['Zn'][11] ph, v = np.meshgrid(np.linspace(0, 14), np.linspace(-2, 4)) result = self.pbx_nofilter.get_decomposition_energy(entry, ph, v) self.assertTrue((result >= 0).all(), "Unstable energy has hull energy of 0 or less") # Test an unstable hydride to ensure HER correction works self.assertAlmostEqual(self.pbx.get_decomposition_energy(entry, -3, -2), 11.093744395) # Test a list of pHs self.pbx.get_decomposition_energy(entry, np.linspace(0, 2, 5), 2) # Test a list of Vs self.pbx.get_decomposition_energy(entry, 4, np.linspace(-3, 3, 10)) # Test a set of matching arrays ph, v = np.meshgrid(np.linspace(0, 14), np.linspace(-3, 3)) self.pbx.get_decomposition_energy(entry, ph, v)
def find_intersections(contour1, contour2): """ Vectorized code to find intersections between contours. All successive duplicate vertices along the input contours must be removed to help avoid division-by-zero errors. There are cases were no intersection exists (eg. parallel lines) where division by zero and invalid value exceptions occur. These exceptions should be caught as warnings: these edge cases are unavoidable with this algorithm and do not indicate that the output is erroneous. """ amin = lambda x1, x2: np.where(x1<x2, x1, x2) # Elementwise min selection amax = lambda x1, x2: np.where(x1>x2, x1, x2) # Elementwise max selection aall = lambda abools: np.dstack(abools).all(axis=2) # dstacks, checks True depthwise # Uses delta (using np.diff) to find successive slopes along path slope = lambda line: (lambda d: d[:,1]/d[:,0])(np.diff(line, axis=0)) # Meshgrids between both paths (x and y). One element sliced off end/beginning x11, x21 = np.meshgrid(contour1[:-1, 0], contour2[:-1, 0]) x12, x22 = np.meshgrid(contour1[1:, 0], contour2[1:, 0]) y11, y21 = np.meshgrid(contour1[:-1, 1], contour2[:-1, 1]) y12, y22 = np.meshgrid(contour1[1:, 1], contour2[1:, 1]) # Meshgrid of all slopes for both paths m1, m2 = np.meshgrid(slope(contour1), slope(contour2)) m2inv = 1/m2 # m1inv was not used. yi = (m1*(x21-x11-m2inv*y21) + y11)/(1 - m1*m2inv) xi = (yi - y21)*m2inv + x21 # (xi, yi) is intersection candidate # Bounding box type conditions for intersection candidates xconds = (amin(x11, x12) < xi, xi <= amax(x11, x12), amin(x21, x22) < xi, xi <= amax(x21, x22) ) yconds = (amin(y11, y12) < yi, yi <= amax(y11, y12), amin(y21, y22) < yi, yi <= amax(y21, y22) ) return xi[aall(xconds)], yi[aall(yconds)]
def select_master_date(date_list, pbase_list=[]): """Select super master date based on input temporal and/or perpendicular baseline info. Return master date in YYYYMMDD format. """ date8_list = ptime.yyyymmdd(date_list) if not pbase_list: # Choose date in the middle m_date8 = date8_list[int(len(date8_list)/2)] else: # Get temporal baseline list tbase_list = ptime.date_list2tbase(date8_list)[0] # Normalization (Pepe and Lanari, 2006, TGRS) temp2perp_scale = (max(pbase_list)-min(pbase_list)) / (max(tbase_list)-min(tbase_list)) tbase_list = [tbase*temp2perp_scale for tbase in tbase_list] # Get distance matrix ttMat1, ttMat2 = np.meshgrid(np.array(tbase_list), np.array(tbase_list)) ppMat1, ppMat2 = np.meshgrid(np.array(pbase_list), np.array(pbase_list)) ttMat = np.abs(ttMat1 - ttMat2) # temporal distance matrix ppMat = np.abs(ppMat1 - ppMat2) # spatial distance matrix # 2D distance matrix in temp/perp domain disMat = np.sqrt(np.square(ttMat) + np.square(ppMat)) # Choose date minimize the total distance of temp/perp baseline disMean = np.mean(disMat, 0) m_idx = np.argmin(disMean) m_date8 = date8_list[m_idx] return m_date8
def increase_grid(x, y, z, increase_y = 0, increase_x = 0, new_y_lims = None, number=100): #generic use function to re-grid data x_grid,y_grid = np.meshgrid(x,y) input_grid = np.ones([len(x_grid.flatten()),2],dtype=float) input_grid[:,0]=x_grid.flatten() input_grid[:,1]=y_grid.flatten() if increase_x: x_out = np.linspace(min(x),max(x),num=number) else: x_out = x if increase_y: if new_y_lims == None: y_out = np.linspace(min(y),max(y),num=number) else: y_out = np.linspace(new_y_lims[0],new_y_lims[1],num=number) else: y_out = y x_grid_output, y_grid_output = np.meshgrid(x_out,y_out) output_grid = np.ones([len(x_grid_output.flatten()),2],dtype=float) output_grid[:,0]=x_grid_output.flatten() output_grid[:,1]=y_grid_output.flatten() z_out = griddata(input_grid,z.flatten(),output_grid) z_out.resize(len(y_out),len(x_out)) return x_out, y_out, z_out
def pflux(self, p, x): Egam = x * 1e-9 # in TeV if self.EG is None: self.EG, self.EP = np.meshgrid(Egam, self.Ep) self.Fgam = self.F_gamma(self.EG, self.EP) elif np.array_equal(Egam, self.EG[0, :]) is False: print(Egam.shape, self.EG.shape) print("Warning different internal vectors. Recomputing.") self.EG, self.EP = np.meshgrid(Egam, self.Ep) self.Fgam = self.F_gamma(self.EG, self.EP) # proton_spec = p[3]*np.exp(-self.EP*p[1]*1e-3)*(self.EP/p[0])**(-p[2]) proton_spec = np.exp(-self.EP * p[1] * 1e-3) * (self.EP / p[0]) ** (-p[2]) if self.Fgam is None: self.Fgam = self.F_gamma(self.EG, self.EP) res = self.Fgam * proton_spec integral = 3e10 * res.sum(0) * self.lbsize / np.log(10.) # Normalize with proton energy content at 1 kpc for n_H = 1 cm^-3 pe_spec = self.Ep ** 2 * np.exp(-self.Ep * p[1] * 1e-3) * (self.Ep / p[0]) ** (-p[2]) pe_spec = pe_spec[(self.Ep >= p[4]) * (self.Ep <= p[5])] norm = 4 * np.pi * (3.1e21) ** 2 * 1.602e9 * pe_spec.sum() * self.lbsize / np.log(10.) / 1e50 # return 3e10*res.sum(0)*self.lbsize/np.log(10.)*1e-9 # per TeV not keV return integral / norm * p[3]
def VectorField(ax, VelocityField = None): if VelocityField is not None: # Get all velocities x, y, z, u, v, w = VelocityField.GetVelocity() #print(x.shape) #print(y.shape) #print(z.shape) #print(u.shape) #print(v.shape) #print(w.shape) #pdb.set_trace() # Meshgrid the vectors x_rec, y_rec, z_rec = np.meshgrid(x, y, z) u_rec, v_rec, w_rec = np.meshgrid(u, v, w) #pdb.set_trace() # Plot Vector field of velocity ax.quiver(x_rec, y_rec, z_rec, u_rec, v_rec, w_rec, length = 0.5) # Set axis labels ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') # Show photo plt.show()
def generate_ind_pairs(img_num_per_cam_person, person_inds, cam_inds): positive_pair_a = numpy.asarray([], dtype='int32') positive_pair_b = numpy.asarray([], dtype='int32') negative_pair_a = numpy.asarray([], dtype='int32') negative_pair_b = numpy.asarray([], dtype='int32') past_person_inds = numpy.asarray([], dtype='int32') for i in person_inds: past_person_inds = numpy.append(past_person_inds, i) past_cam_inds = numpy.asarray([], dtype='int32') for j in cam_inds: past_cam_inds = numpy.append(past_cam_inds, j) target_abs_inds = get_abs_ind_one(img_num_per_cam_person, i, j) pos_cand_abs_inds = get_abs_ind_per_cam_person(img_num_per_cam_person, numpy.asarray([i], dtype='int32'), numpy.setdiff1d(cam_inds, past_cam_inds)) neg_cand_abs_inds = get_abs_ind_per_cam_person(img_num_per_cam_person, numpy.setdiff1d(person_inds, past_person_inds), numpy.setdiff1d(cam_inds, j)) [tmp_a, tmp_b] = numpy.meshgrid(pos_cand_abs_inds, target_abs_inds) positive_pair_a = numpy.append(positive_pair_a, tmp_b.flatten()) positive_pair_b = numpy.append(positive_pair_b, tmp_a.flatten()) [tmp_a, tmp_b] = numpy.meshgrid(neg_cand_abs_inds, target_abs_inds) negative_pair_a = numpy.append(negative_pair_a, tmp_b.flatten()) negative_pair_b = numpy.append(negative_pair_b, tmp_a.flatten()) return [positive_pair_a, positive_pair_b, negative_pair_a, negative_pair_b]
def _init_grid(self): """ Initialize (x,y,z) coordinate grid + (re)draw plot.""" phi_UC = np.linspace(0, 2*pi, 400, endpoint=True) # angles for unit circle self.xy_UC = np.exp(1j * phi_UC) # x,y coordinates of unity circle steps = 100 # number of steps for x, y, r, phi # self.xmin = -1.5; self.xmax = 1.5 # cartesian range limits self.ymin = -1.5; self.ymax = 1.5 rmin = 0; rmax = self.xmin # polar range limits # Calculate grids for 3D-Plots dr = rmax / steps * 2 # grid size for polar range dx = (self.xmax - self.xmin) / steps dy = (self.ymax - self.ymin) / steps # grid size cartesian range if self.chkPolar.isChecked(): # # Plot circular range in 3D-Plot [r, phi] = np.meshgrid(np.arange(rmin, rmax, dr), np.linspace(0, 2 * pi, steps, endpoint=True)) self.x = r * cos(phi) self.y = r * sin(phi) else: # cartesian grid [self.x, self.y] = np.meshgrid(np.arange(self.xmin, self.xmax, dx), np.arange(self.ymin, self.ymax, dy)) self.z = self.x + 1j*self.y # create coordinate grid for complex plane self.draw() # initial plot
def __init__(self): # sift features self.gS = 8 self.pS = 16 self.nrml_thres = 1.0 self.sigma = 0.8 self.sift_thres = 0.2 self.Nangles = 8 self.Nbins = 4 self.Nsamples = self.Nbins**2 self.alpha = 9.0 self.angles = np.array(range(self.Nangles))*2.0*np.pi/self.Nangles self.GH, self.GW = self.gen_dgauss(self.sigma) # compute the weight contribution map # weights is the contribution of each pixel to the corresponding bin center sample_res = self.pS / np.double(self.Nbins) sample_p = np.array(range(self.pS)) sample_ph, sample_pw = np.meshgrid(sample_p,sample_p) sample_ph.resize(sample_ph.size) sample_pw.resize(sample_pw.size) bincenter = np.array(range(1,self.Nbins*2,2)) / 2.0 / self.Nbins * self.pS - 0.5 bincenter_h, bincenter_w = np.meshgrid(bincenter,bincenter) bincenter_h.resize((bincenter_h.size,1)) bincenter_w.resize((bincenter_w.size,1)) dist_ph = abs(sample_ph - bincenter_h) dist_pw = abs(sample_pw - bincenter_w) weights_h = dist_ph / sample_res weights_w = dist_pw / sample_res weights_h = (1-weights_h) * (weights_h <= 1) weights_w = (1-weights_w) * (weights_w <= 1) self.weights = weights_h * weights_w
def graph(self, x_axis, y_axis, image, params, result = None): #plot the sample data from matplotlib import pyplot pyplot.figure() pyplot.subplot(2,1,1) pyplot.contourf(x_axis, y_axis, image, alpha = 0.5) #plot the fit #sample the fit with more precision x_axis_fit = np.linspace(x_axis.min(), x_axis.max(), x_axis.size * 5) y_axis_fit = np.linspace(y_axis.min(), y_axis.max(), y_axis.size * 5) xx, yy = np.meshgrid(x_axis_fit, y_axis_fit) params['background_level'].value = 0 fit_1d = self.ion_model(params, xx, yy, use_1d=True) xx, yy = np.meshgrid(x_axis, y_axis) pyplot.subplot(2,1,2) ximage = image.sum(axis=0) ximage = ximage - ximage[0] a1d = np.ones_like(self.allions_1d) #low_values_indices = self.allions_1d - bg < (0.5)*params['amplitude'].value # Where values are low low_values_indices = self.allions_1d < self.model_threshold # Where values are low a1d[low_values_indices] = 0 # All low values set to 0 pyplot.plot(x_axis, a1d * ximage.max()) #pyplot.plot(x_axis, ximage * a1d, 'o', markersize=10) pyplot.plot(x_axis, ximage) pyplot.plot(x_axis_fit, fit_1d) pyplot.tight_layout() pyplot.show()
def get_patches(data, var, dql, centers, patch_size=5): """ Return patched data, etc, at given centers. """ buff = (patch_size - 1) / 2 # SExtractor doesnt alway put peak on brightest pixel # find shifts, up to 1 pixel that correct for this x, y = np.meshgrid(range(-2, 3), range(-2, 3)) xcs = (x[None, :, :] + centers[:,0][:, None, None]).astype(np.int) ycs = (y[None, :, :] + centers[:,1][:, None, None]).astype(np.int) for i in range(centers.shape[0]): test = data[xcs[i], ycs[i]] ind = (test == np.max(test)) centers[i, 0] += x[ind] centers[i, 1] += y[ind] # Full patch indices x, y = np.meshgrid(range(-buff, buff + 1), range(-buff, buff + 1)) xcs = (x[None, :, :] + centers[:,0][:, None, None]).astype(np.int) ycs = (y[None, :, :] + centers[:,1][:, None, None]).astype(np.int) return data[xcs, ycs], var[xcs, ycs], dql[xcs, ycs], xcs, ycs, \ centers
def __init__(self, gridSpacing, patchSize, nrml_thres=1.0, sigma_edge=0.8, sift_thres=0.2): """ gridSpacing: the spacing for sampling dense descriptors patchSize: the size for each sift patch nrml_thres: low contrast normalization threshold sigma_edge: the standard deviation for the gaussian smoothing before computing the gradient sift_thres: sift thresholding (0.2 works well based on Lowe's SIFT paper) """ self.gS = gridSpacing self.pS = patchSize self.nrml_thres = nrml_thres self.sigma = sigma_edge self.sift_thres = sift_thres # compute the weight contribution map sample_res = self.pS / np.double(Nbins) sample_p = np.array(range(self.pS)) sample_ph, sample_pw = np.meshgrid(sample_p, sample_p) sample_ph.resize(sample_ph.size) sample_pw.resize(sample_pw.size) bincenter = np.array(range(1, Nbins * 2, 2)) / 2.0 / Nbins * self.pS - 0.5 bincenter_h, bincenter_w = np.meshgrid(bincenter, bincenter) bincenter_h.resize((bincenter_h.size, 1)) bincenter_w.resize((bincenter_w.size, 1)) dist_ph = abs(sample_ph - bincenter_h) dist_pw = abs(sample_pw - bincenter_w) weights_h = dist_ph / sample_res weights_w = dist_pw / sample_res weights_h = (1 - weights_h) * (weights_h <= 1) weights_w = (1 - weights_w) * (weights_w <= 1) # weights is the contribution of each pixel to the corresponding bin center self.weights = weights_h * weights_w
def test_label_propagation_closed_form(): n_classes = 2 X, y = make_classification(n_classes=n_classes, n_samples=200, random_state=0) y[::3] = -1 Y = np.zeros((len(y), n_classes + 1)) Y[np.arange(len(y)), y] = 1 unlabelled_idx = Y[:, (-1,)].nonzero()[0] labelled_idx = (Y[:, (-1,)] == 0).nonzero()[0] clf = label_propagation.LabelPropagation(max_iter=10000, gamma=0.1) clf.fit(X, y) # adopting notation from Zhu et al 2002 T_bar = clf._build_graph() Tuu = T_bar[tuple(np.meshgrid(unlabelled_idx, unlabelled_idx, indexing='ij'))] Tul = T_bar[tuple(np.meshgrid(unlabelled_idx, labelled_idx, indexing='ij'))] Y = Y[:, :-1] Y_l = Y[labelled_idx, :] Y_u = np.dot(np.dot(np.linalg.inv(np.eye(Tuu.shape[0]) - Tuu), Tul), Y_l) expected = Y.copy() expected[unlabelled_idx, :] = Y_u expected /= expected.sum(axis=1)[:, np.newaxis] assert_array_almost_equal(expected, clf.label_distributions_, 4)
def find_intersections(self, A, B): ''' (matrix, matrix -> bool''' amin = lambda x1, x2: np.where(x1<x2, x1, x2) amax = lambda x1, x2: np.where(x1>x2, x1, x2) aall = lambda abools: np.dstack(abools).all(axis=2) slope = lambda line: (lambda d: d[:,1]/d[:,0])(np.diff(line, axis=0)) x11, x21 = np.meshgrid(A[:-1, 0], B[:-1, 0]) x12, x22 = np.meshgrid(A[1:, 0], B[1:, 0]) y11, y21 = np.meshgrid(A[:-1, 1], B[:-1, 1]) y12, y22 = np.meshgrid(A[1:, 1], B[1:, 1]) m1, m2 = np.meshgrid(slope(A), slope(B)) m1inv, m2inv = 1/m1, 1/m2 yi = (m1*(x21-x11-m2inv*y21) + y11)/(1 - m1*m2inv) xi = (yi - y21)*m2inv + x21 xconds = (amin(x11, x12) < xi, xi <= amax(x11, x12), amin(x21, x22) < xi, xi <= amax(x21, x22) ) yconds = (amin(y11, y12) < yi, yi <= amax(y11, y12), amin(y21, y22) < yi, yi <= amax(y21, y22) ) if xi[aall(xconds)] and yi[aall(yconds)]: return xi[aall(xconds)], yi[aall(yconds)], True # intersection, don't go! return xi[aall(xconds)], yi[aall(yconds)], False
def _make_carts_dict(self): """ Return a carts dictionary, distances in meters. """ az_deg, range_km, el_deg = self._calc_geometry() # simple calculation involving 4/3 earth radius nsweeps = self.master_header['max_nz'] nrays = self.master_header['max_ny'] ngates = self.master_header['max_nx'] xx = np.empty([nsweeps, nrays, ngates], dtype=np.float32) yy = np.empty([nsweeps, nrays, ngates], dtype=np.float32) zz = np.empty([nsweeps, nrays, ngates], dtype=np.float32) if self.projection == 'rhi': rg, ele = np.meshgrid(range_km, el_deg) rg = np.array(rg, dtype=np.float64) ele = np.array(ele, dtype=np.float64) for aznum in range(nsweeps): azg = np.ones(rg.shape, dtype=np.float64) * az_deg[aznum] x, y, z = antenna_to_cartesian(rg, azg, ele) zz[aznum, :, :] = z xx[aznum, :, :] = x yy[aznum, :, :] = y elif self.projection == 'ppi': rg, azg = np.meshgrid(range_km, az_deg) rg = np.array(rg, dtype=np.float64) azg = np.array(azg, dtype=np.float64) for elnum in range(nsweeps): ele = np.ones(rg.shape, dtype=np.float64) * el_deg[elnum] x, y, z = antenna_to_cartesian(rg, azg, ele) zz[elnum, :, :] = z xx[elnum, :, :] = x yy[elnum, :, :] = y return {'x': xx, 'y': yy, 'z': zz}
def plot_beam(dirname, input_beam, Rho, Phi, ref_output_fluence): filename = lambda name: os.path.join(dirname, name) if len(Rho) > 1: vfluence = np.vectorize(input_beam.fluence) ref_input_fluence = vfluence(*np.meshgrid(Rho, Phi)).T norm_input_fluence = ref_input_fluence / input_beam.ref_fluence norm_output_fluence = ref_output_fluence / input_beam.ref_fluence max_output_fluence = np.amax(norm_output_fluence) n_ref = -1 for n, phi in enumerate(Phi): if n_ref < 0 or abs(phi - input_beam.phi_ref) < abs(Phi[n_ref] - input_beam.phi_ref): n_ref = n rholim = (Rho[0], Rho[-1]) plot.plot_data(filename("fluences"), "Input and Output Fluence", ((Rho,)*2, None, rholim, rho_label), ((norm_input_fluence[:, n_ref], norm_output_fluence[:, n_ref]), None, None, fluence_rel_label), ("input beam", "output beam")) plot.plot_data(filename("fluences_norm"), "Normalized Input and Output Fluence", ((Rho,)*2, None, rholim, rho_label), ((norm_input_fluence[:, n_ref], norm_output_fluence[:, n_ref] / max_output_fluence), None, None, fluence_norm_rel_label), ("input beam", "output beam")) if len(Phi) > 1: FR, RF = np.meshgrid(Phi, Rho) XY, YX = RF * np.cos(FR), RF * np.sin(FR) stride_rho = max(len(Rho) // params.out_count_rho, 1) stride_phi = max(len(Phi) // params.out_count_phi, 1) plot.plot_projection(filename("fluence_in"), "Input Fluence", (XY, None, x_label), (YX, None, y_label), (norm_input_fluence, None, fluence_rel_label), (30, -60), (stride_rho, stride_phi)) plot.plot_projection(filename("fluence_out"), "Output Fluence", (XY, None, x_label), (YX, None, y_label), (norm_output_fluence, None, fluence_rel_label), (30, -60), (stride_rho, stride_phi))
if __name__ == '__main__': # # xor exmaple # X = np.array([[0,0], [0,1], [1,0], [1,1]]) # y = np.array([[0,1,1,0]]).T # nn = Neural_Network([2,2,1], learning_rate=1, opt=False, check_gradients=True, epochs=100000, batch_size=4) # nn.fit(X, y) # Another example with two random circlular regions as positive class X = np.random.rand(300, 2) y = np.atleast_2d(1 * np.logical_or( (X[:, 0] - .8)**2 + (X[:, 1] - .5)**2 < .03, (X[:, 0] - .2)**2 + (X[:, 1] - .8)**2 < .03)).T nn = Neural_Network([2, 10, 1], learning_rate=1, C=0, opt=False, check_gradients=True, batch_size=50, epochs=100000) nn.fit(X, y) xx, yy = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100)) Z = np.round(nn.predict(np.c_[xx.ravel(), yy.ravel()])) Z = Z.reshape(xx.shape) plt.figure(figsize=(10, 8)) plt.contourf(xx, yy, Z, alpha=.8) plt.scatter(X[:, 0], X[:, 1], c=y, s=60) plt.title("Neural Net Accuracy is " + str(np.round(nn.score(X, y), 2))) plt.show()
climo = climoU[i] elif i > 1: var = diffZ[i - 2] pvar = pZ[i - 2] climo = climoZ[i - 2] ax1 = plt.subplot(2, 2, i + 1) m = Basemap(projection='ortho', lon_0=0, lat_0=89, resolution='l', area_thresh=10000.) var, lons_cyclic = addcyclic(var, lon) var, lons_cyclic = shiftgrid(180., var, lons_cyclic, start=False) lon2d, lat2d = np.meshgrid(lons_cyclic, lat) x, y = m(lon2d, lat2d) pvar, lons_cyclic = addcyclic(pvar, lon) pvar, lons_cyclic = shiftgrid(180., pvar, lons_cyclic, start=False) climoq, lons_cyclic = addcyclic(climo, lon) climoq, lons_cyclic = shiftgrid(180., climoq, lons_cyclic, start=False) m.drawmapboundary(fill_color='white', color='dimgray', linewidth=0.7) cs = m.contourf(x, y, var, limit, extend='both') cs1 = m.contourf(x, y, pvar, colors='None', hatches=['....'],
return surf, anim = animation.FuncAnimation(fig, update_surf, frames=len(eta_list), interval=10, blit=False) mpeg_writer = animation.FFMpegWriter(fps=24, bitrate=10000, codec="libx264", extra_args=["-pix_fmt", "yuv420p"]) anim.save("{}.mp4".format(filename), writer=mpeg_writer) return anim if __name__ == '__main__': eta_list = [] x = np.asarray([i for i in range(100)]) y = np.asarray([i for i in range(100)]) x, y = np.meshgrid(x, y) for i in range(0, 10001, 10): eta = [] file = open(f"result/outputH_{i}.txt") lines = file.readlines()[1:] for line in lines: tmp = [float(x) for x in line.split()] eta.append(tmp) eta = np.asarray(eta) - 1 eta_list.append(eta) if i in [0, 10, 100, 1000, 10000]: surface_plot3d(x, y, eta, f"img/H{i}.png", i) eta_animation3d(x, y, eta_list, 10, "h")
d = computeGrad(X, y, theta, reg) theta[0] -= step_size * d[0] theta[1] -= step_size * d[1] # WRITEME: write your update rule(s) here # TODO: remove this line below once you have correctly implemented/gradient-checked your various sub-routines # sys.exit(0) # evaluate training set accuracy scores, probs = predict(X, theta) predicted_class = np.argmax(scores, axis=1) print('training accuracy: %.2f' % (np.mean(predicted_class == y))) # plot the resulting classifier h = 0.02 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) Z = np.dot(np.c_[xx.ravel(), yy.ravel()], W) + b Z = np.argmax(Z, axis=1) Z = Z.reshape(xx.shape) fig = plt.figure() plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=y, s=40, cmap=plt.cm.Spectral) plt.xlim(xx.min(), xx.max()) plt.ylim(yy.min(), yy.max()) plt.savefig(os.getcwd() + '/out/spiral_linear.png') plt.show()
def grid(self): grid = [np.linspace(0, 2 * np.pi, num=self.resolution, endpoint=False) for _ in range(self.units.lattice.D)] return np.meshgrid(*grid)
plt.scatter(x, y) plt.title("scatter") plt.subplot(233) plt.pie(y) plt.title("pie") plt.subplot(234) plt.bar(x, y) plt.title("bar") # 2D data import numpy as np delta = 0.025 x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z = Y**2 + X**2 plt.subplot(235) plt.contour(X,Y,Z) plt.colorbar() plt.title("contour") # read image import matplotlib.image as mpimg img=mpimg.imread('marvin.jpg') plt.subplot(236) plt.imshow(img) plt.title("imshow")
import matplotlib.pylab as plt import numpy as np import pandas as pd from matplotlib import cm from scipy.stats import norm,chi2,binom,gamma import seaborn as sns from math import cos, sin,log,tan,pi,exp,sqrt,cosh,sinh,tanh,atan,atan2,e from cmath import exp as cexp, log as clog from mpl_toolkits.mplot3d import Axes3D for i,t in enumerate(np.linspace(0,2*pi,360)): fig = plt.figure(figsize=(16,16)) ax = fig.add_subplot(111, projection='3d',facecolor='black') p = plt.axis('off') x = np.linspace(-2*pi,2*pi,1000) y = np.linspace(-2*pi,2*pi,1000) xx, yy = np.meshgrid(x, y, sparse=True) z = np.cos(xx*yy +6*t+pi) ax.view_init(t*180/(pi),t*180/(4*pi)) ax.set_zlim(-3,3) ax.plot_surface(xx, yy, z, antialiased=True, shade=True,cmap=cm.winter,alpha=0.8) p = plt.savefig(f'C:/Users/Alejandro/Pictures/RandomPlots/15052020/plot{i}.PNG',facecolor='black')
def readVar(varnames, qbophase, period): ### Call function for surface temperature data from reach run lat, lon, time, lev, tashit = MO.readExperiAll('%s' % varnames, 'HIT', 'surface') lat, lon, time, lev, tasfict = MO.readExperiAll('%s' % varnames, 'FICT', 'surface') ### Create 2d array of latitude and longitude lon2, lat2 = np.meshgrid(lon, lat) ### Read in QBO phases filenamehitp = directorydata + 'HIT/monthly/QBO_%s_HIT.txt' % qbophase[0] filenamehitno = directorydata + 'HIT/monthly/QBO_%s_HIT.txt' % qbophase[1] filenamehitn = directorydata + 'HIT/monthly/QBO_%s_HIT.txt' % qbophase[2] filenamehitp2 = directorydata2 + 'HIT/monthly/QBO_%s_HIT.txt' % qbophase[0] filenamehitno2 = directorydata2 + 'HIT/monthly/QBO_%s_HIT.txt' % qbophase[1] filenamehitn2 = directorydata2 + 'HIT/monthly/QBO_%s_HIT.txt' % qbophase[2] pos_hit = np.append( np.genfromtxt(filenamehitp, unpack=True, usecols=[0], dtype='int'), np.genfromtxt(filenamehitp2, unpack=True, usecols=[0], dtype='int') + 101) non_hit = np.append( np.genfromtxt(filenamehitno, unpack=True, usecols=[0], dtype='int'), np.genfromtxt(filenamehitno2, unpack=True, usecols=[0], dtype='int') + 101) neg_hit = np.append( np.genfromtxt(filenamehitn, unpack=True, usecols=[0], dtype='int'), np.genfromtxt(filenamehitn2, unpack=True, usecols=[0], dtype='int') + 101) filenamefictp = directorydata + 'FICT/monthly/QBO_%s_FICT.txt' % qbophase[0] filenamefictno = directorydata + 'FICT/monthly/QBO_%s_FICT.txt' % qbophase[ 1] filenamefictn = directorydata + 'FICT/monthly/QBO_%s_FICT.txt' % qbophase[2] filenamefictp2 = directorydata2 + 'FICT/monthly/QBO_%s_FICT.txt' % qbophase[ 0] filenamefictno2 = directorydata2 + 'FICT/monthly/QBO_%s_FICT.txt' % qbophase[ 1] filenamefictn2 = directorydata2 + 'FICT/monthly/QBO_%s_FICT.txt' % qbophase[ 2] pos_fict = np.append( np.genfromtxt(filenamefictp, unpack=True, usecols=[0], dtype='int'), np.genfromtxt(filenamefictp2, unpack=True, usecols=[0], dtype='int') + 101) non_fict = np.append( np.genfromtxt(filenamefictno, unpack=True, usecols=[0], dtype='int'), np.genfromtxt(filenamefictno2, unpack=True, usecols=[0], dtype='int') + 101) neg_fict = np.append( np.genfromtxt(filenamefictn, unpack=True, usecols=[0], dtype='int'), np.genfromtxt(filenamefictn2, unpack=True, usecols=[0], dtype='int') + 101) ### Concatonate runs runs = [tashit, tasfict] ### Separate per periods (ON,DJ,FM) if period == 'ON': tas_mo = np.empty( (3, tashit.shape[0], tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i] = np.nanmean(runs[i][:, 9:11, :, :], axis=1) elif period == 'DJ': tas_mo = np.empty( (3, tashit.shape[0] - 1, tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i], tas_mo[i] = UT.calcDecJan(runs[i], runs[i], lat, lon, 'surface', 1) elif period == 'FM': tas_mo = np.empty( (3, tashit.shape[0], tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i] = np.nanmean(runs[i][:, 1:3, :, :], axis=1) elif period == 'DJF': tas_mo = np.empty( (3, tashit.shape[0] - 1, tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i], tas_mo[i] = UT.calcDecJanFeb(runs[i], runs[i], lat, lon, 'surface', 1) elif period == 'M': tas_mo = np.empty( (3, tashit.shape[0], tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i] = runs[i][:, 2, :, :] elif period == 'D': tas_mo = np.empty( (3, tashit.shape[0], tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i] = runs[i][:, -1, :, :] elif period == 'N': tas_mo = np.empty( (3, tashit.shape[0], tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i] = runs[i][:, -2, :, :] elif period == 'ND': tas_mo = np.empty( (3, tashit.shape[0], tashit.shape[2], tashit.shape[3])) for i in range(len(runs)): tas_mo[i] = np.nanmean(runs[i][:, -2:, :, :], axis=1) else: ValueError('Wrong period selected! (ON,DJ,FM)') ### Composite by QBO phase tas_mohitpos = tas_mo[0][pos_hit, :, :] tas_mofictpos = tas_mo[1][pos_fict, :, :] tas_mohitneg = tas_mo[0][neg_hit, :, :] tas_mofictneg = tas_mo[1][neg_fict, :, :] ### Compute climatology climohitpos = np.nanmean(tas_mohitpos, axis=0) climohitneg = np.nanmean(tas_mohitneg, axis=0) climo = [climohitpos, climohitneg] ### Compute comparisons for months - taken ensemble average ficthitpos = np.nanmean(tas_mofictpos - tas_mohitpos, axis=0) ficthitneg = np.nanmean(tas_mofictneg - tas_mohitneg, axis=0) diffruns_mo = [ficthitpos, ficthitneg] ### Calculate significance for ND stat_FICTHITpos, pvalue_FICTHITpos = UT.calc_indttest( tas_mo[1][pos_fict, :, :], tas_mo[0][pos_hit, :, :]) stat_FICTHITnon, pvalue_FICTHITnon = UT.calc_indttest( tas_mo[1][non_fict, :, :], tas_mo[0][non_hit, :, :]) stat_FICTHITneg, pvalue_FICTHITneg = UT.calc_indttest( tas_mo[1][neg_fict, :, :], tas_mo[0][neg_hit, :, :]) pruns_mo = [pvalue_FICTHITpos, pvalue_FICTHITneg] return lat, lon, climo, diffruns_mo, pruns_mo
Mxx = globalMatrix1D(pde.a, mesh, fem, 1, fem, 1, ng) M00 = globalMatrix1D(pde.c, mesh, fem, 0, fem, 0, ng) f = globalVec1D(pde.f, mesh, fem, 0, ng) #%% 5 Dirichlet boundary condition uD = pde.gD(fem.p) uD[fem.dof] = 0.0 bxx = Mxx @ uD b00 = M00 @ uD #%% 6 Neumann boundary condition bN = np.zeros(np.shape(fem.p)) bN[fem.ptype == 2] = pde.gN(fem.p[fem.ptype == 2]) #%% 7 Extract degree of freedom ii, jj = np.meshgrid(fem.dof, fem.dof, indexing='ij') A = Mxx[ii, jj] + M00[ii, jj] b = f[fem.dof] + bN[fem.dof] - bxx[fem.dof] - b00[fem.dof] #%% 8 Solve Linear Sysetem uDof = inv(A) @ b uh = np.copy(uD) uh[fem.dof] = uDof #%% fig, axs = plt.subplots(1, 1, sharex=True, sharey=True, figsize=(8, 6)) tu = pde.exactu(fem.p) for k in range(n): vert = mesh.p[mesh.t[k, :]] x = np.linspace(vert[0], vert[1], 11)
def putVecMaps(centerA, centerB, accumulate_vec_map, count, params_transform, thre): """Implement Part Affinity Fields :param centerA: int with shape (2,) or (3,), centerA will pointed by centerB. :param centerB: int with shape (2,) or (3,), centerB will point to centerA. :param accumulate_vec_map: one channel of paf. :param count: store how many pafs overlaped in one coordinate of accumulate_vec_map. :param params_transform: store the value of stride and crop_szie_y, crop_size_x """ centerA = centerA.astype(float) centerB = centerB.astype(float) stride = params_transform['stride'] crop_size_y = params_transform['crop_size_y'] crop_size_x = params_transform['crop_size_x'] grid_y = crop_size_y / stride grid_x = crop_size_x / stride centerB = centerB / stride centerA = centerA / stride limb_vec = centerB - centerA norm = np.linalg.norm(limb_vec) if norm < 1.0: # print 'limb is too short, ignore it...' return accumulate_vec_map, count limb_vec_unit = limb_vec / norm # print 'limb unit vector: {}'.format(limb_vec_unit) # To make sure not beyond the border of this two points min_x = max(int(round(min(centerA[0], centerB[0]) - thre)), 0) max_x = min(int(round(max(centerA[0], centerB[0]) + thre)), grid_x) min_y = max(int(round(min(centerA[1], centerB[1]) - thre)), 0) max_y = min(int(round(max(centerA[1], centerB[1]) + thre)), grid_y) range_x = list(range(int(min_x), int(max_x), 1)) range_y = list(range(int(min_y), int(max_y), 1)) xx, yy = np.meshgrid(range_x, range_y) ba_x = xx - centerA[0] # the vector from (x,y) to centerA ba_y = yy - centerA[1] limb_width = np.abs(ba_x * limb_vec_unit[1] - ba_y * limb_vec_unit[0]) mask = limb_width < thre # mask is 2D vec_map = np.copy(accumulate_vec_map) * 0.0 vec_map[:, yy, xx] = np.repeat(mask[np.newaxis, :, :], 2, axis=0) vec_map[:, yy, xx] *= limb_vec_unit[:, np.newaxis, np.newaxis] mask = np.logical_or.reduce( (np.abs(vec_map[0, :, :]) > 0, np.abs(vec_map[1, :, :]) > 0)) accumulate_vec_map = np.multiply(accumulate_vec_map, count[np.newaxis, :, :]) accumulate_vec_map += vec_map count[mask == True] += 1 mask = count == 0 count[mask == True] = 1 accumulate_vec_map = np.divide(accumulate_vec_map, count[np.newaxis, :, :]) count[mask == True] = 0 return accumulate_vec_map, count
def main(): optimizers = ['L2L', 'L2L_adv', 'Adam', 'Momentum', 'SGD', 'NAG', 'RMSProp'] problem_path = './problems/quadratic.npz' npzfile = np.load(problem_path) problems_w, problems_b = npzfile['arr_0'], npzfile['arr_1'] prob_num = len(problems_w) x = {} obj = {} for optimizer in optimizers: x[optimizer] = np.load(osp.join('./results', optimizer + '.npy')) print('problem_w', problems_w) print('problem_b', problems_b) for prob_idx in range(prob_num): fig = plt.figure(figsize=(10, 6)) for optimizer in optimizers: obj[optimizer] = list( map(lambda x: np.sum((problems_w[prob_idx].dot(x) - problems_b[prob_idx].squeeze()) ** 2), x[optimizer][prob_idx])) plt.plot(obj[optimizer], label=optimizer) print('Plotting: problem {}, optimizer {}, loss {}, x {}'.format(prob_idx, optimizer, obj[optimizer][-1], x[optimizer][prob_idx][-1])) plt.legend(loc='upper right') plt.xlabel('number of iterations') plt.ylabel('objective value') ax = fig.add_subplot(1, 1, 1) ax.set_yscale("log") plt.savefig('./figs/loss_prob_{}.png'.format(prob_idx)) # X = {} # dictionary to store data # obj = {} # dictionary to store obj value # W = 0.5 * np.eye(2) # Y = 0.5 * np.ones(2) # for name in names: # X[name] = np.loadtxt(name + '.txt') # obj[name] = list(map(lambda x: LA.norm(W.dot(x) - Y) ** 2, X[name])) # plt.plot(obj[name], label=name) # plt.legend(loc='upper right') # plt.xlabel('number of iterations') # plt.ylabel('objective value') # # # plot level set and trajectory for SGD # W = 0.5 * np.eye(2) # Y = 0.5 * np.ones(2) n = len(optimizers) for prob_idx in range(prob_num): fig = plt.figure(figsize=(12, 8)) for i, optimizer in enumerate(optimizers): W = problems_w[prob_idx] Y = problems_b[prob_idx] minx = np.min(x[optimizer][prob_idx][:, 0]) miny = np.min(x[optimizer][prob_idx][:, 1]) maxx = np.max(x[optimizer][prob_idx][:, 0]) maxy = np.max(x[optimizer][prob_idx][:, 1]) min_plot = min(minx, miny) max_plot = max(maxx, maxy) t_min = min_plot - 0.3 * (max_plot - min_plot) t_max = max_plot + 0.3 * (max_plot - min_plot) x1 = np.linspace(t_min, t_max, num=50) x2 = np.linspace(t_min, t_max, num=50) X1, X2 = np.meshgrid(x1, x2) Z = f_quad(X1, X2, W, Y.squeeze()) ax = fig.add_subplot(2, (n+1)/2, i+1) ax.contour(X1, X2, Z, 20) ax.set_aspect('equal') ax.axis([t_min, t_max, t_min, t_max]) ax.set_title(optimizer) ax.scatter(x[optimizer][prob_idx][:, 0], x[optimizer][prob_idx][:, 1], s=5, edgecolors='r', facecolors='none', marker='o') ax.plot(x[optimizer][prob_idx][:, 0], x[optimizer][prob_idx][:, 1], color='r') #plt.colorbar() plt.savefig('./figs/contour_{}.png'.format(prob_idx))
def _all_rgb(): """Return all 256**3 valid rgb tuples.""" base = np.arange(256, dtype=np.uint8) r, g, b = np.meshgrid(base, base, base, indexing='ij') return np.stack((r, g, b), axis=-1).reshape((-1, 3))
# Predicting the Test set results y_pred = classifier.predict(X_test) # Making the Confusion Matrix from sklearn.metrics import confusion_matrix cm = confusion_matrix(y_test, y_pred) # Visualising the Training set results from matplotlib.colors import ListedColormap X_set, y_set = X_train, y_train X1, X2 = np.meshgrid( np.arange(start=X_set[:, 0].min() - 1, stop=X_set[:, 0].max() + 1, step=0.01), np.arange(start=X_set[:, 1].min() - 1, stop=X_set[:, 1].max() + 1, step=0.01)) plt.contourf(X1, X2, classifier.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha=0.75, cmap=ListedColormap(('red', 'green', 'blue'))) plt.xlim(X1.min(), X1.max()) plt.ylim(X2.min(), X2.max()) for i, j in enumerate(np.unique(y_set)): plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1], c=ListedColormap(('red', 'green', 'blue'))(i),
if __name__ == '__main__': n = 4 V = np.arange(0, n, 1) E = [(0, 1, 1.0), (0, 2, 1.0), (1, 2, 1.0), (3, 2, 1.0), (3, 1, 1.0)] G = nx.Graph() G.add_nodes_from(V) G.add_weighted_edges_from(E) step_size = 0.1 a_gamma = np.arange(0, np.pi, step_size) a_beta = np.arange(0, np.pi, step_size) a_gamma, a_beta = np.meshgrid(a_gamma, a_beta) F1 = 3 - (np.sin(2 * a_beta) ** 2 * np.sin(2 * a_gamma) ** 2 - 0.5 * np.sin(4 * a_beta) * np.sin(4 * a_gamma)) * ( 1 + np.cos(4 * a_gamma) ** 2) result = np.where(F1 == np.amax(F1)) a = list(zip(result[0], result[1]))[0] gamma = a[0] * step_size beta = a[1] * step_size prog = make_circuit(4) sample_shot =3962 writefile = open("../data/startQiskit_noisy414.csv", "w") # prog.draw('mpl', filename=(kernel + '.png')) backend = FakeYorktown()
else: m = Basemap(projection='cyl', llcrnrlat=llat, urcrnrlat=ulat, llcrnrlon=llon, urcrnrlon=ulon, resolution='c', area_thresh=1000., ax=ax[mon]) m.drawcoastlines(linewidth=0.5) m.drawcountries(linewidth=0.5) m.drawparallels(np.arange(-90., 90., 10.)) m.drawmeridians(np.arange(-180., 180., 10.)) #m.drawlsmask(land_color='lightgrey',ocean_color='grey') lats = range(-90, 90) lons = range(-180, 180) X, Y = np.meshgrid(lons, lats) m.pcolormesh(X, Y, mongrid[mon], norm=colors.LogNorm(vmin=np.nanmin(mongrid[mon]), vmax=np.nanmax(mongrid[mon])), cmap='jet') ax[mon].title.set_text(names[mon]) plt.tight_layout() plt.suptitle(year) #+' - surface to '+str(limit)+'m') plt.savefig(path + '/plots/' + year + '_' + site + '.png') plt.close() print('Plot saved')
# --------------------------------------------------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------------------------------------------------- # --------------------------------------------------------------------------------------------------------------------------------------------------- # PLOT ALL BAND STRUCTURES (ONE FOR EACH VALUE OF KZ) ON 3D PLOT if delta > 0: fig4 = plt.figure() ax4 = fig4.gca(projection='3d') x = range(len(freqss[0])) kz = kz_list X, KZ = np.meshgrid(x, kz) # Plot bands for index in integer_list: ax4.plot(x, freqss[index], zs=kz[index], zdir='y', color='red', alpha=0.2) ax4.set_xlim([x[0], x[-1]]) # x-axis goes from x[0] = 0 to x[-1] = 33; x[-1] = x[33], x[-2] = x[32], etc. ax4.set_ylim([0.0, max_kz]) upper_z = freqss[-1][-1][-1] + 0.05 ax4.set_zlim([0.0, upper_z]) # z-axis goes from 0 to upper_z # Plot labels if block == 'y': plt.title('Band Structure of Square Photonic Bandgap Fiber; $\epsilon$_core = ' + coreepsilonstring + '; $\epsilon$_PC = ' + pcepsilonstring, size=18) else:
# Define datasets blobs_params = dict(random_state=0, n_samples=n_inliers, n_features=2) datasets = [ make_blobs(centers=[[0, 0], [0, 0]], cluster_std=0.5, **blobs_params)[0], make_blobs(centers=[[2, 2], [-2, -2]], cluster_std=[0.5, 0.5], **blobs_params)[0], make_blobs(centers=[[2, 2], [-2, -2]], cluster_std=[1.5, .3], **blobs_params)[0], 4. * (make_moons(n_samples=n_samples, noise=.05, random_state=0)[0] - np.array([0.5, 0.25])), 14. * (np.random.RandomState(42).rand(n_samples, 2) - 0.5)] # Compare given classifiers under given settings xx, yy = np.meshgrid(np.linspace(-7, 7, 150), np.linspace(-7, 7, 150)) plt.figure(figsize=(len(anomaly_algorithms) * 2 + 3, 12.5)) plt.subplots_adjust(left=.02, right=.98, bottom=.001, top=.96, wspace=.05, hspace=.01) plot_num = 1 rng = np.random.RandomState(42) for i_dataset, X in enumerate(datasets): # Add outliers X = np.concatenate([X, rng.uniform(low=-6, high=6, size=(n_outliers, 2))], axis=0) for name, algorithm in anomaly_algorithms: t0 = time.time()
""" iris = datasets.load_iris() X = iris.data[:, :2] Y = iris.target # 建模 Linearsvc = svm.LinearSVC(dual=False).fit(X, Y) Linear_svc = svm.SVC(kernel='linear').fit(X, Y) # Linear kernel Poly_svc = svm.SVC(kernel='poly').fit(X, Y) # Ploy kernel Rbf_svc = svm.SVC(C=2.0).fit(X, Y) # Rbf kernel # 网格 x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1 y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02)) # 生成采样点 # Title titles = ['LinearSVC (linear kernel)', 'SVC with linear kernel', 'SVC with polynomial kernel', 'SVC with RBF kernel'] for i, clf in enumerate((Linearsvc, Linear_svc, Poly_svc, Rbf_svc)): plt.subplot(2, 2, i+1) plt.subplots_adjust(wspace=0.3, hspace=0.6) # 控制图之间的间隙 Z = clf.predict(np.c_[xx.ravel(), yy.ravel()]).reshape(xx.shape) plt.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8) plt.scatter(X[:, 0], X[:, 1], c=Y, s=30, cmap=plt.cm.coolwarm, alpha=0.8)
def make_meshgrid(x, y, h=.02): x_min, x_max = x.min() - 1, x.max() + 1 y_min, y_max = y.min() - 1, y.max() + 1 xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) return xx, yy
def __init__(self, use_kmeans_anchors=False): super(RPN, self).__init__() if use_kmeans_anchors: print('using k-means anchors') self.anchor_scales = self.anchor_scales_kmeans self.anchor_ratios = self.anchor_ratios_kmeans self.anchor_scales_region = self.anchor_scales_kmeans_region self.anchor_ratios_region = self.anchor_ratios_kmeans_region else: print('using normal anchors') self.anchor_scales, self.anchor_ratios = \ np.meshgrid(self.anchor_scales_normal, self.anchor_ratios_normal, indexing='ij') self.anchor_scales = self.anchor_scales.reshape(-1) self.anchor_ratios = self.anchor_ratios.reshape(-1) self.anchor_scales_region, self.anchor_ratios_region = \ np.meshgrid(self.anchor_scales_normal_region, self.anchor_ratios_normal_region, indexing='ij') self.anchor_scales_region = self.anchor_scales_region.reshape(-1) self.anchor_ratios_region = self.anchor_ratios_region.reshape(-1) self.anchor_num = len(self.anchor_scales) self.anchor_num_region = len(self.anchor_scales_region) # self.features = VGG16(bn=False) self.features = models.vgg16(pretrained=True).features self.features.__delattr__('30') # to delete the max pooling # by default, fix the first four layers network.set_trainable_param(list(self.features.parameters())[:8], requires_grad=False) # self.features = models.vgg16().features self.conv1 = Conv2d(512, 512, 3, same_padding=True) self.score_conv = Conv2d(512, self.anchor_num * 2, 1, relu=False, same_padding=False) self.bbox_conv = Conv2d(512, self.anchor_num * 4, 1, relu=False, same_padding=False) self.conv1_region = Conv2d(512, 512, 3, same_padding=True) self.score_conv_region = Conv2d(512, self.anchor_num_region * 2, 1, relu=False, same_padding=False) self.bbox_conv_region = Conv2d(512, self.anchor_num_region * 4, 1, relu=False, same_padding=False) # loss self.cross_entropy = None self.loss_box = None self.cross_entropy_region = None self.loss_box_region = None # initialize the parameters self.initialize_parameters()
def do_elastic_transform2(image, mask, grid=32, distort=0.2): borderMode = cv2.BORDER_REFLECT_101 height, width = image.shape[:2] x_step = int(grid) xx = np.zeros(width, np.float32) prev = 0 for x in range(0, width, x_step): start = x end = x + x_step if end > width: end = width cur = width else: cur = prev + x_step * (1 + random.uniform(-distort, distort)) xx[start:end] = np.linspace(prev, cur, end - start) prev = cur y_step = int(grid) yy = np.zeros(height, np.float32) prev = 0 for y in range(0, height, y_step): start = y end = y + y_step if end > height: end = height cur = height else: cur = prev + y_step * (1 + random.uniform(-distort, distort)) yy[start:end] = np.linspace(prev, cur, end - start) prev = cur #grid map_x, map_y = np.meshgrid(xx, yy) map_x = map_x.astype(np.float32) map_y = map_y.astype(np.float32) #image = map_coordinates(image, coords, order=1, mode='reflect').reshape(shape) image = cv2.remap(image, map_x, map_y, interpolation=cv2.INTER_LINEAR, borderMode=borderMode, borderValue=( 0, 0, 0, )) mask = cv2.remap(mask, map_x, map_y, interpolation=cv2.INTER_NEAREST, borderMode=borderMode, borderValue=( 0, 0, 0, )) mask = (mask > 0.5).astype(np.float32) return image, mask
def get_radolan_grid(nrows=None, ncols=None, trig=False, wgs84=False): """Calculates x/y coordinates of radolan grid of the German Weather Service .. versionadded:: 0.4.0 Returns the x,y coordinates of the radolan grid positions (lower left corner of every pixel). The radolan grid is a polarstereographic projection, the projection information was taken from RADOLAN-RADVOR-OP Kompositformat_2.2.2 :cite:`DWD2009` .. table:: Coordinates for 900km x 900km grid +------------+-----------+------------+-----------+-----------+ | Coordinate | lon | lat | x | y | +============+===========+============+===========+===========+ | LowerLeft | 3.5889E | 46.9526N | -523.4622 | -4658.645 | +------------+-----------+------------+-----------+-----------+ | LowerRight | 14.6209E | 47.0705N | 376.5378 | -4658.645 | +------------+-----------+------------+-----------+-----------+ | UpperRight | 15.7208E | 54.7405N | 376.5378 | -3758.645 | +------------+-----------+------------+-----------+-----------+ | UpperLeft | 2.0715E | 54.5877N | -523.4622 | -3758.645 | +------------+-----------+------------+-----------+-----------+ .. table:: Coordinates for 1100km x 900km grid +------------+-----------+------------+-----------+-----------+ | Coordinate | lon | lat | x | y | +============+===========+============+===========+===========+ | LowerLeft | 4.6759E | 46.1929N | -443.4622 | -4758.645 | +------------+-----------+------------+-----------+-----------+ | LowerRight | 15.4801E | 46.1827N | 456.5378 | -4758.645 | +------------+-----------+------------+-----------+-----------+ | UpperRight | 17.1128E | 55.5342N | 456.5378 | -3658.645 | +------------+-----------+------------+-----------+-----------+ | UpperLeft | 3.0889E | 55.5482N | -433.4622 | -3658.645 | +------------+-----------+------------+-----------+-----------+ .. table:: Coordinates for 1500km x 1400km grid +------------+-----------+------------+-----------+-----------+ | Coordinate | lon | lat | x | y | +============+===========+============+===========+===========+ | LowerLeft | 2.3419E | 43.9336N | -673.4622 | -5008.645 | +------------+-----------+------------+-----------+-----------+ Parameters ---------- nrows : int number of rows (460, 900 by default, 1100, 1500) ncols : int number of columns (460, 900 by default, 1400) trig : boolean if True, uses trigonometric formulas for calculation if False, uses osr spatial reference system to transform between projections `trig` is recommended to be False, however, the two ways of computation are expected to be equivalent. wgs84 : boolean if True, output coordinates are in wgs84 lonlat format (default: False) Returns ------- radolan_grid : :class:`numpy:numpy.ndarray` Array of shape (rows, cols, 2) xy- or lonlat-grid. Examples -------- >>> # using osr spatial reference transformation >>> import wradlib.georef as georef # noqa >>> radolan_grid = georef.get_radolan_grid() >>> print("{0}, ({1:.4f}, {2:.4f})".format(radolan_grid.shape, *radolan_grid[0,0,:])) # noqa (900, 900, 2), (-523.4622, -4658.6447) >>> # using pure trigonometric transformations >>> import wradlib.georef as georef >>> radolan_grid = georef.get_radolan_grid(trig=True) >>> print("{0}, ({1:.4f}, {2:.4f})".format(radolan_grid.shape, *radolan_grid[0,0,:])) # noqa (900, 900, 2), (-523.4622, -4658.6447) >>> # using osr spatial reference transformation >>> import wradlib.georef as georef >>> radolan_grid = georef.get_radolan_grid(1500, 1400) >>> print("{0}, ({1:.4f}, {2:.4f})".format(radolan_grid.shape, *radolan_grid[0,0,:])) # noqa (1500, 1400, 2), (-673.4622, -5008.6447) >>> # using osr spatial reference transformation >>> import wradlib.georef as georef >>> radolan_grid = georef.get_radolan_grid(900, 900, wgs84=True) >>> print("{0}, ({1:.4f}, {2:.4f})".format(radolan_grid.shape, *radolan_grid[0,0,:])) # noqa (900, 900, 2), (3.5889, 46.9526) See :ref:`notebooks/radolan/radolan_grid.ipynb#Polar-Stereographic-Projection`. # noqa Raises ------ TypeError, ValueError """ # setup default parameters in dicts tiny = {'j_0': 450, 'i_0': 450, 'res': 2} small = {'j_0': 460, 'i_0': 460, 'res': 2} normal = {'j_0': 450, 'i_0': 450, 'res': 1} normal_wx = {'j_0': 370, 'i_0': 550, 'res': 1} extended = {'j_0': 600, 'i_0': 800, 'res': 1} griddefs = {(450, 450): tiny, (460, 460): small, (900, 900): normal, (1100, 900): normal_wx, (1500, 1400): extended} # type and value checking if nrows and ncols: if not (isinstance(nrows, int) and isinstance(ncols, int)): raise TypeError("wradlib.georef: Parameter *nrows* " "and *ncols* not integer") if (nrows, ncols) not in griddefs.keys(): raise ValueError("wradlib.georef: Parameter *nrows* " "and *ncols* mismatch.") else: # fallback for call without parameters nrows = 900 ncols = 900 # tiny, small, normal or extended grid check # reference point changes according to radolan composit format j_0 = griddefs[(nrows, ncols)]['j_0'] i_0 = griddefs[(nrows, ncols)]['i_0'] res = griddefs[(nrows, ncols)]['res'] x_0, y_0 = get_radolan_coords(9.0, 51.0, trig=trig) x_arr = np.arange(x_0 - j_0, x_0 - j_0 + ncols * res, res) y_arr = np.arange(y_0 - i_0, y_0 - i_0 + nrows * res, res) x, y = np.meshgrid(x_arr, y_arr) radolan_grid = np.dstack((x, y)) if wgs84: if trig: # inverse projection lon0 = 10. # central meridian of projection lat0 = 60. # standard parallel of projection sinlat0 = np.sin(np.radians(lat0)) fac = (6370.040 ** 2.) * ((1. + sinlat0) ** 2.) lon = np.degrees(np.arctan((-x / y))) + lon0 lat = np.degrees(np.arcsin((fac - (x ** 2. + y ** 2.)) / (fac + (x ** 2. + y ** 2.)))) radolan_grid = np.dstack((lon, lat)) else: # create radolan projection osr object proj_stereo = create_osr("dwd-radolan") # create wgs84 projection osr object proj_wgs = get_default_projection() radolan_grid = reproject(radolan_grid, projection_source=proj_stereo, projection_target=proj_wgs) return radolan_grid
xx = np.linspace(4, 7.2) plt.scatter(X[y == 0, 0], X[y == 0, 1], color=MAROON, label='\emph{Iris setosa}') plt.scatter(X[y == 1, 0], X[y == 1, 1], color=BLUE, label='\emph{Iris versicolor}') plt.xlim(xx.min(), xx.max()) plt.ylim(1, 5) ax = plt.gca() xlim = ax.get_xlim() ylim = ax.get_ylim() xx = np.linspace(xlim[0], xlim[1], 30) yy = np.linspace(ylim[0], ylim[1], 30) h = 0.002 xx, yy = np.meshgrid(np.arange(xx.min(), xx.max() + h, h), np.arange(1, 5 + h, h)) Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()]) Z = Z[:, 0] - Z[:, 1] Z = Z.reshape(xx.shape) ax.contour(xx, yy, Z, colors='k', levels=[-1, 0, 1], alpha=0.5, linestyles=['--', '-', '--']) ax.contourf(xx, yy, Z, colors=[MAROON, BLUE], levels=[-1, 0, 1], alpha=0.2) ax.contourf(xx, yy, Z, colors=[MAROON, ], levels=[-100, -1], alpha=0.5) ax.contourf(xx, yy, Z, colors=[BLUE, ], levels=[1, 1000], alpha=0.5) plt.legend(loc=1) plt.title('MLP') plt.xlabel('Sepal length')
def read(dictArgs): """Function to read in the data. Returns xarray datasets""" infile = dictArgs["infile"] # Open ice model output and the static file ds = xr.open_mfdataset(infile, combine="by_coords") if "siconc" in ds.variables: ds["CN"] = ds["siconc"] if ds["CN"].max() > 1.0: ds["CN"] = ds["CN"] / 100.0 else: ds["CN"] = ds["CN"].sum(dim="ct") # Detect if we are native grid or 1x1 if (ds.CN.shape[-2] == 180) and (ds.CN.shape[-1] == 360): standard_grid = True else: standard_grid = False if dictArgs["model"] is not None: # use dataset from catalog, either from command line or default cat_platform = ( f"catalogs/{dictArgs['model']}_catalog_{dictArgs['platform']}.yml" ) catfile = pkgr.resource_filename("om4labs", cat_platform) cat = intake.open_catalog(catfile) if standard_grid is True: dstatic = cat["ice_static_1x1"].to_dask() else: dstatic = cat["ice_static"].to_dask() # Override static file if provided if dictArgs["static"] is not None: dstatic = xr.open_dataset(dictArgs["static"]) # Append static fields to the return Dataset if standard_grid is True: _lon = np.array(dstatic["lon"].to_masked_array()) _lat = np.array(dstatic["lat"].to_masked_array()) X, Y = np.meshgrid(_lon, _lat) ds["GEOLON"] = xr.DataArray(X, dims=["lat", "lon"]) ds["GEOLAT"] = xr.DataArray(Y, dims=["lat", "lon"]) _AREA = standard_grid_cell_area(_lat, _lon) _MASK = np.array(dstatic["mask"].fillna(0.0).to_masked_array()) _AREA = _AREA * _MASK ds["CELL_AREA"] = xr.DataArray(_AREA, dims=["lat", "lon"]) ds["AREA"] = xr.DataArray(_AREA, dims=["lat", "lon"]) ds = ds.rename({"lon": "x", "lat": "y"}) else: ds["CELL_AREA"] = dstatic["CELL_AREA"] ds["GEOLON"] = dstatic["GEOLON"] ds["GEOLAT"] = dstatic["GEOLAT"] ds["AREA"] = dstatic["CELL_AREA"] * 4.0 * np.pi * (6.378e6 ** 2) # Get Valid Mask valid_mask = np.where(ds["CELL_AREA"] == 0.0, True, False) # Open observed SIC on 25-km EASE grid (coords already named lat and lon) if dictArgs["obsfile"] is not None: dobs = xr.open_dataset(dictArgs["obsfile"]) else: cat_platform = "catalogs/obs_catalog_" + dictArgs["platform"] + ".yml" catfile = pkgr.resource_filename("om4labs", cat_platform) cat = intake.open_catalog(catfile) dobs = cat[dictArgs["dataset"]].to_dask() # Close the static file (no longer used) dstatic.close() return ds, dobs, valid_mask
def dataproc(ruta, path, paleta): # Open NC file nc = Dataset(path) # Load data data_subset = nc.variables['CMI'][:][:, :] # Create the projection variables ori_proj = nc.variables['goes_imager_projection'] sat_h = ori_proj.perspective_point_height sat_lon = ori_proj.longitude_of_projection_origin sat_sweep = ori_proj.sweep_angle_axis # The projection x and y coordinates equals # the scanning angle (in radians) multiplied by the satellite height (http://proj4.org/projections/geos.html) X = nc.variables['x'][:] * sat_h Y = nc.variables['y'][:] * sat_h p = Proj(proj='geos', h=sat_h, lon_0=sat_lon, sweep=sat_sweep) # Convert map points to latitude and longitude with the magic provided by Pyproj XX, YY = np.meshgrid(X, Y) lons, lats = p(XX, YY, inverse=True) ii = [] jj = [] for i in range(lons.shape[1]): for j in range(lats.shape[0]): if lons[1515, i] >= -100.0 and lats[j, 2325] >= 15.0 and lons[ 1515, i] <= -65.0 and lats[j, 2325] <= 40.0: ii.append(i) jj.append(j) jmin, jmax = (int(np.min(ii)), int(np.max(ii))) imin, imax = (int(np.min(jj)), int(np.max(jj))) # Create map DPI = 150 ax = plt.figure(figsize=(2000 / float(DPI), 2000 / float(DPI)), frameon=True, dpi=DPI) bmap = Basemap(projection='cyl', llcrnrlon=lons[1515, jmin], llcrnrlat=lats[imax, 2325], urcrnrlon=lons[1515, jmax], urcrnrlat=lats[imin, 2325], resolution='l') bmap.drawparallels(np.arange(-90.0, 90.0, 10.0), linewidth=0.1, color='black', labels=[True, False, False, True]) bmap.drawmeridians(np.arange(0.0, 360.0, 10.0), linewidth=0.1, color='black', labels=[True, False, False, True]) # Load Shapefile # bmap.readshapefile('Nueva_DPA','Nueva_DPA',linewidth=0.90,color='darkslategray') # bmap.readshapefile('ne_10m_admin_0_countries','ne_10m_admin_0_countries',linewidth=0.90,color='darkslategray') bmap.drawstates(color='gray', linewidth=0.25) bmap.drawcoastlines(color='k', linewidth=0.9) bmap.drawcountries(color='k', linewidth=0.9) # Converts a CPT file to be used in Python cpt = loadCPT(paleta) # Makes a linear interpolation cpt_convert = LinearSegmentedColormap('cpt', cpt) # Plot the GOES-16 channel with the converted CPT colors #bmap.pcolormesh(lons[imin:imax,jmin:jmax],lats[imin:imax,jmin:jmax],data_subset[imin:imax,jmin:jmax], cmap=cpt_convert, vmin=170, vmax=378) bmap.pcolormesh(lons[imin:imax, jmin:jmax], lats[imin:imax, jmin:jmax], data_subset[imin:imax, jmin:jmax] - 273.15, cmap=cpt_convert, vmin=-103, vmax=104) # Search for the GOES-16 channel in the file name Band = (path[path.find("M3C") + 3:path.find("_G16")]) # Search for the Scan start in the file name Start = (path[path.find("_s") + 2:path.find("_e")]) Start_Formatted = Start[0:4] + " Day " + Start[4:7] + " - " + Start[ 7:9] + ":" + Start[9:11] + ":" + Start[11:13] + "." + Start[ 13:14] + " UTC" # Search for the Scan end in the file name End = (path[path.find("_e") + 2:path.find("_c")]) End_Formatted = End[0:4] + " Day " + End[4:7] + " - " + End[ 7:9] + ":" + End[9:11] + ":" + End[11:13] + "." + End[13:14] + " UTC" # Add a title to the plot plt.title("GOES-16 ABI Band " + Band + "\n Scan from " + Start_Formatted + " to " + End_Formatted, fontsize=10) #bmap.colorbar(location='right', label='Brightness Temperature [K]') bmap.colorbar(location='right', label='Brightness Temperature [C]') # Save the result # Converting from julian day to dd-mm-yyyy year = int(Start[0:4]) dayjulian = int( Start[4:7]) - 1 # Subtract 1 because the year starts at "0" dayconventional = datetime.datetime(year, 1, 1) + datetime.timedelta( dayjulian) # Convert from julian to conventional date = dayconventional.strftime( '%d-%b-%Y') # Format the date according to the strftime directives date_save = dayconventional.strftime('%d%m%Y') time_save = Start[7:9] + Start[9:11] + Start[11:13] plt.savefig('G16_C' + str(Band) + '_' + date_save + '_' + time_save + '_fulldisk.png', dpi=DPI, bbox_inches='tight', pad_inches=0) plt.clf() plt.cla()
from mpl_toolkits.basemap import Basemap import matplotlib.pyplot as plt import numpy as np map = Basemap(projection='sinu', lat_0=0, lon_0=0) lons = np.arange(30, 390, 30) lats = np.arange(0, 100, 10) data = np.indices((lats.shape[0], lons.shape[0])) data = data[0] + data[1] print data print lons lons, data = map.shiftdata(lons, datain=data, lon_0=0) print lons llons, llats = np.meshgrid(lons, lats) x, y = map(llons, llats) map.contourf(x, y, data) map.drawcoastlines() plt.show()
font = {'family': 'Arial', 'weight': 'bold', 'size': 10, } ####LOAD DATA #### obs_data_full = np.genfromtxt('model-data/contour_plot_data.csv', delimiter=',', skip_header=1) ####Interpolation for contours and heatmap R0 = obs_data_full[:,0] I0 = np.log10(obs_data_full[:,1]) prob = obs_data_full[:,4] xi_full, yi_full2 = np.linspace(I0.min(), 2, 300), np.linspace(R0.min(), 1.5, 300) xi_full, yi_full2 = np.meshgrid(xi_full, yi_full2) zi_full2 = scipy.interpolate.griddata((I0, R0), prob, (xi_full, yi_full2), method='linear') #### Make plot fig3 = plt.subplot(1,1,1) plt.contourf(xi_full, yi_full2, zi_full2, 9, cmap=plt.cm.jet, alpha = 0.75) plt.colorbar() CS = plt.contour(xi_full, yi_full2, zi_full2, 9, colors='black', linewidth=.5) # for labels but plotted on the log scale class nf(float): def __repr__(self): str = '%.1f' % (self.__float__(),) if str[-1] == '0':
# ============================================================================= # First file: full-lift-down # ============================================================================= ### Start with uncertainty bars on landline and spaceline ## Load file archive and get data filename = './../results/sweeps/Neptune_27_3sigLow_0.25_180_0522005334.npz' data = np.load(filename, allow_pickle=True) # params = data['params'][0] # array of 1 outsList = data['outsList'] efpaList = data['efpaList'] BCList = data['BCList'] ## Create mesh grid for contour plots, reshape result arrays BCgrid, EFPAgrid = np.meshgrid(BCList, efpaList) fpafgrid = np.reshape([out.fpaf for out in outsList], BCgrid.shape) engfgrid = np.reshape([out.engf for out in outsList], BCgrid.shape) # find line between landing and aerocapture landline_BC_low = BCList landline_EFPA_low = [] for rind in range(fpafgrid.shape[1]): ind = next(ind for ind, val in enumerate(fpafgrid[:,rind])\ if val < 0) landline_EFPA_low.append(efpaList[ind]) # find line between aerocapture and escape spaceline_BC_low = [] spaceline_EFPA_low = [] for rind in range(engfgrid.shape[1]):
def tile_anchors_3d(area_extents, anchor_3d_sizes, anchor_stride, ground_plane): """ Tiles anchors over the area extents by using meshgrids to generate combinations of (x, y, z), (l, w, h) and ry. Args: area_extents: [[min_x, max_x], [min_y, max_y], [min_z, max_z]] anchor_3d_sizes: list of 3d anchor sizes N x (l, w, h) anchor_stride: stride lengths (x_stride, z_stride) ground_plane: coefficients of the ground plane e.g. [0, -1, 0, 0] Returns: boxes: list of 3D anchors in box_3d format N x [x, y, z, l, w, h, ry] """ # Convert sizes to ndarray anchor_3d_sizes = np.asarray(anchor_3d_sizes) anchor_stride_x = anchor_stride[0] anchor_stride_z = anchor_stride[1] anchor_rotations = np.asarray([0, np.pi / 2.0]) x_start = area_extents[0][0] + anchor_stride[0] / 2.0 x_end = area_extents[0][1] x_centers = np.array(np.arange(x_start, x_end, step=anchor_stride_x), dtype=np.float32) z_start = area_extents[2][1] - anchor_stride[1] / 2.0 z_end = area_extents[2][0] z_centers = np.array(np.arange(z_start, z_end, step=-anchor_stride_z), dtype=np.float32) # Use ranges for substitution size_indices = np.arange(0, len(anchor_3d_sizes)) rotation_indices = np.arange(0, len(anchor_rotations)) # Generate matrix for substitution # e.g. for two sizes and two rotations # [[x0, z0, 0, 0], [x0, z0, 0, 1], [x0, z0, 1, 0], [x0, z0, 1, 1], # [x1, z0, 0, 0], [x1, z0, 0, 1], [x1, z0, 1, 0], [x1, z0, 1, 1], ...] before_sub = np.stack(np.meshgrid(x_centers, z_centers, size_indices, rotation_indices), axis=4).reshape(-1, 4) # Place anchors on the ground plane a, b, c, d = ground_plane all_x = before_sub[:, 0] all_z = before_sub[:, 1] all_y = -(a * all_x + c * all_z + d) / b # Create empty matrix to return num_anchors = len(before_sub) all_anchor_boxes_3d = np.zeros((num_anchors, 7)) # Fill in x, y, z all_anchor_boxes_3d[:, 0:3] = np.stack((all_x, all_y, all_z), axis=1) # Fill in shapes sizes = anchor_3d_sizes[np.asarray(before_sub[:, 2], np.int32)] all_anchor_boxes_3d[:, 3:6] = sizes # Fill in rotations rotations = anchor_rotations[np.asarray(before_sub[:, 3], np.int32)] all_anchor_boxes_3d[:, 6] = rotations return all_anchor_boxes_3d
ax = plt.subplot(111) ZM = np.arange(1, 200, 0.35) nx = 384 ny = 216 n_iter = 80 for zm in ZM: zmf = np.exp(-zm/20.) x1 = x0 - 2 * zmf x2 = x0 + 2 * zmf y1 = y0 - 1.13 * zmf y2 = y0 + 1.13 * zmf x, y = np.meshgrid(np.linspace(x1, x2, nx), np.linspace(y1, y2, ny)) c = x + 1j*y z = np.zeros(c.shape) I = np.zeros(c.shape) for k in range(n_iter): z = z ** degree + c ind = (np.abs(z) > tol) * (I == 0) I[ind] = 255 - k ax.imshow(I, extent=(x1, x2, y1, y2)) #, shape=(3840, 2160), interpolation='bilinear') plt.pause(0.001) ax.cla()