def makestate(im, pos, rad, slab=None, mem_level='hi'): """ Workhorse for creating & optimizing states with an initial centroid guess. This is an example function that works for a particular microscope. For your own microscope, you'll need to change particulars such as the psf type and the orders of the background and illumination. Parameters ---------- im : :class:`~peri.util.RawImage` A RawImage of the data. pos : [N,3] element numpy.ndarray. The initial guess for the N particle positions. rad : N element numpy.ndarray. The initial guess for the N particle radii. slab : :class:`peri.comp.objs.Slab` or None, optional If not None, a slab corresponding to that in the image. Default is None. mem_level : {'lo', 'med-lo', 'med', 'med-hi', 'hi'}, optional A valid memory level for the state to control the memory overhead at the expense of accuracy. Default is `'hi'` Returns ------- :class:`~peri.states.ImageState` An ImageState with a linked z-scale, a ConfocalImageModel, and all the necessary components with orders at which are useful for my particular test case. """ if slab is not None: o = comp.ComponentCollection( [ objs.PlatonicSpheresCollection(pos, rad, zscale=zscale), slab ], category='obj' ) else: o = objs.PlatonicSpheresCollection(pos, rad, zscale=zscale) p = exactpsf.FixedSSChebLinePSF() npts, iorder = _calc_ilm_order(im.get_image().shape) i = ilms.BarnesStreakLegPoly2P1D(npts=npts, zorder=iorder) b = ilms.LegendrePoly2P1D(order=(9 ,3, 5), category='bkg') c = comp.GlobalScalar('offset', 0.0) s = states.ImageState(im, [o, i, b, c, p]) runner.link_zscale(s) if mem_level != 'hi': s.set_mem_level(mem_level) opt.do_levmarq(s, ['ilm-scale'], max_iter=1, run_length=6, max_mem=1e4) return s
def table_psfs(): np.random.seed(12) s = create_image() sh = s.psf.shape lpsfs = [ psfs.IdentityPSF(shape=sh, params=np.array([0.0])), psfs.AnisotropicGaussian(shape=sh, params=(2.0, 1.0, 3.0)), psfs.Gaussian4DLegPoly(shape=sh, order=(3, 3, 3)), exactpsf.FixedSSChebLinePSF( shape=sh, zrange=(0, sh[0]), cheb_degree=3, cheb_evals=6, support_size=FIXEDSS, zslab=10., cutoffval=1. / 255, measurement_iterations=3, ), exactpsf.FixedSSChebLinePSF( shape=sh, zrange=(0, sh[0]), cheb_degree=6, cheb_evals=8, support_size=FIXEDSS, zslab=10., cutoffval=1. / 255, measurement_iterations=3, ), ] names = [ r'Identity', r'Gaussian$(x,y)$', r'Gaussian$(x,y,z,z^{\prime})$', r'Cheby linescan (3,6)', r'Cheby linescan (6,8)', ] def vary_func(s, data): s.set_psf(data) return table(s, lpsfs, names, vary_func)
def create_img(): """Creates an image, as a `peri.util.Image`, which is similar to the image in the tutorial""" # 1. particles + coverslip rad = 0.5 * np.random.randn(POS.shape[0]) + 4.5 # 4.5 +- 0.5 px particles part = objs.PlatonicSpheresCollection(POS, rad, zscale=0.89) slab = objs.Slab(zpos=4.92, angles=(-4.7e-3, -7.3e-4)) objects = comp.ComponentCollection([part, slab], category='obj') # 2. psf, ilm p = exactpsf.FixedSSChebLinePSF(kfki=1.07, zslab=-29.3, alpha=1.17, n2n1=0.98, sigkf=-0.33, zscale=0.89, laser_wavelength=0.45) i = ilms.BarnesStreakLegPoly2P1D(npts=(16,10,8,4), zorder=8) b = ilms.LegendrePoly2P1D(order=(7,2,2), category='bkg') off = comp.GlobalScalar(name='offset', value=-2.11) mdl = models.ConfocalImageModel() st = states.ImageState(util.NullImage(shape=[48,64,64]), [objects, p, i, b, off], mdl=mdl, model_as_data=True) b.update(b.params, BKGVALS) i.update(i.params, ILMVALS) im = st.model + np.random.randn(*st.model.shape) * 0.03 return util.Image(im)
im_ilm = util.RawImage('./ilm_test.tif', tile=util.Tile([48, 0, 0], [49, 100, 100])) # also located in the scripts folder # Looking at the image, the illlumination is very stripey, due to the line-scan # nature of our confocal. To account for this, we use a stripe-based ilm: ilm = ilms.BarnesStreakLegPoly2P1D(npts=(50, 30, 20, 13, 7, 7, 7), zorder=1) # (we only use a zorder of 1 since we've truncated to 1 pixel in z). # Our real model will use a point-spread function that will blur out the ilm # field slightly more. So we check the fit with a model that includes the # type of point-spread function that we will use. A model that blur with a # point-spread function takes considerably more time to evaluate than a # SmoothFieldModel, so if you're not sure if your ilm is high enough order # you should first check with a faster SmoothFieldModel. psf = exactpsf.FixedSSChebLinePSF() st = states.ImageState(im_ilm, [ilm, psf], mdl=models.BlurredFieldModel()) opt.do_levmarq(st, st.params) # Plotting the residuals shows that they're good, aside from scan noise # inherent to the line CCD camera: plot_averaged_residuals(st) # Next, we include the coverslip slide. To do this we first re-set the tile on # our raw image to the full image: im_ilm.set_tile(util.Tile([0, 0, 0], [60, 100, 100])) # We then create a coverslip object: slab = objs.Slab(zpos=35.0, category='obj') # We also need our illumination to have a z-dependence now. Since we already
particle_radii = 5.0 particles = objs.PlatonicSpheresCollection(particle_positions, particle_radii) from peri.comp import comp objects = comp.ComponentCollection([particles, coverslip], category='obj') from peri.comp import ilms illumination = ilms.BarnesStreakLegPoly2P1D(npts=(16, 10, 8, 4), zorder=8) background = ilms.LegendrePoly2P1D(order=(7, 2, 2), category='bkg') offset = comp.GlobalScalar(name='offset', value=0.) from peri.comp import exactpsf point_spread_function = exactpsf.FixedSSChebLinePSF() from peri import models model = models.ConfocalImageModel() print(model) from peri import states st = states.ImageState( im, [objects, illumination, background, point_spread_function, offset], mdl=model)
def table(): s = create_image(identity=True) lpoly = [0.0, 0.0, 0.01, 0.05, 0.10] dnames = ['0.0', '0.0', '0.01', '0.05', '0.10'] lilms = [ ilms.LegendrePoly2P1D(shape=s.ilm.shape, order=(1, 1, 1)), ilms.LegendrePoly2P1D(shape=s.ilm.shape, order=(3, 3, 3)), ilms.BarnesStreakLegPoly2P1DX3(shape=s.ilm.shape, order=(1, 1, 1), npts=(10, 5)), ilms.BarnesStreakLegPoly2P1DX3(shape=s.ilm.shape, order=(1, 1, 2), npts=(30, 10)), ilms.BarnesStreakLegPoly2P1DX3(shape=s.ilm.shape, order=s.ilm.order, npts=(30, 10, 5)), ] lnames = [ r'Legendre 2+1D (0,0,0)', r'Legendre 2+1D (2,2,2)', r'Barnes (10, 5), $N_z=1$', r'Barnes (30, 10), $N_z=2$', r'Barnes (30, 10, 5), $N_z=3$', ] lpsfs = [ psfs.IdentityPSF(shape=s.psf.shape, params=np.array([0.0])), psfs.AnisotropicGaussian(shape=s.psf.shape, params=(2.0, 1.0, 3.0)), psfs.Gaussian4DLegPoly(shape=s.psf.shape, order=(3, 3, 3)), exactpsf.FixedSSChebLinePSF( shape=s.psf.shape, zrange=(0, s.psf.shape[0]), cheb_degree=3, cheb_evals=6, support_size=FIXEDSS, zslab=10., cutoffval=1. / 255, measurement_iterations=3, ), exactpsf.FixedSSChebLinePSF( shape=s.psf.shape, zrange=(0, s.psf.shape[0]), cheb_degree=6, cheb_evals=8, support_size=FIXEDSS, zslab=10., cutoffval=1. / 255, measurement_iterations=3, ), ] pnames = [ r'Identity', r'Gaussian$(x,y)$', r'Gaussian$(x,y,z,z^{\prime})$', r'Cheby linescan (3,6)', r'Cheby linescan (6,8)', ] results = OrderedDict() for i in xrange(len(lpoly)): print dnames[i], lnames[i], pnames[i] poly = lpoly[i] ilm = lilms[i] psf = lpsfs[i] s = create_image(polydispersity=poly) s.set_ilm(ilm) s.set_psf(psf) if isinstance(s.ilm, ilms.BarnesStreakLegPoly2P1DX3): s.ilm.randomize_parameters(ptp=0.4, vmax=1.0, fourier=False) s.reset() s.model_to_true_image() pos1 = trackpy(s) pos0 = s.obj.pos.copy() s.obj.set_pos_rad(pos1, s.obj.rad.mean() * np.ones(pos1.shape[0])) s.reset() slicer = s.get_difference_image().shape[0] / 2 results[i] = ( dnames[i], lnames[i], pnames[i], s.get_difference_image()[slicer].copy(), pos0, pos1, ) return results