def cli_core(startyear, endyear, data, usecps, reform, behavior, assump, baseline, outdir, name, make_report, author): """ 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) if make_report: report(tb, name=name, outdir=outputpath, author=author)
def cli_core(startyear, endyear, data, usecps, reform, behavior, assump, baseline, outdir, name, make_report, author): """ Core logic for the CLI Parameters ---------- startyear: int year to start analysis endyear: int last year for analysis data: str or Pandas DataFrame path to or DataFrame with data for Tax-Calculator usecps: bool whether to use the CPS or (if False) the PUF-based file reform: dict parameter changes for reform run in Tax-Calculator behavior: dict behavioral assumptions for Behavioral-Responses assump: dict consumption assumptions base_policy: dict parameter changes (relative to current law baseline) for baseline policy verbose: bool indicator for printing of output Returns ------- None reports saved to disk at path specified by outdir """ 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(exist_ok=True) # 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(exist_ok=True) make_tables(tb, year, yeardir) if make_report: report(tb, name=name, outdir=outputpath, author=author)
def test_report(tb_static): """ Ensure that all report files are created """ outdir = "testreform" name = "Test Report" report(tb_static, name=name, outdir=outdir) dir_path = Path(outdir) assert dir_path.exists() assert Path(dir_path, "Test-Report.md").exists() assert Path(dir_path, "Test-Report.pdf").exists() diff_png = Path(dir_path, "difference_graph.png") assert diff_png.exists() dist_png = Path(dir_path, "dist_graph.png") assert dist_png.exists() shutil.rmtree(dir_path) # test clean report _content = report(tb_static, name=name, outdir=outdir, clean=True) assert not dir_path.exists()
def test_report(tb_static): """ Ensure that all report files are created """ outdir = "testreform" name = "Test Report" report( tb_static, name=name, outdir=outdir ) dir_path = Path(outdir) assert dir_path.exists() assert Path(dir_path, "Test-Report.md").exists() assert Path(dir_path, "Test-Report.pdf").exists() assert Path(dir_path, "Test-Report.html").exists() diff_png = Path(dir_path, "difference_graph.png") diff_svg = Path(dir_path, "difference_graph.svg") assert diff_png.exists() or diff_svg.exists() dist_png = Path(dir_path, "dist_graph.png") dist_svg = Path(dir_path, "dist_graph.svg") assert dist_png.exists() or dist_svg.exists() shutil.rmtree(dir_path)
def run_model(meta_params_dict, adjustment): """ Runs TaxBrain """ # update meta parameters meta_params = MetaParameters() meta_params.adjust(meta_params_dict) # convert COMP user inputs to format accepted by tax-calculator policy_mods = cs2tc.convert_policy_adjustment(adjustment["policy"]) behavior_mods = cs2tc.convert_behavior_adjustment(adjustment["behavior"]) user_mods = {"policy": policy_mods, "behavior": behavior_mods} start_year = int(meta_params.year) use_cps = meta_params.data_source == "CPS" if meta_params.data_source == "PUF": puf_df = retrieve_puf(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) if puf_df is not None: if not isinstance(puf_df, pd.DataFrame): raise TypeError("'puf_df' must be a Pandas DataFrame.") fuzz = True use_cps = False sampling_frac = 0.05 sampling_seed = 2222 full_sample = puf_df else: # Access keys are not available. Default to the CPS. print("Defaulting to the CPS") meta_params.adjust({"data_source": "CPS"}) if meta_params.data_source == "CPS": fuzz = False use_cps = True input_path = os.path.join(TCDIR, "cps.csv.gz") # full_sample = read_egg_csv(cpspath) # pragma: no cover sampling_frac = 0.03 sampling_seed = 180 full_sample = pd.read_csv(input_path) if meta_params.use_full_sample: sample = full_sample end_year = min(start_year + 10, TaxBrain.LAST_BUDGET_YEAR) else: sample = full_sample.sample(frac=sampling_frac, random_state=sampling_seed) end_year = start_year tb = TaxBrain(start_year, end_year, microdata=sample, use_cps=use_cps, reform=policy_mods, behavior=behavior_mods) tb.run() # Collect results for each year delayed_list = [] for year in range(start_year, end_year + 1): print('delaying for', year) delay = delayed(nth_year_results)(tb, year, user_mods, fuzz) delayed_list.append(delay) results = compute(*delayed_list) # process results to get them ready for display # create aggregate plot agg_plot = aggregate_plot(tb) all_to_process = defaultdict(list) for result in results: for key, value in result.items(): all_to_process[key] += value results, downloadable = postprocess(all_to_process) # create report output if it is not a run with no reforme if tb.params["policy"].keys(): report_outputs = report(tb, clean=True) for name, data in report_outputs.items(): if name.endswith(".md"): media_type = "Markdown" elif name.endswith(".pdf"): media_type = "PDF" downloadable.append({ "media_type": media_type, "title": name, "data": data }) agg_output, table_output = create_layout(results, start_year, end_year) comp_outputs = { "renderable": [agg_plot, agg_output, table_output], "downloadable": downloadable } return comp_outputs
# run dynamic analysis tb_dynamic = TaxBrain(2019, 2028, use_cps=True, reform=reform_url, behavior={"sub": 0.25}) tb_dynamic.run() dynamic_table = tb_dynamic.weighted_totals("c00100") print("Dynamic Results") print(dynamic_table) # produce a differences table diff = tb_static.differences_table(2019, "weighted_deciles", "combined") print("\nDifferences Table for 2019") print(diff) # produce a distribution table dist = tb_dynamic.distribution_table(2019, "weighted_deciles", "expanded_income", "reform") print("\nDistribution Table for 2019") print(dist) # produce a pdf report summarizing the effects of the reform outdir = "larsonreform" name = "The Social Security 2100 Act: Rep. John Larson" author = "Anderson Frailey" report( tb_dynamic, name=name, author=author, outdir=outdir )