def pxint(radius=8, factor=8, dx=np.array([0, 0, 0])): # the factor of coarse-graining, goal particle size, and larger size f = factor goalsize = radius goalpsf = np.array([2.0, 1.0, 3.0]) bigsize = goalsize * f bigpsf = goalpsf * np.array([f, f, 1]) s0 = init.create_single_particle_state(imsize=np.array( (4 * goalsize, 4 * bigsize, 4 * bigsize)), radius=bigsize, psfargs={ 'params': bigpsf, 'error': 1e-6 }, stateargs={'zscale': 1.0 * f}) s0.obj.pos += np.array([0, 1, 1]) * (f - 1.0) / 2.0 s0.obj.pos += np.array([1, f, f]) * dx s0.reset() # coarse-grained image sl = np.s_[s0.pad:-s0.pad, s0.pad:-s0.pad, s0.pad:-s0.pad] m = s0.get_model_image()[sl] # indices for coarse-graining e = m.shape[1] i = np.linspace(0, e / f, e, endpoint=False).astype('int') j = np.linspace(0, e / f, e / f, endpoint=False).astype('int') z, y, x = np.meshgrid(*(j, i, i), indexing='ij') ind = x + e * y + e * e * z # finally, c-g'ed image cg = nd.mean(m, labels=ind, index=np.unique(ind)).reshape(e / f, e / f, e / f) # place that into a new image at the expected parameters s = init.create_single_particle_state(imsize=4 * goalsize, sigma=0.05, radius=goalsize, psfargs={ 'params': goalpsf, 'error': 1e-6 }) s.obj.pos += dx s.reset() # measure the true inferred parameters return s, cg
def fit_single_particle_psf(psf_scale, samples=100, imsize=64, sigma=0.05): terrors = [] berrors = [] crbs = [] psf0 = np.array([2.0, 1.0, 4.0]) for scale in psf_scale: print '=' * 79 print 'scale =', scale s = init.create_single_particle_state(imsize, radius=5.0, sigma=0.05, psfargs={'params': scale * psf0}) p = s.state[s.b_pos].reshape(-1, 3).copy() bl = s.explode(s.b_pos) crbs.append( np.sqrt(np.diag(np.linalg.inv( s.fisher_information(blocks=bl)))).reshape(-1, 3)) tmp_tp, tmp_bf = [], [] for i in xrange(samples): print i bench.jiggle_particles(s, pos=p) t = bench.trackpy(s) b = bench.bamfpy_positions(s, sweeps=30) tmp_tp.append(bench.error(s, t)) tmp_bf.append(bench.error(s, b)) terrors.append(tmp_tp) berrors.append(tmp_bf) return np.array(crbs), np.array(terrors), np.array(berrors)
def fit_single_particle_rad(radii, samples=100, imsize=64, sigma=0.05): terrors = [] berrors = [] crbs = [] for rad in radii: print '=' * 79 print 'radius =', rad s = init.create_single_particle_state(imsize, radius=rad, sigma=0.05) p = s.state[s.b_pos].reshape(-1, 3).copy() bl = s.explode(s.b_pos) crbs.append( np.sqrt(np.diag(np.linalg.inv( s.fisher_information(blocks=bl)))).reshape(-1, 3)) tmp_tp, tmp_bf = [], [] for i in xrange(samples): print i bench.jiggle_particles(s, pos=p) t = bench.trackpy(s) b = bench.bamfpy_positions(s, sweeps=30) tmp_tp.append(bench.error(s, t)) tmp_bf.append(bench.error(s, b)) terrors.append(tmp_tp) berrors.append(tmp_bf) return np.array(crbs), np.array(terrors), np.array(berrors)
def diffusion(diffusion_constant=0.2, exposure_time=0.05, samples=200): """ See `diffusion_correlated` for information related to units, etc """ radius = 5 psfsize = np.array([2.0, 1.0, 3.0]) # create a base image of one particle s0 = init.create_single_particle_state(imsize=4 * radius, radius=radius, psfargs={ 'params': psfsize, 'error': 1e-6 }) # add up a bunch of trajectories finalimage = 0 * s0.get_model_image()[s0.inner] position = 0 * s0.obj.pos[0] for i in xrange(samples): offset = np.sqrt( 6 * diffusion_constant * exposure_time) * np.random.randn(3) s0.obj.pos[0] = np.array(s0.image.shape) / 2 + offset s0.reset() finalimage += s0.get_model_image()[s0.inner] position += s0.obj.pos[0] finalimage /= float(samples) position /= float(samples) # place that into a new image at the expected parameters s = init.create_single_particle_state(imsize=4 * radius, sigma=0.05, radius=radius, psfargs={ 'params': psfsize, 'error': 1e-6 }) s.reset() # measure the true inferred parameters return s, finalimage, position
def zjitter(jitter=0.0, radius=5): """ scan jitter is in terms of the fractional pixel difference when moving the laser in the z-direction """ psfsize = np.array([2.0, 1.0, 3.0]) # create a base image of one particle s0 = init.create_single_particle_state(imsize=4 * radius, radius=radius, psfargs={ 'params': psfsize, 'error': 1e-6 }) sl = np.s_[s0.pad:-s0.pad, s0.pad:-s0.pad, s0.pad:-s0.pad] # add up a bunch of trajectories finalimage = 0 * s0.get_model_image()[sl] position = 0 * s0.obj.pos[0] for i in xrange(finalimage.shape[0]): offset = jitter * np.random.randn(3) * np.array([1, 0, 0]) s0.obj.pos[0] = np.array(s0.image.shape) / 2 + offset s0.reset() finalimage[i] = s0.get_model_image()[sl][i] position += s0.obj.pos[0] position /= float(finalimage.shape[0]) # place that into a new image at the expected parameters s = init.create_single_particle_state(imsize=4 * radius, sigma=0.05, radius=radius, psfargs={ 'params': psfsize, 'error': 1e-6 }) s.reset() # measure the true inferred parameters return s, finalimage, position
def create_comparison_state(image, position, radius=5.0, snr=20, method='constrained-cubic', extrapad=2, zscale=1.0): """ Take a platonic image and position and create a state which we can use to sample the error for peri. Also return the blurred platonic image so we can vary the noise on it later """ # first pad the image slightly since they are pretty small image = common.pad(image, extrapad, 0) # place that into a new image at the expected parameters s = init.create_single_particle_state(imsize=np.array(image.shape), sigma=1.0/snr, radius=radius, psfargs={'params': np.array([2.0, 1.0, 3.0]), 'error': 1e-6, 'threads': 2}, objargs={'method': method}, stateargs={'sigmapad': False, 'pad': 4, 'zscale': zscale}) s.obj.pos[0] = position + s.pad + extrapad s.reset() s.model_to_true_image() timage = 1-np.pad(image, s.pad, mode='constant', constant_values=0) timage = s.psf.execute(timage) return s, timage[s.inner]
import numpy as np import pylab as pl from peri.test import init crbs = [] rads = np.arange(1, 10, 1./5) rads = np.linspace(1, 10, 39) rads = np.logspace(0, 1, 50) s = init.create_single_particle_state(imsize=64, radius=1, sigma=0.05) blocks = s.blocks_particle(0) for rad in rads: print "Radius", rad s.update(blocks[-1], np.array([rad])) crb = [] for block in blocks: crb.append( s.fisher_information([block])[0,0] ) crbs.append(crb) crbs = 1.0 / np.sqrt(np.array(crbs)) pl.figure() pl.loglog(rads, crbs[:,0], 'o-', lw=1, label='pos-z') pl.loglog(rads, crbs[:,1], 'o-', lw=1, label='pos-y') pl.loglog(rads, crbs[:,2], 'o-', lw=1, label='pos-x') pl.loglog(rads, crbs[:,3], 'o-', lw=1, label='rad') pl.legend(loc='upper right') pl.xlabel("Radius")
import numpy as np import pylab as pl from peri.test import init radius = 5.0 sigma = 0.05 crbs = [] s = init.create_single_particle_state(imsize=64, radius=radius, sigma=sigma, stateargs={'sigmapad': False}) positions = np.linspace(s.pad - 1.5 * radius, s.pad + 2 * radius, 50) blocks = s.blocks_particle(0) for pos in positions: print "Position", pos s.update(blocks[2], np.array([pos])) crb = [] for block in blocks: crb.append(s.fisher_information([block])[0, 0]) crbs.append(crb) crbs = 1.0 / np.sqrt(np.array(crbs)) pl.figure() pl.plot((positions - s.pad) / radius, crbs[:, 0], 'o-', lw=1, label='pos-z') pl.plot((positions - s.pad) / radius, crbs[:, 1], 'o-', lw=1, label='pos-y') pl.plot((positions - s.pad) / radius, crbs[:, 2], 'o-', lw=1, label='pos-x')