예제 #1
0
def test_Spheres_rotation():
    s1 = Sphere(n = 1.59, r = 1, center = [1, 0, 0])
    s2 = Sphere(n = 1.59, r = 1, center = [-1, 0, 1])
    sc = Spheres(scatterers = [s1, s2])

    sc2 = sc.rotated(-np.pi/2, 0, 0)

    assert_equal(sc.scatterers[0].r, sc2.scatterers[0].r)
    assert_equal(sc.scatterers[1].r, sc2.scatterers[1].r)
    assert_equal(sc.scatterers[0].n, sc2.scatterers[0].n)
    assert_equal(sc.scatterers[1].n, sc2.scatterers[1].n)
    assert_almost_equal([0, -1, 0], sc2.scatterers[0].center)
    assert_almost_equal([0, 1, 1], sc2.scatterers[1].center)
예제 #2
0
def test_Spheres_translation():
    s1 = Sphere(n = 1.59, r = 5, center=[1, -1, 10])
    s2 = Sphere(n = 1.59, r = 1, center=[0,0,0])
    sc = Spheres(scatterers = [s1, s2])

    sc2 = sc.translated(1, 1, 1)

    assert_equal(sc.scatterers[0].r, sc2.scatterers[0].r)
    assert_equal(sc.scatterers[1].r, sc2.scatterers[1].r)
    assert_equal(sc.scatterers[0].n, sc2.scatterers[0].n)
    assert_equal(sc.scatterers[1].n, sc2.scatterers[1].n)
    assert_equal([2, 0, 11], sc2.scatterers[0].center)
    assert_equal([1, 1, 1], sc2.scatterers[1].center)
예제 #3
0
def test_Get_Alpha():

    #checking get_alpha function
    sc = Spheres([Sphere(n = 1.58, r = par(0.5e-6), center = np.array([10., 10., 20.])),
              Sphere(n = 1.58, r = par(0.5e-6), center = np.array([9., 11., 21.]))])
    model = Model(sc, calc_holo, alpha = par(.7,[.6,1]))

    sc = Spheres([Sphere(n = 1.58, r = par(0.5e-6), center = np.array([10., 10., 20.])),
              Sphere(n = 1.58, r = par(0.5e-6), center = np.array([9., 11., 21.]))])
    model2 = Model(sc, calc_holo)

    assert_equal(model.get_alpha(model.parameters).guess, 0.7)
    assert_equal(model.get_alpha(model.parameters).name, 'alpha')
    assert_equal(model2.get_alpha(model2.parameters), 1.0)
예제 #4
0
def test_nonlinearity():
    # look at superposition of scattering from two large particles;
    # make sure that this is *not equal* to sum of holograms from
    # individual scatterers (scattered intensity should be
    # non-negligible for this case)

    x2 = x*2
    y2 = y*2
    z2 = z*2
    scaling_alpha = 1.0
    r = wavelen    # order of wavelength

    sphere1 = Sphere(n=n, r=r, center = (x, y, z))
    sphere2 = Sphere(n=n, r=r, center = (x2, y2, z2))

    sc = Spheres(scatterers = [sphere1, sphere2])

    holo_1 = calc_holo(xschema, sphere1, index, wavelen, illum_polarization=xpolarization, scaling=scaling_alpha)
    holo_2 = calc_holo(xschema, sphere2, index, wavelen, illum_polarization=xpolarization, scaling=scaling_alpha)
    holo_super = calc_holo(xschema, sc, index, wavelen, xpolarization, scaling=scaling_alpha, theory=Mie)

    # test nonlinearity by subtracting off individual holograms
    try:
        assert_array_almost_equal(holo_super - holo_1 + 1, holo_2)
    except AssertionError:
        pass    # no way to do "assert array not equal" in numpy.testing
    else:
        raise AssertionError("Holograms computed for "
                             "wavelength-scale scatterers should "
                             "not superpose linearly")
