Skip to content

Commit cdaf8b8

Browse files
authored
Adding sbd distance method and its test. (#40)
* Adding sbd distance method and its test.
1 parent 335a7ba commit cdaf8b8

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

khiva/distances.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def euclidean(tss):
2828
between time series 0 and time series 1.
2929
"""
3030
b = ctypes.c_void_p(0)
31-
KhivaLibrary().c_khiva_library.euclidean(ctypes.pointer(tss.arr_reference),
32-
ctypes.pointer(b))
31+
KhivaLibrary().c_khiva_library.euclidean(ctypes.pointer(tss.arr_reference), ctypes.pointer(b))
3332
return Array(array_reference=b)
3433

3534

@@ -43,8 +42,7 @@ def dtw(tss):
4342
distance between time series 0 and time series 1.
4443
"""
4544
b = ctypes.c_void_p(0)
46-
KhivaLibrary().c_khiva_library.dtw(ctypes.pointer(tss.arr_reference),
47-
ctypes.pointer(b))
45+
KhivaLibrary().c_khiva_library.dtw(ctypes.pointer(tss.arr_reference), ctypes.pointer(b))
4846
return Array(array_reference=b)
4947

5048

@@ -58,8 +56,7 @@ def hamming(tss):
5856
between time series 0 and time series 1.
5957
"""
6058
b = ctypes.c_void_p(0)
61-
KhivaLibrary().c_khiva_library.hamming(ctypes.pointer(tss.arr_reference),
62-
ctypes.pointer(b))
59+
KhivaLibrary().c_khiva_library.hamming(ctypes.pointer(tss.arr_reference), ctypes.pointer(b))
6360
return Array(array_reference=b)
6461

6562

@@ -73,8 +70,22 @@ def manhattan(tss):
7370
between time series 0 and time series 1.
7471
"""
7572
b = ctypes.c_void_p(0)
76-
KhivaLibrary().c_khiva_library.manhattan(ctypes.pointer(tss.arr_reference),
77-
ctypes.pointer(b))
73+
KhivaLibrary().c_khiva_library.manhattan(ctypes.pointer(tss.arr_reference), ctypes.pointer(b))
74+
return Array(array_reference=b)
75+
76+
77+
def sbd(tss):
78+
""" Calculates the Shape-Based distance (SBD). It computes the normalized cross-correlation and
79+
it returns the value that maximizes the correlation value between time series.
80+
81+
:param tss: Expects an input array whose dimension zero is the length of the time series (all the same) and
82+
dimension one indicates the number of time series.
83+
:return: Array with an upper triangular matrix where each position corresponds to the distance between two time series.
84+
Diagonal elements will be zero. For example: Position row 0 column 1 records the distance between time series 0
85+
and time series 1.
86+
"""
87+
b = ctypes.c_void_p(0)
88+
KhivaLibrary().c_khiva_library.sbd(ctypes.pointer(tss.arr_reference), ctypes.pointer(b))
7889
return Array(array_reference=b)
7990

8091

@@ -88,6 +99,5 @@ def squared_euclidean(tss):
8899
and time series 1.
89100
"""
90101
b = ctypes.c_void_p(0)
91-
KhivaLibrary().c_khiva_library.squared_euclidean(ctypes.pointer(tss.arr_reference),
92-
ctypes.pointer(b))
102+
KhivaLibrary().c_khiva_library.squared_euclidean(ctypes.pointer(tss.arr_reference), ctypes.pointer(b))
93103
return Array(array_reference=b)

tests/unit_tests/distances_unit_tests.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ def test_euclidean(self):
3131
expected = np.array([0, 0, 0, 8, 0, 0, 16, 8, 0])
3232
np.testing.assert_array_almost_equal(euclidean_result, expected, decimal=1)
3333

34-
def test_squared_euclidean(self):
35-
squared_euclidean_result = squared_euclidean(
36-
Array(data=[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])).to_numpy().flatten()
37-
expected = np.array([0, 0, 0, 64, 0, 0, 256, 64, 0])
38-
np.testing.assert_array_almost_equal(squared_euclidean_result, expected, decimal=1)
39-
4034
def test_dtw(self):
4135
euclidean_result = dtw(Array(
4236
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()
@@ -55,6 +49,18 @@ def test_manhattan(self):
5549
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]])
5650
np.testing.assert_array_almost_equal(result, expected, decimal=2)
5751

52+
def test_sbd(self):
53+
sbd_result = sbd(
54+
Array(data=[[1, 2, 3, 4, 5], [1, 1, 0, 1, 1], [10, 12, 0, 0, 1]])).to_numpy().flatten()
55+
expected = np.array([0, 0, 0, 0.505025, 0, 0, 0.458583, 0.564093, 0])
56+
np.testing.assert_array_almost_equal(sbd_result, expected, decimal=1)
57+
58+
def test_squared_euclidean(self):
59+
squared_euclidean_result = squared_euclidean(
60+
Array(data=[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]])).to_numpy().flatten()
61+
expected = np.array([0, 0, 0, 64, 0, 0, 256, 64, 0])
62+
np.testing.assert_array_almost_equal(squared_euclidean_result, expected, decimal=1)
63+
5864

5965
if __name__ == '__main__':
6066
suite = unittest.TestLoader().loadTestsFromTestCase(DistancesTest)

0 commit comments

Comments
 (0)