Beispiel #1
0
def test_next_model():
    exampleresult = FitResult(parameters={
        'center[1]': 31.367170884695756, 'r': 0.6465280831465722,
        'center[0]': 32.24150087110443,
        'center[2]': 35.1651561654966,
        'alpha': 0.7176299231169572,
        'n': 1.580122175314896},
        scatterer=Sphere(n=1.580122175314896, r=0.6465280831465722,
        center=[32.24150087110443, 31.367170884695756, 35.1651561654966]),
        chisq=0.0001810513851216454, rsq=0.9727020197282801,
        converged=True, time=5.179728031158447,
        model=Model(scatterer=ParameterizedObject(obj=
        Sphere(n=Parameter(guess=1.59, limit=[1.4, 1.7], name='n'),
        r=Parameter(guess=0.65, limit=[0.6, 0.7], name='r'),
        center=[Parameter(guess=32.110424836601304, limit=[2, 40], name='center[0]'),
        Parameter(guess=31.56683986928105, limit=[4, 40], name='center[1]'),
        Parameter(guess=33, limit=[5, 45], name='center[2]')])),
        theory=Mie.calc_holo, alpha=Parameter(guess=0.6, limit=[0.1, 1], name='alpha'),
        constraints=[]), minimizer = None, minimization_details = None)

    gold = Model(scatterer=ParameterizedObject(obj=Sphere(
        n=Parameter(guess=1.580122175314896, limit=[1.4, 1.7], name='n'),
        r=Parameter(guess=0.6465280831465722, limit=[0.6, 0.7], name='r'),
        center=[Parameter(guess=32.24150087110443, limit=[2, 40], name='center[0]'),
        Parameter(guess=31.367170884695756, limit=[4, 40], name='center[1]'),
        Parameter(guess=35.1651561654966, limit=[5, 45], name='center[2]')])),
        theory=Mie.calc_holo, alpha=Parameter(guess=0.7176299231169572, limit=[0.1, 1], name='alpha'),
        constraints=[])

    assert_obj_close(gold, exampleresult.next_model())
Beispiel #2
0
 def test_BaseModel_lnprior(self):
     scat = Sphere(r=prior.Gaussian(1, 1), n=prior.Gaussian(1, 1),
                   center=[10, 10, 10])
     mod = Model(scat, noise_sd=0.1)
     # Desired: log(sqrt(0.5/pi))-1/2
     desired_sigma = -1.4189385332
     assert_obj_close(mod.lnprior({'n': 0, 'r': 0}), desired_sigma * 2)
Beispiel #3
0
def test_calc_field():
    s = Sphere(n=1.59, r=.5, center=(0, 0, 1))
    t = update_metadata(detector_grid(shape=(2, 2), spacing=.1),
                        illum_wavelen=0.66,
                        medium_index=1.33,
                        illum_polarization=(1, 0))
    thry = Mie(False)
    f = calc_field(t, s, 1.33, .66, (1, 0), theory=thry)
    assert_obj_close(t.attrs, f.attrs)
    gold = xr.DataArray(np.array([[[
        -3.95866810e-01 + 2.47924378e+00j, 0.00000000e+00 + 0.00000000e+00j,
        0.00000000e+00 - 0.00000000e+00j
    ],
                                   [
                                       -4.91260953e-01 + 2.32779296e+00j,
                                       9.21716363e-20 - 5.72226912e-19j,
                                       2.99878926e-18 - 1.41959276e-17j
                                   ]],
                                  [[
                                      -4.89755627e-01 + 2.31844748e+00j,
                                      0.00000000e+00 + 0.00000000e+00j,
                                      4.89755627e-02 - 2.31844748e-01j
                                  ],
                                   [
                                       -5.71886751e-01 + 2.17145168e+00j,
                                       1.72579090e-03 - 8.72241140e-03j,
                                       5.70160960e-02 - 2.16272927e-01j
                                   ]]]),
                        dims=['x', 'y', 'vector'],
                        coords={
                            'x': t.x,
                            'y': t.y,
                            'vector': ['x', 'y', 'z']
                        })
    assert abs((f - gold).max()) < 5e-9
Beispiel #4
0
 def test_flat_colour_dimension_gives_greyscale(self):
     xr3 = convert_ndarray_to_xarray(ARRAY_4D[:, :, :, 0:1],
                                     extra_dims={ILLUM: [0]})
     displayed_xr = display_image(xr3)
     displayed_np = display_image(ARRAY_3D)
     displayed_np.attrs = displayed_xr.attrs
     assert_obj_close(displayed_xr.shape, displayed_np.shape)
