Skip to content

Commit cb231d5

Browse files
committed
ruff tests
1 parent 0b163e5 commit cb231d5

File tree

7 files changed

+96
-80
lines changed

7 files changed

+96
-80
lines changed

tests/test_cli.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
here = os.path.dirname(os.path.abspath(__file__))
1313

14+
1415
class TestCLI(unittest.TestCase):
1516
"""Tests for `dtw` package."""
1617

1718
def test_command_line_interface(self):
1819
"""Test the CLI."""
19-
out = main2(os.path.join(here,"query.csv"),
20-
os.path.join(here,"reference.csv"),
21-
"symmetric2")
22-
assert '0.1292' in out
23-
24-
20+
out = main2(
21+
os.path.join(here, "query.csv"),
22+
os.path.join(here, "reference.csv"),
23+
"symmetric2",
24+
)
25+
assert "0.1292" in out

tests/test_countPaths.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import unittest
32

43
import numpy as np
@@ -9,24 +8,22 @@
98

109

1110
class Test_countPaths(unittest.TestCase):
12-
1311
# From dtw()'s example
1412
def test_example_ds(self):
15-
ldist = np.full( (6,6), 1.0)
16-
ldist[1,:] = 0
17-
ldist[:,4] = 0
18-
ldist[1,4] = .01
13+
ldist = np.full((6, 6), 1.0)
14+
ldist[1, :] = 0
15+
ldist[:, 4] = 0
16+
ldist[1, 4] = 0.01
1917
ds = dtw(ldist, keep_internals=True)
2018
pds = countPaths(ds)
2119
assert_equal(pds, 1683)
22-
20+
2321
def test_example_da(self):
24-
ldist = np.full( (6,6), 1.0)
25-
ldist[1,:] = 0
26-
ldist[:,4] = 0
27-
ldist[1,4] = .01
22+
ldist = np.full((6, 6), 1.0)
23+
ldist[1, :] = 0
24+
ldist[:, 4] = 0
25+
ldist[1, 4] = 0.01
2826

2927
da = dtw(ldist, step_pattern=asymmetric, keep_internals=True)
3028
pda = countPaths(da)
31-
assert_equal(pda, 51)
32-
29+
assert_equal(pda, 51)

tests/test_cran.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import unittest
22

33
import numpy as np
4-
from numpy.testing import (assert_approx_equal, assert_equal,
5-
assert_array_equal)
4+
from numpy.testing import assert_approx_equal, assert_equal, assert_array_equal
65
from dtw import *
76

87

98
def i(l):
10-
return np.array([int(x) for x in l.split()], dtype=int)-1
9+
return np.array([int(x) for x in l.split()], dtype=int) - 1
1110

1211

1312
ldist = np.full((6, 6), 1.0)
1413
ldist[1, :] = 0
1514
ldist[:, 4] = 0
16-
ldist[1, 4] = .01
15+
ldist[1, 4] = 0.01
1716

1817

1918
"""Same tests as CRAN checks"""
@@ -25,13 +24,20 @@ def test_ldist_symmetric2(self):
2524
assert_equal(ds.distance, 2)
2625
assert_array_equal(ds.index1, i("1 2 2 2 3 4 5 6 6"))
2726
assert_array_equal(ds.index2, i("1 2 3 4 5 5 5 5 6"))
28-
assert_array_equal(ds.costMatrix, np.array(
29-
[[1, 2, 3, 4, 4.00, 5.00],
30-
[1, 1, 1, 1, 1.01, 1.01],
31-
[2, 2, 2, 2, 1.00, 2.00],
32-
[3, 3, 3, 3, 1.00, 2.00],
33-
[4, 4, 4, 4, 1.00, 2.00],
34-
[5, 5, 5, 5, 1.00, 2.00]], dtype=float))
27+
assert_array_equal(
28+
ds.costMatrix,
29+
np.array(
30+
[
31+
[1, 2, 3, 4, 4.00, 5.00],
32+
[1, 1, 1, 1, 1.01, 1.01],
33+
[2, 2, 2, 2, 1.00, 2.00],
34+
[3, 3, 3, 3, 1.00, 2.00],
35+
[4, 4, 4, 4, 1.00, 2.00],
36+
[5, 5, 5, 5, 1.00, 2.00],
37+
],
38+
dtype=float,
39+
),
40+
)
3541

3642
def test_ldist_asymmetric(self):
3743
ds = dtw(ldist, keep_internals=True, step_pattern=asymmetric)
@@ -56,19 +62,17 @@ def test_ldist_asymmetricP1(self):
5662
# Count paths is in another file
5763

5864
def test_open_begin_end(self):
59-
query = np.arange(2, 4)+.01
60-
ref = np.arange(4)+1
61-
obe = dtw(query, ref,
62-
open_begin=True, open_end=True,
63-
step_pattern=asymmetric)
65+
query = np.arange(2, 4) + 0.01
66+
ref = np.arange(4) + 1
67+
obe = dtw(query, ref, open_begin=True, open_end=True, step_pattern=asymmetric)
6468
assert_approx_equal(obe.distance, 0.02)
6569
assert_array_equal(obe.index2, i("2 3"))
6670

6771
def test_cdist(self):
6872
from scipy.spatial.distance import cdist
6973

7074
query = np.vstack([np.arange(1, 11), np.ones(10)]).T
71-
ref = np.vstack([np.arange(11, 16), 2*np.ones(5)]).T
75+
ref = np.vstack([np.arange(11, 16), 2 * np.ones(5)]).T
7276

7377
cxdist = cdist(query, ref, metric="cityblock")
7478

tests/test_doctests.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import unittest
32
import doctest
43
import glob
@@ -17,7 +16,7 @@ def run_suite():
1716
class TestDoctests(unittest.TestCase):
1817
def test_doctests(self):
1918
r = run_suite()
20-
assert len(r.failures)==0
19+
assert len(r.failures) == 0
2120

2221

2322
if __name__ == "__main__":

tests/test_dtw.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import unittest
32

43
import numpy as np
@@ -37,11 +36,9 @@
3736
"""
3837

3938

40-
4139
class TestDTW(unittest.TestCase):
4240
def test_sincos(self):
43-
idx = np.linspace(0,6.28,num=100)
44-
query = np.sin(idx) + np.random.uniform(size=100)/10.0
41+
idx = np.linspace(0, 6.28, num=100)
42+
query = np.sin(idx) + np.random.uniform(size=100) / 10.0
4543
reference = np.cos(idx)
46-
alignment = dtw(query,reference)
47-
44+
alignment = dtw(query, reference)

tests/test_dtw_s.py

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,51 @@
22

33
import numpy as np
44
from 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
76
from dtw import *
87

9-
8+
109
class 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-

tests/test_plot.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1-
21
import unittest
32

43
import numpy as np
54
from dtw import *
65

76
try:
87
import matplotlib
8+
99
matplotlib.use("Agg")
10-
matplotlib_available=True
10+
matplotlib_available = True
1111
except:
12-
matplotlib_available=False
12+
matplotlib_available = False
1313

1414

1515
"""
1616
From the "quickstart" examples.
1717
"""
1818

19+
1920
@unittest.skipUnless(matplotlib_available, "Could not import matplotlib")
2021
class TestPlot(unittest.TestCase):
2122
def setUp(self):

0 commit comments

Comments
 (0)