def key_press_event(self, event): self.event = event if event.key == 'c': self.component = self.cycle(self.component, self.components) self.set_field() self.draw() return if event.key == 'v': self.mode = 'view' if event.key == 'a': self.mode = 'add' if event.key == 'r': self.mode = 'remove' if event.key == 'g': self.mode = 'grab' if event.key == 'e': self.mode = 'optimize' if event.key == 'q': self.view = self.cycle(self.view, self.views) self.set_field() self.draw() return if event.key == 'w': self.modifier = self.cycle(self.modifier, self.modifiers) self.set_field() self.draw() return log.info("Switching mode to {}".format(self.mode)) for c in self._calls: self.fig.canvas.mpl_disconnect(c) self.register_events()
def twoslice(field, center=None, size=6.0, cmap='bone_r', vmin=0, vmax=1, orientation='vertical', figpad=1.09, off=0.01): """ Plot two parts of the ortho view, the two sections given by ``orientation``. """ center = center or [i // 2 for i in field.shape] slices = [] for i, c in enumerate(center): blank = [np.s_[:]] * len(center) blank[i] = c slices.append(tuple(blank)) z, y, x = [float(i) for i in field.shape] w = float(x + z) h = float(y + z) def show(field, ax, slicer, transpose=False): tmp = field[slicer] if not transpose else field[slicer].T ax.imshow(tmp, cmap=cmap, interpolation='nearest', vmin=vmin, vmax=vmax) ax.set_xticks([]) ax.set_yticks([]) ax.grid('off') if orientation.startswith('v'): # rect = l,b,w,h log.info('{} {} {} {} {} {}'.format(x, y, z, w, h, x / h)) r = x / h q = y / h f = 1 / (1 + 3 * off) fig = pl.figure(figsize=(size * r, size * f)) ax1 = fig.add_axes((off, f * (1 - q) + 2 * off, f, f * q)) ax2 = fig.add_axes((off, off, f, f * (1 - q))) show(field, ax1, slices[0]) show(field, ax2, slices[1]) else: # rect = l,b,w,h r = y / w q = x / w f = 1 / (1 + 3 * off) fig = pl.figure(figsize=(size * f, size * r)) ax1 = fig.add_axes((off, off, f * q, f)) ax2 = fig.add_axes((2 * off + f * q, off, f * (1 - q), f)) show(field, ax1, slices[0]) show(field, ax2, slices[2], transpose=True) return fig, ax1, ax2
def gd(state, N=1, ratio=1e-1): state.set_current_particle() for i in range(N): log.info('{}'.format(state.loglikelihood())) grad = state.gradloglikelihood() n = state.state + 1.0/np.abs(grad).max() * ratio * grad state.set_state(n) log.info('{}'.format(state.loglikelihood()))
def mouse_press_view(self, event): self.event = event p = self._pt_xyz(event) if p is not None: log.info("Moving view to [{:.5}, {:.5}, {:.5}]".format(*p)) self.slices = p self.draw()
def do_samples(s, sweeps, burn, stepout=0.1, save_period=-1, prefix='peri', save_name=None, sigma=True, pos=True, quiet=False, postfix=None): h = [] ll = [] if not save_name: with tempfile.NamedTemporaryFile(suffix='.peri-state.pkl', prefix=prefix) as f: save_name = f.name for i in range(sweeps): if save_period > 0 and i % save_period == 0: with open(save_name, 'w') as tfile: pickle.dump([s, h, ll], tfile, protocol=2) if postfix is not None: states.save(s, desc=postfix, extra=[np.array(h), np.array(ll)]) if not quiet: log.info('{:=^79}'.format(' Sweep ' + str(i) + ' ')) #sample_particles(s, stepout=stepout) if pos: sample_particle_pos(s, stepout=stepout, quiet=quiet) sample_particle_rad(s, stepout=stepout, quiet=quiet) sample_block(s, 'psf', stepout=stepout, quiet=quiet) sample_block(s, 'ilm', stepout=stepout, quiet=quiet) sample_block(s, 'off', stepout=stepout, quiet=quiet) sample_block(s, 'zscale', stepout=stepout, quiet=quiet) if s.bkg: sample_block(s, 'bkg', stepout=stepout, quiet=quiet) if s.slab: sample_block(s, 'slab', stepout=stepout, quiet=quiet) if sigma and s.nlogs: sample_block(s, 'sigma', stepout=stepout / 10, quiet=quiet) if i >= burn: h.append(s.state.copy()) ll.append(s.loglikelihood) if save_period > 0 and save_name: os.remove(save_name) h = np.array(h) ll = np.array(ll) return h, ll
def residual(vec, state, blocks, relax_particles=True): log.info('res {}'.format(state.loglikelihood())) modify(state, blocks, vec) for i in range(3): #sample_particles(state, quiet=True) optimize_particles(state) return state.residuals().flatten()
def sample_block(state, blockname, explode=True, stepout=0.1, quiet=False): if not quiet: log.info('{:-^39}'.format(' ' + blockname.upper() + ' ')) blocks = [state.create_block(blockname)] if explode: blocks = state.explode(blocks[0]) return sample_state(state, blocks, stepout)
def mouse_press_remove(self, event): self.event = event p = self._pt_xyz(event) if p is not None: log.info("Removing particle near {}".format(p)) rp = self._remove_closest_particle(p) self.update_particle_field(poses=rp.reshape((1, -1)), add=False) self.update_field() self.draw()
def mouse_press_remove(self, event): self.event = event p = self._pt_xyz(event) if p is not None: log.info("Removing particle near [{:.5}, {:.5}, {:.5}]".format(*p)) ind = self.state.obj_closest_particle(p) self.state.obj_remove_particle(ind) self.state.set_tile(self.state.oshape) self.set_field() self.draw()
def sample_particles(state, stepout=1, start=0, quiet=False): if not quiet: log.info('{:-^39}'.format(' POS / RAD ')) for particle in state.active_particles(): if not quiet: log.info('{}'.format(particle)) sys.stdout.flush() blocks = state.blocks_particle(particle) sample_state(state, blocks, stepout=stepout) return state.state.copy()
def mouse_press_add(self, event): self.event = event p = self._pt_xyz(event) if p is not None: p = np.array(p) log.info("Adding particle at {}".format(p)) self.pos = np.append(self.pos, p.reshape((1, -1)), axis=0) self.update_particle_field(poses=p.reshape((1, -1))) self.update_field() self.draw()
def scan(im, cycles=1, sleep=0.3, vmin=0, vmax=1, cmap='bone'): pl.figure(1) pl.show() time.sleep(3) for c in range(cycles): for i, sl in enumerate(im): log.info('{}'.format(i)) pl.clf() pl.imshow(sl, cmap=cmap, interpolation='nearest', origin='lower', vmin=vmin, vmax=vmax) pl.draw() time.sleep(sleep)
def scan_noise(image, state, element, size=0.01, N=1000): start = state.state[element] xs, ys = [], [] for i in range(N): log.info('{}'.format(i)) test = image + np.random.normal(0, state.sigma, image.shape) x, y = sample_ll(test, state, element, size=size, N=300) state.update(element, start) xs.append(x) ys.append(y) return xs, ys
def diag_crb_particles(state): crbpos = [] crbrad = [] for i in np.arange(state.N)[state.state[state.b_typ] == 1.]: log.info('{}'.format(i)) bl = state.blocks_particle(i) for b in bl[:-1]: crbpos.append(np.sqrt(1.0 / state.fisher_information(blocks=[b]))) crbrad.append(np.sqrt(1.0 / state.fisher_information(blocks=[bl[-1]]))) cx, cr = np.array(crbpos).reshape(-1, 3), np.squeeze(np.array(crbrad)) cx[np.isinf(cx)] = 0 cr[np.isinf(cr)] = 0 return cx, cr
def remove_overlaps_naive(pos, rad, zscale=1, doprint=False): N = rad.shape[0] z = np.array([zscale, 1, 1]) for i in range(N): for j in range(N): if i == j: continue d = np.sqrt(((z * (pos[i] - pos[j]))**2).sum()) r = rad[i] + rad[j] diff = d - r if diff < 0: rad[i] -= np.abs(diff) * rad[i] / (rad[i] + rad[j]) + 1e-10 rad[j] -= np.abs(diff) * rad[j] / (rad[i] + rad[j]) + 1e-10 if doprint: log.info('{} {} {}'.format(diff, rad[i], rad[j]))
def mouse_press_optimize(self, event): self.event = event p = self._pt_xyz(event) if p is not None: log.info( "Optimizing particle near [{:.5}, {:.5}, {:.5}]".format(*p)) n = self.state.obj_closest_particle(p) old_err = self.state.error _ = opt.do_levmarq_particles(self.state, np.array([n]), max_iter=2) new_err = self.state.error log.info('{}->{}'.format(old_err, new_err)) self.state.set_tile(self.state.oshape) self.set_field() self.draw()
def key_press_event(self, event): self.event = event if event.key == 'v': self.mode = 'view' elif event.key == 'a': self.mode = 'add' elif event.key == 'r': self.mode = 'remove' log.info("Switching mode to {}".format(self.mode)) for c in self._calls: self.fig.canvas.mpl_disconnect(c) self.register_events()
def scan_together(im, p, delay=2, vmin=0, vmax=1, cmap='bone'): pl.figure(1) pl.show() time.sleep(3) z,y,x = p.T for i in range(len(im)): log.info('{}'.format(i)) sl = im[i] pl.clf() pl.imshow(sl, cmap=cmap, interpolation='nearest', origin='lower', vmin=vmin, vmax=vmax) m = z.astype('int') == i pl.plot(x[m], y[m], 'o') pl.xlim(0, sl.shape[0]) pl.ylim(0, sl.shape[1]) pl.draw() time.sleep(delay)
def remove_overlaps(pos, rad, zscale=1, doprint=False): N = rad.shape[0] z = np.array([zscale, 1, 1]) for i in range(N): o = np.arange(i + 1, N) d = np.sqrt(((z * (pos[i] - pos[o]))**2).sum(axis=-1)) r = rad[i] + rad[o] diff = d - r mask = diff < 0 imask = o[mask] dmask = diff[mask] for j, d in zip(imask, dmask): rad[i] -= np.abs(d) * rad[i] / (rad[i] + rad[j]) + 1e-10 rad[j] -= np.abs(d) * rad[j] / (rad[i] + rad[j]) + 1e-10 if doprint: log.info('{} {} {}'.format(diff, rad[i], rad[j]))
def mouse_press_add(self, event): self.event = event p = self._pt_xyz(event) if p is not None: p = np.array(p) r = self.state.obj_get_radii() if len(r) == 0: r = 5.0 else: r = r.mean() log.info("Adding particle at [{:.5}, {:.5}, {:.5}], {:.4}".format( *(p.tolist() + [r]))) self.state.obj_add_particle(p, r) self.state.set_tile(self.state.oshape) self.set_field() self.draw()
def do_blocks(s, blocks, sweeps, burn, stepout=0.1, postfix=None, quiet=False): h, ll = [], [] for i in range(sweeps): if postfix is not None: states.save(s, desc=postfix, extra=[np.array(h), np.array(ll)]) if not quiet: log.info('{:=^79}'.format(' Sweep ' + str(i) + ' ')) sample_state(s, blocks, stepout=stepout, N=1, doprint=~quiet) if i >= burn: h.append(s.state.copy()) ll.append(s.loglikelihood) h = np.array(h) ll = np.array(ll) return h, ll
def test(): N = 128 for i in range(50): log.info('{}'.format(i)) x = np.random.rand(N, 3) r = 0.05 * np.random.rand(N) a = HardSphereOverlapNaive(x, r) b = HardSphereOverlapCell(x, r) assert ((a.logpriors == b.logpriors).all()) for j in range(100): l = np.random.randint(N, size=1) pp = x[l] #np.random.rand(3) rp = 0.05 * np.random.rand(1) a.update(l, pp, rp) b.update(l, pp, rp) if not (a.logpriors == b.logpriors).all(): log.info('{} {} {}'.format(l, pp, rp)) log.info('{}'.format((a.logpriors - b.logpriors).sum())) raise IOError
def crb_compare(state0, samples0, state1, samples1, crb0=None, crb1=None, zlayer=None, xlayer=None): """ To run, do: s,h = pickle... s1,h1 = pickle... i.e. /media/scratch/bamf/vacancy/vacancy_zoom-1.tif_t002.tif-featured-v2.pkl i.e. /media/scratch/bamf/frozen-particles/0.tif-featured-full.pkl crb0 = diag_crb_particles(s); crb1 = diag_crb_particles(s1) crb_compare(s,h[-25:],s1,h1[-25:], crb0, crb1) """ s0 = state0 s1 = state1 h0 = np.array(samples0) h1 = np.array(samples1) slicez = zlayer or s0.image.shape[0] // 2 slicex = xlayer or s0.image.shape[2] // 2 slicer1 = np.s_[slicez, s0.pad:-s0.pad, s0.pad:-s0.pad] slicer2 = np.s_[s0.pad:-s0.pad, s0.pad:-s0.pad, slicex] center = (slicez, s0.image.shape[1] // 2, slicex) mu0 = h0.mean(axis=0) mu1 = h1.mean(axis=0) std0 = h0.std(axis=0) std1 = h1.std(axis=0) mask0 = (s0.state[s0.b_typ] == 1.) & (analyze.trim_box( s0, mu0[s0.b_pos].reshape(-1, 3))) mask1 = (s1.state[s1.b_typ] == 1.) & (analyze.trim_box( s1, mu1[s1.b_pos].reshape(-1, 3))) active0 = np.arange(s0.N)[mask0] #s0.state[s0.b_typ]==1.] active1 = np.arange(s1.N)[mask1] #s1.state[s1.b_typ]==1.] pos0 = mu0[s0.b_pos].reshape(-1, 3)[active0] pos1 = mu1[s1.b_pos].reshape(-1, 3)[active1] rad0 = mu0[s0.b_rad][active0] rad1 = mu1[s1.b_rad][active1] link = analyze.nearest(pos0, pos1) dpos = pos0 - pos1[link] drad = rad0 - rad1[link] drift = dpos.mean(axis=0) log.info('drift {}'.format(drift)) dpos -= drift fig = pl.figure(figsize=(24, 10)) #========================================================================= #========================================================================= gs0 = ImageGrid(fig, rect=[0.02, 0.4, 0.4, 0.60], nrows_ncols=(2, 3), axes_pad=0.1) lbl(gs0[0], 'A') for i, slicer in enumerate([slicer1, slicer2]): ax_real = gs0[3 * i + 0] ax_fake = gs0[3 * i + 1] ax_diff = gs0[3 * i + 2] diff0 = s0.get_model_image() - s0.image diff1 = s1.get_model_image() - s1.image a = (s0.image - s1.image) b = (s0.get_model_image() - s1.get_model_image()) c = (diff0 - diff1) ptp = 0.7 * max([np.abs(a).max(), np.abs(b).max(), np.abs(c).max()]) cmap = pl.cm.RdBu_r ax_real.imshow(a[slicer], cmap=cmap, vmin=-ptp, vmax=ptp) ax_real.set_xticks([]) ax_real.set_yticks([]) ax_fake.imshow(b[slicer], cmap=cmap, vmin=-ptp, vmax=ptp) ax_fake.set_xticks([]) ax_fake.set_yticks([]) ax_diff.imshow(c[slicer], cmap=cmap, vmin=-ptp, vmax=ptp) #cmap=pl.cm.RdBu, vmin=-1.0, vmax=1.0) ax_diff.set_xticks([]) ax_diff.set_yticks([]) if i == 0: ax_real.set_title(r"$\Delta$ Confocal image", fontsize=24) ax_fake.set_title(r"$\Delta$ Model image", fontsize=24) ax_diff.set_title(r"$\Delta$ Difference", fontsize=24) ax_real.set_ylabel('x-y') else: ax_real.set_ylabel('x-z') #========================================================================= #========================================================================= gs1 = GridSpec(1, 3, left=0.05, bottom=0.125, right=0.42, top=0.37, wspace=0.15, hspace=0.05) spos0 = std0[s0.b_pos].reshape(-1, 3)[active0] spos1 = std1[s1.b_pos].reshape(-1, 3)[active1] srad0 = std0[s0.b_rad][active0] srad1 = std1[s1.b_rad][active1] def hist(ax, vals, bins, *args, **kwargs): y, x = np.histogram(vals, bins=bins) x = (x[1:] + x[:-1]) / 2 y /= len(vals) ax.plot(x, y, *args, **kwargs) def pp(ind, tarr, tsim, tcrb, var='x'): bins = 10**np.linspace(-3, 0.0, 30) bin2 = 10**np.linspace(-3, 0.0, 100) bins = np.linspace(0.0, 0.2, 30) bin2 = np.linspace(0.0, 0.2, 100) xlim = (0.0, 0.12) #xlim = (1e-3, 1e0) ylim = (1e-2, 30) ticks = ticker.FuncFormatter( lambda x, pos: '{:0.0f}'.format(np.log10(x))) scaler = lambda x: x #np.log10(x) ax_crb = pl.subplot(gs1[0, ind]) ax_crb.hist(scaler(np.abs(tarr)), bins=bins, normed=True, alpha=0.7, histtype='stepfilled', lw=1) ax_crb.hist(scaler(np.abs(tcrb)).ravel(), bins=bin2, normed=True, alpha=1.0, histtype='step', ls='solid', lw=1.5, color='k') ax_crb.hist(scaler(np.abs(tsim).ravel()), bins=bin2, normed=True, alpha=1.0, histtype='step', lw=3) ax_crb.set_xlabel(r"$\Delta = |%s(t_1) - %s(t_0)|$" % (var, var), fontsize=24) #ax_crb.semilogx() ax_crb.set_xlim(xlim) #ax_crb.semilogy() #ax_crb.set_ylim(ylim) #ax_crb.xaxis.set_major_formatter(ticks) ax_crb.grid(b=False, which='both', axis='both') if ind == 0: lbl(ax_crb, 'B') ax_crb.set_ylabel(r"$P(\Delta)$") else: ax_crb.set_yticks([]) ax_crb.locator_params(axis='x', nbins=3) f, g = 1.5, 1.95 sim = f * sim_crb_diff(spos0[:, 1], spos1[:, 1][link]) crb = g * sim_crb_diff(crb0[0][:, 1][active0], crb1[0][:, 1][active1][link]) pp(0, dpos[:, 1], sim, crb, 'x') sim = f * sim_crb_diff(spos0[:, 0], spos1[:, 0][link]) crb = g * sim_crb_diff(crb0[0][:, 0][active0], crb1[0][:, 0][active1][link]) pp(1, dpos[:, 0], sim, crb, 'z') sim = f * sim_crb_diff(srad0, srad1[link]) crb = g * sim_crb_diff(crb0[1][active0], crb1[1][active1][link]) pp(2, drad, sim, crb, 'a') #ax_crb_r.locator_params(axis='both', nbins=3) #gs1.tight_layout(fig) #========================================================================= #========================================================================= gs2 = GridSpec(2, 2, left=0.48, bottom=0.12, right=0.99, top=0.95, wspace=0.35, hspace=0.35) ax_hist = pl.subplot(gs2[0, 0]) ax_hist.hist(std0[s0.b_pos], bins=np.logspace(-3.0, 0, 50), alpha=0.7, label='POS', histtype='stepfilled') ax_hist.hist(std0[s0.b_rad], bins=np.logspace(-3.0, 0, 50), alpha=0.7, label='RAD', histtype='stepfilled') ax_hist.set_xlim((10**-3.0, 1)) ax_hist.semilogx() ax_hist.set_xlabel(r"$\bar{\sigma}$") ax_hist.set_ylabel(r"$P(\bar{\sigma})$") ax_hist.legend(loc='upper right') lbl(ax_hist, 'C') imdiff = ((s0.get_model_image() - s0.image) / s0._sigma_field)[s0.image_mask == 1.].ravel() mu = imdiff.mean() #sig = imdiff.std() #print mu, sig x = np.linspace(-5, 5, 10000) ax_diff = pl.subplot(gs2[0, 1]) ax_diff.plot(x, 1.0 / np.sqrt(2 * np.pi) * np.exp(-(x - mu)**2 / 2), '-', alpha=0.7, color='k', lw=2) ax_diff.hist(imdiff, bins=1000, histtype='step', alpha=0.7, normed=True) ax_diff.semilogy() ax_diff.set_ylabel(r"$P(\delta)$") ax_diff.set_xlabel(r"$\delta = (M_i - d_i)/\sigma_i$") ax_diff.locator_params(axis='x', nbins=5) ax_diff.grid(b=False, which='minor', axis='y') ax_diff.set_xlim(-5, 5) ax_diff.set_ylim(1e-4, 1e0) lbl(ax_diff, 'D') pos = mu0[s0.b_pos].reshape(-1, 3) rad = mu0[s0.b_rad] mask = analyze.trim_box(s0, pos) pos = pos[mask] rad = rad[mask] gx, gy = analyze.gofr(pos, rad, mu0[s0.b_zscale][0], resolution=5e-2, mask_start=0.5) mask = gx < 5 gx = gx[mask] gy = gy[mask] ax_gofr = pl.subplot(gs2[1, 0]) ax_gofr.plot(gx, gy, '-', lw=1) ax_gofr.set_xlabel(r"$r/a$") ax_gofr.set_ylabel(r"$g(r/a)$") ax_gofr.locator_params(axis='both', nbins=5) #ax_gofr.semilogy() lbl(ax_gofr, 'E') gx, gy = analyze.gofr(pos, rad, mu0[s0.b_zscale][0], method='surface') mask = gx < 5 gx = gx[mask] gy = gy[mask] gy[gy <= 0.] = gy[gy > 0].min() ax_gofrs = pl.subplot(gs2[1, 1]) ax_gofrs.plot(gx, gy, '-', lw=1) ax_gofrs.set_xlabel(r"$r/a$") ax_gofrs.set_ylabel(r"$g_{\rm{surface}}(r/a)$") ax_gofrs.locator_params(axis='both', nbins=5) ax_gofrs.grid(b=False, which='minor', axis='y') #ax_gofrs.semilogy() lbl(ax_gofrs, 'F') ylim = ax_gofrs.get_ylim() ax_gofrs.set_ylim(gy.min(), ylim[1])