Skip to content

Commit 4d08bec

Browse files
authored
MAINT: Fix pytest warnings and use pytest in Conda CI (#615)
* MAINT: Fix pytest warnings * CI: Use pytest on conda build * CI: Fix coverage
1 parent c34fe53 commit 4d08bec

File tree

9 files changed

+99
-64
lines changed

9 files changed

+99
-64
lines changed

.github/workflows/conda_ci.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ jobs:
4343
conda activate test
4444
python setup.py build
4545
46-
- name: Run Tests
46+
- name: flake8 Tests
4747
shell: bash -l {0}
4848
run: |
4949
flake8 --select F401, F405, E231 quantecon
50-
coverage run -m nose -a "!slow"
50+
51+
- name: Run Tests (pytest)
52+
shell: bash -l {0}
53+
run: |
54+
coverage run -m pytest quantecon
5155
coverage lcov
5256
5357
- name: Coveralls

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
4848
- name: Run Tests (pytest)
4949
run: |
50-
coverage run -m pytest
50+
coverage run -m pytest quantecon
5151
coverage lcov
5252
5353
- name: Coveralls

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ quantecon.egg-info/
1414
# Numba cache files
1515
*.nbc
1616
*.nbi
17+
18+
# Coverage
19+
20+
coverage.xml

environment.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ channels:
33
- conda-forge
44
- defaults
55
dependencies:
6-
- nose
76
- coverage
87
- numpy
98
- scipy

quantecon/dle.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,38 @@ class DLE(object):
2222
h_t = \Delta_h h_{t-1} + \Theta_h c_t
2323
s_t = \Lambda h_{t-1} + \Pi c_t
2424
25-
and
25+
and
2626
2727
z_{t+1} = A_{22} z_t + C_2 w_{t+1}
2828
b_t = U_b z_t
29-
d_t = U_d z_t
29+
d_t = U_d z_t
3030
3131
where h_{-1}, k_{-1}, and z_0 are given as initial conditions.
3232
33-
Section 5.5 of HS2013 describes how to map these matrices into those of
34-
a LQ problem.
33+
Section 5.5 of HS2013 describes how to map these matrices into those of
34+
a LQ problem.
3535
3636
HS2013 sort the matrices defining the problem into three groups:
3737
38-
Information: A_{22}, C_2, U_b , and U_d characterize the motion of information
38+
Information: A_{22}, C_2, U_b , and U_d characterize the motion of information
3939
sets and of taste and technology shocks
4040
41-
Technology: \Phi_c, \Phi_g, \Phi_i, \Gamma, \Delta_k, and \Theta_k determine the
41+
Technology: \Phi_c, \Phi_g, \Phi_i, \Gamma, \Delta_k, and \Theta_k determine the
4242
technology for producing consumption goods
4343
44-
Preferences: \Delta_h, \Theta_h, \Lambda, and \Pi determine the technology for
45-
producing consumption services from consumer goods. A scalar discount factor \beta
44+
Preferences: \Delta_h, \Theta_h, \Lambda, and \Pi determine the technology for
45+
producing consumption services from consumer goods. A scalar discount factor \beta
4646
determines the preference ordering over consumption services.
4747
4848
Parameters
4949
----------
5050
Information : tuple
5151
Information is a tuple containing the matrices A_{22}, C_2, U_b, and U_d
5252
Technology : tuple
53-
Technology is a tuple containing the matrices \Phi_c, \Phi_g, \Phi_i, \Gamma,
53+
Technology is a tuple containing the matrices \Phi_c, \Phi_g, \Phi_i, \Gamma,
5454
\Delta_k, and \Theta_k
5555
Preferences : tuple
56-
Preferences is a tuple containing the matrices \Delta_h, \Theta_h, \Lambda,
56+
Preferences is a tuple containing the matrices \Delta_h, \Theta_h, \Lambda,
5757
\Pi, and the scalar \beta
5858
5959
"""
@@ -145,9 +145,9 @@ def __init__(self, information, technology, preferences):
145145

146146
# === Construct matrices for Lagrange Multipliers === #
147147

148-
self.Mk = -2 * np.asscalar(self.beta) * (np.hstack((np.zeros((self.nk, self.nh)), np.eye(
148+
self.Mk = -2 * self.beta.item() * (np.hstack((np.zeros((self.nk, self.nh)), np.eye(
149149
self.nk), np.zeros((self.nk, self.nz))))).dot(self.P).dot(self.A0)
150-
self.Mh = -2 * np.asscalar(self.beta) * (np.hstack((np.eye(self.nh), np.zeros(
150+
self.Mh = -2 * self.beta.item() * (np.hstack((np.eye(self.nh), np.zeros(
151151
(self.nh, self.nk)), np.zeros((self.nh, self.nz))))).dot(self.P).dot(self.A0)
152152
self.Ms = -(self.Sb - self.Ss)
153153
self.Md = -(np.linalg.inv(np.vstack((self.phic.T, self.phig.T))).dot(
@@ -296,7 +296,7 @@ def irf(self, ts_length=100, shock=None):
296296
return
297297

298298
def canonical(self):
299-
"""
299+
"""
300300
Compute canonical preference representation
301301
Uses auxiliary problem of 9.4.2, with the preference shock process reintroduced
302302
Calculates pihat, llambdahat and ubhat for the equivalent canonical household technology

quantecon/markov/tests/test_approximation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class TestTauchen:
1313

1414
def setup(self):
1515
self.rho, self.sigma_u = np.random.rand(2)
16-
self.n = np.random.random_integers(3, 25)
17-
self.m = np.random.random_integers(4)
16+
self.n = np.random.randint(3, 25)
17+
self.m = np.random.randint(5)
1818
self.tol = 1e-12
1919
self.b = 0.
2020

@@ -56,8 +56,8 @@ class TestRouwenhorst:
5656

5757
def setup(self):
5858
self.rho, self.sigma = np.random.uniform(0, 1, size=2)
59-
self.n = np.random.random_integers(3, 25)
60-
self.ybar = np.random.random_integers(0, 10)
59+
self.n = np.random.randint(3, 26)
60+
self.ybar = np.random.randint(0, 11)
6161
self.tol = 1e-10
6262

6363
mc = rouwenhorst(self.n, self.ybar, self.sigma, self.rho)

quantecon/markov/tests/test_gth_solve.py

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
assert_allclose)
88

99
from quantecon.markov import gth_solve
10+
import pytest
1011

1112

1213
TOL = 1e-15
@@ -90,39 +91,6 @@ def __init__(self):
9091
self.gen_matrix_dicts.append(matrix_dict)
9192

9293

93-
def test_stoch_matrix():
94-
"""Test with stochastic matrices"""
95-
print(__name__ + '.' + test_stoch_matrix.__name__)
96-
matrices = Matrices()
97-
for matrix_dict in matrices.stoch_matrix_dicts:
98-
x = gth_solve(matrix_dict['A'])
99-
yield StationaryDistSumOne(), x
100-
yield StationaryDistNonnegative(), x
101-
yield StationaryDistEqualToKnown(), matrix_dict['stationary_dist'], x
102-
103-
104-
def test_kmr_matrix():
105-
"""Test with KMR matrices"""
106-
print(__name__ + '.' + test_kmr_matrix.__name__)
107-
matrices = Matrices()
108-
for matrix_dict in matrices.kmr_matrix_dicts:
109-
x = gth_solve(matrix_dict['A'])
110-
yield StationaryDistSumOne(), x
111-
yield StationaryDistNonnegative(), x
112-
yield StationaryDistLeftEigenVec(), matrix_dict['A'], x
113-
114-
115-
def test_gen_matrix():
116-
"""Test with generator matrices"""
117-
print(__name__ + '.' + test_gen_matrix.__name__)
118-
matrices = Matrices()
119-
for matrix_dict in matrices.gen_matrix_dicts:
120-
x = gth_solve(matrix_dict['A'])
121-
yield StationaryDistSumOne(), x
122-
yield StationaryDistNonnegative(), x
123-
yield StationaryDistEqualToKnown(), matrix_dict['stationary_dist'], x
124-
125-
12694
class AddDescription:
12795
def __init__(self):
12896
self.description = self.__class__.__name__
@@ -148,6 +116,63 @@ def __call__(self, y, x):
148116
assert_allclose(y, x, atol=TOL)
149117

150118

119+
test_classes = [
120+
StationaryDistSumOne,
121+
StationaryDistNonnegative,
122+
]
123+
124+
125+
@pytest.mark.parametrize("test_class", test_classes)
126+
def test_stoch_matrix(test_class):
127+
"""Test with stochastic matrices"""
128+
matrices = Matrices()
129+
for matrix_dict in matrices.stoch_matrix_dicts:
130+
x = gth_solve(matrix_dict['A'])
131+
test_class()(x)
132+
133+
134+
def test_stoch_matrix_1():
135+
"""Test with stochastic matrices"""
136+
matrices = Matrices()
137+
for matrix_dict in matrices.stoch_matrix_dicts:
138+
x = gth_solve(matrix_dict['A'])
139+
StationaryDistEqualToKnown()(matrix_dict['stationary_dist'], x)
140+
141+
142+
@pytest.mark.parametrize("test_class", test_classes)
143+
def test_kmr_matrix(test_class):
144+
"""Test with KMR matrices"""
145+
matrices = Matrices()
146+
for matrix_dict in matrices.kmr_matrix_dicts:
147+
x = gth_solve(matrix_dict['A'])
148+
test_class()(x)
149+
150+
151+
def test_kmr_matrix_1():
152+
"""Test with KMR matrices"""
153+
matrices = Matrices()
154+
for matrix_dict in matrices.kmr_matrix_dicts:
155+
x = gth_solve(matrix_dict['A'])
156+
StationaryDistLeftEigenVec()(matrix_dict['A'], x)
157+
158+
159+
@pytest.mark.parametrize("test_class", test_classes)
160+
def test_gen_matrix(test_class):
161+
"""Test with generator matrices"""
162+
matrices = Matrices()
163+
for matrix_dict in matrices.gen_matrix_dicts:
164+
x = gth_solve(matrix_dict['A'])
165+
test_class()(x)
166+
167+
168+
def test_gen_matrix_1():
169+
"""Test with generator matrices"""
170+
matrices = Matrices()
171+
for matrix_dict in matrices.gen_matrix_dicts:
172+
x = gth_solve(matrix_dict['A'])
173+
StationaryDistEqualToKnown()(matrix_dict['stationary_dist'], x)
174+
175+
151176
def test_matrices_with_C_F_orders():
152177
"""
153178
Test matrices with C- and F-contiguous orders

quantecon/tests/test_ivp.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
# use the Solow Model with Cobb-Douglas production as test case
1111
def solow_model(t, k, g, n, s, alpha, delta):
12-
"""
12+
r"""
1313
Equation of motion for capital stock (per unit effective labor).
1414
1515
Parameters
@@ -41,7 +41,7 @@ def solow_model(t, k, g, n, s, alpha, delta):
4141

4242

4343
def solow_jacobian(t, k, g, n, s, alpha, delta):
44-
"""
44+
r"""
4545
Jacobian matrix for the Solow model.
4646
4747
Parameters
@@ -73,7 +73,7 @@ def solow_jacobian(t, k, g, n, s, alpha, delta):
7373

7474

7575
def solow_steady_state(g, n, s, alpha, delta):
76-
"""
76+
r"""
7777
Steady-state level of capital stock (per unit effective labor).
7878
7979
Parameters
@@ -101,7 +101,7 @@ def solow_steady_state(g, n, s, alpha, delta):
101101

102102

103103
def solow_analytic_solution(t, k0, g, n, s, alpha, delta):
104-
"""
104+
r"""
105105
Analytic solution for the path of capital stock (per unit effective labor).
106106
107107
Parameters

quantecon/tests/test_ricatti.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66
from numpy.testing import assert_allclose, assert_raises
77
from quantecon.matrix_eqn import solve_discrete_riccati
8+
import pytest
89

910

1011
def dare_golden_num_float(method):
@@ -73,13 +74,15 @@ def dare_tjm_3(method):
7374
]
7475

7576

76-
def test_solve_discrete_riccati():
77-
def _test_factory(func, method):
78-
func(method)
77+
_test_methods = [
78+
'doubling', 'qz'
79+
]
80+
7981

80-
for method in ['doubling', 'qz']:
81-
for func in _test_funcs:
82-
yield _test_factory, func, method
82+
@pytest.mark.parametrize("test_func", _test_funcs)
83+
@pytest.mark.parametrize("test_method", _test_methods)
84+
def test_solve_discrete_riccati(test_func, test_method):
85+
test_func(test_method)
8386

8487

8588
def test_solve_discrete_riccati_invalid_method():

0 commit comments

Comments
 (0)