Skip to content

Commit 97f6113

Browse files
authored
Merge pull request #249 from jGaboardi/GH245_pytest
convert from `unittest` to `pytest` and `ruff`en tests
2 parents dab90b3 + 5a15c2e commit 97f6113

File tree

8 files changed

+96
-144
lines changed

8 files changed

+96
-144
lines changed

giddy/tests/test_directional.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import unittest
21
import libpysal as ps
3-
from .. import directional
42
import numpy as np
3+
import pytest
4+
5+
from .. import directional
56

67

7-
class Rose_Tester(unittest.TestCase):
8-
def setUp(self):
9-
f = open(ps.examples.get_path("spi_download.csv"), "r")
10-
lines = f.readlines()
11-
f.close()
8+
class TestRose:
9+
def setup_method(self):
10+
with open(ps.examples.get_path("spi_download.csv")) as f:
11+
lines = f.readlines()
1212
lines = [line.strip().split(",") for line in lines]
1313
names = [line[2] for line in lines[1:-5]]
1414
data = np.array([list(map(int, line[3:])) for line in lines[1:-5]])
@@ -43,8 +43,8 @@ def test_rose(self):
4343
exp = [0.0, 1.57079633, 3.14159265, 4.71238898, 6.28318531]
4444
obs = list(r4.cuts)
4545
for i in range(k + 1):
46-
self.assertAlmostEqual(exp[i], obs[i])
47-
self.assertEqual(list(r4.counts), [32, 5, 9, 2])
46+
assert pytest.approx(exp[i]) == obs[i]
47+
assert list(r4.counts) == [32, 5, 9, 2]
4848

4949
import matplotlib.pyplot as plt
5050

@@ -61,14 +61,3 @@ def test_rose(self):
6161
# customize plot
6262
fig, _ = r4.plot_vectors(arrows=False)
6363
plt.close(fig)
64-
65-
66-
suite = unittest.TestSuite()
67-
test_classes = [Rose_Tester]
68-
for i in test_classes:
69-
a = unittest.TestLoader().loadTestsFromTestCase(i)
70-
suite.addTest(a)
71-
72-
if __name__ == "__main__":
73-
runner = unittest.TextTestRunner()
74-
runner.run(suite)

giddy/tests/test_ergodic.py

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import numpy as np
12
import pytest
2-
import unittest
3+
34
from .. import ergodic
4-
import numpy as np
55

66

7-
class SteadyState_Tester(unittest.TestCase):
8-
def setUp(self):
7+
class TestSteadyState:
8+
def setup_method(self):
99
self.p = np.array([[0.5, 0.25, 0.25], [0.5, 0, 0.5], [0.25, 0.25, 0.5]])
1010
self.p2 = np.array([[0.5, 0.5, 0], [0.3, 0.7, 0], [0, 0, 1]])
1111
self.p3 = np.array([[0.5, 0.5, 0], [0.3, 0.7, 0], [0, 0, 0]])
@@ -28,11 +28,12 @@ def test_steady_state(self):
2828
exp = np.array([[0.375, 0.625, 0.0], [0.0, 0.0, 1.0]])
2929
np.testing.assert_array_almost_equal(exp, obs)
3030

31-
self.assertRaises(ValueError, ergodic.steady_state, self.p3, False)
31+
with pytest.raises(ValueError, match="Input transition probability matrix"):
32+
ergodic.steady_state(self.p3)
3233

3334

34-
class Mfpt_Tester(unittest.TestCase):
35-
def setUp(self):
35+
class TestMfpt:
36+
def setup_method(self):
3637
self.p = np.array([[0.5, 0.25, 0.25], [0.5, 0, 0.5], [0.25, 0.25, 0.5]])
3738
self.p2 = np.array([[0.5, 0.5, 0], [0.3, 0.7, 0], [0, 0, 1]])
3839
self.p3 = np.array([[0.5, 0.5, 0], [0.3, 0.7, 0], [0, 0, 0]])
@@ -81,8 +82,8 @@ def test_mfpt(self):
8182
np.testing.assert_array_almost_equal(exp, obs)
8283