예제 #5
0
def test_Mie_multiple():
    s1 = Sphere(n = 1.59, r = 5e-7, center = (1e-6, -1e-6, 10e-6))
    s2 = Sphere(n = 1.59, r = 1e-6, center=[8e-6,5e-6,5e-6])
    s3 = Sphere(n = 1.59+0.0001j, r = 5e-7, center=[5e-6,10e-6,3e-6])
    sc = Spheres(scatterers=[s1, s2, s3])
    thry = Mie(False)

    schema = yschema
    fields = calc_field(schema, sc, index, wavelen, ypolarization, thry)

    verify(fields, 'mie_multiple_fields')
    calc_intensity(schema, sc, index, wavelen, ypolarization, thry)

    holo = calc_holo(schema, sc, index, wavelen, theory=thry)
    verify(holo, 'mie_multiple_holo')
    # should throw exception when fed a ellipsoid
    el = Ellipsoid(n = 1.59, r = (1e-6, 2e-6, 3e-6), center=[8e-6,5e-6,5e-6])
    with assert_raises(TheoryNotCompatibleError) as cm:
        calc_field(schema, el, index, wavelen, theory=Mie)
    assert_equal(str(cm.exception), "Mie scattering theory can't handle "
                 "scatterers of type Ellipsoid")
    assert_raises(TheoryNotCompatibleError, calc_field, schema, el, index, wavelen, xpolarization, Mie)
    assert_raises(TheoryNotCompatibleError, calc_intensity,
                  schema, el, index, wavelen, xpolarization, Mie)
    assert_raises(TheoryNotCompatibleError, calc_holo, schema, el, index, wavelen, xpolarization, Mie)
예제 #6
0
def test_Spheres_construction_typechecking():
    # heterogeneous composite should raise exception, since a
    # sphere cluster must contain only Spheres
    s1 = Sphere(n = 1.59, r = 5e-7, center = (1e-6, -1e-6, 10e-6))
    s2 = Sphere(n = 1.59, r = 1e-6, center=[0,0,0])
    s3 = Sphere(n = 1.59+0.0001j, r = 5e-7, center=[5e-6,0,0])
    cs = Ellipsoid(n=1.59+0.0001j, r=(5e-7, 5e-7, 8e-7),
                      center=[-5e-6, 0,0])
    sc = Spheres(scatterers=[s1, s2, s3, cs])
예제 #7
0
def test_Spheres_parameters():
    s1 = Sphere(n = 1.59, r = 5e-7, center=[1e-6, -1e-6, 10e-6])
    s2 = Sphere(n = 1.59, r = 1e-6, center=[0,0,0])
    sc = Spheres(scatterers = [s1, s2])

    assert_equal(sc.parameters, dict([('0:center', [1e-6, -1e-6, 1.0e-05]),
                                      ('0:n', 1.59), ('0:r', 5e-07),
                                      ('1:center', [0, 0, 0]),
                                      ('1:n', 1.59), ('1:r', 1e-06)]))

    sc2 = sc.from_parameters(sc.parameters)

    assert_equal(sc.scatterers[0].r, sc2.scatterers[0].r)
    assert_equal(sc.scatterers[1].r, sc2.scatterers[1].r)
    assert_equal(sc.scatterers[0].n, sc2.scatterers[0].n)
    assert_equal(sc.scatterers[1].n, sc2.scatterers[1].n)
    assert_equal(sc.scatterers[0].center, sc2.scatterers[0].center)
    assert_equal(sc.scatterers[1].center, sc2.scatterers[1].center)
예제 #8
0
def test_Tying():

    #tied parameters
    n1 = par(1.59)
    sc = Spheres([Sphere(n = n1, r = par(0.5e-6), center = np.array([10., 10., 20.])),
              Sphere(n = n1, r = par(0.5e-6), center = np.array([9., 11., 21.]))])
    model = Model(sc, calc_holo, alpha = par(.7,[.6,1]))

    assert_equal(model.parameters[0].guess, 1.59)
    assert_equal(model.parameters[1].guess, 5e-7)
    assert_equal(len(model.parameters),4)
