def plot_bases(self, autoscale=True, stampsize=None): import pylab as plt N = len(self.psfbases) cols = int(np.ceil(np.sqrt(N))) rows = int(np.ceil(N / float(cols))) plt.clf() plt.subplots_adjust(hspace=0, wspace=0) cut = 0 if stampsize is not None: H, W = self.shape assert H == W cut = max(0, (H - stampsize) / 2) ima = dict(interpolation="nearest", origin="lower") if autoscale: mx = self.psfbases.max() ima.update(vmin=-mx, vmax=mx) nil, xpows, ypows = self.polynomials(0.0, 0.0, powers=True) for i, (xp, yp, b) in enumerate(zip(xpows, ypows, self.psfbases)): plt.subplot(rows, cols, i + 1) if cut > 0: b = b[cut:-cut, cut:-cut] if autoscale: plt.imshow(b, **ima) else: mx = np.abs(b).max() plt.imshow(b, vmin=-mx, vmax=mx, **ima) plt.xticks([]) plt.yticks([]) plt.title("x^%i y^%i" % (xp, yp)) plt.suptitle("PsfEx eigen-bases")
def plot_Barycenter(dataset_name, feat, unfeat, repo): if dataset_name==MNIST: _, _, test=get_data(dataset_name, repo, labels=True) xtest1,_,_, labels,_=test else: _, _, test=get_data(dataset_name, repo, labels=False) xtest1,_,_ =test labels=np.zeros((len(xtest1),)) # get labels def bary_wdl2(index): return _bary_wdl2(index, xtest1, feat, unfeat) n=xtest1.shape[-1] num_class = (int)(max(labels)+1) barys=[bary_wdl2(np.where(labels==i)) for i in range(num_class)] pl.figure(1, (num_class, 1)) for i in range(num_class): pl.subplot(1,10,1+i) pl.imshow(barys[i][0,0,:,:],cmap='Blues',interpolation='nearest') pl.xticks(()) pl.yticks(()) if i==0: pl.ylabel('DWE Bary.') if num_class >1: pl.title('{}'.format(i)) pl.tight_layout(pad=0,h_pad=-2,w_pad=-2) pl.savefig("imgs/{}_dwe_bary.pdf".format(dataset_name))
def fixup_gauge(current_data): import pylab size = 34 pylab.title("Pressure at Gauge 0", fontsize=size) pylab.xticks([1.0, 1.5, 2.0, 2.5, 3.0], fontsize=size) pylab.yticks([-0.3, -0.1, 0.1, 0.3, 0.5], fontsize=size)
def plot_predictions(self): data = self.get_next_batch(train=False)[2] # get a test batch num_classes = self.test_data_provider.get_num_classes() NUM_ROWS = 2 NUM_COLS = 4 NUM_IMGS = NUM_ROWS * NUM_COLS NUM_TOP_CLASSES = min(num_classes, 4) # show this many top labels label_names = self.test_data_provider.batch_meta['label_names'] if self.only_errors: preds = n.zeros((data[0].shape[1], num_classes), dtype=n.single) else: preds = n.zeros((NUM_IMGS, num_classes), dtype=n.single) rand_idx = nr.randint(0, data[0].shape[1], NUM_IMGS) print rand_idx data[0] = n.require(data[0][:,rand_idx], requirements='C') data[1] = n.require(data[1][:,rand_idx], requirements='C') data += [preds] temp = data[0] print data print temp.ndim,temp.shape,temp.size # Run the model self.libmodel.startFeatureWriter(data, self.sotmax_idx) self.finish_batch() fig = pl.figure(3) fig.text(.4, .95, '%s test case predictions' % ('Mistaken' if self.only_errors else 'Random')) if self.only_errors: err_idx = nr.permutation(n.where(preds.argmax(axis=1) != data[1][0,:])[0])[:NUM_IMGS] # what the net got wrong data[0], data[1], preds = data[0][:,err_idx], data[1][:,err_idx], preds[err_idx,:] data[0] = self.test_data_provider.get_plottable_data(data[0]) for r in xrange(NUM_ROWS): for c in xrange(NUM_COLS): img_idx = r * NUM_COLS + c if data[0].shape[0] <= img_idx: break pl.subplot(NUM_ROWS*2, NUM_COLS, r * 2 * NUM_COLS + c + 1) pl.xticks([]) pl.yticks([]) try: img = data[0][img_idx,:,:,:] except IndexError: # maybe greyscale? img = data[0][img_idx,:,:] pl.imshow(img, interpolation='nearest') true_label = int(data[1][0,img_idx]) img_labels = sorted(zip(preds[img_idx,:], label_names), key=lambda x: x[0])[-NUM_TOP_CLASSES:] pl.subplot(NUM_ROWS*2, NUM_COLS, (r * 2 + 1) * NUM_COLS + c + 1, aspect='equal') ylocs = n.array(range(NUM_TOP_CLASSES)) + 0.5 height = 0.5 width = max(ylocs) pl.barh(ylocs, [l[0]*width for l in img_labels], height=height, \ color=['r' if l[1] == label_names[true_label] else 'b' for l in img_labels]) pl.title(label_names[true_label]) pl.yticks(ylocs + height/2, [l[1] for l in img_labels]) pl.xticks([width/2.0, width], ['50%', '']) pl.ylim(0, ylocs[-1] + height*2)
def getOptCandGamma(cv_train, cv_label): print "Finding optimal C and gamma for SVM with RBF Kernel" C_range = 10.0 ** np.arange(-2, 9) gamma_range = 10.0 ** np.arange(-5, 4) param_grid = dict(gamma=gamma_range, C=C_range) cv = StratifiedKFold(y=cv_label, n_folds=40) # Use the svm.SVC() as the cost function to evaluate parameter choices # NOTE: Perhaps we should run computations in parallel if needed. Does it # do that already within the class? grid = GridSearchCV(svm.SVC(), param_grid=param_grid, cv=cv) grid.fit(cv_train, cv_label) score_dict = grid.grid_scores_ scores = [x[1] for x in score_dict] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) pl.figure(figsize=(8,6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show() print "The best classifier is: ", grid.best_estimator_
def plotMatrix(matrix, color_scheme, row_headers, col_headers, vmin, vmax, options): pylab.imshow(matrix, cmap=color_scheme, origin='lower', vmax=vmax, vmin=vmin, interpolation='nearest') # offset=0: x=center,y=center # offset=0.5: y=top/x=right offset = 0.0 if options.xticks: pylab.xticks([offset + x for x in range(len(options.xticks))], options.xticks, rotation="vertical", fontsize="8") else: if col_headers and len(col_headers) < 100: pylab.xticks([offset + x for x in range(len(col_headers))], col_headers, rotation="vertical", fontsize="8") if options.yticks: pylab.yticks([offset + y for y in range(len(options.yticks))], options.yticks, fontsize="8") else: if row_headers and len(row_headers) < 100: pylab.yticks([offset + y for y in range(len(row_headers))], row_headers, fontsize="8")
def plot(self,title='',include_baseline=False,equal_aspect=True): """ Method that generates a plot of the ROC curve Parameters: title: Title of the chart include_baseline: Add the baseline plot line if it's True equal_aspect: Aspects to be equal for all plot """ pylab.clf() pylab.plot([x[0] for x in self.derived_points], [y[1] for y in self.derived_points], self.linestyle) if include_baseline: pylab.plot([0.0,1.0], [0.0,1.0],'k-.') pylab.ylim((0,1)) pylab.xlim((0,1)) pylab.xticks(pylab.arange(0,1.1,.1)) pylab.yticks(pylab.arange(0,1.1,.1)) pylab.grid(True) if equal_aspect: cax = pylab.gca() cax.set_aspect('equal') pylab.xlabel('1 - Specificity') pylab.ylabel('Sensitivity') pylab.title(title) pylab.show()
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_mtx(mtx=None, title=None, newfig=False, cbar=True, **kwargs): """ :: static method for plotting a matrix as a time-frequency distribution (audio features) """ if mtx is None or type(mtx) != np.ndarray: raise ValueError('First argument, mtx, must be a array') if newfig: P.figure() dbscale = kwargs.pop('dbscale', False) bels = kwargs.pop('bels',False) norm = kwargs.pop('norm',False) normalize = kwargs.pop('normalize',False) origin=kwargs.pop('origin','lower') aspect=kwargs.pop('aspect','auto') interpolation=kwargs.pop('interpolation','nearest') cmap=kwargs.pop('cmap',P.cm.gray_r) clip=-100. X = scale_mtx(mtx, normalize=normalize, dbscale=dbscale, norm=norm, bels=bels) i_min, i_max = np.where(X.mean(1))[0][[0,-1]] X = X[i_min:i_max+1].copy() if dbscale or bels: if bels: clip/=10. P.imshow(P.clip(X,clip,0),origin=origin, aspect=aspect, interpolation=interpolation, cmap=cmap, **kwargs) else: P.imshow(X,origin=origin, aspect=aspect, interpolation=interpolation, cmap=cmap, **kwargs) if title: P.title(title,fontsize=16) if cbar: P.colorbar() P.yticks(np.arange(0,i_max+1-i_min,3),pc_labels[i_min:i_max+1:3],fontsize=14) P.xlabel('Tactus', fontsize=14) P.ylabel('MIDI Pitch', fontsize=14) P.grid()
def plot_multiple_roc(rocList,title='',labels=None, include_baseline=False, equal_aspect=True): """ Plots multiple ROC curves on the same chart. Parameters: rocList: the list of ROCData objects title: The tile of the chart labels: The labels of each ROC curve include_baseline: if it's True include the random baseline equal_aspect: keep equal aspect for all roc curves """ pylab.clf() pylab.ylim((0,1)) pylab.xlim((0,1)) pylab.xticks(pylab.arange(0,1.1,.1)) pylab.yticks(pylab.arange(0,1.1,.1)) pylab.grid(True) if equal_aspect: cax = pylab.gca() cax.set_aspect('equal') pylab.xlabel("1 - Specificity") pylab.ylabel("Sensitivity") pylab.title(title) if not labels: labels = [ '' for x in rocList] _remove_duplicate_styles(rocList) for ix, r in enumerate(rocList): pylab.plot([x[0] for x in r.derived_points], [y[1] for y in r.derived_points], r.linestyle, linewidth=1, label=labels[ix]) if include_baseline: pylab.plot([0.0,1.0], [0.0, 1.0], 'k-', label= 'random') if labels: pylab.legend(loc='lower right') pylab.show()
def gui_repr(self): """Generate a GUI to represent the sentence alignments """ if __pylab_loaded__: fig_width = max(len(self.text_e), len(self.text_f)) + 1 fig_height = 3 pylab.figure(figsize=(fig_width*0.8, fig_height*0.8), facecolor='w') pylab.box(on=False) pylab.subplots_adjust(left=0, right=1, bottom=0, top=1) pylab.xlim(-1, fig_width - 1) pylab.ylim(0, fig_height) pylab.xticks([]) pylab.yticks([]) e = [0 for _ in xrange(len(self.text_e))] f = [0 for _ in xrange(len(self.text_f))] for (i, j) in self.align: e[i] = 1 f[j] = 1 # draw the middle line pylab.arrow(i, 2, j - i, -1, color='r') for i in xrange(len(e)): # draw e side line pylab.text(i, 2.5, self.text_e[i], ha = 'center', va = 'center', rotation=30) if e[i] == 1: pylab.arrow(i, 2.5, 0, -0.5, color='r', alpha=0.3, lw=2) for i in xrange(len(f)): # draw f side line pylab.text(i, 0.5, self.text_f[i], ha = 'center', va = 'center', rotation=30) if f[i] == 1: pylab.arrow(i, 0.5, 0, 0.5, color='r', alpha=0.3, lw=2) pylab.draw()
def animation_compare(filename1, filename2, prefix, tend): num = 0 for t in timestep(0,tend): t1,s1 = FieldInitializer.LoadState(filename1, t) t2,s2 = FieldInitializer.LoadState(filename2, t) rot1 = s1.CalculateRotationRodrigues()['z'] rot2 = s2.CalculateRotationRodrigues()['z'] pylab.figure(figsize=(5.12*2,6.12)) rho = s2.CalculateRhoFourier().modulus() rot = s2.CalculateRotationRodrigues()['z'] pylab.subplot(121) #pylab.imshow(rho*(rho>0.5), alpha=1, cmap=pylab.cm.bone_r) pylab.imshow(rot1) pylab.xticks([]) pylab.yticks([]) pylab.xlabel(r'$d_0$', fontsize=25) pylab.subplot(122) pylab.imshow(rot2) pylab.xticks([]) pylab.yticks([]) pylab.subplots_adjust(0.,0.,1.,1.,0.01,0.05) pylab.xlabel(r'$d_0/2$', fontsize=25) pylab.suptitle("Nabarro-Herring", fontsize=25) pylab.savefig("%s%04i.png" %(prefix, num)) pylab.close('all') num = num + 1
def default_frame31shareX(self, data_label, x_tpl, y1_tpl, y2_tpl, y3_tpl, pltstyle_dict): x, labelx = x_tpl y1, label1 = y1_tpl y2, label2 = y2_tpl y3, label3 = y3_tpl pylab.subplots_adjust(hspace=0.001) self.ax1 = pylab.subplot(311) self.ax1.plot(x, y1, label=data_label, **pltstyle_dict) pylab.ylabel(label1, fontsize=20, horizontalalignment='left') pylab.yticks(fontsize=13) #self.ax1.yaxis.set_label_coords(-.12, 0.5) self.ax1.yaxis.set_label_coords(-.10, 0.25) pylab.ylim(-30, -10) self.ax1.legend() self.ax2 = pylab.subplot(312, sharex=self.ax1) self.ax2.plot(x, y2, **pltstyle_dict) self.ax2.yaxis.tick_right() pylab.ylim(0, 3) self.ax2.yaxis.set_label_coords(1.12, 0.5) pylab.ylabel(label2, fontsize=20) self.ax3 = pylab.subplot(313, sharex=self.ax1) self.ax3.plot(x, y3, **pltstyle_dict) xticklabels = self.ax1.get_xticklabels()+self.ax2.get_xticklabels() pylab.setp(xticklabels, visible=False) pylab.ylabel(label3, fontsize=20) pylab.xlabel(labelx, fontsize=20) pylab.xticks(fontsize=13)
def default_frame21(self, data_label, x_tpl, y1_tpl, y2_tpl, pltstyle_dict): x, labelx = x_tpl y1, label1 = y1_tpl y2, label2 = y2_tpl self.ax1 = pylab.subplot(211) self.ax1.plot(x, y1, label=data_label, **pltstyle_dict) pylab.ylabel(label1, fontsize=20, horizontalalignment='left') pylab.yticks(fontsize=13) self.ax1.yaxis.set_label_coords(-.12, 0.5) self.ax1.legend() self.ax2 = pylab.subplot(212) self.ax2.plot(x, y2, **pltstyle_dict) #self.ax2.yaxis.tick_right() #pylab.ylim(0,3) #self.ax2.yaxis.set_label_coords(1.12, 0.5) pylab.ylabel(label2, fontsize=20) #xticklabels = self.ax1.get_xticklabels()+self.ax2.get_xticklabels() #pylab.setp(xticklabels, visible=False) #pylab.ylabel(label3, fontsize = 20) pylab.xlabel(labelx, fontsize=20) pylab.xticks(fontsize=13)
def decorate(mean): pl.legend(loc='upper center', bbox_to_anchor=(.5,-.3)) xmin, xmax, ymin, ymax = pl.axis() pl.vlines([mean], -ymax, ymax*10, linestyle='dashed', zorder=20) pl.xticks([0, .5, 1]) pl.yticks([]) pl.axis([-.01, 1.01, -ymax*.05, ymax*1.01])
def plotGeometry(self,SNP,PCAD,title): fig=plt.figure(figsize=self.figsize, dpi=self.dpi);plt.ioff() SNP.loc['Reference0']=np.zeros(SNP.shape[1],dtype=int) plt.subplot(1,2,1) D=pd.DataFrame(None,index=SNP.index,columns=SNP.index) for i in SNP.index: for j in SNP.index: D.loc[i,j]= sum(np.logical_xor(SNP.loc[i],SNP.loc[j])) D=D.astype(float) im=plt.imshow(D,interpolation='nearest',cmap='Reds') plt.gca().xaxis.tick_top() x=np.arange(D.index.shape[0]) plt.yticks(x,map(lambda x: x.replace('mdio','') ,D.index.values)) plt.xticks(x,map(lambda x: x.replace('mdio','') ,D.columns)) plt.colorbar(im) plt.gca().tick_params(axis='both', which='major', labelsize=10) plt.title('Pairwise Hamming Distance',y=1.03) plt.subplot(1,2,0) D=PCAD.astype(float) im=plt.imshow(D,interpolation='nearest',cmap='Reds') plt.gca().xaxis.tick_top() x=np.arange(D.index.shape[0]) plt.yticks(x,map(lambda x: x.replace('mdio','') ,D.index.values)) plt.xticks(x,map(lambda x: x.replace('mdio','') ,D.columns)) plt.colorbar(im) plt.gca().tick_params(axis='both', which='major', labelsize=10) plt.title('Euclidean Distance in PC3',y=1.03) plt.suptitle('Figure {}. {}'.format(self.fignumber, title),fontsize=self.titleSizeSup); self.pdf.savefig(fig);self.fignumber+=1
def plotCorr(self,pars=None,SHOW=True,SAVE=True): skipfits=True pb.clf() for irn in range(len(self.ranges)): for ist in range(len(self.params)/3): pb.errorbar(np.arange(len(self.Pcorr[irn,ist,:]))/self.Fs,self.Pcorr[irn,ist,:],yerr=self.PcorrERR[irn,ist,:],color=colours[ist]) pb.ylim([0,1]) pb.grid(True) pb.xlabel('Time (s)',fontsize=20) pb.ylabel('Probabilities',fontsize=20) pb.xticks(fontsize=16) pb.yticks(fontsize=16) if SAVE: fn=os.path.basename(self.filename) pb.savefig(self.figdir+'corr_'+'range_'+str(irn)+fn[:-4]+'.eps') f=open(self.datdir+fn[:-4]+'.dat','a') f.seek(0,2) f.write('#corr_range_'+str(irn)+'\n') #f.write('BinCentre(pA)\tBinMin(pA)\tBinMax(pA)\tCounts\n') #for i in range(len(self.n)): # f.write(str(self.x[i])+'\t'+str(self.bins[i])+'\t'+str(self.bins[i+1])+'\t'+str(self.n[i])+'\n') f.close() if SHOW:pb.show() pb.clf() return
def command(args): from pylab import bar, yticks, subplots_adjust, show from numpy import arange import sr.tools.bom.bom as bom import sr.tools.bom.parts_db as parts_db db = parts_db.get_db() m = bom.MultiBoardBom(db) m.load_boards_args(args.arg) m.prime_cache() prices = [] for srcode, pg in m.items(): if srcode == "sr-nothing": continue prices.append((srcode, pg.get_price())) prices.sort(key=lambda x: x[1]) bar(0, 0.8, bottom=range(0, len(prices)), width=[x[1] for x in prices], orientation='horizontal') yticks(arange(0, len(prices)) + 0.4, [x[0] for x in prices]) subplots_adjust(left=0.35) show()
def rmsdSpreadSubplot(multiplier=1.0, layout=(-1, -1)): rmsd_data = dict( (e, rad_data[e]['innov'][quant]) for e in rad_data.iterkeys() ) spread_data = dict( (e, rad_data[e]['spread'][quant]) for e in rad_data.iterkeys() ) times = temp.getTimes() n_t = len(times) for exp, exp_name in exp_names.iteritems(): pylab.plot(sawtooth(times, times)[:(n_t + 1)], rmsd_data[exp][:(n_t + 1)], color=colors[exp], linestyle='-') pylab.plot(times[(n_t / 2):], rmsd_data[exp][n_t::2], color=colors[exp], linestyle='-') for exp, exp_name in exp_names.iteritems(): pylab.plot(sawtooth(times, times)[:(n_t + 1)], spread_data[exp][:(n_t + 1)], color=colors[exp], linestyle='--') pylab.plot(times[(n_t / 2):], spread_data[exp][n_t::2], color=colors[exp], linestyle='--') ylim = pylab.ylim() pylab.plot(times, -1 * np.ones((len(times),)), color='#999999', linestyle='-', label="RMS Innovation") pylab.plot(times, -1 * np.ones((len(times),)), color='#999999', linestyle='--', label="Spread") pylab.axhline(y=7, color='k', linestyle=':') pylab.axvline(x=14400, color='k', linestyle=':') pylab.ylabel("RMS Innovation/Spread (dBZ)", size='large') pylab.xlim(times[0], times[-1]) pylab.ylim(ylim) pylab.legend(loc=4) pylab.xticks(times[::2], [ "" for t in times[::2] ]) pylab.yticks(size='x-large') return
def plot(self, filesuffix=('.png',)): pylab.figure() kPluses = 10**numpy.linspace(0, 3, 100) kMinuses = 10**numpy.linspace(6, 9, 100) figureOfMerits = numpy.zeros((len(kPluses), len(kMinuses), 4), 'd') for i, kPlus in enumerate(kPluses): for j, kMinus in enumerate(kMinuses): figureOfMerits[i, j, :] = self.figureOfMerit(self.generateData({'kPlus' : kPlus, 'kMinus' : kMinus})) data = self.generateData({'kPlus' : kPluses[0], 'kMinus' : kMinuses[0]}) self._contourf(kMinuses, kPluses, figureOfMerits, data) pylab.xticks((10**6, 10**7, 10**8, 10**9), fontsize=self.fontsize) pylab.yticks((10**0, 10**1, 10**2, 10**3), fontsize=self.fontsize) pylab.xlabel(r'$k^-$ $\left(1\per\metre\right)$', fontsize=self.fontsize) pylab.ylabel(r'$k^+$ $\left(\power{\metre}{3}\per\mole\cdot\second\right)$', fontsize=self.fontsize) pylab.text(2 * 10**6, 7 * 10**2, r'I', fontsize=self.fontsize) pylab.text(3 * 10**7, 7 * 10**2, r'II', fontsize=self.fontsize) pylab.text(6 * 10**8, 7 * 10**2, r'III', fontsize=self.fontsize) pylab.text(6 * 10**8, 7 * 10**1, r'IV', fontsize=self.fontsize) for fP, kPlus, paxeslabel in ((1.143,3.51e+00, False), (0.975, 9.33e+00, False), (0.916, 3.51e+01, False), (0.89, 9.33e+01, False), (0.87, 3.51e+02, True)): for fM, kMinus, maxeslabel in ((1.4, 2.48e+06, False), (1.07, 7.05e+6, False), (0.96, 2.48e+07, False), (0.91, 7.05e+7, False), (0.88, 2.48e+08, True)): xpos = (numpy.log10(kMinus) - 6.) / 3. * fM ypos = numpy.log10(kPlus) / 3. * fP self.makeBackGroundPlot({'kPlus' : kPlus, 'kMinus' : kMinus}, xpos, ypos, axeslabel=paxeslabel and maxeslabel) for fs in filesuffix: pylab.savefig('kPlusVkMinus' + fs) pylab.close('all')
def plotCoeff(X, y, obj, featureNames, whichReg): """ Plot Regression's Coeff """ clf = classifiers[whichReg] clf,_,_ = fitAlgo(clf, X,y, opt= True, param_dict = param_dist_dict[whichReg]) if whichReg == "LogisticRegression": coeff = np.absolute(clf.coef_[0]) else: coeff = np.absolute(clf.coef_) print coeff indices = np.argsort(coeff)[::-1] print indices print featureNames featureList = [] # num_features = len(featureNames) print("Feature ranking:") for f in range(num_features): featureList.append(featureNames[indices[f]]) print("%d. feature %s (%.2f)" % (f, featureNames[indices[f]], coeff[indices[f]])) fig = pl.figure(figsize=(8,6),dpi=150) pl.title("Feature importances",fontsize=30) # pl.bar(range(num_features), coeff[indices], # yerr = std_importance[indices], color=paired[0], align="center", # edgecolor=paired[0],ecolor=paired[1]) pl.bar(range(num_features), coeff[indices], color=paired[0], align="center", edgecolor=paired[0],ecolor=paired[1]) pl.xticks(range(num_features), featureList, size=15,rotation=90) pl.ylabel("Importance",size=30) pl.yticks(size=20) pl.xlim([-1, num_features]) # fix_axes() pl.tight_layout() save_path = 'plots/'+obj+'/'+whichReg+'_feature_importances.pdf' fig.savefig(save_path)
def RosenbrockTest(): nDim = 3 numOfParticles = 20 maxIteration = 200 minX = array([-5.0]*nDim) maxX = array([5.0]*nDim) maxV = 0.2*(maxX - minX) minV = -1.0*maxV numOfTrial = 20 gBest = array([0.0]*maxIteration) for i in xrange(numOfTrial): p1 = RPSO.PSOProblem(nDim, numOfParticles, maxIteration, minX, maxX, minV, maxV, RPSO.Rosenbrock) p1.run() gBest = gBest + p1.gBestArray[:maxIteration] gBest = gBest / numOfTrial pylab.title('$G_{best}$ over 20 trials') pylab.xlabel('The $N^{th}$ Iteratioin') pylab.ylabel('Average gBest over '+str(numOfTrial)+' runs (logscale)') pylab.grid(True) # pylab.yscale('log') ymin, ymax = -1.5, 2.5 ystep = 0.5 pylab.ylim(ymin, ymax) yticks = linspace(ymin, ymax, (ymax-ymin)/ystep+1) pylab.yticks(tuple(yticks),tuple(map(str,yticks))) pylab.plot(range(maxIteration), log10(gBest),'-', label='Global best') pylab.legend() pylab.show()
def task1(): ''' Task 1 Generate, transform and plot gaussian data ''' X = generate_data(100) X2 = scale_data(X) X3 = standardise_data(X) # Plot data # Your code here # Hint: Use the functions pl.scatter(x[0,:],[1,:],c='r'), pl.hold(True), # pl.legend, pl.title, pl.xlabel, pl.ylabel fig = pl.figure() ax1 = fig.add_subplot(111) print ax1.scatter(X[0], X[1], c='y', label='Raw data') ax1.scatter(X2[0], X2[1], c='r', label='Scaled data') ax1.scatter(X3[0], X3[1], c='b', label='Standardised data') pl.title('Simple transformations of Gaussian Data') pl.xlabel('x') pl.ylabel('y') pl.xticks(range(-6,10,2)) pl.yticks(range(-4,5,1)) # ax1.title('Simple transformations of Gaussian Data') ???? -> does not work ax1.legend(scatterpoints=1) pl.savefig('task_1_340528_341455.pdf')
def plot_importances(imp, clfName, obj): imp=np.vstack(imp) print imp mean_importance = np.mean(imp,axis=0) std_importance = np.std(imp,axis=0) indices = np.argsort(mean_importance)[::-1] print indices print featureNames featureList = [] # num_features = len(featureNames) print("Feature ranking:") for f in range(num_features): featureList.append(featureNames[indices[f]]) print("%d. feature %s (%.2f)" % (f, featureNames[indices[f]], mean_importance[indices[f]])) fig = pl.figure(figsize=(8,6),dpi=150) pl.title("Feature importances",fontsize=30) pl.bar(range(num_features), mean_importance[indices], yerr = std_importance[indices], color=paired[0], align="center", edgecolor=paired[0],ecolor=paired[1]) pl.xticks(range(num_features), featureList, size=15,rotation=90) pl.ylabel("Importance",size=30) pl.yticks(size=20) pl.xlim([-1, num_features]) # fix_axes() pl.tight_layout() save_path = 'plots/'+obj+'/'+clfName+'_feature_importances.pdf' fig.savefig(save_path)
def plot_file_color(base, thin=True, start=0, size=14, save=False): conf, track, pegs = load(base) fig = pl.figure(figsize=(size,size*conf['top']/conf['wall'])) track = track[start:] x = track[:,0]; y = track[:,1] t = np.linspace(0,1,x.shape[0]) points = np.array([x,y]).transpose().reshape(-1,1,2) segs = np.concatenate([points[:-1],points[1:]],axis=1) lc = LineCollection(segs, linewidths=0.25, cmap=pl.cm.coolwarm) lc.set_array(t) pl.gca().add_collection(lc) #pl.scatter(x, y, c=np.arange(len(x)),linestyle='-',cmap=pl.cm.coolwarm) #pl.plot(track[-1000000:,0], track[-1000000:,1], '-', linewidth=0.0125, alpha=0.8) for peg in pegs: pl.gca().add_artist(pl.Circle(peg, conf['radius'], color='k', alpha=0.3)) pl.xlim(0, conf['wall']) pl.ylim(0, conf['top']) pl.xticks([]) pl.yticks([]) pl.tight_layout() pl.show() if save: pl.savefig(base+".png", dpi=200)
def tracks_movie(base, skip=1, frames=500, size=10): """ A movie of each particle as a point """ conf, track, pegs = load(base) fig = pl.figure(figsize=(size,size*conf['top']/conf['wall'])) plot = None for t in xrange(1,max(frames, track.shape[1]/skip)): tmp = track[:,t*skip,:] if not ((tmp[:,0] > 0) & (tmp[:,1] > 0) & (tmp[:,0] < conf['wall']) & (tmp[:,1] < conf['top'])).any(): continue if plot is None: plot = pl.plot(tmp[:,0], tmp[:,1], 'k,', alpha=1.0, ms=0.1)[0] pl.xticks([]) pl.yticks([]) pl.xlim(0,conf['wall']) pl.ylim(0,conf['top']) pl.tight_layout() else: plot.set_xdata(tmp[:,0]) plot.set_ydata(tmp[:,1]) pl.draw() pl.savefig(base+'-movie-%05d.png' % (t-1))
def plot_question(fname, question_text, data): import pylab import numpy as np from matplotlib.font_manager import FontProperties from matplotlib.text import Text pylab.figure().clear() pylab.title(question_text) #pylab.xlabel("Verteilung") #pylab.subplot(101) if True or len(data) < 3: width = 0.95 pylab.bar(range(len(data)), [max(y, 0.01) for x, y in data], 0.95, color="g") pylab.xticks([i+0.5*width for i in range(len(data))], [x for x, y in data]) pylab.yticks([0, 10, 20, 30, 40, 50]) #ind = np.arange(len(data)) #pylab.bar(ind, [y for x, y in data], 0.95, color="g") #pylab.ylabel("#") #pylab.ylim(ymax=45) #pylab.ylabel("Antworten") #pylab.xticks(ind+0.5, histo.get_ticks()) #pylab.legend(loc=3, prop=FontProperties(size="smaller")) ##pylab.grid(True) else: pylab.pie([max(y, 0.1) for x, y in data], labels=[x for x, y in data], autopct="%.0f%%") pylab.savefig(fname, format="png", dpi=75)
def traceplot(traces, thin, burn): ''' Plot parameter estimates for different levels of the model into the same plots. Black lines are individual observers and red lines are mean estimates. ''' variables = ['Slope1', 'Slope2', 'Offset', 'Split'] for i, var in enumerate(variables): plt.subplot(2, 2, i + 1) vals = get_values(traces, var, thin, burn) dim = (vals.min() - vals.std(), vals.max() + vals.std()) x = plt.linspace(*dim, num=1000) for v in vals.T: a = gaussian_kde(v) y = a.evaluate(x) y = y / y.max() plt.plot(x, y, 'k', alpha=.5) try: vals = get_values(traces, 'Mean_' + var, thin, burn) a = gaussian_kde(vals) y = a.evaluate(x) y = y / y.max() plt.plot(x, y, 'r', alpha=.75) except KeyError: pass plt.ylim([0, 1.1]) plt.yticks([0]) sns.despine(offset=5, trim=True) plt.title(var)
def align_subplots(N,M,xlim=None, ylim=None): """make all of the subplots have the same limits, turn off unnecessary ticks""" #find sensible xlim,ylim if xlim is None: xlim = [np.inf,-np.inf] for i in range(N*M): pb.subplot(N,M,i+1) xlim[0] = min(xlim[0],pb.xlim()[0]) xlim[1] = max(xlim[1],pb.xlim()[1]) if ylim is None: ylim = [np.inf,-np.inf] for i in range(N*M): pb.subplot(N,M,i+1) ylim[0] = min(ylim[0],pb.ylim()[0]) ylim[1] = max(ylim[1],pb.ylim()[1]) for i in range(N*M): pb.subplot(N,M,i+1) pb.xlim(xlim) pb.ylim(ylim) if (i)%M: pb.yticks([]) else: removeRightTicks() if i<(M*(N-1)): pb.xticks([]) else: removeUpperTicks()
def plot_C_gamma_grid_search(grid, C_range, gamma_range, score): ''' Plots the scores computed on a grid. Arguments: grid - the grid search object created using GridSearchCV() C_range - the C parameter range gamma_range - the gamma parameter range score - the scoring function ''' # grid_scores_ contains parameter settings and scores # We extract just the scores scores = [x[1] for x in grid.grid_scores_] scores = np.array(scores).reshape(len(C_range), len(gamma_range)) # draw heatmap of accuracy as a function of gamma and C pl.figure(figsize=(8, 6)) pl.subplots_adjust(left=0.05, right=0.95, bottom=0.15, top=0.95) pl.imshow(scores, interpolation='nearest', cmap=pl.cm.spectral) pl.title("Grid search on C and gamma for best %s" % score) pl.xlabel('gamma') pl.ylabel('C') pl.colorbar() pl.xticks(np.arange(len(gamma_range)), gamma_range, rotation=45) pl.yticks(np.arange(len(C_range)), C_range) pl.show()
agglo = cluster.WardAgglomeration(connectivity=connectivity, n_clusters=32) agglo.fit(X) X_reduced = agglo.transform(X) X_restored = agglo.inverse_transform(X_reduced) images_restored = np.reshape(X_restored, images.shape) pl.figure(1, figsize=(4, 3.5)) pl.clf() pl.subplots_adjust(left=.01, right=.99, bottom=.01, top=.91) for i in range(4): pl.subplot(3, 4, i + 1) pl.imshow(images[i], cmap=pl.cm.gray, vmax=16, interpolation='nearest') pl.xticks(()) pl.yticks(()) if i == 1: pl.title('Original data') pl.subplot(3, 4, 4 + i + 1) pl.imshow(images_restored[i], cmap=pl.cm.gray, vmax=16, interpolation='nearest') if i == 1: pl.title('Agglomerated data') pl.xticks(()) pl.yticks(()) pl.subplot(3, 4, 10) pl.imshow(np.reshape(agglo.labels_, images[0].shape), interpolation='nearest',
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()
def Network(X_train, y_train, X_validate, y_validate, X_test, y_test, num_layers, num_nodes, batch_size, epochs, data, input_hidden_activation, output_activation, drop, derived_feat, optimizer, learning_rate): model = tf.keras.Sequential() start_time = time.time() input_initializer = tf.keras.initializers.RandomNormal(mean=0.0,stddev=0.1) hidden_initializer = tf.keras.initializers.RandomNormal(mean=0.0,stddev=0.05) output_initializer = tf.keras.initializers.RandomNormal(mean=0.0,stddev=0.001) model.add(tf.keras.layers.Dense(num_nodes, kernel_initializer=input_initializer, activation=input_hidden_activation, input_dim=X_train.shape[1])) for i in range(num_layers): model.add(tf.keras.layers.Dense(num_nodes, kernel_initializer=hidden_initializer, activation=input_hidden_activation)) if drop == 'True' and i == 0: model.add(tf.keras.layers.Dropout(0.5)) model.add(tf.keras.layers.Dense(y_train.shape[1], kernel_initializer=output_initializer, activation=output_activation)) earlystop = tf.keras.callbacks.EarlyStopping(monitor='val_loss',min_delta=0.00001,patience=10, verbose=1) sgd = tf.keras.optimizers.SGD(lr=learning_rate,momentum=0.95,decay=0.0000002) model.compile(optimizer=sgd, loss='binary_crossentropy') if optimizer == 'sgd' else model.compile(optimizer=optimizer, loss='binary_crossentropy') model_info = model.fit(X_train,y_train,epochs=epochs,batch_size=batch_size,validation_data=[X_validate,y_validate], callbacks=[earlystop]) ypred = model.predict(X_test, batch_size=batch_size) print('Layers: ', num_layers, 'Nodes: ', num_nodes, 'Batch size: ', batch_size) AUC = roc_auc_score(y_test,ypred) False_positive_rate, True_positive_rate, b = roc_curve(y_test,ypred) print('AUC: ', AUC) file = open('AUC_result_layers%d_nodes%d_%s_drop-%s_Feat%s_optimizer-%s_lr-%.4f.txt' % (num_layers,num_nodes,data,drop,derived_feat,optimizer,learning_rate),'w') file.write('AUC: %f \n' % AUC) file.write('Dataset: %s \n' % data) file.write('Nodes: %f \n' % num_nodes) file.write('Batch: %f \n' % batch_size) file.write('Layers: %f \n' % num_layers) file.write('Dropout: %s \n' % drop) file.write('Feature: %s \n' % derived_feat) file.write('Optimizer: %s \n' % optimizer) file.write('Learning rate: %s \n' % learning_rate) file.write('Total runtime: %f' % (time.time() - start_time)) file.close() plt.plot(True_positive_rate,1-False_positive_rate) plt.xlim([0.0,1.0]) plt.ylim([0.0,1.0]) plt.xlabel('Signal efficiency', fontsize=18) plt.ylabel('Background rejection', fontsize=18) plt.title('ROC Curve',fontsize=20) pylab.xticks(fontsize=14) pylab.yticks(fontsize=14) plt.savefig('ROC_dataset_%s_nodes%d_nlayers%d_drop-%s_Feat%s_optimizer-%s_lr-%.4f.png' % (data,num_nodes,num_layers,drop,derived_feat,optimizer,learning_rate))
imgRGB = cv2.cvtColor( img_C, cv2.COLOR_BGR2RGB ) # Passage BGR --> RGB via cv2.cvtColor (non utilisé par la suite) # 2.1.1 Affichage image via opencv --> affichage format BGR cv2.namedWindow("mon image BGR", cv2.WINDOW_NORMAL) # Pour dimensionner la fenetre d'affichage cv2.imshow("mon image BGR", img_C) cv2.namedWindow("mon image RGB", cv2.WINDOW_NORMAL) cv2.imshow("mon image RGB", img_C2) # Affichage image via plt --> affichage format RGB plt.figure() plt.subplot(121) plt.imshow(img_C) plt.xticks([]), plt.yticks([]) plt.title(" Mon image BGR") plt.subplot(122) plt.imshow(img_C2) plt.xticks([]), plt.yticks([]) plt.title(" Mon image RGB") plt.show() # 2.1.2 Affichage des trois canaux séparéments plt.figure() plt.subplot(131) plt.imshow(b, cmap='gray') plt.xticks([]), plt.yticks([]) plt.title(" Mon image B") plt.subplot(132) plt.imshow(g, cmap='gray')
def main(): # init - random generator set_state(rand_state) # init - tool joints = (0, 0, 0, 0, 0, 0) dh_params['tool'] = hom(0, 0, 0, [0.005, 0.005, 0.233]) tool = dh_params['tool'] tool[:3, :3] = rot_mat_rts(0, -20, 90).dot(tool[:3, :3]) e = lambda mag: mat([uniform(-1, 1) * mag for _ in range(9)]).reshape(3, 3) #-------------- prepare mesurements ----------------- old_time = None res = [] for iteration in xrange(100): # standard value is 100 print "iteration {} of 100".format(iteration + 1) old_time = time.time() geom = { 'wobjs': [], 'pen_oris': [], 'poses': [], 'flanges': [], } # 20x20 = 400 measurements xy = it.product(linspace(-0.3, 0.3, 20), linspace(-0.3, 0.3, 20)) for x, y in xy: w = ori_pen_to_pattern(uniform(-90, 90), uniform(0, 40), uniform(-90, 90)) pen = w[:3, :3] = w[:3, :3] + e(2e-3) wobj = hom(0, 180, -90, [x, y, 0]) wobj[:3, :3] = wobj[:3, :3] + e(1e-1) pose = wobj.dot(w.T) try: geom['flanges'].append(flange(pose)) geom['wobjs'].append(wobj) geom['poses'].append(pose) geom['pen_oris'].append(pen) except: pass ## print len(geom['poses']) rx, ry, rz = [], [], [] num_meas = range(4, 300) for k in num_meas: A = mat(geom['poses'])[:k, :3, :3] B = mat(geom['flanges'])[:k, :3, :3] r1, (u, s, v) = solve_ori(A, B) r1 = r1.T R = tool[:3, :3] xang = acos(R[:, 0].dot(r1[:, 0])) * 180 / pi yang = acos(R[:, 1].dot(r1[:, 1])) * 180 / pi zang = acos(R[:, 2].dot(r1[:, 2])) * 180 / pi rx.append(xang) ry.append(yang) rz.append(zang) res.append((rx, ry, rz)) ##print len(res) print "- time: {}\n".format(time.time() - old_time) #-------------- prepare results / plots ------------- figpath = r"C:\Users\***REMOVED***\Dropbox\exjobb\results\calibrationalg_res\penori" res = mat(res) rx = res[:, 0, :] ry = res[:, 1, :] rz = res[:, 2, :] _all = [rx, ry, rz] _titles = ["x-axis", "y-axis", "z-axis"] print matplotlib.get_backend() for o, t in zip(_all, _titles): clf() plot(num_meas, n.max(o, axis=0), label="max") plot(num_meas, n.mean(o, axis=0), label="mean") plot(num_meas, n.min(o, axis=0), label="min") legend(fontsize=LEGEND_SIZE) grid() xlim([num_meas[0], num_meas[-1]]) xticks(fontsize=TICK_SIZE) yticks(fontsize=TICK_SIZE) #xticks(arange(4,300,(300)/12)) xlabel("Number of measurements", fontsize=LABEL_SIZE) ylabel("Error [deg]", fontsize=LABEL_SIZE) title(t, fontsize=TITLE_SIZE) # QT backend manager = plt.get_current_fig_manager() manager.window.showMaximized() savefig(path.join(figpath, "{}.png".format(t.replace("-", "_"))), bbox_inches="tight")
# Changing plot limits: import pylab as plb plb.figure(figsize=(6, 3), dpi=100) d = plb.linspace(-plb.pi * 2, plb.pi * 2, 128, endpoint=True) d_sin = plb.sin(d) d_cos = plb.cos(d) #we now set the x,y limits for the 'sin' function plb.subplot(2, 1, 1) plb.plot(d, d_sin, color="blue", linewidth=1.2, linestyle="-") plb.xlim(d_sin.min() * 6.5, d_sin.max() * 6.5) plb.ylim(d_sin.min() * 1.2, d_sin.max() * 1.2) plb.xticks([-plb.pi * 2, -plb.pi, 0, plb.pi, plb.pi * 2]) plb.yticks([-1, 0, 1]) #below we set the x,y limits for the 'cos' function plb.subplot(2, 1, 2) plb.plot(d, d_cos, color="red", linewidth=1, linestyle="--") plb.xlim(d_cos.min() * 6.5, d_cos.max() * 6.5) plb.ylim(d_cos.min() * 1.2, d_cos.max() * 1.2) plb.show()
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 cornerplot(theory, theory2, show_cuts=False): plt.switch_backend("pdf") plt.style.use("y1a1") matplotlib.rcParams["ytick.minor.visible"] = False matplotlib.rcParams["xtick.minor.visible"] = False matplotlib.rcParams["ytick.minor.width"] = 0.1 matplotlib.rcParams["ytick.major.size"] = 2.5 matplotlib.rcParams["xtick.major.size"] = 2.5 matplotlib.rcParams["xtick.minor.size"] = 1.8 ni, nj = 4, 4 #ni,nj=np.genfromtxt("%s/shear_xi_plus/values.txt"%theory1[0]).T[2] ni = int(ni) nj = int(nj) rows, cols = ni + 1, nj + 2 count = 0 for i in range(ni): for j in range(nj): count += 1 if j > i: continue print(i, j) # (xp,xip,dxip),(xm,xim,dxim) = get_real_spectra(i,j,data,error=True) posp = positions[(i + 1, j + 1, "+")] ax = plt.subplot(rows, cols, posp) ax.annotate( "(%d, %d)" % (i + 1, j + 1), (3., 2), textcoords='data', fontsize=9, ) ax.yaxis.set_tick_params(which='minor', left='off', right='off') plt.ylim(-6, 4) plt.xscale("log") if (posp == 19) or (posp == 1) or (posp == 7) or (posp == 13): plt.yticks(visible=True) #plt.ylabel(r"$\Delta \xi_+(\theta)/\xi_+$", fontsize=12) #plt.xlabel(r"$\theta$ / arcmin", fontsize=12) plt.yticks([-6, -4, -2, 0, 2], fontsize=ytickfontsize) else: plt.yticks(visible=False) if (posp == 19): plt.ylabel( r"$ \Delta \xi_+/\xi_+ = \xi^{\rm TATT}_+ - \xi^{\rm NLA}_+/\xi^{\rm NLA}_+ $", fontsize=11) plt.xlabel(r"$\theta \;\; [ \mathrm{arcmin} ]$", fontsize=10) #plt.yticks([0,1,2,3],fontsize=ytickfontsize) else: pass if posp in [19, 14, 9, 4]: plt.xlabel(r"$\theta \;\; [ \mathrm{arcmin} ]$ ", fontsize=10) plt.xlim(2.2, 270) plt.xticks([10, 100], ["10", "100"], fontsize=9) #plt.yticks([-2,0,2,4,6,8],['-2', '0', '2', '4', '6', '8']) plt.axhline(0, color='k', ls=':') if show_cuts: xlower, xupper = lims['+'][(i + 1, j + 1)] plt.axvspan(1e-6, xlower, color='gray', alpha=0.2) plt.axvspan(xupper, 500, color='gray', alpha=0.2) linestyles = ['-', ':', '--', '-'] xta, xip_theory, xim_theory, xip_theory_gi, xim_theory_gi, xip_theory_ii, xim_theory_ii = get_theory_spectra( i, j, theory) xtb, xip_theory2, xim_theory2, xip_theory2_gi, xim_theory2_gi, xip_theory2_ii, xim_theory2_ii = get_theory_spectra( i, j, theory2) # import pdb ; pdb.set_trace() # xip_a_remapped = (interp_xip_a(np.log10(xip_a[0]))) # xip_b_remapped = (interp_xip_b(np.log10(xip_b[0]))) IA1 = xip_theory_gi + xip_theory_ii IA2 = xip_theory2_gi + xip_theory2_ii plt.plot(xta, (xip_theory - xip_theory2) / xip_theory2, color='darkmagenta', lw=1.5, label='GG+GI+II') plt.plot(xta, (IA1 - IA2) / IA2, color='pink', lw=1.5, ls='-', label='GI') #plt.plot(xip_a[0], (xip_b_remapped-xip_a_remapped)/xip_b_remapped, ls=linestyles[iline], color="darkmagenta") # plt.plot(xip_b[0], 1e5*(xip_b[1]-xip_b_remapped), ls=linestyles[iline], color="royalblue") posm = positions[(i + 1, j + 1, "-")] ax = plt.subplot(rows, cols, posm) ax.annotate( "(%d, %d)" % (i + 1, j + 1), (3, 2), textcoords='data', fontsize=9, ) ax.yaxis.set_tick_params(which='minor', left='off', right='off') # ax.xaxis.set_tick_params(which='minor', bottom='on', top='off') plt.ylim(-6, 4) ax.yaxis.set_ticks_position("right") ax.yaxis.set_label_position("right") if (posm == 30) or (posm == 12) or (posm == 18) or (posm == 24): plt.yticks(visible=True) #plt.ylabel(r"$\Delta \xi_+(\theta)/\xi_+$", fontsize=12) #plt.xlabel(r"$\theta$ / arcmin", fontsize=12) ax.yaxis.set_label_position("right") plt.yticks([-6, -4, -2, 0, 2], fontsize=ytickfontsize) else: plt.yticks(visible=False) if (posm == 30): plt.ylabel(r"$ \Delta \xi_-/ \xi_-$", fontsize=11) plt.yticks([-4, -2, 0, 2], fontsize=ytickfontsize) else: pass if posm in [30, 29, 28, 27]: plt.xlabel(r"$\theta \;\; [ \mathrm{arcmin} ]$ ", fontsize=10) plt.xscale("log") plt.xlim(2.2, 270) # if (posm==27): # plt.xticks([1,10,100],["1","10", "100"], fontsize=9) # else: plt.xticks([10, 100], ["10", "100"], fontsize=9) #ax.xaxis.grid(True, which='minor') # ax.xaxis.set_minor_locator(MultipleLocator(10)) plt.yticks([-4, -2, 0, 2], fontsize=ytickfontsize) plt.axhline(0, color='k', ls=':') if show_cuts: xlower, xupper = lims['-'][(i + 1, j + 1)] plt.axvspan(1e-6, xlower, color='gray', alpha=0.2) plt.axvspan(xupper, 500, color='gray', alpha=0.2) IA1 = xim_theory_gi + xim_theory_ii IA2 = xim_theory2_gi + xim_theory2_ii plt.plot(xta, (xim_theory - xim_theory2) / xim_theory2, color='darkmagenta', lw=1.5, label='GG+GI+II') plt.plot(xta, (IA1 - IA2) / IA2, color='pink', lw=1.5, ls='-', label='GI') #print((IA1-IA2)/IA2) #plt.plot(xim_a[0], 1e5*(xim_a[1]-xim_a_remapped), ls=linestyles[iline], color="red") #plt.plot(xim_b[0], 1e5*(xim_b[1]-xim_b_remapped), ls=linestyles[iline], color="royalblue") # plt.plot(xip_a[0], (xim_b_remapped-xim_a_remapped)/xim_b_remapped, ls=linestyles[iline], color="darkmagenta") # plt.legend([p1,p2,p3],title='title', bbox_to_anchor=(1.05, 1), loc='upper right', fontsize=12) # ax = plt.subplot(111) box = ax.get_position() ax.set_position([box.x0, box.y0, box.width * 0.65, box.height]) legend_x = 4.2 legend_y = 5.2 proxies = [ plt.Line2D([0, 2.5], [0, 0], linestyle=ls, linewidth=1.5, color=lc) for ls, lc in [('-', 'darkmagenta'), ('-', 'pink'), ( '--', 'pink'), (':', 'midnightblue')] ] plt.legend(proxies, ["GG+GI+II", "GI+II"], loc='upper right', bbox_to_anchor=(legend_x, legend_y), fontsize=9) plt.subplots_adjust(hspace=0, wspace=0, bottom=0.14, left=0.14, right=0.88) plt.savefig("plots/unblinded_datavector_xipm_diff.pdf") plt.savefig("plots/unblinded_datavector_xipm_diff.png")