def test_prob_2_priors(self): real = Uniform(*(np.random.rand(2) + np.array([0, 1]))) imag = Uniform(*(np.random.rand(2) + np.array([0, 1]))) cp = ComplexPrior(real, imag) self.assertEqual(cp.prob(1), 0) self.assertEqual(cp.prob(1.0j), 0) self.assertAlmostEqual(cp.prob(1 + 1.0j), real.prob(1) * imag.prob(1))
def test_lnprob_2_priors(self): real = Uniform(*np.random.rand(2) + np.array([0, 1])) imag = Uniform(*np.random.rand(2) + np.array([0, 1])) cp = ComplexPrior(real, imag) self.assertEqual(cp.lnprob(1), -np.inf) self.assertEqual(cp.lnprob(1.0j), -np.inf) self.assertEqual(cp.lnprob(1 + 1.0j), real.lnprob(1) + imag.lnprob(1))
def test_lnprob_fixed_real(self): real = np.random.rand() imag = Uniform(*(np.random.rand(2) + np.array([0, 1]))) cp = ComplexPrior(real, imag) self.assertEqual(cp.lnprob(1), -np.inf) self.assertEqual(cp.lnprob(1.0j), imag.lnprob(1)) self.assertEqual(cp.lnprob(1 + 1.0j), imag.lnprob(1))
def test_sample_fixed_imag(self): n_samples = 10 real = Uniform(*(np.random.rand(2) + np.array([0, 1]))) imag = np.random.rand() cp = ComplexPrior(real, imag) samples = cp.sample(n_samples) self.assertTrue(np.all(samples.real < real.upper_bound)) self.assertTrue(np.all(samples.real > real.lower_bound)) self.assertTrue(np.all(samples.imag == imag))
def test_sample_fixed_real(self): n_samples = 10 real = np.random.rand() imag = Uniform(*(np.random.rand(2) + np.array([0, 1]))) cp = ComplexPrior(real, imag) samples = cp.sample(n_samples) self.assertTrue(np.all(samples.real == real)) self.assertTrue(np.all(samples.imag < imag.upper_bound)) self.assertTrue(np.all(samples.imag > imag.lower_bound))
def _make_model(self): sphere = 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(sphere, theory=Mie(False), alpha=Uniform(0.1, 1, 0.6)) return model
def test_complex_prior(self): self.assertEqual(self.c + 2 + 1j, ComplexPrior(self.u + 2, self.g + 1)) self.assertEqual(self.c + 2, ComplexPrior(self.u + 2, self.g)) self.assertEqual(self.c * 2, ComplexPrior(self.u * 2, self.g * 2)) self.assertEqual(-self.c, ComplexPrior(-self.u, -self.g)) cp = ComplexPrior(2, self.g) self.assertEqual(self.c + cp, ComplexPrior(self.u + 2, self.g + self.g))
def test_expand_parameters(self): array_f = xr.DataArray([[11, 12, 13], [14, 15, 16]], dims=['d2', 'd3'], coords={ 'd3': ['H', 'He', 'Li'], 'd2': ['Left', 'Right'] }) compressed = { 'a': 0, 'b': [0.5, 1, 2], 'c': { 'c1': 3, 'c2': 4 }, 'd': ComplexPrior(5, 6), 'e': { 'e1': ComplexPrior(7, 8), 'e2': [9, 10] }, 'f': array_f } expansion = dict(_expand_parameters(compressed.items())) self.assertTrue(expansion == self.expanded)
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_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)
def __new__(self, real, imag, name=None): fit_warning('hp.inference.prior.ComplexPrior', 'ComplexParameter') if isinstance(real, Prior) or isinstance(imag, Prior): return ComplexPrior(real, imag, name) else: return real + 1.0j * imag
def test_my_properties(self): self.assertEqual(self.u, Uniform(1, 2)) self.assertEqual(self.g, Gaussian(1, 2)) self.assertEqual(self.b, BoundedGaussian(1, 2, 0, 3)) self.assertEqual(self.c, ComplexPrior(self.u, self.g))
def c(self): return ComplexPrior(self.u, self.g)
def test_guess_fixed_real(self): real = np.random.rand() imag = Uniform(*(np.random.rand(2) + np.array([0, 1]))) cp = ComplexPrior(real, imag) self.assertEqual(cp.guess, real + 1.0j * imag.guess)
def test_construction(self): parameters = {'real': Uniform(1, 2), 'imag': 3, 'name': 'a'} cp = ComplexPrior(**parameters) self.assertTrue(isinstance(cp, Prior)) for key, val in parameters.items(): self.assertEqual(getattr(cp, key), val)