Beispiel #5
0
 def test_xarray_dimension_order(self):
     xarray_grid = convert_ndarray_to_xarray(ARRAY_3D)
     displayed = display_image(xarray_grid, scaling=None)
     displayed_transposed = display_image(xarray_grid.transpose(),
                                          scaling=None)
     assert_obj_close(displayed, xarray_grid)
     assert_obj_close(displayed_transposed, xarray_grid)
Beispiel #6
0
def test_minimizer():
    x = np.arange(-10, 10, .1)
    a = 5.3
    b = -1.8
    c = 3.4
    gold_list = [a, b, c]
    y = a * x**2 + b * x + c

    # This test does NOT handle scaling correctly -- we would need a Model
    # which knows the parameters to properly handle the scaling/unscaling
    def cost_func(pars):
        a, b, c = pars
        return a * x**2 + b * x + c - y

    # test basic usage
    parameters = [
        prior.Uniform(-np.inf, np.inf, name='a', guess=5),
        prior.Uniform(-np.inf, np.inf, name='b', guess=-2),
        prior.Uniform(-np.inf, np.inf, name='c', guess=3)
    ]
    minimizer = Nmpfit()
    result, minimization_details = minimizer.minimize(parameters, cost_func)
    assert_obj_close(result, gold_list, context='basic_minimized_parameters')

    # now test limiting minimizer iterations
    minimizer = Nmpfit(maxiter=1)
    try:
        result, minimization_details = minimizer.minimize(
            parameters, cost_func)
    except MinimizerConvergenceFailed as cf:  # the fit shouldn't converge
        result, minimization_details = cf.result, cf.details
    assert_equal(minimization_details.niter,
                 2)  # there's always an offset of 1
Beispiel #7
0
def test_serialization():
    par_s = Sphere(center=(Uniform(0, 1e-5, guess=.567e-5),
                           Uniform(0, 1e-5, .567e-5), Uniform(1e-5, 2e-5)),
                   r=Uniform(1e-8, 1e-5, 8.5e-7),
                   n=Uniform(1, 2, 1.59))

    alpha = Uniform(.1, 1, .6, 'alpha')

    schema = update_metadata(detector_grid(shape=100, spacing=.1151e-6),
                             illum_wavelen=.66e-6,
                             medium_index=1.33,
                             illum_polarization=(1, 0))

    model = AlphaModel(par_s,
                       medium_index=schema.medium_index,
                       illum_wavelen=schema.illum_wavelen,
                       alpha=alpha)

    holo = calc_holo(schema, model.scatterer.guess, scaling=model.alpha.guess)

    result = fix_flat(NmpfitStrategy().fit(model, holo))
    temp = tempfile.NamedTemporaryFile(suffix='.h5', delete=False)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        save(temp.name, result)
        loaded = load(temp.name)
        assert_obj_close(result, loaded, context='serialized_result')
def test_next_model():
    exampleresult = FitResult(parameters={
        'center[1]': 31.367170884695756, 'r': 0.6465280831465722, 
        'center[0]': 32.24150087110443, 
        'center[2]': 35.1651561654966, 
        'alpha': 0.7176299231169572, 
        'n': 1.580122175314896}, 
        scatterer=Sphere(n=1.580122175314896, r=0.6465280831465722, 
        center=[32.24150087110443, 31.367170884695756, 35.1651561654966]), 
        chisq=0.0001810513851216454, rsq=0.9727020197282801, 
        converged=True, time=5.179728031158447, 
        model=Model(scatterer=ParameterizedObject(obj=
        Sphere(n=Parameter(guess=1.59, limit=[1.4, 1.7], name='n'), 
        r=Parameter(guess=0.65, limit=[0.6, 0.7], name='r'), 
        center=[Parameter(guess=32.110424836601304, limit=[2, 40], name='center[0]'), 
        Parameter(guess=31.56683986928105, limit=[4, 40], name='center[1]'), 
        Parameter(guess=33, limit=[5, 45], name='center[2]')])), 
        theory=Mie.calc_holo, alpha=Parameter(guess=0.6, limit=[0.1, 1], name='alpha'), 
        constraints=[]), minimizer = None, minimization_details = None)

    gold = Model(scatterer=ParameterizedObject(obj=Sphere(
        n=Parameter(guess=1.580122175314896, limit=[1.4, 1.7], name='n'), 
        r=Parameter(guess=0.6465280831465722, limit=[0.6, 0.7], name='r'), 
        center=[Parameter(guess=32.24150087110443, limit=[2, 40], name='center[0]'), 
        Parameter(guess=31.367170884695756, limit=[4, 40], name='center[1]'), 
        Parameter(guess=35.1651561654966, limit=[5, 45], name='center[2]')])), 
        theory=Mie.calc_holo, alpha=Parameter(guess=0.7176299231169572, limit=[0.1, 1], name='alpha'), 
        constraints=[])

    assert_obj_close(gold, exampleresult.next_model())
