Exemplo n.º 1
0
 def test_prob(self):
     np.random.seed(992)
     mean, sd = np.random.rand(2)
     g = Gaussian(mean, sd)
     norm = 1 / np.sqrt(2 * np.pi * sd**2)
     self.assertTrue(np.allclose(g.prob(mean), norm))
     self.assertTrue(np.allclose(g.prob(mean + sd), np.exp(-1 / 2) * norm))
Exemplo n.º 2
0
 def test_sample(self):
     n_samples = 10000
     np.random.seed(37)
     mean, sd = np.random.rand(2)
     g = Gaussian(mean, sd)
     samples = g.sample(n_samples)
     mean_sd = sd / np.sqrt(n_samples)  # by central limit theorem
     self.assertTrue(np.allclose(mean, samples.mean(), atol=3 * mean_sd))
     self.assertTrue(np.allclose(sd, samples.std(ddof=1), atol=0.01))
Exemplo n.º 3
0
 def test_prob(self):
     mean, sd = np.random.rand(2)
     g = Gaussian(mean, sd)
     bg = BoundedGaussian(mean, sd, -1, 2)
     self.assertEqual(bg.prob(1), g.prob(1))
     self.assertEqual(bg.prob(-1), g.prob(-1))
     self.assertEqual(bg.prob(2), g.prob(2))
     self.assertEqual(bg.prob(-2), 0)
     self.assertEqual(bg.prob(3), 0)
Exemplo n.º 4
0
def test_scale_factor():
    p1 = Gaussian(3, 1)
    assert_equal(p1.scale_factor, 3)
    p2 = Gaussian(0, 2)
    assert_equal(p2.scale_factor, 2)
    p4 = Uniform(-1, 1, 0)
    assert_equal(p4.scale_factor, 0.2)
    p5 = Uniform(1, 4)
    assert_equal(p5.scale_factor, 2.5)
    p6 = Uniform(0, np.inf)
    assert_equal(p6.scale_factor, 1)
    assert_equal(p2.scale(10), 5)
    assert_equal(p2.unscale(5), 10)
Exemplo n.º 5
0
def test_generate_guess():
    gold1 = np.array([[-0.091949, 0.270532], [-1.463350, 0.691041],
                      [1.081791, 0.220404], [-0.239325, 0.811950],
                      [-0.491129, 0.010526]])
    gold2 = np.array([[-0.045974, 0.535266], [-0.731675, 0.745520],
                      [0.540895, 0.510202], [-0.119662, 0.805975],
                      [-0.245564, 0.405263]])
    pars = [Gaussian(0, 1), Uniform(0, 1, 0.8)]
    guess1 = generate_guess(pars, 5, seed=22)
    guess2 = generate_guess(pars, 5, scaling=0.5, seed=22)
    assert_allclose(guess1, gold1, atol=1e-5)
    assert_allclose(guess2, gold2, atol=1e-5)
Exemplo n.º 6
0
 def test_basic(self):
     expected = [Gaussian(0, 2), Gaussian(2, 2), Uniform(0, 40)]
     evaluated = make_center_priors(self.image)
     self.assertEqual(evaluated, expected)
Exemplo n.º 7
0
 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))
Exemplo n.º 8
0
 def g(self):
     return Gaussian(1, 2)
Exemplo n.º 9
0
def test_gaussian():
    g = Gaussian(1, 1)
    assert_equal(g.guess, 1)
    assert_obj_close(g.lnprob(0), gold_sigma)
Exemplo n.º 10
0
 def test_gaussian_constant_addition(self):
     self.assertEqual(self.g + 1., Gaussian(2, 2.))
     self.assertEqual(-self.g, Gaussian(-1, 2))
Exemplo n.º 11
0
 def test_guess(self):
     mean = np.random.rand()
     g = Gaussian(mean, 1)
     self.assertEqual(g.guess, mean)
     self.assertRaises(AttributeError, setattr, g, 'guess', 2)  # property
Exemplo n.º 12
0
 def test_xy_uncertainty(self):
     expected = [Gaussian(0, 4), Gaussian(2, 4), Uniform(0, 40)]
     evaluated = make_center_priors(self.image, xy_uncertainty_pixels=2)
     self.assertEqual(evaluated, expected)