예제 #9
0
def test_fit_superposition():
    """
    Fit Mie superposition to a calculated hologram from two spheres
    """
    # Make a test hologram
    optics = Optics(wavelen=6.58e-07,
                    index=1.33,
                    polarization=[0.0, 1.0],
                    divergence=0,
                    spacing=None,
                    train=None,
                    mag=None,
                    pixel_scale=[2 * 2.302e-07, 2 * 2.302e-07])

    s1 = Sphere(n=1.5891 + 1e-4j, r=.65e-6, center=(1.56e-05, 1.44e-05, 15e-6))
    s2 = Sphere(n=1.5891 + 1e-4j, r=.65e-6, center=(3.42e-05, 3.17e-05, 10e-6))
    sc = Spheres([s1, s2])
    alpha = .629

    theory = Mie(optics, 100)
    holo = theory.calc_holo(sc, alpha)

    # Now construct the model, and fit
    parameters = [
        Parameter(name='x0', guess=1.6e-5, limit=[0, 1e-4]),
        Parameter('y0', 1.4e-5, [0, 1e-4]),
        Parameter('z0', 15.5e-6, [0, 1e-4]),
        Parameter('r0', .65e-6, [0.6e-6, 0.7e-6]),
        Parameter('nr', 1.5891, [1, 2]),
        Parameter('x1', 3.5e-5, [0, 1e-4]),
        Parameter('y1', 3.2e-5, [0, 1e-4]),
        Parameter('z1', 10.5e-6, [0, 1e-4]),
        Parameter('r1', .65e-6, [0.6e-6, 0.7e-6])
    ]

    def make_scatterer(x0, x1, y0, y1, z0, z1, r0, r1, nr):
        s = Spheres([
            Sphere(center=(x0, y0, z0), r=r0, n=nr + 1e-4j),
            Sphere(center=(x1, y1, z1), r=r1, n=nr + 1e-4j)
        ])
        return s

    model = Model(parameters,
                  Mie,
                  make_scatterer=make_scatterer,
                  alpha=Parameter('alpha', .63, [.5, 0.8]))
    result = fit(model, holo)

    assert_obj_close(result.scatterer, sc)
    assert_approx_equal(result.alpha, alpha, significant=4)
    assert_equal(result.model, model)
    assert_read_matches_write(result)
예제 #10
0
def test_RigidCluster():
    # test construction
    s1 = Sphere(n = 1, center = (1, 0, 0))
    s2 = Sphere(n = 2, center = (-1, 0, 0))
    s3 = Sphere(n = 3, center = (0, 1,0))
    s4 = Sphere(n = 4, center = (0, -1, 0))
    base = Spheres([s1, s2, s3, s4])
    rc = RigidCluster(base)
    assert_obj_close(rc.scatterers, base.scatterers)
    assert_raises(InvalidScatterer, RigidCluster,s1)

    # test transformation
    ts1 = Sphere(n = 1, center = [-1/np.sqrt(2)+1, 2., -1/np.sqrt(2)+3])
    ts2 = Sphere(n = 2, center = [1/np.sqrt(2)+1, 2., 1/np.sqrt(2)+3])
    ts3 = Sphere(n = 3, center = [-1/np.sqrt(2)+1,2., +1/np.sqrt(2)+3])
    ts4 = Sphere(n = 4, center = [1/np.sqrt(2)+1, 2., -1/np.sqrt(2)+3])
    trans = Spheres([ts1,ts2,ts3,ts4])
    trc = RigidCluster(base, rotation=(np.pi/4,np.pi/2,np.pi/2),translation=(1,2,3))
    assert_obj_close(trc.scatterers,trans.scatterers)

    # test parameters, from_parameters
    assert_obj_close(trc.from_parameters(trc.parameters), trans)
예제 #11
0
    def test_mielens_multiple_returns_nonzero(self):
        scatterers = [
            Sphere(n=1.59, r=5e-7, center=(1e-6, -1e-6, 10e-6)),
            Sphere(n=1.59, r=1e-6, center=[8e-6, 5e-6, 5e-6]),
            Sphere(n=1.59 + 0.0001j, r=5e-7, center=[5e-6, 10e-6, 3e-6]),
            ]
        sphere_collection = Spheres(scatterers=scatterers)
        theory = MieLens()

        schema = yschema
        holo = calc_holo(schema, sphere_collection, index, wavelen,
                         theory=theory)
        self.assertTrue(holo is not None)
        self.assertTrue(holo.values.std() > 0)
