def main(): SAMPLE_NUM = 10 degree = 9 x, y = sin_wgn_sample(SAMPLE_NUM) fig = pylab.figure(1) pylab.grid(True) pylab.xlabel('x') pylab.ylabel('y') pylab.axis([-0.1,1.1,-1.5,1.5]) # sin(x) + noise # markeredgewidth mew # markeredgecolor mec # markerfacecolor mfc # markersize ms # linewidth lw # linestyle ls pylab.plot(x, y,'bo',mew=2,mec='b',mfc='none',ms=8) # sin(x) x2 = linspace(0, 1, 1000) pylab.plot(x2,sin(2*x2*pi),'#00FF00',lw=2,label='$y = \sin(x)$') # polynomial fit reg = exp(-18) w = curve_poly_fit(x, y, degree,reg) #w = polyfit(x, y, 3) po = poly1d(w) xx = linspace(0, 1, 1000) pylab.plot(xx, po(xx),'-r',label='$M = 9, \ln\lambda = -18$',lw=2) pylab.legend() pylab.show() fig.savefig("poly_fit9_10_reg.pdf")
def plot_tracks(src, fakewcs, spa=None, **kwargs): # NOTE -- MAGIC 61 = monthly; this is ASSUMEd below. tt = np.linspace(2010., 2015., 61) t0 = TAITime(None, mjd=TAITime.mjd2k + 365.25*10) #rd0 = src.getPositionAtTime(t0) #print 'rd0:', rd0 xx,yy = [],[] rr,dd = [],[] for t in tt: #print 'Time', t rd = src.getPositionAtTime(t0 + (t - 2010.)*365.25*24.*3600.) ra,dec = rd.ra, rd.dec rr.append(ra) dd.append(dec) ok,x,y = fakewcs.radec2pixelxy(ra,dec) xx.append(x - 1.) yy.append(y - 1.) if spa is None: spa = [None,None,None] for rows,cols,sub in spa: if sub is not None: plt.subplot(rows,cols,sub) ax = plt.axis() plt.plot(xx, yy, 'k-', **kwargs) plt.axis(ax) return rr,dd,tt
def m2screenshot(mayavi_fig=None, mpl_axes=None, autocrop=True): """ Capture a screeshot of the Mayavi figure and display it in the matplotlib axes. """ import pylab as pl # Late import to avoid triggering wx imports before needed. try: from mayavi import mlab except ImportError: # Try out old install of Mayavi, with namespace packages from enthought.mayavi import mlab if mayavi_fig is None: mayavi_fig = mlab.gcf() else: mlab.figure(mayavi_fig) if mpl_axes is not None: pl.axes(mpl_axes) filename = tempfile.mktemp('.png') mlab.savefig(filename, figure=mayavi_fig) image3d = pl.imread(filename) if autocrop: bg_color = mayavi_fig.scene.background image3d = autocrop_img(image3d, bg_color) pl.imshow(image3d) pl.axis('off') os.unlink(filename)
def check_vpd_ks2_astrometry(): """ Check the VPD and quiver plots for our KS2-extracted, re-transformed astrometry. """ catFile = workDir + '20.KS2_PMA/wd1_catalog.fits' tab = atpy.Table(catFile) good = (tab.xe_160 < 0.05) & (tab.ye_160 < 0.05) & \ (tab.xe_814 < 0.05) & (tab.ye_814 < 0.05) & \ (tab.me_814 < 0.05) & (tab.me_160 < 0.05) tab2 = tab.where(good) dx = (tab2.x_160 - tab2.x_814) * ast.scale['WFC'] * 1e3 dy = (tab2.y_160 - tab2.y_814) * ast.scale['WFC'] * 1e3 py.clf() q = py.quiver(tab2.x_814, tab2.y_814, dx, dy, scale=5e2) py.quiverkey(q, 0.95, 0.85, 5, '5 mas', color='red', labelcolor='red') py.savefig(workDir + '20.KS2_PMA/vec_diffs_ks2_all.png') py.clf() py.plot(dy, dx, 'k.', ms=2) lim = 30 py.axis([-lim, lim, -lim, lim]) py.xlabel('Y Proper Motion (mas)') py.ylabel('X Proper Motion (mas)') py.savefig(workDir + '20.KS2_PMA/vpd_ks2_all.png') idx = np.where((np.abs(dx) < 10) & (np.abs(dy) < 10))[0] print('Cluster Members (within dx < 10 mas and dy < 10 mas)') print((' dx = {dx:6.2f} +/- {dxe:6.2f} mas'.format(dx=dx[idx].mean(), dxe=dx[idx].std()))) print((' dy = {dy:6.2f} +/- {dye:6.2f} mas'.format(dy=dy[idx].mean(), dye=dy[idx].std())))
def plotLDDecaySelection3d(ax, sweep=False): import pylab as plt; import matplotlib as mpl;mpl.rc('font', **{'family': 'serif', 'serif': ['Computer Modern'], 'size':16}) ; mpl.rc('text', usetex=True) def neutral(ld0, t, d, r=2 * 1e-8): if abs(d) <= 5e3: d = np.sign(d) * 5e3 if d == 0: d = 5e3 return ((np.exp(-2 * r * t * abs(d)))) * ld0 t = np.arange(0, 200 + 1., 2) L=1e6+1 pos=500000 r=2*1e-8 ld0 = 0.5 s = 0.05 nu0 = 0.1 positions=np.arange(0,L,1000) dist=(positions - pos) T, D = np.meshgrid(t, dist) if not sweep: zs = np.array([neutral(ld0, t, d) for t, d in zip(np.ravel(T), np.ravel(D))]) else: zs = np.array([LD(t, ld0, s, nu0, r, abs(d), 0) for t, d in zip(np.ravel(T), np.ravel(D))]) Z = zs.reshape(T.shape) ax.plot_surface(T, D, Z,cmap=mpl.cm.autumn) ax.set_xlabel('Generations') ax.set_ylabel('Position') plt.yticks(plt.yticks()[0][1:-1],map(lambda x:'{:.0f}K'.format((pos+(x))/1000),plt.yticks()[0][1:-1])) plt.ylim([-500000,500000]) ax.set_zlabel(r"$|\rho_t|$") pplt.setSize(plt.gca(), fontsize=6) plt.axis('tight');
def plot_matches(self, name, show_below = True, match_maximum = None): """ 対応点を線で結んで画像を表示する 入力: im1,im2(配列形式の画像)、locs1,locs2(特徴点座標) machescores(match()の出力)、 show_below(対応の下に画像を表示するならTrue)""" im1 = self._image_1.get_array_image() im2 = self._image_2.get_array_image() self.appendimages() im3 = self._append_image if self._match_score is None: self.match() locs1 = self._image_1.get_shift_location() locs2 = self._image_2.get_shift_location() if show_below: im3 = numpy.vstack((im3,im3)) pylab.figure(dpi=160) pylab.gray() pylab.imshow(im3, aspect = 'auto') cols1 = im1.shape[1] match_num = 0 for i,m in enumerate(self._match_score): if m > 0 : pylab.plot([locs1[i][0],locs2[m][0]+cols1], [locs1[i][1],locs2[m][1]], 'c') match_num = match_num + 1 if match_maximum is not None and match_num >= match_maximum: break pylab.axis('off') pylab.savefig(name, dpi=160)
def compareAnimals(animals, precision): """Assumes animals is a list of animals, precision an int >= 0 Builds a table of Euclidean distance between each animal""" #Get labels for columns and rows columnLabels = [] for a in animals: columnLabels.append(a.getName()) rowLabels = columnLabels[:] tableVals = [] #Get distances between pairs of animals #For each row for a1 in animals: row = [] #For each column for a2 in animals: if a1 == a2: row.append('--') else: distance = a1.distance(a2) row.append(str(round(distance, precision))) tableVals.append(row) #Produce table table = pylab.table(rowLabels = rowLabels, colLabels = columnLabels, cellText = tableVals, cellLoc = 'center', loc = 'center', colWidths = [0.2]*len(animals)) table.scale(1, 2.5) pylab.axis('off') #Don't display x and y-axes pylab.savefig('distances')
def simplegrid(): nzones = 7 gr = gpu.grid(nzones, xmin=0, xmax=1) gpu.drawGrid(gr, edgeTicks=0) # label a few cell-centers gpu.labelCenter(gr, nzones/2, r"$i$") gpu.labelCenter(gr, nzones/2-1, r"$i-1$") gpu.labelCenter(gr, nzones/2+1, r"$i+1$") # label a few edges gpu.labelEdge(gr, nzones/2, r"$i-1/2$") gpu.labelEdge(gr, nzones/2+1, r"$i+1/2$") # draw an average quantity gpu.drawCellAvg(gr, nzones/2, 0.4, color="r") gpu.labelCellAvg(gr, nzones/2, 0.4, r"$\,\langle a \rangle_i$", color="r") pylab.axis([gr.xmin-1.5*gr.dx,gr.xmax+1.5*gr.dx, -0.25, 1.5]) pylab.axis("off") pylab.subplots_adjust(left=0.05,right=0.95,bottom=0.05,top=0.95) f = pylab.gcf() f.set_size_inches(10.0,2.5) pylab.savefig("simplegrid2.png") pylab.savefig("simplegrid2.eps")
def makeContourPlot(scores, average, HEIGHT, WIDTH, outputId, maskId, plt_title, outputdir, barcodeId=-1, vmaxVal=100): pylab.bone() #majorFormatter = FormatStrFormatter('%.f %%') #ax = pylab.gca() #ax.xaxis.set_major_formatter(majorFormatter) pylab.figure() ax = pylab.gca() ax.set_xlabel(str(WIDTH) + ' wells') ax.set_ylabel(str(HEIGHT) + ' wells') ax.autoscale_view() pylab.jet() pylab.imshow(scores,vmin=0, vmax=vmaxVal, origin='lower') pylab.vmin = 0.0 pylab.vmax = 100.0 ticksVal = getTicksForMaxVal(vmaxVal) pylab.colorbar(format='%.0f %%',ticks=ticksVal) print "'%s'" % average if(barcodeId!=-1): if(barcodeId==0): maskId = "No Barcode Match," else: maskId = "Barcode Id %d," % barcodeId if plt_title != '': maskId = '%s\n%s' % (plt_title,maskId) print "Checkpoint A" pylab.title('%s Loading Density (Avg ~ %0.f%%)' % (maskId, average)) pylab.axis('scaled') print "Checkpoint B" pngFn = outputdir+'/'+outputId+'_density_contour.png' print "Try save to", pngFn; pylab.savefig(pngFn, bbox_inches='tight') print "Plot saved to", pngFn;
def __init__(self, baseConfig) : self.figsize = baseConfig.get('figsize',None) self.axis = baseConfig.get('axis',None) self.title = baseConfig.get('title','NoName') self.ylabel = baseConfig.get('ylabel','NoName') self.grid = baseConfig.get('grid',False) self.xaxis_locator = baseConfig.get('xaxis_locator',None) self.yaxis_locator = baseConfig.get('yaxis_locator',None) self.legend_loc = baseConfig.get('legend_loc',0) if self.figsize != None : pylab.figure(figsize = self.figsize) if self.axis != None : pylab.axis(self.axis) pylab.title(self.title) pylab.ylabel(self.ylabel) ax = pylab.gca() pylab.grid(self.grid) if self.xaxis_locator != None : ax.xaxis.set_major_locator( pylab.MultipleLocator(self.xaxis_locator) ) if self.yaxis_locator != None : ax.yaxis.set_major_locator( pylab.MultipleLocator(self.yaxis_locator) ) self.lineList = [] self.id = 1
def display_image_from_array(nparray,colory='binary',roi=None): """ Produce a display of the nparray 2D matrix @param nparray : image to display @type nparray : numpy 2darray @param colory : color mapping of the image (see http://www.scipy.org/Cookbook/Matplotlib/Show_colormaps) @type colory : string """ #Set the region of interest to display : # (0,0) is set at lower left corner of the image if roi == None: roi = ((0,0),nparray.shape) nparraydsp = nparray print roi elif type(roi[0])==tuple and type(roi[1])==tuple: # Case of 2 points definition of the domain : roi = integers index of points ((x1,y1),(x2,y2)) print roi nparraydsp = nparray[roi[0][0]:roi[1][0],roi[0][1]:roi[1][1]] elif type(roi[0])==int and type(roi[1])==int: # Case of image centered domain : roi = integers (width,high) nparraydsp = nparray[int(nparray.shape[0]/2)-int(roi[0])/2:int(nparray.shape[0]/2)+int(roi[0])/2,int(nparray.shape[1]/2)-int(roi[1])/2:int(nparray.shape[1]/2)+int(roi[1])/2] fig = pylab.figure() #Display array with grayscale intensity and no pixel smoothing interpolation pylab.imshow(nparraydsp,cmap=colory,interpolation='nearest')#,origin='lower') pylab.colorbar() pylab.axis('off')
def correlation_matrix(data, size=8.0): """ Calculates and shows the correlation matrix of the pandas data frame 'data' as a heat map. Only the correlations between numerical variables are calculated! """ # calculate the correlation matrix corr = data.corr() #print corr lc = len(corr.columns) # set some settings for plottin' pl.pcolor(corr, vmin = -1, vmax = 1, edgecolor = "black") pl.colorbar() pl.xlim([-5,lc]) pl.ylim([0,lc+5]) pl.axis('off') # anotate the rows and columns with their corresponding variables ax = pl.gca() for i in range(0,lc): ax.annotate(corr.columns[i], (-0.5, i+0.5), \ size='large', horizontalalignment='right', verticalalignment='center') ax.annotate(corr.columns[i], (i+0.5, lc+0.5),\ size='large', rotation='vertical',\ horizontalalignment='center', verticalalignment='right') # change the size of the image fig = pl.figure(num=1) fig.set_size_inches(size+(size/4), size) pl.show()
def __call__(self, n): if len(self.f.shape) == 3: # f = f[x,v,t], 2 dim in phase space ft = self.f[n,:,:] pylab.pcolormesh(self.X, self.V, ft.T, cmap = 'jet') pylab.colorbar() pylab.clim(0,0.38) # for Landau test case pylab.grid() pylab.axis([self.xmin, self.xmax, self.ymin, self.ymax]) pylab.xlabel('$x$', fontsize = 18) pylab.ylabel('$v$', fontsize = 18) pylab.title('$N_x$ = %d, $N_v$ = %d, $t$ = %2.1f' % (self.x.N, self.v.N, self.it*self.t.width)) pylab.savefig(self.path + self.filename) pylab.clf() return None if len(self.f.shape) == 2: # f = f[x], 1 dim in phase space ft = self.f[n,:] pylab.plot(self.x.gridvalues,ft,'ob') pylab.grid() pylab.axis([self.xmin, self.xmax, self.ymin, self.ymax]) pylab.xlabel('$x$', fontsize = 18) pylab.ylabel('$f(x)$', fontsize = 18) pylab.savefig(self.path + self.filename) return None
def display_head(set_x, set_y, n = 5): ''' show some figures based on gray image matrixs @type set_x: TensorSharedVariable, @param set_x: gray level value matrix of the @type set_y: TensorVariable, @param set_y: label of the figures @type n: int, @param n: numbers of figure to be display, less than 10, default 5 ''' import pylab if n > 10: n = 10 img_x = set_x.get_value()[0:n].reshape(n, 28, 28) img_y = set_y.eval()[0:n] for i in range(n): pylab.subplot(1, n, i+1); pylab.axis('off'); pylab.title(' %d' % img_y[i]) pylab.gray() pylab.imshow(img_x[i])
def update(self): if self.pose != []: plt.figure(1) clf() self.fig1 = plt.figure(num=1, figsize=(self.window_size, \ self.window_size), dpi=80, facecolor='w', edgecolor='w') title (self.title) xlabel('Easting [m]') ylabel('Northing [m]') axis('equal') grid (True) poseT = zip(*self.pose) pose_plt = plot(poseT[1],poseT[2],'#ff0000') if self.wptnav != []: mode = self.wptnav[-1][MODE] if not (self.wptnav[-1][B_E] == 0 and self.wptnav[-1][B_N] == 0 and self.wptnav[-1][A_E] == 0 and self.wptnav[-1][A_N] == 0): b_dot = plot(self.wptnav[-1][B_E],self.wptnav[-1][B_N],'ro',markersize=8) a_dot = plot(self.wptnav[-1][A_E],self.wptnav[-1][A_N],'go',markersize=8) ab_line = plot([self.wptnav[-1][B_E],self.wptnav[-1][A_E]],[self.wptnav[-1][B_N],self.wptnav[-1][A_N]],'g') target_dot = plot(self.wptnav[-1][TARGET_E],self.wptnav[-1][TARGET_N],'ro',markersize=5) if mode == -1: pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'b^',markersize=8) elif mode == 1: pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'bs',markersize=8) elif mode == 2: pose_dot = plot(self.wptnav[-1][POSE_E],self.wptnav[-1][POSE_N],'bo',markersize=8) if self.save_images: self.fig1.savefig ('plot_map%05d.jpg' % self.image_count) self.image_count += 1 draw()
def _draw_V(self): """ draw the V-cycle on our optional visualization """ xdown = numpy.linspace(0.0, 0.5, self.nlevels) xup = numpy.linspace(0.5, 1.0, self.nlevels) ydown = numpy.linspace(1.0, 0.0, self.nlevels) yup = numpy.linspace(0.0, 1.0, self.nlevels) pylab.plot(xdown, ydown, lw=2, color="k") pylab.plot(xup, yup, lw=2, color="k") pylab.scatter(xdown, ydown, marker="o", color="k", s=40) pylab.scatter(xup, yup, marker="o", color="k", s=40) if self.up_or_down == "down": pylab.scatter( xdown[self.nlevels - self.current_level - 1], ydown[self.nlevels - self.current_level - 1], marker="o", color="r", zorder=100, s=38, ) else: pylab.scatter(xup[self.current_level], yup[self.current_level], marker="o", color="r", zorder=100, s=38) pylab.text(0.7, 0.1, "V-cycle %d" % (self.current_cycle)) pylab.axis("off")
def plot_density(self, plot_filename="out/density.png"): x, y, labels = self.load_data() figure(figsize=(self.fig_width, self.fig_height), dpi=80) # Perform a kernel density estimator on the coords in data. # The following 10 lines can be commented out if density map not needed. space_factor = 1.2 xmin = space_factor * x.min() xmax = space_factor * x.max() ymin = space_factor * y.min() ymax = space_factor * y.max() X, Y = mgrid[xmin:xmax:100j, ymin:ymax:100j] positions = c_[X.ravel(), Y.ravel()] values = c_[x, y] kernel = stats.kde.gaussian_kde(values.T) Z = reshape(kernel(positions.T).T, X.T.shape) imshow(rot90(Z), cmap=cm.gist_earth_r, extent=[xmin, xmax, ymin, ymax]) # Plot the labels num_labels_to_plot = min([len(labels), self.max_labels, len(x), len(y)]) if self.has_labels: for i in range(num_labels_to_plot): text(x[i], y[i], labels[i]) # assumes m size and order matches labels else: plot(x, y, "k.", markersize=1) axis("equal") axis("off") savefig(plot_filename) print "wrote %s" % (plot_filename)
def plot_commerror(name1,logfile1,name2,logfile2): file1 = open(str(logfile1),'r').readlines() data1 = [float(line.split()[1]) for line in file1] iso1 = data1[0::3] aniso1 = data1[1::3] ml1 = data1[2::3] file2 = open(str(logfile2),'r').readlines() data2 = [float(line.split()[1]) for line in file2] iso2 = data2[0::3] aniso2 = data2[1::3] ml2 = data2[2::3] # x axis x=[8,16,32,64,128] xlabels=['A','B','C','D','E'] orderone = [1,.5,.25,.125,.0625] ordertwo = [i**2 for i in orderone] plot1 = pylab.figure(figsize = (6, 6.5)) size = 15 ax = pylab.subplot(111) ax.plot(x,iso1, linestyle='solid',color='red',lw=2) ax.plot(x,aniso1, linestyle='dashed',color='red',lw=2) ax.plot(x,ml1, linestyle='dashdot',color='red',lw=2) ax.plot(x,iso2, linestyle='solid',color='blue',lw=2) ax.plot(x,aniso2, linestyle='dashed',color='blue',lw=2) ax.plot(x,ml2, linestyle='dashdot',color='blue',lw=2) #ax.plot(x,orderone, linestyle='solid',color='black',lw=2) #ax.plot(x,ordertwo, linestyle='dashed',color='black') #pylab.legend((str(name1)+'_iso',str(name1)+'_aniso',str(name2)+'_iso',str(name2)+'_aniso','order 1'),loc="best") pylab.legend((str(name1)+'_iso',str(name1)+'_aniso',str(name1)+'_ml',str(name2)+'_iso',str(name2)+'_aniso',str(name2)+'_ml'),loc="best") leg = pylab.gca().get_legend() ltext = leg.get_texts() pylab.setp(ltext, fontsize = size, color = 'black') frame=leg.get_frame() frame.set_fill(False) frame.set_visible(False) #ax.grid("True") for tick in ax.xaxis.get_major_ticks(): tick.label1.set_fontsize(size) for tick in ax.yaxis.get_major_ticks(): tick.label1.set_fontsize(size) # set axes to logarithmic pylab.gca().set_xscale('log',basex=2) pylab.gca().set_yscale('log',basex=2) pylab.axis([8,128,1.e-4,2]) ax.set_xticks(x) ax.set_xticklabels(xlabels) #pylab.axis([1,5,1.e-6,1.]) ax.set_xlabel('Mesh resolution', ha="center",fontsize=size) ax.set_ylabel('commutation error',fontsize=size) pylab.savefig('commerrorplot.eps') pylab.savefig('commerrorplot.pdf') return
def savepng(pre, img, title=None, **kwargs): fn = '%s-%s.png' % (pre, idstr) print 'Saving', fn plt.clf() plt.imshow(img, **kwargs) ax = plt.axis() if debug: print len(xplotx),len(allobjx) for i,(objx,objy,objc) in enumerate(zip(allobjx,allobjy,allobjc)): plt.plot(objx,objy,'-',c=objc) tempx = [] tempx.append(xplotx[i]) tempx.append(objx[0]) tempy = [] tempy.append(xploty[i]) tempy.append(objy[0]) plt.plot(tempx,tempy,'-',c='purple') plt.plot(pointx,pointy,'y.') plt.plot(xplotx,xploty,'xg') plt.axis(ax) if title is not None: plt.title(title) plt.colorbar() plt.gray() plt.savefig(fn)
def plot_features(im, features, num_to_plot=100, colors=["blue"]): plt.imshow(im) for i in range(min(features.shape[0], num_to_plot)): x = features[i,0] y = features[i,1] scale = features[i,2] rot = features[i,3] color = colors[i % len(colors)] box = patches.Rectangle((-scale/2,-scale/2), scale, scale, edgecolor=color, facecolor="none", lw=1) arrow = patches.Arrow(0, -scale/2, 0, scale, width=10, edgecolor=color, facecolor="none") t_start = plt.gca().transData transform = mpl.transforms.Affine2D().rotate(rot).translate(x,y) + t_start box.set_transform(transform) arrow.set_transform(transform) plt.gca().add_artist(box) plt.gca().add_artist(arrow) plt.axis('off') plt.set_cmap('gray') plt.show()
def newFigLayer(): pylab.clf() pylab.figure(figsize=(8, 8)) pylab.axes([0.15, 0.15, 0.8, 0.81]) pylab.axis([0.6, -0.4, -0.4, 0.6]) pylab.xlabel(r"$\Delta$\textsf{RA from Sgr A* (arcsec)}") pylab.ylabel(r"$\Delta$\textsf{Dec. from Sgr A* (arcsec)}")
def plot_margin(X1_train, X2_train, clf): def f(x, w, b, c=0): # given x, return y such that [x,y] in on the line # w.x + b = c return (-w[0] * x - b + c) / w[1] pl.plot(X1_train[:,0], X1_train[:,1], "ro") pl.plot(X2_train[:,0], X2_train[:,1], "bo") pl.scatter(clf.sv[:,0], clf.sv[:,1], s=100, c="g") # w.x + b = 0 a0 = -4; a1 = f(a0, clf.w, clf.b) b0 = 4; b1 = f(b0, clf.w, clf.b) pl.plot([a0,b0], [a1,b1], "k") # w.x + b = 1 a0 = -4; a1 = f(a0, clf.w, clf.b, 1) b0 = 4; b1 = f(b0, clf.w, clf.b, 1) pl.plot([a0,b0], [a1,b1], "k--") # w.x + b = -1 a0 = -4; a1 = f(a0, clf.w, clf.b, -1) b0 = 4; b1 = f(b0, clf.w, clf.b, -1) pl.plot([a0,b0], [a1,b1], "k--") pl.axis("tight") pl.show()
def hinton(W, maxWeight=None): """ Source: http://wiki.scipy.org/Cookbook/Matplotlib/HintonDiagrams Draws a Hinton diagram for visualizing a weight matrix. Temporarily disables matplotlib interactive mode if it is on, otherwise this takes forever. """ reenable = False if pl.isinteractive(): pl.ioff() pl.clf() height, width = W.shape if not maxWeight: maxWeight = 2**np.ceil(np.log(np.max(np.abs(W)))/np.log(2)) pl.fill(np.array([0,width,width,0]),np.array([0,0,height,height]),'gray') pl.axis('off') pl.axis('equal') for x in xrange(width): for y in xrange(height): _x = x+1 _y = y+1 w = W[y,x] if w > 0: _blob(_x - 0.5, height - _y + 0.5, min(1,w/maxWeight),'white') elif w < 0: _blob(_x - 0.5, height - _y + 0.5, min(1,-w/maxWeight),'black') if reenable: pl.ion() pl.show()
def plot_iris_knn(): iris = datasets.load_iris() X = iris.data[:, :2] # we only take the first two features. We could # avoid this ugly slicing by using a two-dim dataset y = iris.target knn = neighbors.KNeighborsClassifier(n_neighbors=3) knn.fit(X, y) 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.linspace(x_min, x_max, 100), np.linspace(y_min, y_max, 100)) Z = knn.predict(np.c_[xx.ravel(), yy.ravel()]) # Put the result into a color plot Z = Z.reshape(xx.shape) pl.figure() pl.pcolormesh(xx, yy, Z, cmap=cmap_light) # Plot also the training points pl.scatter(X[:, 0], X[:, 1], c=y, cmap=cmap_bold) pl.xlabel('sepal length (cm)') pl.ylabel('sepal width (cm)') pl.axis('tight')
def draw_io( type ): f = open("data\\io.txt") s_list = f.readlines() f.close() r_list_x = [] r_list_y = [] w_list_x = [] w_list_y = [] y_min = 0x80000000 y_max = 0 for s in s_list: pos = s.find('[') addr = int( s[pos+1:pos+9], 16 ) if addr > y_max: y_max = addr if addr < y_min: y_min = addr if s.find('|R') != -1: r_list_y.append( addr ) r_list_x.append( int( s.strip('\n').split('#')[1] ) ) #read counter if s.find('|W') != -1: w_list_y.append( int( addr ) ) w_list_x.append( int( s.strip('\n').split('#')[1] ) ) #read counter if type == 'W': pylab.plot( w_list_x, w_list_y, "ro" ) if type == 'R': pylab.plot( r_list_x, r_list_y, "ro" ) pylab.axis( [0, get_trace_count(), y_min - 1000, y_max + 1000] ) pylab.show()
def plot_adsorbed_circles(adsorbed_x,adsorbed_y,radius, width, label=""): import pylab from matplotlib.patches import Circle # Plot each run fig = pylab.figure() ax = fig.add_subplot(111) for p in range(len(adsorbed_x)): ax.add_patch(Circle((adsorbed_x[p], adsorbed_y[p]), radius)) # Plot "image" particles to verify that periodic boundary conditions are working # if adsorbed_x[p] < radius: # ax.add_patch(Circle((adsorbed_x[p] + width,adsorbed_y[p]), radius, facecolor='red')) # elif adsorbed_x[p] > (width-radius): # ax.add_patch(Circle((adsorbed_x[p] - width,adsorbed_y[p]), radius, facecolor='red')) # if adsorbed_y[p] < radius: # ax.add_patch(Circle((adsorbed_x[p],adsorbed_y[p] + width), radius, facecolor='red')) # elif adsorbed_y[p] > (width-radius): # ax.add_patch(Circle((adsorbed_x[p],adsorbed_y[p] - width), radius, facecolor='red')) ax.set_aspect(1.0) pylab.axhline(y=0, color='k') pylab.axhline(y=width, color='k') pylab.axvline(x=0, color='k') pylab.axvline(x=width, color='k') pylab.axis([-0.1*width, width*1.1, -0.1*width, width*1.1]) pylab.xlabel("non-dimensional x") pylab.ylabel("non-dimensional y") pylab.title("Adsorbed particles at theta="+label) return ax
def show_frequencies(video_filename, bounds=None): """Graph the average value of the video as well as the frequency strength""" original_video, fps = load_video(video_filename) print fps averages = [] if bounds: for x in range(1, original_video.shape[0] - 1): averages.append(original_video[x, bounds[2]:bounds[3], bounds[0]:bounds[1], :].sum()) else: for x in range(1, original_video.shape[0] - 1): averages.append(original_video[x, :, :, :].sum()) charts_x = 1 charts_y = 2 pylab.figure(figsize=(charts_y, charts_x)) pylab.subplots_adjust(hspace=.7) pylab.subplot(charts_y, charts_x, 1) pylab.title("Pixel Average") pylab.plot(averages) frequencies = scipy.fftpack.fftfreq(len(averages), d=1.0 / fps) pylab.subplot(charts_y, charts_x, 2) pylab.title("FFT") pylab.axis([0, 15, -2000000, 5000000]) pylab.plot(frequencies, scipy.fftpack.fft(averages)) pylab.show()
def __init__(self, baseConfig): self.figsize = baseConfig.get("figsize", None) self.axis = baseConfig.get("axis", None) self.title = baseConfig.get("title", "NoName") self.ylabel = baseConfig.get("ylabel", "NoName") self.grid = baseConfig.get("grid", False) self.xaxis_locator = baseConfig.get("xaxis_locator", None) self.yaxis_locator = baseConfig.get("yaxis_locator", None) self.legend_loc = baseConfig.get("legend_loc", 0) if self.figsize != None: pylab.figure(figsize=self.figsize) if self.axis != None: pylab.axis(self.axis) pylab.title(self.title) pylab.ylabel(self.ylabel) ax = pylab.gca() pylab.grid(self.grid) if self.xaxis_locator != None: ax.xaxis.set_major_locator(pylab.MultipleLocator(self.xaxis_locator)) if self.yaxis_locator != None: ax.yaxis.set_major_locator(pylab.MultipleLocator(self.yaxis_locator)) self.lineList = [] self.id = 1
def plot_polynomial_regression(): rng = np.random.RandomState(0) x = 2*rng.rand(100) - 1 f = lambda t: 1.2 * t**2 + .1 * t**3 - .4 * t **5 - .5 * t ** 9 y = f(x) + .4 * rng.normal(size=100) x_test = np.linspace(-1, 1, 100) pl.figure() pl.scatter(x, y, s=4) X = np.array([x**i for i in range(5)]).T X_test = np.array([x_test**i for i in range(5)]).T regr = linear_model.LinearRegression() regr.fit(X, y) pl.plot(x_test, regr.predict(X_test), label='4th order') X = np.array([x**i for i in range(10)]).T X_test = np.array([x_test**i for i in range(10)]).T regr = linear_model.LinearRegression() regr.fit(X, y) pl.plot(x_test, regr.predict(X_test), label='9th order') pl.legend(loc='best') pl.axis('tight') pl.title('Fitting a 4th and a 9th order polynomial') pl.figure() pl.scatter(x, y, s=4) pl.plot(x_test, f(x_test), label="truth") pl.axis('tight') pl.title('Ground truth (9th order polynomial)')
def plotslice(pos,filename='',boxsize=100.): ng = pos.shape[0] M.clf() M.scatter(pos[ng/4,:,:,1].flatten(),pos[ng/4,:,:,2].flatten(),s=1.,lw=0.) M.axis('tight') if filename != '': M.savefig(filename)
lda.set_labels(labels) lda.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 = lda.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 main(): parser = OptionParser(usage=usage) parser.add_option( "-c", "--compare", action="store_true", dest="compare", default=False, help="compare with default scipy.sparse solver [default: %default]") parser.add_option("-p", "--plot", action="store_true", dest="plot", default=False, help="plot time statistics [default: %default]") parser.add_option("-d", "--default-url", action="store_true", dest="default_url", default=False, help="use default url [default: %default]") parser.add_option("-f", "--format", type=type(''), dest="format", default='triplet', help="matrix format [default: %default]") (options, args) = parser.parse_args() if (len(args) >= 1): matrixNames = args else: parser.print_help(), return sizes, nnzs, times, errors = [], [], [], [] legends = ['umfpack', 'sparse.solve'] for ii, matrixName in enumerate(matrixNames): print('*' * 50) mtx = readMatrix(matrixName, options) sizes.append(mtx.shape) nnzs.append(mtx.nnz) tts = np.zeros((2, ), dtype=np.double) times.append(tts) err = np.zeros((2, 2), dtype=np.double) errors.append(err) print('size : %s (%d nnz)' % (mtx.shape, mtx.nnz)) sol0 = np.ones((mtx.shape[0], ), dtype=np.double) rhs = mtx * sol0 umfpack = um.UmfpackContext() tt = time.clock() sol = umfpack(um.UMFPACK_A, mtx, rhs, autoTranspose=True) tts[0] = time.clock() - tt print("umfpack : %.2f s" % tts[0]) error = mtx * sol - rhs err[0, 0] = nla.norm(error) print('||Ax-b|| :', err[0, 0]) error = sol0 - sol err[0, 1] = nla.norm(error) print('||x - x_{exact}|| :', err[0, 1]) if options.compare: tt = time.clock() sol = sp.solve(mtx, rhs) tts[1] = time.clock() - tt print("sparse.solve : %.2f s" % tts[1]) error = mtx * sol - rhs err[1, 0] = nla.norm(error) print('||Ax-b|| :', err[1, 0]) error = sol0 - sol err[1, 1] = nla.norm(error) print('||x - x_{exact}|| :', err[1, 1]) if options.plot: try: import pylab except ImportError: raise ImportError("could not import pylab") times = np.array(times) print(times) pylab.plot(times[:, 0], 'b-o') if options.compare: pylab.plot(times[:, 1], 'r-s') else: del legends[1] print(legends) ax = pylab.axis() y2 = 0.5 * (ax[3] - ax[2]) xrng = list(range(len(nnzs))) for ii in xrng: yy = y2 + 0.4 * (ax[3] - ax[2])\ * np.sin(ii * 2 * np.pi / (len(xrng) - 1)) if options.compare: pylab.text( ii + 0.02, yy, '%s\n%.2e err_umf\n%.2e err_sp' % (sizes[ii], np.sum( errors[ii][0, :]), np.sum(errors[ii][1, :]))) else: pylab.text( ii + 0.02, yy, '%s\n%.2e err_umf' % (sizes[ii], np.sum(errors[ii][0, :]))) pylab.plot([ii, ii], [ax[2], ax[3]], 'k:') pylab.xticks(xrng, ['%d' % (nnzs[ii]) for ii in xrng]) pylab.xlabel('nnz') pylab.ylabel('time [s]') pylab.legend(legends) pylab.axis([ax[0] - 0.05, ax[1] + 1, ax[2], ax[3]]) pylab.show()
def mctweak(wcs, xy, rd): obj = McTweak(wcs, xy, rd) # Initial args args, sigs = get_sip_args(wcs) print('Args:', args) print('Sigs:', sigs) print('Number of arguments:', len(args)) print('Logodds:', obj(args)) ndim, nwalkers = len(args), 100 p0 = emcee.utils.sample_ball(args, sigs, size=nwalkers) print('p0', p0.shape) ps = PlotSequence('mctweak') W, H = wcs.get_width(), wcs.get_height() mywcs = Sip(wcs) sampler = emcee.EnsembleSampler(nwalkers, ndim, obj) lnp0, rstate = None, None pp = [] for step in range(10000): print('Step', step) p0, lnp0, rstate = sampler.run_mcmc(p0, 1, lnprob0=lnp0, rstate0=rstate) print('Best logprob:', np.max(lnp0)) i = np.argmax(lnp0) print('Best args:', p0[i, :]) pp.extend(sampler.flatchain) sampler.reset() if step % 100 != 0: continue plt.clf() plt.plot(obj.testxy[:, 0], obj.testxy[:, 1], 'r.') for args in p0[np.random.permutation(nwalkers)[:10], :]: set_sip_args(mywcs, args) sip_compute_inverse_polynomials(mywcs, 20, 20, 1, W, 1, H) ok, x, y = mywcs.radec2pixelxy(obj.refra, obj.refdec) plt.plot(x, y, 'bo', mec='b', mfc='none', alpha=0.25) ex = 10. ngridx = ngridy = 10 stepx = stepy = 100 xgrid = np.linspace(0, W, ngridx) ygrid = np.linspace(0, H, ngridy) X = np.linspace(0, W, int(np.ceil(W / stepx))) Y = np.linspace(0, H, int(np.ceil(H / stepy))) for x in xgrid: DX, DY = [], [] xx, yy = [], [] for y in Y: dx, dy = mywcs.get_distortion(x, y) xx.append(x) yy.append(y) DX.append(dx) DY.append(dy) DX = np.array(DX) DY = np.array(DY) xx = np.array(xx) yy = np.array(yy) EX = DX + ex * (DX - xx) EY = DY + ex * (DY - yy) #plot(xx, yy, 'k-', alpha=0.5) plt.plot(EX, EY, 'b-', alpha=0.1) for y in ygrid: DX, DY = [], [] xx, yy = [], [] for x in X: dx, dy = mywcs.get_distortion(x, y) DX.append(dx) DY.append(dy) xx.append(x) yy.append(y) DX = np.array(DX) DY = np.array(DY) xx = np.array(xx) yy = np.array(yy) EX = DX + ex * (DX - xx) EY = DY + ex * (DY - yy) #plot(xx, yy, 'k-', alpha=0.5) plt.plot(EX, EY, 'b-', alpha=0.1) for x in xgrid: plt.plot(x + np.zeros_like(Y), Y, 'k-', alpha=0.5) for y in ygrid: plt.plot(X, y + np.zeros_like(X), 'k-', alpha=0.5) plt.axis([1, W, 1, H]) plt.axis('scaled') ps.savefig() pp = np.vstack(pp) print('pp', pp.shape) # plt.clf() # triangle.corner(pp, plot_contours=False) # ps.savefig() pp = []
return cos(x) - 3 * exp( -(x - 0.2) ** 2) # encontra mínimos de f(x), # começa de 1.0 e 2.0 respectivamente minimum1 = fmin(f, 1.0) print("Busca iniciada em x=1., minimo é", minimum1) minimum2 = fmin(f, 2.0) print("Busca iniciada em x=2., minimo é", minimum2) # plota função x = arange(-10, 10, 0.1) y = f(x) pylab.plot(x, y, label='$\cos(x)-3e^{-(x-0.2)^2}$') pylab.xlabel('x') pylab.grid() pylab.axis([-5, 5, -2.2, 0.5]) # adiciona minimo1 para plot pylab.plot(minimum1, f(minimum1), 'vr', label='minimo 1') # adiciona ponto de partida 1 para plot pylab.plot(1.0, f(1.0), 'or', label='partida 1') # adiciona minimo2 para plot pylab.plot(minimum2,f(minimum2),'vg',\ label='minimo 2') # adiciona ponto de partida 2 para plot pylab.plot(2.0,f(2.0),'og',label='partida 2') pylab.legend(loc='lower left')
def nebtest(MyNEB=NEB, nimages=22): import pylab as pl from interpolate import InterpolatedPath from pygmin.optimize import lbfgs_py NEBquenchParams = dict() NEBquenchParams["iprint"] = 20 NEBquenchParams["debug"] = True NEBquenchParams["maxErise"] = 0.1 x = np.arange(.5, 5., .05) y = np.arange(.5, 5., .05) z = np.zeros([len(x), len(y)]) potential = test.leps() for i in range(0, len(x)): for j in range(0, len(y)): z[j, i] = potential.getEnergy([x[i], y[j]]) print "done" #z[z>0.] = 0. #pl.imshow(z) #pl.show() initial = np.array([.75, 2.]) #np.random.random(3) final = np.array([2., .75]) #np.random.random(3) # from pygmin.optimize import quench # print "quench initial" # ret = quench.lbfgs_py(initial, potential.getEnergyGradient) # initial = ret[0] # print "quench final" # ret = quench.quench(final, potential.getEnergyGradient) # final = ret[0] # print "done with quenching" # print initial, final #print "Initial: ", initial #print "Final: ", final #pl.imshow(z) neb = MyNEB(InterpolatedPath(initial, final, nimages), potential, k=1000, dneb=False, quenchParams=NEBquenchParams) tmp = neb.coords energies_interpolate = neb.energies.copy() pl.figure() pl.subplot(2, 2, 1) pl.subplots_adjust(wspace=0.3, left=0.05, right=0.95, bottom=0.14) pl.title("path") #pl.contourf(x, y, z) pl.pcolor(x, y, z, vmax=-0.5, cmap=pl.cm.PuBu) pl.colorbar() pl.plot(tmp[:, 0], tmp[:, 1], 'ko-') print "optimizing NEB" neb.optimize() #quenchRoutine=quench.fire) print "done" tmp = neb.coords pl.plot(tmp[:, 0], tmp[:, 1], 'ro-') pl.xlabel("x") pl.ylabel("y") pl.axis(xmin=0.5, xmax=2.5, ymin=0.5, ymax=2.5) pl.subplot(1, 2, 2) pl.title("energy") pl.plot(energies_interpolate, 'ko-', label="interpolate") pl.plot(neb.energies, 'ro-', label="neb") pl.xlabel("image") pl.ylabel("energy") pl.legend(loc='best') pl.subplot(1, 2, 2) pl.title("energy") pl.plot(energies_interpolate, 'ko-', label="interpolate") pl.plot(neb.energies, 'ro-', label="neb") pl.xlabel("image") pl.ylabel("energy") pl.legend(loc='best') pl.show()
def my_axis(ymax): pl.axis([-5,105,-ymax/10.,ymax])
x_indices = np.arange(x.shape[-1]) ############################################################################### # Univariate feature selection with F-test for feature scoring # We use the default selection function: the 10% most significant features selector = SelectPercentile(f_classif, percentile=10) selector.fit(x, y) scores = -np.log10(selector._pvalues) scores /= scores.max() pl.bar(x_indices - .45, scores, width=.3, label=r'Univariate score ($-Log(p_{value})$)', color='g') ############################################################################### # Compare to the weights of an SVM clf = svm.SVC(kernel='linear') clf.fit(x, y) svm_weights = (clf.coef_**2).sum(axis=0) svm_weights /= svm_weights.max() pl.bar(x_indices - .15, svm_weights, width=.3, label='SVM weight', color='r') pl.title("Comparing feature selection") pl.xlabel('Feature number') pl.yticks(()) pl.axis('tight') pl.legend(loc='upper right') pl.show()
import random, math, pylab alpha = -0.8 nsteps = 1000000 samples_x = [] samples_y = [] x, y = 0.0, 0.0 for step in range(nsteps): xnew = random.uniform(-1.0, 1.0) ynew = random.uniform(-1.0, 1.0) exp_new = -0.5 * (xnew ** 2 + ynew ** 2) - alpha * (xnew ** 4 + ynew ** 4) exp_old = -0.5 * (x ** 2 + y ** 2) - alpha * (x ** 4 + y ** 4) if random.uniform(0.0, 1.0) < math.exp(exp_new - exp_old): x = xnew y = ynew samples_x.append(x) samples_y.append(y) pylab.hexbin(samples_x, samples_y, gridsize=50, bins=1000) pylab.axis([-1.0, 1.0, -1.0, 1.0]) cb = pylab.colorbar() pylab.xlabel('x') pylab.ylabel('y') pylab.title('A3_1') pylab.savefig('plot_A3_1.png') pylab.show()
import pylab as plt import numpy as np import pandas as pd xvalues = np.arange(0.0, 20.0, 0.1) df = pd.DataFrame({ 'x': xvalues, 'sin': np.sin(xvalues), }) plt.figure() df.plot('x', 'sin') plt.xlabel('x') plt.ylabel('y') plt.axis([0.0, 20.0, -1.2, 1.2]) plt.title('Sinusfunktion') plt.savefig('sinusfunktion.png')
LineWidth = 4.5 p.subplot(223) #p.title('Turbulence level = %(turb)1.3f ; Death rate = %(death)1.3f'% \ # {"turb":tubulence, "death":death_rate}) p.bar(ASLD_hist_ranges, ASLD_to_hist_mean + ASLD_to_hist_std, width=ASLD_hist_ranges_bin_width, color=(0.75, 0.75, 0.75, 0.75), linewidth=0) p.bar(ASLD_hist_ranges, ASLD_to_hist_mean, width=ASLD_hist_ranges_bin_width, color='k', linewidth=0) p.axis([ 0, ASLD_hist_ranges.max(), 0, (ASLD_to_hist_mean + ASLD_to_hist_std).max() ]) p.xlabel('time passed between reproductions (steps)', fontsize=FontSize) #p.ylabel('Frequency of occurrence', fontsize=FontSize) p.ylabel('number of cells', fontsize=FontSize) p.xticks(size=TickSize) p.yticks(size=TickSize) p.grid(True) #p.figure(2, figsize=(1000,600)) p.subplot(221) p.title(r'$\delta = %(death)1.3f $ $T = %(turb)1.3f $ ' % { "death": death_rate, "turb": tubulence }, fontsize=FontSize + 2)
plt.subplot(2, 2, 2) y4 = [y1[i] - y2[i] for i in range(N)] pylab.plot(grid, y4, '-', label=r'$\beta = %g$' % beta) plt.title(r'$Error$ $Matrix$ $Square$ $with$ $\chi = %g$ $\lambda=%g$' % (khi, lambdah)) plt.legend(prop={'size': 4.}) z_exa = sum( exp(-beta * 0.5 * (khi + lambdah + 2 * i)**2) for i in range(N)) print zvalues, ' ', z_exa, ' ', beta E_simulation = -(1.0 / beta) * log(zvalues) E_exac = 0.5 * (lambdah + khi)**2 print 'E simulation = ', E_simulation print 'E exact = ', E_exac #---------------------------------------Plot Potential --------------------------------------# plt.subplot(2, 2, 3) y1 = [(v(x)) for x in grid] pylab.plot(grid, y1, '-', label='$V(x)$') pylab.axis([0, pi / 2, 10, 1000]) plt.title(r'$Potential$ $with$ $\chi=%0.1f$ $and$ $\lambda= %0.1f$' % (khi, lambdah)) plt.legend(loc=1, prop={'size': 8.}) #--------------------------------------------------------------------------------------------# plt.get_current_fig_manager().resize(780, 660) plt.tight_layout() pylab.show()
import pylab from PIL import Image # open random image of dimensions 639x516 img = Image.open('images/3wolfmoon.jpg') print img img = numpy.asarray(img, dtype='float64') / 256. print "Image shape: ", img.shape # put image in 4D tensor of shape (1, 3, height, width) img_ = img.swapaxes(0, 2).swapaxes(1, 2).reshape(1, 3, 639, 516) # print img_ * 256 filtered_img = f(img_) print "The filtered img shape: ", filtered_img.shape # plot original image and first and second components of output pylab.subplot(1, 3, 1) pylab.axis('off') pylab.imshow(img) pylab.gray() # recall that the convOp output (filtered image) is actually a "minibatch", # of size 1 here, so we take index 0 in the first dimension: pylab.subplot(1, 3, 2) pylab.axis('off') pylab.imshow(filtered_img[0, 0, :, :]) pylab.subplot(1, 3, 3) pylab.axis('off') pylab.imshow(filtered_img[0, 1, :, :]) pylab.show()
from shelf import PKL import cross_coil as cc import scipy as sp from surface import bernstein from scipy.interpolate import interp1d import scipy.optimize as op import seaborn as sns rc = {'figure.figsize':[7*12/14,7],'savefig.dpi':110, #*12/16 'savefig.jpeg_quality':100,'savefig.pad_inches':0.1, 'lines.linewidth':0.75} sns.set(context='paper',style='white',font='sans-serif',palette='Set2', font_scale=7/8,rc=rc) color = cycle(sns.color_palette('Set2')) pl.figure() pl.axis('equal') pl.axis('off') eqdsk = 'vde' eqdsk = 'SN' sf = SF(Config(eqdsk)) sf.eq['ncoil'] = 0 if eqdsk == 'vde': eq = EQ(sf,dCoil=1,limit=[4.25,8,-4.5,2],n=1e3) #eq = EQ(sf,dCoil=1,limit=[3,9,-6,6],n=5e3) else: eq = EQ(sf,dCoil=1,limit=[5,13,-5.5,5],n=5e4)
Xhr = pylab.linspace(0, 1, 101) Yhr = pylab.linspace(0, 1, 101) fhr = exactSol(2, 5, Xhr, Yhr) vmin, vmax = fhr.min(), fhr.max() # make plots f1 = pylab.figure(1) pylab.subplot(1, 2, 1) cax = pylab.pcolormesh(Xf, Yf, pylab.transpose(q[:, :, 0]), vmin=vmin, vmax=vmax) pylab.axis('image') pylab.subplot(1, 2, 2) cax = pylab.pcolormesh(Xhr, Yhr, fhr) pylab.axis('image') # compute error fex = exactSol(2, 5, Xf, Yf) error = numpy.abs(fex - pylab.transpose(q_1)).sum() fex = exactSol(2, 5, Xf_5, Yf_5) error = error + numpy.abs(fex - pylab.transpose(q_5)).sum() fex = exactSol(2, 5, Xf_8, Yf_8) error = error + numpy.abs(fex - pylab.transpose(q_8)).sum()
def main(argv=None): if argv is None: argv = sys.argv[1:] # Read options and check for errors. options = read_command_line(argv) if (options == None): return s = starset.StarSet(options.align_root) nEpochs = len(s.stars[0].years) nStars = len(s.stars) names = np.array(s.getArray('name')) if (options.center_star != None): idx = np.where(names == options.center_star)[0] if (len(idx) > 0): options.xcenter = s.stars[idx].x options.ycenter = s.stars[idx].y else: print 'Could not find star to center, %s. Reverting to Sgr A*.' % \ (options.center_star) # Create a combined error term (quad sum positional and alignment) combineErrors(s) yearsInt = np.floor(s.years) # Set up a color scheme cnorm = colors.normalize(s.years.min(), s.years.max()+1) cmap = cm.gist_ncar colorList = [] for ee in range(nEpochs): colorList.append( cmap(cnorm(yearsInt[ee])) ) py.close(2) py.figure(2, figsize=(10,10)) previousYear = 0.0 for ee in range(nEpochs): x = s.getArrayFromEpoch(ee, 'x') y = s.getArrayFromEpoch(ee, 'y') xe = s.getArrayFromEpoch(ee, 'xerr') ye = s.getArrayFromEpoch(ee, 'yerr') mag = s.getArrayFromEpoch(ee, 'mag') idx = np.where((x > -1000) & (y > -1000))[0] x = x[idx] y = y[idx] xe = xe[idx] ye = ye[idx] mag = mag[idx] tmpNames = names[idx] if yearsInt[ee] != previousYear: previousYear = yearsInt[ee] label = '%d' % yearsInt[ee] else: label = '_nolegend_' (line, foo1, foo2) = py.errorbar(x, y, xerr=xe, yerr=ye, color=colorList[ee], fmt='k^', markeredgecolor=colorList[ee], label=label, picker=4) #selector = HighlightSelected(line, ) class HighlightSelected(lines.VertexSelector): def __init__(self, line): lines.VertexSelector.__init__(self, line) self.markers, = self.axes.plot([], [], 'k^', markerfacecolor='none') def process_selected(self, ind, xs, ys): self.markers.set_data(xs, ys) self.canvas.draw() def onpick(event): polyCollection = event.artist polyCollection.get_xdata() polyCollection.get_xdata() ind = event.ind xlo = options.xcenter + (options.range) xhi = options.xcenter - (options.range) ylo = options.ycenter - (options.range) yhi = options.ycenter + (options.range) py.axis('equal') py.axis([xlo, xhi, ylo, yhi]) py.legend(numpoints=1, loc='lower left') py.show() return
bias = Bias(alpha, X, y) ym = (y.flatten() < 0).nonzero()[0] yp = (y.flatten() > 0).nonzero()[0] ii = inner(wv, X) bias2 = -0.5 * (max(ii[ym]) + min(ii[yp])) print('weight vector: %s' % wv) print('support vectors: %s %s' % (sv1, sv2)) print('bias (from points): %s' % bias) print('bias (with vectors): %s' % bias2) # plot data pylab.plot(c1[:, 0], c1[:, 1], 'bo', markersize=5) pylab.plot(c2[:, 0], c2[:, 1], 'yo', markersize=5) # plot hyperplane: wv[0] x + wv[1] y + bias = 0 xmin, xmax, ymin, ymax = pylab.axis() hx = array([floor(xmin - .1), ceil(xmax + .1)]) hy = -wv[0] / wv[1] * hx - bias / wv[1] pylab.plot(hx, hy, 'k-') #pylab.axis([xmin,xmax,ymin,ymax]) # plot the support points pylab.plot(XX[sv1, 0], XX[sv1, 1], 'bo', markersize=8) pylab.plot(-XX[sv2, 0], -XX[sv2, 1], 'yo', markersize=8) #pylab.axis('equal') pylab.show() # end of file
print mc, ma, av, D Ds.append(D) Eslist.append(Es) ED=EDmax_hansen(ka, mu=1., Ns=Ns) print print f, ka, sum(Ds)/N_MC, ED print sys.stdout.flush() ecdfD=ECDF(Ds) pylab.figure(1) pylab.plot(deval,ecdfD(deval), '%s+-'%clr, label="ECDF (Dipoles), $N_{obs}$=%d"%(N_obs_points)) output_data.append(remove_nan(ecdfD(deval))) #pylab.plot(deval, [FD_hertz_one_cut(d) for d in deval], label="Theoretical CDF (a=0 m)") #pylab.plot(deval, [FD_hertz_one_cut_costheta(d) for d in deval], label="Theoretical CDF cos(theta)(a=0 m)") pylab.axis([deval[0],deval[-1],0,1]) pylab.grid() pylab.legend(loc=4) pylab.xlabel("Max. Directivity D") pylab.ylabel("CDF") pylab.title("$N_{dipoles}=%d$, MC runs=%d, $Frequency=%dGHz$, $R=%dm$"%(N_dipole,N_MC,f/1e9,distance)) #pylab.show() fig = matplotlib.pyplot.gcf() fig.set_size_inches(18.5, 10.5) pp = PdfPages(r'D:\HIWI\python-script\new_new_results\4.13/result_d.pdf') pylab.savefig(pp, format='pdf',dpi=fig1.dpi, bbox_inches='tight') pp.close() output=zip(*output_data) numpy.savetxt(r"D:\HIWI\python-script\new_new_results\4.13/4.13d.dat", output, fmt=['%.6f']*len(output_data))
if filtered in corpus: continue corpus.append(filtered) vocab = sorted(list(vocab)) vocab_index = {} for i, w in enumerate(vocab): vocab_index[w] = i st.subheader("Wordcloud") no_of_words = st.slider('How many words do you want?', 1, 50, 20) wordcloud = WordCloud(background_color='white', stopwords=stopset, max_words=no_of_words).generate( ' '.join(all_filtered_words)) plt.figure(figsize=(12, 12)) plt.imshow(wordcloud, interpolation="bilinear") plt.axis("off") st.pyplot(plt) new_corpus = [] for doc in corpus: new_doc = [] for word in doc: word_idx = vocab_index[word] new_doc.append(word_idx) new_corpus.append(new_doc) st.subheader('Topic Modelling') model_type = st.radio( "Please choose the topic model", ('LDA - Latent Dirichlet Allocation', 'hLDA - hierarchical LDA')) if model_type == 'hLDA - hierarchical LDA': #HLDA模型 st.subheader("Parameters for hLDA:")
def plot_subgraph(G, m2m_list, ms1_peakids_to_highlight, motif_idx, colour_map, save_to=None): # parameters for the M2M nodes motif_node_alpha = 0.75 motif_node_size = 4000 # parameters for the ms1_peakids_to_highlight highlight_node_colour = 'gray' highlight_node_alpha = 0.50 highlight_node_size = 2000 # parameters for other nodes other_nodes_colour = 'lightgray' other_nodes_alpha = 0.50 other_nodes_size = 1000 # label parameters label_colour = 'black' label_background_colour = 'gray' label_alpha = 0.75 label_fontsize = 24 # other parameters fig_width = 15 fig_height = 15 edge_alpha = 0.25 highlight_docs = set() other_docs = set() motif_nodes = set() motif_colours = [] doc_labels = {} motif_labels = {} for node_id, node_data in G.nodes(data=True): # group == 1 is a doc, 2 is a motif if node_data['group'] == 1 and int( node_data['peakid']) in ms1_peakids_to_highlight: highlight_docs.add(node_id) doc_labels[node_id] = node_data['peakid'] elif node_data['group'] == 2: motif_name = node_data['name'] _, motif_id = motif_name.split('_') motif_id = int(motif_id) if motif_id in m2m_list: motif_nodes.add(node_id) neighbours = G.neighbors(node_id) other_docs.update(neighbours) node_colour = colour_map.to_rgba(motif_idx[motif_id]) node_colour = rgb2hex(node_colour) motif_colours.append(node_colour) motif_labels[node_id] = 'M2M_%d' % motif_id other_docs = other_docs - highlight_docs to_keep = motif_nodes | highlight_docs | other_docs SG = nx.Graph(G.subgraph(to_keep)) for u, v, d in SG.edges(data=True): d['weight'] *= 100 plt.figure(figsize=(fig_width, fig_height), dpi=900) plt.axis('off') fig = plt.figure(1) pos = nx.spring_layout(SG, k=0.25, iterations=50) nx.draw_networkx_nodes(SG, pos, nodelist=other_docs, alpha=other_nodes_alpha, node_color=other_nodes_colour, node_size=other_nodes_size) nx.draw_networkx_nodes(SG, pos, nodelist=highlight_docs, alpha=highlight_node_alpha, node_color=highlight_node_colour, node_size=highlight_node_size) nx.draw_networkx_nodes(SG, pos, nodelist=motif_nodes, alpha=motif_node_alpha, node_color=motif_colours, node_size=motif_node_size) nx.draw_networkx_edges(SG, pos, alpha=edge_alpha) nx.draw_networkx_labels(SG, pos, doc_labels, font_size=label_fontsize) nx.draw_networkx_labels(SG, pos, motif_labels, font_size=label_fontsize) # for node_id in node_labels: # x, y = pos[node_id] # label = node_labels[node_id] # y_offset = -0.3 # if node_id in motif_nodes: # y_offset += -0.5 # plt.text(x,y+y_offset, s=node_labels[node_id], # color=label_colour, fontsize=label_fontsize, # bbox=dict(facecolor=label_background_colour, alpha=label_alpha), # horizontalalignment='center') if save_to is not None: print "Figure saved to %s" % save_to plt.savefig(save_to, bbox_inches='tight') plt.show() return SG
args = parser.parse_args() raw_image = Image.open('data/empire.jpg') if args.blackwhite: raw_image = raw_image.convert('L') im = array(raw_image) if args.blackwhite: im2 = filters.gaussian_filter(im, args.sigma) else: im2 = zeros(im.shape) for i in range(3): im2[:, :, i] = filters.gaussian_filter(im[:, :, i], args.sigma) # im2 = uint8(im2) normalized_image = im / im2 if not args.blackwhite: normalized_image = normalized_image.astype('uint8') figure() if args.blackwhite: gray() imshow(normalized_image) axis('equal') axis('off') show()
for het in ['Slightly', 'Moderately', 'Very']: model = dismod3.data.fetch_disease_model_if_necessary(30026, 'models/mi/') model.keep(areas=['europe_western'], sexes=['male', 'total'], start_year=2000) model.parameters['i']['heterogeneity'] = het model.vars += dismod3.ism.age_specific_rate(model, 'i') dismod3.fit.fit_asr(model, 'i') m[het] = model # display uncertainty in age pattern pl.clf() for i, het in enumerate(['Slightly', 'Moderately', 'Very']): est = m[het].vars['i']['mu_age'].stats() # x = pl.arange(40,101,10) y = est['mean'][x] yerr = [y - est['95% HPD interval'][x,0], est['95% HPD interval'][x,1] - y] # pl.errorbar(x+i-1, y, yerr=yerr, fmt='s-', color=colors[i], mec='w', label=het) pl.legend(title='Heterogeneity:', fancybox=True, shadow=True, loc='upper left') pl.xlabel('Age (Years)') pl.ylabel('Incidence (Per PY)') pl.title('Parameter Uncertainty of Empirical Prior\n(incidence data for europe_western only)') dismod3.graphics.plot_data_bars(model.get_data('i')) pl.axis([35,105,-.001,.11])
def Main(): options, _ = MakeOpts().parse_args(sys.argv) assert options.species_filename assert options.first_col and options.second_col print 'Reading species list from', options.species_filename filter_cols, filter_vals = [], [] if options.filter_cols: assert options.filter_vals filter_cols = map(str.strip, options.filter_cols.split(',')) filter_vals = map(str.strip, options.filter_vals.split(',')) # Read and filter species data r = csv.DictReader(open(options.species_filename)) first_col, second_col = options.first_col, options.second_col pairmap = {} for row in r: apply_filter = lambda x, y: x in row and row[x] == y or_reduce = lambda x, y: x or y passed_filter = reduce(or_reduce, map(apply_filter, filter_cols, filter_vals), True) if not passed_filter: continue a, b = row[first_col].strip(), row[second_col].strip() if not a or not b: continue key = (a, b) pairmap.setdefault(key, []).append(row) #for key, row_list in pairmap.iteritems(): # if len(row_list) < 5: # print key, row_list # Find cross-product of column values (all pairs). all_a = list(set([x[0] for x in pairmap.keys()])) all_b = list(set([x[1] for x in pairmap.keys()])) a_to_num = dict((v, i) for i, v in enumerate(all_a)) b_to_num = dict((v, i) for i, v in enumerate(all_b)) all_possible_pairs = list(itertools.product(all_a, all_b)) third_col = options.third_col or 'fake key' get_col = lambda x: x.get(third_col, None) col_vals = dict((k, map(get_col, v)) for k, v in pairmap.iteritems()) counts = {} totals = [] all_vals = set() for k, v in col_vals.iteritems(): counter = Counter(v) all_vals.update(counter.keys()) counts[k] = counter totals.append(sum(counter.values())) x_vals = [] y_vals = [] count_array = [] z_vals = [] max_val = max(totals) for pair in all_possible_pairs: a, b = pair x_vals.append(a_to_num[a]) y_vals.append(b_to_num[b]) z_vals.append(sum(counts.get(pair, {}).values())) count_array.append(counts.get(pair, {})) # Plot circle scatter. axes = pylab.axes() axes.grid(color='g', linestyle='--', linewidth=1) if options.third_col: colormap = ColorMap(all_vals) PieScatter(axes, x_vals, y_vals, count_array, max_val, colormap) handles, labels = axes.get_legend_handles_labels() mapped_labels = dict(zip(labels, handles)) labels = sorted(mapped_labels.keys()) handles = [mapped_labels[k] for k in labels] pylab.legend(handles, labels) else: scaled_z_vals = pylab.array(map(float, z_vals)) max_z = max(scaled_z_vals) scaled_z_vals /= (4.0 * max_z) scaled_z_vals = pylab.sqrt(scaled_z_vals) CircleScatter(axes, x_vals, y_vals, scaled_z_vals) for x, y, z in zip(x_vals, y_vals, z_vals): pylab.text(x + 0.1, y + 0.1, str(z)) # Labels, titles and ticks. pylab.title('%s vs. %s' % (options.first_col, options.second_col)) pylab.xlabel(options.first_col) pylab.ylabel(options.second_col) pylab.figtext(0.70, 0.02, '%d examples total.' % sum(totals)) size_8 = FontProperties(size=8) a_labels = [a or "None given" for a in all_a] b_labels = [b or "None given" for b in all_b] pylab.xticks(range(0, len(a_labels)), a_labels, fontproperties=size_8) pylab.yticks(range(0, len(b_labels)), b_labels, fontproperties=size_8) # Scale and show. pylab.axis('scaled') pylab.axis([-1, len(all_a), -1, len(all_b)]) pylab.show()
def plot_bipartite(G, min_degree, fig_width=10, fig_height=20, spacing_left=1, spacing_right=2): # extract subgraph of docs connected to at least min_degree motifs doc_nodes_to_keep = set() motif_nodes_to_keep = set() nodes = G.nodes(data=True) for node_id, node_data in nodes: # group == 1 is a doc, 2 is a motif if node_data['group'] == 1 and G.degree(node_id) >= min_degree: neighbours = G.neighbors(node_id) doc_nodes_to_keep.add(node_id) motif_nodes_to_keep.update(neighbours) to_keep = doc_nodes_to_keep | motif_nodes_to_keep # set union SG = nx.Graph(G.subgraph(to_keep)) # make bipartite layout, put doc nodes on left, motif nodes on right pos = dict() pos.update( (n, (1, i * spacing_left)) for i, n in enumerate(doc_nodes_to_keep)) pos.update( (n, (2, i * spacing_right)) for i, n in enumerate(motif_nodes_to_keep)) # for labelling purpose motif_singleton = {} for n in motif_nodes_to_keep: children = G.neighbors(n) # get the children of this motif degree_dict = G.degree(nbunch=children) # get the degrees of children # count how many children have degree == 1 children_degrees = [degree_dict[c] for c in degree_dict] count_singleton = sum(child_deg == 1 for child_deg in children_degrees) motif_singleton[n] = count_singleton # set the node and edge labels doc_labels = {} motif_labels = {} doc_motifs = {} # used for the fragmentation spectra plot all_motifs = set() for node_id, node_data in SG.nodes(data=True): if node_data['group'] == 2: # is a motif motif_labels[node_id] = "%s (+%d)" % (node_data['name'], motif_singleton[node_id]) elif node_data['group'] == 1: # is a doc pid = int(node_data['peakid']) doc_labels[node_id] = 'pid_%d' % pid parent_motifs = set() for neighbour_id in SG.neighbors(node_id): motif_name = SG.node[neighbour_id]['name'] _, motif_id = motif_name.split('_') parent_motifs.add(int(motif_id)) doc_motifs[pid] = parent_motifs all_motifs.update(parent_motifs) # plot the bipartite graph plt.figure(figsize=(fig_width, fig_height)) plt.axis('off') fig = plt.figure(1) nx.draw_networkx_nodes(SG, pos, alpha=0.25) nx.draw_networkx_edges(SG, pos, alpha=0.25) _ = nx.draw_networkx_labels(SG, pos, doc_labels, font_size=10) _ = nx.draw_networkx_labels(SG, pos, motif_labels, font_size=16) ymax = max( len(doc_labels) * spacing_left, len(motif_labels) * spacing_right) _ = plt.ylim([-1, ymax]) _ = plt.title('MS1 peaks connected to at least %d motifs' % min_degree) # assign index to each M2M i = 0 motif_idx = {} for key in all_motifs: motif_idx[key] = i i += 1 return doc_nodes_to_keep, doc_motifs, motif_idx
def main(): import optparse parser = optparse.OptionParser('%prog [options]') parser.add_option('-o', dest='outfn', help='Output filename (FITS table)') parser.add_option('-i', dest='imgfn', help='Image input filename') parser.add_option('-f', dest='flagfn', help='Flags input filename') parser.add_option('-z', dest='flagzero', help='Flag image: zero = 0', action='store_true') parser.add_option('-p', dest='psffn', help='PsfEx input filename') #parser.add_option('-s', dest='postxt', help='Source positions input text file') parser.add_option( '-S', dest='statsfn', help='Output image statistis filename (FITS table); optional') parser.add_option('--sky', dest='fitsky', action='store_true', help='Fit sky level as well as fluxes?') parser.add_option( '--band', '-b', dest='band', default='r', help= 'Which SDSS band to use for forced photometry profiles: default %default' ) parser.add_option('-g', dest='gaussianpsf', action='store_true', default=False, help='Use multi-Gaussian approximation to PSF?') parser.add_option('-P', dest='plotbase', default='scuss', help='Plot base filename (default: %default)') parser.add_option('-l', dest='local', action='store_true', default=False, help='Use local SDSS tree?') # TESTING parser.add_option('--sub', dest='sub', action='store_true', help='Cut to small sub-image for testing') parser.add_option('--res', dest='res', action='store_true', help='Just plot results from previous run') opt, args = parser.parse_args() # Check command-line arguments if len(args): print('Extra arguments:', args) parser.print_help() sys.exit(-1) for fn, name, exists in [ (opt.outfn, 'output filename (-o)', False), (opt.imgfn, 'image filename (-i)', True), (opt.flagfn, 'flag filename (-f)', True), (opt.psffn, 'PSF filename (-p)', True), #(opt.postxt, 'Source positions filename (-s)', True), ]: if fn is None: print('Must specify', name) sys.exit(-1) if exists and not os.path.exists(fn): print('Input file', fn, 'does not exist') sys.exit(-1) lvl = logging.DEBUG logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout) if opt.res: ps = PlotSequence(opt.plotbase) plot_results(opt.outfn, ps) sys.exit(0) sdss = DR9(basedir='.') #data/unzip') if opt.local: sdss.useLocalTree(pobj='photoObjs-new') sdss.saveUnzippedFiles('data/unzip') # Read inputs print('Reading input image', opt.imgfn) img, hdr = fitsio.read(opt.imgfn, header=True) print('Read img', img.shape, img.dtype) H, W = img.shape img = img.astype(np.float32) sky = hdr['SKYADU'] print('Sky:', sky) cal = hdr['CALIA73'] print('Zeropoint cal:', cal) zpscale = 10.**((2.5 + cal) / 2.5) print('Zp scale', zpscale) wcs = anwcs(opt.imgfn) print('WCS pixel scale:', wcs.pixel_scale()) print('Reading flags', opt.flagfn) flag = fitsio.read(opt.flagfn) print('Read flag', flag.shape, flag.dtype) imslice = None if opt.sub: imslice = (slice(0, 800), slice(0, 800)) if imslice is not None: img = img[imslice] H, W = img.shape flag = flag[imslice] wcs.set_width(W) wcs.set_height(H) print('Reading PSF', opt.psffn) psf = PsfEx(opt.psffn, W, H) if opt.gaussianpsf: picpsffn = opt.psffn + '.pickle' if not os.path.exists(picpsffn): psf.savesplinedata = True print('Fitting PSF model...') psf.ensureFit() pickle_to_file(psf.splinedata, picpsffn) print('Wrote', picpsffn) else: print('Reading PSF model parameters from', picpsffn) data = unpickle_from_file(picpsffn) print('Fitting PSF...') psf.fitSavedData(*data) # x = psf.instantiateAt(0., 0.) print('PSF', x.shape) x = x.shape[0] #psf.radius = (x+1)/2. psf.radius = 20 print('Computing image sigma...') if opt.flagzero: bad = np.flatnonzero((flag == 0)) good = (flag != 0) else: bad = np.flatnonzero((flag != 0)) good = (flag == 0) igood = img[good] #plo,med,phi = [percentile_f(igood, p) for p in [25, 50, 75]] #sky = med plo, phi = [percentile_f(igood, p) for p in [25, 75]] # Wikipedia says: IRQ -> sigma: sigma = (phi - plo) / (0.6745 * 2) print('Sigma:', sigma) invvar = np.zeros_like(img) + (1. / sigma**2) invvar.flat[bad] = 0. del bad del good del igood band = 'u' # Get SDSS sources within the image... print('Reading SDSS objects...') cols = [ 'objid', 'ra', 'dec', 'fracdev', 'objc_type', 'theta_dev', 'theta_deverr', 'ab_dev', 'ab_deverr', 'phi_dev_deg', 'theta_exp', 'theta_experr', 'ab_exp', 'ab_experr', 'phi_exp_deg', 'devflux', 'expflux', 'resolve_status', 'nchild', 'flags', 'objc_flags', 'run', 'camcol', 'field', 'id', 'psfflux', 'psfflux_ivar', 'cmodelflux', 'cmodelflux_ivar', 'modelflux', 'modelflux_ivar', 'extinction' ] T = read_photoobjs_in_wcs(wcs, 1. / 60., sdss=sdss, cols=cols) print('Got', len(T), 'SDSS objs') T.treated_as_pointsource = treat_as_pointsource(T, band_index(opt.band)) ok, T.x, T.y = wcs.radec2pixelxy(T.ra, T.dec) # We will break the image into cells for speed -- save the # original full-size inputs here. fullinvvar = invvar fullimg = img fullpsf = psf fullT = T # We add a margin around each cell -- we want sources within the # cell, we need to include a margin of image pixels touched by # those sources, and also an additional margin of sources that # touch those pixels. margin = 10 # pixels # Number of cells to split the image into imh, imw = img.shape nx = int(np.round(imw / 400.)) ny = int(np.round(imh / 400.)) #nx = ny = 20 #nx = ny = 1 # cell positions XX = np.round(np.linspace(0, W, nx + 1)).astype(int) YY = np.round(np.linspace(0, H, ny + 1)).astype(int) results = [] # Image statistics imstats = fits_table() imstats.xlo = np.zeros(((len(YY) - 1) * (len(XX) - 1)), int) imstats.xhi = np.zeros_like(imstats.xlo) imstats.ylo = np.zeros_like(imstats.xlo) imstats.yhi = np.zeros_like(imstats.xlo) imstats.ninbox = np.zeros_like(imstats.xlo) imstats.ntotal = np.zeros_like(imstats.xlo) imstatkeys = ['imchisq', 'imnpix', 'sky'] for k in imstatkeys: imstats.set(k, np.zeros(len(imstats))) # Plots: ps = PlotSequence(opt.plotbase) # Loop over cells... celli = -1 for yi, (ylo, yhi) in enumerate(zip(YY, YY[1:])): for xi, (xlo, xhi) in enumerate(zip(XX, XX[1:])): celli += 1 imstats.xlo[celli] = xlo imstats.xhi[celli] = xhi imstats.ylo[celli] = ylo imstats.yhi[celli] = yhi print() print('Doing image cell %i: x=[%i,%i), y=[%i,%i)' % (celli, xlo, xhi, ylo, yhi)) # We will fit for sources in the [xlo,xhi), [ylo,yhi) box. # We add a margin in the image around that ROI # Beyond that, we add a margin of extra sources # image region: [ix0,ix1) ix0 = max(0, xlo - margin) ix1 = min(W, xhi + margin) iy0 = max(0, ylo - margin) iy1 = min(H, yhi + margin) S = (slice(iy0, iy1), slice(ix0, ix1)) img = fullimg[S] invvar = fullinvvar[S] if not opt.gaussianpsf: # Instantiate pixelized PSF at this cell center. pixpsf = fullpsf.instantiateAt((xlo + xhi) / 2., (ylo + yhi) / 2.) print('Pixpsf:', pixpsf.shape) psf = PixelizedPSF(pixpsf) else: psf = fullpsf psf = ShiftedPsf(fullpsf, ix0, iy0) # sources nearby x0 = max(0, xlo - margin * 2) x1 = min(W, xhi + margin * 2) y0 = max(0, ylo - margin * 2) y1 = min(H, yhi + margin * 2) # FITS pixel indexing, so -1 J = np.flatnonzero((fullT.x - 1 >= x0) * (fullT.x - 1 < x1) * (fullT.y - 1 >= y0) * (fullT.y - 1 < y1)) T = fullT[J].copy() T.row = J # Remember which sources are within the cell (not the margin) T.inbounds = ((T.x - 1 >= xlo) * (T.x - 1 < xhi) * (T.y - 1 >= ylo) * (T.y - 1 < yhi)) # Shift source positions so they are correct for this subimage (cell) #T.x -= ix0 #T.y -= iy0 imstats.ninbox[celli] = sum(T.inbounds) imstats.ntotal[celli] = len(T) # print 'Image subregion:', img.shape print('Number of sources in ROI:', sum(T.inbounds)) print('Number of sources in ROI + margin:', len(T)) #print 'Source positions: x', T.x.min(), T.x.max(), 'y', T.y.min(), T.y.max() twcs = WcslibWcs(None, wcs=wcs) twcs.setX0Y0(ix0, iy0) # Create tractor.Image object tim = Image(data=img, invvar=invvar, psf=psf, wcs=twcs, sky=ConstantSky(sky), photocal=LinearPhotoCal(zpscale, band=band), name=opt.imgfn, domask=False) # Create tractor catalog objects cat, catI = get_tractor_sources_dr9(None, None, None, bandname=opt.band, sdss=sdss, objs=T.copy(), bands=[band], nanomaggies=True, fixedComposites=True, useObjcType=True, getobjinds=True) print('Got', len(cat), 'Tractor sources') assert (len(cat) == len(catI)) # for r,d,src in zip(T.ra[catI], T.dec[catI], cat): # print 'Source', src.getPosition() # print ' vs', r, d # Create Tractor object. tractor = Tractor([tim], cat) # print 'All params:' # tractor.printThawedParams() t0 = Time() tractor.freezeParamsRecursive('*') tractor.thawPathsTo(band) if opt.fitsky: tractor.thawPathsTo('sky') # print 'Fitting params:' # tractor.printThawedParams() minsig = 0.1 # making plots? #if celli <= 10: # mod0 = tractor.getModelImage(0) # Forced photometry X = tractor.optimize_forced_photometry( #minsb=minsig*sigma, mindlnp=1., minFlux=None, variance=True, fitstats=True, shared_params=False, sky=opt.fitsky, use_ceres=True, BW=8, BH=8) IV = X.IV fs = X.fitstats print('Forced photometry took', Time() - t0) # print 'Fit params:' # tractor.printThawedParams() # Record results X = np.zeros(len(T), np.float32) X[catI] = np.array([ src.getBrightness().getBand(band) for src in cat ]).astype(np.float32) T.set('tractor_%s_nanomaggies' % band, X) X = np.zeros(len(T), np.float32) X[catI] = IV.astype(np.float32) T.set('tractor_%s_nanomaggies_invvar' % band, X) X = np.zeros(len(T), bool) X[catI] = True T.set('tractor_%s_has_phot' % band, X) # DEBUG X = np.zeros(len(T), np.float64) X[catI] = np.array([src.getPosition().ra for src in cat]) T.tractor_ra = X X = np.zeros(len(T), np.float64) X[catI] = np.array([src.getPosition().dec for src in cat]) T.tractor_dec = X T.cell = np.zeros(len(T), int) + celli if fs is not None: # Per-source stats for k in [ 'prochi2', 'pronpix', 'profracflux', 'proflux', 'npix' ]: T.set(k, getattr(fs, k)) # Per-image stats for k in imstatkeys: X = getattr(fs, k) imstats.get(k)[celli] = X[0] #T.about() # DEBUG ## KK = np.flatnonzero(T.tractor_u_nanomaggies[catI] > 3.) ## T.cut(catI[KK]) ## cat = [cat[k] for k in KK] ## catI = np.arange(len(cat)) ## #T.about() ## print T.tractor_u_nanomaggies ## print T.psfflux[:,0] results.append(T.copy()) # tc = T.copy() # print 'tc' # print tc.tractor_u_nanomaggies # print tc.psfflux[:,0] # plot_results(None, ps, tc) # mc = merge_tables([x.copy() for x in results]) # print 'Results:' # for x in results: # print x.tractor_u_nanomaggies # print x.psfflux[:,0] # print 'Merged' # print mc.tractor_u_nanomaggies # print mc.psfflux[:,0] # plot_results(None, ps, mc) # Make plots for the first N cells if celli >= 10: continue mod = tractor.getModelImage(0) ima = dict(interpolation='nearest', origin='lower', vmin=sky + -2. * sigma, vmax=sky + 5. * sigma, cmap='gray', extent=[ix0 - 0.5, ix1 - 0.5, iy0 - 0.5, iy1 - 0.5]) ok, rc, dc = wcs.pixelxy2radec((ix0 + ix1) / 2., (iy0 + iy1) / 2.) plt.clf() plt.imshow(img, **ima) plt.title('Data: ~ (%.3f, %.3f)' % (rc, dc)) #ps.savefig() ax = plt.axis() plt.plot(T.x - 1, T.y - 1, 'o', mec='r', mfc='none', ms=10) plt.axis(ax) plt.title('Data + SDSS sources ~ (%.3f, %.3f)' % (rc, dc)) ps.savefig() flim = 2.5 I = np.flatnonzero(T.psfflux[catI, 0] > flim) for ii in I: tind = catI[ii] src = cat[ii] fluxes = [ T.psfflux[tind, 0], src.getBrightness().getBand(band) ] print('Fluxes', fluxes) mags = [-2.5 * (np.log10(flux) - 9) for flux in fluxes] print('Mags', mags) t = '' if type(src) == ExpGalaxy: t = 'E' elif type(src) == DevGalaxy: t = 'D' elif type(src) == PointSource: t = 'S' elif type(src) == FixedCompositeGalaxy: t = 'C' else: t = str(type(src)) plt.text(T.x[tind], T.y[tind] + 3, '%.1f / %.1f %s' % (mags[0], mags[1], t), color='r', va='bottom', bbox=dict(facecolor='k', alpha=0.5)) plt.plot(T.x[tind] - 1, T.y[tind] - 1, 'rx') for i, src in enumerate(cat): flux = src.getBrightness().getBand(band) if flux < flim: continue tind = catI[i] fluxes = [T.psfflux[tind, 0], flux] print('RA,Dec', T.ra[tind], T.dec[tind]) print(src.getPosition()) print('Fluxes', fluxes) mags = [-2.5 * (np.log10(flux) - 9) for flux in fluxes] print('Mags', mags) plt.text(T.x[tind], T.y[tind] - 3, '%.1f / %.1f' % (mags[0], mags[1]), color='g', va='top', bbox=dict(facecolor='k', alpha=0.5)) plt.plot(T.x[tind] - 1, T.y[tind] - 1, 'g.') plt.axis(ax) ps.savefig() # plt.clf() # plt.imshow(mod0, **ima) # plt.title('Initial Model') # #plt.colorbar() # ps.savefig() # plt.clf() # plt.imshow(mod0, interpolation='nearest', origin='lower', # cmap='gray', extent=[ix0-0.5, ix1-0.5, iy0-0.5, iy1-0.5]) # plt.title('Initial Model') # plt.colorbar() # ps.savefig() plt.clf() plt.imshow(mod, **ima) plt.title('Model') ps.savefig() noise = np.random.normal(scale=sigma, size=img.shape) plt.clf() plt.imshow(mod + noise, **ima) plt.title('Model + noise') ps.savefig() chi = (img - mod) * tim.getInvError() plt.clf() plt.imshow(chi, interpolation='nearest', origin='lower', cmap='RdBu', vmin=-5, vmax=5) plt.title('Chi') ps.savefig() # Merge results from the cells TT = merge_tables(results) # Cut to just the sources within the cells TT.cut(TT.inbounds) TT.delete_column('inbounds') # Sort them back into original order TT.cut(np.argsort(TT.row)) #TT.delete_column('row') TT.writeto(opt.outfn) print('Wrote results to', opt.outfn) if opt.statsfn: imstats.writeto(opt.statsfn) print('Wrote image statistics to', opt.statsfn) plot_results(opt.outfn, ps)
def plot_results(outfn, ps, T=None): if T is None: T = fits_table(outfn) print('read', len(T)) I = np.flatnonzero(T.tractor_u_has_phot) print('Plotting', len(I), 'with phot') stars = (T.objc_type[I] == 6) gals = (T.objc_type[I] == 3) # SDSS measurements nm = np.zeros(len(I)) nm[stars] = T.psfflux[I[stars], 0] nm[gals] = T.modelflux[I[gals], 0] # Tractor measurements counts = T.tractor_u_nanomaggies[I] dcounts = 1. / np.sqrt(T.tractor_u_nanomaggies_invvar[I]) # plt.clf() # plt.errorbar(nm, counts, yerr=dcounts, fmt='o', ms=5) # plt.xlabel('SDSS nanomaggies') # plt.ylabel('Tractor counts') # plt.title('Tractor forced photometry of SCUSS data') # ps.savefig() # # plt.clf() # plt.errorbar(np.maximum(1e-2, nm), np.maximum(1e-3, counts), yerr=dcounts, fmt='o', ms=5, alpha=0.5) # plt.xlabel('SDSS nanomaggies') # plt.ylabel('Tractor counts') # plt.title('Tractor forced photometry of SCUSS data') # plt.xscale('log') # plt.yscale('log') # ps.savefig() xx, dc = [], [] xxmags = [] for mag in [24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12]: tnm = 10.**((mag - 22.5) / -2.5) if (mag > 12): nmlo = tnm / np.sqrt(2.5) nmhi = tnm * np.sqrt(2.5) K = np.flatnonzero((counts > nmlo) * (counts < nmhi)) xx.append(tnm) xxmags.append(mag) dc.append(np.median(dcounts[K])) xx = np.array(xx) dc = np.array(dc) if False: plt.clf() plt.loglog(np.maximum(1e-2, nm[stars]), np.maximum(1e-2, counts[stars]), 'b.', ms=5, alpha=0.5) plt.loglog(np.maximum(1e-2, nm[gals]), np.maximum(1e-2, counts[gals]), 'g.', ms=5, alpha=0.5) plt.xlabel('SDSS nanomaggies') plt.ylabel('Tractor nanomaggies') plt.title('Tractor forced photometry of SCUSS data') ax = plt.axis() plt.axhline(1e-2, color='r', alpha=0.5) plt.axvline(1e-2, color='r', alpha=0.5) mx = max(ax[1], ax[3]) plt.plot([1e-2, mx], [1e-2, mx], 'b-', alpha=0.25, lw=2) for tnm, mag in zip(xx, xxmags): plt.axvline(tnm, color='k', alpha=0.25) plt.text(tnm * 1.05, 3e4, '%i mag' % mag, ha='left', rotation=90, color='0.5') plt.errorbar(xx, xx, yerr=dc, fmt=None, ecolor='r', elinewidth=2, capsize=3) #, capthick=2) plt.plot([xx, xx], [xx - dc, xx + dc], 'r-') mm = np.arange(11, 27) nn = 10.**((mm - 22.5) / -2.5) plt.xticks(nn, ['%i' % i for i in mm]) plt.yticks(nn, ['%i' % i for i in mm]) plt.xlim(0.8e-2, ax[1]) plt.ylim(0.8e-2, ax[3]) ps.savefig() lo, hi = 11, 26 smag = np.clip(-2.5 * (np.log10(nm) - 9), lo, hi) tmag = np.clip(-2.5 * (np.log10(counts) - 9), lo, hi) dt = np.abs((-2.5 / np.log(10.)) * dc / xx) xxmag = -2.5 * (np.log10(xx) - 9) plt.clf() p1 = plt.plot(smag[stars], tmag[stars], 'b.', ms=5, alpha=0.5) p2 = plt.plot(smag[gals], tmag[gals], 'g.', ms=5, alpha=0.5) plt.xlabel('SDSS mag') plt.ylabel('Tractor mag') plt.title('Tractor forced photometry of SCUSS data') plt.plot([lo, hi], [lo, hi], 'b-', alpha=0.25, lw=2) plt.axis([hi, lo, hi, lo]) plt.legend((p1[0], p2[0]), ('Stars', 'Galaxies'), loc='lower right') plt.errorbar(xxmag, xxmag, dt, fmt=None, ecolor='r', elinewidth=2, capsize=3) plt.plot([xxmag, xxmag], [xxmag - dt, xxmag + dt], 'r-') ps.savefig() plt.clf() p1 = plt.plot(smag[stars], tmag[stars] - smag[stars], 'b.', ms=5, alpha=0.5) p2 = plt.plot(smag[gals], tmag[gals] - smag[gals], 'g.', ms=5, alpha=0.5) plt.xlabel('SDSS mag') plt.ylabel('Tractor mag - SDSS mag') plt.title('Tractor forced photometry of SCUSS data') plt.axhline(0, color='b', alpha=0.25, lw=2) plt.axis([hi, lo, -1, 1]) plt.legend((p1[0], p2[0]), ('Stars', 'Galaxies'), loc='lower right') plt.errorbar(xxmag, np.zeros_like(xxmag), dt, fmt=None, ecolor='r', elinewidth=2, capsize=3) plt.plot([xxmag, xxmag], [-dt, +dt], 'r-') ps.savefig() # lo,hi = -2,5 # plt.clf() # loghist(np.clip(np.log10(nm),lo,hi), np.clip(np.log10(counts), lo, hi), 200, # range=((lo-0.1,hi+0.1),(lo-0.1,hi+0.1))) # plt.xlabel('SDSS nanomaggies') # plt.ylabel('Tractor nanomaggies') # plt.title('Tractor forced photometry of SCUSS data') # ps.savefig() if True: # Cut to valid/bright ones I = np.flatnonzero((nm > 1e-2) * (counts > 1e-2)) J = np.flatnonzero((nm > 1) * (counts > 1e-2)) # Estimate zeropoint med = np.median(counts[J] / nm[J]) plt.clf() plt.loglog(nm[I], counts[I] / nm[I], 'b.', ms=5, alpha=0.25) plt.xlabel('SDSS nanomaggies') plt.ylabel('Tractor nanomaggies / SDSS nanomaggies') plt.title('Tractor forced photometry of SCUSS data') ax = plt.axis() #plt.axhline(med, color='k', alpha=0.5) plt.axhline(1, color='k', alpha=0.5) plt.axis(ax) plt.ylim(0.1, 10.) ps.savefig() C = fits_table( 'data/scuss-w1-images/photozCFHTLS-W1_270912.fits', columns=['alpha', 'delta', 'u', 'eu', 'g', 'stargal', 'ebv']) Cfull = C Tfull = T if False: # Johan's mag vs error plots #plt.clf() #ha = dict(range=((19,26),(-3,0)))#, doclf=False) ha = dict(range=((19, 26), (np.log10(5e-2), np.log10(0.3)))) #plt.subplot(2,2,1) loghist(Cfull.u, np.log10(Cfull.eu), 100, **ha) plt.xlabel('u (mag)') plt.ylabel('u error (mag)') plt.title('CFHT') yt = [0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.2, 0.3] plt.yticks(np.log10(yt), ['%g' % y for y in yt]) ps.savefig() #plt.subplot(2,2,2) su = -2.5 * (np.log10(Tfull.modelflux[:, 0]) - 9) se = np.abs( (-2.5 / np.log(10.)) * (1. / np.sqrt(Tfull.modelflux_ivar[:, 0])) / Tfull.modelflux[:, 0]) loghist(su, np.log10(se), 100, **ha) plt.xlabel('u (mag)') plt.ylabel('u error (mag)') plt.yticks(np.log10(yt), ['%g' % y for y in yt]) plt.title('SDSS') ps.savefig() c = Tfull.tractor_u_nanomaggies d = 1. / np.sqrt(Tfull.tractor_u_nanomaggies_invvar) tu = -2.5 * (np.log10(c) - 9) te = np.abs((-2.5 / np.log(10.)) * d / c) #plt.subplot(2,2,3) loghist(tu, np.log10(te), 100, **ha) plt.xlabel('u (mag)') plt.ylabel('u error (mag)') plt.yticks(np.log10(yt), ['%g' % y for y in yt]) plt.title('SCUSS') ps.savefig() # (don't use T.cut(): const T) T = T[T.tractor_u_has_phot] print('C stargal:', np.unique(C.stargal)) I, J, d = match_radec(T.ra, T.dec, C.alpha, C.delta, 1. / 3600.) C = C[J] T = T[I] stars = (T.objc_type == 6) gals = (T.objc_type == 3) #stars = (C.stargal == 1) #gals = (C.stargal == 0) counts = T.tractor_u_nanomaggies dcounts = 1. / np.sqrt(T.tractor_u_nanomaggies_invvar) sdssu = -2.5 * (np.log10(T.modelflux[:, 0]) - 9) tmag = -2.5 * (np.log10(counts) - 9) dt = np.abs((-2.5 / np.log(10.)) * dcounts / counts) #cmag = C.u + 0.241 * (C.u - C.g) sdssu = -2.5 * (np.log10(T.psfflux[:, 0]) - 9) sdssg = -2.5 * (np.log10(T.psfflux[:, 1]) - 9) sdssugal = -2.5 * (np.log10(T.modelflux[:, 0]) - 9) #sdssu = -2.5*(np.log10(T.modelflux[:,0])-9) #sdssg = -2.5*(np.log10(T.modelflux[:,1])-9) #cmag = C.u #cmag = C.u + 0.241 * (sdssu - sdssg) #cmag += (4.705 * C.ebv) def _comp_plot(smag, cmag, tt, xname='SDSS', yname='CFHTLS'): plt.clf() lo, hi = 13, 26 # p1 = plt.plot(cmag[stars], smag[stars], 'b.', ms=5, alpha=0.5) # p2 = plt.plot(cmag[gals] , smag[gals ], 'g.', ms=5, alpha=0.5) # plt.xlabel('CFHTLS u mag') # plt.ylabel('SDSS u mag') p1 = plt.plot(smag[stars], cmag[stars] - smag[stars], 'b.', ms=5, alpha=0.5) p2 = plt.plot(smag[gals], cmag[gals] - smag[gals], 'g.', ms=5, alpha=0.5) plt.xlabel('%s u mag' % xname) plt.ylabel('%s u mag - %s u mag' % (yname, xname)) plt.title(tt) #plt.plot([lo,hi],[lo,hi], 'b-', alpha=0.25, lw=2) #plt.axis([hi,lo,hi,lo]) plt.axhline(0, color='b', alpha=0.25, lw=2) plt.axis([hi, lo, -2, 2]) plt.legend((p1[0], p2[0]), ('Stars', 'Galaxies'), loc='lower right') ps.savefig() smag = sdssu cmag = C.u eu = 4.705 * C.ebv ct = 0.241 * (sdssu - sdssg) sboth = np.zeros_like(smag) sboth[stars] = sdssu[stars] sboth[gals] = sdssugal[gals] _comp_plot(smag, cmag, 'CFHTLS vs SDSS -- raw (PSF)') _comp_plot(sdssugal, cmag, 'CFHTLS vs SDSS -- raw (model)') _comp_plot(sboth, cmag, 'CFHTLS vs SDSS -- raw') _comp_plot(sboth, cmag + eu, 'CFHTLS vs SDSS -- un-extincted') _comp_plot(sboth, cmag + ct, 'CFHTLS vs SDSS -- color term') #_comp_plot(sboth, cmag + ct + eu, 'CFHTLS vs SDSS -- un-extincted, color term') #_comp_plot(sboth, cmag - eu, 'CFHTLS vs SDSS -- -un-extincted') #_comp_plot(sboth, cmag + ct - eu, 'CFHTLS vs SDSS -- -un-extincted, color term') _comp_plot(cmag + ct, tmag, 'CFHTLS+ct vs Tractor(SCUSS)', xname='CFHTLS', yname='Tractor(SCUSS)') _comp_plot(cmag, tmag, 'CFHTLS vs Tractor(SCUSS)', xname='CFHTLS', yname='Tractor(SCUSS)') _comp_plot(sboth, tmag, 'SDSS vs Tractor(SCUSS)', xname='SDSS', yname='Tractored SCUSS') plt.clf() keep = ((cmag > 17) * (cmag < 22)) plt.plot((sdssu - sdssg)[keep * stars], (tmag - cmag)[keep * stars], 'b.', ms=5, alpha=0.5) plt.plot((sdssu - sdssg)[keep * gals], (tmag - cmag)[keep * gals], 'g.', ms=5, alpha=0.5) plt.xlabel('SDSS u-g') plt.ylabel('Tractor(SCUSS) - CFHTLS') plt.axis([-1, 4, -1, 1]) ps.savefig() # I = np.flatnonzero((sboth < 16) * ((cmag + ct - sboth) > 0.25)) # for r,d in zip(T.ra[I], T.dec[I]): # print 'RA,Dec', r,d # print 'http://skyservice.pha.jhu.edu/DR10/ImgCutout/getjpeg.aspx?ra=%.5f&dec=%.5f&width=100&height=100' % (r,d) # ??? # sdssu -= T.extinction[:,0] # sdssg -= T.extinction[:,1] # tmag -= T.extinction[:,0] xxmag = np.arange(13, 26) dx = [] dc = [] for xx in xxmag: ii = np.flatnonzero((tmag > xx - 0.5) * (tmag < xx + 0.5)) dx.append(np.median(dt[ii])) ii = np.flatnonzero((cmag > xx - 0.5) * (cmag < xx + 0.5)) dc.append(np.median(C.eu[ii])) dc = np.array(dc) dx = np.array(dx) plt.clf() lo, hi = 13, 26 p1 = plt.plot(cmag[stars], tmag[stars], 'b.', ms=5, alpha=0.5) p2 = plt.plot(cmag[gals], tmag[gals], 'g.', ms=5, alpha=0.5) plt.xlabel('CFHTLS mag') plt.ylabel('Tractor mag') plt.title('Tractor forced photometry of SCUSS data') plt.plot([lo, hi], [lo, hi], 'b-', alpha=0.25, lw=2) plt.axis([hi, lo, hi, lo]) plt.legend((p1[0], p2[0]), ('Stars', 'Galaxies'), loc='lower right') plt.errorbar(xxmag, xxmag, dx, fmt=None, ecolor='r', elinewidth=2, capsize=3) plt.plot([xxmag, xxmag], [xxmag - dx, xxmag + dx], 'r-') dd = 0.1 plt.errorbar(xxmag + dd, xxmag, dc, fmt=None, ecolor='m', elinewidth=2, capsize=3) plt.plot([xxmag + dd, xxmag + dd], [xxmag - dc, xxmag + dc], 'm-') ps.savefig() plt.clf() p1 = plt.plot(cmag[stars], tmag[stars] - cmag[stars], 'b.', ms=5, alpha=0.5) p2 = plt.plot(cmag[gals], tmag[gals] - cmag[gals], 'g.', ms=5, alpha=0.5) plt.xlabel('CFHTLS mag') plt.ylabel('Tractor mag - CFHTLS mag') plt.title('Tractor forced photometry of SCUSS data') plt.axhline(0, color='b', alpha=0.25, lw=2) plt.axis([hi, lo, -1, 1.5]) plt.legend((p1[0], p2[0]), ('Stars', 'Galaxies'), loc='lower right') plt.errorbar(xxmag, np.zeros_like(xxmag), dx, fmt=None, ecolor='r', elinewidth=2, capsize=3) plt.plot([xxmag, xxmag], [-dx, +dx], 'r-') plt.errorbar(xxmag + dd, np.zeros_like(xxmag), dc, fmt=None, ecolor='m', elinewidth=2, capsize=3) plt.plot([xxmag + dd, xxmag + dd], [-dc, +dc], 'm-') ps.savefig()
import numpy as np import pylab as pl from sklearn import svm # we create 20 points np.random.seed(0) X = np.r_[np.random.randn(10, 2) + [1, 1], np.random.randn(10, 2)] Y = [1] * 10 + [-1] * 10 sample_weight = 100 * np.abs(np.random.randn(20)) # and assign a bigger weight to the last 10 samples sample_weight[:10] *= 10 # # fit the model clf = svm.SVC() clf.fit(X, Y, sample_weight=sample_weight) # plot the decision function xx, yy = np.meshgrid(np.linspace(-4, 5, 500), np.linspace(-4, 5, 500)) Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # plot the line, the points, and the nearest vectors to the plane pl.set_cmap(pl.cm.bone) pl.contourf(xx, yy, Z, alpha=0.75) pl.scatter(X[:, 0], X[:, 1], c=Y, s=sample_weight, alpha=0.9) pl.axis('off') pl.show()
def plot_data(evalpts, datapts, style='k.'): pylab.plot(evalpts, datapts, '%s' % style) pylab.axis([0, 5, 0, 50], 'k-') pylab.draw() return
def plot_commerror(name1, logfile1, name2, logfile2): file1 = open(str(logfile1), 'r').readlines() data1 = [float(line.split()[1]) for line in file1] iso1 = data1[0::3] aniso1 = data1[1::3] ml1 = data1[2::3] file2 = open(str(logfile2), 'r').readlines() data2 = [float(line.split()[1]) for line in file2] iso2 = data2[0::3] aniso2 = data2[1::3] ml2 = data2[2::3] # x axis x = [8, 16, 32, 64, 128] xlabels = ['A', 'B', 'C', 'D', 'E'] orderone = [1, .5, .25, .125, .0625] ordertwo = [i**2 for i in orderone] plot1 = pylab.figure(figsize=(6, 6.5)) size = 15 ax = pylab.subplot(111) ax.plot(x, iso1, linestyle='solid', color='red', lw=2) ax.plot(x, aniso1, linestyle='dashed', color='red', lw=2) ax.plot(x, ml1, linestyle='dashdot', color='red', lw=2) ax.plot(x, iso2, linestyle='solid', color='blue', lw=2) ax.plot(x, aniso2, linestyle='dashed', color='blue', lw=2) ax.plot(x, ml2, linestyle='dashdot', color='blue', lw=2) #ax.plot(x,orderone, linestyle='solid',color='black',lw=2) #ax.plot(x,ordertwo, linestyle='dashed',color='black') #pylab.legend((str(name1)+'_iso',str(name1)+'_aniso',str(name2)+'_iso',str(name2)+'_aniso','order 1'),loc="best") pylab.legend( (str(name1) + '_iso', str(name1) + '_aniso', str(name1) + '_ml', str(name2) + '_iso', str(name2) + '_aniso', str(name2) + '_ml'), loc="best") leg = pylab.gca().get_legend() ltext = leg.get_texts() pylab.setp(ltext, fontsize=size, color='black') frame = leg.get_frame() frame.set_fill(False) frame.set_visible(False) #ax.grid("True") for tick in ax.xaxis.get_major_ticks(): tick.label1.set_fontsize(size) for tick in ax.yaxis.get_major_ticks(): tick.label1.set_fontsize(size) # set axes to logarithmic pylab.gca().set_xscale('log', basex=2) pylab.gca().set_yscale('log', basex=2) pylab.axis([8, 128, 1.e-4, 2]) ax.set_xticks(x) ax.set_xticklabels(xlabels) #pylab.axis([1,5,1.e-6,1.]) ax.set_xlabel('Mesh resolution', ha="center", fontsize=size) ax.set_ylabel('commutation error', fontsize=size) pylab.savefig('commerrorplot.eps') pylab.savefig('commerrorplot.pdf') return
##################PLOT THE BAND STRUCTURE######################### calc.get_potential_energy() # Create an array of eigenvalue arrays of kpoints e_kn = np.array([calc.get_eigenvalues(k) for k in range(len(kpts))]) import pylab as plt # Subtract the fermi level from the eigenvalues e_kn -= efermi emin = e_kn.min() - 1.0 emax = e_kn[:, nbands].max() - 1.0 plt.figure(figsize=(5, 6)) for i in range(nbands): plt.scatter(x, e_kn[:, i]) for j in X: plt.plot([j, j], [emin, emax], 'k-') plt.plot([0, X[-1]], [0, 0], 'k-') # Label the kpoint path #plt.xticks(X, ['$%s$' % n for n in ['L', r'\Gamma', 'X', 'W', 'K', r'\Gamma']]) plt.xticks(X, ['$%s$' % n for n in ['L', r'\Gamma', 'X', 'U', r'\Gamma']]) # x-axis range is range of x-coordinates of special points, y-axis is eigenvalues plt.axis(xmin=0, xmax=X[-1], ymin=emin, ymax=emax) plt.xlabel('k-vector') plt.ylabel('E - E$_F$ (eV)') plt.title('Band Structure of Diamond') plt.savefig('Diamond_bands_2.png') plt.show()