def test_custom_dump_variables(dumpvar_str, str_valid, num_vars): """ Test TaxCalcIO custom_dump_variables method. """ recdict = {'RECID': 1, 'MARS': 1, 'e00300': 100000, 's006': 1e8} recdf = pd.DataFrame(data=recdict, index=[0]) year = 2018 tcio = TaxCalcIO(input_data=recdf, tax_year=year, baseline=None, reform=None, assump=None) assert not tcio.errmsg tcio.init(input_data=recdf, tax_year=year, baseline=None, reform=None, assump=None, growdiff_response=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg varset = tcio.custom_dump_variables(dumpvar_str) assert isinstance(varset, set) valid = len(tcio.errmsg) == 0 assert valid == str_valid if valid: assert len(varset) == num_vars
def test_custom_dump_variables(dumpvar_str, str_valid, num_vars): """ Test TaxCalcIO custom_dump_variables method. """ recdict = {'RECID': 1, 'MARS': 1, 'e00300': 100000, 's006': 1e8} recdf = pd.DataFrame(data=recdict, index=[0]) year = 2018 tcio = TaxCalcIO(input_data=recdf, tax_year=year, baseline=None, reform=None, assump=None) assert not tcio.errmsg tcio.init(input_data=recdf, tax_year=year, baseline=None, reform=None, assump=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg varset = tcio.custom_dump_variables(dumpvar_str) assert isinstance(varset, set) valid = len(tcio.errmsg) == 0 assert valid == str_valid if valid: assert len(varset) == num_vars
def cli_tc_main(): """ Contains command-line interface (CLI) to Tax-Calculator TaxCalcIO class. """ # parse command-line arguments: usage_str = 'tc INPUT TAXYEAR {}{}{}'.format( '[--reform REFORM] [--assump ASSUMP]\n', ' ', '[--exact] [--tables] [--graphs] [--ceeu] [--dump] [--sqldb] [--test]') parser = argparse.ArgumentParser( prog='', usage=usage_str, description=('Writes to a file the federal income and payroll tax ' 'OUTPUT for each filing unit specified in the INPUT ' 'file, with the OUTPUT computed from the INPUT for the ' 'TAXYEAR using Tax-Calculator. The OUTPUT file is a ' 'CSV-formatted file that contains tax information for ' 'each INPUT filing unit under the reform.')) parser.add_argument('INPUT', nargs='?', help=('INPUT is name of CSV-formatted file that ' 'contains for each filing unit variables used ' 'to compute taxes for TAXYEAR. Specifying ' '"cps.csv" uses CPS input files included in ' 'the taxcalc package.'), default='') parser.add_argument('TAXYEAR', nargs='?', help=('TAXYEAR is calendar year for which taxes ' 'are computed.'), type=int, default=0) parser.add_argument('--reform', help=('REFORM is name of optional JSON reform file. ' 'No --reform implies a "null" reform (that is, ' 'current-law policy).'), default=None) parser.add_argument('--assump', help=('ASSUMP is name of optional JSON economic ' 'assumptions file. No --assump implies use ' 'of no customized assumptions.'), default=None) parser.add_argument('--exact', help=('optional flag that suppresses the smoothing of ' '"stair-step" provisions in the tax law that ' 'complicate marginal-tax-rate calculations.'), default=False, action="store_true") parser.add_argument('--tables', help=('optional flag that causes distributional ' 'tables to be written to a text file.'), default=False, action="store_true") parser.add_argument('--graphs', help=('optional flag that causes graphs to be written ' 'to HTML files for viewing in browser.'), default=False, action="store_true") parser.add_argument('--ceeu', help=('optional flag that causes normative welfare ' 'statistics, including certainty-equivalent ' 'expected-utility (ceeu) of after-tax income ' 'values for different ' 'constant-relative-risk-aversion parameter ' 'values, to be written to screen.'), default=False, action="store_true") parser.add_argument('--dump', help=('optional flag that causes OUTPUT to contain ' 'all INPUT variables (extrapolated to TAXYEAR) ' 'and all calculated tax variables for the ' 'reform, where all the variables are named ' 'using their internal Tax-Calculator names. ' 'No --dump option implies OUTPUT contains ' 'minimal tax output for the reform. ' 'NOTE: create a space-delimited file named ' 'tcdumpvars in directory where output is being ' 'written in order to specify a custom set of ' 'dump variables.'), default=False, action="store_true") parser.add_argument('--sqldb', help=('optional flag that writes SQLite database ' 'with dump table containing same output as ' 'produced by --dump option.'), default=False, action="store_true") parser.add_argument('--test', help=('optional flag that conducts installation ' 'test.'), default=False, action="store_true") args = parser.parse_args() # write test input and expected output files if --test option specified if args.test: _write_expected_test_output() inputfn = TEST_INPUT_FILENAME taxyear = TEST_TAXYEAR else: inputfn = args.INPUT taxyear = args.TAXYEAR # instantiate taxcalcio object and do tax analysis tcio = TaxCalcIO(input_data=inputfn, tax_year=taxyear, reform=args.reform, assump=args.assump) if tcio.errmsg: sys.stderr.write(tcio.errmsg) sys.stderr.write('USAGE: tc --help\n') return 1 aging = inputfn.endswith('puf.csv') or inputfn.endswith('cps.csv') tcio.init(input_data=inputfn, tax_year=taxyear, reform=args.reform, assump=args.assump, growdiff_response=None, aging_input_data=aging, exact_calculations=args.exact) if tcio.errmsg: sys.stderr.write(tcio.errmsg) sys.stderr.write('USAGE: tc --help\n') return 1 dumpvar_set = None if args.dump or args.sqldb: if os.path.exists('tcdumpvars'): with open('tcdumpvars') as vfile: dump_vars_str = vfile.read() dumpvar_set = tcio.custom_dump_variables(dump_vars_str) if tcio.errmsg: sys.stderr.write(tcio.errmsg) sys.stderr.write('USAGE: tc --help\n') return 1 tcio.analyze(writing_output_file=True, output_tables=args.tables, output_graphs=args.graphs, output_ceeu=args.ceeu, dump_varset=dumpvar_set, output_dump=args.dump, output_sqldb=args.sqldb) # compare test output with expected test output if --test option specified if args.test: retcode = _compare_test_output_files() else: retcode = 0 # return exit code return retcode