def test_estimate_gaussian_from_supervectors_with_no_variance_at_all(self): svectors = [ SuperVector([4, 4, 4]), SuperVector([4, 4, 4]), SuperVector([4, 4, 4]), ] with self.assertRaises(em.NoVarianceException): em.estimate_n_gaussians(svectors, 1, 20)
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))
def test_estimate_gaussian_from_supervectors_with_same_one_dimension(self): svectors = [ SuperVector([4, 0, 3]), SuperVector([4, 2, 4]), SuperVector([4, 3, 4]), SuperVector([4, 1, 2]), SuperVector([4, 5, 1]), ] with self.assertRaises(em.NoVarianceException): em.estimate_n_gaussians(svectors, 1, 20)
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))
def test_correct_number_of_estimated_gaussians(self): svectors = [SuperVector([1, 3, 2, 4]), SuperVector([3, 5, 5, 3])] gaussians = em.estimate_n_gaussians(svectors, 1, 1) self.assertEqual(len(gaussians), 1, "Invalid number of estimated gaussians!") gaussians = em.estimate_n_gaussians(svectors, 3, 1) self.assertEqual(len(gaussians), 3, "Invalid number of estimated gaussians!") gaussians = em.estimate_n_gaussians(svectors, 18, 1) self.assertEqual(len(gaussians), 18, "Invalid number of estimated gaussians!")
def init_from_mfccphrase(cls, mfccphrase, em_gaussians, em_iterations): vectors = [] for i in range(mfccphrase.count_mfccs()): vectors.append(SuperVector.init_from_mfcc(mfccphrase.get_mfcc(i))) gaussians = em.estimate_n_gaussians(vectors, em_gaussians, em_iterations) return Command(mfccphrase.get_name(), gaussians)
def __match_mfcc(self, mfcc): sv = SuperVector.init_from_mfcc(mfcc) results = [] for com in iter(self.__commands): results.append(com.get_probability_of_this_command(sv.matrix())) max_value = max(results) # TODO: Watch out for floats!!! :O command_id = results.index(max_value) return self.__commands[command_id].name
def test_equal_vectors(self): vectorA = SuperVector([1, 2, 3]) vectorB = SuperVector([1, 2, 3]) self.assertTrue(vectorA.is_equal_to(vectorB))
def test_equality_of_empty_vectors(self): vectorA = SuperVector([]) vectorB = SuperVector([]) self.assertTrue(vectorA.is_equal_to(vectorB))
def test_equality_of_vectors_with_different_length(self): vectorA = SuperVector([1, 2, 3]) vectorB = SuperVector([1, 2, 3, 4]) self.assertFalse(vectorA.is_equal_to(vectorB))
def test_tolerance(self): vectorA = SuperVector([1, 2, 3]) vectorB = SuperVector([1, 2, 4]) self.assertTrue(vectorA.is_equal_to(vectorB, tolerance=1))
def test_not_equal_vectors_with_same_length(self): vectorA = SuperVector([1, 2, 3]) vectorB = SuperVector([1, 2, 4]) self.assertFalse(vectorA.is_equal_to(vectorB))
def test_initialization_from_mfcc(self): mfcc = MFCC([[1, 2], [3, 4]]) vectorA = SuperVector.init_from_mfcc(mfcc) vectorB = SuperVector([1, 2, 3, 4]) self.assertTrue(vectorA.is_equal_to(vectorB))
def test_estimate_gaussian_from_single_supervector(self): svectors = [SuperVector([0, 0])] with self.assertRaises(em.NotEnoughSuperVectorsException): em.estimate_n_gaussians(svectors, 1, 20)
def test_estimating_from_supervectors_with_different_amount_of_dimensions( self): svectors = [SuperVector([0, 0]), SuperVector([0, 2, 4])] with self.assertRaises(em.DifferentNumberOfDimensionsException): em.estimate_n_gaussians(svectors, 1, 20)
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))