-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from PolicyEngine/api-parity
Add documentation and chart functions
- Loading branch information
Showing
25 changed files
with
4,221 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Mainly simulation options and parameters.""" | ||
|
||
# Datasets | ||
|
||
ENHANCED_FRS = "hf://policyengine/policyengine-uk-data/enhanced_frs_2022_23.h5" | ||
FRS = "hf://policyengine/policyengine-uk-data/frs_2022_23.h5" | ||
|
||
ENHANCED_CPS = "hf://policyengine/policyengine-us-data/enhanced_cps_2024.h5" | ||
CPS = "hf://policyengine/policyengine-us-data/cps_2023.h5" | ||
POOLED_CPS = "hf://policyengine/policyengine-us-data/pooled_3_year_cps_2023.h5" | ||
|
||
DATASETS = { | ||
"uk": {"frs": FRS, "enhanced_frs": ENHANCED_FRS}, | ||
"us": {"cps": CPS, "enhanced_cps": ENHANCED_CPS, "pooled_cps": POOLED_CPS}, | ||
} | ||
|
||
DEFAULT_DATASETS = { | ||
"uk": ENHANCED_FRS, | ||
"us": CPS, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
def net_income(simulation): | ||
return simulation.baseline.calculate("household_net_income").sum() | ||
return simulation.selected.calculate("household_net_income").sum() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
policyengine/outputs/macro/comparison/local_areas/parliamentary_constituencies/data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from policyengine import Simulation | ||
|
||
|
||
def data(simulation: Simulation) -> dict: | ||
if not simulation.options.get("include_constituencies"): | ||
return {} | ||
|
||
constituency_baseline = simulation.calculate( | ||
"macro/baseline/gov/local_areas/parliamentary_constituencies" | ||
) | ||
constituency_reform = simulation.calculate( | ||
"macro/reform/gov/local_areas/parliamentary_constituencies" | ||
) | ||
|
||
result = {} | ||
|
||
for constituency in constituency_baseline: | ||
result[constituency] = {} | ||
for key in constituency_baseline[constituency]: | ||
result[constituency][key] = { | ||
"change": constituency_reform[constituency][key] | ||
- constituency_baseline[constituency][key], | ||
"baseline": constituency_baseline[constituency][key], | ||
"reform": constituency_reform[constituency][key], | ||
} | ||
|
||
return result |
136 changes: 136 additions & 0 deletions
136
policyengine/outputs/macro/comparison/local_areas/parliamentary_constituencies/heatmap.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
from policyengine import Simulation | ||
import pandas as pd | ||
from policyengine.utils.huggingface import download | ||
import plotly.express as px | ||
from policyengine.utils.charts import * | ||
|
||
|
||
def heatmap( | ||
simulation: Simulation, | ||
variable: str = None, | ||
aggregator: str = None, | ||
relative: bool = None, | ||
) -> dict: | ||
if not simulation.options.get("include_constituencies"): | ||
return {} | ||
|
||
options = {} | ||
|
||
if variable is not None: | ||
options["variables"] = [variable] | ||
if aggregator is not None: | ||
options["aggregator"] = aggregator | ||
|
||
constituency_baseline = simulation.calculate( | ||
"macro/baseline/gov/local_areas/parliamentary_constituencies", | ||
**options, | ||
) | ||
constituency_reform = simulation.calculate( | ||
"macro/reform/gov/local_areas/parliamentary_constituencies", **options | ||
) | ||
|
||
result = {} | ||
|
||
constituency_names_file_path = download( | ||
repo="policyengine/policyengine-uk-data", | ||
repo_filename="constituencies_2024.csv", | ||
local_folder=None, | ||
version=None, | ||
) | ||
constituency_names = pd.read_csv(constituency_names_file_path) | ||
hex_map_locations = pd.read_csv( | ||
"/Users/nikhilwoodruff/uk-local-area-calibration/policyengine_uk_local_areas/hex_map/hex_map_2024.csv" | ||
).set_index("code") | ||
|
||
if variable is None: | ||
variable = "household_net_income" | ||
if relative is None: | ||
relative = True | ||
|
||
for constituency in constituency_baseline: | ||
if relative: | ||
result[constituency] = ( | ||
constituency_reform[constituency][variable] | ||
/ constituency_baseline[constituency][variable] | ||
- 1 | ||
) | ||
else: | ||
result[constituency] = ( | ||
constituency_reform[constituency][variable] | ||
- constituency_baseline[constituency][variable] | ||
) | ||
|
||
constituency_names["x"] = hex_map_locations.loc[ | ||
constituency_names["code"] | ||
]["x"].values | ||
constituency_names["y"] = hex_map_locations.loc[ | ||
constituency_names["code"] | ||
]["y"].values | ||
x_range = constituency_names["x"].max() - constituency_names["x"].min() | ||
y_range = constituency_names["y"].max() - constituency_names["y"].min() | ||
# Expand x range to preserve aspect ratio | ||
expanded_lower_x_range = -(y_range - x_range) / 2 | ||
expanded_upper_x_range = x_range - expanded_lower_x_range | ||
constituency_names.x = ( | ||
constituency_names.x - (constituency_names.y % 2 == 0) * 0.5 | ||
) | ||
constituency_names["Relative change"] = ( | ||
pd.Series(list(result.values()), index=list(result.keys())) | ||
.loc[constituency_names["name"]] | ||
.values | ||
) | ||
|
||
label = simulation.baseline.tax_benefit_system.variables[variable].label | ||
|
||
fig = px.scatter( | ||
constituency_names, | ||
x="x", | ||
y="y", | ||
color="Relative change", | ||
hover_name="name", | ||
title=f"{'Relative change' if relative else 'Change'} in {label} by parliamentary constituency", | ||
) | ||
|
||
format_fig(fig) | ||
|
||
# Show hexagons on scatter points | ||
|
||
fig.update_traces( | ||
marker=dict( | ||
symbol="hexagon", line=dict(width=0, color="lightgray"), size=15 | ||
) | ||
) | ||
fig.update_layout( | ||
xaxis_tickvals=[], | ||
xaxis_title="", | ||
yaxis_tickvals=[], | ||
yaxis_title="", | ||
xaxis_range=[expanded_lower_x_range, expanded_upper_x_range], | ||
yaxis_range=[ | ||
constituency_names["y"].min(), | ||
constituency_names["y"].max(), | ||
], | ||
).update_traces(marker_size=10).update_layout( | ||
xaxis_range=[30, 85], yaxis_range=[-50, 2] | ||
) | ||
|
||
x_min = fig.data[0]["marker"]["color"].min() | ||
x_max = fig.data[0]["marker"]["color"].max() | ||
max_abs = max(abs(x_min), abs(x_max)) | ||
|
||
fig.update_layout( | ||
coloraxis=dict( | ||
cmin=-max_abs, | ||
cmax=max_abs, | ||
colorscale=[ | ||
[0, DARK_GRAY], | ||
[0.5, "lightgray"], | ||
[1, BLUE], | ||
], | ||
colorbar=dict( | ||
tickformat=".0%" if relative else ",.0f", | ||
), | ||
) | ||
) | ||
|
||
return fig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.