Beispiel #9
0
def test_fit_mie_single():
    holo = normalize(get_example_data('image0001.yaml'))

    parameters = [
        Parameter(name='x', guess=.567e-5, limit=[0.0, 1e-5]),
        Parameter(name='y', guess=.576e-5, limit=[0, 1e-5]),
        Parameter(name='z', guess=15e-6, limit=[1e-5, 2e-5]),
        Parameter(name='n', guess=1.59, limit=[1, 2]),
        Parameter(name='r', guess=8.5e-7, limit=[1e-8, 1e-5])
    ]

    def make_scatterer(x, y, z, r, n):
        return Sphere(n=n + 1e-4j, r=r, center=(x, y, z))

    thry = Mie(False)
    model = Model(Parametrization(make_scatterer, parameters),
                  thry.calc_holo,
                  alpha=Parameter(name='alpha', guess=.6, limit=[.1, 1]))

    assert_raises(InvalidMinimizer, fit, model, holo, minimizer=Sphere)

    result = fit(model, holo)

    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3)
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
Beispiel #10
0
def test_serialization():
    par_s = Sphere(center=(par(.567e-5, [0, 1e-5]), par(.576e-6, [0, 1e-5]),
                           par(15e-6, [1e-5, 2e-5])),
                   r=par(8.5e-7, [1e-8, 1e-5]),
                   n=par(1.59, [1, 2]))

    alpha = par(.6, [.1, 1], 'alpha')

    schema = ImageSchema(shape=100,
                         spacing=.1151e-6,
                         optics=Optics(.66e-6, 1.33))

    model = Model(par_s, Mie.calc_holo, alpha=alpha)

    holo = Mie.calc_holo(model.scatterer.guess, schema, model.alpha.guess)

    result = fit(model, holo)

    temp = tempfile.NamedTemporaryFile()
    save(temp, result)

    temp.flush()
    temp.seek(0)
    loaded = load(temp)

    assert_obj_close(result, loaded, context='serialized_result')
Beispiel #11
0
def test_Emcee_Class():
    np.random.seed(40)
    scat = model.Parametrization(0,[prior.Gaussian(0,1)])
    mod = model.BaseModel(scat)
    e = mcmc.Emcee(mod,[],nwalkers=3)
    assert_equal(e.nwalkers,3)
    assert_obj_close(e.make_guess(),prior_dist)
Beispiel #12
0
def test_polarization():
    # test holograms for orthogonal polarizations; make sure they're
    # not the same, nor too different from one another.

    sc = Spheres([sphere])

    xholo = calc_holo(xschema,
                      sc,
                      index,
                      wavelen,
                      xpolarization,
                      scaling=scaling_alpha)
    yholo = calc_holo(yschema,
                      sc,
                      index,
                      wavelen,
                      ypolarization,
                      scaling=scaling_alpha)

    # the two arrays should not be equal
    try:
        assert_array_almost_equal(xholo, yholo)
    except AssertionError:
        pass
    else:
        raise AssertionError(
            "Holograms computed for both x- and y-polarized light are too similar."
        )

    # but their max and min values should be close
    assert_obj_close(xholo.max(), yholo.max())
    assert_obj_close(xholo.min(), yholo.min())
    return xholo, yholo
Beispiel #13
0
def test_subset_tempering():
    holo = normalize(get_example_data('image0001'))
    scat = Sphere(r=0.65e-6,n=1.58,center=[5.5e-6,5.8e-6,14e-6])
    mod = AlphaModel(scat,noise_sd=.1, alpha=prior.Gaussian(0.7,0.1))
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        inf = sample.tempered_sample(mod, holo, nwalkers=4, samples=10, stages=1, stage_len=10, threads=None, seed=40)
    assert_obj_close(inf.MAP,gold_alpha, rtol=1e-3)
Beispiel #14
0
 def test_complex_values_return_magnitude(self):
     xarray_real = convert_ndarray_to_xarray(ARRAY_3D)
     xarray_complex = xarray_real + 0j
     xarray_complex[0, 0, :] *= (1 + 1j) / np.sqrt(2)
     with warnings.catch_warnings():
         warnings.simplefilter('ignore')
         displayed = display_image(xarray_complex, scaling=None)
         assert_obj_close(displayed, xarray_real)
