Skip to content

Commit

Permalink
Merge pull request #2 from MattHJensen/conflicts_merged
Browse files Browse the repository at this point in the history
pep8 cleanup
  • Loading branch information
PeterDSteinberg committed Oct 12, 2015
2 parents 3855b95 + 75ccf03 commit 0fb822b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
27 changes: 17 additions & 10 deletions taxcalc/parameters_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def set_default_vals(self):
values = data['value']
setattr(self, name,
self.expand_array(values, inflate=cpi_inflated,
inflation_rates=getattr(self,
'_inflation_rates',
None),
inflation_rates=(getattr(self,
'_inflation_rates',
None)),
num_years=self._num_years))
self.set_year(self._start_year)

Expand Down Expand Up @@ -258,15 +258,18 @@ def expand_1D(x, inflate, inflation_rates, num_years):
ans[len(x):] = extra
return ans.astype(x.dtype, casting='unsafe')

return ParametersBase.expand_1D(np.array([x]), inflate, inflation_rates, num_years)
return ParametersBase.expand_1D(np.array([x]),
inflate,
inflation_rates,
num_years)

@staticmethod
def expand_2D(x, inflate, inflation_rates, num_years):
"""
Expand the given data to account for the given number of budget years.
For 2D arrays, we expand out the number of rows until we have num_years
number of rows. For each expanded row, we inflate by the given inflation
rate.
number of rows. For each expanded row, we inflate by the given
inflation rate.
"""

if isinstance(x, np.ndarray):
Expand Down Expand Up @@ -320,7 +323,8 @@ def expand_2D(x, inflate, inflation_rates, num_years):

return ans.astype(c.dtype, casting='unsafe')

return ParametersBase.expand_2D(np.array(x), inflate, inflation_rates, num_years)
return ParametersBase.expand_2D(np.array(x), inflate, inflation_rates,
num_years)

