Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flake8 errors #470

Merged
merged 7 commits into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ install:
- python setup.py install

script:
- flake8 --select F401 quantecon
- flake8 --select F401, F405,E231 quantecon
- nosetests --with-coverage -a "!slow" --cover-package=quantecon

after_success:
Expand Down
9 changes: 6 additions & 3 deletions quantecon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
try:
import numba
except:
raise ImportError("Cannot import numba from current anaconda distribution. Please run `conda install numba` to install the latest version.")
raise ImportError(
"Cannot import numba from current anaconda distribution. Please run `conda install numba` to install the latest version.")

#-Modules-#
from . import distributions
Expand Down Expand Up @@ -35,8 +36,10 @@
from .matrix_eqn import solve_discrete_lyapunov, solve_discrete_riccati
from .quadsums import var_quadratic_sum, m_quadratic_sum
#->Propose Delete From Top Level
from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, gth_solve, tauchen, rouwenhorst #Promote to keep current examples working
from .markov import mc_compute_stationary, mc_sample_path #Imports that Should be Deprecated with markov package
#Promote to keep current examples working
from .markov import MarkovChain, random_markov_chain, random_stochastic_matrix, gth_solve, tauchen, rouwenhorst
#Imports that Should be Deprecated with markov package
from .markov import mc_compute_stationary, mc_sample_path
#<-
from .rank_nullspace import rank_est, nullspace
from .robustlq import RBLQ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_seed(self):
@raises(ValueError)
def test_raises_value_error_too_large_inputs(self):
n, k = 100, 50
g = tournament_game(n, k)
tournament_game(n, k)


class TestUnitVectorGame:
Expand Down Expand Up @@ -149,7 +149,7 @@ def test_redraw(self):
@raises(ValueError)
def test_raises_value_error_avoid_pure_nash_n_1(self):
n = 1
g = unit_vector_game(n, avoid_pure_nash=True)
unit_vector_game(n, avoid_pure_nash=True)


def test_payoff_range():
Expand Down
2 changes: 1 addition & 1 deletion quantecon/game_theory/tests/test_mclennan_tourky.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def test_best_response_selection_no_indptr():
[(0, 3), (6, 1)]]
g = NormalFormGame(bimatrix)

test_obj = _best_response_selection([1/3, 1/3, 1/3, 1/2,1/2], g)
test_obj = _best_response_selection([1/3, 1/3, 1/3, 1/2, 1/2], g)
expected_output = np.array([0., 1., 0., 0., 1.])

assert_array_equal(test_obj, expected_output)
Expand Down
14 changes: 7 additions & 7 deletions quantecon/game_theory/tests/test_normal_form_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,43 +482,43 @@ def test_player_repr():

@raises(ValueError)
def test_player_zero_actions():
p = Player([[]])
Player([[]])


@raises(ValueError)
def test_normalformgame_invalid_input_players_shape_inconsistent():
p0 = Player(np.zeros((2, 3)))
p1 = Player(np.zeros((2, 3)))
g = NormalFormGame([p0, p1])
NormalFormGame([p0, p1])


@raises(ValueError)
def test_normalformgame_invalid_input_players_num_inconsistent():
p0 = Player(np.zeros((2, 2, 2)))
p1 = Player(np.zeros((2, 2, 2)))
g = NormalFormGame([p0, p1])
NormalFormGame([p0, p1])


@raises(ValueError)
def test_normalformgame_invalid_input_players_dtype_inconsistent():
p0 = Player(np.zeros((2, 2), dtype=int))
p1 = Player(np.zeros((2, 2), dtype=float))
g = NormalFormGame([p0, p1])
NormalFormGame([p0, p1])


@raises(ValueError)
def test_normalformgame_invalid_input_nosquare_matrix():
g = NormalFormGame(np.zeros((2, 3)))
NormalFormGame(np.zeros((2, 3)))


@raises(ValueError)
def test_normalformgame_invalid_input_payoff_profiles():
g = NormalFormGame(np.zeros((2, 2, 1)))
NormalFormGame(np.zeros((2, 2, 1)))


@raises(ValueError)
def test_normalformgame_zero_actions():
g = NormalFormGame((2, 0))
NormalFormGame((2, 0))


# Utility functions #
Expand Down
2 changes: 1 addition & 1 deletion quantecon/game_theory/tests/test_support_enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def test_no_error_skew_sym(self):
n, m = 3, 2
seed = 7028
g = random_skew_sym(n, m, random_state=seed)
NEs = support_enumeration(g)
support_enumeration(g)