Beispiel #15
0
def test_subset_tempering():
    holo = normalize(get_example_data('image0001'))
    scat = Sphere(r=0.65e-6,n=1.58,center=[5.5e-6,5.8e-6,14e-6])
    mod = AlphaModel(scat,noise_sd=.1, alpha=prior.Gaussian(0.7,0.1))
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        inf = sample.tempered_sample(mod, holo, nwalkers=4, samples=10, stages=1, stage_len=10, threads=None, seed=40)
    assert_obj_close(inf.MAP,gold_alpha, rtol=1e-3)
Beispiel #16
0
 def test_scaling_constricts_intensity_bounds(self):
     scale = (-5, 100)
     wide3 = ARRAY_3D.copy()
     wide3[0, 0, 0] = -5
     wide3[-1, -1, -1] = 100
     xr3 = convert_ndarray_to_xarray(ARRAY_3D) / 59
     assert_equal(display_image(wide3).attrs['_image_scaling'], scale)
     assert_obj_close(display_image(wide3, (0, 59)).values, xr3.values)
Beispiel #17
0
def test_layered():
    l = LayeredSphere(n = (1, 2), t = (1, 1), center = (2, 2, 2))
    s = Sphere(n = (1,2), r = (1, 2), center = (2, 2, 2))
    sch = detector_grid((10, 10), .2)
    wavelen = .66
    hl = calc_holo(sch, l, index, wavelen, illum_polarization=xpolarization)
    hs = calc_holo(sch, s, index, wavelen, illum_polarization=xpolarization)
    assert_obj_close(hl, hs, rtol=0)
Beispiel #18
0
def test_propagate_0_distance():
    im = get_example_data('image0003')
    rec = propagate(im, 0)
    # propagating no distance should leave the image unchanged
    assert_obj_close(im, rec)

    rec = propagate(im, [0, 3e-6])
    verify(rec, 'recon_multiple_with_0')
Beispiel #19
0
 def test_specify_axes_for_numpy_arrays(self):
     transposed = np.transpose(ARRAY_3D, [1, 0, 2])
     displayed_transposed = display_image(transposed, scaling=None)
     xr_transposed = convert_ndarray_to_xarray(transposed)
     assert_obj_close(display_image(ARRAY_3D, depth_axis=1, scaling=None),
                      xr_transposed)
     assert_obj_close(
         display_image(ARRAY_3D, vert_axis=0, horiz_axis=2, scaling=None),
         xr_transposed)
Beispiel #20
0
def test_subimaged():
    # make a dummy image so that we can pretend we are working with
    # data we want to subimage
    im = xschema
    h = calc_holo(im, sphere, index, wavelen, xpolarization)
    sub = (60, 70), 30
    hs = calc_holo(subimage(im, *sub), sphere, index, wavelen, xpolarization)

    assert_obj_close(subimage(h, *sub), hs)
Beispiel #21
0
 def test_colour_name_formats(self):
     base = convert_ndarray_to_xarray(
         ARRAY_4D, extra_dims={ILLUM: ['red', 'green', 'blue']})
     cols = [['Red', 'Green', 'Blue'], ['r', 'g', 'b'], [0, 1, 2],
             ['a', 's', 'd']]
     for collist in cols:
         xr4 = convert_ndarray_to_xarray(ARRAY_4D,
                                         extra_dims={ILLUM: collist})
         assert_obj_close(display_image(xr4, scaling=None), base)
Beispiel #22
0
 def test_no_metadata(self):
     filename = os.path.join(self.tempdir, 'image0007.tif')
     header = ifd2()
     header[270] = 'Dummy String'
     pilimage.fromarray(self.holo.values[0]).save(filename, tiffinfo=header)
     # load doesn't work
     self.assertRaises(NoMetadata, load, filename)
     # load_image does
     l = load_image(filename, spacing=get_spacing(self.holo))
     assert_obj_close(l, copy_metadata(l, self.holo))
