Skip to content

Commit 643e810

Browse files
authored
Fix and run pre commit (#28)
* Add pyupgrade, follow black's move to psf, update other pre-commit hooks, and remove restriction on pydocstyle < 4 (this fixes #21). * Run pre-commit hooks on all files, works with few irrelevant exceptions
1 parent 630adae commit 643e810

36 files changed

+699
-249
lines changed

.pre-commit-config.yaml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
exclude: ^\.
33
repos:
44
- repo: https://github.com/asottile/reorder_python_imports
5-
rev: v1.4.0
5+
rev: v1.7.0
66
hooks:
77
- id: reorder-python-imports
88
files: '(\.pyi?|wscript)$'
99
- repo: https://github.com/pre-commit/pre-commit-hooks
10-
rev: v2.1.0
10+
rev: v2.3.0
1111
hooks:
1212
- id: check-yaml
1313
- id: check-added-large-files
@@ -19,28 +19,35 @@ repos:
1919
- id: pretty-format-json
2020
args: [--autofix, --no-sort-keys]
2121
- id: trailing-whitespace
22+
- repo: https://github.com/asottile/pyupgrade
23+
rev: v1.24.0
24+
hooks:
25+
- id: pyupgrade
26+
args: [
27+
--py36-plus
28+
]
2229
- repo: https://github.com/asottile/blacken-docs
23-
rev: v0.5.0
30+
rev: v1.3.0
2431
hooks:
2532
- id: blacken-docs
2633
additional_dependencies: [black==19.3b0]
2734
files: '(\.md|\.rst)$'
28-
- repo: https://github.com/ambv/black
29-
rev: stable
35+
- repo: https://github.com/psf/black
36+
rev: 19.3b0
3037
hooks:
3138
- id: black
3239
files: '(\.pyi?|wscript)$'
3340
language_version: python3.7
3441
- repo: https://gitlab.com/pycqa/flake8
35-
rev: 3.7.7
42+
rev: 3.7.8
3643
hooks:
3744
- id: flake8
3845
files: '(\.py|wscript)$'
3946
additional_dependencies: [
4047
flake8-bugbear, flake8-builtins, flake8-comprehensions,
4148
flake8-deprecated, flake8-docstrings, flake8-eradicate, flake8-print,
4249
flake8-rst-docstrings, flake8-todo, flake8-type-annotations,
43-
pep8-naming, pydocstyle<4
50+
pep8-naming, pydocstyle
4451
]
4552
# Harmonizing flake8 and black
4653
args: [

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
2-
filterwarnings =
2+
filterwarnings =
33
ignore::DeprecationWarning
44
ignore::PendingDeprecationWarning

src/analysis/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

src/analysis/hypo.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1-
# -*- coding: utf-8 -*-
21
# !/usr/bin/env python3
32
"""
43
Created on Fri Jun 15 14:36:30 2018
54
65
@author: iza6354
76
"""
7+
import itertools
8+
89
import matplotlib.pyplot as plt
9-
import pandas as pd
1010
import numpy as np
11-
import itertools
11+
import pandas as pd
12+
from custompygraph.make_plot import make_plot
1213
from termcolor import cprint
1314

14-
# from check_hypo import check_hypo
15-
from custompygraph.make_plot import make_plot
1615
from bld.project_paths import project_paths_join as ppj
17-
18-
from src.model_code.hypo_helpers import (
19-
get_ref_text,
20-
hypo_graph_settings,
21-
get_reform_names,
22-
get_hh_text,
23-
)
24-
from src.model_code.imports import get_params, say_hello, tarif_ubi
2516
from src.analysis.tax_transfer import tax_transfer
2617
from src.analysis.tax_transfer_funcs.taxes import tarif
27-
from src.analysis.tax_transfer_ubi import tax_transfer_ubi, ubi_settings
18+
from src.analysis.tax_transfer_ubi import tax_transfer_ubi
19+
from src.analysis.tax_transfer_ubi import ubi_settings
20+
from src.model_code.hypo_helpers import get_hh_text
21+
from src.model_code.hypo_helpers import get_ref_text
22+
from src.model_code.hypo_helpers import get_reform_names
23+
from src.model_code.hypo_helpers import hypo_graph_settings
24+
from src.model_code.imports import get_params
25+
from src.model_code.imports import say_hello
26+
from src.model_code.imports import tarif_ubi
27+
28+
# from check_hypo import check_hypo
2829

2930

3031
def flip(items, ncol):
@@ -185,7 +186,7 @@ def make_comp_plots(lego, t, maxinc, xlabels, ylabels, lang, settings, ref):
185186
ncol=ncol,
186187
frameon=False,
187188
)
188-
plt.savefig(ppj("OUT_FIGURES", "hypo/lego_{}_{}_{}.png".format(ref, t, lang)))
189+
plt.savefig(ppj("OUT_FIGURES", f"hypo/lego_{ref}_{t}_{lang}.png"))
189190

190191

191192
def create_hypo_data(settings, tb, types, rents, avg_wage=51286):
@@ -509,7 +510,7 @@ def create_hypo_data(settings, tb, types, rents, avg_wage=51286):
509510

510511
df["child6_num"] = df["child3_6_num"]
511512
for var in ["11", "15", "18", ""]:
512-
df["child{}_num".format(var)] = df["child3_6_num"] + df["child7_16_num"]
513+
df[f"child{var}_num"] = df["child3_6_num"] + df["child7_16_num"]
513514

514515
# Erster Mann ist immer Head, der Rest sind Frauen und Mädchen
515516
df["head"] = ~df["female"]
@@ -536,7 +537,7 @@ def create_hypo_data(settings, tb, types, rents, avg_wage=51286):
536537
"child",
537538
]
538539
for var in tuvars:
539-
df["{}num_tu".format(var)] = df["{}_num".format(var)]
540+
df[f"{var}num_tu"] = df[f"{var}_num"]
540541

