-
Notifications
You must be signed in to change notification settings - Fork 1
/
calculator.py
234 lines (180 loc) · 7.08 KB
/
calculator.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
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
224
225
226
227
228
229
230
231
232
233
234
import pandas as pd
from results import calculate_consolidated_results
from graph import create_reform_comparison_graph
from utils import MAIN_METRICS, format_credit_name, format_currency
import streamlit as st
from policyengine_us.variables.household.income.household.household_benefits import (
household_benefits as HouseholdBenefits,
)
from utils import format_program_name, format_currency
from results import load_credits_from_yaml
def calculate_reforms(inputs, progress_text, chart_placeholder):
summary_results = {}
# Calculate baseline first to get all possible metrics
progress_text.text("Calculating Baseline...")
baseline_results = calculate_consolidated_results("Baseline", **inputs)
# Initialize DataFrame with all metrics from baseline results
results_df = pd.DataFrame(
index=baseline_results.columns,
columns=["Baseline", "Harris", "Trump"],
dtype=float,
)
# Fill in baseline values and show initial results
for idx in results_df.index:
results_df.at[idx, "Baseline"] = baseline_results.loc["Baseline", idx]
summary_results["Baseline"] = results_df.at["Household Net Income", "Baseline"]
fig = create_reform_comparison_graph(summary_results)
chart_placeholder.plotly_chart(fig, use_container_width=True)
# Calculate other reforms
for reform in ["Harris", "Trump"]:
progress_text.text(f"Calculating {reform}...")
reform_results = calculate_consolidated_results(reform, **inputs)
# Update results for all metrics
for idx in results_df.index:
if idx in reform_results.columns:
results_df.at[idx, reform] = reform_results.loc[reform, idx]
# Update summary for chart
summary_results[reform] = results_df.at["Household Net Income", reform]
# Update chart
fig = create_reform_comparison_graph(summary_results)
chart_placeholder.plotly_chart(fig, use_container_width=True)
return summary_results, results_df
def format_detailed_metrics(results_df):
formatted_df = results_df.copy()
formatted_df = formatted_df.loc[MAIN_METRICS]
formatted_df = formatted_df.round(2)
formatted_df = formatted_df.applymap(format_currency)
return formatted_df
def format_federal_credit_components(results_df):
"""Format federal credit components, showing items with baseline or reform values"""
formatted_df = results_df.copy()
# Get credits loaded from federal YAML file
package = "policyengine_us"
resource_path_federal = "parameters/gov/irs/credits/refundable.yaml"
try:
federal_refundable_credits = load_credits_from_yaml(
package, resource_path_federal
)
except FileNotFoundError:
federal_refundable_credits = []
# Get credits that are in the federal YAML
federal_credit_rows = [
idx
for idx in formatted_df.index
if idx not in MAIN_METRICS and idx in federal_refundable_credits
]
if not federal_credit_rows:
return None
# Keep only credits with values
formatted_df = formatted_df.loc[federal_credit_rows]
formatted_df = formatted_df.round(2)
formatted_df = formatted_df.applymap(format_currency)
# Format index names
formatted_df.index = [format_credit_name(idx) for idx in formatted_df.index]
return formatted_df
def format_state_credit_components(results_df, state_code):
"""Format state credit components, showing items with baseline or reform values"""
formatted_df = results_df.copy()
# Get all state-specific rows
state_credit_rows = [
idx for idx in formatted_df.index if idx.startswith(state_code.lower() + "_")
]
print(f"Found state credit rows: {state_credit_rows}")
if not state_credit_rows:
return None
formatted_df = formatted_df.loc[state_credit_rows]
formatted_df = formatted_df.round(2)
formatted_df = formatted_df.applymap(format_currency)
# Format index names
formatted_df.index = [
format_credit_name(idx, state_code) for idx in formatted_df.index
]
return formatted_df
def format_benefits_components(results_df):
"""Format the benefits breakdown showing all non-zero benefits"""
formatted_df = results_df.copy()
# List of known benefit names
benefits = [
"snap",
"tanf",
"ssi",
"housing_vouchers",
"medicaid",
"medicare",
"social_security",
"unemployment_compensation",
"wic",
"free_school_meals",
"reduced_price_school_meals",
"spm_unit_broadband_subsidy",
"high_efficiency_electric_home_rebate",
"residential_efficiency_electrification_rebate",
"head_start",
"early_head_start",
]
# Get all benefits that appear in the results
benefit_rows = [
idx
for idx in formatted_df.index
if idx in benefits and any(formatted_df.loc[idx] != 0)
]
if not benefit_rows:
return None
# Keep only benefits with values
formatted_df = formatted_df.loc[benefit_rows]
formatted_df = formatted_df.round(2)
formatted_df = formatted_df.applymap(format_currency)
# Format index names using program name formatter
formatted_df.index = [format_program_name(idx) for idx in formatted_df.index]
# Sort by baseline values
formatted_df = formatted_df.sort_values("Baseline", ascending=False)
return formatted_df
def format_tax_components(results_df):
"""Format the tax breakdown components"""
formatted_df = results_df.copy()
# List of tax component rows
tax_components = [
"employee_payroll_tax",
"income_tax_before_refundable_credits",
"household_state_tax_before_refundable_credits",
]
# Keep only tax components and transpose to get components as columns
try:
formatted_df = formatted_df.loc[
[row for row in tax_components if row in formatted_df.index]
]
if formatted_df.empty:
return None
formatted_df = formatted_df.round(2)
formatted_df = formatted_df.applymap(format_currency)
# Rename the index for better display
formatted_df.index = [
"Payroll Tax",
"Federal Income Tax",
"State Income Tax",
]
return formatted_df
except KeyError:
return None
def format_tariff_components(results_df):
"""Format the tariff components"""
formatted_df = results_df.copy()
# List of tariff component rows
tariff_components = ["china_tariffs", "other_tariffs"]
# Keep only tariff components
try:
formatted_df = formatted_df.loc[
[row for row in tariff_components if row in formatted_df.index]
]
if formatted_df.empty:
return None
formatted_df = formatted_df.round(2)
formatted_df = formatted_df.applymap(format_currency)
# Rename the index for better display
formatted_df.index = [
"New Tariffs on Chinese Imports",
"New Tariffs on Other Imports",
]
return formatted_df
except KeyError:
return None