Beispiel #23
0
def test_multidim():
    s = Sphere(n = {'r':par(0,[-1,1]),'g':par(0,0),'b':prior.Gaussian(0,1),'a':0}, r = xr.DataArray([prior.Gaussian(0,1),par(0,[-1,1]),par(0,0),0], dims='alph',coords={'alph':['a','b','c','d']}), center=[par(0,[-1,1]),par(0,0),0])
    par_s = ParameterizedObject(s)
    params = {'n_r':3,'n_g':4,'n_b':5,'n_a':6,'r_a':7,'r_b':8,'r_c':9,'r_d':10,'center[0]':7,'center[1]':8,'center[2]':9}
    out_s = Sphere(n={'r':3,'g':0,'b':5,'a':0}, r = xr.DataArray([7,8,0,0], dims='alph',coords={'alph':['a','b','c','d']}), center=[7,0,0])
    assert_obj_close(par_s.make_from(params), out_s)

    m = Model(s, np.sum)
    m._use_parameter(OrderedDict([('r',par(0,[-1,1])),('g',par(0,0)),('b',prior.Gaussian(0,1)),('a',0)]),'letters')
    m._use_parameter(xr.DataArray([prior.Gaussian(0,1),par(0,[-1,1]),par(0,0),0],dims='numbers',coords={'numbers':['one','two','three','four']}),'count')
    expected_params = [par(0,[-1,1],'letters_r'),par(0,0,'letters_g'),prior.Gaussian(0,1,'letters_b'),prior.Gaussian(0,1,'count_one'),par(0,[-1,1],'count_two'), par(0,0,'count_three')]
    assert_equal(m.parameters[-6:], expected_params)
Beispiel #24
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)
Beispiel #25
0
def test_nmpfit():
    fitresult = nmpfit.mpfit(residfunct,
                             parinfo=parinfo,
                             ftol=ftol,
                             xtol=xtol,
                             gtol=gtol,
                             damp=damp,
                             maxiter=maxiter,
                             quiet=quiet)

    assert_obj_close(fitresult.params,
                     gold_single[np.array([0, 2, 3, 4, 5, 6])],
                     rtol=1e-3)
Beispiel #26
0
def test_load_optics():
    optics_yaml = """wavelen: 785e-9
polarization: [1.0, 0]
divergence: 0
pixel_size: [6.8e-6, 6.8e-6]
pixel_scale: [3.3e-7, 3.3e-7]"""
    t = tempfile.TemporaryFile()
    t.write(optics_yaml)
    t.seek(0)

    o = Optics(**load(t))

    assert_obj_close(o, Optics(wavelen=7.85e-07, polarization=[1.0, 0.0], divergence=0, pixel_size=[6.8e-06, 6.8e-06], pixel_scale=[3.3e-07, 3.3e-07]))
Beispiel #27
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 guess, parameters, from_parameters
    assert_obj_close(trc.guess, trans)
    assert_obj_close(trc.from_parameters(trc.parameters), trans)
Beispiel #28
0
    def test_returns_close_values(self):
        model = self._make_model()
        holo = normalize(get_example_data('image0001'))

        np.random.seed(40)
        result = NmpfitStrategy(npixels=1000).fit(model, holo)

        # TODO: figure out if it is a problem that alpha is frequently
        # coming out wrong in the 3rd decimal place.
        self.assertAlmostEqual(result.parameters['alpha'],
                               gold_alpha,
                               places=3)
        # TODO: this tolerance has to be rather large to pass, we should
        # probably track down if this is a sign of a problem
        assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2)
Beispiel #29
0
 def test_missing_colours(self):
     base = convert_ndarray_to_xarray(
         ARRAY_4D, extra_dims={ILLUM: ['red', 'green', 'blue']})
     slices = [[0, 2, 1], [1, 0], [0, 1], [0, 1]]
     possible_valid_colors = [['red', 'blue', 'green'], ['green', 'red'],
                              [0, 1], ['x-pol', 'y-pol']]
     dummy_channel = [None, 2, -1, -1]
     for i, c, d in zip(slices, possible_valid_colors, dummy_channel):
         xr4 = convert_ndarray_to_xarray(ARRAY_4D[:, :, :, i],
                                         extra_dims={ILLUM: c})
         xr4 = display_image(xr4, scaling=None)
         if d is not None:
             assert_equal(xr4.attrs['_dummy_channel'], d)
             del xr4.attrs['_dummy_channel']
         assert_obj_close(xr4, base)
Beispiel #30
0
def test_fit_single_openopt():
    holo = normalize(get_example_data('image0001.yaml'))
    s = Sphere(center = (par(guess=.567e-5, limit=[.4e-5,.6e-5]),
                         par(.567e-5, (.4e-5, .6e-5)), par(15e-6, (1.3e-5, 1.8e-5))),
               r = par(8.5e-7, (5e-7, 1e-6)),
               n = ComplexParameter(par(1.59, (1.5,1.8)), 1e-4j))

    model = Model(s, Mie(False).calc_holo, alpha = par(.6, [.1,1]))
    try:
        minimizer = OpenOpt('scipy_slsqp')
    except ImportError:
        raise SkipTest
    result = fit(model, holo, minimizer = minimizer)
    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3)
    # TODO: see if we can get this back to 3 sig figs correct alpha
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
Beispiel #31
0
def test_fit_mie_par_scatterer():
    holo = normalize(get_example_data('image0001'))

    s = Sphere(center=(Uniform(0, 1e-5, guess=.567e-5),
                       Uniform(0, 1e-5, .567e-5), Uniform(1e-5, 2e-5)),
               r=Uniform(1e-8, 1e-5, 8.5e-7),
               n=ComplexPrior(Uniform(1, 2, 1.59), 1e-4))

    thry = Mie(False)
    model = AlphaModel(s, theory=thry, alpha=Uniform(.1, 1, .6))

    result = fix_flat(NmpfitStrategy().fit(model, holo))
    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3)
    # TODO: see if we can get this back to 3 sig figs correct alpha
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
    assert_read_matches_write(result)