예제 #12
0
def test_constraint():
    sch = ImageSchema(100)
    with warnings.catch_warnings():
        # TODO: we should really only supress overlap warnings here,
        # but I am too lazy to figure it out right now, and I don't
        # think we are likely to hit warnings here that won't get
        # caught elsewhere -tgd 2013-12-01
        warnings.simplefilter("ignore")
        spheres = Spheres([Sphere(r=.5, center=(0,0,0)),
                           Sphere(r=.5, center=(0,0,par(.2)))])
        model = Model(spheres, Multisphere.calc_holo, constraints=limit_overlaps())
        coster = CostComputer(sch, model)
        cost = coster._calc({'1:Sphere.center[2]' : .2})
        assert_equal(cost, np.ones_like(sch)*np.inf)
예제 #13
0
def test_Spheres_construction():

    # cluster of multiple spheres
    s1 = Sphere(n = 1.59, r = 5e-7, center = (1e-6, -1e-6, 10e-6))
    s2 = Sphere(n = 1.59, r = 1e-6, center=[0,0,0])
    s3 = Sphere(n = 1.59+0.0001j, r = 5e-7, center=[5e-6,0,0])
    sc = Spheres(scatterers=[s1, s2, s3])

    # test attribute access
    sc.n
    sc.n_real
    sc.n_imag
    sc.r
    sc.x
    sc.y
    sc.z
    sc.centers
    sc.center
예제 #14
0
def test_linearity():
    # look at superposition of scattering from two point particles;
    # make sure that this is sum of holograms from individual point
    # particles (scattered intensity should be negligible for this
    # case)

    x2 = x*2
    y2 = y*2
    z2 = z*2
    scaling_alpha = 1.0
    r = 1e-2*wavelen    # something much smaller than wavelength

    sphere1 = Sphere(n=n, r=r, center = (x, y, z))
    sphere2 = Sphere(n=n, r=r, center = (x2, y2, z2))

    sc = Spheres(scatterers = [sphere1, sphere2])

    holo_1 = calc_holo(xschema, sphere1, index, wavelen, xpolarization, scaling=scaling_alpha)
    holo_2 = calc_holo(xschema, sphere2, index, wavelen, xpolarization, scaling=scaling_alpha)
    holo_super = calc_holo(xschema, sc, index, wavelen, xpolarization, theory=Mie, scaling=scaling_alpha)

    # make sure we're not just looking at uniform arrays (could
    # happen if the size is set too small)
    try:
        assert_array_almost_equal(holo_1, holo_2, decimal=12)
    except AssertionError:
        pass    # no way to do "assert array not equal" in numpy.testing
    else:
        raise AssertionError("Hologram computed for point particle" +
                             " looks suspiciously close to having" +
                             " no fringes")

    # Test linearity by subtracting off individual holograms.
    # This should recover the other hologram
    assert_array_almost_equal(holo_super - holo_1 + 1, holo_2)
    assert_array_almost_equal(holo_super - holo_2 + 1, holo_1)
