예제 #1
0
 def test_random_gaussians_differ(self):
     """
     Test that random Gaussians generated sequentially are different.
     """
     var_names = ["a", "b"]
     random_gaussian_0 = make_random_gaussian(var_names)
     random_gaussian_1 = make_random_gaussian(var_names)
     self.assertEqual(random_gaussian_0.var_names, var_names)
     self.assertFalse(random_gaussian_0.equals(random_gaussian_1))
예제 #2
0
 def test_random_gaussians_same(self):
     """
     Test that random Gaussians generated sequentially with the same random seed are the same.
     """
     var_names = ["a", "b"]
     np.random.seed(0)
     random_gaussian_0 = make_random_gaussian(var_names)
     np.random.seed(0)
     random_gaussian_1 = make_random_gaussian(var_names)
     self.assertTrue(random_gaussian_0.equals(random_gaussian_1))
예제 #3
0
 def test_plot(self):
     """
     Test that the plot method does not break.
     """
     # TODO: improve this test.
     g_1 = make_random_gaussian(var_names=["a"])
     g_1.plot()
     g_1.plot(log=True)
     g_2 = make_random_gaussian(var_names=["a", "b"])
     g_2.plot()
예제 #4
0
 def test_correct_marginal_fails_vars(self):
     """
     Test that the get_marginal function fails when there is no factor containing the variables to keep.
     """
     g_1 = make_random_gaussian(["a", "b"])
     g_2 = make_random_gaussian(["b", "c"])
     cga = ClusterGraph([g_1, g_2])
     cga.process_graph()
     with self.assertRaises(ValueError):
         cga.get_marginal(["a", "c"])
예제 #5
0
 def test_kl_divergence_fails_with_different_dims(self):
     """
     Test that the kl_divergence function fails with a value error when trying to calculate the KL-divergence between
     Gaussians with different dimensionality.
     """
     g_1 = make_random_gaussian(var_names=["a", "b", "c"])
     g_2 = make_random_gaussian(var_names=["a", "b"])
     with self.assertRaises(ValueError) as raises_context:
         g_1.kl_divergence(g_2)
     error_msg = str(raises_context.exception)
     self.assertTrue("dimensionalities" in error_msg)
예제 #6
0
def get_cg2(seed=0, process=False):
    """
    Helper function for making a cluster graph.
    """
    np.random.seed(seed)
    factors = [
        make_random_gaussian(["a", "b"]),
        make_random_gaussian(["c", "b"]),
        make_random_gaussian(["c", "d"]),
        make_random_gaussian(["e", "d"]),
        make_random_gaussian(["e", "f"]),
    ]
    cluster_graph = ClusterGraph(factors)
    if process:
        cluster_graph.process_graph()
    return cluster_graph
예제 #7
0
 def test_kl_divergence_between_same_factors(self):
     """
     Test that the distance between two identical factors is zero.
     """
     np.random.seed(0)
     g_1 = make_random_gaussian(var_names=["a", "b"])
     g_2 = g_1.copy()
     self.assertTrue(g_1.kl_divergence(g_2) == 0.0)
예제 #8
0
 def test_kl_divergence_vac_novac(self):
     """
     Test that the distance between a vacuous and a non-vacuous factor is infinite.
     """
     g_1 = Gaussian.make_vacuous(var_names=["a", "b"])
     g_2 = make_random_gaussian(var_names=["a", "b"])
     self.assertTrue(g_1.kl_divergence(g_2) == np.inf)
     self.assertTrue(g_2.kl_divergence(g_1) == np.inf)
예제 #9
0
 def test_show_vis(self, sns_heatmap_mock):
     """
     Test that the show_vis method does not break.
     """
     # TODO: improve this test.
     g_1 = make_random_gaussian(var_names=["a", "b"])
     g_1.show_vis()
     sns_heatmap_mock.assert_called()
