From c716a0e05fe5f9a7acca9368a1b7b58feadb4509 Mon Sep 17 00:00:00 2001 From: andersonfrailey Date: Tue, 2 Jul 2019 09:44:26 -0400 Subject: [PATCH 1/2] Add CLI pt 2 --- taxbrain/cli.py | 165 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 taxbrain/cli.py diff --git a/taxbrain/cli.py b/taxbrain/cli.py new file mode 100644 index 0000000..34d1738 --- /dev/null +++ b/taxbrain/cli.py @@ -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() From 2f42093e88527ed9aeeffa50b143b8eab1d05179 Mon Sep 17 00:00:00 2001 From: andersonfrailey Date: Tue, 2 Jul 2019 09:46:19 -0400 Subject: [PATCH 2/2] update realeases.md --- RELEASES.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/RELEASES.md b/RELEASES.md index ac61686..02c2a56 100644 --- a/RELEASES.md +++ b/RELEASES.md @@ -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: @@ -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