Skip to content

Commit

Permalink
Merge pull request #68 from andersonfrailey/cli
Browse files Browse the repository at this point in the history
Add CLI Pt. 2
  • Loading branch information
andersonfrailey authored Jul 2, 2019
2 parents c93dfe4 + 2f42093 commit 281f9a8
Show file tree
Hide file tree
Showing 2 changed files with 167 additions and 2 deletions.
4 changes: 2 additions & 2 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 2019-xx-xx Release x.x.x

Last Merged Pull Request: [#67](https://github.com/PSLmodels/Tax-Brain/pull/67)
Last Merged Pull Request: [#68](https://github.com/PSLmodels/Tax-Brain/pull/68)

Changes in this release:

Expand All @@ -16,7 +16,7 @@ Changes in this release:
* Allow users to specify an alternative policy to use as the baseline, rather
than current law ([#64](https://github.com/PSLmodels/Tax-Brain/pull/64))
* Update COMP table outputs so they are more readable ([#66](https://github.com/PSLmodels/Tax-Brain/pull/66))
* Add TaxBrain command line interface ([#67](https://github.com/PSLmodels/Tax-Brain/pull/67))
* Add TaxBrain command line interface ([#67](https://github.com/PSLmodels/Tax-Brain/pull/67), [#68](https://github.com/PSLmodels/Tax-Brain/pull/68))

## 2019-05-24 Release 2.2.1

Expand Down
165 changes: 165 additions & 0 deletions taxbrain/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
"""
Command line interface for the Tax-Brain package
"""
import argparse
from taxbrain import TaxBrain
from pathlib import Path
from datetime import datetime


def make_tables(tb, year, outpath):
"""
Make and write all of the tables for a given year
"""
dist_table_base = tb.distribution_table(
year, "weighted_deciles", "expanded_income", "base"
)
dist_table_base.to_csv(
Path(outpath, f"distribution_table_base_{year}.csv")
)
dist_table_reform = tb.distribution_table(
year, "weighted_deciles", "expanded_income", "reform"
)
dist_table_reform.to_csv(
Path(outpath, f"distribution_table_reform_{year}.csv")
)
diff_table = tb.differences_table(
year, "weighted_deciles", "combined"
)
diff_table.to_csv(
Path(outpath, f"differences_table_{year}.csv")
)
del dist_table_base, dist_table_reform, diff_table


def cli_core(startyear, endyear, data, usecps, reform, behavior, assump,
baseline, outdir, name):
"""
Core logic for the CLI
"""
tb = TaxBrain(
start_year=startyear, end_year=endyear, microdata=data,
use_cps=usecps, reform=reform, behavior=behavior,
assump=assump, base_policy=baseline, verbose=True
)
tb.run()

# create outputs
dirname = name
if not dirname:
dirname = f"TaxBrain Analysis {datetime.today().date()}"
outputpath = Path(outdir, dirname)
outputpath.mkdir()
# create output tables
aggregate = tb.weighted_totals("combined")
aggregate.to_csv(
Path(outputpath, "aggregate_tax_liability.csv")
)
for year in range(startyear, endyear + 1):
yeardir = Path(outputpath, str(year))
yeardir.mkdir()
make_tables(tb, year, yeardir)


def cli_main():
"""
Command line interface to taxbrain package
"""
parser_desription = (
"This is the command line interface for the taxbrain package."
)
parser = argparse.ArgumentParser(
prog="taxbrain",
description=parser_desription
)
parser.add_argument(
"startyear",
help=(
"startyear is the first year of the analysis you want to run."
),
default=TaxBrain.FIRST_BUDGET_YEAR,
type=int
)
parser.add_argument(
"endyear",
help=(
"endyear is the last year of the analysis you want to run."
),
default=TaxBrain.LAST_BUDGET_YEAR,
type=int
)
parser.add_argument(
"--data",
help=(
"The file path to a micro-dataset that is formatted for use in "
"Tax-Calculator."
),
default=None
)
parser.add_argument(
"--usecps",
help=(
"If this argument is present, the CPS file included in "
"Tax-Calculator will be used for the analysis."
),
default=False,
action="store_true"
),
parser.add_argument(
"--reform",
help=(
"--reform should be a path to a JSON file."
),
default=None
)
parser.add_argument(
"--behavior",
help=(
"--behavior should be a path to a JSON file containing behavioral "
"assumptions."
),
default=None
)
parser.add_argument(
"--assump",
help=(
"--assump should be a path to a JSON file containing user "
"specified economic assumptions."
),
default=None
)
parser.add_argument(
"--baseline",
help=(
"--baseline should be a path to a JSON file containing a policy "
"that will be used as the baseline of the analysis"
)
)
parser.add_argument(
"--outdir",
help=(
"outdir is the name of the output directory. Not including "
"--outdir will result in files being written to the current "
"directory."
),
default=""
),
parser.add_argument(
"--name",
help=(
"Name of the analysis. This will be used to name the directory "
"where all output files will be written."
),
default=None
)
args = parser.parse_args()

# run the analysis
cli_core(
args.startyear, args.endyear, args.data, args.usecps, args.reform,
args.behavior, args.assump, args.baseline, args.outdir, args.name
)


if __name__ == "__main__":
cli_main()

0 comments on commit 281f9a8

Please sign in to comment.