def test_fit_single_openopt():
    holo = normalize(get_example_data('image0001.yaml'))
    s = Sphere(center = (par(guess=.567e-5, limit=[.4e-5,.6e-5]),
                         par(.567e-5, (.4e-5, .6e-5)), par(15e-6, (1.3e-5, 1.8e-5))),
               r = par(8.5e-7, (5e-7, 1e-6)),
               n = ComplexParameter(par(1.59, (1.5,1.8)), 1e-4j))

    model = Model(s, Mie(False).calc_holo, alpha = par(.6, [.1,1]))
    try:
        minimizer = OpenOpt('scipy_slsqp')
    except ImportError:
        raise SkipTest
    result = fit(model, holo, minimizer = minimizer)
    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3)
    # TODO: see if we can get this back to 3 sig figs correct alpha
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
def test_fit_mie_par_scatterer():
    holo = normalize(get_example_data('image0001.yaml'))

    s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]),
                         par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))),
               r = par(8.5e-7, (1e-8, 1e-5)),
               n = ComplexParameter(par(1.59, (1,2)), 1e-4j))

    thry = Mie(False)
    model = Model(s, thry.calc_holo, alpha = par(.6, [.1,1]))

    result = fit(model, holo)

    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3)
    # TODO: see if we can get this back to 3 sig figs correct alpha
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
    assert_read_matches_write(result)
Beispiel #34
0
def test_fit_mie_par_scatterer():
    holo = normalize(get_example_data('image0001.yaml'))

    s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]),
                         par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))),
               r = par(8.5e-7, (1e-8, 1e-5)),
               n = ComplexParameter(par(1.59, (1,2)), 1e-4j))

    thry = Mie(False)
    model = Model(s, thry.calc_holo, alpha = par(.6, [.1,1]))

    result = fit(model, holo)

    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-3)
    # TODO: see if we can get this back to 3 sig figs correct alpha
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
    assert_read_matches_write(result)
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)
Beispiel #36
0
def test_optimization_with_maxiter_of_2():
    gold_fit_dict = {
        '0:r': 0.52480509800531849,
        '1:center.1': 14.003687569304704,
        'alpha': 0.93045027963762217,
        '0:center.2': 19.93177549652841,
        '1:r': 0.56292664494653732,
        '0:center.1': 15.000340621607815,
        '1:center.0': 14.020984607646726,
        '0:center.0': 15.000222185576494,
        '1:center.2': 20.115613202192328
    }

    #calculate a hologram with known particle positions to do a fit against
    schema = detector_grid(shape=100, spacing=.1)

    s1 = Sphere(center=(15, 15, 20), n=1.59, r=0.5)
    s2 = Sphere(center=(14, 14, 20), n=1.59, r=0.5)
    cluster = Spheres([s1, s2])
    holo = calc_holo(schema, cluster, 1.33, .66, illum_polarization=(1, 0))

    #trying to do a fast fit:
    guess1 = Sphere(center=(prior.Uniform(5, 25, guess=15),
                            prior.Uniform(5, 25, 15), prior.Uniform(5, 25,
                                                                    20)),
                    r=(prior.Uniform(.4, .6, guess=.45)),
                    n=1.59)
    guess2 = Sphere(center=(prior.Uniform(5, 25, guess=14),
                            prior.Uniform(5, 25, 14), prior.Uniform(5, 25,
                                                                    20)),
                    r=(prior.Uniform(.4, .6, guess=.45)),
                    n=1.59)
    par_s = Spheres([guess1, guess2])

    model = AlphaModel(par_s,
                       medium_index=1.33,
                       illum_wavelen=.66,
                       illum_polarization=(1, 0),
                       alpha=prior.Uniform(.1, 1, .6))
    optimizer = Nmpfit(maxiter=2)
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')
        result = optimizer.fit(model, holo)
    assert_obj_close(gold_fit_dict, result.parameters, rtol=1e-5)
