22
33import numpy as np
44from numpy import nan
5- from numpy .testing import (assert_approx_equal ,
6- assert_array_equal , assert_raises )
5+ from numpy .testing import assert_approx_equal , assert_array_equal , assert_raises
76from dtw import *
87
9-
8+
109class TestDTWs (unittest .TestCase ):
1110 def test_matrix (self ):
1211 dm = 10 * np .ones ((4 , 4 )) + np .eye (4 )
1312 al = dtw (dm , keep_internals = True )
14- assert_array_equal (al .costMatrix ,
15- np .array ([[11. , 21. , 31. , 41. ],
16- [21. , 32. , 41. , 51. ],
17- [31. , 41. , 52. , 61. ],
18- [41. , 51. , 61. , 72. ]]))
13+ assert_array_equal (
14+ al .costMatrix ,
15+ np .array (
16+ [
17+ [11.0 , 21.0 , 31.0 , 41.0 ],
18+ [21.0 , 32.0 , 41.0 , 51.0 ],
19+ [31.0 , 41.0 , 52.0 , 61.0 ],
20+ [41.0 , 51.0 , 61.0 , 72.0 ],
21+ ]
22+ ),
23+ )
1924
2025 def test_rectangular (self ):
2126 # Hand-checked
2227 x = np .array ([1 , 2 , 3 ])
2328 y = np .array ([2 , 3 , 4 , 5 , 6 ])
2429 al = dtw (x , y , keep_internals = True )
25- assert_array_equal (al .costMatrix ,
26- np .array ([[1. , 3. , 6. , 10. , 15. ],
27- [1. , 2. , 4. , 7. , 11. ],
28- [2. , 1. , 2. , 4. , 7. ]]))
29- assert_approx_equal (al .normalizedDistance ,0.875 )
30-
30+ assert_array_equal (
31+ al .costMatrix ,
32+ np .array (
33+ [
34+ [1.0 , 3.0 , 6.0 , 10.0 , 15.0 ],
35+ [1.0 , 2.0 , 4.0 , 7.0 , 11.0 ],
36+ [2.0 , 1.0 , 2.0 , 4.0 , 7.0 ],
37+ ]
38+ ),
39+ )
40+ assert_approx_equal (al .normalizedDistance , 0.875 )
3141
3242 def test_backtrack (self ):
3343 x = np .array ([1 , 2 , 3 ])
3444 y = np .array ([2 , 3 , 4 , 5 , 6 ])
3545 al = dtw (x , y )
36- assert_array_equal (al .index1 , np .array ([0 , 1 , 2 , 2 , 2 , 2 ]))
46+ assert_array_equal (al .index1 , np .array ([0 , 1 , 2 , 2 , 2 , 2 ]))
3747 assert_array_equal (al .index1s , np .array ([0 , 1 , 2 , 2 , 2 , 2 ]))
38- assert_array_equal (al .index2 , np .array ([0 , 0 , 1 , 2 , 3 , 4 ]))
48+ assert_array_equal (al .index2 , np .array ([0 , 0 , 1 , 2 , 3 , 4 ]))
3949 assert_array_equal (al .index2s , np .array ([0 , 0 , 1 , 2 , 3 , 4 ]))
40-
4150
4251 def test_vectors (self ):
4352 x = np .array ([1 , 2 , 3 ])
@@ -46,26 +55,34 @@ def test_vectors(self):
4655 assert_approx_equal (al .distance , 2.0 )
4756
4857 def test_asymmetric (self ):
49- lm = np .array ([[1 , 1 , 2 , 2 , 3 , 3 ],
50- [1 , 1 , 1 , 2 , 2 , 2 ],
51- [3 , 1 , 2 , 2 , 3 , 3 ],
52- [3 , 1 , 2 , 1 , 1 , 2 ],
53- [3 , 2 , 1 , 2 , 1 , 2 ],
54- [3 , 3 , 3 , 2 , 1 , 2 ]], dtype = np .double )
58+ lm = np .array (
59+ [
60+ [1 , 1 , 2 , 2 , 3 , 3 ],
61+ [1 , 1 , 1 , 2 , 2 , 2 ],
62+ [3 , 1 , 2 , 2 , 3 , 3 ],
63+ [3 , 1 , 2 , 1 , 1 , 2 ],
64+ [3 , 2 , 1 , 2 , 1 , 2 ],
65+ [3 , 3 , 3 , 2 , 1 , 2 ],
66+ ],
67+ dtype = np .double ,
68+ )
5569 alignment = dtw (lm , step_pattern = asymmetric , keep_internals = True )
56- assert_array_equal (alignment .costMatrix ,
57- np .array ([[1. , nan , nan , nan , nan , nan ],
58- [2. , 2. , 2. , nan , nan , nan ],
59- [5. , 3. , 4. , 4. , 5. , nan ],
60- [8. , 4. , 5. , 4. , 5. , 6. ],
61- [11. , 6. , 5. , 6. , 5. , 6. ],
62- [14. , 9. , 8. , 7. , 6. , 7. ]])
63- )
70+ assert_array_equal (
71+ alignment .costMatrix ,
72+ np .array (
73+ [
74+ [1.0 , nan , nan , nan , nan , nan ],
75+ [2.0 , 2.0 , 2.0 , nan , nan , nan ],
76+ [5.0 , 3.0 , 4.0 , 4.0 , 5.0 , nan ],
77+ [8.0 , 4.0 , 5.0 , 4.0 , 5.0 , 6.0 ],
78+ [11.0 , 6.0 , 5.0 , 6.0 , 5.0 , 6.0 ],
79+ [14.0 , 9.0 , 8.0 , 7.0 , 6.0 , 7.0 ],
80+ ]
81+ ),
82+ )
6483
6584 def test_impossible (self ):
6685 x = np .ones (4 )
6786 y = np .ones (20 )
6887 with assert_raises (ValueError ):
6988 dtw (x , y , step_pattern = asymmetric )
70-
71-
0 commit comments