Beispiel #1
0
 def test_triplicate_name(self):
     tied = prior.Uniform(-5, 5, name='dummy')
     sphere = Sphere(n=prior.Uniform(1, 2, name='dummy'),
                     r=prior.Uniform(1, 2, name='dummy'),
                     center=[tied, tied, prior.Uniform(0, 10, name='z')])
     model = AlphaModel(sphere)
     expected = ['dummy', 'dummy_0', 'dummy_1', 'z']
     self.assertEqual(model._parameter_names, expected)
Beispiel #2
0
 def test_add_tie_updates_parameter_names(self):
     tied = prior.Uniform(-5, 5)
     sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(1, 2),
                     center=[tied, tied, 10])
     model = AlphaModel(sphere)
     model.add_tie(['r', 'n'])
     expected = ['n', 'center.0']
     self.assertEqual(model._parameter_names, expected)
Beispiel #3
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 #4
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([0, 0]), desired_sigma * 2)
Beispiel #5
0
 def test_parameters_checks_ties(self):
     spheres = [Sphere(n=Uniform(0, 1), r=Uniform(0, 1)) for i in range(3)]
     ties = {'r': ['0:r', '1:r', '2:r'], 'n': ['0:n', '1:n', '2:n']}
     spheres = Spheres(spheres, ties=ties)
     spheres.ties['n'].append('dummy_name')
     msg = 'Tied parameter dummy_name not present in raw parameters'
     with self.assertRaisesRegex(ValueError, msg):
         spheres.parameters
Beispiel #6
0
 def test_parameters_map(self):
     tied = prior.Uniform(0, 1)
     scatterer = Sphere(n=tied, r=prior.Uniform(0.5, 1.5),
                        center=[tied, 10, prior.Uniform(0, 10)])
     model = AlphaModel(scatterer)
     expected = [dict, [[['n', '_parameter_0'], ['r', '_parameter_1'],
                         ['center', ['_parameter_0', 10, '_parameter_2']]]]]
     self.assertEqual(model._maps['scatterer'], expected)
Beispiel #7
0
 def test_from_parameters_keeps_attributes(self):
     spheres = [
         Sphere(n=np.random.rand(), r=0.1, center=[i, i, i])
         for i in range(3)
     ]
     spheres = Spheres(spheres, warn="TEST")
     spheres = spheres.from_parameters(spheres.parameters)
     self.assertEqual(spheres.warn, "TEST")
Beispiel #8
0
 def test_parameterize_scatterer_spheres(self):
     sphere = Sphere(r=0.5, n=1, center=[0, 0, 0])
     model = make_default_model(Spheres([sphere, sphere], warn=False))
     expected = {
         '0:n', '1:n', '0:r', '1:r', 'alpha', '0:x', '0:y', '0:z', '1:x',
         '1:y', '1:z'
     }
     self.assertEqual(set(model.parameters.keys()), expected)
Beispiel #9
0
 def test_add_tie_specify_name(self):
     tied = prior.Uniform(-5, 5)
     sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(1, 2),
                     center=[tied, tied, 10])
     model = AlphaModel(sphere)
     model.add_tie(['r', 'n'], new_name='dummy')
     expected = ['dummy', 'center.0']
     self.assertEqual(model._parameter_names, expected)
Beispiel #10
0
 def make_scatterer(self, params):
     center = self._make_center_priors(params)
     index = prior.Uniform(
         self._min_index, self._max_index, guess=params['n'])
     radius = prior.Uniform(
         self._min_radius, self._max_radius, guess=params['r'])
     scatterer = Sphere(n=index, r=radius, center=center)
     return scatterer
Beispiel #11
0
 def test_add_tie_updates_map(self):
     tied = prior.Uniform(-5, 5)
     sphere = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(1, 2),
                     center=[tied, tied, 10])
     model = AlphaModel(sphere)
     model.add_tie(['r', 'n'])
     expected = [dict, [[['n', '_parameter_0'], ['r', '_parameter_0'],
                         ['center', ['_parameter_1', '_parameter_1', 10]]]]]
     self.assertEqual(model._maps['scatterer'], expected)
Beispiel #12
0
 def test_parameters_list(self):
     tied = prior.Uniform(0, 1)
     scatterer = Sphere(n=tied, r=prior.Uniform(0.5, 1.5),
                        center=[tied, 10, prior.Uniform(0, 10)])
     model = AlphaModel(scatterer)
     expected = [prior.Uniform(0, 1),
                 prior.Uniform(0.5, 1.5),
                 prior.Uniform(0, 10)]
     self.assertEqual(model._parameters, expected)