8384

84-
class VarMfpt_Tester(unittest.TestCase):
85-
def setUp(self):
85+
class TestVarMfpt:
86+
def setup_method(self):
8687
self.p = np.array([[0.5, 0.25, 0.25], [0.5, 0, 0.5], [0.25, 0.25, 0.5]])
8788

8889
def test_var_mfpt(self):
@@ -95,14 +96,3 @@ def test_var_mfpt(self):
9596
]
9697
)
9798
np.testing.assert_array_almost_equal(exp, obs)
98-
99-
100-
suite = unittest.TestSuite()
101-
test_classes = [SteadyState_Tester, Mfpt_Tester, VarMfpt_Tester]
102-
for i in test_classes:
103-
a = unittest.TestLoader().loadTestsFromTestCase(i)
104-
suite.addTest(a)
105-
106-
if __name__ == "__main__":
107-
runner = unittest.TextTestRunner()
108-
runner.run(suite)

giddy/tests/test_markov.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import unittest
21
import libpysal as ps
3-
import numpy as np
42
import mapclassify as mc
3+
import numpy as np
4+
55
from ..markov import (
6+
FullRank_Markov,
7+
GeoRank_Markov,
8+
LISA_Markov,
69
Markov,
10+
Spatial_Markov,
711
kullback,
812
prais,
9-
Spatial_Markov,
10-
LISA_Markov,
11-
FullRank_Markov,
1213
sojourn_time,
13-
GeoRank_Markov,
1414
)
1515

1616
RTOL = 0.00001
1717

1818

19-
class test_Markov(unittest.TestCase):
19+
class TestMarkov:
2020
def test___init__(self):
2121
# markov = Markov(class_ids, classes)
2222
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
@@ -106,8 +106,8 @@ def test___init__(self):
106106
np.testing.assert_array_almost_equal(m.sojourn_time, expected)
107107

108108

109-
class test_Spatial_Markov(unittest.TestCase):
110-
def setUp(self):
109+
class TestSpatialMarkov:
110+
def setup_method(self):
111111
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
112112
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
113113
pci = pci.transpose()
@@ -273,7 +273,7 @@ def test_discretized(self):
273273
np.testing.assert_array_equal(sm.T, answer)
274274

275275

276-
class test_chi2(unittest.TestCase):
276+
class TestChi2:
277277
def test_chi2(self):
278278
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
279279
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
@@ -305,7 +305,7 @@ def test_chi2(self):
305305
np.testing.assert_array_almost_equal(obs, np.array(sm.shtest))
306306

307307

308-
class test_LISA_Markov(unittest.TestCase):
308+
class TestLISAMarkov:
309309
def test___init__(self):
310310
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
311311
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]).transpose()
@@ -348,7 +348,7 @@ def test___init__(self):
348348
np.testing.assert_allclose(lm_random.chi_2, c, RTOL)
349349

350350

351-
class test_kullback(unittest.TestCase):
351+
class TestKullback:
352352
def test___init__(self):
353353
s1 = np.array(
354354
[
@@ -382,7 +382,7 @@ def test___init__(self):
382382
np.testing.assert_array_almost_equal(0.0, p_value)
383383

384384

385-
class test_prais(unittest.TestCase):
385+
class TestPrais:
386386
def test___init__(self):
387387
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
388388
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
@@ -392,7 +392,7 @@ def test___init__(self):
392392
np.testing.assert_array_almost_equal(prais(m.p), res)
393393

394394

395-
class FullRank_Markov_Tester(unittest.TestCase):
395+
class TestFullRankMarkov:
396396
def test___init__(self):
397397
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
398398
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]).transpose()
@@ -664,7 +664,7 @@ def test___init__(self):
664664
np.testing.assert_array_almost_equal(m.sojourn_time, expected)
665665

666666

