Skip to content

Commit 0ab43fa

Browse files
authored
Adding sbd method (#23)
Adding SBD method and its test.
1 parent 5812bbb commit 0ab43fa

File tree

2 files changed

+56
-21
lines changed

2 files changed

+56
-21
lines changed

src/main/java/io/shapelets/khiva/Distances.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class Distances extends Library {
2222

2323
private native static long[] manhattan(long ref);
2424

25+
private native static long[] sbd(long ref);
26+
2527
private native static long[] squaredEuclidean(long ref);
2628

2729

@@ -69,6 +71,22 @@ public static Array hamming(Array tss) {
6971
return new Array(refs[1]);
7072
}
7173

74+
/**
75+
* Calculates the Shape-Based distance (SBD). It computes the normalized cross-correlation and it returns 1.0
76+
* minus the value that maximizes the correlation value between each pair of time series.
77+
*
78+
* @param tss Expects an input array whose dimension zero is the length of the time series (all the same) and
79+
* dimension one indicates the number of time series.
80+
* @return Array with an upper triangular matrix where each position corresponds to the distance between two
81+
* time series. Diagonal elements will be zero. For example: Position row 0 column 1 records the distance between time
82+
* series 0 and time series 1.
83+
*/
84+
public static Array sbd(Array tss) {
85+
long[] refs = sbd(tss.getReference());
86+
tss.setReference(refs[0]);
87+
return new Array(refs[1]);
88+
}
89+
7290
/**
7391
* Calculates Manhattan distances between time series.
7492
*

src/test/java/io/shapelets/khiva/DistancesTest.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,6 @@ public void testEuclidean() throws Exception {
4343
}
4444
}
4545

46-
@Test
47-
public void testSquaredEuclidean() throws Exception {
48-
float[] timeSeries = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
49-
long[] dims = {4, 3, 1, 1};
50-
try (
51-
Array arrayOfTimeSeries = new Array(timeSeries, dims);
52-
Array b = Distances.squaredEuclidean(arrayOfTimeSeries)
53-
) {
54-
float[] result = b.getData();
55-
Assert.assertEquals(result[0], 0, DELTA);
56-
Assert.assertEquals(result[1], 0, DELTA);
57-
Assert.assertEquals(result[2], 0, DELTA);
58-
Assert.assertEquals(result[3], 64, DELTA);
59-
Assert.assertEquals(result[4], 0, DELTA);
60-
Assert.assertEquals(result[5], 0, DELTA);
61-
Assert.assertEquals(result[6], 256, DELTA);
62-
Assert.assertEquals(result[7], 64, DELTA);
63-
Assert.assertEquals(result[8], 0, DELTA);
64-
}
65-
}
66-
6746
@Test
6847
public void testDwt() throws Exception {
6948
float[] timeSeries = {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};
@@ -114,4 +93,42 @@ public void testManhattan() throws Exception {
11493
}
11594
}
11695
}
96+
97+
@Test
98+
public void testSBD() throws Exception {
99+
float[] timeSeries = {1, 2, 3, 4, 5, 1, 1, 0, 1, 1, 10, 12, 0, 0, 1};
100+
long[] dims = {5, 3, 1, 1};
101+
try (
102+
Array a = new Array(timeSeries, dims);
103+
Array b = Distances.sbd(a)
104+
) {
105+
float[] result = b.getData();
106+
float[] expectedResult = {0, 0, 0, 0.505025f, 0, 0, 0.458583f, 0.564093f, 0};
107+
Assert.assertEquals(expectedResult.length, result.length, DELTA);
108+
for (int i = 0; i < result.length; i++) {
109+
Assert.assertEquals(expectedResult[i], result[i], DELTA);
110+
}
111+
}
112+
}
113+
114+
@Test
115+
public void testSquaredEuclidean() throws Exception {
116+
float[] timeSeries = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
117+
long[] dims = {4, 3, 1, 1};
118+
try (
119+
Array arrayOfTimeSeries = new Array(timeSeries, dims);
120+
Array b = Distances.squaredEuclidean(arrayOfTimeSeries)
121+
) {
122+
float[] result = b.getData();
123+
Assert.assertEquals(result[0], 0, DELTA);
124+
Assert.assertEquals(result[1], 0, DELTA);
125+
Assert.assertEquals(result[2], 0, DELTA);
126+
Assert.assertEquals(result[3], 64, DELTA);
127+
Assert.assertEquals(result[4], 0, DELTA);
128+
Assert.assertEquals(result[5], 0, DELTA);
129+
Assert.assertEquals(result[6], 256, DELTA);
130+
Assert.assertEquals(result[7], 64, DELTA);
131+
Assert.assertEquals(result[8], 0, DELTA);
132+
}
133+
}
117134
}

0 commit comments

Comments
 (0)