Пример #1
0
    def test_estimating_two_2d_gaussians(self):
        svectors = [
            SuperVector([0, 0]),
            SuperVector([0, 2]),
            SuperVector([2, 0]),
            SuperVector([2, 2]),
            SuperVector([5, 5]),
            SuperVector([5, 4]),
            SuperVector([5, 6]),
            SuperVector([4, 5]),
            SuperVector([6, 5]),
        ]
        gaussians = em.estimate_n_gaussians(svectors, 2, 40)

        expected_first_mean = np.array([1, 1], dtype=float)
        expected_second_mean = np.array([5, 5], dtype=float)

        expected_first_cov = np.array([1, 1], dtype=float)
        expected_second_cov = np.array([0.4, 0.4], dtype=float)

        expected_gaussians = [
            Gaussian(expected_first_mean, expected_first_cov),
            Gaussian(expected_second_mean, expected_second_cov)
        ]

        self.assertTrue(are_gaussians_equal(gaussians, expected_gaussians))
Пример #2
0
 def test_equal_of_two_gaussians_with_different_dimensions(self):
     meanA = np.array([3], dtype=float)
     covA = np.array([10], dtype=float)
     meanB = np.array([3, 5.000001, 3.999999], dtype=float)
     covB = np.array([10.00001, 20, 40], dtype=float)
     first_gaussian = Gaussian(meanA, covA)
     second_gaussian = Gaussian(meanB, covB)
     self.assertFalse(first_gaussian.is_equal_to(second_gaussian, tolerance=0.0001))
Пример #3
0
 def test_equal_three_dimensional_gaussians_with_too_small_tolerance(self):
     meanA = np.array([3, 5, 4], dtype=float)
     covA = np.array([10, 20, 40], dtype=float)
     meanB = np.array([3, 5.000001, 3.999999], dtype=float)
     covB = np.array([10.001, 20, 40], dtype=float)
     first_gaussian = Gaussian(meanA, covA)
     second_gaussian = Gaussian(meanB, covB)
     self.assertFalse(first_gaussian.is_equal_to(second_gaussian, tolerance=0.0001))
Пример #4
0
 def test_equal_two_dimensional_gaussians_with_too_small_tolerance(self):
     meanA = np.array([3, 5], dtype=float)
     covA = np.array([3, 2], dtype=float)
     meanB = np.array([3, 5.1], dtype=float)
     covB = np.array([3.00001, 2], dtype=float)
     first_gaussian = Gaussian(meanA, covA)
     second_gaussian = Gaussian(meanB, covB)
     self.assertFalse(first_gaussian.is_equal_to(second_gaussian, tolerance=0.0001))
Пример #5
0
 def test_equal_one_dimensional_gaussians(self):
     meanA = np.array([3], dtype=float)
     covA = np.array([3], dtype=float)
     meanB = np.array([3], dtype=float)
     covB = np.array([3.00001], dtype=float)
     first_gaussian = Gaussian(meanA, covA)
     second_gaussian = Gaussian(meanB, covB)
     self.assertTrue(first_gaussian.is_equal_to(second_gaussian, tolerance=0.0001))
Пример #6
0
    def test_estimating_one_gaussian(self):
        svectors = [SuperVector([1, 3, 2]), SuperVector([3, 5, 1])]
        gaussians = em.estimate_n_gaussians(svectors, 1, 1)

        expected_mean = np.array([2, 4, 1.5], dtype=float)
        expected_cov = np.array([1, 1, 0.25], dtype=float)

        expected_gaussians = [Gaussian(expected_mean, expected_cov)]

        self.assertTrue(are_gaussians_equal(gaussians, expected_gaussians))
Пример #7
0
    def test_are_gaussians_equal_helper_method(self):
        first_list = []
        second_list = []
        self.assertTrue(are_gaussians_equal(first_list, second_list))

        cov = np.array([3, 2], dtype=float)
        gaussian_A = Gaussian(np.array([3, 5], dtype=float), cov)
        gaussian_B = Gaussian(np.array([5, 3], dtype=float), cov)
        gaussian_C = Gaussian(np.array([4, 4], dtype=float), cov)

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_A, gaussian_B, gaussian_C]
        self.assertTrue(are_gaussians_equal(first_list, second_list))

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_C, gaussian_B, gaussian_A]
        self.assertTrue(are_gaussians_equal(first_list, second_list))

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_C, gaussian_A, gaussian_B]
        self.assertTrue(are_gaussians_equal(first_list, second_list))

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_C, gaussian_A]
        self.assertFalse(are_gaussians_equal(first_list, second_list))

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_A, gaussian_B, gaussian_B, gaussian_C]
        self.assertFalse(are_gaussians_equal(first_list, second_list))

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_A, gaussian_B, gaussian_B, gaussian_C]
        self.assertFalse(are_gaussians_equal(first_list, second_list))

        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_A, gaussian_B, gaussian_B]
        self.assertFalse(are_gaussians_equal(first_list, second_list))

        gaussian_D = Gaussian(np.array([4, 4], dtype=float), cov)
        first_list = [gaussian_A, gaussian_B, gaussian_C]
        second_list = [gaussian_D, gaussian_B, gaussian_A]
        self.assertTrue(are_gaussians_equal(first_list, second_list))