@staticmethod
def strip_Nones(x):
Expand Down Expand Up @@ -355,7 +359,8 @@ def strip_Nones(x):
@staticmethod
def expand_array(x, inflate, inflation_rates, num_years):
"""
Dispatch to either expand_1D or expand2D depending on the dimension of x
Dispatch to either expand_1D or expand_2D
depending on the dimension of x
Parameters
----------
Expand All @@ -377,9 +382,11 @@ def expand_array(x, inflate, inflation_rates, num_years):
x = np.array(ParametersBase.strip_Nones(x))
try:
if len(x.shape) == 1:
return ParametersBase.expand_1D(x, inflate, inflation_rates, num_years)
return ParametersBase.expand_1D(x, inflate, inflation_rates,
num_years)
elif len(x.shape) == 2:
return ParametersBase.expand_2D(x, inflate, inflation_rates, num_years)
return ParametersBase.expand_2D(x, inflate, inflation_rates,
num_years)
else:
raise ValueError("Need a 1D or 2D array")
except AttributeError:
Expand Down
5 changes: 3 additions & 2 deletions taxcalc/tests/test_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ def test_make_Calculator_user_mods_with_cpi_flags(policyfile):
calc.policy.implement_reform(user_mods)

inf_rates = [IRATES[1991 + i] for i in range(0, 12)]
exp_almdep = Policy.expand_array(np.array([7150, 7250, 7400]), inflate=True,
inflation_rates=inf_rates, num_years=12)
exp_almdep = Policy.expand_array(np.array([7150, 7250, 7400]),
inflate=True,
inflation_rates=inf_rates, num_years=12)
act_almdep = getattr(calc.policy, '_almdep')
assert_array_equal(act_almdep, exp_almdep)
exp_almsep_values = [40400] + [41050] * 11
Expand Down
31 changes: 19 additions & 12 deletions taxcalc/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def test_expand_1D_short_array():
exp = np.zeros(10)
exp[:3] = exp1
exp[3:] = exp2
res = Policy.expand_1D(x, inflate=True, inflation_rates=[0.02] * 10, num_years=10)
res = Policy.expand_1D(x, inflate=True, inflation_rates=[0.02] * 10,
num_years=10)
assert(np.allclose(exp.astype(x.dtype, casting='unsafe'), res))


Expand All @@ -60,14 +61,16 @@ def test_expand_1D_variable_rates():
exp2 = []
cur = 9.0
exp = np.array([4, 5, 9, 9 * 1.03, 9 * 1.03 * 1.035])
res = Policy.expand_1D(x, inflate=True, inflation_rates=irates, num_years=5)
res = Policy.expand_1D(x, inflate=True, inflation_rates=irates,
num_years=5)
assert(np.allclose(exp.astype('f4', casting='unsafe'), res))


def test_expand_1D_scalar():
x = 10.0
exp = np.array([10.0 * math.pow(1.02, i) for i in range(0, 10)])
res = Policy.expand_1D(x, inflate=True, inflation_rates=[0.02] * 10, num_years=10)
res = Policy.expand_1D(x, inflate=True, inflation_rates=[0.02] * 10,
num_years=10)
assert(np.allclose(exp, res))


Expand All @@ -82,7 +85,8 @@ def test_expand_1D_accept_None():
cur *= 1.035
exp.append(cur)
exp = np.array(exp)
res = Policy.expand_array(x, inflate=True, inflation_rates=irates, num_years=5)
res = Policy.expand_array(x, inflate=True, inflation_rates=irates,
num_years=5)
assert(np.allclose(exp.astype('f4', casting='unsafe'), res))


Expand All @@ -94,7 +98,8 @@ def test_expand_2D_short_array():
exp = np.zeros((5, 3))
exp[:1] = exp1
exp[1:] = exp2
res = Policy.expand_2D(x, inflate=True, inflation_rates=[0.02] * 5, num_years=5)
res = Policy.expand_2D(x, inflate=True, inflation_rates=[0.02] * 5,
num_years=5)
assert(np.allclose(exp, res))


Expand All @@ -115,7 +120,8 @@ def test_expand_2D_variable_rates():
exp = np.zeros((5, 3))
exp[:1] = exp1
exp[1:] = exp2
res = Policy.expand_2D(x, inflate=True, inflation_rates=irates, num_years=5)
res = Policy.expand_2D(x, inflate=True, inflation_rates=irates,
num_years=5)
npt.assert_array_equal(res, np.array(exp).astype('f8', casting='unsafe'))
assert(np.allclose(exp, res))

Expand Down Expand Up @@ -351,7 +357,7 @@ def test_expand_2D_already_filled():
[40000, 74900, 37450, 50200, 74900, 37450]]

res = Policy.expand_2D(_II_brk2, inflate=True, inflation_rates=[0.02] * 5,
num_years=3)
num_years=3)

npt.assert_array_equal(res, np.array(_II_brk2))

Expand Down Expand Up @@ -382,7 +388,7 @@ def test_expand_2D_partial_expand():
exp = np.array(exp).astype('i4', casting='unsafe')

res = Policy.expand_2D(_II_brk2, inflate=True, inflation_rates=inf_rates,
num_years=4)
num_years=4)

npt.assert_array_equal(res, exp)

Expand Down Expand Up @@ -419,8 +425,9 @@ def test_expand_2D_accept_None():

exp = np.array(exp).astype('i4', casting='unsafe')

res = Policy.expand_array(_II_brk2, inflate=True, inflation_rates=[0.02] * 5,
num_years=4)
res = Policy.expand_array(_II_brk2, inflate=True,
inflation_rates=[0.02] * 5,
num_years=4)

npt.assert_array_equal(res, exp)

Expand Down Expand Up @@ -455,8 +462,8 @@ def test_expand_2D_accept_None_additional_row():

inflation_rates = [0.015, 0.02, 0.02, 0.03]

res = Policy.expand_array(_II_brk2, inflate=True, inflation_rates=inflation_rates,
num_years=5)
res = Policy.expand_array(_II_brk2, inflate=True,
inflation_rates=inflation_rates, num_years=5)

npt.assert_array_equal(res, exp)

Expand Down
1 change: 1 addition & 0 deletions taxcalc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def wrapper(*args, **kwargs):
return f(*arrays)
return wrapper


def count_gt_zero(agg):
return sum([1 for a in agg if a > 0])

Expand Down

0 comments on commit 0fb822b

Please sign in to comment.