541542
df["hhsize_tu"] = df["hhsize"]
542543
df["adult_num_tu"] = df["adult_num"]
@@ -695,7 +696,7 @@ def hypo_graphs(dfs, settings, types, lang):
695696
ylim_low=0,
696697
xlim_low=0,
697698
xlim_high=maxinc / 12,
698-
).savefig(ppj("OUT_FIGURES", "hypo/{}_{}_{}.png".format(plottype, t, lang)))
699+
).savefig(ppj("OUT_FIGURES", f"hypo/{plottype}_{t}_{lang}.png"))
699700

700701
# These plots are only for non-baseline reforms
701702
for plottype in ["budget_diff"]:
@@ -710,7 +711,7 @@ def hypo_graphs(dfs, settings, types, lang):
710711
xlim_high=maxinc / 12,
711712
showlegend=False,
712713
hline=[0],
713-
).savefig(ppj("OUT_FIGURES", "hypo/{}_{}_{}.png".format(plottype, t, lang)))
714+
).savefig(ppj("OUT_FIGURES", f"hypo/{plottype}_{t}_{lang}.png"))
714715
# Empty memory
715716
plt.clf()
716717

@@ -883,7 +884,7 @@ def hypo_excel(dfs, settings, types):
883884
# cprint('Producing Excel Output for debugging...', 'red', 'on_white')
884885
for ref, df in dfs.items():
885886
df = df.sort_values(by=["typ_bud", "y_wage"])
886-
writer = pd.ExcelWriter(ppj("OUT_DATA", "check_hypo_{}.xlsx".format(ref)))
887+
writer = pd.ExcelWriter(ppj("OUT_DATA", f"check_hypo_{ref}.xlsx"))
887888