예제 #15
0
    def _scsmfo_setup(self, scatterer, medium_wavevec, medium_index):
        """
        Given multiple spheres, calculate amn coefficients for scattered
        field expansion in VSH using SCSMFO.

        Parameters
        ----------
        scatterer : :mod:`.scatterer` object
            scatterer or list of scatterers to compute field for

        Returns
        -------
        amn : arrays of field expansion coefficients

        """
        if isinstance(scatterer,Sphere):
            scatterer=Spheres([scatterer])
        elif not isinstance(scatterer, Spheres):
            raise TheoryNotCompatibleError(self, scatterer)
        # check for spheres being uniform
        for sph in scatterer.scatterers:
            if not np.isscalar(sph.n):
                raise TheoryNotCompatibleError(self, scatterer, "Multisphere" +
                                               " cannot compute scattering" +
                                               " from layered particles.")

        # check that the parameters are in a range where the multisphere
        # expansion will work
        for s in scatterer.scatterers:
            if s.r * medium_wavevec > 1e3:
                raise InvalidScatterer(s, "radius too large, field "+
                                            "calculation would take forever")

        # switch to centroid weighted coordinate system tmatrix code expects
        # and nondimensionalize
        centers = (scatterer.centers - scatterer.centers.mean(0)) * medium_wavevec

        m = scatterer.n / medium_index

        if (centers > 1e4).any():
            raise InvalidScatterer(scatterer, "Particle separation "
                                        "too large, calculation would take forever")
        with SuppressOutput(suppress_output=self.suppress_fortran_output):
            # The fortran code uses oppositely directed z axis (they
            # have laser propagation as positive, we have it negative),
            # so we multiply the z coordinate by -1 to correct for that.
            _, lmax, amn0, converged = scsmfo_min.amncalc(
                1, centers[:,0],  centers[:,1],
                -1.0 * centers[:,2],  m.real, m.imag,
                scatterer.r * medium_wavevec, self.niter, self.eps,
                self.qeps1, self.qeps2,  self.meth, (0,0))

        # converged == 1 if the SCSMFO iterative solver converged
        # f2py converts F77 LOGICAL to int
        if not converged:
            raise MultisphereFailure()

        # chop off unused parts of amn0, the fortran code currently has a hard
        # coded number of parameters so it will return too many coefficients.
        # We truncate here to reduce the length of stuff we have to compute with
        # later.
        limit = lmax**2 + 2*lmax
        amn = amn0[:, 0:limit, :]

        if np.isnan(amn).any():
            raise MultisphereFailure()

        return amn, lmax
예제 #16
0
 def make_scatterer(x0, x1, y0, y1, z0, z1, r0, r1, nr0, nr1):
     s = Spheres([
             Sphere(center = (x0, y0, z0), r=r0, n = nr0+1e-5j),
             Sphere(center = (x1, y1, z1), r=r1, n = nr1+1e-5j)])
     return s
예제 #17
0
def test_Spheres_ovelap_checking():
    s1 = Sphere(n = 1.59, r = 5e-7, center=(1e-6, -1e-6, 10e-6))
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter('always', OverlapWarning)
        sc = Spheres([s1, s1, s1])
        assert len(w) > 0
예제 #18
0
import holopy as hp
from holopy.scattering.theory import Mie
from holopy.scattering.scatterer import Sphere, Spheres
from holopy.core import ImageSchema, Optics
schema = ImageSchema(shape=100,
                     spacing=.1,
                     optics=Optics(wavelen=.660,
                                   index=1.33,
                                   polarization=[1, 0]))
s1 = Sphere(center=(5, 5, 5), n=1.59, r=.5)
s2 = Sphere(center=(4, 4, 5), n=1.59, r=.5)
collection = Spheres([s1, s2])
holo = Mie.calc_holo(collection, schema)
hp.show(holo)
예제 #19
0
import xarray as xr
from numpy.testing import assert_allclose, assert_equal
from nose.plugins.attrib import attr

from holopy.core import detector_grid, detector_points
from holopy.core.metadata import update_metadata, flat
from holopy.scattering.theory.scatteringtheory import ScatteringTheory
from holopy.scattering.theory import Mie
from holopy.scattering.scatterer import Sphere, Spheres, Ellipsoid
from holopy.scattering.errors import TheoryNotCompatibleError
from holopy.scattering.interface import prep_schema
from holopy.scattering.tests.common import xschema as XSCHEMA

SPHERE = Sphere(n=1.5, r=1.0, center=(0, 0, 2))
SPHERES = Spheres([
    Sphere(n=1.5, r=1.0, center=(-1, -1, 2)),
    Sphere(n=1.5, r=1.0, center=(+1, +1, 2)),
])
ELLIPSOID = Ellipsoid(n=1.5, r=(0.5, 0.5, 1.2), center=(0, 0, 2))
TOLS = {'atol': 1e-14, 'rtol': 1e-14}
MEDTOLS = {'atol': 1e-7, 'rtol': 1e-7}

SCAT_SCHEMA = prep_schema(detector_grid(shape=(5, 5), spacing=.1),
                          medium_index=1.33,
                          illum_wavelen=0.66,
                          illum_polarization=False)


class MockTheory(ScatteringTheory):
    """Minimally-functional daughter of ScatteringTheory for fast tests."""
    def __init__(*args, **kwargs):
        pass  # an init is necessary for the repr