Beispiel #13
0
 def test_yaml_preserves_parameter_ties(self):
     tied = prior.Uniform(0, 1)
     sphere = Sphere(n=tied, r=prior.Uniform(0.6, 1, name='radius'),
                     center=[prior.Uniform(0.6, 1), tied, 10])
     alpha = {'r': 0.6, 'g': prior.Uniform(0.8, 0.9)}
     model = AlphaModel(sphere, alpha=alpha)
     model.add_tie(['radius', 'center.0'])
     post_model = take_yaml_round_trip(model)
     self.assertEqual(model.parameters, post_model.parameters)
Beispiel #14
0
 def test_yaml_preserves_parameter_names(self):
     n = prior.ComplexPrior(prior.Uniform(1, 2), prior.Uniform(0, 0.1))
     sphere = Sphere(n=n, r=prior.Uniform(0, 1.5, name='radius'),
                     center=[1, 1, prior.Uniform(10, 20)])
     alpha = {'r': 0.6, 'g': prior.Uniform(0.6, 1.0)}
     model = AlphaModel(sphere, alpha=alpha)
     pre_names = model._parameter_names
     post_names = take_yaml_round_trip(model)._parameter_names
     self.assertEqual(pre_names, post_names)
Beispiel #15
0
 def test_optics_from_schema(self):
     model = AlphaModel(Sphere(), medium_index=prior.Uniform(1, 2))
     schema = detector_grid(2, 2)
     schema.attrs['illum_wavelen'] = 0.6
     schema.attrs['illum_polarization'] = [1, 0]
     found_optics = model._find_optics([1.5], schema)
     expected = {'medium_index': 1.5, 'illum_wavelen': 0.6,
                 'illum_polarization': [1, 0]}
     self.assertEqual(found_optics, expected)
Beispiel #16
0
 def test_add_tie(self):
     spheres = [Sphere(n=Uniform(0, 1), r=Uniform(0, 1)) for i in range(3)]
     spheres = Spheres(spheres)
     spheres.add_tie('0:r', '1:r')
     spheres.add_tie('r', '2:r')
     spheres.add_tie('0:n', '1:n')
     spheres.add_tie('0:n', '2:n')
     expected_keys = {'n', 'r'}
     self.assertEqual(set(spheres.parameters.keys()), expected_keys)
Beispiel #17
0
 def test_model_optics_take_precedence(self):
     model = AlphaModel(Sphere(), medium_index=1.5, illum_wavelen=0.8)
     schema = detector_grid(2, 2)
     schema.attrs['illum_wavelen'] = 0.6
     schema.attrs['illum_polarization'] = [1, 0]
     found_optics = model._find_optics([], schema)
     expected = {'medium_index': 1.5, 'illum_wavelen': 0.8,
                 'illum_polarization': [1, 0]}
     self.assertEqual(found_optics, expected)
Beispiel #18
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)
Beispiel #19
0
    def test_raw_scat_matrs_same_as_mie(self):
        theory_mie = Mie()
        theory_tmat = Tmatrix()

        pos = np.array([10, 0, 0])[:,None]
        s = Sphere(n=1.59, r=0.9, center=(2, 2, 80))

        s_mie = theory_mie._raw_scat_matrs(s, pos, 2*np.pi/.660, 1.33)
        s_tmat = theory_tmat._raw_scat_matrs(s, pos, 2*np.pi/.660, 1.33)
        self.assertTrue(np.allclose(s_mie, s_tmat))
Beispiel #20
0
 def test_calc_holo_with_twocolor_alpha(self):
     detector = detector_grid(
         5, 1, extra_dims={'illumination': ['red', 'green']})
     scatterer = Sphere(
         r=0.5, n={'red': 1.5, 'green': 1.6}, center=(2, 2, 2))
     alpha = {'red': 0.8, 'green': 0.9}
     result = calc_holo(
         detector, scatterer, scaling=alpha, illum_polarization=(0, 1),
         illum_wavelen={'red': 0.66, 'green': 0.52}, medium_index=1.33)
     assert result is not None
Beispiel #21
0
 def test_save_and_open(self):
     n = Uniform(0, 1)
     r = Uniform(0, np.pi / 2)
     spheres = [Sphere(n=n, r=r, center=[i, i, i]) for i in range(3)]
     spheres = Spheres(spheres)
     expected_keys = spheres.parameters.keys()
     with tempfile.TemporaryDirectory() as tempdir:
         hp.save(tempdir + '/test.out', spheres)
         spheres = hp.load(tempdir + '/test.out')
     self.assertEqual(spheres.parameters.keys(), expected_keys)