@raises(TypeError)
Expand Down
1 change: 0 additions & 1 deletion quantecon/game_theory/vertex_enumeration.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ def __init__(self, opponent_player, idx=0, qhull_options=None):
# Shift the payoffs to be nonnegative and have no zero column
col_mins = B.min(axis=0)
col_maxs = B.max(axis=0)
neg_cols = (col_mins < 0)
nonpos_const_cols = (col_maxs == col_mins) * (col_mins <= 0)
shifts = np.zeros(m)
shifts[col_mins < 0] = -col_mins[col_mins < 0]
Expand Down
10 changes: 7 additions & 3 deletions quantecon/lss.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,17 @@ def __init__(self, A, C, G, H=None, mu_0=None, Sigma_0=None):
# = Check Input Shapes = #
ni, nj = self.A.shape
if ni != nj:
raise ValueError("Matrix A (shape: %s) needs to be square" % (self.A.shape))
raise ValueError(
"Matrix A (shape: %s) needs to be square" % (self.A.shape))
if ni != self.C.shape[0]:
raise ValueError("Matrix C (shape: %s) does not have compatible dimensions with A. It should be shape: %s" % (self.C.shape, (ni,1)))
raise ValueError(
"Matrix C (shape: %s) does not have compatible dimensions with A. "
"It should be shape: %s" % (self.C.shape, (ni, 1)))
self.m = self.C.shape[1]
self.k, self.n = self.G.shape
if self.n != ni:
raise ValueError("Matrix G (shape: %s) does not have compatible dimensions with A (%s)"%(self.G.shape, self.A.shape))
raise ValueError("Matrix G (shape: %s) does not have compatible dimensions with A (%s)"%(
self.G.shape, self.A.shape))
if H is None:
self.H = None
self.l = None
Expand Down
1 change: 0 additions & 1 deletion quantecon/markov/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ def cdfs(self):
def cdfs1d(self):
if (self._cdfs1d is None) and self.is_sparse:
data = self.P.data
indices = self.P.indices
indptr = self.P.indptr

cdfs1d = np.empty(self.P.nnz, order='C')
Expand Down
3 changes: 2 additions & 1 deletion quantecon/markov/ddp.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ def to_product_form(self):
R[self.s_indices, self.a_indices] = self.R
Q = np.zeros((ns, na, ns))
if self._sparse:
_fill_dense_Q(self.s_indices, self.a_indices, self.Q.toarray(), Q)
_fill_dense_Q(self.s_indices, self.a_indices,
self.Q.toarray(), Q)
else:
_fill_dense_Q(self.s_indices, self.a_indices, self.Q, Q)
return DiscreteDP(R, Q, self.beta)
Expand Down
15 changes: 9 additions & 6 deletions quantecon/markov/tests/test_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def test_states_sum_0(self):
class TestRouwenhorst(unittest.TestCase):

def setUp(self):
self.rho, self.sigma = np.random.uniform(0,1, size=2)
self.n = np.random.random_integers(3,25)
self.ybar = np.random.random_integers(0,10)
self.rho, self.sigma = np.random.uniform(0, 1, size=2)
self.n = np.random.random_integers(3, 25)
self.ybar = np.random.random_integers(0, 10)
self.tol = 1e-12

mc = rouwenhorst(self.n, self.ybar, self.sigma, self.rho)
Expand Down Expand Up @@ -87,10 +87,13 @@ def test_control_case(self):
sigma_y = np.sqrt(sigma**2 / (1-rho**2))
psi = sigma_y * np.sqrt(n-1)
known_x = np.array([-psi+5.0, 5., psi+5.0])
known_P = np.array([[0.81, 0.18, 0.01], [0.09, 0.82, 0.09], [0.01, 0.18, 0.81]])
self.assertTrue(sum(mc_rouwenhorst.x - known_x) < self.tol and sum(sum(mc_rouwenhorst.P - known_P)) < self.tol)
known_P = np.array(
[[0.81, 0.18, 0.01], [0.09, 0.82, 0.09], [0.01, 0.18, 0.81]])
self.assertTrue(sum(mc_rouwenhorst.x - known_x) <
self.tol and sum(sum(mc_rouwenhorst.P - known_P)) < self.tol)


if __name__ == '__main__':
suite = unittest.TestLoader().loadTestsFromTestCase([TestTauchen, TestRouwenhorst])
suite = unittest.TestLoader().loadTestsFromTestCase(
[TestTauchen, TestRouwenhorst])
unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)
2 changes: 1 addition & 1 deletion quantecon/markov/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ def test_get_index():
@raises(ValueError)
def test_raises_value_error_non_2dim():
"""Test with non 2dim input"""
mc = MarkovChain(np.array([0.4, 0.6]))
MarkovChain(np.array([0.4, 0.6]))


def test_raises_value_error_non_sym():
Expand Down
9 changes: 0 additions & 9 deletions quantecon/optimize/tests/test_nelder_mead.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ def colville(x):
@njit
def styblinski_tang(x):
# https://www.sfu.ca/~ssurjano/stybtang.html
d = x.size
f = 0.5 * (x ** 4 - 16 * x ** 2 + 5 * x).sum()
return -f

