-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathapi.py
More file actions
223 lines (203 loc) · 5.96 KB
/
api.py
File metadata and controls
223 lines (203 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import time
import plotly.graph_objects as go
import requests
from .formatting import (
BLUE_LIGHT,
BLUE_PRIMARY,
DARK_GRAY,
MEDIUM_DARK_GRAY,
WHITE,
format_fig,
)
def get_api_chart_data(
country_id: str,
reform_policy_id: int,
chart_key: str,
region: str,
time_period: str,
baseline_policy_id: int = None,
version: str = None,
) -> dict:
if baseline_policy_id is None or version is None:
response = requests.get(f"https://api.policyengine.org/{country_id}/metadata")
result = response.json().get("result", {})
baseline_policy_id = result.get("current_law_id")
version = result.get("version")
url = f"https://api.policyengine.org/{country_id}/economy/{reform_policy_id}/over/{baseline_policy_id}?region={region}&time_period={time_period}&version={version}"
response = requests.get(url)
if not response.ok:
raise ValueError(response.text)
json = response.json()
while json.get("status") == "computing":
time.sleep(1)
response = requests.get(url)
if not response.ok:
raise ValueError(response.text)
json = response.json()
return json.get("result", {}).get(chart_key)
# Specific chart definitions
def intra_decile_chart(
country_id: str,
reform_policy_id: int,
region: str,
time_period: str,
baseline_policy_id: int = None,
) -> go.Figure:
impact = get_api_chart_data(
country_id=country_id,
reform_policy_id=reform_policy_id,
chart_key="intra_decile",
baseline_policy_id=baseline_policy_id,
region=region,
time_period=time_period,
)
impact = {"intra_decile": impact}
decile_numbers = list(range(1, 11))
outcome_labels = [
"Gain more than 5%",
"Gain less than 5%",
"No change",
"Lose less than 5%",
"Lose more than 5%",
]
outcome_colours = [
DARK_GRAY,
MEDIUM_DARK_GRAY,
WHITE,
BLUE_LIGHT,
BLUE_PRIMARY,
][::-1]
data = []
for outcome_label, outcome_colour in zip(outcome_labels, outcome_colours):
data.append(
{
"type": "bar",
"y": ["All"],
"x": [impact["intra_decile"]["all"][outcome_label]],
"name": outcome_label,
"legendgroup": outcome_label,
"offsetgroup": outcome_label,
"marker": {
"color": outcome_colour,
},
"orientation": "h",
"text": [f"{impact['intra_decile']['all'][outcome_label] * 100:.0f}%"],
"textposition": "inside",
"textangle": 0,
"xaxis": "x",
"yaxis": "y",
"showlegend": False,
"hoverinfo": "none",
}
)
for outcome_label, outcome_colour in zip(outcome_labels, outcome_colours):
data.append(
{
"type": "bar",
"y": decile_numbers,
"x": impact["intra_decile"]["deciles"][outcome_label],
"name": outcome_label,
"marker": {
"color": outcome_colour,
},
"orientation": "h",
"text": [
f"{value * 100:.0f}%"
for value in impact["intra_decile"]["deciles"][outcome_label]
],
"textposition": "inside",
"textangle": 0,
"xaxis": "x2",
"yaxis": "y2",
"hoverinfo": "none",
}
)
layout = {
"barmode": "stack",
"grid": {
"rows": 2,
"columns": 1,
},
"yaxis": {
"title": "",
"tickvals": ["All"],
"domain": [0.91, 1],
},
"xaxis": {
"title": "",
"tickformat": ".0%",
"anchor": "y",
"matches": "x2",
"showgrid": False,
"showticklabels": False,
},
"xaxis2": {
"title": "Population share",
"tickformat": ".0%",
"anchor": "y2",
},
"yaxis2": {
"title": "Income decile",
"tickvals": decile_numbers,
"anchor": "x2",
"domain": [0, 0.85],
},
"uniformtext": {
"mode": "hide",
"minsize": 10,
},
}
return format_fig(go.Figure(data=data, layout=layout)).update_traces(
marker_line_width=0,
width=[0.9] * 10,
)
def decile_chart(
country_id: str,
reform_policy_id: int,
region: str,
time_period: str,
baseline_policy_id: int = None,
) -> go.Figure:
impact = get_api_chart_data(
country_id=country_id,
reform_policy_id=reform_policy_id,
chart_key="decile",
baseline_policy_id=baseline_policy_id,
region=region,
time_period=time_period,
)
impact = {"decile": impact}
decile_numbers = list(range(1, 11))
# Sort deciles by key order 1 to 10
decile_values = []
for i in decile_numbers:
decile_values.append(impact["decile"]["relative"][str(i)])
data = [
{
"x": decile_numbers,
"y": decile_values,
"type": "bar",
"marker": {
"color": [
DARK_GRAY if value < 0 else BLUE_PRIMARY for value in decile_values
],
},
"text": [f"{value:+.1%}" for value in decile_values],
"textangle": 0,
}
]
layout = {
"xaxis": {
"title": "Income decile",
"tickvals": decile_numbers,
},
"yaxis": {
"title": "Relative change",
"tickformat": "+,.0%",
},
"uniformtext": {
"mode": "hide",
"minsize": 8,
},
}
return format_fig(go.Figure(data=data, layout=layout))