def test_emb_metrics(self): # Test embedding metrics dis = Distance(ImgFormat.EMBEDDINGS) metrics = dis.get_metrics() # Test manhattan distance self.assertEqual(metrics.manhattan(self.emb_0, self.emb_1), 3) self.assertEqual(metrics.manhattan(self.emb_1, self.emb_1), 0) self.assertAlmostEqual(metrics.manhattan(self.emb_123, self.emb_1), 3) self.assertAlmostEqual(metrics.manhattan(self.emb_0, self.emb_123), 6) # Test euclidean distance self.assertAlmostEqual(metrics.euclidean(self.emb_0, self.emb_1), np.sqrt(3)) self.assertEqual(metrics.euclidean(self.emb_1, self.emb_1), 0) self.assertAlmostEqual(metrics.euclidean(self.emb_123, self.emb_0), np.sqrt((1**2) + (2**2) + (3**2))) # Test exception with self.assertRaises(TypeError): metrics.manhattan(self.emb_0.tolist(), self.emb_1) with self.assertRaises(ValueError): metrics.manhattan(self.emb_0, np.ones(4)) with self.assertRaises(TypeError): metrics.euclidean(self.emb_0.tolist(), self.emb_1) with self.assertRaises(ValueError): metrics.euclidean(self.emb_0, np.ones(4))
def test_rgb_metrics(self): # Test RGB metrics dis = Distance(ImgFormat.RGB) metrics = dis.get_metrics() # Test manhattan distance self.assertEqual(metrics.manhattan(self.img_rgb_0, self.img_rgb_255), 255.0) self.assertEqual(metrics.manhattan(self.img_rgb_255, self.img_rgb_0), 255.0) self.assertAlmostEqual( metrics.manhattan(self.img_rgb_topleft, self.img_rgb_botright), (abs(255 - 200) + abs(255 - 100)) / (3 * 3)) self.assertEqual(metrics.manhattan(self.img_rgb_255, self.img_rgb_255), 0) # Test sum squared distance self.assertEqual(metrics.sum_squared(self.img_rgb_0, self.img_rgb_255), 1.0) self.assertEqual( metrics.sum_squared(self.img_rgb_0, self.img_rgb_topleft), 1.0) self.assertAlmostEqual( metrics.sum_squared(self.img_rgb_topleft, self.img_rgb_botright), 0.049633607) self.assertAlmostEqual( metrics.sum_squared(self.img_rgb_255, self.img_rgb_botright), 0.005283143) self.assertEqual( metrics.sum_squared(self.img_rgb_255, self.img_rgb_255), 0) # Test cross correlation distance self.assertEqual( metrics.cross_correlation(self.img_rgb_0, self.img_rgb_255), 0) self.assertAlmostEqual( metrics.cross_correlation(self.img_rgb_topleft, self.img_rgb_botright), 0.9755619) self.assertAlmostEqual( metrics.cross_correlation(self.img_rgb_255, self.img_rgb_botright), 0.99759716) self.assertEqual( metrics.cross_correlation(self.img_rgb_255, self.img_rgb_255), 1.0) # Test exception with self.assertRaises(TypeError): metrics.manhattan(self.img_rgb_0.tolist(), self.img_rgb_255) with self.assertRaises(ValueError): metrics.manhattan(self.img_gray_0, self.img_rgb_255) with self.assertRaises(TypeError): metrics.sum_squared(self.img_rgb_0.tolist(), self.img_rgb_255) with self.assertRaises(ValueError): metrics.sum_squared(self.img_gray_0, self.img_rgb_255) with self.assertRaises(TypeError): metrics.cross_correlation(self.img_rgb_0.tolist(), self.img_rgb_255) with self.assertRaises(ValueError): metrics.cross_correlation(self.img_gray_0, self.img_rgb_255)