def test_fit_random_subset():
    holo = normalize(get_example_data('image0001.yaml'))

    s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]),
                         par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))),
               r = par(8.5e-7, (1e-8, 1e-5)), n = ComplexParameter(par(1.59, (1,2)),1e-4j))

    model = Model(s, Mie.calc_holo, alpha = par(.6, [.1,1]))
    np.random.seed(40)
    result = fit(model, holo, use_random_fraction=.1)

    # we have to use a relatively loose tolerance here because the random
    # selection occasionally causes the fit to be a bit worse
    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2)
    # TODO: figure out if it is a problem that alpha is frequently coming out
    # wrong in the 3rd decimal place.
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=2)
    assert_equal(model, result.model)

    assert_read_matches_write(result)
Beispiel #38
0
def test_fit_random_subset():
    holo = normalize(get_example_data('image0001.yaml'))

    s = Sphere(center = (par(guess=.567e-5, limit=[0,1e-5]),
                         par(.567e-5, (0, 1e-5)), par(15e-6, (1e-5, 2e-5))),
               r = par(8.5e-7, (1e-8, 1e-5)), n = ComplexParameter(par(1.59, (1,2)),1e-4))

    model = Model(s, Mie(False).calc_holo, alpha = par(.6, [.1,1]))
    np.random.seed(40)
    result = fit(model, holo, random_subset=.1)

    # TODO: this tolerance has to be rather large to pass, we should
    # probably track down if this is a sign of a problem
    assert_obj_close(result.scatterer, gold_sphere, rtol=1e-2)
    # TODO: figure out if it is a problem that alpha is frequently coming out
    # wrong in the 3rd decimal place.
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)

    assert_read_matches_write(result)
Beispiel #39
0
def test_polarization():
    # test holograms for orthogonal polarizations; make sure they're
    # not the same, nor too different from one another.

    sc = Spheres([sphere])

    xholo = calc_holo(xschema, sc, index, wavelen, xpolarization, scaling=scaling_alpha)
    yholo = calc_holo(yschema, sc, index, wavelen, ypolarization, scaling=scaling_alpha)

    # the two arrays should not be equal
    try:
        assert_array_almost_equal(xholo, yholo)
    except AssertionError:
        pass
    else:
        raise AssertionError("Holograms computed for both x- and y-polarized light are too similar.")

    # but their max and min values should be close
    assert_obj_close(xholo.max(), yholo.max())
    assert_obj_close(xholo.min(), yholo.min())
    return xholo, yholo
def test_fit_mie_single():
    holo = normalize(get_example_data('image0001.yaml'))

    parameters = [Parameter(name='x', guess=.567e-5, limit = [0.0, 1e-5]),
                  Parameter(name='y', guess=.576e-5, limit = [0, 1e-5]),
                  Parameter(name='z', guess=15e-6, limit = [1e-5, 2e-5]),
                  Parameter(name='n', guess=1.59, limit = [1, 2]),
                  Parameter(name='r', guess=8.5e-7, limit = [1e-8, 1e-5])]

    def make_scatterer(x, y, z, r, n):
        return Sphere(n=n+1e-4j, r = r, center = (x, y, z))

    thry = Mie(False)
    model = Model(Parametrization(make_scatterer, parameters), thry.calc_holo,
                  alpha=Parameter(name='alpha', guess=.6, limit = [.1, 1]))

    assert_raises(InvalidMinimizer, fit, model, holo, minimizer=Sphere)

    result = fit(model, holo)

    assert_obj_close(result.scatterer, gold_sphere, rtol = 1e-3)
    assert_approx_equal(result.parameters['alpha'], gold_alpha, significant=3)
    assert_equal(model, result.model)
def test_serialization():
    par_s = Sphere(center = (par(.567e-5, [0, 1e-5]), par(.576e-6, [0, 1e-5]),
                                                           par(15e-6, [1e-5,
                                                                       2e-5])),
                   r = par(8.5e-7, [1e-8, 1e-5]), n = par(1.59, [1,2]))

    alpha = par(.6, [.1, 1], 'alpha')

    schema = ImageSchema(shape = 100, spacing = .1151e-6, optics = Optics(.66e-6, 1.33))

    model = Model(par_s, Mie.calc_holo, alpha=alpha)

    holo = Mie.calc_holo(model.scatterer.guess, schema, model.alpha.guess)

    result = fit(model, holo)

    temp = tempfile.NamedTemporaryFile()
    save(temp, result)

    temp.flush()
    temp.seek(0)
    loaded = load(temp)

    assert_obj_close(result, loaded, context = 'serialized_result')