Beispiel #22
0
 def test_ties(self):
     n = Uniform(0, 1)
     r = Uniform(0, 1)
     spheres = [Sphere(n=n, r=r, center=[i, i, i]) for i in range(3)]
     spheres = Spheres(spheres)
     expected_ties = {
         'n': ['0:n', '1:n', '2:n'],
         'r': ['0:r', '1:r', '2:r']
     }
     self.assertEqual(spheres.ties, expected_ties)
Beispiel #23
0
def test_layered():
    s = Sphere(n=(1, 2), r=(1, 2), center=(2, 2, 2))
    sch = detector_grid((10, 10), .2)
    hs = calc_holo(sch, s, 1, .66, (1, 0))

    guess = LayeredSphere((1, 2), (Uniform(1, 1.01), Uniform(.99, 1)),
                          (2, 2, 2))
    model = ExactModel(guess, calc_holo)
    res = NmpfitStrategy().fit(model, hs)
    assert_allclose(res.scatterer.t, (1, 1), rtol=1e-12)
Beispiel #24
0
def test_propagate_e_field():
    e = calc_field(detector_grid(100, 0.1),
                   Sphere(1.59, .5, (5, 5, 5)),
                   illum_wavelen=0.66,
                   medium_index=1.33,
                   illum_polarization=(1, 0),
                   theory=Mie(False))

    prop_e = propagate(e, 10)
    verify(prop_e, 'propagate_e_field')
Beispiel #25
0
 def test_equal_not_identical_do_not_tie(self):
     scatterer = Sphere(n=prior.Uniform(1, 2), r=prior.Uniform(1, 2),
                        center=[10, 10, prior.Uniform(1, 2)])
     model = AlphaModel(scatterer)
     expected_priors = [prior.Uniform(1, 2),
                        prior.Uniform(1, 2),
                        prior.Uniform(1, 2)]
     expected_names = ['n', 'r', 'center.2']
     self.assertEqual(model._parameters, expected_priors)
     self.assertEqual(model._parameter_names, expected_names)
Beispiel #26
0
 def test_transformed_priors_are_tied(self):
     base_prior = prior.Uniform(0, 2, name='x')
     transformed = prior.TransformedPrior(np.sqrt, base_prior, name='y')
     scatterer = Sphere(n=1.5, r=0.5, center=[base_prior, transformed,
                                              prior.Uniform(5, 10)])
     model = AlphaModel(scatterer)
     expected_priors = [base_prior, prior.Uniform(5, 10)]
     expected_names = ['x', 'center.2']
     self.assertEqual(model._parameters, expected_priors)
     self.assertEqual(model._parameter_names, expected_names)
Beispiel #27
0
    def test_raw_fields_similar_to_mie(self):
        theory_mie = Mie(False, False)
        theory_tmat = Tmatrix()

        pos = np.array([10, 0, 0])[:,None]
        s = Sphere(n=1.59, r=0.9, center=(2, 2, 80))
        pol = pd.Series([1, 0])

        fields_mie = theory_mie._raw_fields(pos, s, 2*np.pi/.660, 1.33, pol)
        fields_tmat = theory_tmat._raw_fields(pos, s, 2*np.pi/.660, 1.33, pol)
        self.assertTrue(np.allclose(fields_mie, fields_tmat))
Beispiel #28
0
def residfunct(p, fjac=None):
    # nmpfit calls residfunct w/fjac as a kwarg, we ignore

    sphere = Sphere(n=p[0] + n_particle_imag * 1j, r=p[1], center=p[2:5])
    thry = Mie(False)
    calculated = calc_holo(holo, sphere, scaling=p[5], theory=thry)

    status = 0
    derivates = holo - calculated

    return ([status, get_values(flat(derivates))])
Beispiel #29
0
 def test_alpha_subset_tempering(self):
     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))
     strat = TemperedStrategy(nwalkers=4, nsamples=10, stages=1,
                              stage_len=10, parallel=None, seed=40)
     with warnings.catch_warnings():
         warnings.simplefilter('ignore')
         inference_result = strat.sample(mod, holo)
     desired_alpha = np.array([0.650348])
     assert_allclose(inference_result._parameters, desired_alpha, rtol=5e-3)
Beispiel #30
0
 def test_tied_parameter_naming(self):
     n = Uniform(0, 1)
     r = [Uniform(0, 1), Uniform(0, 1)]
     spheres = [Sphere(n=n, r=r[i % 2]) for i in range(4)]
     spheres = Spheres(spheres)
     expected_ties = {
         'n': ['0:n', '1:n', '2:n', '3:n'],
         'r': ['0:r', '2:r'],
         '1:r': ['1:r', '3:r']
     }
     self.assertEqual(spheres.ties, expected_ties)