888889
for typ in types:
889890
df.loc[(df["typ_bud"] == typ)].to_excel(
@@ -981,7 +982,7 @@ def hypo_excel(dfs, settings, types):
981982

982983
# Export to check against Stata Output
983984
taxout_hypo[ref][taxout_hypo[ref]["head"]].to_json(
984-
ppj("OUT_DATA", "python_check_{}.json".format(ref))
985+
ppj("OUT_DATA", f"python_check_{ref}.json")
985986
)
986987

987988
# produce excel control output

src/analysis/meanwages.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
Created on Fri May 24 09:17:49 2019
43

src/analysis/output.py

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
"""
4-
import pandas as pd
5-
import numpy as np
63
import matplotlib.pyplot as plt
4+
import numpy as np
5+
import pandas as pd
76
import seaborn as sns
8-
from src.model_code.hypo_helpers import get_reform_names
7+
98
from bld.project_paths import project_paths_join as ppj
9+
from src.model_code.hypo_helpers import get_reform_names
1010

1111

1212
def output(settings):
@@ -16,7 +16,7 @@ def output(settings):
1616
- Benefit recipients
1717
1818
"""
19-
19+
2020
budget = pd.DataFrame(columns=settings["Reforms"])
2121
recip = pd.DataFrame(columns=settings["Reforms"])
2222
dpis = pd.DataFrame(columns=settings["Reforms"])
@@ -29,7 +29,7 @@ def output(settings):
2929
budgetvars = taxvars + benvars
3030

3131
# load reform-specific results
32-
df = pd.read_json(ppj("OUT_DATA", "taxben_results_{}.json".format(ref)))
32+
df = pd.read_json(ppj("OUT_DATA", f"taxben_results_{ref}.json"))
3333
# print("Number of adults: {}".format(df[df['age']>=18]['pweight'].sum()))
3434
# print("Number of children: {}".format(df[df['age']<18]['pweight'].sum()))
3535

@@ -53,19 +53,19 @@ def output(settings):
5353

5454
# also calculate equivalized income
5555
df["eq_scale"] = (
56-
1 + 0.5 * np.maximum((df["hhsize"] - df["child14_num"] - 1), 0) + 0.3 * (df["child14_num"])
56+
1
57+
+ 0.5 * np.maximum((df["hhsize"] - df["child14_num"] - 1), 0)
58+
+ 0.3 * (df["child14_num"])
5759
)
5860

5961
dpis[ref]["dpi_eq"] = df["dpi"] / df["eq_scale"]
6062
dpis[ref]["dpi_per_head"] = df["dpi"] / df["hhsize"]
61-
dpis[ref]["dpi"] = df["dpi"]
63+
dpis[ref]["dpi"] = df["dpi"]
6264
dpis[ref]["pweight"] = df["pweight"]
6365
if ref != base:
6466
dpis[ref]["d_dpi"] = dpis[ref]["dpi_per_head"] - dpis[base]["dpi_per_head"]
6567
# print(dpis[ref]["d_dpi"].describe())
6668

67-
68-
6969
# calculate total budget
7070
budget.loc["TOTAL"] = budget.sum()
7171
# Calculate Differences to baseline
@@ -78,7 +78,6 @@ def output(settings):
7878
diff_rev[ref] = budget[ref] - budget[base]
7979
recip_rev[ref] = recip[ref] - recip[base]
8080

81-
8281
print("-" * 80)
8382
print("Budget Differences (bn € per year)")
8483
print(diff_rev)
@@ -89,54 +88,68 @@ def output(settings):
8988
# TODO: output to HD
9089
# DISTRIBUTIONAL OUTPUT
9190
# Density Plots. Note that these are unweighted!
92-
fig = plt.figure(figsize=(8,5))
91+
fig = plt.figure(figsize=(8, 5))
9392
for ref in settings["Reforms"]:
94-
sns.distplot(dpis[ref]["dpi_eq"][dpis[ref]["dpi_eq"].between(0,5000)],
95-
kde=True,
96-
hist=False,
97-
kde_kws={'shade': True,
98-
'bw': 100},
99-
label=get_reform_names("en")[ref]
100-
)
101-
plt.title('Income Distributions')
102-
plt.xlabel('Personal Income')
103-
plt.ylabel('Density')
93+
sns.distplot(
94+
dpis[ref]["dpi_eq"][dpis[ref]["dpi_eq"].between(0, 5000)],
95+
kde=True,
96+
hist=False,
97+
kde_kws={"shade": True, "bw": 100},
98+
label=get_reform_names("en")[ref],
99+
)
100+
plt.title("Income Distributions")
101+
plt.xlabel("Personal Income")
102+
plt.ylabel("Density")
104103
plt.savefig(ppj("OUT_FIGURES", "income_densities.png"))
105104

106105
# Winner/Loser Analysis
107106
for ref in settings["Reforms"][1:]:
108-
dpis[ref]["winner"] = df["pweight"] * (dpis[ref]["dpi"] > (dpis[base]["dpi"] + 5))
109-
dpis[ref]["loser"] = df["pweight"] * (dpis[ref]["dpi"] < (dpis[base]["dpi"] - 5))
107+
dpis[ref]["winner"] = df["pweight"] * (
108+
dpis[ref]["dpi"] > (dpis[base]["dpi"] + 5)
109+
)
110+
dpis[ref]["loser"] = df["pweight"] * (
111+
dpis[ref]["dpi"] < (dpis[base]["dpi"] - 5)
112+
)
110113
winshare = 100 * (dpis[ref]["winner"].sum() / dpis[ref]["pweight"].sum())
111114
loseshare = 100 * (dpis[ref]["loser"].sum() / dpis[ref]["pweight"].sum())
112115
winavg = dpis[ref]["d_dpi"][dpis[ref]["winner"] > 0].mean()
113116
losavg = dpis[ref]["d_dpi"][dpis[ref]["loser"] > 0].mean()
114-
print("Income Gains: {}".format(dpis[ref]["d_dpi"][dpis[ref]["winner"] > 0].describe()))
115-
print("Income Losses: {}".format(dpis[ref]["d_dpi"][dpis[ref]["loser"] > 0].describe()))
116-
117-
fig = plt.figure(figsize=(8,5))
118-
sns.distplot(dpis[ref]["d_dpi"][dpis[ref]["d_dpi"].between(-5000,2000)],
119-
kde=True,
120-
hist=False,
121-
kde_kws={'shade': True,
122-
'bw': 100},
123-
)
124-
# sns.distplot(dpis[ref]["d_dpi"][dpis[ref]["d_dpi"].between(.01,2000)],
125-
# kde=True,
126-
# hist=False,
127-
# kde_kws={'shade': True,
128-
# 'bw': 100},
129-
# label="Winners"
130-
# )
131-
plt.text(-2000, .0004, "{:.2f}% Losers \nAverage Loss: € {:.0f}".format(loseshare,
132-
losavg * (-1)))
133-
plt.text(500, .0006, "{:.2f}% Winners \nAverage Gain: € {:.0f}".format(winshare,
134-
winavg))
117+
print(
118+
"Income Gains: {}".format(
119+
dpis[ref]["d_dpi"][dpis[ref]["winner"] > 0].describe()
120+
)
121+
)
122+
print(
123+
"Income Losses: {}".format(
124+
dpis[ref]["d_dpi"][dpis[ref]["loser"] > 0].describe()
125+
)
126+
)
127+
128+
fig = plt.figure(figsize=(8, 5))
129+
sns.distplot(
130+
dpis[ref]["d_dpi"][dpis[ref]["d_dpi"].between(-5000, 2000)],
131+
kde=True,
132+
hist=False,
133+
kde_kws={"shade": True, "bw": 100},
134+
)
135+
# sns.distplot(dpis[ref]["d_dpi"][dpis[ref]["d_dpi"].between(.01,2000)],
136+
# kde=True,
137+
# hist=False,
138+
# kde_kws={'shade': True,
139+
# 'bw': 100},
140+
# label="Winners"
141+
# )
142+
plt.text(
143+
-2000,
144+
0.0004,
145+
"{:.2f}% Losers \nAverage Loss: € {:.0f}".format(loseshare, losavg * (-1)),
146+
)
147+
plt.text(500, 0.0006, f"{winshare:.2f}% Winners \nAverage Gain: € {winavg:.0f}")
135148
plt.axvline(0)
136-
plt.title('Distribution of income change')
137-
plt.xlabel('Monthly income per person')
138-
plt.ylabel('Density')
139-
plt.savefig(ppj("OUT_FIGURES", "d_inc_per_person_{}.png".format(ref)))
149+
plt.title("Distribution of income change")
150+
plt.xlabel("Monthly income per person")
151+
plt.ylabel("Density")
152+
plt.savefig(ppj("OUT_FIGURES", f"d_inc_per_person_{ref}.png"))
140153

141154

142155
# print(df["dpi_eq"].describe())

src/analysis/tax_transfer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
TAX TRANSFER SIMULATION
43

src/analysis/tax_transfer_funcs/benefits/wohngeld.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def calc_wg_income(df, tb, hhsize):
122122
"gkvbeit",
123123
"uhv",
124124
]:
125-
wg_income["{}_tu_k".format(inc)] = aggr(df, inc, "all_tu")
125+
wg_income[f"{inc}_tu_k"] = aggr(df, inc, "all_tu")
126126