Expand Down Expand Up @@ -288,8 +287,6 @@ def test_styblinski_tang(self):

def test_goldstein_price(self):
x0 = np.array([-1.5, 0.5])
bounds = np.array([[-2., 2.],
[-2., 2.]])

results = nelder_mead(goldstein_price, x0)

Expand Down Expand Up @@ -335,19 +332,13 @@ def test_discontinuous(self):

@raises(ValueError)
def test_invalid_bounds_1():
vertices = np.array([[-2., 1.],
[1.05 * -2., 1.],
[-2., 1.05 * 1.]])
x0 = np.array([-2., 1.])
bounds = np.array([[10., -10.], [10., -10.]])
nelder_mead(rosenbrock, x0, bounds=bounds)


@raises(ValueError)
def test_invalid_bounds_2():
vertices = np.array([[-2., 1.],
[1.05 * -2., 1.],
[-2., 1.05 * 1.]])
x0 = np.array([-2., 1.])
bounds = np.array([[10., -10., 10., -10.]])
nelder_mead(rosenbrock, x0, bounds=bounds)
Expand Down
4 changes: 3 additions & 1 deletion quantecon/optimize/tests/test_root_finding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from numpy.testing import assert_almost_equal, assert_allclose
from numba import njit

from quantecon.optimize import *
from quantecon.optimize import (
newton, newton_halley, newton_secant, bisect, brentq
)


@njit
Expand Down
3 changes: 2 additions & 1 deletion quantecon/robustlq.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ def b_operator(self, P):
S1 = Q + beta * dot(B.T, dot(P, B))
S2 = beta * dot(B.T, dot(P, A))
S3 = beta * dot(A.T, dot(P, A))
F = solve(S1, S2) if not self.pure_forecasting else np.zeros((self.k, self.n))
F = solve(S1, S2) if not self.pure_forecasting else np.zeros(
(self.k, self.n))
new_P = R - dot(S2.T, F) + S3

return F, new_P
Expand Down
2 changes: 1 addition & 1 deletion quantecon/tests/test_compute_fp.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ def test_raises_value_error_nonpositive_max_iter():
f = lambda x: 0.5*x
init = 1.
max_iter = 0
fp = compute_fixed_point(f, init, max_iter=max_iter)
compute_fixed_point(f, init, max_iter=max_iter)
7 changes: 4 additions & 3 deletions quantecon/tests/test_dle.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def setUp(self):
deltah = np.array([[.9]])
thetah = np.array([[1]]) - deltah
ub = np.array([[30, 0, 0]])
x0 = np.array([[5, 150,1,0,0]]).T

information = (a22, c2, ub, ud)
technology = (phic, phig, phii, gamma, deltak, thetak)
Expand Down Expand Up @@ -98,7 +97,8 @@ def test_compute_steadystate(self):
}
self.dle.compute_steadystate()
for item in solutions.keys():
assert_allclose(self.dle.__dict__[item], solutions[item], atol=ATOL)
assert_allclose(self.dle.__dict__[
item], solutions[item], atol=ATOL)

def test_canonical(self):
solutions = {
Expand All @@ -108,7 +108,8 @@ def test_canonical(self):
}
self.dle.canonical()
for item in solutions.keys():
assert_allclose(self.dle.__dict__[item], solutions[item], atol=ATOL)
assert_allclose(self.dle.__dict__[
item], solutions[item], atol=ATOL)


if __name__ == '__main__':
Expand Down
6 changes: 4 additions & 2 deletions quantecon/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ def test_hamilton_filter():
data = pd.read_csv(os.path.join(data_dir, "employment.csv"),
names = ['year', 'employment', 'matlab_cyc', 'matlab_cyc_rw'])

data['hamilton_cyc'], data['hamilton_trend'] = hamilton_filter(100*np.log(data['employment']), 8, 4)
data['hamilton_cyc_rw'], data['hamilton_trend_rw'] = hamilton_filter(100*np.log(data['employment']), 8)
data['hamilton_cyc'], data['hamilton_trend'] = hamilton_filter(
100*np.log(data['employment']), 8, 4)
data['hamilton_cyc_rw'], data['hamilton_trend_rw'] = hamilton_filter(
100*np.log(data['employment']), 8)
assert_allclose(data['matlab_cyc'], data['hamilton_cyc'],
rtol = 1e-07, atol = 1e-07)
assert_allclose(data['matlab_cyc_rw'], data['hamilton_cyc_rw'])
Expand Down
2 changes: 1 addition & 1 deletion quantecon/tests/test_graph_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def test_node_labels_subgraph():
@raises(ValueError)
def test_raises_value_error_non_sym():
"""Test with non symmetric input"""
g = DiGraph(np.array([[0.4, 0.6]]))
DiGraph(np.array([[0.4, 0.6]]))


def test_raises_non_homogeneous_node_labels():
Expand Down
Loading