示例#1
0
def test_scatterer_io():
    s = Sphere()
    assert_read_matches_write(s)

    s1 = Sphere(1.59, .5, [1, 1, 2])
    s2 = Sphere(1.59, .5, [1, 3, 2])
    sc = Spheres([s1, s2])

    assert_read_matches_write(sc)
示例#2
0
 def test_dataarray_attr(self):
     result = generate_fit_result()
     samples = xr.DataArray([[1, 2, 3], [4, 5, 6]],
                            dims=['dim1', 'dim2'],
                            coords={
                                'dim1': ['left', 'right'],
                                'dim2': ['r', 'b', 'g']
                            })
     result.add_attr({'samples': samples})
     assert_read_matches_write(result)
示例#3
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)
示例#4
0
    def test_subset_data_fit_result_is_saveable(self):
        model = self._make_model()
        holo = get_example_data('image0001')

        np.random.seed(40)
        fitter = NmpfitStrategy(npixels=100, maxiter=1)
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', UserWarning)
            # ignore not-converged warnings since we only do 2 iterations
            fitted = fitter.fit(model, holo)

        result = fitted  # was fix_flat(fitted)
        assert_read_matches_write(result)
示例#5
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)
示例#6
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)
示例#7
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)
示例#8
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)
示例#9
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)
示例#10
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-4j))

    model = Model(s, Mie.calc_holo, alpha = par(.6, [.1,1]))
    np.random.seed(40)
    result = fit(model, holo, random_subset=.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-3)
    # 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)
示例#11
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-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)
示例#12
0
def test_fit_random_subset():
    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))

    model = AlphaModel(s, theory=Mie(False), alpha=Uniform(.1, 1, .6))
    np.random.seed(40)
    result = fix_flat(NmpfitStrategy(npixels=1000).fit(model, holo))

    # 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)
示例#13
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)
示例#14
0
def test_hologram_io():
    holo = normalize(get_example_data('image0001.yaml'))

    assert_read_matches_write(holo)
示例#15
0
def test_io():
    model = ExactModel(Sphere(1), calc_holo)
    assert_read_matches_write(model)

    model = ExactModel(Sphere(1), calc_holo, theory=Mie(False))
    assert_read_matches_write(model)
示例#16
0
def test_io():
    model = Model(Sphere(par(1)), Mie.calc_holo)
    assert_read_matches_write(model)

    model = Model(Sphere(par(1)), Mie(False).calc_holo)
    assert_read_matches_write(model)
示例#17
0
def test_marray_io():
    d = Marray(np.random.random((10, 10)))
    assert_read_matches_write(d)
示例#18
0
 def test_hologram_io(self):
     assert_read_matches_write(normalize(self.holo))
示例#19
0
def test_hologram_io():
    holo = normalize(get_example_data('image0001.yaml'))

    assert_read_matches_write(holo)
示例#20
0
def test_io():
    model = Model(Sphere(par(1)), calc_holo)
    assert_read_matches_write(model)

    model = Model(Sphere(par(1)), calc_holo, theory=Mie(False))
    assert_read_matches_write(model)
示例#21
0
def test_marray_io():
    d = Marray(np.random.random((10, 10)))
    assert_read_matches_write(d)
示例#22
0
def test_io():
    model = Model(Sphere(par(1)), Mie.calc_holo)
    assert_read_matches_write(model)

    model = Model(Sphere(par(1)), Mie(False).calc_holo)
    assert_read_matches_write(model)
示例#23
0
 def test_base_io(self):
     result = FitResult(DATA, MODEL, CmaStrategy(), 10,
                        {'intervals': INTERVALS})
     assert_read_matches_write(result)
示例#24
0
 def test_saved_calculations(self):
     result = generate_fit_result()
     result.best_fit
     result.max_lnprob
     assert_read_matches_write(result)