127127
wg_income["wg_abzuege"] = calc_wg_abzuege(wg_income, tb)
128128

src/analysis/tax_transfer_funcs/taxes/calc_taxes.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ def tax_sched(df, tb):
1919
ts = pd.DataFrame(index=df.index.copy())
2020
for inc in tb["zve_list"]:
2121
ts["tax_" + inc] = tb["tax_schedule"](df["zve_" + inc], tb)
22-
ts["tax_{}_tu".format(inc)] = ts["tax_{}".format(inc)]
23-
ts.loc[adult_married, "tax_{}_tu".format(inc)] = ts["tax_{}".format(inc)][
24-
adult_married
25-
].sum()
22+
ts[f"tax_{inc}_tu"] = ts[f"tax_{inc}"]
23+
ts.loc[adult_married, f"tax_{inc}_tu"] = ts[f"tax_{inc}"][adult_married].sum()
2624

2725
# Abgeltungssteuer
2826
ts["abgst"] = abgeltung(df, tb)
@@ -49,8 +47,8 @@ def tax_sched(df, tb):
4947
[df["zveranl"], ~df["zveranl"]], [ts["soli_tu"] / 2, ts["soli_tu"]]
5048
)
5149
return ts[
52-
["tax_{}".format(inc) for inc in tb["zve_list"]]
53-
+ ["tax_{}_tu".format(inc) for inc in tb["zve_list"]]
50+
[f"tax_{inc}" for inc in tb["zve_list"]]
51+
+ [f"tax_{inc}_tu" for inc in tb["zve_list"]]
5452
+ ["abgst_tu", "abgst", "soli", "soli_tu"]
5553
]
5654

0 commit comments

Comments
 (0)