Пример #8
0
    def test_estimating_three_2d_gaussians(self):
        svectors = [
            SuperVector([0, 0]),
            SuperVector([0, 2]),
            SuperVector([2, 0]),
            SuperVector([2, 2]),
            SuperVector([7, 9]),
            SuperVector([9, 7]),
            SuperVector([7, 7]),
            SuperVector([9, 9]),
            SuperVector([5, 5]),
            SuperVector([5, 4]),
            SuperVector([5, 6]),
            SuperVector([4, 5]),
            SuperVector([6, 5]),
        ]
        gaussians = em.estimate_n_gaussians(svectors, 3, 13)

        expected_first_mean = np.array([1, 1], dtype=float)
        expected_second_mean = np.array([8, 8], dtype=float)
        expected_third_mean = np.array([5, 5], dtype=float)

        expected_first_cov = np.array([1, 1], dtype=float)
        expected_second_cov = np.array([1, 1], dtype=float)
        expected_third_cov = np.array([0.4, 0.4], dtype=float)

        expected_gaussians = [
            Gaussian(expected_first_mean, expected_first_cov),
            Gaussian(expected_second_mean, expected_second_cov),
            Gaussian(expected_third_mean, expected_third_cov)
        ]

        print("expected")
        print(expected_gaussians[0].get_mean())
        print(expected_gaussians[0].get_covariance())

        print("actual")
        print(gaussians[0].get_mean())
        print(gaussians[0].get_covariance())

        self.assertTrue(are_gaussians_equal(gaussians, expected_gaussians))
Пример #9
0
    def test_gaussian_command_conversion(self):
        if not os.path.exists("src/tests/testdata"):
            os.makedirs("src/tests/testdata")
        try:
            os.remove("src/tests/testdata/test_gaussian_command.json")
        except FileNotFoundError:
            pass

        pis = [3.11, 5]
        means = [
            np.array([2.0, 1.6], dtype=float),
            np.array([1.0, 2.2222], dtype=float)
        ]
        variances = [
            np.array([[2, 0], [0, 3]], dtype=float),
            np.array([[5, 0], [0, 1.1]], dtype=float)
        ]

        input_command_name = "test_gaussian_command"
        input_gaussians = [
            Gaussian(pis[0], means[0], variances[0]),
            Gaussian(pis[1], means[1], variances[1])
        ]

        input_command = GaussianCommand(input_command_name, input_gaussians)
        input_command.save_to_json_file(
            "src/tests/testdata/test_gaussian_command.json")

        output_command = GaussianCommand.from_json_file(
            "src/tests/testdata/test_gaussian_command.json")

        self.assertEqual(input_command.get_name(), output_command.get_name())
        self.assertTrue(
            input_command.get_gaussians()[0].is_equal_with_gaussian(
                output_command.get_gaussians()[0]))
        self.assertTrue(
            input_command.get_gaussians()[1].is_equal_with_gaussian(
                output_command.get_gaussians()[1]))
Пример #10
0
 def from_json_file(cls, json_filename):
     file = open(json_filename, "r")
     json_str = file.read()
     file.close()
     # TODO Handle that
     json_str = json.JSONDecoder().decode(json_str)
     name = json_str["name"]
     gaussians_count = len(json_str["gaussians"])
     gaussians = []
     for i in range(gaussians_count):
         pi = json_str["gaussians"][i]["pi"]
         mean = np.array(json_str["gaussians"][i]["mean"], dtype=float)
         var = np.array(json_str["gaussians"][i]["variances"], dtype=float)
         gaussians.append(Gaussian(pi, mean, var))
     return cls(name, gaussians)
Пример #11
0
 def __gaussians_from_json(cls, obj):
     gaussians = []
     for g in iter(obj):
         gaussians.append(Gaussian.from_json_obj(g))
     return obj
Пример #12
0
 def test_zero_dimension_covariance(self):
     mean = np.array([3])
     cov = np.array([])
     with self.assertRaises(Gaussian.ZeroDimensionsException):
         Gaussian(mean, cov)
Пример #13
0
def maximization_step(svectors, weights):
    mean = __super_vectors_mean(svectors, weights)
    sigma = __super_vectors_sigma(svectors, mean, weights)
    return Gaussian(mean, sigma)
Пример #14
0
def __generate_random_gaussians(n_gaussians, n_dimensions, one_vector):
    gaussians = []
    for _ in range(n_gaussians):
        gaussians.append(
            Gaussian.generate_random_gaussian(n_dimensions, one_vector))
    return gaussians
Пример #15
0
 def test_different_dimensions_for_mean_and_covariance(self):
     mean = np.array([3])
     cov = np.array([3, 2])
     with self.assertRaises(Gaussian.MeanAndCovarianceHaveDifferentSizesException):
         Gaussian(mean, cov)
Пример #16
0
 def test_create_one_dimensional_gaussian(self):
     mean = np.array([3])
     cov = np.array([3])
     Gaussian(mean, cov)
Пример #17
0
 def test_not_singledimensional_covariance_matrix(self):
     mean = np.array([3])
     cov = np.array([[3]])
     with self.assertRaises(Gaussian.CovarianceMatrixIsNotDimensionedException):
         Gaussian(mean, cov)
Пример #18
0
 def test_two_dimensional_mean_array(self):
     mean = np.array([[3, 2], [5, 1]])
     cov = np.array([3, 2])
     with self.assertRaises(Gaussian.MeanMatrixIsNotSingleDimensionedException):
         Gaussian(mean, cov)
Пример #19
0
 def test_not_numpy_array(self):
     mean = [1, 3]
     cov = [5, 5]
     with self.assertRaises(Gaussian.MeanOrCovarianceIsNotNumpyArrayException):
         Gaussian(mean, cov)
Пример #20
0
 def test_empty_list_for_mean_and_covariance(self):
     mean = np.array([])
     cov = np.array([])
     with self.assertRaises(Gaussian.ZeroDimensionsException):
         Gaussian(mean, cov)
Пример #21
0
 def test_create_three_dimensional_gaussian(self):
     mean = np.array([3, 2, 5])
     cov = np.array([3, 3, 3])
     Gaussian(mean, cov)