我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用matplotlib.pylab.imshow()。
def plot_x_y_yhat(x, y, y_hat, xsz, ysz, binz=False): """Plot x, y and y_hat side by side.""" plt.close("all") f = plt.figure(figsize=(15, 10.8), dpi=300) gs = gridspec.GridSpec(1, 3) if binz: y_hat = (y_hat > 0.5) * 1. ims = [x, y, y_hat] tils = [ "x:" + str(xsz) + "x" + str(xsz), "y:" + str(ysz) + "x" + str(ysz), "yhat:" + str(ysz) + "x" + str(ysz)] for n, ti in zip([0, 1, 2], tils): f.add_subplot(gs[n]) if n == 0: plt.imshow(ims[n], cmap=cm.Greys_r) else: plt.imshow(ims[n], cmap=cm.Greys_r) plt.title(ti) return f
def plot_x_x_yhat(x, x_hat): """Plot x, y and y_hat side by side.""" plt.close("all") f = plt.figure() # figsize=(15, 10.8), dpi=300 gs = gridspec.GridSpec(1, 2) ims = [x, x_hat] tils = [ "xin:" + str(x.shape[0]) + "x" + str(x.shape[1]), "xout:" + str(x.shape[1]) + "x" + str(x_hat.shape[1])] for n, ti in zip([0, 1], tils): f.add_subplot(gs[n]) plt.imshow(ims[n], cmap=cm.Greys_r) plt.title(ti) ax = f.gca() ax.set_axis_off() return f
def show_mpl(self, im, enhance=True, clear_fig=True): if self._pylab is None: import pylab self._pylab = pylab if self._render_figure is None: self._render_figure = self._pylab.figure(1) if clear_fig: self._render_figure.clf() if enhance: nz = im[im > 0.0] nim = im / (nz.mean() + 6.0 * np.std(nz)) nim[nim > 1.0] = 1.0 nim[nim < 0.0] = 0.0 del nz else: nim = im ax = self._pylab.imshow(nim[:,:,:3]/nim[:,:,:3].max(), origin='upper') return ax
def plot_allsky_healpix(image, nside, fn, label = "", rotation = None, take_log = True, resolution=512, cmin=None, cmax=None): import matplotlib.figure import matplotlib.backends.backend_agg if rotation is None: rotation = np.eye(3).astype("float64") img, count = pixelize_healpix(nside, image, resolution, resolution, rotation) fig = matplotlib.figure.Figure((10, 5)) ax = fig.add_subplot(1,1,1,projection='aitoff') if take_log: func = np.log10 else: func = lambda a: a implot = ax.imshow(func(img), extent=(-np.pi,np.pi,-np.pi/2,np.pi/2), clip_on=False, aspect=0.5, vmin=cmin, vmax=cmax) cb = fig.colorbar(implot, orientation='horizontal') cb.set_label(label) ax.xaxis.set_ticks(()) ax.yaxis.set_ticks(()) canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(fig) canvas.print_figure(fn) return img, count
def plot_2d(dataset, nbins, data, extra=None): with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=2) rows, cols = nbins im = np.zeros(nbins) for i in xrange(rows): for j in xrange(cols): im[i,j] = ((data[:,0] == i) & (data[:,1] == j)).sum() plt.imshow(im, cmap='gray_r', interpolation='none') if extra is not None: dataset += extra plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def plot_2d(dataset, nbins, data=None, extra=None): if data is None: data = np.loadtxt('experiments/uci/data/splits/{0}_all.csv'.format(dataset), skiprows=1, delimiter=',')[:,-2:] with sns.axes_style('white'): plt.rc('font', weight='bold') plt.rc('grid', lw=2) plt.rc('lines', lw=2) rows, cols = nbins im = np.zeros(nbins) for i in xrange(rows): for j in xrange(cols): im[i,j] = ((data[:,0] == i) & (data[:,1] == j)).sum() plt.imshow(im, cmap='gray_r', interpolation='none') if extra is not None: dataset += extra plt.savefig('plots/marginals-{0}.pdf'.format(dataset.replace('_','-')), bbox_inches='tight') plt.clf() plt.close()
def show_samples(y, ndim, nb=10, cmap=''): if ndim == 4: for i in range(nb**2): plt.subplot(nb, nb, i+1) plt.imshow(y[i], cmap=cmap, interpolation='none') plt.axis('off') else: x = y[0] y = y[1] plt.figure(0) for i in range(10): plt.subplot(2, 5, i+1) plt.imshow(x[i], cmap=cmap, interpolation='none') plt.axis('off') plt.figure(1) for i in range(10): plt.subplot(2, 5, i+1) plt.imshow(y[i], cmap=cmap, interpolation='none') plt.axis('off') plt.show()
def visualize_input(model): sess = tf.Session() sess.run(tf.global_variables_initializer()) tf.train.start_queue_runners(sess=sess) batch_img, batch_cap = sess.run([model.images, model.input_seqs]) # show first img batch_img = batch_img[0,:] batch_img = (batch_img + 1.) / 2. # show caption fid = open('/media/DATA/MS-COCO/word_counts.txt') raw_words = fid.readlines() words = [] for raw_word in raw_words: word, _ = raw_word.split() words.append(word) batch_cap = batch_cap[0] sentence = [] for tmp_id in batch_cap: sentence.append(words[int(tmp_id)]) print(sentence) plt.imshow(batch_img) plt.show()
def gen_aperture(imgsize,ypos,xpos,radius,pixval=1,showaperture=False,verbose=True): """ Generating an aperture image --- INPUT --- imgsize The dimensions of the array to return. Expects [y-size,x-size]. The aperture will be positioned in the center of a (+/-x-size/2., +/-y-size/2) sized array ypos Pixel position in the y direction xpos Pixel position in the x direction radius Radius of aperture in pixels showaperture Display image of generated aperture verbose Toggle verbosity --- EXAMPLE OF USE --- import tdose_utilities as tu apertureimg = tu.gen_aperture([20,40],10,5,10,showaperture=True) apertureimg = tu.gen_aperture([2000,4000],900,1700,150,showaperture=True) """ if verbose: print ' - Generating aperture in image (2D array)' y , x = np.ogrid[-ypos:imgsize[0]-ypos, -xpos:imgsize[1]-xpos] mask = x*x + y*y <= radius**2. aperture = np.zeros(imgsize) if verbose: print ' - Assigning pixel value '+str(pixval)+' to aperture' aperture[mask] = pixval if showaperture: if verbose: print ' - Displaying resulting image of aperture' plt.imshow(aperture,interpolation='none') plt.title('Generated aperture') plt.show() return aperture # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def gen_overview_plot_image(ax,imagefile,imgext=0,cubelayer=1,title='Img Title?',fontsize=6,lthick=2,alpha=0.5, cmap='coolwarm'): """ Plotting commands for image (cube layer) overview plotting --- INPUT --- cubelayer If the content of the file is a cube, provide the cube layer to plot. If cubelayer = 'fmax' the layer with most flux is plotted """ ax.set_title(title,fontsize=fontsize) if os.path.isfile(imagefile): imgdata = pyfits.open(imagefile)[imgext].data if len(imgdata.shape) == 3: # it is a cube imgdata = imgdata[cubelayer,:,:] ax.imshow(imgdata, interpolation='None',cmap=cmap,aspect='equal', origin='lower') ax.set_xlabel('x-pixel') ax.set_ylabel('y-pixel ') ax.set_xticks([]) ax.set_yticks([]) else: textstr = 'No image\nfound' ax.text(1.0,22,textstr,horizontalalignment='center',verticalalignment='center',fontsize=fontsize) ax.set_ylim([28,16]) ax.plot([0.0,2.0],[28,16],'r--',lw=lthick) ax.plot([2.0,0.0],[28,16],'r--',lw=lthick) ax.set_xlabel(' ') ax.set_ylabel(' ') ax.set_xticks([]) ax.set_yticks([]) # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def residual_multigauss(param, dataimage, nonfinite = 0.0, ravelresidual=True, showimages=False, verbose=False): """ Calculating the residual bestween the multigaussian model with the paramters 'param' and the data. --- INPUT --- param Parameters of multi-gaussian model to generate. See modelimage_multigauss() header for details dataimage Data image to take residual nonfinite Value to replace non-finite entries in residual with ravelresidual To np.ravel() the residual image set this to True. Needed by scipy.optimize.leastsq() optimizer function showimages To show model and residiual images set to True verbose Toggle verbosity --- EXAMPLE OF USE --- import tdose_model_FoV as tmf param = [18,31,1*0.3,2.1*0.3,1.2*0.3,30*0.3, 110,90,200*0.5,20.1*0.5,15.2*0.5,0*0.5] dataimg = pyfits.open('/Users/kschmidt/work/TDOSE/mock_cube_sourcecat161213_tdose_mock_cube.fits')[0].data[0,:,:] residual = tmf.residual_multigauss(param, dataimg, showimages=True) """ if verbose: ' - Estimating residual (= model - data) between model and data image' imgsize = dataimage.shape xgrid, ygrid = tu.gen_gridcomponents(imgsize) modelimg = tmf.modelimage_multigauss((xgrid, ygrid),param,imgsize,showmodelimg=showimages, verbose=verbose) residualimg = modelimg - dataimage if showimages: plt.imshow(residualimg,interpolation='none', vmin=1e-5, vmax=np.max(residualimg), norm=mpl.colors.LogNorm()) plt.title('Resdiaul (= model - data) image') plt.show() if nonfinite is not None: residualimg[~np.isfinite(residualimg)] = 0.0 if ravelresidual: residualimg = np.ravel(residualimg) return residualimg # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def imageSegmentor(imageFilePath, matFilePath): mat = readMatFile(matFilePath); # read mat file image = getImage(imageFilePath); # input the image typeOfFruit = getTypeOfFruit(image); # on basis of counting or temperature value there are 2 types of fruit plt.imshow(image); _fft = getFFT(image); _mag = getMag(_fft); _ang = getAngleInDegrees(_fft); edges = edgeDetector(image); # detects the edges of the image _segmentation = segmentation(image, typeOfFruit); # segments different parts of image filteredImage = filterImageFromSegmentation(image, _segmentation); # filter the object part of image outputMatrix = imageMapping(filteredImage, mat['IR']); # map the value part of the image and else 0 prefix = re.split('IR_|.pgm', imageFilePath)[0]; postfix = re.split('IR_|.pgm', imageFilePath)[1]; nameOfFile = prefix + "csv_" nameOfFile = nameOfFile + postfix; print(nameOfFile); writeToCSV(outputMatrix, nameOfFile); # write it to the CSV file writeFF2CSV(outputMatrix, _mag, _ang, nameOfFile); fig, ((fig1, fig2), (fig3, fig4)) = plt.subplots(2, 2, figsize = (10, 8)); # subplot the different plots fig1.imshow(image, cmap = plt.cm.gray); # colormap used here is gray fig2.imshow(image, cmap = plt.cm.gray); fig3.imshow(edges, cmap = plt.cm.gray); fig4.imshow(filteredImage, cmap = plt.cm.gray); return # header file
def draw(m, name, extra=None): FIG.clf() matrix = m orig_shape = np.shape(matrix) # lose the channel shape in the end of orig_shape new_shape = orig_shape[:-1] matrix = np.reshape(matrix, new_shape) ax = FIG.add_subplot(1,1,1) ax.set_aspect('equal') plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.gray) # plt.imshow(matrix, interpolation='nearest', cmap=plt.cm.ocean) plt.colorbar() if extra != None: greens, reds = extra grn_x, grn_y, = greens red_x, red_y = reds plt.scatter(x=grn_x, y=grn_y, c='g', s=40) plt.scatter(x=red_x, y=red_y, c='r', s=40) # # put a blue dot at (10, 20) # plt.scatter([10], [20]) # # put a red dot, size 40, at 2 locations: # plt.scatter(x=[3, 4], y=[5, 6], c='r', s=40) # # plt.plot() plt.savefig(name)
def plot1D_mat(a, b, M, title=''): """ Plot matrix M with the source and target 1D distribution Creates a subplot with the source distribution a on the left and target distribution b on the tot. The matrix M is shown in between. Parameters ---------- a : np.array, shape (na,) Source distribution b : np.array, shape (nb,) Target distribution M : np.array, shape (na,nb) Matrix to plot """ na, nb = M.shape gs = gridspec.GridSpec(3, 3) xa = np.arange(na) xb = np.arange(nb) ax1 = pl.subplot(gs[0, 1:]) pl.plot(xb, b, 'r', label='Target distribution') pl.yticks(()) pl.title(title) ax2 = pl.subplot(gs[1:, 0]) pl.plot(a, xa, 'b', label='Source distribution') pl.gca().invert_xaxis() pl.gca().invert_yaxis() pl.xticks(()) pl.subplot(gs[1:, 1:], sharex=ax1, sharey=ax2) pl.imshow(M, interpolation='nearest') pl.axis('off') pl.xlim((0, nb)) pl.tight_layout() pl.subplots_adjust(wspace=0., hspace=0.2)
def rasta_plp_extractor(x, sr, plp_order=0, do_rasta=True): spec = log_power_spectrum_extractor(x, int(sr*0.02), int(sr*0.01), 'hamming', False) bark_filters = int(np.ceil(freq2bark(sr//2))) wts = get_fft_bark_mat(sr, int(sr*0.02), bark_filters) ''' plt.figure() plt.subplot(211) plt.imshow(wts) plt.subplot(212) plt.hold(True) for i in range(18): plt.plot(wts[i, :]) plt.show() ''' bark_spec = np.matmul(wts, spec) if do_rasta: bark_spec = np.where(bark_spec == 0.0, np.finfo(float).eps, bark_spec) log_bark_spec = np.log(bark_spec) rasta_log_bark_spec = rasta_filt(log_bark_spec) bark_spec = np.exp(rasta_log_bark_spec) post_spec = postaud(bark_spec, sr/2.) if plp_order > 0: lpcas = do_lpc(post_spec, plp_order) # lpcas = do_lpc(spec, plp_order) # just for test else: lpcas = post_spec return lpcas
def run_frey(): # import dataset data = pods.datasets.brendan_faces() # Y = data['Y'][:50, :] Y = data['Y'] Yn = Y - np.mean(Y, axis=0) Yn /= np.std(Y, axis=0) Y = Yn # inference print "inference ..." M = 30 D = 20 lvm = vfe.SGPLVM(Y, D, M, lik='Gaussian') lvm.optimise(method='L-BFGS-B', maxiter=10) plt.figure() mx, vx = lvm.get_posterior_x() zu = lvm.sgp_layer.zu plt.scatter(mx[:, 0], mx[:, 1]) plt.plot(zu[:, 0], zu[:, 1], 'ko') nx = ny = 30 x_values = np.linspace(-5, 5, nx) y_values = np.linspace(-5, 5, ny) sx = 28 sy = 20 canvas = np.empty((sx * ny, sy * nx)) for i, yi in enumerate(x_values): for j, xi in enumerate(y_values): z_mu = np.array([[xi, yi]]) x_mean, x_var = lvm.predict_f(z_mu) canvas[(nx - i - 1) * sx:(nx - i) * sx, j * sy:(j + 1) * sy] = x_mean.reshape(sx, sy) plt.figure(figsize=(8, 10)) Xi, Yi = np.meshgrid(x_values, y_values) plt.imshow(canvas, origin="upper", cmap="gray") plt.tight_layout() plt.show()
def run_frey(): # import dataset data = pods.datasets.brendan_faces() # Y = data['Y'][:50, :] Y = data['Y'] Yn = Y - np.mean(Y, axis=0) Yn /= np.std(Y, axis=0) Y = Yn # inference print "inference ..." M = 30 D = 20 lvm = aep.SGPLVM(Y, D, M, lik='Gaussian') # lvm.train(alpha=0.5, no_epochs=10, n_per_mb=100, lrate=0.1, fixed_params=['sn']) lvm.optimise(method='L-BFGS-B', alpha=0.1, maxiter=10) plt.figure() mx, vx = lvm.get_posterior_x() zu = lvm.sgp_layer.zu plt.scatter(mx[:, 0], mx[:, 1]) plt.plot(zu[:, 0], zu[:, 1], 'ko') nx = ny = 30 x_values = np.linspace(-5, 5, nx) y_values = np.linspace(-5, 5, ny) sx = 28 sy = 20 canvas = np.empty((sx * ny, sy * nx)) for i, yi in enumerate(x_values): for j, xi in enumerate(y_values): z_mu = np.array([[xi, yi]]) x_mean, x_var = lvm.predict_f(z_mu) canvas[(nx - i - 1) * sx:(nx - i) * sx, j * sy:(j + 1) * sy] = x_mean.reshape(sx, sy) plt.figure(figsize=(8, 10)) Xi, Yi = np.meshgrid(x_values, y_values) plt.imshow(canvas, origin="upper", cmap="gray") plt.tight_layout() plt.show()
def snapshot(self, fn = None, clip_ratio = None): import matplotlib.pylab as pylab pylab.figure(2) self.transfer_function.show() pylab.draw() im = Camera.snapshot(self, fn, clip_ratio) pylab.figure(1) pylab.imshow(im / im.max()) pylab.draw() self.frames.append(im)
def imageSegy(Data): """ imageSegy(Data) Image segy Data """ import matplotlib.pylab as plt plt.imshow(Data) plt.title('pymat test') plt.grid(True) plt.show() #%%
def draw_heatmap(xedges, yedges, heatmap): extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]] plt.figure(figsize=(8, 8)) plt.imshow(heatmap, extent=extent) plt.show()
def animate(y, ndim, cmap) : plt.ion() if ndim == 5: plt.figure() plt.show() for i in range(y.shape[1]) : print "Showing batch", i plt.close('all') for j in range(y.shape[0]) : plt.imshow(y[j,i], interpolation='none', cmap=cmap) plt.pause(0.1) time.sleep(1) else: for i in range(y.shape[1]) : print "Showing batch", i plt.close('all') for j in range(y.shape[0]) : plt.figure(0) plt.imshow(y[j,i], interpolation='none', cmap=cmap) plt.figure(1) plt.imshow(x[j,i], interpolation='none', cmap=cmap) plt.pause(0.2) time.sleep(1)
def fancy_show(y, cmap=''): x = y[0] y = y[1] plt.figure(0) for i in range(100): plt.subplot(10, 10, i+1) plt.imshow(x[i], cmap=cmap, interpolation='none') plt.axis('off') plt.figure(1) for i in range(100): plt.subplot(10, 10, i+1) plt.imshow(y[i], cmap=cmap, interpolation='none') plt.axis('off') plt.show()
def imshow_(x, **kwargs): if x.ndim == 2: plt.imshow(x, interpolation="nearest", **kwargs) elif x.ndim == 1: plt.imshow(x[:,None].T, interpolation="nearest", **kwargs) plt.yticks([]) plt.axis("tight") # ------------- Data -------------
def show_image(data, shape, order='f', cmap=cm.gray): """ display an image from a 1d vector :param data: 1d vector containing image information :param shape: actual image dimensions :param order: 'c' or 'f' :param cmap: colour map, defaults to grayscale :return: """ img = data.reshape(shape, order=order) plt.imshow(img, cmap=cmap) plt.show()
def plotImgPatchPrototypes(doShowNow=True): from matplotlib import pylab pylab.figure() for kk in range(K): pylab.subplot(2, 4, kk + 1) Xp = makeImgPatchPrototype(D, kk) pylab.imshow(Xp, interpolation='nearest') if doShowNow: pylab.show()
def plotTrueCovMats(doShowNow=True): from matplotlib import pylab pylab.figure() for kk in range(K): pylab.subplot(2, 4, kk + 1) pylab.imshow(Sigma[kk], interpolation='nearest') if doShowNow: pylab.show()
def plotBlackWhiteStateSeqForMeeting(meetingNum=1, badUIDs=[-1, -2], **kwargs): ''' Make plot like in Fig. 3 of AOAS paper ''' from matplotlib import pylab Data = get_data(meetingNum=args.meetingNum) Z = np.asarray(Data.TrueParams['Z'], dtype=np.int32) uLabels = np.unique(Z) uLabels = np.asarray([u for u in uLabels if u not in badUIDs]) sizes = np.asarray([np.sum(Z == u) for u in uLabels]) sortIDs = np.argsort(-1 * sizes) Zim = np.zeros((10, Z.size)) for rankID, uID in enumerate(uLabels[sortIDs]): Zim[1 + rankID, Z == uID] = 1 size = sizes[sortIDs[rankID]] frac = size / float(Z.size) print 'state %3d: %5d tsteps (%.3f)' % (rankID + 1, size, frac) for uID in badUIDs: size = np.sum(Z == uID) frac = size / float(Z.size) print 'state %3d: %5d tsteps (%.3f)' % (uID, size, frac) pylab.imshow(1 - Zim, interpolation='nearest', aspect=Zim.shape[1] / float(Zim.shape[0]) / 3, cmap='bone', vmin=0, vmax=1, origin='lower') pylab.show()
def MakePlans(Data, model, LP, Q=None, **kwargs): ''' Create list of Plans ''' newTopics, Info = makeCandidateTopics(Data, Q, model, LP, **kwargs) if 'doVizBirth' in kwargs and kwargs['doVizBirth']: from matplotlib import pylab pylab.imshow(newTopics, vmin=0, vmax=0.01, aspect=Data.vocab_size / newTopics.shape[0], interpolation='nearest') Plans = list() for kk in xrange(newTopics.shape[0]): Plan = dict(ktarget=None, Data=None, targetWordIDs=None, targetWordFreq=newTopics[kk]) # Add material to the log topWords = np.argsort(-1 * Plan['targetWordFreq'])[:10] if hasattr(Data, 'vocab_dict'): Vocab = getVocab(Data) topWordStr = ' '.join([Vocab[w] for w in topWords]) else: topWordStr = ' '.join([str(w) for w in topWords]) Plan['log'] = list() Plan['topWordIDs'] = topWords Plan['log'].append(topWordStr) if 'anchorID' in Info: anchorWordStr = 'Anchor: ' + str(Info['anchorID'][kk]) Plan['anchorWordID'] = anchorWordStr Plan['log'].append(anchorWordStr) Plans.append(Plan) return Plans
def roll_2Dprofile(profile,position,padvalue=0.0,showprofiles=False): """ Move 2D profile to given position in array by rolling it in x and y. Note that the roll does not handle sub-pixel moves. tu.shift_2Dprofile() does this using interpolation --- INPUT --- profile profile to shift position position to move center of image (profile) to: [ypos,xpos] padvalue the values to padd the images with when shifting profile showprofiles Show profile when shifted? --- EXAMPLE OF USE --- tu.roll_2Dprofile(gauss2D,) """ profile_dim = profile.shape yroll = np.int(np.round(position[0]-profile_dim[0]/2.)) xroll = np.int(np.round(position[1]-profile_dim[1]/2.)) profile_shifted = np.roll(np.roll(profile,yroll,axis=0),xroll,axis=1) if showprofiles: vmaxval = np.max(profile_shifted) plt.imshow(profile_shifted,interpolation='none',vmin=-vmaxval, vmax=vmaxval) plt.title('Positioned Source') plt.show() if yroll < 0: profile_shifted[yroll:,:] = padvalue else: profile_shifted[:yroll,:] = padvalue if xroll < 0: profile_shifted[:,xroll:] = padvalue else: profile_shifted[:,:xroll] = padvalue if showprofiles: plt.imshow(profile_shifted,interpolation='none',vmin=-vmaxval, vmax=vmaxval) plt.title('Positioned Source with 0s inserted') plt.show() return profile_shifted # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def optimize_img_scale(img_data,img_std,img_model,optimizer='curve_fit',show_residualimg=False,verbose=True): """ optimize the (flux) scaling of an image with respect to a (noisy) data image --- INPUT --- img_data The (noisy) data image to scale model image provide in img_model to img_std Standard deviation image for data to use in optimization img_model Model image to (flux) scale to match img_data optimizer The optimizer to use when scaling the layers show_residualimg To show the residual image (data - model) for the optimize layers, set this to true verbose Toggle verbosity --- EXAMPLE OF USE --- import tdose_model_cube as tmc scale, cov = tmc.optimize_img_scale() """ if verbose: print ' - Optimize residual between model (multiple Gaussians) and data with least squares in 2D' if verbose: print ' ----------- Started on '+tu.get_now_string()+' ----------- ' # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if optimizer == 'leastsq': sys.exit('optimizer = "leastsq" no enabled') # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - elif optimizer == 'curve_fit': imgsize = img_data.shape xgrid, ygrid = tu.gen_gridcomponents(imgsize) scale_best, scale_cov = opt.curve_fit(lambda (xgrid, ygrid), scale: tmc.curve_fit_fct_wrapper_imgscale((xgrid, ygrid), scale, img_model), (xgrid, ygrid), img_data.ravel(), p0 = [1.0], sigma=img_std.ravel() ) output = scale_best, scale_cov else: sys.exit(' ---> Invalid optimizer ('+optimizer+') chosen in optimize_img_scale()') # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if verbose: print ' ----------- Finished on '+tu.get_now_string()+' ----------- ' # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - if show_residualimg: if verbose: print ' - Displaying the residual image between data and scaled model image ' res_img = img_model-img_data plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm()) plt.title('Initial Residual = Initial Model Image - Data Image') plt.show() res_img = img_model*scale_best-img_data plt.imshow(res_img,interpolation='none', vmin=1e-5, vmax=np.max(res_img), norm=mpl.colors.LogNorm()) plt.title('Best Residual = Scaled (by '+str(scale_best)+') Model Image - Data Image') plt.show() # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - return output # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
def run_mnist(): np.random.seed(42) # import dataset f = gzip.open('./tmp/data/mnist.pkl.gz', 'rb') (x_train, t_train), (x_valid, t_valid), (x_test, t_test) = cPickle.load(f) f.close() Y = x_train[:100, :] labels = t_train[:100] Y[Y < 0.5] = -1 Y[Y > 0.5] = 1 # inference print "inference ..." M = 30 D = 2 # lvm = vfe.SGPLVM(Y, D, M, lik='Gaussian') lvm = vfe.SGPLVM(Y, D, M, lik='Probit') # lvm.train(alpha=0.5, no_epochs=10, n_per_mb=100, lrate=0.1, fixed_params=['sn']) lvm.optimise(method='L-BFGS-B') plt.figure() mx, vx = lvm.get_posterior_x() zu = lvm.sgp_layer.zu plt.scatter(mx[:, 0], mx[:, 1], c=labels) plt.plot(zu[:, 0], zu[:, 1], 'ko') nx = ny = 30 x_values = np.linspace(-5, 5, nx) y_values = np.linspace(-5, 5, ny) sx = 28 sy = 28 canvas = np.empty((sx * ny, sy * nx)) for i, yi in enumerate(x_values): for j, xi in enumerate(y_values): z_mu = np.array([[xi, yi]]) x_mean, x_var = lvm.predict_f(z_mu) t = x_mean / np.sqrt(1 + x_var) Z = 0.5 * (1 + special.erf(t / np.sqrt(2))) canvas[(nx - i - 1) * sx:(nx - i) * sx, j * sy:(j + 1) * sy] = Z.reshape(sx, sy) plt.figure(figsize=(8, 10)) Xi, Yi = np.meshgrid(x_values, y_values) plt.imshow(canvas, origin="upper", cmap="gray") plt.tight_layout() plt.show()
def plot_model_no_control(model, plot_title='', name_suffix=''): # plot function mx, vx = model.get_posterior_x() mins = np.min(mx, axis=0) - 0.5 maxs = np.max(mx, axis=0) + 0.5 nGrid = 50 xspaced = np.linspace(mins[0], maxs[0], nGrid) yspaced = np.linspace(mins[1], maxs[1], nGrid) xx, yy = np.meshgrid(xspaced, yspaced) Xplot = np.vstack((xx.flatten(), yy.flatten())).T mf, vf = model.predict_f(Xplot) fig = plt.figure() plt.imshow((mf[:, 0]).reshape(*xx.shape), vmin=mf.min(), vmax=mf.max(), origin='lower', extent=[mins[0], maxs[0], mins[1], maxs[1]], aspect='auto') plt.colorbar() plt.contour( xx, yy, (mf[:, 0]).reshape(*xx.shape), colors='k', linewidths=2, zorder=100) zu = model.dyn_layer.zu plt.plot(zu[:, 0], zu[:, 1], 'wo', mew=0, ms=4) for i in range(mx.shape[0] - 1): plt.plot(mx[i:i + 2, 0], mx[i:i + 2, 1], '-bo', ms=3, linewidth=2, zorder=101) plt.xlabel(r'$x_{t, 1}$') plt.ylabel(r'$x_{t, 2}$') plt.xlim([mins[0], maxs[0]]) plt.ylim([mins[1], maxs[1]]) plt.title(plot_title) plt.savefig('/tmp/hh_gpssm_dim_0' + name_suffix + '.pdf') fig = plt.figure() plt.imshow((mf[:, 1]).reshape(*xx.shape), vmin=mf.min(), vmax=mf.max(), origin='lower', extent=[mins[0], maxs[0], mins[1], maxs[1]], aspect='auto') plt.colorbar() plt.contour( xx, yy, (mf[:, 1]).reshape(*xx.shape), colors='k', linewidths=2, zorder=100) zu = model.dyn_layer.zu plt.plot(zu[:, 0], zu[:, 1], 'wo', mew=0, ms=4) for i in range(mx.shape[0] - 1): plt.plot(mx[i:i + 2, 0], mx[i:i + 2, 1], '-bo', ms=3, linewidth=2, zorder=101) plt.xlabel(r'$x_{t, 1}$') plt.ylabel(r'$x_{t, 2}$') plt.xlim([mins[0], maxs[0]]) plt.ylim([mins[1], maxs[1]]) plt.title(plot_title) plt.savefig('/tmp/hh_gpssm_dim_1' + name_suffix + '.pdf')
def run_mnist(): np.random.seed(42) # import dataset f = gzip.open('./tmp/data/mnist.pkl.gz', 'rb') (x_train, t_train), (x_valid, t_valid), (x_test, t_test) = cPickle.load(f) f.close() Y = x_train[:100, :] labels = t_train[:100] Y[Y < 0.5] = -1 Y[Y > 0.5] = 1 # inference print "inference ..." M = 30 D = 2 # lvm = aep.SGPLVM(Y, D, M, lik='Gaussian') lvm = aep.SGPLVM(Y, D, M, lik='Probit') # lvm.train(alpha=0.5, no_epochs=10, n_per_mb=100, lrate=0.1, fixed_params=['sn']) lvm.optimise(method='L-BFGS-B', alpha=0.1) plt.figure() mx, vx = lvm.get_posterior_x() zu = lvm.sgp_layer.zu plt.scatter(mx[:, 0], mx[:, 1], c=labels) plt.plot(zu[:, 0], zu[:, 1], 'ko') nx = ny = 30 x_values = np.linspace(-5, 5, nx) y_values = np.linspace(-5, 5, ny) sx = 28 sy = 28 canvas = np.empty((sx * ny, sy * nx)) for i, yi in enumerate(x_values): for j, xi in enumerate(y_values): z_mu = np.array([[xi, yi]]) x_mean, x_var = lvm.predict_f(z_mu) t = x_mean / np.sqrt(1 + x_var) Z = 0.5 * (1 + special.erf(t / np.sqrt(2))) canvas[(nx - i - 1) * sx:(nx - i) * sx, j * sy:(j + 1) * sy] = Z.reshape(sx, sy) plt.figure(figsize=(8, 10)) Xi, Yi = np.meshgrid(x_values, y_values) plt.imshow(canvas, origin="upper", cmap="gray") plt.tight_layout() plt.show()
def plot_waterfall(fil, f_start=None, f_stop=None, if_id=0, logged=True,cb=False,freq_label=False,MJD_time=False, **kwargs): """ Plot waterfall of data Args: f_start (float): start frequency, in MHz f_stop (float): stop frequency, in MHz logged (bool): Plot in linear (False) or dB units (True), cb (bool): for plotting the colorbar kwargs: keyword args to be passed to matplotlib imshow() """ matplotlib.rc('font', **font) plot_f, plot_data = fil.grab_data(f_start, f_stop, if_id) # Make sure waterfall plot is under 4k*4k dec_fac_x, dec_fac_y = 1, 1 if plot_data.shape[0] > MAX_IMSHOW_POINTS[0]: dec_fac_x = plot_data.shape[0] / MAX_IMSHOW_POINTS[0] if plot_data.shape[1] > MAX_IMSHOW_POINTS[1]: dec_fac_y = plot_data.shape[1] / MAX_IMSHOW_POINTS[1] plot_data = rebin(plot_data, dec_fac_x, dec_fac_y) if MJD_time: extent=(plot_f[0], plot_f[-1], fil.timestamps[-1], fil.timestamps[0]) else: extent=(plot_f[0], plot_f[-1], (fil.timestamps[-1]-fil.timestamps[0])*24.*60.*60, 0.0) this_plot = plt.imshow(plot_data, aspect='auto', rasterized=True, interpolation='nearest', extent=extent, cmap='viridis_r', **kwargs ) if cb: plt.colorbar() if freq_label: plt.xlabel("Frequency [Hz]",fontdict=font) if MJD_time: plt.ylabel("Time [MJD]",fontdict=font) else: plt.ylabel("Time [s]",fontdict=font) return this_plot