Skip to content

Commit

Permalink
Merge pull request PolicyEngine#162 from noman404/noman404/issue157
Browse files Browse the repository at this point in the history
changed output type from file to console
  • Loading branch information
MaxGhenis authored Jan 2, 2025
2 parents cf87b47 + d6d5d9c commit 29a61b0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

taxsimid,year,state,fiitax,siitax,fica,tfica,v10,v11,v12,v13,v14,v17,v18,v19,v22,v23,v24,v26,v27,v28,v29,v30,v42,v43,v44,v45
6,2023,39,3956.0,2486.7,6196.5,6196.5,81000.0,0.0,0.0,27700.0,0.0,2486.7,53300.0,5956.0,2000.0,0.0,0.0,81000.0,0.0,5956.0,6196.5,68360.8,0.0,0.0,1174.5,0.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

taxsimid,year,state,fiitax,siitax,fica,tfica,v10,v11,v12,v13,v14,v17,v18,v19,v22,v23,v24,v26,v27,v28,v29,v30,v42,v43,v44,v45
6,2023,39,4945.0,2486.7,6196.5,6196.5,81000.0,0.0,0.0,20800.0,0.0,2486.7,60200.0,6945.0,2000.0,0.0,0.0,81000.0,0.0,6945.0,6196.5,67371.8,0.0,0.0,1174.5,0.0

5 changes: 5 additions & 0 deletions output/policyengine_taxsim_joint_household_output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

taxsimid,year,state,fiitax,siitax,fica,tfica,v10,v11,v12,v13,v14,v17,v18,v19,v22,v23,v24,v26,v27,v28,v29,v30,v42,v43,v44,v45
999,2023,44,1716.0,0.0,6043.5,6043.5,79000.0,0.0,0.0,27700.0,0.0,0.0,51300.0,5716.0,4000.0,0.0,0.0,79000.0,0.0,5716.0,6043.5,71240.5,0.0,0.0,1145.5,0.0
11,2023,44,1476.0,0.0,5890.5,5890.5,77000.0,0.0,0.0,27700.0,0.0,0.0,49300.0,5476.0,4000.0,0.0,0.0,77000.0,0.0,5476.0,5890.5,69633.5,0.0,0.0,1116.5,0.0

3 changes: 3 additions & 0 deletions output/policyengine_taxsim_single_household_output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
taxsimid,year,state,fiitax,siitax,fica,tfica,v10,v11,v12,v13,v14,v17,v18,v19,v22,v23,v24,v26,v27,v28,v29,v30,v42,v43,v44,v45
999,2021,3,2775.0,1008.9,3748.5,3748.5,49000.0,0.0,0.0,12550.0,0.0,1008.9,36450.0,4175.0,0.0,0.0,0.0,49000.0,0.0,4175.0,3748.5,41467.6,0.0,0.0,710.5,1400.0
11,2021,3,2535.0,942.1,3595.5,3595.5,47000.0,0.0,0.0,12550.0,0.0,942.1,34450.0,3935.0,0.0,0.0,0.0,47000.0,0.0,3935.0,3595.5,39927.4,0.0,0.0,681.5,1400.0
20 changes: 11 additions & 9 deletions policyengine_taxsim/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,29 @@ def main(input_file, output, logs):
for _, row in df.iterrows():
taxsim_input = row.to_dict()
pe_situation = generate_household(taxsim_input)

taxsim_output = export_household(taxsim_input, pe_situation, logs)

idtl = taxsim_input['idtl']
if idtl == 0:
taxsim_output = export_household(taxsim_input, pe_situation, logs)
idtl_0_results.append(taxsim_output)
elif idtl == 2:
taxsim_output = export_household(taxsim_input, pe_situation, logs)
idtl_2_results.append(taxsim_output)
else:
taxsim_output = export_household(taxsim_input, pe_situation, logs)
idtl_5_results += taxsim_output

# Create output dataframe and save to csv
idtl_0_output = to_csv_str(idtl_0_results)
idtl_2_output = to_csv_str(idtl_2_results)

output_path = Path(output)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_str = ""
if idtl_0_output:
output_str += idtl_0_output
if idtl_2_output:
output_str += f"\n{idtl_2_output}"
if idtl_5_results:
output_str += f"\n{idtl_5_results}"

with open(output_path, 'w', encoding='utf-8') as txt_file:
txt_file.write(f"{idtl_0_output}\n{idtl_2_output}\n{idtl_5_results}")
click.echo(f"Output saved to {output}")
print(output_str)
except Exception as e:
click.echo(f"Error processing input: {str(e)}", err=True)
raise
Expand Down
54 changes: 41 additions & 13 deletions policyengine_taxsim/exe.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
import sys
import os
from io import StringIO


# Delay imports until runtime
Expand Down Expand Up @@ -38,29 +39,56 @@ def main(input_file, output, logs):
"""
try:
# Get mapper functions at runtime
import_single_household, export_single_household = get_mappers()
generate_household, export_household = get_mappers()

# Read input file
df = pd.read_csv(input_file)

# Process each row
results = []
idtl_0_results = []
idtl_2_results = []
idtl_5_results = ""

for _, row in df.iterrows():
taxsim_input = row.to_dict()
pe_situation = import_single_household(taxsim_input)
taxsim_output = export_single_household(taxsim_input, pe_situation, logs)
results.append(taxsim_output)

# Create output dataframe and save to csv
output_df = pd.DataFrame(results)
output_path = Path(output)
output_path.parent.mkdir(parents=True, exist_ok=True)
output_df.to_csv(output_path, index=False)
click.echo(f"Output saved to {output}")
pe_situation = generate_household(taxsim_input)

taxsim_output = export_household(taxsim_input, pe_situation, logs)

idtl = taxsim_input['idtl']
if idtl == 0:
idtl_0_results.append(taxsim_output)
elif idtl == 2:
idtl_2_results.append(taxsim_output)
else:
idtl_5_results += taxsim_output

idtl_0_output = to_csv_str(idtl_0_results)
idtl_2_output = to_csv_str(idtl_2_results)

output_str = ""
if idtl_0_output:
output_str += idtl_0_output
if idtl_2_output:
output_str += f"\n{idtl_2_output}"
if idtl_5_results:
output_str += f"\n{idtl_5_results}"

print(output_str)
except Exception as e:
click.echo(f"Error processing input: {str(e)}", err=True)
raise


def to_csv_str(results):
if len(results) == 0 or results is None:
return ""

df = pd.DataFrame(results)
content = df.to_csv(index=False, float_format='%.1f', lineterminator='\n')
cleaned_df = pd.read_csv(StringIO(content))
return cleaned_df.to_csv(index=False, lineterminator='\n')


if __name__ == "__main__":
main()
main()

0 comments on commit 29a61b0

Please sign in to comment.