def test_tables(reformfile1): """ Test TaxCalcIO with output_tables=True and with positive weights. """ # create tabable input nobs = 100 idict = dict() idict['RECID'] = [i for i in range(1, nobs + 1)] idict['MARS'] = [2 for i in range(1, nobs + 1)] idict['s006'] = [10.0 for i in range(1, nobs + 1)] idict['e00300'] = [10000 * i for i in range(1, nobs + 1)] idict['expanded_income'] = idict['e00300'] idf = pd.DataFrame(idict, columns=list(idict)) # create and initialize TaxCalcIO object tcio = TaxCalcIO(input_data=idf, tax_year=2020, baseline=None, reform=reformfile1.name, assump=None) assert not tcio.errmsg tcio.init(input_data=idf, tax_year=2020, baseline=None, reform=reformfile1.name, assump=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg # create TaxCalcIO tables file tcio.analyze(writing_output_file=False, output_tables=True) # delete tables file output_filename = tcio.output_filepath() fname = output_filename.replace('.csv', '-tab.text') if os.path.isfile(fname): os.remove(fname)
def test_sqldb_option(reformfile1, assumpfile1): """ Test TaxCalcIO output_sqldb option when not writing_output_file. """ taxyear = 2021 tcio = TaxCalcIO(input_data=pd.read_csv(StringIO(RAWINPUT)), tax_year=taxyear, baseline=None, reform=reformfile1.name, assump=assumpfile1.name) assert not tcio.errmsg tcio.init(input_data=pd.read_csv(StringIO(RAWINPUT)), tax_year=taxyear, baseline=None, reform=reformfile1.name, assump=assumpfile1.name, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg outfilepath = tcio.output_filepath() dbfilepath = outfilepath.replace('.csv', '.db') # --sqldb output try: tcio.analyze(writing_output_file=False, output_sqldb=True) except Exception: # pylint: disable=broad-except if os.path.isfile(dbfilepath): try: os.remove(dbfilepath) except OSError: pass # sometimes we can't remove a generated temporary file assert 'TaxCalcIO.analyze(sqldb)_ok' == 'no' # if try was successful, remove the db file if os.path.isfile(dbfilepath): os.remove(dbfilepath)
def test_output_options(reformfile1, assumpfile1): """ Test TaxCalcIO output_dump options when writing_output_file. """ taxyear = 2021 tcio = TaxCalcIO(input_data=pd.read_csv(StringIO(RAWINPUT)), tax_year=taxyear, baseline=None, reform=reformfile1.name, assump=assumpfile1.name) assert not tcio.errmsg tcio.init(input_data=pd.read_csv(StringIO(RAWINPUT)), tax_year=taxyear, baseline=None, reform=reformfile1.name, assump=assumpfile1.name, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg outfilepath = tcio.output_filepath() # minimal output with no --dump option try: tcio.analyze(writing_output_file=True, output_dump=False) except Exception: # pylint: disable=broad-except if os.path.isfile(outfilepath): try: os.remove(outfilepath) except OSError: pass # sometimes we can't remove a generated temporary file assert 'TaxCalcIO.analyze(minimal_output)_ok' == 'no' # --dump output with full dump try: tcio.analyze(writing_output_file=True, output_dump=True) except Exception: # pylint: disable=broad-except if os.path.isfile(outfilepath): try: os.remove(outfilepath) except OSError: pass # sometimes we can't remove a generated temporary file assert 'TaxCalcIO.analyze(full_dump_output)_ok' == 'no' # --dump output with partial dump try: tcio.analyze(writing_output_file=True, dump_varset=set(['combined']), output_dump=True) except Exception: # pylint: disable=broad-except if os.path.isfile(outfilepath): try: os.remove(outfilepath) except OSError: pass # sometimes we can't remove a generated temporary file assert 'TaxCalcIO.analyze(partial_dump_output)_ok' == 'no' # if tries were successful, remove doc file and output file docfilepath = outfilepath.replace('.csv', '-doc.text') if os.path.isfile(docfilepath): os.remove(docfilepath) if os.path.isfile(outfilepath): os.remove(outfilepath)
def test_analyze_warnings_print(warnreformfile): """ Test TaxCalcIO.analyze method when there is a reform warning. """ taxyear = 2020 recdict = {'RECID': 1, 'MARS': 1, 'e00300': 100000, 's006': 1e8} recdf = pd.DataFrame(data=recdict, index=[0]) tcio = TaxCalcIO(input_data=recdf, tax_year=taxyear, baseline=None, reform=warnreformfile.name, assump=None) assert not tcio.errmsg tcio.init(input_data=recdf, tax_year=taxyear, baseline=None, reform=warnreformfile.name, assump=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg tcio.analyze(writing_output_file=False) assert tcio.tax_year() == taxyear
def test_ceeu_with_behavior(lumpsumreformfile, assumpfile2): """ Test TaxCalcIO.analyze method when assuming behavior & doing ceeu calcs. """ taxyear = 2020 recdict = {'RECID': 1, 'MARS': 1, 'e00300': 100000, 's006': 1e8} recdf = pd.DataFrame(data=recdict, index=[0]) tcio = TaxCalcIO(input_data=recdf, tax_year=taxyear, baseline=None, reform=lumpsumreformfile.name, assump=assumpfile2.name) assert not tcio.errmsg tcio.init(input_data=recdf, tax_year=taxyear, baseline=None, reform=lumpsumreformfile.name, assump=assumpfile2.name, growdiff_response=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg tcio.analyze(writing_output_file=False, output_ceeu=True) assert tcio.tax_year() == taxyear
def test_ceeu_output2(): """ Test TaxCalcIO calculate method with no output writing using ceeu option. """ taxyear = 2020 recdict = {'RECID': 1, 'MARS': 1, 'e00300': 100000, 's006': 1e8} recdf = pd.DataFrame(data=recdict, index=[0]) tcio = TaxCalcIO(input_data=recdf, tax_year=taxyear, baseline=None, reform=None, assump=None) assert not tcio.errmsg tcio.init(input_data=recdf, tax_year=taxyear, baseline=None, reform=None, assump=None, growdiff_response=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg tcio.analyze(writing_output_file=False, output_ceeu=True) assert tcio.tax_year() == taxyear
def test_tables(reformfile1): """ Test TaxCalcIO with output_tables=True and with positive weights. """ # create tabable input nobs = 100 idict = dict() idict['RECID'] = [i for i in range(1, nobs + 1)] idict['MARS'] = [2 for i in range(1, nobs + 1)] idict['s006'] = [10.0 for i in range(1, nobs + 1)] idict['e00300'] = [10000 * i for i in range(1, nobs + 1)] idict['expanded_income'] = idict['e00300'] idf = pd.DataFrame(idict, columns=list(idict)) # create and initialize TaxCalcIO object tcio = TaxCalcIO(input_data=idf, tax_year=2020, baseline=None, reform=reformfile1.name, assump=None) assert not tcio.errmsg tcio.init(input_data=idf, tax_year=2020, baseline=None, reform=reformfile1.name, assump=None, growdiff_response=None, aging_input_data=False, exact_calculations=False) assert not tcio.errmsg # create TaxCalcIO tables file tcio.analyze(writing_output_file=False, output_tables=True) # delete tables file output_filename = tcio.output_filepath() fname = output_filename.replace('.csv', '-tab.text') if os.path.isfile(fname): os.remove(fname)
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.')) 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 use of 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 (possibly extrapolated ' 'to TAXYEAR) and all calculated tax variables, ' 'where all the variables are named using their ' 'internal Tax-Calculator names. No --dump ' 'option implies OUTPUT contains minimal tax ' 'output.'), 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 tcio.analyze(writing_output_file=True, output_tables=args.tables, output_graphs=args.graphs, output_ceeu=args.ceeu, 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