def make_plots(results, img=None, label='', sidelabel=''): ln = len(results) if img is None: size = 3 fig = pl.figure(figsize=(ln * size, size + 2.0 / ln)) img = ImageGrid(fig, rect=[0.05 / ln, 0.05, 1 - 0.1 / ln, 0.90], nrows_ncols=[1, ln], axes_pad=0.1) # get a common color bar scale for all images mins, maxs = [], [] for i, v in enumerate(results): if v[0] == 'Reference': continue mins.append(v[1].min()) maxs.append(v[1].max()) mins = min(mins) maxs = max(maxs) mins = -0.5 * max(np.abs([mins, maxs])) maxs = -mins for i, v in enumerate(results): if v[0] == 'Reference': img[i].imshow(v[1], vmin=0, vmax=1, cmap='bone') else: img[i].imshow(v[1], vmin=mins, vmax=maxs) img[i].set_title(v[0], fontsize=17) img[i].set_xticks([]) img[i].set_yticks([]) if label: lbl(img[i], label + str(i + 1), 18) if i == 0 and sidelabel: img[i].set_ylabel(sidelabel)
def doplot(image0, image1, xs, crbs, errors, labels, diff_image_scale=0.1, dolabels=True, multiple_crbs=True, xlim=None, ylim=None, highlight=None, detailed_labels=False, xlabel="", title=""): """ Standardizing the plot format of the does_matter section. See any of the accompaning files to see how to use this generalized plot. image0 : ground true image1 : difference image xs : list of x values for the plots crbs : list of lines of values of the crbs errors : list of lines of errors labels : legend labels for each curve """ if len(crbs) != len(errors) or len(crbs) != len(labels): raise IndexError, "lengths are not consistent" fig = pl.figure(figsize=(14, 7)) ax = fig.add_axes([0.43, 0.15, 0.52, 0.75]) gs = ImageGrid(fig, rect=[0.05, 0.05, 0.25, 0.90], nrows_ncols=(2, 1), axes_pad=0.25, cbar_location='right', cbar_mode='each', cbar_size='10%', cbar_pad=0.04) diffm = diff_image_scale * np.ceil(np.abs(image1).max() / diff_image_scale) im0 = gs[0].imshow(image0, vmin=0, vmax=1, cmap='bone_r') im1 = gs[1].imshow(image1, vmin=-diffm, vmax=diffm, cmap='RdBu') cb0 = pl.colorbar(im0, cax=gs[0].cax, ticks=[0, 1]) cb1 = pl.colorbar(im1, cax=gs[1].cax, ticks=[-diffm, diffm]) cb0.ax.set_yticklabels(['0', '1']) cb1.ax.set_yticklabels(['-%0.1f' % diffm, '%0.1f' % diffm]) image_names = ["Reference", "Difference"] for i in xrange(2): gs[i].set_xticks([]) gs[i].set_yticks([]) gs[i].set_ylabel(image_names[i]) if dolabels: lbl(gs[i], figlbl[i]) symbols = ['o', '^', 'D', '>'] for i in xrange(len(labels)): c = COLORS[i] if multiple_crbs or i == 0: if multiple_crbs: label = labels[i] if ( i != 0 and not detailed_labels) else '%s CRB' % labels[i] else: label = 'CRB' ax.plot(xs[i], crbs[i], '-', c=c, lw=3, label=label) label = labels[i] if ( i != 0 and not detailed_labels) else '%s Error' % labels[i] ax.plot(xs[i], errors[i], symbols[i], ls='--', lw=2, c=c, label=label, ms=12) if dolabels: lbl(ax, 'D') ax.loglog() if xlim: ax.set_xlim(xlim) if ylim: ax.set_ylim(ylim) ax.legend(loc='upper left', ncol=2, prop={'size': 18}, numpoints=1) ax.set_xlabel(xlabel) ax.set_ylabel(r"Position CRB, Error") ax.grid(False, which='both', axis='both') ax.set_title(title) return gs, ax
def plot_noise_pole(diff): center = np.array(diff.shape) / 2 poles = np.array([ [190, 277], [227, 253], [233, 256], [255, 511], [255, 271], #[255, 240], #[198, 249], #[247, 253] ]) q = np.fft.fftshift(np.fft.fftn(diff)) t = pole_removal(q, poles, sig=4) r = np.real(np.fft.ifftn(np.fft.fftshift(t))) fig = pl.figure(figsize=(24, 8)) gs = ImageGrid(fig, rect=[0.05, 0.05, 0.33, 0.85], nrows_ncols=(2, 2), axes_pad=0.05) images = [[diff, q], [r, t]] maxs, mins = [], [] for i, im in enumerate(images): a, b = im[0], np.abs(im[1])**0.2 maxs.append([a.max(), b.max()]) mins.append([a.min(), b.min()]) maxs, mins = np.array(maxs), np.array(mins) maxs = 0.8 * maxs.max(axis=0) mins = 0.8 * mins.min(axis=0) labels = [['A', 'B'], ['C', 'D']] for i, (im, lb) in enumerate(zip(images, labels)): ax = [gs[2 * i + 0], gs[2 * i + 1]] ax[0].imshow(im[0], vmin=mins[0], vmax=maxs[0], cmap=pl.cm.bone) ax[1].imshow(np.abs(im[1])**0.2, vmin=mins[1], vmax=maxs[1], cmap=pl.cm.bone) lbl(ax[0], lb[0]) lbl(ax[1], lb[1]) for a in ax: a.grid(False, which='both', axis='both') a.set_xticks([]) a.set_yticks([]) if i == 0: ax[0].set_title("Real-space") ax[1].set_title("k-space") ax[0].set_ylabel("Raw noise") if i == 1: ax[0].set_ylabel("Poles removed") for p in poles: for pp in [p, 2 * center - p]: a.plot(pp[1], pp[0], 'wo') ax = fig.add_axes([0.37, 0.15, 0.28, 0.7]) ax.imshow(diff - r, cmap=pl.cm.bone) ax.set_xticks([]) ax.set_yticks([]) ax.set_title("Correction") lbl(ax, 'E') ax = fig.add_axes([0.70, 0.15, 0.25, 0.7]) sig = diff.std() y, x = np.histogram(diff, bins=np.linspace(-5 * sig, 5 * sig, 300), normed=True) x = (x[1:] + x[:-1]) / 2 ax.plot(x, y, '-', lw=2, alpha=0.6, label='Raw noise') y, x = np.histogram(r, bins=np.linspace(-5 * sig, 5 * sig, 300), normed=True) x = (x[1:] + x[:-1]) / 2 ax.plot(x, y, '-', lw=2, alpha=0.6, label='Poles removed') ax.plot(x, 1 / np.sqrt(2 * np.pi * sig**2) * np.exp(-x**2 / (2 * sig**2)), 'k--', lw=1.5, label='Gaussian fit') ax.set_xlabel("Pixel value") ax.set_ylabel("Probability") ax.legend(loc='best', prop={'size': 18}) ax.grid(False, which='minor', axis='y') ax.locator_params(axis='x', nbins=5) #ax.set_title("Distribution of residuals") lbl(ax, 'F') ax.semilogy()
def plot_noise(diff): dat = diff.flatten() sig = dat.std() fig = pl.figure(figsize=(13, 4)) #===================================================== gs2 = GridSpec(1, 2, left=0.05, bottom=0.22, right=0.45, top=0.85, wspace=0.25, hspace=0.25) ax = pl.subplot(gs2[0, 0]) ax.imshow(diff[0], cmap=pl.cm.bone) ax.set_xticks([]) ax.set_yticks([]) ax.grid(False, which='both', axis='both') ax.set_xlabel(r"$x$") ax.set_ylabel(r"$y$") lbl(ax, 'A') q = np.fft.fftn(diff) q = np.fft.fftshift(np.abs(q[0])**0.1) ax = pl.subplot(gs2[0, 1]) ax.imshow(q, cmap=pl.cm.bone) ax.set_xticks([]) ax.set_yticks([]) ax.grid(False, which='both', axis='both') ax.set_xlabel(r"$q_x$") ax.set_ylabel(r"$q_y$") lbl(ax, 'B') #===================================================== gs = GridSpec(1, 1, left=0.59, bottom=0.22, right=0.89, top=0.85, wspace=0.35, hspace=0.35) y, x = np.histogram(dat, bins=np.linspace(-5 * sig, 5 * sig, 300), normed=True) x = (x[1:] + x[:-1]) / 2 ax = pl.subplot(gs[0, 0]) ax.plot(x, y, '-', lw=2, alpha=0.6, label='Confocal image') ax.plot(x, 1 / np.sqrt(2 * np.pi * sig**2) * np.exp(-x**2 / (2 * sig**2)), 'k--', lw=1.5, label='Gaussian fit') ax.semilogy() ymin = y[y > 0].min() ymax = y[y > 0].max() #ax.legend(loc='best', bbox_to_anchor=(0.75, 0.25), prop={'size':18}) ax.locator_params(axis='x', nbins=5) ax.set_xlim(-5 * sig, 5 * sig) ax.set_ylim(ymin, 1.35 * ymax) ax.set_xlabel("Pixel value") ax.set_ylabel("Probability") ax.grid(False, which='minor', axis='y') lbl(ax, 'C') return gs
def smile_comparison_plot(state0, state1, stdfrac=0.7): fig = pl.figure(figsize=(10, 12)) ig = ImageGrid(fig, rect=[0.05, 0.55, 0.90, 0.40], nrows_ncols=(1, 2), axes_pad=0.2) ax0 = fig.add_axes([0.13, 0.05, 0.74, 0.40]) ax1 = ax0.twinx() stringer = lambda o: ', '.join([str(i) for i in o]) states = [state0, state1] orders = [stringer(s.ilm.order) for s in states] colors = ('#598FEF', '#BCE85B', '#6C0514') dmin, dmax = 1e10, -1e10 rstd = -1e10 for i, (s, o, color) in enumerate(zip(states, orders, colors)): ax = ig[i] sl = np.s_[s.pad:-s.pad, s.pad:-s.pad, s.pad:-s.pad] diff = -(s.image - s.get_model_image())[sl] m = good_particles(s, False) r = s.obj.rad[m] std = stdfrac * r.std() rstd = max([rstd, std]) dmin = min([dmin, diff.min()]) dmax = max([dmax, diff.max()]) for i, (s, o, color) in enumerate(zip(states, orders, colors)): ax = ig[i] m = good_particles(s, False) p = s.obj.pos[m] r = s.obj.rad[m] z, y, x = p.T sl = np.s_[s.pad:-s.pad, s.pad:-s.pad, s.pad:-s.pad] mu = r.mean() c = pl.cm.RdBu_r(Normalize(vmin=mu - rstd, vmax=mu + rstd)(r))[:, :3] diff = -(s.image - s.get_model_image())[sl] if i == 0: lbl(ax, 'A') elif i == 1: lbl(ax, 'B') ax.set_title(o) ax.imshow(diff[-5], vmin=dmin, vmax=dmax) ax.scatter(x - s.pad, y - s.pad, s=60, c=c) ax.set_xlim(0, diff.shape[1]) ax.set_ylim(0, diff.shape[2]) ax.set_xticks([]) ax.set_yticks([]) ax.grid('off') sect = 255 * nd.gaussian_filter( diff.mean(axis=(0, 1)), 3, mode='reflect') ax0.plot(sect, lw=2, c=color, label='Residual %s' % o) ax1.plot(x, r, 'o', mfc=color, mec='black', label='Radii %s' % o, ms=6) ax0.set_xlim(50, diff.shape[1]) ax1.set_xlim(50, diff.shape[1]) ax0.set_ylim(sect.mean() - 16 * sect.std(), sect.mean() + 26 * sect.std()) ax1.set_ylim(r.mean() - 5 * r.std(), r.mean() + 2 * r.std()) ax0.grid('off') ax1.grid('off') ax0.set_xticks([]) ax1.set_xticks([]) ax0.set_xlabel("Pixel position") ax0.set_ylabel("Residual value") ax1.set_ylabel("Particle Radius") ax0.legend(bbox_to_anchor=(1.08, 1.3), ncol=4) ax1.legend(bbox_to_anchor=(1, 1.17), ncol=4, numpoints=1) if i == 1: lbl(ax0, 'C') lbl(ax1, 'C')