Exemplo n.º 13
0
 def test_variance(self):
     sd = np.random.rand()
     g = Gaussian(0, sd)
     self.assertEqual(g.variance, sd**2)
     self.assertRaises(AttributeError, setattr, g, 'variance',
                       2)  # property
Exemplo n.º 14
0
 def test_construction(self):
     parameters = {'mu': 1, 'sd': 3, 'name': 'a'}
     g = Gaussian(**parameters)
     self.assertTrue(isinstance(g, Prior))
     for key, val in parameters.items():
         self.assertEqual(getattr(g, key), val)
Exemplo n.º 15
0
 def test_prior_array_math(self):
     expected_sum = np.array([Gaussian(1, 2), Gaussian(2, 2)])
     expected_product = np.array([Gaussian(1, 2), Gaussian(2, 4)])
     self.assertTrue(np.all(self.g + np.array([0, 1]) == expected_sum))
     self.assertTrue(np.all(self.g * np.array([1, 2]) == expected_product))
Exemplo n.º 16
0
 def test_adding_2_gaussians(self):
     self.assertEqual(self.g + self.g, Gaussian(2, np.sqrt(8)))
     diff_name_sum = Gaussian(1, 2, 'a') + Gaussian(1, 2, 'b')
     self.assertEqual(diff_name_sum.name, 'GaussianSum')
Exemplo n.º 17
0
 def test_gaussian_multiplication(self):
     self.assertEqual(2 * self.g, Gaussian(2, 4))
     self.assertEqual(self.g * 2, Gaussian(2, 4))
     self.assertEqual(self.g / 2, Gaussian(0.5, 1))
     self.assertEqual(-1 * self.g, Gaussian(-1, 2))
     self.assertEqual(self.g * (-1), Gaussian(-1, 2))
Exemplo n.º 18
0
 def test_z_range_units(self):
     expected = [Gaussian(0, 2), Gaussian(2, 2), Uniform(2, 10)]
     evaluated = make_center_priors(self.image, z_range_units=(2, 10))
     self.assertEqual(evaluated, expected)
Exemplo n.º 19
0
 def test_lnprob(self):
     g = Gaussian(0, 1)
     self.assertTrue(np.allclose(g.lnprob(1), GOLD_SIGMA))
     g = Gaussian(0, 2)
     self.assertTrue(np.allclose(g.lnprob(2), GOLD_SIGMA - np.log(2)))
Exemplo n.º 20
0
def test_prior_math():
    u = Uniform(1, 2)
    g = Gaussian(1, 2)
    b = prior.BoundedGaussian(1, 2, 0, 3)

    assert_equal(u + 1, Uniform(2, 3))
    assert_equal(1 + u, Uniform(2, 3))
    assert_equal(-u, Uniform(-2, -1))
    assert_equal(1 - u, Uniform(-1, 0))
    assert_equal(u - 1, Uniform(0, 1))
    assert_equal(2 * u, Uniform(2, 4))
    assert_equal(u * 2, Uniform(2, 4))
    assert_equal(u / 2, Uniform(0.5, 1))

    print(g + 1.)
    assert_equal(g + 1., Gaussian(2, 2.))
    assert_equal(-g, Gaussian(-1, 2))
    assert_equal(b + 1., prior.BoundedGaussian(2., 2, 1., 4.))
    assert_equal(-b, prior.BoundedGaussian(-1, 2, -3, 0))
    assert_equal(2 * g, Gaussian(2, 4))
    assert_equal(g * 2, Gaussian(2, 4))
    assert_equal(g / 2, Gaussian(0.5, 1))

    assert_equal(g + g, Gaussian(2, np.sqrt(8)))
    assert_equal(g + np.array([0, 1]),
                 np.array([Gaussian(1, 2), Gaussian(2, 2)]))
    assert_equal(g * np.array([1, 2]),
                 np.array([Gaussian(1, 2), Gaussian(2, 4)]))

    with assert_raises(TypeError):
        u + u
    with assert_raises(TypeError):
        g + b
    with assert_raises(TypeError):
        g + [0, 1]
    with assert_raises(TypeError):
        g * g
Exemplo n.º 21
0
def test_gaussian():
    g = Gaussian(1, 1)
    assert_equal(g.guess, 1)
    assert_obj_close(g.lnprob(0),gold_sigma)