Esempio n. 1
0
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
Esempio n. 2
0
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)
Esempio n. 3
0
bkg = ilms.LegendrePoly2P1D(order=(7, 3, 5), category='bkg', operation='+')

# This detail is important not so much for its effect on the reconstruction
# of this blank image, but for what it illustrates -- while it is practically
# impossible to implement an exact generative model, careful thinking can allow
# for a model that is almost equivalent to the exact answer. To answer how
# much this approximation matters for measuring particle properties in an,
# we could generate an image with a more exact representation of these psf
# long tails and then fit it with our more approximate model.
# Incidentally, while the support of our psf is finite, it's quite large --
# 35 pixels in z, or 44% of the image in z! If we wanted, we could increase
# this by changing the ``support_size`` keyword when calling
# exactpsf.FixedSSChebLinePSF.

# Finally, we create an offset:
off = comp.GlobalScalar('offset', 0.0)
st = states.ImageState(im_ilm, [ilm_z, off, psf, bkg, slab])
# As an illustration, we'll optimize certain parameters first for speed.
# Since we know that our xy-ilm parameters are the same, we'll start by
# optimizing the background and the ilm-z- params.
opt.do_levmarq(st,
               st.get('bkg').params +
               ['ilm-z-{}'.format(i) for i in xrange(ilm_z.zorder)],
               max_iter=2)
# Looking at this with the OrthoManipulator it already looks good, but we do
# a full optimization to ensure that we're at the best fit.
opt.do_levmarq(st, st.params, exptol=1e-5, errtol=1e-3)
# (this will take some time; half an hour or so on my machine)

# Finally, plotting the average along different directions looks good:
Esempio n. 4
0
coverslip = objs.Slab(zpos=6)
particle_positions = numpy.load('../../Downloads/particle-positions.npy')
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],