From fd3dcc999ff0b3b177e07c54df98947b16b026cf Mon Sep 17 00:00:00 2001 From: Antonio Vilches Date: Wed, 19 Dec 2018 09:49:26 +0100 Subject: [PATCH 1/3] Adding sbd distance method and its test. --- khiva/distances.py | 30 ++++++++++++++++-------- tests/unit_tests/distances_unit_tests.py | 6 +++++ 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/khiva/distances.py b/khiva/distances.py index 85b50b5..e25ffbf 100644 --- a/khiva/distances.py +++ b/khiva/distances.py @@ -28,8 +28,7 @@ def euclidean(tss): between time series 0 and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.euclidean(ctypes.pointer(tss.arr_reference), - ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.euclidean(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) @@ -43,8 +42,7 @@ def dtw(tss): distance between time series 0 and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.dtw(ctypes.pointer(tss.arr_reference), - ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.dtw(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) @@ -58,8 +56,7 @@ def hamming(tss): between time series 0 and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.hamming(ctypes.pointer(tss.arr_reference), - ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.hamming(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) @@ -73,8 +70,7 @@ def manhattan(tss): between time series 0 and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.manhattan(ctypes.pointer(tss.arr_reference), - ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.manhattan(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) @@ -88,6 +84,20 @@ def squared_euclidean(tss): and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.squared_euclidean(ctypes.pointer(tss.arr_reference), - ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.squared_euclidean(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) + return Array(array_reference=b) + + +def sbd(tss): + """ Calculates the Shape-Based distance (SBD). It computes the normalized cross-correlation and + it returns the value that maximizes the correlation value between time series. + + :param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and + dimension one indicates the number of time series. + :return: Array with an upper triangular matrix where each position corresponds to the distance between two time series. + Diagonal elements will be zero. For example: Position row 0 column 1 records the distance between time series 0 + and time series 1. + """ + b = ctypes.c_void_p(0) + KhivaLibrary().c_khiva_library.sbd(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) diff --git a/tests/unit_tests/distances_unit_tests.py b/tests/unit_tests/distances_unit_tests.py index 8ea8ed0..42b0d15 100644 --- a/tests/unit_tests/distances_unit_tests.py +++ b/tests/unit_tests/distances_unit_tests.py @@ -55,6 +55,12 @@ def test_manhattan(self): expected = np.array([[0, 0, 0, 0, 0], [5, 0, 0, 0, 0], [10, 5, 0, 0, 0], [15, 10, 5, 0, 0], [20, 15, 10, 5, 0]]) np.testing.assert_array_almost_equal(result, expected, decimal=2) + def test_sbd(self): + sbd_result = sbd( + Array(data=[[1, 2, 3, 4, 5], [1, 1, 0, 1, 1], [10, 12, 0, 0, 1]])).to_numpy().flatten() + expected = np.array([0, 0, 0, 0.505025, 0, 0, 0.458583, 0.564093, 0]) + np.testing.assert_array_almost_equal(sbd_result, expected, decimal=1) + if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(DistancesTest) From 38dc17b2cc579656db22da7e2d885654d160721d Mon Sep 17 00:00:00 2001 From: Antonio Vilches Date: Wed, 19 Dec 2018 09:56:40 +0100 Subject: [PATCH 2/3] Ordering function and test by alfabetical order. --- khiva/distances.py | 14 +++++++------- tests/unit_tests/distances_unit_tests.py | 13 +++++++------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/khiva/distances.py b/khiva/distances.py index e25ffbf..ed74f04 100644 --- a/khiva/distances.py +++ b/khiva/distances.py @@ -74,8 +74,9 @@ def manhattan(tss): return Array(array_reference=b) -def squared_euclidean(tss): - """ Calculates the non squared version of the euclidean distance. +def sbd(tss): + """ Calculates the Shape-Based distance (SBD). It computes the normalized cross-correlation and + it returns the value that maximizes the correlation value between time series. :param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and dimension one indicates the number of time series. @@ -84,13 +85,12 @@ def squared_euclidean(tss): and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.squared_euclidean(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.sbd(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) -def sbd(tss): - """ Calculates the Shape-Based distance (SBD). It computes the normalized cross-correlation and - it returns the value that maximizes the correlation value between time series. +def squared_euclidean(tss): + """ Calculates the non squared version of the euclidean distance. :param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and dimension one indicates the number of time series. @@ -99,5 +99,5 @@ def sbd(tss): and time series 1. """ b = ctypes.c_void_p(0) - KhivaLibrary().c_khiva_library.sbd(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) + KhivaLibrary().c_khiva_library.squared_euclidean(ctypes.pointer(tss.arr_reference), ctypes.pointer(b)) return Array(array_reference=b) diff --git a/tests/unit_tests/distances_unit_tests.py b/tests/unit_tests/distances_unit_tests.py index 42b0d15..f404f99 100644 --- a/tests/unit_tests/distances_unit_tests.py +++ b/tests/unit_tests/distances_unit_tests.py @@ -31,12 +31,6 @@ def test_euclidean(self): expected = np.array([0, 0, 0, 8, 0, 0, 16, 8, 0]) np.testing.assert_array_almost_equal(euclidean_result, expected, decimal=1) - def test_squared_euclidean(self): - squared_euclidean_result = squared_euclidean( - Array(data=[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])).to_numpy().flatten() - expected = np.array([0, 0, 0, 64, 0, 0, 256, 64, 0]) - np.testing.assert_array_almost_equal(squared_euclidean_result, expected, decimal=1) - def test_dtw(self): euclidean_result = dtw(Array( data=[[1, 1, 1, 1, 1], [2, 2, 2, 2, 2], [3, 3, 3, 3, 3], [4, 4, 4, 4, 4], [5, 5, 5, 5, 5]])).to_numpy() @@ -61,6 +55,13 @@ def test_sbd(self): expected = np.array([0, 0, 0, 0.505025, 0, 0, 0.458583, 0.564093, 0]) np.testing.assert_array_almost_equal(sbd_result, expected, decimal=1) + def test_squared_euclidean(self): + squared_euclidean_result = squared_euclidean( + Array(data=[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])).to_numpy().flatten() + expected = np.array([0, 0, 0, 64, 0, 0, 256, 64, 0]) + np.testing.assert_array_almost_equal(squared_euclidean_result, expected, decimal=1) + + if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(DistancesTest) From 67083b0f68968972d0dd1bae5ef122bce6abc8b4 Mon Sep 17 00:00:00 2001 From: Antonio Vilches Date: Wed, 19 Dec 2018 11:26:16 +0100 Subject: [PATCH 3/3] Deleting line to satisfy PEP8 syntax rules --- tests/unit_tests/distances_unit_tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit_tests/distances_unit_tests.py b/tests/unit_tests/distances_unit_tests.py index f404f99..1b8dbf1 100644 --- a/tests/unit_tests/distances_unit_tests.py +++ b/tests/unit_tests/distances_unit_tests.py @@ -62,7 +62,6 @@ def test_squared_euclidean(self): np.testing.assert_array_almost_equal(squared_euclidean_result, expected, decimal=1) - if __name__ == '__main__': suite = unittest.TestLoader().loadTestsFromTestCase(DistancesTest) unittest.TextTestRunner(verbosity=2).run(suite)