667-
class GeoRank_Markov_Tester(unittest.TestCase):
667+
class TestGeoRankMarkov:
668668
def test___init__(self):
669669
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
670670
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)]).transpose()
@@ -707,8 +707,8 @@ def test___init__(self):
707707
np.testing.assert_array_almost_equal(gm.sojourn_time[:10], expected)
708708

709709

710-
class Sojourn_time_Tester(unittest.TestCase):
711-
def setUp(self):
710+
class TestSojournTime:
711+
def setup_method(self):
712712
self.p = np.array([[0.5, 0.25, 0.25], [0.5, 0, 0.5], [0.25, 0.25, 0.5]])
713713
self.p2 = np.array([[0.5, 0.5, 0], [0.3, 0.7, 0], [0, 0, 1]])
714714

@@ -719,7 +719,3 @@ def test_sojourn_time(self):
719719

720720
exp = np.array([2.0, 3.33333333, np.inf])
721721
np.testing.assert_array_almost_equal(exp, sojourn_time(self.p2))
722-
723-
724-
if __name__ == "__main__":
725-
unittest.main()

giddy/tests/test_mobility.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import unittest
21
import libpysal as ps
3-
import numpy as np
42
import mapclassify as mc
3+
import numpy as np
4+
55
from ..markov import Markov
66
from ..mobility import markov_mobility
77

88

9-
class Shorrock_Tester(unittest.TestCase):
9+
class TestShorrock:
1010
def test___init__(self):
11-
import numpy as np
12-
1311
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
1412
pci = np.array([f.by_col[str(y)] for y in range(1929, 2010)])
1513
q5 = np.array([mc.Quantiles(y).yb for y in pci]).transpose()
@@ -19,7 +17,7 @@ def test___init__(self):
1917
)
2018

2119

22-
class MarkovMobility_Tester(unittest.TestCase):
20+
class TestMarkovMobility:
2321
def test___init__(self):
2422
pi = np.array([0.1, 0.2, 0.2, 0.4, 0.1])
2523
f = ps.io.open(ps.examples.get_path("usjoin.csv"))
@@ -41,7 +39,3 @@ def test___init__(self):
4139
np.testing.assert_array_almost_equal(
4240
markov_mobility(m.p, measure="B2", ini=pi), 0.046366601194789261
4341
)
44-
45-
46-
if __name__ == "__main__":
47-
unittest.main()

giddy/tests/test_rank.py

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
import unittest
21
import libpysal as ps
3-
from .. import rank
42
import numpy as np
3+
import pytest
4+
5+
from .. import rank
56

67

7-
class Theta_Tester(unittest.TestCase):
8-
def setUp(self):
8+
class TestTheta:
9+
def setup_method(self):
910
f = ps.io.open(ps.examples.get_path("mexico.csv"))
10-
vnames = ["pcgdp%d" % dec for dec in range(1940, 2010, 10)]
11+
vnames = [f"pcgdp{dec}" for dec in range(1940, 2010, 10)]
1112
self.y = np.transpose(np.array([f.by_col[v] for v in vnames]))
1213
self.regime = np.array(f.by_col["esquivel99"])
1314

14-
def test_Theta(self):
15+
def test_defaults(self):
1516
np.random.seed(10)
1617
t = rank.Theta(self.y, self.regime, 999)
1718
k = self.y.shape[1]
1819
obs = t.theta.tolist()
1920
exp = [[0.41538462, 0.28070175, 0.61363636, 0.62222222, 0.33333333, 0.47222222]]
2021
for i in range(k - 1):
21-
self.assertAlmostEqual(exp[0][i], obs[0][i])
22+
assert pytest.approx(exp[0][i]) == obs[0][i]
2223
obs = t.pvalue_left.tolist()
2324
exp = [0.307, 0.077, 0.823, 0.552, 0.045, 0.735]
2425
for i in range(k - 1):
25-
self.assertAlmostEqual(exp[i], obs[i])
26+
assert pytest.approx(exp[i]) == obs[i]
2627
obs = t.total.tolist()
2728
exp = [130.0, 114.0, 88.0, 90.0, 90.0, 72.0]
2829
for i in range(k - 1):
29-
self.assertAlmostEqual(exp[i], obs[i])
30-
self.assertEqual(t.max_total, 512)
30+
assert pytest.approx(exp[i]) == obs[i]
31+
assert t.max_total == 512
3132

