forked from PolicyEngine/new-york-credit-comparison
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.py
161 lines (137 loc) · 4.65 KB
/
format.py
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
# format.py
import streamlit as st
import plotly.graph_objects as go
import pandas as pd
import numpy as np
from constants import colors
def render_results(results, results_df):
"""Visualization function that shows net income changes only."""
if results_df.empty:
st.warning("No results to display. Please run the simulation first.")
return
# Process dataframe
df = prepare_data(results_df)
# Show net income changes
st.subheader("Net Income Change")
income_fig = create_bar_chart(
df,
metrics={
"Hochul": {
"column": "ctc_change",
"years": [2025, 2026],
"color": colors["TEAL_ACCENT"],
"label": "Hochul's ESCC Expansion",
},
"WFTC": {
"column": "wftc_change",
"years": [2025, 2026, 2029],
"color": colors["BLUE"], # Example color choice
"label": "WFTC Proposal",
},
},
y_title="Net Income Change ($)",
)
st.plotly_chart(income_fig, use_container_width=False)
def prepare_data(results_df):
"""Process and clean the input dataframe."""
# Get the current year and reform years data
current_year_data = (
results_df[results_df["year"] == 2024].copy()
if 2024 in results_df["year"].values
else pd.DataFrame()
)
reform_years_data = results_df[results_df["year"] > 2024].copy()
# Combine the data
df = pd.concat([current_year_data, reform_years_data])
# Convert any numpy arrays to simple numbers
for col in df.columns:
df[col] = df[col].apply(
lambda x: x.item() if isinstance(x, np.ndarray) and x.size == 1 else x
)
# Fill NaN values with 0
df = df.fillna(0)
return df
def create_bar_chart(df, metrics, y_title):
"""Create a bar chart with specified metrics and formatting.
Args:
df: DataFrame with the data
metrics: Dictionary with metrics definition:
{
"Policy Name": {
"column": column_name,
"years": [list of years],
"color": color_code,
"label": legend_label
}
}
y_title: Title for Y axis
"""
fig = go.Figure()
# List to keep track of policies for legend
legend_items = []
# Process each policy metric
for policy_name, config in metrics.items():
years = config.get("years", [])
column = config["column"]
color = config["color"]
label = config.get("label", policy_name)
# Add to legend items if not already there
if policy_name not in legend_items:
legend_items.append(policy_name)
# Add a bar for each year
for year in years:
value = extract_value(df, year, column)
x_label = f"{label.split(' ')[0]} {year}" if year != 2024 else label
fig.add_trace(
go.Bar(
x=[x_label],
y=[value],
name=policy_name,
marker_color=color,
showlegend=False, # We'll add custom legend items later
)
)
# Add value label if value exists
if value > 0:
fig.add_annotation(
x=x_label,
y=value,
text=f"${int(value)}",
showarrow=False,
yshift=10,
)
# Create custom legend
legend_colors = {policy: config["color"] for policy, config in metrics.items()}
for policy in legend_items:
fig.add_trace(
go.Bar(
x=[None],
y=[None],
name=metrics[policy]["label"],
marker_color=legend_colors[policy],
showlegend=True,
)
)
# Layout configuration
fig.update_layout(
showlegend=True,
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5),
xaxis_title="",
yaxis_title=y_title,
yaxis_tickformat="$,.0f",
height=500,
margin=dict(t=100),
)
return fig
def extract_value(df, year, column):
"""Safely extract a value from the dataframe."""
value = 0
if year in df["year"].values and column in df.columns:
data = df[df["year"] == year]
if not data.empty:
val = data[column].values[0]
import pandas as pd
if pd.isna(val) or not isinstance(val, (int, float)):
val = 0
value = val
return value