예제 #10
0
 def test__plot_2d_no_limits(self, g_get_limits_for_2d_plot_mock):
     """
     Test that the _factor_utils fu_get_limits_for_2d_plot_mock function is called when no x and y limits are
     provided.
     """
     g_1 = make_random_gaussian(['a', 'b'])
     g_get_limits_for_2d_plot_mock.return_value = ([0, 1], [0, 1])
     g_1._plot_2d(log=False, x_lim=None, y_lim=None)
     g_get_limits_for_2d_plot_mock.assert_called()
예제 #11
0
 def test_show(self, _get_can_repr_str_mock, _get_cov_repr_str_mock):
     """
     Test that the correct repr functions are called when show is called.
     """
     g_1 = make_random_gaussian(['a'])
     g_1._update_canform()
     g_1.show()
     _get_can_repr_str_mock.assert_called()
     _get_can_repr_str_mock.assert_called()
예제 #12
0
 def test_split_gaussian(self):
     """
     Test that the _split_gaussian function splits a gaussian into a mixture with three different components that has
     the same mean an variance.
     """
     g_1 = make_random_gaussian(var_names=["a"])
     gm_1 = g_1._split_gaussian()
     self.assertTrue(len(gm_1.components) == 3)
     g_1_m_projection = gm_1.moment_match_to_single_gaussian()
     self.assertTrue(g_1.equals(g_1_m_projection))
예제 #13
0
 def test_plot_2d(self, fu_plot_2d_mock):
     """
     Test that the _factor_utils plot_2d function is called
     """
     # TODO: improve this test.
     g_1 = make_random_gaussian(['a', 'b'])
     g_1._plot_2d(log=False, x_lim=[0, 1], y_lim=[0, 1])
     fu_plot_2d_mock.assert_called()
     g_1._plot_2d(log=True, x_lim=[0, 1], y_lim=[0, 1])
     fu_plot_2d_mock.assert_called()
예제 #14
0
    def test_get_sigma_points_array(self):
        """
        Test that the computed sigma points are correct.
        """
        g_1 = gauss.make_random_gaussian(var_names=["a", "b", "c"])
        expected_cov = g_1.get_cov()
        expected_mean = g_1.get_mean()
        sps = _sigma_points.get_sigma_points_array(g_1)
        actual_mean = np.expand_dims(np.mean(sps, axis=1), axis=1)
        actual_cov = np.cov(sps, bias=True)

        self.assertTrue(np.allclose(expected_cov, actual_cov))
        self.assertTrue(np.allclose(expected_mean, actual_mean))
예제 #15
0
    def test_init_fail_duplicate_cluster_ids(self):
        """
        Test that the initializer fails when clusters have duplicate cluster_ids and returns the correct error message.
        """
        with mock.patch(
                "veroku.cluster_graph.Cluster.cluster_id",
                new_callable=unittest.mock.PropertyMock) as mock_cluster_id:
            mock_cluster_id.return_value = "same_id"
            with self.assertRaises(ValueError) as error_context:
                ClusterGraph([
                    make_random_gaussian(["a", "b"]),
                    make_random_gaussian(["b", "c"]),
                    make_random_gaussian(["c", "d"]),
                ])
            exception_msg = error_context.exception.args[0]

            self.assertTrue("non-unique" in exception_msg.lower())

            expected_num_same_id_cluster = 3
            actual_same_id_clusters = exception_msg.count("same_id")
            self.assertTrue(expected_num_same_id_cluster,
                            actual_same_id_clusters)
예제 #16
0
    def test_sample(self):
        """
        Test that the samples drawn from a Gaussian distribution have the correct statistics.
        """
        g_1 = make_random_gaussian(var_names=["a", "b", "c"])
        expected_cov = g_1.get_cov()
        expected_mean = g_1.get_mean()

        samples = g_1.sample(1000000)
        actual_mean = np.expand_dims(np.mean(samples, axis=1), axis=1)
        actual_cov = np.cov(samples)

        self.assertTrue(
            np.allclose(expected_cov, actual_cov, rtol=1.0e-3, atol=1.0e-2))
        self.assertTrue(
            np.allclose(expected_mean, actual_mean, rtol=1.0e-3, atol=1.0e-2))