Beispiel #42
0
def test_subset_tempering():
    np.random.seed(40)
    holo = normalize(get_example_data('image0001.yaml'))
    scat = Sphere(r=0.65e-6,n=1.58,center=[5.5e-6,5.8e-6,14e-6])
    mod = AlphaModel(scat,Mie,noise_sd=.1,alpha=prior.Gaussian(0.7,0.1))
    inf=mcmc.subset_tempering(mod,holo,final_len=10,nwalkers=4,stages=1,stage_len=10,threads=None, verbose=False,seed=40)
    assert_obj_close(inf.most_probable_values(),gold_alpha)
    assert_equal(inf.n_steps,gold_nsteps)
    assert_obj_close(inf.acceptance_fraction,gold_frac)
    assert_obj_close(float(inf.data_frame(burn_in=6)[1:2].alpha),gold_alpha)
Beispiel #43
0
def test_2_sph():
    sc = Spheres(scatterers=[Sphere(center=[7.1e-6, 7e-6, 10e-6],
                                       n=1.5811+1e-4j, r=5e-07),
                                Sphere(center=[6e-6, 7e-6, 10e-6],
                                       n=1.5811+1e-4j, r=5e-07)])


    holo = calc_holo(schema, sc, theory=Multisphere, scaling=.6)

    assert_obj_close(holo.max(), 1.4140292298443309)
    assert_obj_close(holo.mean(), 0.9955420925817654)
    assert_obj_close(holo.std(), 0.09558537595025796)
Beispiel #44
0
def test_gaussian():
    g = prior.Gaussian(1, 1)
    assert_equal(g.guess, 1)
    assert_obj_close(g.lnprob(0),gold_sigma)
Beispiel #45
0
def test_NoiseModel_lnprior():
    scat=Sphere(r=prior.Gaussian(1,1),n=prior.Gaussian(1,1),center=[10,10,10])
    mod=NoiseModel(scat, noise_sd=.1)
    assert_obj_close(mod.lnprior([0,0]),gold_sigma*2)
Beispiel #46
0
def test_image_io():
    holo = get_example_data('image0001.yaml')
    t = tempfile.mkdtemp()

    filename = os.path.join(t, 'image0001.tif')
    save(filename, holo)
    l = load(filename)
    assert_obj_close(l, holo)

    # check that it defaults to saving as tif
    filename = os.path.join(t, 'image0002')
    save_image(filename, holo)
    l = load(filename+'.tif')
    assert_obj_close(l, holo)

    # check saving 16 bit
    filename = os.path.join(t, 'image0003')
    save_image(filename, holo, scaling=None, depth=16)
    l = load(filename+'.tif')
    assert_obj_close(l, holo)

    # test that yaml save works corretly with a string instead of a file
    filename = os.path.join(t, 'image0001.yaml')
    save(filename, holo)
    loaded = load(filename)
    assert_obj_close(loaded, holo)

    f = get_example_data_path('image0001.yaml')
    spacing = .1
    optics = Optics(.66, 1.33, (1,0))
    with warnings.catch_warnings(record =True) as w:
        warnings.simplefilter('always')
        h = load(f, spacing = spacing, optics = optics)
        assert_obj_close(h.optics, optics)
        assert_equal(h.spacing, spacing)
        assert_equal(len(w), 1)
        assert "Overriding spacing and optics of loaded yaml" in w[-1].message


    with warnings.catch_warnings(record =True) as w:
        warnings.simplefilter('always')
        h = load(f, optics = optics)
        assert_obj_close(h.optics, optics)
        assert_equal(h.spacing, holo.spacing)
        assert_equal(len(w), 1)
        assert ("WARNING: overriding optics of loaded yaml without "
                "overriding spacing, this is probably incorrect." in
                w[-1].message)


    with warnings.catch_warnings(record =True) as w:
        warnings.simplefilter('always')
        h = load(f, spacing = spacing)
        assert_obj_close(h.optics, holo.optics)
        assert_equal(h.spacing, spacing)
        assert_equal(len(w), 1)
        assert ("WARNING: overriding spacing of loaded yaml without "
                "overriding optics, this is probably incorrect." in
                w[-1].message)

    shutil.rmtree(t)
Beispiel #47
0
def test_updated():
    p=prior.BoundedGaussian(1,2,-1,2)    
    d=mcmc.UncertainValue(1,0.5,1)
    u=prior.updated(p,d)
    assert_equal(u.guess,1)
    assert_obj_close(u.lnprob(0),gold_sigma)
def test_model_guess():
    ps = Sphere(n=par(1.59, [1.5,1.7]), r = .5, center=(5,5,5))
    m = Model(ps, Mie)
    assert_obj_close(m.scatterer.guess, Sphere(n=1.59, r=0.5, center=[5, 5, 5]))