def plotComparison(ens_mean, ens_ob_mean, ens_ob_std, obs, ob_locations, refl, grid, levels, cmap, title, file_name): max_std = 5.0 xs, ys = grid.getXY() obs_xs, obs_ys = grid(*ob_locations) clip_box = Bbox([[0, 0], [1, 1]]) pylab.figure() pylab.contourf(xs, ys, ens_mean, cmap=cmap, levels=levels) pylab.colorbar() pylab.contour(xs, ys, refl, colors='k', levels=np.arange(20, 80, 20)) for ob_x, ob_y, ob, ob_mean, ob_std in zip(obs_xs, obs_ys, obs, ens_ob_mean, ens_ob_std): color_bin = np.argmin(np.abs(ob - levels)) if ob > levels[color_bin]: color_bin += 1 color_level = float(color_bin) / len(levels) ob_z_score = (ob - ob_mean) / ob_std print "Ob z-score:", ob_z_score, "Ob:", ob, "Ob mean:", ob_mean, "Ob std:", ob_std pylab.plot(ob_x, ob_y, 'ko', markerfacecolor=cmap(color_level), markeredgecolor=std_cmap(ob_z_score / max_std), markersize=4, markeredgewidth=1) # pylab.text(ob_x - 1000, ob_y + 1000, "%5.1f" % temp_K, ha='right', va='bottom', size='xx-small', clip_box=clip_box, clip_on=True) grid.drawPolitical() pylab.suptitle(title) pylab.savefig(file_name) pylab.close() return
def plot_ch(): for job in jobs_orig: print "plane of", job.path pylab.clf() x_center = int((job.position(0)[0] + job.position(1)[0])/2) x_final = 50 + x_center #plane = np.concatenate((job.plane(y=50)[:, x_final:], # job.plane(y=50)[:, :x_final]), axis=1) plane = job.plane(y=50) myplane = plane[plane < 0.0] p0 = myplane.min() p12 = np.median(myplane) p14 = np.median(myplane[myplane<p12]) p34 = np.median(myplane[myplane>p12]) p1 = myplane.max() contour_values = (p0, p14, p12, p34, p1) pylab.title(r'$u_x=%.4f,\ D_{-}=%.4f,\ D_{+}=%.4f,\ ch=%i$ ' % (job.u_x, job.D_minus, job.D_plus, job.ch_objects)) car = pylab.imshow(plane, vmin=-0.001, vmax=0.0, interpolation='nearest') pylab.contour(plane, contour_values, linestyles='dashed', colors='white') pylab.grid(True) pylab.colorbar(car) #imgfilename = 'plane_r20-y50-u_x%.4fD%.4fch%03i.png' % \ # (job.u_x, job.D_minus, job.ch_objects) imgfilename = 'plane_%s.png' % job.job_id pylab.savefig(imgfilename)
def plot(self): rows = np.sqrt(self.M).round() cols = np.ceil(self.M/rows) if self.D==1: xmin = self.X.min() xmax = self.X.max() xmin,xmax = xmin-0.1*(xmax-xmin), xmax+0.1*(xmax-xmin) Xgrid = np.linspace(xmin,xmax,100)[:,None] zz = self.predict(Xgrid) if self.D==2: xmin,ymin = np.vstack(self.X).min(0) xmax,ymax = np.vstack(self.X).max(0) xmin,xmax = xmin-0.1*(xmax-xmin), xmax+0.1*(xmax-xmin) ymin,ymax = ymin-0.1*(ymax-ymin), ymax+0.1*(ymax-ymin) xx,yy = np.mgrid[xmin:xmax:100j,ymin:ymax:100j] Xgrid = np.vstack((xx.flatten(),yy.flatten())).T zz = self.predict(Xgrid).reshape(100,100,self.M) for m in range(self.M): pb.subplot(rows,cols,m+1) if self.D==1: pb.hist(self.X[m,:,0],self.N/10.,normed=True) pb.plot(Xgrid,zz[:,m],'r',linewidth=2) elif self.D==2: pb.plot(self.X[m,:,0],self.X[m,:,1],'rx',mew=2) zz_data = self.predict(self.X[m])[:,m] pb.contour(xx,yy,zz[:,:,m],[stats.scoreatpercentile(zz_data,5)],colors='r',linewidths=1.5) pb.imshow(zz[:,:,m].T,extent=[xmin,xmax,ymin,ymax],origin='lower',cmap=pb.cm.binary,vmin=0.,vmax=zz_data.max())
def Contour(X,Y,Z, label='', levels=None, cmapidx=0, colors=None, fmt='%g', lwd=1, fsz=10, inline=0, wire=True, cbar=True, zorder=None, markZero='', clabels=True): """ Plot contour ============ """ L = None if levels != None: if not hasattr(levels, "__iter__"): # not a list or array... levels = linspace(Z.min(), Z.max(), levels) if colors==None: c1 = contourf (X,Y,Z, cmap=Cmap(cmapidx), levels=levels, zorder=None) else: c1 = contourf (X,Y,Z, colors=colors, levels=levels, zorder=None) if wire: c2 = contour (X,Y,Z, colors=('k'), levels=levels, linewidths=[lwd], zorder=None) if clabels: clabel (c2, inline=inline, fontsize=fsz) if cbar: cb = colorbar (c1, format=fmt) cb.ax.set_ylabel (label) if markZero: c3 = contour(X,Y,Z, levels=[0], colors=[markZero], linewidths=[2]) if clabels: clabel(c3, inline=inline, fontsize=fsz)
def contour(*args, **kwargs): """ Make an SPH image of the given simulation and render it as contours. nlevels and levels are passed to pyplot's contour command. Other arguments are as for *image*. """ import copy kwargs_image = copy.copy(kwargs) nlevels = kwargs_image.pop('nlevels',None) levels = kwargs_image.pop('levels',None) width = kwargs_image.get('width','10 kpc') kwargs_image['noplot']=True im = image(*args, **kwargs_image) res = im.shape units = kwargs_image.get('units',None) if isinstance(width, str) or issubclass(width.__class__, _units.UnitBase): if isinstance(width, str): width = _units.Unit(width) sim = args[0] width = width.in_units(sim['pos'].units, **sim.conversion_context()) width = float(width) x,y = np.meshgrid(np.linspace(-width/2,width/2,res[0]),np.linspace(-width/2,width/2,res[0])) p.contour(x,y,im,nlevels=nlevels,levels=levels)
def gkde_contours(MC, varname1, varname2, varslice=None, percentiles=[0.0027,0.0455,0.3173,0.5,0.75], colors=[(0.4,0.4,1,0.2),(1,0.4,1,0.5),(1,0.2,0.2,0.75),(1,0.1,0.1,1),(0.8,0.0,0.0,1),(0,0,0,1)], ticklabels=['3$\\sigma$','2$\\sigma$','1$\\sigma$','50%','75%'], fignum=1, ngridpts=101, clear=False,): """ Contours for kernel densit estimate... to compare to real contours """ import scipy.stats data1 = MC.trace(varname1)[slice(*varslice)] data2 = MC.trace(varname2)[slice(*varslice)] gkde = scipy.stats.gaussian_kde([data1,data2]) xvals = np.linspace(data1.min(),data1.max(),ngridpts) yvals = np.linspace(data2.min(),data2.max(),ngridpts) xx,yy = np.meshgrid(xvals, yvals) zz = np.array(gkde.evaluate([xx.flatten(),yy.flatten()])).reshape(xx.shape) pylab.figure(fignum) if clear: pylab.clf() pylab.contour(xx, yy, zz, linewidths=1, alpha=.5, cmap=matplotlib.cm.Greys) pylab.xlabel(varname1) pylab.ylabel(varname2)
def plot_mcmc_results(chain): # Pull m and b arrays out of the Markov chain. mm = [m for b,m in chain] bb = [b for b,m in chain] # Scatterplot of m,b posterior samples plt.clf() plt.contour(bgrid, mgrid, posterior, pdf_contour_levels(posterior)) plt.plot(bb, mm, 'b.', alpha=0.1) plot_mb_setup() plt.show() # Histograms import triangle triangle.corner(chain, labels=['b','m'], extents=[0.99]*2) plt.show() # Traces plt.clf() plt.subplot(2,1,1) plt.plot(mm, 'k-') plt.ylim(mlo,mhi) plt.ylabel('m') plt.subplot(2,1,2) plt.plot(bb, 'k-') plt.ylabel('b') plt.ylim(blo,bhi) plt.show()
def plot_contour(X, X1, X2, clf, title): pl.figure() pl.title(title) # Plot instances of class 1. pl.plot(X1[:, 0], X1[:, 1], "ro") # Plot instances of class 2. pl.plot(X2[:, 0], X2[:, 1], "bo") # Select "support vectors". if hasattr(clf, "support_vectors_"): sv = clf.support_vectors_ else: sv = X[clf.coef_.ravel() != 0] # Plot support vectors. pl.scatter(sv[:, 0], sv[:, 1], s=100, c="g") # Plot decision surface. A, B = np.meshgrid(np.linspace(-6, 6, 50), np.linspace(-6, 6, 50)) C = np.array([[x1, x2] for x1, x2 in zip(np.ravel(A), np.ravel(B))]) Z = clf.decision_function(C).reshape(A.shape) pl.contour(A, B, Z, [0.0], colors="k", linewidths=1, origin="lower") pl.axis("tight")
def doSubplot(multiplier=1.0, layout=(-1, -1)): if time_sec < 16200: xs, ys = xs_1, ys_1 domain_bounds = bounds_1sthalf grid = grid_1 else: xs, ys = xs_2, ys_2 domain_bounds = bounds_2ndhalf grid = grid_2 try: mo = ARPSModelObsFile("%s/%s/KCYS%03dan%06d" % (base_path, exp, min_ens, time_sec)) except AssertionError: mo = ARPSModelObsFile("%s/%s/KCYS%03dan%06d" % (base_path, exp, min_ens, time_sec), mpi_config=(2, 12)) except: print "Can't load reflectivity ..." mo = {'Z':np.zeros((1, 255, 255), dtype=np.float32)} pylab.contour(xs, ys, wind[exp]['w'][wdt][domain_bounds], levels=np.arange(2, 102, 2), styles='-', colors='k') pylab.contour(xs, ys, wind[exp]['w'][wdt][domain_bounds], levels=np.arange(-100, 0, 2), styles='--', colors='k') pylab.quiver(xs[thin], ys[thin], wind[exp]['u'][wdt][domain_bounds][thin], wind[exp]['v'][wdt][domain_bounds][thin]) pylab.contourf(xs, ys, mo['Z'][0][domain_bounds], levels=np.arange(10, 85, 5), cmap=NWSRef, zorder=-10) grid.drawPolitical(scale_len=10) row, col = layout if col == 1: pylab.text(-0.075, 0.5, exp_names[exp], transform=pylab.gca().transAxes, rotation=90, ha='center', va='center', size=12 * multiplier)
def do(beta, eta, iterations, color = 'k'): nhidden = 2 momentum = 0.9 print "%s_beta-%f_eta-%f_iterations-%f" % (test, beta, eta, iterations) tron = mlp.mlp(classC, targetC, nhidden, beta, momentum, 'logistic') tron.mlptrain(classC, targetC, eta, iterations) xrange = numpy.arange( -4 , 4 , 0.1 ) yrange = numpy.arange(-4 , 4 , 0.1 ) xgrid,ygrid = numpy.meshgrid ( xrange , yrange ) noOfPoints = xgrid.shape[ 0 ] * xgrid.shape [ 1 ] xcoords = xgrid.reshape( ( noOfPoints , 1 ) ) ycoords = ygrid.reshape( ( noOfPoints , 1 ) ) samples = numpy.concatenate( ( xcoords , ycoords) , axis=1) ones = -numpy.ones ( xcoords.shape ) samples = numpy.concatenate ( ( samples , ones ) , axis =1) indicator = tron.mlpfwd ( samples ) indicator = indicator.reshape ( xgrid . shape ) pylab.contour ( xrange , yrange , indicator , ( 0.5 , ), colors=color )
def contour(list): xrange = numpy.arange(-5, 5, 0.05) yrange = numpy.arange(-5, 5, 0.05) grid = matrix([[indicator(x, y, list) for y in yrange] for x in xrange]) pylab.contour(xrange, yrange, grid, (-1.0, 0.0, 1.0), colors=('red', 'black', 'blue'), linewidths=(1, 3, 1)) pylab.show()
def view_pcem(self,PCEM): nbins = np.sqrt(len(PCEM)) levels=[0.0,0.01,0.02,0.03,0.04,0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5] #levels2=[0.0,0.01,0.05,0.1,0.2,0.3,0.4,0.5,1.0] T1 = PCEM[:,0].reshape( (nbins,nbins)) T2 = PCEM[:,1].reshape( (nbins,nbins)) E = PCEM[:,2].reshape( (nbins,nbins)) EC = PCEM[:,3].reshape( (nbins,nbins)) M = PCEM[:,4].reshape( (nbins,nbins)) pp.figure() pp.subplot(1,2,1) pp.contour( T1, T2, E, levels,linewidths=0.5,colors="k",linestyles='solid' ) pp.contourf( T1, T2, E, levels, alpha=0.75 ) pp.xlabel( "proposal") pp.ylabel( "current") pp.title( "Error" ) pp.colorbar() # pp.subplot(1,3,2) # pp.contour( T1, T2, EC, levels,linewidths=0.5,colors="k",linestyles='solid' ) # pp.contourf( T1, T2, EC, levels, alpha=0.75 ) # pp.xlabel( "proposal") # pp.ylabel( "current") # pp.colorbar() # pp.title( "Corrected Error" ) pp.subplot(1,2,2) pp.contour( T1, T2, M, 20,linewidths=1.0,colors="k",linestyles='solid' ) pp.contourf( T1, T2, M, 20 ) pp.xlabel( "proposal") pp.ylabel( "current") pp.colorbar() pp.title("MU Z")
def test_masking(): xi, yi = 20*random.rand(2, 100) hi = exp( -(xi-10.)**2/10.0 -(yi-10.)**2/5.0) h = grid.extrap_xy_to_grid(xi, yi, hi) pl.contour(grid.x_rho, grid.y_rho, h) print h pl.show()
def plot_model(gm, dim): X, Y, Z, V = gm.density_on_grid(dim = dim) h = gm.plot(dim = dim) [i.set_linestyle('-.') for i in h] P.contour(X, Y, Z, V) data = gm.sample(200) P.plot(data[:, dim[0]], data[:,dim[1]], '.')
def plot_debug(result): import pylab from pylab import contour, grid, show contours = (70.9/22.4, 70.9/22.4*3, 70.9/22.4*20) contour(result, contours) grid(True) show()
def doSubplot(multiplier=1.0, layout=(-1, -1)): data = cPickle.load(open("cold_pool_%s.pkl" % exp, 'r')) wdt = temporal.getTimes().index(time_sec) try: mo = ARPSModelObsFile("%s/%s/KCYSan%06d" % (base_path, exp, time_sec)) except AssertionError: mo = ARPSModelObsFile("%s/%s/KCYSan%06d" % (base_path, exp, time_sec), mpi_config=(2, 12)) except: print "Can't load reflectivity ..." mo = {'Z':np.zeros((1, 255, 255), dtype=np.float32)} cmap = matplotlib.cm.get_cmap('Blues_r') cmap.set_over('#ffffff') # cmap.set_under(tuple( cmap._segmentdata[c][0][-1] for c in ['red', 'green', 'blue'] )) # cmap.set_under(cmap[0]) xs, ys = grid.getXY() # pylab.pcolormesh(xs, ys, data['t'][2][domain_bounds], cmap=cmap, vmin=288., vmax=295.) pylab.contour(xs, ys, mo['Z'][0][domain_bounds], levels=np.arange(10, 80, 10), colors='k', zorder=10) pylab.contourf(xs, ys, data['t'][wdt][domain_bounds], levels=range(289, 296), cmap=cmap) grid.drawPolitical(scale_len=10) # pylab.plot(track_xs, track_ys, 'mv-', lw=2.5, mfc='k', ms=8) pylab.text(0.05, 0.95, experiments[exp], ha='left', va='top', transform=pylab.gca().transAxes, size=14 * multiplier) return
def polarmap_contours(polarmap): # Example docstring """ Identifies the real and imaginary contours in a polar map. Returns the real and imaginary contours as 2D vertex arrays together with the pairs of contours known to intersect. The coordinate system is normalized so x and y coordinates range from zero to one. Contour plotting requires origin='upper' for consistency with image coordinate system. """ # Convert to polar and normalise normalized_polar = normalize_polar_channel(polarmap) figure_handle = plt.figure() # Real component re_contours_plot = plt.contour(normalized_polar.real, levels=[0], origin='upper') re_path_collections = re_contours_plot.collections[0] re_contour_paths = re_path_collections.get_paths() # Imaginary component im_contours_plot = plt.contour(normalized_polar.imag, levels=[0], origin='upper') im_path_collections = im_contours_plot.collections[0] im_contour_paths = im_path_collections.get_paths() plt.close(figure_handle) intersections = [ (re_ind, im_ind) for (re_ind, re_path) in enumerate(re_contour_paths) for (im_ind, im_path) in enumerate(im_contour_paths) if im_path.intersects_path(re_path)] (ydim, xdim) = polarmap.shape # Contour vertices 0.5 pixel inset. Eg. (0,0)-(48,48)=>(0.5, 0.5)-(47.5, 47.5) # Returned values will not therefore reach limits of 0.0 and 1.0 re_contours = [remove_path_duplicates(re_path.vertices) / [ydim, xdim] \ for re_path in re_contour_paths] im_contours = [remove_path_duplicates(im_path.vertices) / [ydim, xdim] \ for im_path in im_contour_paths] return (re_contours, im_contours, intersections)
def probcontour(xarr,yarr,style='black',smooth=2,bins=100,weights=None,label=None): from scipy import ndimage import numpy H,xbins,ybins = pylab.histogram2d(xarr,yarr,bins=bins,weights=weights) H = ndimage.gaussian_filter(H,smooth) sortH = numpy.sort(H.flatten()) cumH = sortH.cumsum() # 1, 2, 3-sigma, for the old school: lvl68 = sortH[cumH>cumH.max()*0.32].min() lvl95 = sortH[cumH>cumH.max()*0.05].min() lvl99 = sortH[cumH>cumH.max()*0.003].min() if style == 'black': pylab.contour(H.T,[lvl68,lvl95,lvl99],colors=style,\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1])) else: # Shaded areas: if type(style) == str: pylab.contourf(H.T,[lvl99,lvl95],colors=style,alpha=0.2,\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1])) pylab.contourf(H.T,[lvl95,lvl68],colors=style,alpha=0.5,\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1])) pylab.contourf(H.T,[lvl68,1e8],colors=style,alpha=0.9,\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]),label=label) else: pylab.contourf(H.T,[lvl99,lvl95],colors=rgb_to_hex(rgb_alpha(style,0.2)),\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1])) pylab.contourf(H.T,[lvl95,lvl68],colors=rgb_to_hex(rgb_alpha(style,0.5)),\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1])) pylab.contourf(H.T,[lvl68,1e8],colors=rgb_to_hex(rgb_alpha(style,0.9)),\ extent=(xbins[0],xbins[-1],ybins[0],ybins[-1]),label=label)
def plot2d(self,key1='a',key2='b',scale1=0.1,scale2=0.1): """ under development plot the least_squared around the minimum assuming a quadratic approximation given by the hessian """ import pylab key1,key2=key2,key1 keys=self.last_variables.keys() keys.sort() nv=len(self.last_variables) for k in range(len(keys)): if keys[k]==key1: i,vi=k, self.last_variables[key1] if keys[k]==key2: j,vj=k, self.last_variables[key2] ax=pylab.arange(vi*(1.0-10*scale1),vi*(1.0+12*scale1),vi*scale1)[:21] ay=pylab.arange(vj*(1.0-10*scale2),vj*(1.0+12*scale2),vj*scale2)[:21] dv=[0.0]*nv z=[[0 for ik in ax] for jk in ay] i0=0 for x in ax: j0=0 for y in ay: dv[i],dv[j]=x-vi,y-vj least_squares=0.0 for ik in range(nv): for jk in range(nv): least_squares+=0.5*dv[ik]*dv[jk]*self.last_hessian[ik][jk] z[i0][j0]=least_squares-self.last_least_squares j0+=1 i0+=1 pylab.contour(z,extent=(min(ay),max(ay),min(ax),max(ax))) pylab.show()
def plot_function_contour(x, y, function, **kwargs): """Make a contour plot of a function of two variables. Parameters ---------- x, y : array_like of float The positions of the nodes of a plotting grid. function : function The function to plot. filling : bool Fill contours if True (default). num_contours : int The number of contours to plot, 50 by default. xlabel, ylabel : str, optional The axes labels. Empty by default. title : str, optional The title. Empty by default. """ X, Y = numpy.meshgrid(x, y) Z = [] for y_value in y: Z.append([]) for x_value in x: Z[-1].append(function(x_value, y_value)) Z = numpy.array(Z) num_contours = kwargs.get('num_contours', 50) if kwargs.get('filling', True): pylab.contourf(X, Y, Z, num_contours, cmap=pylab.cm.jet) else: pylab.contour(X, Y, Z, num_contours, cmap=pylab.cm.jet) pylab.xlabel(kwargs.get('xlabel', '')) pylab.ylabel(kwargs.get('ylabel', '')) pylab.title(kwargs.get('title', ''))
def doSubplot(multiplier=1.0, layout=(-1, -1)): pylab.contour(xs, ys, vr_obs[0][bounds], colors='k', linestyles='-', levels=np.arange(-50, 60, 10)) pylab.contour(xs, ys, model_obs['vr'][0][bounds], colors='k', linestyles='--', levels=np.arange(-50, 60, 10)) pylab.contourf(xs, ys, (model_obs['vr'][0] - vr_obs[0])[bounds], cmap=matplotlib.cm.get_cmap('RdBu_r'), zorder=-1) grid.drawPolitical() return
def plot_decision_region(x1_min, x2_min, x1_max, x2_max, clf, n_points=1000): print "Plotting decision region" x1, x2 = numpy.meshgrid(numpy.linspace(x1_min, x1_max, n_points), numpy.linspace(x2_min, x2_max, n_points)) z = clf.decision_function(numpy.c_[x1.ravel(), x2.ravel()]).reshape(x1.shape) pylab.contour(x1, x2, z, levels=[0.0], linestyles="solid", linewidths=2.0) pylab.contour(x1, x2, z, levels=[-1.0, 1.0], linestyles="dashed", linewidths=1.0)
def visualize_fits(fitsfile, figname, colorbar = False) : '''Saves fitsfile contents as an image, <figname>.png''' pl.figure() pl.contour(get_fits_obj(fitsfile)) if colorbar: pl.colorbar() pl.savefig(figname+'.png')
def main(): print("Running main method.") print("") # Generate the datapoints and plot them. classA, classB = gen_datapoints_helper() kernels = [LINEAR, POLYNOMIAL, RADIAL_BASIS, SIGMOID] for k in kernels: pylab.figure() pylab.title(kernel_name(k)) pylab.plot([p[0] for p in classA], [p[1] for p in classA], 'bo') pylab.plot([p[0] for p in classB], [p[1] for p in classB], 'ro') # Add the sets together and shuffle them around. datapoints = gen_datapoints_from_classes(classA, classB) t= train(datapoints, k) # Plot the decision boundaries. xr=numpy.arange(-4, 4, 0.05) yr=numpy.arange(-4, 4, 0.05) grid=matrix([[indicator(t, x, y, k) for y in yr] for x in xr]) pylab.contour(xr, yr, grid, (-1.0, 0.0, 1.0), colors=('red', 'black', 'blue'), linewidths=(1, 3, 1)) # Now that we are done we show the ploted datapoints and the decision # boundary. pylab.show()
def main(): import pylab # Create the gaussian data Xin, Yin = pylab.mgrid[0:201, 0:201] data = gaussian(3, 100, 100, 20, 40)(Xin, Yin) + np.random.random(Xin.shape) pylab.matshow(data, cmap='gist_rainbow') params = fitgaussian(data) fit = gaussian(*params) pylab.contour(fit(*pylab.indices(data.shape)), cmap='copper') ax = pylab.gca() (height, x, y, width_x, width_y) = params pylab.text(0.95, 0.05, """ x : %.1f y : %.1f width_x : %.1f width_y : %.1f""" %(x, y, width_x, width_y), fontsize=16, horizontalalignment='right', verticalalignment='bottom', transform=ax.transAxes) pylab.show()
def plotRadTilt(plot_data, plot_cmaps, plot_titles, grid, file_name, base_ref=None): subplot_base = 220 n_plots = 4 xs, ys, gs_x, gs_y, map = grid pylab.figure(figsize=(12,12)) pylab.subplots_adjust(left=0.02, right=0.98, top=0.98, bottom=0.02, wspace=0.04) for plot in range(n_plots): pylab.subplot(subplot_base + plot + 1) cmap, vmin, vmax = plot_cmaps[plot] cmap.set_under("#ffffff", alpha=0.0) pylab.pcolormesh(xs - gs_x / 2, ys - gs_y / 2, plot_data[plot] >= -90, vmin=0, vmax=1, cmap=mask_cmap) pylab.pcolormesh(xs - gs_x / 2, ys - gs_y / 2, plot_data[plot], vmin=vmin, vmax=vmax, cmap=cmap) pylab.colorbar() if base_ref is not None: pylab.contour(xs, ys, base_ref, levels=[10.], colors='k', lw=0.5) # if plot == 0: # pylab.contour(xs, ys, refl_88D[:, :, 0], levels=np.arange(10, 80, 10), colors='#808080', lw=0.5) # pylab.plot(radar_x, radar_y, 'ko') pylab.title(plot_titles[plot]) drawPolitical(map) pylab.savefig(file_name) pylab.close() return
def multiple_optima(gene_number=937, resolution=80, model_restarts=10, seed=10000, max_iters=300, optimize=True, plot=True): """ Show an example of a multimodal error surface for Gaussian process regression. Gene 939 has bimodal behaviour where the noisy mode is higher. """ # Contour over a range of length scales and signal/noise ratios. length_scales = np.linspace(0.1, 60., resolution) log_SNRs = np.linspace(-3., 4., resolution) try:import pods except ImportError: print 'pods unavailable, see https://github.com/sods/ods for example datasets' return data = pods.datasets.della_gatta_TRP63_gene_expression(data_set='della_gatta',gene_number=gene_number) # data['Y'] = data['Y'][0::2, :] # data['X'] = data['X'][0::2, :] data['Y'] = data['Y'] - np.mean(data['Y']) lls = GPy.examples.regression._contour_data(data, length_scales, log_SNRs, GPy.kern.RBF) if plot: pb.contour(length_scales, log_SNRs, np.exp(lls), 20, cmap=pb.cm.jet) ax = pb.gca() pb.xlabel('length scale') pb.ylabel('log_10 SNR') xlim = ax.get_xlim() ylim = ax.get_ylim() # Now run a few optimizations models = [] optim_point_x = np.empty(2) optim_point_y = np.empty(2) np.random.seed(seed=seed) for i in range(0, model_restarts): # kern = GPy.kern.RBF(1, variance=np.random.exponential(1.), lengthscale=np.random.exponential(50.)) kern = GPy.kern.RBF(1, variance=np.random.uniform(1e-3, 1), lengthscale=np.random.uniform(5, 50)) m = GPy.models.GPRegression(data['X'], data['Y'], kernel=kern) m.likelihood.variance = np.random.uniform(1e-3, 1) optim_point_x[0] = m.rbf.lengthscale optim_point_y[0] = np.log10(m.rbf.variance) - np.log10(m.likelihood.variance); # optimize if optimize: m.optimize('scg', xtol=1e-6, ftol=1e-6, max_iters=max_iters) optim_point_x[1] = m.rbf.lengthscale optim_point_y[1] = np.log10(m.rbf.variance) - np.log10(m.likelihood.variance); if plot: pb.arrow(optim_point_x[0], optim_point_y[0], optim_point_x[1] - optim_point_x[0], optim_point_y[1] - optim_point_y[0], label=str(i), head_length=1, head_width=0.5, fc='k', ec='k') models.append(m) if plot: ax.set_xlim(xlim) ax.set_ylim(ylim) return m # (models, lls)
def plot_haxby(activation, title): z = 25 fig = pl.figure(figsize=(4, 5.4)) fig.subplots_adjust(bottom=0., top=1., left=0., right=1.) pl.axis('off') # pl.title('SVM vectors') pl.imshow(np.rot90(mean_img[:, 4:58, z]), cmap=pl.cm.gray, interpolation='nearest') pl.imshow(np.rot90(activation[:, 4:58, z]), cmap=pl.cm.hot, interpolation='nearest') mask_house = nibabel.load(h.mask_house[0]).get_data() mask_face = nibabel.load(h.mask_face[0]).get_data() pl.contour(np.rot90(mask_house[:, 4:58, z].astype(np.bool)), contours=1, antialiased=False, linewidths=4., levels=[0], interpolation='nearest', colors=['blue']) pl.contour(np.rot90(mask_face[:, 4:58, z].astype(np.bool)), contours=1, antialiased=False, linewidths=4., levels=[0], interpolation='nearest', colors=['limegreen']) p_h = Rectangle((0, 0), 1, 1, fc="blue") p_f = Rectangle((0, 0), 1, 1, fc="limegreen") pl.legend([p_h, p_f], ["house", "face"]) pl.title(title, x=.05, ha='left', y=.90, color='w', size=28)
def contourFromFunction(XYfunction,plotPoints=100,\ xrange=None,yrange=None,numContours=20,alpha=1.0, contourLines=None): """ Given a 2D function, plots constant contours over the given range. If the range is not given, the current plotting window range is used. """ # set up x and y ranges currentAxis = pylab.axis() if xrange is not None: xvalues = pylab.linspace(xrange[0],xrange[1],plotPoints) else: xvalues = pylab.linspace(currentAxis[0],currentAxis[1],plotPoints) if yrange is not None: yvalues = pylab.linspace(yrange[0],yrange[1],plotPoints) else: yvalues = pylab.linspace(currentAxis[2],currentAxis[3],plotPoints) #coordArray = _coordinateArray2D(xvalues,yvalues) # add extra dimension to this to make iterable? # bug here! need to fix for contour plots z = map( lambda y: map(lambda x: XYfunction(x,y), xvalues), yvalues) if contourLines: pylab.contour(xvalues,yvalues,z,contourLines,alpha=alpha) else: pylab.contour(xvalues,yvalues,z,numContours,alpha=alpha)
def kappa_residual_grid_plot(env, model, base_model, obj_index, with_contours=False, only_contours=False, with_colorbar=True): obj0,data0 = base_model['obj,data'][obj_index] obj1,data1 = model['obj,data'][obj_index] kappa = data1['kappa'] - data0['kappa'] grid = obj1.basis._to_grid(kappa, obj1.basis.subdivision) R = obj1.basis.mapextent kw = {'extent': [-R,R,-R,R], 'interpolation': 'nearest', 'aspect': 'equal', 'origin': 'upper', 'cmap': cm.gist_stern, 'fignum': False} #'vmin': -1, #'vmax': 1} if not only_contours: pl.matshow(grid, **kw) if only_contours or with_contours: kw.update({'colors':'k', 'linewidths':1, 'cmap':None}) pl.contour(grid, **kw) kw.update({'colors':'k', 'linewidths':2, 'cmap':None}) pl.contour(grid, [0], **kw) if with_colorbar: glscolorbar() return
from pylab import cm, imshow, contour, clabel, colorbar, show print("Helly ML!") myMLP = mlpModule.MLP(2, 8, 1) myBack = mlpModule.Backpropagation(myMLP, 0.3, 0.001) print("Backpropagation: ------------------------------") for i in range(5000): myBack.iterate([[0, 0], [0, 1], [1, 0], [1, 1], [0.5, 0.5], [0.75, 0.5], [0.3, 0.5], [0.45, 0.2], [0.2, 0.7]], [[0], [1], [1], [0], [0], [1], [1], [0], [1]]) print(myMLP.compute([0, 0])) print(myMLP.compute([0, 1])) print(myMLP.compute([1, 0])) print(myMLP.compute([1, 1])) #tender a 01 11 11 01 print("------------------------------") x = np.arange(0, 1.0, 0.1) y = np.arange(0, 1.0, 0.1) X, Y = np.meshgrid(x, y) Z = X for i in range(10): for j in range(10): Z[i][j] = myMLP.compute2Arg(X[i][j], Y[i][j]) im = imshow(Z, cmap=cm.RdBu) cset = contour(Z, np.arange(0, 1, 0.2), linewidths=2, cmap=cm.Set2) clabel(cset, inline=True, fmt='%1.1f', fontsize=10) colorbar(im) show()
# X0=(18, 9) => I ~ 72.4 |delta = 4.68E-06 % # # Potting iso-contours of IF can be a good representation of trajectories, # without having to integrate the ODE # #------------------------------------------------------- # plot iso contours nb_points = 80 # grid size x = linspace(0, xmax, nb_points) y = linspace(0, ymax, nb_points) X2, Y2 = meshgrid(x, y) # create the grid Z2 = IF([X2, Y2]) # compute IF on each point f3 = p.figure() CS = p.contourf(X2, Y2, Z2, cmap=p.cm.Purples_r, alpha=0.5) CS2 = p.contour(X2, Y2, Z2, colors='black', linewidths=2.) p.clabel(CS2, inline=1, fontsize=16, fmt='%.f') p.grid() p.xlabel('Number of rabbits') p.ylabel('Number of foxes') p.ylim(1, ymax) p.xlim(1, xmax) p.title('IF contours') f3.savefig('rabbits_and_foxes_3.png') p.show() # # # # vim: set et sts=4 sw=4:
# data since we want to plot the support vectors svc = SVC(C=C[n], kernel="precomputed") svc.fit(gaussianKernel(X, X, sigma[i]), y) # Plot the decision boundary u = linspace(min(X[:, 0]), max(X[:, 0]), 200) v = linspace(min(X[:, 1]), max(X[:, 1]), 200) z = zeros(shape=(len(u), len(v))) for i in range(len(u)): for j in range(len(v)): z[i, j] = svc.predict( gaussianKernel(array([[u[i], v[j]]]), X, sigma[i])) plot(X[:, 0][y == 1], X[:, 1][y == 1], 'r+', label="c1") plot(X[:, 0][y == 0], X[:, 1][y == 0], 'bo', label="c2") contour(u, v, z.T, [0]) legend(['y = 1', 'y = 0', 'Decision boundary'], numpoints=1) show() #Compute accuracy on the validation set p = svc.predict(gaussianKernel(Xval, Xval, sigma)) counter = 0 for i in range(yval.size): if p[i] == yval[i]: counter += 1 Accuracy[i][j] = (counter / float(y.size) * 100.0) print(Accuracy) print("Best case")
predict2 = array([1, 7.0]).dot(theta).flatten() print 'For population = 70,000, we predict a profit of %f' % (predict2 * 10000) #Plot the results result = it.dot(theta).flatten() plot(data[:, 0], result) show() #Grid over which we will calculate J theta0_vals = linspace(-10, 10, 100) theta1_vals = linspace(-1, 4, 100) #initialize J_vals to a matrix of 0's J_vals = zeros(shape=(theta0_vals.size, theta1_vals.size)) #Fill out J_vals for t1, element in enumerate(theta0_vals): for t2, element2 in enumerate(theta1_vals): thetaT = zeros(shape=(2, 1)) thetaT[0][0] = element thetaT[1][0] = element2 J_vals[t1, t2] = compute_cost(it, y, thetaT) #Contour plot J_vals = J_vals.T #Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100 contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20)) xlabel('theta_0') ylabel('theta_1') scatter(theta[0][0], theta[1][0]) # show()
i))[0] except: break fname_gt = glob("{:s}vol_gt_{:03d}_*".format(dir_out_vols, i))[0] fname_im = glob("{:s}vol_im_{:03d}_*".format(dir_out_vols, i))[0] vol_pred = np.load(fname_pred) vol_gt = np.load(fname_gt) vol_im = np.load(fname_im) # post-processing if to_post_proc: vol_pred = post_proc(vol_pred) dice = dice_wp(vol_pred[np.newaxis], vol_gt[np.newaxis]).asscalar() dice_list.append(dice) print(dice) dice_per_slice = dice_wp(vol_pred, vol_gt).asnumpy() plt.figure(i) if show_fig: for j in range(vol_pred.shape[0]): plt.subplot(n_rows, np.ceil(vol_pred.shape[0] / n_rows), j + 1) plt.imshow(vol_im[1, j], cmap='gray', vmin=0, vmax=1) plt.contour(vol_gt[j], colors='y', linewidths=.5) plt.contour(vol_pred[j], colors='r', linewidths=.5) plt.axis('off') plt.title('{:.2f}'.format(dice_per_slice[j])) plt.show() print('Mean dice: {:.3f}'.format(np.array(dice_list).mean()))
def extract_profiles(name): gridreader = vtk.vtkXMLStructuredGridReader() gridreader.SetFileName(name) gridreader.Update() grid = gridreader.GetOutput() data = grid.GetPointData() points = grid.GetPoints() dims = grid.GetDimensions() velocity = data.GetArray("Velocity") phase = data.GetArray("Phase") vz = numpy.zeros([dims[1], dims[2]]) vy = numpy.zeros([dims[1], dims[2]]) vx = numpy.zeros([dims[1], dims[2]]) phase_numpy = numpy.zeros([dims[1], dims[2]]) print vz.shape print vy.shape #for counter in range(0,points.GetNumberOfPoints()): # coorx,coory,coorz=points.GetPoint(counter) # if coorx==0: # velx,vely,velz=velocity.GetTuple3(counter) # vz[int(coory),int(coorz)]=velz # vy[int(coory),int(coorz)]=vely # vx[int(coory),int(coorz)]=velx # phase_numpy[int(coory),int(coorz)]=phase.GetTuple1(counter) for coory in range(0, dims[1]): for coorz in range(0, dims[2]): counter = coorz * dims[0] * dims[1] + coory * dims[0] velx, vely, velz = velocity.GetTuple3(counter) vz[coory, coorz] = velz vy[coory, coorz] = vely vx[coory, coorz] = velx phase_numpy[coory, coorz] = phase.GetTuple1(counter) numpy.savetxt("vz.txt", vz) numpy.savetxt("vy.txt", vy) numpy.savetxt("phase.txt", phase_numpy[1:, 1:]) #parameters of the binary liquid model k = 0.04 a = 0.04 center = phase_numpy[0, :] z1 = numpy.min(numpy.where(center < 0.0)) z2 = numpy.max(numpy.where(center < 0.0)) if z1 == 0: z2 = numpy.min(numpy.where(center > 0.0)) + dims[2] z1 = numpy.max(numpy.where(center > 0.0)) print z1, z2 mid = ((z1 + z2) / 2) % dims[2] print mid phase_mid = numpy.zeros([dims[0], dims[1]]) #for counter in range(0,points.GetNumberOfPoints()): # coorx,coory,coorz=points.GetPoint(counter) # if coorz==mid: # phase_mid[int(coorx),int(coory)]=phase.GetTuple1(counter) for coorx in range(0, dims[0]): for coory in range(0, dims[1]): counter = mid * dims[0] * dims[1] + coory * dims[0] + coorx phase_mid[coorx, coory] = phase.GetTuple1(counter) #pylab.figure() #pylab.imshow(phase_mid[1:,1:]) prof_axis = phase_mid[0, 1:] prof_diag = numpy.diag(phase_mid[1:, 1:]) print Get_Zero(prof_axis) print Get_Zero(prof_diag) axis_zero = Get_Zero(prof_axis) / (dims[0] - 2.0) diag_zero = math.sqrt(2.0) * Get_Zero(prof_diag) / (dims[0] - 2.0) print "Velx", vx[0, mid] print "Vely", vy[0, mid] print "Velz", vz[0, mid] #Calculation of the capillary number #capillary=vz[0,mid]*(2.0/3.0)/math.sqrt(8.0*k*a/9.0) capillary = vz[0, z2 % dims[2]] * (2.0 / 3.0) / math.sqrt( 8.0 * k * a / 9.0) print axis_zero, diag_zero print "Capillary=", capillary vel_bulk = vz[0, ((z1 + z2 + dims[2]) / 2) % dims[2]] print "Velocity_bulk=", vel_bulk print "Velocity difference=", vz[0, z2 % dims[2]] - vel_bulk vz_diff = vz - vz[0, z2 % dims[2]] numpy.savetxt("vz_diff.txt", vz - vz[0, z2 % dims[2]]) #fig=pylab.figure() #pylab.title(r'''$Ca='''+str(capillary)[0:4]+r'''$''',size=30) #ax1 = fig.add_subplot(111) #ax1.plot(phase_mid[0,:],"b-",linewidth=3) #ax1.set_ylabel("Phase",color='b',size=30) #ax2 = ax1.twinx() #ax2.plot(numpy.abs(vy[:,(z2+20)%dims[2]+1]-vy[:,(z2+20)%dims[2]-1]),"r.",linewidth=3) #ax2.set_ylabel(r'''$\partial_x v_y$''',color='r',size=30) #fig=pylab.figure() #pylab.plot(vy[:,(z2+20)%dims[2]]) #pylab.plot(vz[0,:]-vz[0,z2%dims[2]]) #fig=pylab.figure() #pylab.imshow(phase_numpy,origin="lower") x, y = numpy.mgrid[0:dims[0], 0:dims[2]] deltax = 0.5 / (dims[0] - 1) deltay = 15.0 / (dims[2] - 1) positive = numpy.where(phase_numpy > 0.0) negative = numpy.where(phase_numpy < 0.0) large = numpy.where(x > 25) bounds = numpy.where(numpy.logical_and(y > z1 - 5, y < z2 % dims[2] + 10)) print z1, z2 #,bounds #print y<z1-30 x_short = x[::3, ::25] * deltay y_short = y[::3, ::25] * deltax vz_diff_mask = vz_diff #[numpy.where(phase_numpy>0.0)] vz_diff_mask[negative] = None vz_diff_mask[large] = None vz_diff_mask[bounds] = None velx_short = vz_diff_mask[::3, ::25] vy_mask = vy #[numpy.where(phase_numpy>0.0)] vy_mask[negative] = None vy_mask[large] = None vz_diff_mask[bounds] = None vely_short = vy_mask[::3, ::25] #pylab.figure() #phase_neg=phase_numpy #phase_neg[negative]=None #pylab.imshow(phase_neg, origin="lower") fig = pylab.figure(figsize=[10, 10]) fig = fig.subplots_adjust(bottom=0.15, top=0.8) #axes=fig.xaxis pylab.quiver(y_short, x_short, velx_short, vely_short, scale=0.08, width=0.0033, headwidth=10, headlength=5, headaxislength=5) # ,scale=0.1) pylab.title(r'''$Ca=''' + str(capillary)[0:4] + r'''$''', size=40) pylab.contour(phase_numpy, [0.0], linewidths=[4], extent=(0.0, 15, 0.0, 0.5)) pylab.xticks(fontsize=24) pylab.yticks(fontsize=24) pylab.ylabel(r'''$y$''', fontsize=30) pylab.xlabel(r'''$x$''', fontsize=30) #labels=[r'''$H_{eff}='''+str(value-2)+r'''$''' for value in ny] #leg=pylab.legend(["CPU results","Refined grid","Large body force","Heil","GPU results"],fancybox=True) #legtext = leg.get_texts() # all the text.Text instance in the legend #for text in legtext: # text.set_fontsize(20) #fig.subplots_adjust(left=0.17,bottom=0.17) #for line in ax.yaxis.get_majorticklines(): # line is a Line2D instance #line.set_color('green') #line.set_markersize(25) # line.set_markeredgewidth(2) #for line in ax.xaxis.get_majorticklines(): # line is a Line2D instance #line.set_color('green') #line.set_markersize(25) # line.set_markeredgewidth(2) #for line in ax.yaxis.get_minorticklines(): # line is a Line2D instance #line.set_color('green') #line.set_markersize(25) # line.set_markeredgewidth(2) #for line in ax.xaxis.get_minorticklines(): # line is a Line2D instance #line.set_color('green') #line.set_markersize(25) # line.set_markeredgewidth(2) if capillary >= 1.0: name_cap = str(capillary * 100)[0:3] else: name_cap = str(capillary * 100)[0:2] pylab.xlim(xmax=15) pylab.ylim(ymax=0.5) pylab.savefig("vortex_ca" + name_cap + ".eps", format="EPS", dpi=300) pylab.show()
import pylab import numpy as np import matplotlib.pyplot as plt ax1 = plt.subplot() data = np.loadtxt("tochki.txt") ax1.plot(data[:, 0], data[:, 1], 'r.') ax1.plot(data[:, 0], data[:, 1], lw=2) def makeData(): x = np.arange(-48, 48, 0.05) y = np.arange(-48, 48, 0.05) xgrid, ygrid = np.meshgrid(x, y) zgrid = -42 * xgrid - 12 * ygrid - 3 * xgrid * xgrid - 2 * ygrid * ygrid - 158 return xgrid, ygrid, zgrid if __name__ == '__main__': x, y, z = makeData() pylab.contour(x, y, z) pylab.show()
def find_conj_gradient_minimum(self, max_iteration_number=100, x_start=20, y_start=20, show=True): ''' find minimum of functional on our model parameters with conjugate gradient method ''' # we calling step function depending on type_ # Make data. # X is E W/m^2 # Y is T Celsius E = np.arange(-100, 200, 0.25) T = np.arange(-30, 40, 0.05) X, Y = np.meshgrid(E, T) d1x, d2x = np.shape(X) d1y, d2y = np.shape(Y) X_ = X.reshape((d1x * d2x, 1)) Y_ = Y.reshape((d1y * d2y, 1)) t = 1 # start time z_min = np.array((0,)) y_min = np.array((0,)) x_min = np.array((0,)) x_grad = np.array([x_start]) y_grad = np.array([y_start]) error = np.array((0,)) # conjugate gradient parameters dx1 = 0.1 dx2 = 0.1 lam = 0.001 S1 = 0 S2 = 0 df1 = 0 df2 = 0 for i in range(0, max_iteration_number): # main cycle for gradient iterations ZZ_ = self.vector_calc_fuctional(X_, Y_, t) # find min of ZZ_ n_min = np.argmin(ZZ_) z_min = np.append(z_min, np.min(ZZ_)) x_min = np.append(x_min, X_[n_min]) y_min = np.append(y_min, Y_[n_min]) # gradient step xg, yg, df1_n, df2_n, S1_n, S2_n = self.conj_gradient_step((i == 0), x_grad[i], \ y_grad[i], dx1, dx2, df1, df2, S1, S2, lam, t) x_grad = np.append(x_grad, xg) y_grad = np.append(y_grad, yg) # remember new parameters S1 = S1_n S2 = S2_n df1 = df1_n df2 = df2_n # error F_now = self.scalar_calc_functional(xg, yg, t) F_min = np.min(ZZ_) er = (F_now - F_min) error = np.append(error, er) if (i % 10 == 0 and show): print(i) # plot all steps Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) # plotting min point trajectory pl.plot(x_min, y_min, "-b") # plotting gradient steps trajectory pl.plot(x_grad, y_grad, "-or") # plotting where is min point now pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.show() # print(x_grad[i], y_grad[i], t) # print(x_min[i], y_min[i]) t = t + 5 * self.param['dt'] # at the end of all iterations: if (show): Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # Add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) pl.plot(x_min, y_min, "-b") pl.plot(x_grad, y_grad, "-or") pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.savefig("growing.png") pl.show() return error
def plot(self, labels=False, show=True): ''' This function plots 2D and 3D models :param labels: :param show: If True, the plots are displayed at the end of this call. If False, plt.show() should be called outside this function :return: ''' if self.k == 3: import mayavi.mlab as mlab predictFig = mlab.figure(figure='predict') errorFig = mlab.figure(figure='error') if self.testfunction: truthFig = mlab.figure(figure='test') dx = 1 pts = 25j X, Y, Z = np.mgrid[0:dx:pts, 0:dx:pts, 0:dx:pts] scalars = np.zeros(X.shape) errscalars = np.zeros(X.shape) for i in range(X.shape[0]): for j in range(X.shape[1]): for k1 in range(X.shape[2]): errscalars[i][j][k1] = self.predicterr_normalized( [X[i][j][k1], Y[i][j][k1], Z[i][j][k1]]) scalars[i][j][k1] = self.predict_normalized( [X[i][j][k1], Y[i][j][k1], Z[i][j][k1]]) if self.testfunction: tfscalars = np.zeros(X.shape) for i in range(X.shape[0]): for j in range(X.shape[1]): for k1 in range(X.shape[2]): tfplot = tfscalars[i][j][k1] = self.testfunction( [X[i][j][k1], Y[i][j][k1], Z[i][j][k1]]) plot = mlab.contour3d(tfscalars, contours=15, transparent=True, figure=truthFig) plot.compute_normals = False # obj = mlab.contour3d(scalars, contours=10, transparent=True) plot = mlab.contour3d(scalars, contours=15, transparent=True, figure=predictFig) plot.compute_normals = False errplt = mlab.contour3d(errscalars, contours=15, transparent=True, figure=errorFig) errplt.compute_normals = False if show: mlab.show() if self.k == 2: fig = pylab.figure(figsize=(8, 6)) samplePoints = zip(*self.X) # Create a set of data to plot plotgrid = 61 x = np.linspace(self.normRange[0][0], self.normRange[0][1], num=plotgrid) y = np.linspace(self.normRange[1][0], self.normRange[1][1], num=plotgrid) x = np.linspace(0, 1, num=plotgrid) y = np.linspace(0, 1, num=plotgrid) X, Y = np.meshgrid(x, y) # Predict based on the optimized results zs = np.array([ self.predict([x, y]) for x, y in zip(np.ravel(X), np.ravel(Y)) ]) Z = zs.reshape(X.shape) # Z = (Z*(self.ynormRange[1]-self.ynormRange[0]))+self.ynormRange[0] #Calculate errors zse = np.array([ self.predict_var([x, y]) for x, y in zip(np.ravel(X), np.ravel(Y)) ]) Ze = zse.reshape(X.shape) spx = (self.X[:, 0] * (self.normRange[0][1] - self.normRange[0][0]) ) + self.normRange[0][0] spy = (self.X[:, 1] * (self.normRange[1][1] - self.normRange[1][0]) ) + self.normRange[1][0] contour_levels = 25 ax = fig.add_subplot(222) CS = pylab.contourf(X, Y, Ze, contour_levels) pylab.colorbar() pylab.plot(spx, spy, 'ow') ax = fig.add_subplot(221) if self.testfunction: # Setup the truth function zt = self.testfunction(np.array(zip(np.ravel(X), np.ravel(Y)))) ZT = zt.reshape(X.shape) CS = pylab.contour(X, Y, ZT, contour_levels, colors='k', zorder=2) # contour_levels = np.linspace(min(zt), max(zt),50) if self.testfunction: contour_levels = CS.levels delta = np.abs(contour_levels[0] - contour_levels[1]) contour_levels = np.insert(contour_levels, 0, contour_levels[0] - delta) contour_levels = np.append(contour_levels, contour_levels[-1] + delta) CS = plt.contourf(X, Y, Z, contour_levels, zorder=1) pylab.plot(spx, spy, 'ow', zorder=3) pylab.colorbar() ax = fig.add_subplot(212, projection='3d') # fig = plt.gcf() #ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z, rstride=3, cstride=3, alpha=0.4) if self.testfunction: ax.plot_wireframe(X, Y, ZT, rstride=3, cstride=3) if show: pylab.show()
def find_triangle_minimum(self, alpha, beta, gamma, max_iteration_number=40, x_start=20, y_start=20, show=True): ''' find minimum of functional on our model parameters with triangle deformation method ''' # Make data. # X is E W/m^2 # Y is T Celsius E = np.arange(-100, 200, 0.25) T = np.arange(-30, 40, 0.05) X, Y = np.meshgrid(E, T) d1x, d2x = np.shape(X) d1y, d2y = np.shape(Y) X_ = X.reshape((d1x * d2x, 1)) Y_ = Y.reshape((d1y * d2y, 1)) t = 1 # start time # Z_ = self.vector_calc_fuctional(X_, Y_, t) z_min = np.array((0,)) y_min = np.array((0,)) x_min = np.array((0,)) # np array for quadratic errors of each iteration error = np.array((0,)) # set start triangle triangles = [] x01 = np.array((x_start, y_start)) x02 = np.array((x_start + 2, y_start)) x03 = np.array((x_start, y_start + 2)) triangles.append([x01, x02, x03]) for i in range(0, max_iteration_number): print("step number {}".format(i)) # main cycle for optimization iterations print("vector calc") ZZ_ = self.noise_vector_calc_fuctional(X_, Y_, t) print("end vector calc") # find min of ZZ_ n_min = np.argmin(ZZ_) z_min = np.append(z_min, np.min(ZZ_)) x_min = np.append(x_min, X_[n_min]) y_min = np.append(y_min, Y_[n_min]) # triangle step print("triangle step") x1, x2, x3, x_mid = self.triangle_step(triangles[i][0], triangles[i][1], triangles[i][2], t, alpha, beta, gamma) print("triangle step done") triangles.append([np.array(x1), np.array(x2), np.array(x3)]) print("scalar calc") F_now = self.scalar_calc_functional(x_mid[0], x_mid[1], t) print(" end scalar calc") # F_min = self.scalar_calc_functional(X_[n_min], Y_[n_min], t) F_min = np.min(ZZ_) er = (F_now - F_min) error = np.append(error, er) if (i % 2 == 0 and show == True): # plot all steps Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) # plotting min point trajectory pl.plot(x_min, y_min, "-b") # plotting triangles axes = pl.gca() for tr in triangles: draw_triangle(axes, tr[0], tr[1], tr[2]) # draw last triangle draw_triangle(axes, triangles[i + 1][0], triangles[i + 1][1], triangles[i + 1][2], color_='g') # plotting where is min point now pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.show() print(triangles[i][0], triangles[i][1], triangles[i][2], t) print(x_min[i], y_min[i]) print(F_now, F_min) t = t + 5 * self.param['dt'] # at the end of all iterations: if (show == True): Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # Add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) pl.plot(x_min, y_min, "-b") # plotting triangles axes = pl.gca() for tr in triangles: draw_triangle(axes, tr[0], tr[1], tr[2]) pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.savefig("triangles.png") pl.show() return error
# we create 40 separable points np.random.seed(0) X = np.r_[np.random.randn(20, 2) - [2,2], np.random.randn(20, 2) + [2, 2]] Y = [0]*20 + [1]*20 # fit the model clf = SGDClassifier(loss="hinge", alpha = 0.01, n_iter=50, fit_intercept=True) clf.fit(X, Y) # plot the line, the points, and the nearest vectors to the plane xx = np.linspace(-5, 5, 10) yy = np.linspace(-5, 5, 10) X1, X2 = np.meshgrid(xx, yy) Z = np.empty(X1.shape) for (i,j), val in np.ndenumerate(X1): x1 = val x2 = X2[i,j] p = clf.decision_function([x1, x2]) Z[i,j] = p[0] levels = [-1.0, 0.0, 1.0] linestyles = ['dashed','solid', 'dashed'] colors = 'k' pl.set_cmap(pl.cm.Paired) pl.contour(X1, X2, Z, levels, colors=colors, linestyles=linestyles) pl.scatter(X[:,0], X[:,1], c=Y) pl.axis('tight') pl.show()
def find_gradient_minimum(self, max_iteration_number=100, x_start=20, y_start=20, show=True, gamma=0.09): ''' find minimum of functional on our model parameters with simple gradient method ''' # Make data. # X is E W/m^2 # Y is T Celsius E = np.arange(-100, 200, 0.25) T = np.arange(-30, 40, 0.05) X, Y = np.meshgrid(E, T) d1x, d2x = np.shape(X) d1y, d2y = np.shape(Y) X_ = X.reshape((d1x * d2x, 1)) Y_ = Y.reshape((d1y * d2y, 1)) t = 1 # start time # Z_ = self.vector_calc_fuctional(X_, Y_, t) z_min = np.array((0,)) y_min = np.array((0,)) x_min = np.array((0,)) x_grad = np.array([x_start]) y_grad = np.array([y_start]) error = np.array((0,)) # gradient parameters dx1 = 0.1 dx2 = 0.1 for i in range(0, max_iteration_number): # main cycle for gradient iterations ZZ_ = self.vector_calc_fuctional(X_, Y_, t) # find min of ZZ_ n_min = np.argmin(ZZ_) z_min = np.append(z_min, np.min(ZZ_)) x_min = np.append(x_min, X_[n_min]) y_min = np.append(y_min, Y_[n_min]) # gradient step xg, yg = self.gradient_step(x_grad[i], y_grad[i], t, dx1, dx2, gamma) x_grad = np.append(x_grad, xg) y_grad = np.append(y_grad, yg) # squared error calculation Fmin = np.min(ZZ_) Fnow = self.scalar_calc_functional(xg, yg, t) er = (Fmin - Fnow) error = np.append(error, er) if (i % 5 == 0 and show): # plot all steps Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) # plotting min point trajectory pl.plot(x_min, y_min, "-b") # plotting gradient steps trajectory pl.plot(x_grad, y_grad, "-or") # plotting where is min point now pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.show() # print(x_grad[i], y_grad[i], t) # print(x_min[i], y_min[i]) t = t + 3 * self.param['dt'] # at the end of all iterations: if (show): Z = ZZ_.reshape((len(T), len(E))) fig = pl.figure() cs = pl.contour(X, Y, Z, 20) pl.clabel(cs, fmt='%.1f', colors="black") # Add a color bar which maps values to colors. fig.colorbar(cs, shrink=0.5, aspect=5) pl.plot(x_min, y_min, "-b") pl.plot(x_grad, y_grad, "-or") pl.plot(X_[n_min], Y_[n_min], "sk") pl.grid() pl.savefig("growing.png") pl.show() return error
# This function train a mixture model with k components, returns the trained # model and the BIC def cluster(data, k, mode='full'): d = data.shape[1] gm = GM(d, k, mode) gmm = GMM(gm) em = EM() em.train(data, gmm, maxiter=20) return gm, gmm.bic(data) # bc will contain a list of BIC values for each model trained bc = [] mode = 'full' P.figure() for k in range(1, 5): # Train a model of k component, and plots isodensity curve P.subplot(2, 2, k) gm, b = cluster(dt, k=k, mode=mode) bc.append(b) X, Y, Z, V = gm.density_on_grid() P.contour(X, Y, Z, V) P.plot(dt[:, 0], dt[:, 1], '.') P.xlabel('duration time (scaled)') P.ylabel('waiting time (scaled)') print "According to the BIC, model with %d components is better" % ( N.argmax(bc) + 1)
def plot_topo_file(topoplotdata): """ Read in a topo or bathy file and produce a pcolor map. """ import os import pylab from pyclaw.data import Data fname = topoplotdata.fname topotype = topoplotdata.topotype if topoplotdata.climits: # deprecated option cmin = topoplotdata.climits[0] cmax = topoplotdata.climits[1] else: cmin = topoplotdata.cmin cmax = topoplotdata.cmax figno = topoplotdata.figno addcolorbar = topoplotdata.addcolorbar addcontour = topoplotdata.addcontour contour_levels = topoplotdata.contour_levels xlimits = topoplotdata.xlimits ylimits = topoplotdata.ylimits coarsen = topoplotdata.coarsen imshow = topoplotdata.imshow gridedges_show = topoplotdata.gridedges_show neg_cmap = topoplotdata.neg_cmap pos_cmap = topoplotdata.pos_cmap print_fname = topoplotdata.print_fname if neg_cmap is None: neg_cmap = colormaps.make_colormap({ cmin: [0.3, 0.2, 0.1], 0: [0.95, 0.9, 0.7] }) if pos_cmap is None: pos_cmap = colormaps.make_colormap({ 0: [.5, .7, 0], cmax: [.2, .5, .2] }) if abs(topotype) == 1: X, Y, topo = topotools.topofile2griddata(fname, topotype) topo = pylab.flipud(topo) Y = pylab.flipud(Y) x = X[0, :] y = Y[:, 0] xllcorner = x[0] yllcorner = y[0] cellsize = x[1] - x[0] elif abs(topotype) == 3: file = open(fname, 'r') lines = file.readlines() ncols = int(lines[0].split()[0]) nrows = int(lines[1].split()[0]) xllcorner = float(lines[2].split()[0]) yllcorner = float(lines[3].split()[0]) cellsize = float(lines[4].split()[0]) NODATA_value = int(lines[5].split()[0]) print "Loading file ", fname print " nrows = %i, ncols = %i" % (nrows, ncols) topo = pylab.loadtxt(fname, skiprows=6, dtype=float) print " Done loading" if 0: topo = [] for i in range(nrows): topo.append(pylab.array(lines[6 + i], )) print '+++ topo = ', topo topo = pylab.array(topo) topo = pylab.flipud(topo) x = pylab.linspace(xllcorner, xllcorner + ncols * cellsize, ncols) y = pylab.linspace(yllcorner, yllcorner + nrows * cellsize, nrows) print "Shape of x, y, topo: ", x.shape, y.shape, topo.shape else: raise Exception("*** Only topotypes 1 and 3 supported so far") if coarsen > 1: topo = topo[slice(0, nrows, coarsen), slice(0, ncols, coarsen)] x = x[slice(0, ncols, coarsen)] y = y[slice(0, nrows, coarsen)] print "Shapes after coarsening: ", x.shape, y.shape, topo.shape if topotype < 0: topo = -topo if figno: pylab.figure(figno) if topoplotdata.imshow: color_norm = Normalize(cmin, cmax, clip=True) xylimits = (x[0], x[-1], y[0], y[-1]) #pylab.imshow(pylab.flipud(topo.T), extent=xylimits, \ pylab.imshow(pylab.flipud(topo), extent=xylimits, \ cmap=cmap, interpolation='nearest', \ norm=color_norm) else: neg_topo = ma.masked_where(topo > 0., topo) all_masked = (ma.count(neg_topo) == 0) if not all_masked: pylab.pcolormesh(x, y, neg_topo, cmap=neg_cmap) pylab.clim([cmin, 0]) if addcolorbar: pylab.colorbar() pos_topo = ma.masked_where(topo < 0., topo) all_masked = (ma.count(pos_topo) == 0) if not all_masked: pylab.pcolormesh(x, y, pos_topo, cmap=pos_cmap) pylab.clim([0, cmax]) if addcolorbar: pylab.colorbar() pylab.axis('scaled') if addcontour: pylab.contour(x, y, topo, levels=contour_levels, colors='k') if gridedges_show: pylab.plot([x[0], x[-1]], [y[0], y[0]], 'k') pylab.plot([x[0], x[-1]], [y[-1], y[-1]], 'k') pylab.plot([x[0], x[0]], [y[0], y[-1]], 'k') pylab.plot([x[-1], x[-1]], [y[0], y[-1]], 'k') if print_fname: fname2 = os.path.splitext(fname)[0] pylab.text(xllcorner + cellsize, yllcorner + cellsize, fname2, color='m') topodata = Data() topodata.x = x topodata.y = y topodata.topo = topo return topodata
surf = ax.plot_surface(X, Y, u[:], rstride=1, cstride=1, cmap=rvb, linewidth=0, antialiased=True) ax.set_zlim(0, 10.5) ax.set_xlabel('$x$') ax.set_ylabel('$y$') title('concentracion bacterias') pyplot.show() im = imshow(u, cmap=rvb) # dibujamos la función # Añadimos las lineas de contorno y las etiquetas a cada una. cset = contour(u, arange(0.7, 2, 0.3), linewidths=1, cmap=cm.binary) clabel(cset, inline=True, fmt='%1.1f', fontsize=10) colorbar(im) # Añadimos la barra de color a la derecha # Título title('concentracion bacterias') #A parte, podemos salvar la figura Des-comentando la siguiente linea #savefig('bacteriaslineas') pyplot.show() u = FKPP(200, nx, condicion_inicial) fig = pyplot.figure() ax = fig.gca(projection='3d') surf = ax.plot_surface(X, Y, u[:], rstride=1,
import DeepDiscovery as dd from DeepDiscovery import tf import nibabel as nib import pylab session = tf.InteractiveSession() modelPath = 'BrainExtraction2d.net' dataPath = 'tal_sub-A00028185_ses-NFB3_t1w.mnc.004.mnc' net = dd.Net.Net.load(modelPath) image = nib.load(dataPath) brainProb = net.segment(image.get_data())[..., 1] output = nib.Nifti2Image(brainProb, image.affine, header=image.header) nib.save(output, 'brainProb.nii') slc = [slice(None), slice(None), slice(None)] slc[1] = 100 pylab.ion() pylab.figure('Brain Probability') pylab.clf() pylab.imshow(image.get_data()[slc], cmap=pylab.cm.gray, origin='lower') pylab.contour(brainProb[slc], [0.5], colors='g')
P = buildP(data) N = 20 q = matrix(-1.0, (N, 1)) # Vector of size N containing only -1 G = matrix(spdiag( N * [-1.0])) # Diagonal matrix with -1 on the diagonal, 0 otherwise h = matrix(0.0, (N, 1)) # Vector of size N containing only 0 # QP Optimizer r = qp(matrix(P), matrix(q), matrix(G), matrix(h)) alpha = list(r['x']) # Get support vectors sv = getSupportVectors(alpha) # Plot data pylab.hold(True) pylab.plot([p[0] for p in classA], [p[1] for p in classA], 'bo') pylab.plot([p[0] for p in classB], [p[1] for p in classB], 'ro') # Plot boundaries xrange = numpy.arange(-4, 4, 0.05) yrange = numpy.arange(-4, 4, 0.05) grid = matrix([[indicator(x, y, data, alpha, sv) for y in yrange] for x in xrange]) pylab.contour(xrange, yrange, grid, (-1.0, 0.0, 1.0), colors=('red', 'black', 'blue'), linewidths=(1, 3, 1)) pylab.show()
# ch1-14-exercises.py # pp. 26 from PIL import Image import numpy as numpy import pylab as pylab from scipy.ndimage import filters # 1: Take an image and apply Gaussian blur. Plot the image contours for increasing blur values. im = pylab.array(Image.open('images/pug.jpg').convert('L')) pylab.gray() pylab.subplot(2, 2, 1) pylab.axis('equal') pylab.contour(im, origin='image') im2 = filters.gaussian_filter(im, 2) pylab.subplot(2, 2, 2) pylab.axis('equal') pylab.contour(im2, origin='image') im3 = filters.gaussian_filter(im, 5) pylab.subplot(2, 2, 3) pylab.axis('equal') pylab.contour(im3, origin='image') im4 = filters.gaussian_filter(im, 8) pylab.subplot(2, 2, 4) pylab.axis('equal') pylab.contour(im4, origin='image')
maxOrder = int(np.log10(np.nanmax(simVals))) minOrder = int(np.log10(np.nanmin(simVals))) - 1 lvls0 = [] lvlLbls = [] fmt = {} for mag in range(minOrder, maxOrder + 1): maglvls = 6 #make 6 contour levels for each magnitude magstep = 1. / maglvls for i in range(maglvls): l0 = 10.**(mag + (i * magstep)) lvls0.append(l0) if mag < 0: fmt[l0] = '%.3f' % (int(l0 / (10.**mag)) * (10.**mag)) else: fmt[l0] = '%i' % (int(l0 / (10.**mag)) * (10.**mag)) CS = p.contour(grid_x, grid_y, grid_z, lvls0) if np.abs(maxOrder - minOrder) > 2: p.clabel(CS, CS.levels[maglvls * 2::4], inline=0, fontsize=25, colors='black', fmt=fmt) #label every 4th level p.clabel(CS, CS.levels[:2 * maglvls], inline=0, fontsize=25, colors='black', fmt=fmt) #label every level of the lowest 2 orders else: p.clabel(
for j in range(nd): ix = int((x[j] - xmin) / xbsize) iy = int((y[j] - ymin) / ybsize) if ix < 0 or ix > nbins-1 or iy < 0 or iy > nbins-1: continue dgrid[iy][ix] += 1 #pl.contour(xarr,yarr,dgrid,10,origin='upper') #pl.plot(data[:nd-60,0],data[:nd-60,1],'.') #pl.plot(x[nd-60:],y[nd-60:],'o') pl.xlabel(r'$\log(N_H)$',fontsize=20) pl.ylabel(r'$V_{\rm exp}$',fontsize=20) pl.contour(xarr,yarr,dgrid,4,colors='gray',linewidths=2) mesh = pl.pcolormesh(xarr,yarr,dgrid,cmap='YlGnBu',alpha=0.75) pl.axis([xmin,xmax,ymin,ymax]) pl.savefig(datan + '_pdf.eps') #pl.show()
def kappa_plot(env, model, obj_index, **kwargs): obj, data = model['obj,data'][obj_index] if not data: return with_contours = kwargs.pop('with_contours', False) only_contours = kwargs.pop('only_contours', False) label_contours = kwargs.pop('label_contours', False) clevels = kwargs.pop('clevels', 30) with_colorbar = kwargs.pop('with_colorbar', True) vmin = kwargs.pop('vmin', None) vmax = kwargs.pop('vmax', None) subtract = kwargs.pop('subtract', 0) xlabel = kwargs.pop('xlabel', r'arcsec') ylabel = kwargs.pop('ylabel', r'arcsec') # print pl.gca() # print pl.gca().get_frame() # print pl.gca().get_frame().get_bbox() # print pl.gca().get_geometry() # print pl.gca().get_position() # print pl.gca().get_window_extent() # l= pl.gca().get_axes_locator() # print l # help(l) # #help(pl.gca()) # assert 0 R = obj.basis.mapextent kw = default_kw(R, kwargs) #grid = obj.basis.kappa_grid(data) #print data['kappa'].shape #print subtract grid = obj.basis._to_grid(data['kappa'] - subtract, 1) if vmin is None: w = data['kappa'] != 0 if not np.any(w): vmin = -15 grid += 10**vmin else: vmin = np.log10(np.amin(data['kappa'][w])) #print 'min?', np.amin(data['kappa'] != 0) kw.setdefault('vmin', vmin) if vmax is not None: kw.setdefault('vmax', vmax) grid = np.log10(grid.copy()) # + 1e-15) # grid2 = grid.copy() # for i in xrange(grid.shape[0]): # for j in xrange(grid.shape[1]): # grid[i,j] = abs(grid2[grid.shape[0]-i-1, grid.shape[1]-j-1] - grid[i,j]) / grid[i,j] # grid = grid.copy() + 1e-4 #grid[grid >= 1] = 0 if not only_contours: #pl.matshow(np.log10(grid), **kw) pl.matshow(grid, **kw) #imshow(grid, fignum=False, **kw) #pl.matshow(grid, fignum=False, **kw) if with_colorbar: glscolorbar() if only_contours or with_contours: #if 'colors' in kw and 'cmap' in kw: #kw.pop('cmap') kw.setdefault('colors', 'w') kw.setdefault('extend', 'both') kw.setdefault('alpha', 0.7) kw.pop('cmap') #kw.pop('colors') C = pl.contour(grid, clevels, **kw) if label_contours: pl.clabel(C, inline=1, fontsize=10) pl.gca().set_aspect('equal') pl.xlabel(xlabel) pl.ylabel(ylabel)
import numpy as np import pylab as pl x, y = np.mgrid[-2.9:5.8:.05, -2.5:5:.05] x = x.T y = y.T pl.figure(figsize=(5, 5)) pl.subplot(1, 2, 1) pl.clf() pl.axes([0, 0, 1, 1]) contours = pl.contour(np.sqrt((x - 3)**2 + (y - 2)**2), extent=[-3, 6, -2.5, 5], cmap=pl.cm.gnuplot) pl.clabel(contours, inline=1, fmt='%1.1f', fontsize=14) pl.plot([-3, 6], [1.5, -1.5], 'k', linewidth=2) pl.plot(3, 2, 'ko') pl.text(3 - .2, 2 + .3, '$Global minimum$', size=15) pl.plot(2.3, -0.29, 'ro') pl.text(2.3 + .2, -0.29, '$Optimum$', size=15) pl.text(6.1, -1.6, '$g(x) = 0 $', size=15) pl.text(6.1, -1, '$g(x) > 0 $', size=15) pl.text(6.1, -2.3, '$g(x) < 0 $', size=15) pl.fill_between([-3, 6], [1.5, -1.5], [-2.5, -2.5], color='.8')
qda.set_labels(labels) qda.train(features) # compute output plot iso-lines xs = np.array(np.concatenate([x_pos, x_neg])) ys = np.array(np.concatenate([y_pos, y_neg])) x1_max = max(1.2 * xs) x1_min = min(1.2 * xs) x2_max = max(1.2 * ys) x2_min = min(1.2 * ys) x1 = np.linspace(x1_min, x1_max, size) x2 = np.linspace(x2_min, x2_max, size) x, y = np.meshgrid(x1, x2) dense = RealFeatures(np.array((np.ravel(x), np.ravel(y)))) dense_labels = qda.apply(dense).get_labels() z = dense_labels.reshape((size, size)) pcolor(x, y, z, shading='interp') contour(x, y, z, linewidths=1, colors='black', hold=True) axis([x1_min, x1_max, x2_min, x2_max]) connect('key_press_event', util.quit) show()
def swi_graphpng(self, dir, name, bcount=100, conf=0.95, x=None, y=None, m=None, multi=None, dpi=80, width=8, height=6, compare='', sortby='', xvals='', yvals='', multivals='', contour_n=20, contour_min='', contour_max='', contour_lines=10, contour_fmt='', xlabel='', ylabel='', xtickrotation='', b_left='', b_bot='', b_top='', b_right=''): pylab.figure(figsize=(float(width), float(height))) try: b_left = float(b_left) except: b_left = 0.1 try: b_right = float(b_right) except: b_right = 0.1 try: b_top = float(b_top) except: b_top = 0.1 try: b_bot = float(b_bot) except: b_bot = 0.1 pylab.axes( (b_left, b_bot, 1.0 - b_left - b_right, 1.0 - b_top - b_bot)) try: xtickrotation = float(xtickrotation) except: xtickrotation = 0 if len(compare) > 0: if '/' not in compare: c_dir, c_name = compare, 'default' else: c_dir, c_name = compare.split('/', 1) c_names, c_sample, c_ci = extract_individual_data( c_dir, c_name, int(bcount), float(conf), m) else: c_names = [] xvals = xvals.split(';') yvals = yvals.split(';') multivals = multivals.split(';') if type(m) is str: m = [m] if x is None and y is None: names, sample, ci = extract_individual_data( dir, name, int(bcount), float(conf), m) if len(c_names) > 0 and sortby != '': reversed = False if sortby.endswith('_r'): reversed = True sortby = sortby[:-2] data = compare_stats(sortby, names, sample, ci, c_names, c_sample, c_ci) data.sort() if reversed: data.reverse() index = [names.index(x[1]) for x in data] names = [names[i] for i in index] sample = pylab.array([sample[i] for i in index]) ci = pylab.array([ci[i] for i in index]) c_sample = pylab.array([c_sample[i] for i in index]) c_ci = pylab.array([c_ci[i] for i in index]) xvalpts = pylab.array(range(len(names))) if len(c_names) > 0: capsize = 4 c_yerr = pylab.array( [c_sample - c_ci[:, 1], c_ci[:, 0] - c_sample]) compare = True barwidth = 0.4 pylab.bar(xvalpts + 0.2, c_sample, align='center', color='#CCCCCC', width=barwidth) pylab.errorbar(xvalpts + 0.2, c_sample, yerr=c_yerr, ecolor='k', capsize=capsize, linewidth=0, elinewidth=1) xvalpts = xvalpts - 0.2 else: capsize = 5 compare = False barwidth = 0.8 pylab.bar(xvalpts, sample, align='center', color='#EEEEEE', width=barwidth) yerr = pylab.array([sample - ci[:, 1], ci[:, 0] - sample]) pylab.errorbar(xvalpts, sample, yerr=yerr, ecolor='k', capsize=capsize, linewidth=0, elinewidth=1) pylab.xticks(range(len(names)), names, rotation=xtickrotation) pylab.xlim(-1, len(names)) elif x is not None and y is None: setting = parse_setting_name(name) xvalpts = pylab.array(range(len(xvals))) gfile = open('most_recent_graph.py', 'w') if False and len(compare) > 0: pass elif multi is not None: for mv in multivals: for measure in m: v = [] vlow = [] vhigh = [] for xval in xvals: setting2 = dict(setting) setting2[x] = xval setting2[multi] = mv name2 = make_setting_name(dir, setting2) names, sample, ci = extract_individual_data( dir, name2, int(bcount), float(conf), measure) v.append(sample[0]) vlow.append(sample[0] - ci[0][1]) vhigh.append(ci[0][0] - sample[0]) if len(multivals) == 1: label = measure elif len(m) == 1: label = mv else: label = '%s:%s' % (mv, measure) pylab.plot(xvalpts, v, label=label) pylab.errorbar(xvalpts, v, yerr=[vlow, vhigh], ecolor='k', capsize=3, linewidth=0, elinewidth=1) gfile.write('pylab.plot(%s,%s)\n' % (list(xvalpts), v)) gfile.write('pylab.errorbar(%s,%s,yerr=[%s,%s])\n' % (list(xvalpts), v, vlow, vhigh)) pylab.legend(loc='best') else: for measure in m: v = [] vlow = [] vhigh = [] for xval in xvals: setting2 = dict(setting) setting2[x] = xval name2 = make_setting_name(dir, setting2) names, sample, ci = extract_individual_data( dir, name2, int(bcount), float(conf), measure) v.append(sample[0]) vlow.append(sample[0] - ci[0][1]) vhigh.append(ci[0][0] - sample[0]) pylab.plot(xvalpts, v, label=measure) pylab.errorbar(xvalpts, v, yerr=[vlow, vhigh], ecolor='k', capsize=3, linewidth=0, elinewidth=1) pylab.legend(loc='best') pylab.xticks(xvalpts, xvals) pylab.xlim(-0.2, xvalpts[-1] + 0.2) if xlabel == '': pylab.xlabel(x) else: pylab.xlabel(xlabel) if ylabel != '': pylab.ylabel(ylabel) elif x is not None and y is not None: setting = parse_setting_name(name) xvalpts = pylab.array(range(len(xvals))) yvalpts = pylab.array(range(len(yvals))) contour_n = int(contour_n) contour_lines = int(contour_lines) if len(contour_max) == 0: contour_max = None else: contour_max = float(contour_max) if len(contour_min) == 0: contour_min = None else: contour_min = float(contour_min) if contour_fmt == '': contour_fmt = '%1.3f' data = [] for yval in yvals: setting2 = dict(setting) setting2[y] = yval row = [] for xval in xvals: setting2[x] = xval name2 = make_setting_name(dir, setting2) names, sample, ci = extract_individual_data( dir, name2, int(bcount), float(conf), m) if len(compare) > 0: d = compare_combine(sortby, names, sample, ci, c_names, c_sample, c_ci) else: d = sample[0] row.append(d) data.append(row) if contour_min is None or contour_max is None: pylab.contourf(xvalpts, yvalpts, data, contour_n, antialiased=True, extend='both') cs = pylab.contour(xvalpts, yvalpts, data, contour_lines, colors='k', linewidths=1) else: clevels = pylab.array(range(contour_n)) * ( contour_max - contour_min) / (contour_n - 1) + contour_min pylab.contourf(xvalpts, yvalpts, data, list(clevels), antialiased=True, extend='both') clevels = pylab.array( range(contour_lines)) * (contour_max - contour_min) / ( contour_lines - 1) + contour_min cs = pylab.contour(xvalpts, yvalpts, data, list(clevels), colors='k', linewidths=1) pylab.clabel(cs, fmt=contour_fmt) pylab.xticks(xvalpts, xvals, rotation=xtickrotation) if xlabel == '': pylab.xlabel(x) else: pylab.xlabel(xlabel) pylab.yticks(yvalpts, yvals) if ylabel == '': pylab.ylabel(y) else: pylab.ylabel(ylabel) img = StringIO.StringIO() if type(dpi) is list: dpi = dpi[-1] pylab.savefig(img, dpi=int(dpi), format='png') pylab.close() return 'image/png', img.getvalue()
def manually_refine_components(Y, xxx_todo_changeme, A, C, Cn, thr=0.9, display_numbers=True, max_number=None, cmap=None, **kwargs): """Plots contour of spatial components against a background image and allows to interactively add novel components by clicking with mouse Parameters ----------- Y: ndarray movie in 2D (dx,dy): tuple dimensions of the square used to identify neurons (should be set to the galue of gsiz) A: np.ndarray or sparse matrix Matrix of Spatial components (d x K) Cn: np.ndarray (2D) Background image (e.g. mean, correlation) thr: scalar between 0 and 1 Energy threshold for computing contours (default 0.995) display_number: Boolean Display number of ROIs if checked (default True) max_number: int Display the number for only the first max_number components (default None, display all numbers) cmap: string User specifies the colormap (default None, default colormap) Returns -------- A: np.ndarray matrix A os estimated spatial component contributions C: np.ndarray array of estimated calcium traces """ (dx, dy) = xxx_todo_changeme if issparse(A): A = np.array(A.todense()) else: A = np.array(A) d1, d2 = np.shape(Cn) d, nr = np.shape(A) if max_number is None: max_number = nr x, y = np.mgrid[0:d1:1, 0:d2:1] pl.imshow(Cn, interpolation=None, cmap=cmap) coordinates = [] cm = com(A, d1, d2) Bmat = np.zeros((np.minimum(nr, max_number), d1, d2)) for i in range(np.minimum(nr, max_number)): pars = dict(kwargs) indx = np.argsort(A[:, i], axis=None)[::-1] cumEn = np.cumsum(A[:, i].flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn Bmat[i] = np.reshape(Bvec, np.shape(Cn), order='F') T = np.shape(Y)[-1] pl.close() fig = pl.figure() # ax = fig.add_subplot(111) ax = pl.gca() ax.imshow(Cn, interpolation=None, cmap=cmap, vmin=np.percentile(Cn[~np.isnan(Cn)], 1), vmax=np.percentile(Cn[~np.isnan(Cn)], 99)) for i in range(np.minimum(nr, max_number)): pl.contour(y, x, Bmat[i], [thr]) if display_numbers: for i in range(np.minimum(nr, max_number)): ax.text(cm[i, 1], cm[i, 0], str(i + 1)) A3 = np.reshape(A, (d1, d2, nr), order='F') while True: pts = fig.ginput(1, timeout=0) if pts != []: print(pts) xx, yy = np.round(pts[0]).astype(np.int) coords_y = np.array(list(range(yy - dy, yy + dy + 1))) coords_x = np.array(list(range(xx - dx, xx + dx + 1))) coords_y = coords_y[(coords_y >= 0) & (coords_y < d1)] coords_x = coords_x[(coords_x >= 0) & (coords_x < d2)] a3_tiny = A3[coords_y[0]:coords_y[-1] + 1, coords_x[0]:coords_x[-1] + 1, :] y3_tiny = Y[coords_y[0]:coords_y[-1] + 1, coords_x[0]:coords_x[-1] + 1, :] # y3med = np.median(y3_tiny,axis=-1) # y3_tiny = y3_tiny - y3med[...,np.newaxis] #y3_tiny = y3_tiny-np.median(y3_tiny,axis=-1) dy_sz, dx_sz = np.shape(a3_tiny)[:-1] y2_tiny = np.reshape(y3_tiny, (dx_sz * dy_sz, T), order='F') a2_tiny = np.reshape(a3_tiny, (dx_sz * dy_sz, nr), order='F') y2_res = y2_tiny - a2_tiny.dot(C) # pl.plot(xx,yy,'k*') y3_res = np.reshape(y2_res, (dy_sz, dx_sz, T), order='F') a__, c__, center__, b_in__, f_in__ = greedyROI( y3_res, nr=1, gSig=[ np.floor(old_div(dx_sz, 2)), np.floor(old_div(dy_sz, 2)) ], gSiz=[dx_sz, dy_sz]) # a__ = model.fit_transform(np.maximum(y2_res,0)); # c__ = model.components_; a_f = np.zeros((d, 1)) idxs = np.meshgrid(coords_y, coords_x) a_f[np.ravel_multi_index(idxs, (d1, d2), order='F').flatten()] = a__ A = np.concatenate([A, a_f], axis=1) C = np.concatenate([C, c__], axis=0) indx = np.argsort(a_f, axis=None)[::-1] cumEn = np.cumsum(a_f.flatten()[indx]**2) cumEn /= cumEn[-1] Bvec = np.zeros(d) Bvec[indx] = cumEn bmat = np.reshape(Bvec, np.shape(Cn), order='F') pl.contour(y, x, bmat, [thr]) pl.pause(.01) elif pts == []: break nr += 1 A3 = np.reshape(A, (d1, d2, nr), order='F') return A, C
def plots(opt): from astrometry.util.plotutils import antigray import tractor.sfd T = fits_table(opt.files[0]) print('Read', len(T), 'bricks summarized in', opt.files[0]) import pylab as plt import matplotlib B = fits_table('survey-bricks.fits.gz') print('Looking up brick bounds') ibrick = dict([(n, i) for i, n in enumerate(B.brickname)]) bi = np.array([ibrick[n] for n in T.brickname]) T.ra1 = B.ra1[bi] T.ra2 = B.ra2[bi] T.dec1 = B.dec1[bi] T.dec2 = B.dec2[bi] assert (np.all(T.ra2 > T.ra1)) T.area = ((T.ra2 - T.ra1) * (T.dec2 - T.dec1) * np.cos(np.deg2rad((T.dec1 + T.dec2) / 2.))) del B del bi del ibrick print('Total sources:', sum(T.nobjs)) print('Approx area:', len(T) / 16., 'sq deg') print('Area:', np.sum(T.area)) print('g,r,z coverage:', sum((T.nexp_g > 0) * (T.nexp_r > 0) * (T.nexp_z > 0)) / 16.) decam = True if decam: release = 'DECaLS DR8' else: release = 'BASS+MzLS DR8' if decam: # DECam #ax = [360, 0, -21, 36] ax = [300, -60, -75, 36] def map_ra(r): return r + (-360 * (r > 300)) else: # MzLS+BASS ax = [310, 90, 30, 80] def map_ra(r): return r udec = np.unique(T.dec) print('Number of unique Dec values:', len(udec)) print('Number of unique Dec values in range', ax[2], ax[3], ':', np.sum((udec >= ax[2]) * (udec <= ax[3]))) def radec_plot(): plt.axis(ax) plt.xlabel('RA (deg)') if decam: # plt.xticks(np.arange(0, 361, 45)) #tt = np.arange(0, 361, 60) #plt.xticks(tt, map_ra(tt)) plt.xticks([-60, 0, 60, 120, 180, 240, 300], [300, 0, 60, 120, 180, 240, 300]) else: plt.xticks(np.arange(90, 311, 30)) plt.ylabel('Dec (deg)') def plot_broken(rr, dd, *args, **kwargs): dr = np.abs(np.diff(rr)) I = np.flatnonzero(dr > 90) #print('breaks:', rr[I]) #print('breaks:', rr[I+1]) if len(I) == 0: plt.plot(rr, dd, *args, **kwargs) return for lo, hi in zip(np.append([0], I + 1), np.append(I + 1, -1)): #print('Cut:', lo, ':', hi, '->', rr[lo], rr[hi-1]) plt.plot(rr[lo:hi], dd[lo:hi], *args, **kwargs) # Galactic plane lines gl = np.arange(361) gb = np.zeros_like(gl) from astrometry.util.starutil_numpy import lbtoradec rr, dd = lbtoradec(gl, gb) plot_broken(map_ra(rr), dd, 'k-', alpha=0.5, lw=1) rr, dd = lbtoradec(gl, gb + 10) plot_broken(map_ra(rr), dd, 'k-', alpha=0.25, lw=1) rr, dd = lbtoradec(gl, gb - 10) plot_broken(map_ra(rr), dd, 'k-', alpha=0.25, lw=1) plt.figure(1, figsize=(8, 5)) plt.subplots_adjust(left=0.1, right=0.98, top=0.93) plt.figure(2, figsize=(8, 4)) #plt.subplots_adjust(left=0.06, right=0.98, top=0.98) plt.subplots_adjust(left=0.08, right=0.98, top=0.98) plt.figure(1) # Map of the tile centers we want to observe... """ if decam: O = fits_table('obstatus/decam-tiles_obstatus.fits') else: O = fits_table('mosaic-tiles_obstatus.fits') """ # File from the "observing" svn repo: from pkg_resources import resource_filename if decam: tilefile = resource_filename('legacyzpts', 'data/decam-tiles_obstatus.fits') else: tilefile = resource_filename('legacyzpts', 'data/mosaic-tiles_obstatus.fits') O = fits_table(tilefile) O.cut(O.in_desi == 1) rr, dd = np.meshgrid(np.linspace(ax[1], ax[0], 700), np.linspace(ax[2], ax[3], 200)) from astrometry.libkd.spherematch import match_radec I, J, d = match_radec(O.ra, O.dec, rr.ravel(), dd.ravel(), 1.) desimap = np.zeros(rr.shape, bool) desimap.flat[J] = True # Smoothed DESI boundary contours from scipy.ndimage.filters import gaussian_filter from scipy.ndimage.morphology import binary_dilation C = plt.contour(gaussian_filter( binary_dilation(desimap).astype(np.float32), 2), [0.5], extent=[ax[1], ax[0], ax[2], ax[3]]) plt.clf() desi_map_boundaries = C.collections[0] def desi_map_outline(): segs = desi_map_boundaries.get_segments() for seg in segs: plt.plot(seg[:, 0], seg[:, 1], 'b-') def desi_map(): # Show the DESI tile map in the background. plt.imshow(desimap, origin='lower', interpolation='nearest', extent=[ax[1], ax[0], ax[2], ax[3]], aspect='auto', cmap=antigray, vmax=8) base_cmap = 'viridis' # Dust map -- B&W version nr, nd = 610, 350 plt.figure(2) plt.clf() dmap = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), O.ra, O.dec, 1.0, nearest=True) iy, ix = np.unravel_index(I, rr.shape) #dmap[iy,ix] = O.ebv_med[J] sfd = tractor.sfd.SFDMap() ebv = sfd.ebv(rr[iy, ix], dd[iy, ix]) dmap[iy, ix] = ebv mx = np.percentile(dmap[dmap > 0], 98) plt.imshow(dmap, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=mx) #desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar(cax=cax) cbar.set_label('Extinction E(B-V)') plt.savefig('ext-bw.pdf') plt.clf() dmap = sfd.ebv(rr.ravel(), dd.ravel()).reshape(rr.shape) plt.imshow(dmap, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=0.25) desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar(cax=cax) cbar.set_label('Extinction E(B-V)') plt.savefig('ext-bw-2.pdf') plt.figure(1) #sys.exit(0) plt.clf() depthlo, depthhi = 21.5, 25.5 for band in 'grz': depth = T.get('galdepth_%s' % band) ha = dict(histtype='step', bins=50, range=(depthlo, depthhi)) ccmap = dict(g='g', r='r', z='m') plt.hist(depth[depth > 0], label='%s band' % band, color=ccmap[band], **ha) plt.xlim(depthlo, depthhi) plt.xlabel('Galaxy depth (median per brick) (mag)') plt.ylabel('Number of Bricks') plt.title(release) plt.savefig('galdepths.png') for band in 'grz': depth = T.get('galdepth_%s' % band) nexp = T.get('nexp_%s' % band) #lo,hi = 22.0-0.05, 24.2+0.05 lo, hi = depthlo - 0.05, depthhi + 0.05 nbins = 1 + int((depthhi - depthlo) / 0.1) ha = dict(histtype='step', bins=nbins, range=(lo, hi)) ccmap = dict(g='g', r='r', z='m') area = 0.25**2 plt.clf() I = np.flatnonzero((depth > 0) * (nexp == 1)) plt.hist(depth[I], label='%s band, 1 exposure' % band, color=ccmap[band], lw=1, weights=area * np.ones_like(depth[I]), **ha) I = np.flatnonzero((depth > 0) * (nexp == 2)) plt.hist(depth[I], label='%s band, 2 exposures' % band, color=ccmap[band], lw=2, alpha=0.5, weights=area * np.ones_like(depth[I]), **ha) I = np.flatnonzero((depth > 0) * (nexp >= 3)) plt.hist(depth[I], label='%s band, 3+ exposures' % band, color=ccmap[band], lw=3, alpha=0.3, weights=area * np.ones_like(depth[I]), **ha) plt.title('%s: galaxy depths, %s band' % (release, band)) plt.xlabel('5-sigma galaxy depth (mag)') plt.ylabel('Square degrees') plt.xlim(lo, hi) plt.xticks(np.arange(depthlo, depthhi + 0.01, 0.2)) plt.legend(loc='upper right') plt.savefig('depth-hist-%s.png' % band) for band in 'grz': plt.clf() desi_map() N = T.get('nexp_%s' % band) I = np.flatnonzero(N > 0) #cm = matplotlib.cm.get_cmap('jet', 6) #cm = matplotlib.cm.get_cmap('winter', 5) mx = 10 cm = cmap_discretize(base_cmap, mx) plt.scatter(map_ra(T.ra[I]), T.dec[I], c=N[I], s=3, edgecolors='none', vmin=0.5, vmax=mx + 0.5, cmap=cm) radec_plot() cax = colorbar_axes(plt.gca(), frac=0.08) plt.colorbar(cax=cax, ticks=range(mx + 1)) plt.title('%s: Number of exposures in %s' % (release, band)) plt.savefig('nexp-%s.png' % band) #cmap = cmap_discretize(base_cmap, 15) cmap = cmap_discretize(base_cmap, 10) plt.clf() desi_map() psf = T.get('psfsize_%s' % band) I = np.flatnonzero(psf > 0) plt.scatter(map_ra(T.ra[I]), T.dec[I], c=psf[I], s=3, edgecolors='none', cmap=cmap, vmin=0.5, vmax=2.5) #vmin=0, vmax=3.) radec_plot() plt.colorbar() plt.title('%s: PSF size, band %s' % (release, band)) plt.savefig('psfsize-%s.png' % band) plt.clf() desi_map() depth = T.get('galdepth_%s' % band) - T.get('ext_%s' % band) mn, mx = np.percentile(depth[depth > 0], [10, 98]) mn = np.floor(mn * 10) / 10. mx = np.ceil(mx * 10) / 10. cmap = cmap_discretize(base_cmap, 1 + int((mx - mn + 0.001) / 0.1)) I = (depth > 0) plt.scatter(map_ra(T.ra[I]), T.dec[I], c=depth[I], s=3, edgecolors='none', vmin=mn - 0.05, vmax=mx + 0.05, cmap=cmap) radec_plot() plt.colorbar() plt.title( '%s: galaxy depth, band %s, median per brick, extinction-corrected' % (release, band)) plt.savefig('galdepth-%s.png' % band) # B&W version plt.figure(2) plt.clf() mn, mx = np.percentile(depth[depth > 0], [2, 98]) print('Raw mn,mx', mn, mx) mn = np.floor((mn + 0.05) * 10) / 10. - 0.05 mx = np.ceil((mx - 0.05) * 10) / 10. + 0.05 print('rounded mn,mx', mn, mx) nsteps = int((mx - mn + 0.001) / 0.1) print('discretizing into', nsteps, 'colormap bins') #nsteps = 1+int((mx-mn+0.001)/0.1) cmap = cmap_discretize(antigray, nsteps) nr, nd = 610, 228 dmap = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), T.ra, T.dec, 0.2, nearest=True) iy, ix = np.unravel_index(I, rr.shape) dmap[iy, ix] = depth[J] plt.imshow(dmap, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap=cmap, vmin=mn, vmax=mx) desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar( cax=cax, ticks=np.arange(20, 26, 0.5) ) #ticks=np.arange(np.floor(mn/5.)*5., 0.1+np.ceil(mx/5.)*5, 0.2)) cbar.set_label('Depth (5-sigma, galaxy profile, AB mag)') plt.savefig('galdepth-bw-%s.pdf' % band) plt.figure(1) plt.clf() desi_map() ext = T.get('ext_%s' % band) mn = 0. mx = 0.5 cmap = 'hot' cmap = cmap_discretize(cmap, 10) #cmap = cmap_discretize(base_cmap, 1+int((mx-mn+0.001)/0.1)) plt.scatter(map_ra(T.ra), T.dec, c=ext, s=3, edgecolors='none', vmin=mn, vmax=mx, cmap=cmap) radec_plot() plt.colorbar() plt.title('%s: extinction, band %s' % (release, band)) plt.savefig('ext-%s.png' % band) T.ngal = T.nsimp + T.nrex + T.nexp + T.ndev + T.ncomp for col in [ 'nobjs', 'npsf', 'nsimp', 'nrex', 'nexp', 'ndev', 'ncomp', 'ngal' ]: if not col in T.get_columns(): continue plt.clf() desi_map() N = T.get(col) / T.area mx = np.percentile(N, 99.5) plt.scatter(map_ra(T.ra), T.dec, c=N, s=3, edgecolors='none', vmin=0, vmax=mx) radec_plot() cbar = plt.colorbar() cbar.set_label('Objects per square degree') tt = 'of type %s' % col[1:] if col == 'nobjs': tt = 'total' plt.title('%s: Number of objects %s' % (release, tt)) plt.savefig('nobjs-%s.png' % col[1:]) # B&W version plt.figure(2) plt.clf() # plt.scatter(map_ra(T.ra), T.dec, c=N, s=3, # edgecolors='none', vmin=0, vmax=mx, cmap=antigray) # Approximate pixel size in PNG plot # This doesn't work correctly -- we've already binned to brick resolution, so get moire patterns # nobjs,xe,ye = np.histogram2d(map_ra(T.ra), T.dec, weights=T.get(col), # bins=(nr,nd), range=((ax[1],ax[0]),(ax[2],ax[3]))) # nobjs = nobjs.T # area = np.diff(xe)[np.newaxis,:] * (np.diff(ye) * np.cos(np.deg2rad(ye[:-1])))[:,np.newaxis] # nobjs /= area # plt.imshow(nobjs, extent=[ax[1],ax[0],ax[2],ax[3]], interpolation='nearest', origin='lower', # aspect='auto') #print('Computing neighbours for nobjs plot...') nr, nd = 610, 228 nobjs = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), T.ra, T.dec, 0.2, nearest=True) iy, ix = np.unravel_index(I, rr.shape) nobjs[iy, ix] = T.get(col)[J] / T.area[J] #print('done') #mx = 2. * np.median(nobjs[nobjs > 0]) mx = np.percentile(N, 99) plt.imshow(nobjs, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=mx) desi_map_outline() radec_plot() #cax = colorbar_axes(plt.gca(), frac=0.08) cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar(cax=cax, format=matplotlib.ticker.FuncFormatter( lambda x, p: format(int(x), ','))) cbar.set_label('Objects per square degree') plt.savefig('nobjs-bw-%s.pdf' % col[1:]) #plt.savefig('nobjs-bw-%s.png' % col[1:]) plt.figure(1) Ntot = T.nobjs for col in ['npsf', 'nsimp', 'nrex', 'nexp', 'ndev', 'ncomp', 'ngal']: if not col in T.get_columns(): continue plt.clf() desi_map() N = T.get(col) / (Ntot.astype(np.float32)) N[Ntot == 0] = 0. print(col, 'max frac:', N.max()) mx = np.percentile(N, 99.5) print('mx', mx) plt.scatter(map_ra(T.ra), T.dec, c=N, s=3, edgecolors='none', vmin=0, vmax=mx) radec_plot() plt.colorbar() plt.title('%s: Fraction of objects of type %s' % (release, col[1:])) plt.savefig('fobjs-%s.png' % col[1:]) # B&W version plt.figure(2) plt.clf() #plt.scatter(map_ra(T.ra), T.dec, c=N * 100., s=3, # edgecolors='none', vmin=0, vmax=mx*100., cmap=antigray) fobjs = np.zeros((nd, nr)) rr = np.linspace(ax[0], ax[1], nr) dd = np.linspace(ax[2], ax[3], nd) rr = rr[:-1] + 0.5 * (rr[1] - rr[0]) dd = dd[:-1] + 0.5 * (dd[1] - dd[0]) rr, dd = np.meshgrid(rr, dd) I, J, d = match_radec(rr.ravel(), dd.ravel(), T.ra, T.dec, 0.2, nearest=True) iy, ix = np.unravel_index(I, rr.shape) fobjs[iy, ix] = N[J] * 100. #mx = 2. * np.median(fobjs[fobjs > 0]) mx = np.percentile(N * 100., 99) plt.imshow(fobjs, extent=[ax[0], ax[1], ax[2], ax[3]], interpolation='nearest', origin='lower', aspect='auto', cmap='Greys', vmin=0, vmax=mx) desi_map_outline() radec_plot() cax = colorbar_axes(plt.gca(), frac=0.12) cbar = plt.colorbar( cax=cax, format=matplotlib.ticker.FuncFormatter(lambda x, p: '%.2g' % x)) cbar.set_label('Percentage of objects of type %s' % col[1:].upper()) plt.savefig('fobjs-bw-%s.pdf' % col[1:]) #plt.savefig('fobjs-bw-%s.png' % col[1:]) plt.figure(1) return 0
def show(self, som, distance2=1, row_normalized=False, show_data=True, contour=True, blob=False, labels=False): umat = self.build_u_matrix(som, distance=distance2, row_normalized=row_normalized) msz = som.codebook.mapsize proj = som.project_data(som.data_raw) coord = som.bmu_ind_to_xy(proj) self._fig, ax = plt.subplots(1, 1) pylab.imshow(umat, cmap=plt.cm.get_cmap('RdYlBu_r'), alpha=1) if contour: mn = np.min(umat.flatten()) mx = np.max(umat.flatten()) std = np.std(umat.flatten()) md = np.median(umat.flatten()) mx = md + 0 * std pylab.contour(umat, np.linspace(mn, mx, 15), linewidths=0.7, cmap=plt.cm.get_cmap('Blues')) if show_data: plt.scatter(coord[:, 1], coord[:, 0], s=2, alpha=1., c='Gray', marker='o', cmap='jet', linewidths=3, edgecolor='Gray') plt.axis('off') if labels: if labels is True: labels = som.build_data_labels() for label, x, y in zip(labels, coord[:, 1], coord[:, 0]): plt.annotate(str(label), xy=(x, y), horizontalalignment='center', verticalalignment='center') ratio = float(msz[0]) / (msz[0] + msz[1]) self._fig.set_size_inches((1 - ratio) * 15, ratio * 15) plt.tight_layout() plt.subplots_adjust(hspace=.00, wspace=.000) sel_points = [] if blob: from skimage.color import rgb2gray from skimage.feature import blob_log image = 1 / umat rgb2gray(image) # 'Laplacian of Gaussian' blobs = blob_log(image, max_sigma=5, num_sigma=4, threshold=.152) blobs[:, 2] = blobs[:, 2] * sqrt(2) pylab.imshow(umat, cmap=plt.cm.get_cmap('RdYlBu_r'), alpha=1) sel_points = [] for blob in blobs: row, col, r = blob c = plt.Circle((col, row), r, color='red', linewidth=2, fill=False) ax.add_patch(c) dist = scipy.spatial.distance_matrix( coord[:, :2], np.array([row, col])[np.newaxis, :]) sel_point = dist <= r plt.plot(coord[:, 1][sel_point[:, 0]], coord[:, 0][sel_point[:, 0]], '.r') sel_points.append(sel_point[:, 0]) plt.show() return sel_points, umat
(1, -2, -2, 1)] uW[core] = aWW[core] * K[Ws] - aWP[core] * K[core] uE[core] = -aEE[core] * K[Es] + aEP[core] * K[core] uN[core] = -aNN[core] * K[Ns] + aNP[core] * K[core] uS[core] = aSS[core] * K[Ss] - aSP[core] * K[core] # update the u BCs for BC in (uW, uE, uN, uS): BC[0, 1:-1] = BC[1, 1:-1] BC[-1, 1:-1] = BC[-2, 1:-1] BC[1:-1, 0] = BC[1:-1, 1] BC[1:-1, -1] = BC[1:-1, -2] BC[(0, -1, 0, -1), (0, -1, -1, 0)] = BC[(1, -2, 1, -2), (1, -2, -2, 1)] X, Y = np.meshgrid(np.arange(ncols), np.arange(nrows)) uval = uW[core] + uE[core] vval = uN[core] + uS[core] # velmag = sqrt(uval**2 + vval**2) # uval /= velmag # vval /= velmag # imshow_node_grid(mg, h) figure(1) mg = RasterModelGrid((nrows, ncols)) f1 = imshow_grid_at_node(mg, hR[core].flatten(), grid_units=("m", "m")) figure(2) f2 = contour(X, Y, hR[core], locator=MaxNLocator(nbins=100)) # f2 = contour(X, Y, np.sqrt(uval**2+vval**2), locator=MaxNLocator(nbins=10)) clabel(f2) quiver(X, Y, uval, vval)
ds.select("z>50") selected = subspace.selected() print(subspace.mean()) print(subspace.var()) print(subspace.limits_sigma()) print(subspace.limits_sigma(sigmas=1)) #limits = subspace.minmax() print("square limits", limits) grid = subspace.histogram(limits=limits) grid_selected = selected.histogram(limits=limits) subspace.plot(np.log(grid), limits=limits) pylab.contour(np.log(grid_selected), 2, linewidth="2pt", colors="blue", extent=limits.flatten(), alpha=0.8) pylab.show() print("datasets", list) # dsa dataset = None import traceback def printminmax(args): subspace, limits = args print("minmax for", subspace, limits) return subspace, limits
# 訓練データをプロット pl.scatter(X[:, 0], X[:, 1], c=Y, zorder=10, cmap=cmap) # サポートベクトルを強調 pl.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], s=80, facecolors='none', zorder=10) x_min = -3 x_max = 3 y_min = -3 y_max = 3 # 識別境界をプロット XX, YY = np.mgrid[x_min:x_max:200j, y_min:y_max:200j] # decision_function()は識別境界までの距離を返す Z = clf.decision_function(np.c_[XX.ravel(), YY.ravel()]) Z = Z.reshape(XX.shape) pl.pcolormesh(XX, YY, Z > 0, cmap=cmap) pl.contour(XX, YY, Z, colors=['k', 'k', 'k'], linestyles=['--', '-', '--'], levels=[-0.5, 0, 0.5]) pl.title("kernel: %s" % kernel) pl.xlim(x_min, x_max) pl.ylim(y_min, y_max) fignum += 1 pl.show()