3233

33-
class SpatialTau_Tester(unittest.TestCase):
34-
def setUp(self):
34+
class TestSpatialTau:
35+
def setup_method(self):
3536
f = ps.io.open(ps.examples.get_path("mexico.csv"))
36-
vnames = ["pcgdp%d" % dec for dec in range(1940, 2010, 10)]
37+
vnames = [f"pcgdp{dec}" for dec in range(1940, 2010, 10)]
3738
self.y = np.transpose(np.array([f.by_col[v] for v in vnames]))
3839
regime = np.array(f.by_col["esquivel99"])
3940
self.w = ps.weights.block_weights(regime)
4041

41-
def test_SpatialTau(self):
42+
def test_defaults(self):
4243
np.random.seed(12345)
4344
k = self.y.shape[1]
4445
obs = [
@@ -49,35 +50,24 @@ def test_SpatialTau(self):
4950
ev_tau_s = [0.659, 0.706, 0.772, 0.752, 0.705, 0.819]
5051
p_vals = [0.010, 0.010, 0.020, 0.210, 0.270, 0.280]
5152
for i in range(k - 1):
52-
self.assertAlmostEqual(tau_s[i], obs[i].tau_spatial, 3)
53-
self.assertAlmostEqual(ev_tau_s[i], obs[i].taus.mean(), 3)
54-
self.assertAlmostEqual(p_vals[i], obs[i].tau_spatial_psim, 3)
53+
assert pytest.approx(tau_s[i], 3) == obs[i].tau_spatial
54+
assert pytest.approx(ev_tau_s[i], 3) == obs[i].taus.mean()
55+
assert pytest.approx(p_vals[i], 3) == obs[i].tau_spatial_psim
5556
st12 = rank.SpatialTau(self.y[:, 1], self.y[:, 2], self.w)
5657
st21 = rank.SpatialTau(self.y[:, 2], self.y[:, 1], self.w)
57-
self.assertEqual(st12.tau_spatial, st21.tau_spatial)
58+
assert pytest.approx(st12.tau_spatial) == st21.tau_spatial
5859

5960

60-
class Tau_Tester(unittest.TestCase):
61-
def test_Tau(self):
61+
class TestTau:
62+
def test_defaults(self):
6263
x1 = [12, 2, 1, 12, 2]
6364
x2 = [1, 4, 7, 1, 0]
6465
kt = rank.Tau(x1, x2)
65-
self.assertAlmostEqual(kt.tau, -0.47140452079103173, 5)
66-
self.assertAlmostEqual(kt.tau_p, 0.24821309157521476, 5)
66+
assert pytest.approx(kt.tau, 5) == -0.47140452079103173
67+
assert pytest.approx(kt.tau_p, 5) == 0.24821309157521476
6768
x1 = [0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1]
6869
x2 = [1, 4, 7, 1, 1, 3, 1, 6, 7, 7, 3, 6, 2, 7, 2, 8]
6970
kt12 = rank.Tau(x1, x2)
7071
kt21 = rank.Tau(x2, x1)
71-
self.assertEqual(kt12.tau, kt21.tau)
72-
self.assertAlmostEqual(kt12.tau, 0.15494494670022804)
73-
74-
75-
suite = unittest.TestSuite()
76-
test_classes = [Theta_Tester, SpatialTau_Tester, Tau_Tester]
77-
for i in test_classes:
78-
a = unittest.TestLoader().loadTestsFromTestCase(i)
79-
suite.addTest(a)
80-
81-
if __name__ == "__main__":
82-
runner = unittest.TextTestRunner()
83-
runner.run(suite)
72+
assert kt12.tau == kt21.tau
73+
assert pytest.approx(kt12.tau) == 0.15494494670022804

0 commit comments

Comments
 (0)