From 0a3b4065bdb990df4d4139a081798dfd276763be Mon Sep 17 00:00:00 2001 From: jdebacker Date: Thu, 16 May 2024 14:54:26 -0400 Subject: [PATCH 1/6] use not age specific, save all output to folder --- examples/run_og_usa.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/examples/run_og_usa.py b/examples/run_og_usa.py index 8606b1c6..b3eb94e7 100644 --- a/examples/run_og_usa.py +++ b/examples/run_og_usa.py @@ -46,6 +46,7 @@ def main(): ) ) p.tax_func_type = "GS" + p.age_specific = False c = Calibration(p, estimate_tax_functions=True, client=client) # close and delete client bc cache is too large client.close() @@ -99,6 +100,7 @@ def main(): ) ) p2.tax_func_type = "GS" + p2.age_specific = False # Use calibration class to estimate reform tax functions from # Tax-Calculator, specifying reform for Tax-Calculator in iit_reform c2 = Calibration( @@ -152,12 +154,21 @@ def main(): # create plots of output op.plot_all( - base_dir, reform_dir, os.path.join(CUR_DIR, "OG-USA_example_plots") + base_dir, reform_dir, os.path.join(CUR_DIR, "OG-USA_example_plots_tables") ) + # Create CSV file with output + ot.tp_output_dump_table( + base_params, + base_tpi, + reform_params, + reform_tpi, + table_format="csv", + path=os.path.join(CUR_DIR, "OG-USA_example_plots_tables", "macro_time_series_output.csv") + ) print("Percentage changes in aggregates:", ans) # save percentage change output to csv file - ans.to_csv("ogusa_example_output.csv") + ans.to_csv(os.path.join(CUR_DIR, "OG-USA_example_plots_tables", "ogusa_example_output.csv")) if __name__ == "__main__": From 121b5e2e41b375f5ad8194e3f88be10770992ffa Mon Sep 17 00:00:00 2001 From: jdebacker Date: Thu, 16 May 2024 14:56:35 -0400 Subject: [PATCH 2/6] similar updates to CP example --- examples/run_og_usa_current_policy_baseline.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/examples/run_og_usa_current_policy_baseline.py b/examples/run_og_usa_current_policy_baseline.py index 00c26180..6aa0ac4a 100644 --- a/examples/run_og_usa_current_policy_baseline.py +++ b/examples/run_og_usa_current_policy_baseline.py @@ -47,6 +47,7 @@ def main(): ) ) p.tax_func_type = "GS" + p.age_specific = False # get current policy JSON file base_url = ( "github://PSLmodels:Tax-Calculator@master/taxcalc/" @@ -111,6 +112,7 @@ def main(): ) ) p2.tax_func_type = "GS" + p.age_specific = False # Use calibration class to estimate reform tax functions from # Tax-Calculator, specifying reform for Tax-Calculator in iit_reform c2 = Calibration( @@ -168,14 +170,23 @@ def main(): # create plots of output op.plot_all( - base_dir, reform_dir, os.path.join(CUR_DIR, "OG-USA_example_plots") + base_dir, reform_dir, os.path.join(CUR_DIR, "OG-USA_current_policy_example_plots_tables") ) + # Create CSV file with output + ot.tp_output_dump_table( + base_params, + base_tpi, + reform_params, + reform_tpi, + table_format="csv", + path=os.path.join(CUR_DIR, "OG-USA_example_plots_tables", "macro_time_series_output.csv") + ) print("Percentage changes in aggregates:", ans) # save percentage change output to csv file - ans.to_csv("ogusa_example_output.csv") + ans.to_csv(os.path.join(CUR_DIR, "OG-USA_current_policy_example_plots_tables", "ogusa_example_output.csv")) if __name__ == "__main__": # execute only if run as a script - main() + main() \ No newline at end of file From 0b91c8a835c22241e65e4d5e8c6a37997046535e Mon Sep 17 00:00:00 2001 From: jdebacker Date: Thu, 16 May 2024 14:57:20 -0400 Subject: [PATCH 3/6] format --- examples/run_og_usa.py | 28 ++++++++++------ .../run_og_usa_current_policy_baseline.py | 32 +++++++++++++------ 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/examples/run_og_usa.py b/examples/run_og_usa.py index b3eb94e7..7b94ef56 100644 --- a/examples/run_og_usa.py +++ b/examples/run_og_usa.py @@ -154,21 +154,31 @@ def main(): # create plots of output op.plot_all( - base_dir, reform_dir, os.path.join(CUR_DIR, "OG-USA_example_plots_tables") + base_dir, + reform_dir, + os.path.join(CUR_DIR, "OG-USA_example_plots_tables"), ) # Create CSV file with output ot.tp_output_dump_table( - base_params, - base_tpi, - reform_params, - reform_tpi, - table_format="csv", - path=os.path.join(CUR_DIR, "OG-USA_example_plots_tables", "macro_time_series_output.csv") - ) + base_params, + base_tpi, + reform_params, + reform_tpi, + table_format="csv", + path=os.path.join( + CUR_DIR, + "OG-USA_example_plots_tables", + "macro_time_series_output.csv", + ), + ) print("Percentage changes in aggregates:", ans) # save percentage change output to csv file - ans.to_csv(os.path.join(CUR_DIR, "OG-USA_example_plots_tables", "ogusa_example_output.csv")) + ans.to_csv( + os.path.join( + CUR_DIR, "OG-USA_example_plots_tables", "ogusa_example_output.csv" + ) + ) if __name__ == "__main__": diff --git a/examples/run_og_usa_current_policy_baseline.py b/examples/run_og_usa_current_policy_baseline.py index 6aa0ac4a..b3df53fe 100644 --- a/examples/run_og_usa_current_policy_baseline.py +++ b/examples/run_og_usa_current_policy_baseline.py @@ -170,23 +170,35 @@ def main(): # create plots of output op.plot_all( - base_dir, reform_dir, os.path.join(CUR_DIR, "OG-USA_current_policy_example_plots_tables") + base_dir, + reform_dir, + os.path.join(CUR_DIR, "OG-USA_current_policy_example_plots_tables"), ) # Create CSV file with output ot.tp_output_dump_table( - base_params, - base_tpi, - reform_params, - reform_tpi, - table_format="csv", - path=os.path.join(CUR_DIR, "OG-USA_example_plots_tables", "macro_time_series_output.csv") - ) + base_params, + base_tpi, + reform_params, + reform_tpi, + table_format="csv", + path=os.path.join( + CUR_DIR, + "OG-USA_example_plots_tables", + "macro_time_series_output.csv", + ), + ) print("Percentage changes in aggregates:", ans) # save percentage change output to csv file - ans.to_csv(os.path.join(CUR_DIR, "OG-USA_current_policy_example_plots_tables", "ogusa_example_output.csv")) + ans.to_csv( + os.path.join( + CUR_DIR, + "OG-USA_current_policy_example_plots_tables", + "ogusa_example_output.csv", + ) + ) if __name__ == "__main__": # execute only if run as a script - main() \ No newline at end of file + main() From 638322481fed815d6b40286ac2a40785efade11b Mon Sep 17 00:00:00 2001 From: jdebacker Date: Thu, 16 May 2024 14:58:48 -0400 Subject: [PATCH 4/6] only upload codecov from ogusa repo --- .github/workflows/build_and_test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index 6730c157..813ba02c 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -54,7 +54,7 @@ jobs: run: | python -m pytest -m "not local" --cov=./ --cov-report=xml - name: Upload coverage to Codecov - if: matrix.os == 'ubuntu-latest' + if: matrix.os == 'ubuntu-latest' && contains(github.repository, 'PSLmodels/OG-USA') uses: codecov/codecov-action@v4 with: files: ./coverage.xml From 78b5e750d8366bd542810f5dcba70a6ac48c640c Mon Sep 17 00:00:00 2001 From: jdebacker Date: Thu, 16 May 2024 17:43:02 -0400 Subject: [PATCH 5/6] style the plots --- examples/run_og_usa.py | 200 +++++++++--------- .../run_og_usa_current_policy_baseline.py | 9 + 2 files changed, 113 insertions(+), 96 deletions(-) diff --git a/examples/run_og_usa.py b/examples/run_og_usa.py index 7b94ef56..4f8bee97 100644 --- a/examples/run_og_usa.py +++ b/examples/run_og_usa.py @@ -4,6 +4,7 @@ import json import time from taxcalc import Calculator +import matplotlib.pyplot as plt from ogusa.calibrate import Calibration from ogcore.parameters import Specifications from ogcore import output_tables as ot @@ -11,6 +12,13 @@ from ogcore.execute import runner from ogcore.utils import safe_read_pickle +# Use a custom matplotlib style file for plots +style_file_url = ( + "https://raw.githubusercontent.com/PSLmodels/OG-Core/" + + "master/ogcore/OGcorePlots.mplstyle" +) +plt.style.use(style_file_url) + def main(): # Define parameters to use for multiprocessing @@ -29,104 +37,104 @@ def main(): ------------------------------------------------------------------------ """ # Set up baseline parameterization - p = Specifications( - baseline=True, - num_workers=num_workers, - baseline_dir=base_dir, - output_base=base_dir, - ) - # Update parameters for baseline from default json file - p.update_specifications( - json.load( - open( - os.path.join( - CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" - ) - ) - ) - ) - p.tax_func_type = "GS" - p.age_specific = False - c = Calibration(p, estimate_tax_functions=True, client=client) - # close and delete client bc cache is too large - client.close() - del client - client = Client(n_workers=num_workers, threads_per_worker=1) - d = c.get_dict() - # # additional parameters to change - updated_params = { - "etr_params": d["etr_params"], - "mtrx_params": d["mtrx_params"], - "mtry_params": d["mtry_params"], - "mean_income_data": d["mean_income_data"], - "frac_tax_payroll": d["frac_tax_payroll"], - } - p.update_specifications(updated_params) - # Run model - start_time = time.time() - runner(p, time_path=True, client=client) - print("run time = ", time.time() - start_time) + # p = Specifications( + # baseline=True, + # num_workers=num_workers, + # baseline_dir=base_dir, + # output_base=base_dir, + # ) + # # Update parameters for baseline from default json file + # p.update_specifications( + # json.load( + # open( + # os.path.join( + # CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" + # ) + # ) + # ) + # ) + # p.tax_func_type = "GS" + # p.age_specific = False + # c = Calibration(p, estimate_tax_functions=True, client=client) + # # close and delete client bc cache is too large + # client.close() + # del client + # client = Client(n_workers=num_workers, threads_per_worker=1) + # d = c.get_dict() + # # # additional parameters to change + # updated_params = { + # "etr_params": d["etr_params"], + # "mtrx_params": d["mtrx_params"], + # "mtry_params": d["mtry_params"], + # "mean_income_data": d["mean_income_data"], + # "frac_tax_payroll": d["frac_tax_payroll"], + # } + # p.update_specifications(updated_params) + # # Run model + # start_time = time.time() + # runner(p, time_path=True, client=client) + # print("run time = ", time.time() - start_time) - """ - ------------------------------------------------------------------------ - Run reform policy - ------------------------------------------------------------------------ - """ - # Grab a reform JSON file already in Tax-Calculator - # In this example the 'reform' is a change to 2017 law (the - # baseline policy is tax law in 2018) - reform_url = ( - "github://PSLmodels:examples@main/psl_examples/" - + "taxcalc/2017_law.json" - ) - ref = Calculator.read_json_param_objects(reform_url, None) - iit_reform = ref["policy"] + # """ + # ------------------------------------------------------------------------ + # Run reform policy + # ------------------------------------------------------------------------ + # """ + # # Grab a reform JSON file already in Tax-Calculator + # # In this example the 'reform' is a change to 2017 law (the + # # baseline policy is tax law in 2018) + # reform_url = ( + # "github://PSLmodels:examples@main/psl_examples/" + # + "taxcalc/2017_law.json" + # ) + # ref = Calculator.read_json_param_objects(reform_url, None) + # iit_reform = ref["policy"] - # create new Specifications object for reform simulation - p2 = Specifications( - baseline=False, - num_workers=num_workers, - baseline_dir=base_dir, - output_base=reform_dir, - ) - # Update parameters for baseline from default json file - p2.update_specifications( - json.load( - open( - os.path.join( - CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" - ) - ) - ) - ) - p2.tax_func_type = "GS" - p2.age_specific = False - # Use calibration class to estimate reform tax functions from - # Tax-Calculator, specifying reform for Tax-Calculator in iit_reform - c2 = Calibration( - p2, iit_reform=iit_reform, estimate_tax_functions=True, client=client - ) - # close and delete client bc cache is too large - client.close() - del client - client = Client(n_workers=num_workers, threads_per_worker=1) - # update tax function parameters in Specifications Object - d = c2.get_dict() - # # additional parameters to change - updated_params = { - "cit_rate": [[0.35]], - "etr_params": d["etr_params"], - "mtrx_params": d["mtrx_params"], - "mtry_params": d["mtry_params"], - "mean_income_data": d["mean_income_data"], - "frac_tax_payroll": d["frac_tax_payroll"], - } - p2.update_specifications(updated_params) - # Run model - start_time = time.time() - runner(p2, time_path=True, client=client) - print("run time = ", time.time() - start_time) - client.close() + # # create new Specifications object for reform simulation + # p2 = Specifications( + # baseline=False, + # num_workers=num_workers, + # baseline_dir=base_dir, + # output_base=reform_dir, + # ) + # # Update parameters for baseline from default json file + # p2.update_specifications( + # json.load( + # open( + # os.path.join( + # CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" + # ) + # ) + # ) + # ) + # p2.tax_func_type = "GS" + # p2.age_specific = False + # # Use calibration class to estimate reform tax functions from + # # Tax-Calculator, specifying reform for Tax-Calculator in iit_reform + # c2 = Calibration( + # p2, iit_reform=iit_reform, estimate_tax_functions=True, client=client + # ) + # # close and delete client bc cache is too large + # client.close() + # del client + # client = Client(n_workers=num_workers, threads_per_worker=1) + # # update tax function parameters in Specifications Object + # d = c2.get_dict() + # # # additional parameters to change + # updated_params = { + # "cit_rate": [[0.35]], + # "etr_params": d["etr_params"], + # "mtrx_params": d["mtrx_params"], + # "mtry_params": d["mtry_params"], + # "mean_income_data": d["mean_income_data"], + # "frac_tax_payroll": d["frac_tax_payroll"], + # } + # p2.update_specifications(updated_params) + # # Run model + # start_time = time.time() + # runner(p2, time_path=True, client=client) + # print("run time = ", time.time() - start_time) + # client.close() """ ------------------------------------------------------------------------ diff --git a/examples/run_og_usa_current_policy_baseline.py b/examples/run_og_usa_current_policy_baseline.py index b3df53fe..eda391d5 100644 --- a/examples/run_og_usa_current_policy_baseline.py +++ b/examples/run_og_usa_current_policy_baseline.py @@ -5,6 +5,7 @@ import json import time from taxcalc import Calculator +import matplotlib.pyplot as plt from ogusa.calibrate import Calibration from ogcore.parameters import Specifications from ogcore import output_tables as ot @@ -13,6 +14,14 @@ from ogcore.utils import safe_read_pickle +# Use a custom matplotlib style file for plots +style_file_url = ( + "https://raw.githubusercontent.com/PSLmodels/OG-Core/" + + "master/ogcore/OGcorePlots.mplstyle" +) +plt.style.use(style_file_url) + + def main(): # Define parameters to use for multiprocessing num_workers = min(multiprocessing.cpu_count(), 7) From 13841dd33d770716fa3983a1925ed2b9950bbe3f Mon Sep 17 00:00:00 2001 From: jdebacker Date: Thu, 16 May 2024 17:54:39 -0400 Subject: [PATCH 6/6] undo comment --- examples/run_og_usa.py | 192 ++++++++++++++++++++--------------------- 1 file changed, 96 insertions(+), 96 deletions(-) diff --git a/examples/run_og_usa.py b/examples/run_og_usa.py index 4f8bee97..1f0e501d 100644 --- a/examples/run_og_usa.py +++ b/examples/run_og_usa.py @@ -37,104 +37,104 @@ def main(): ------------------------------------------------------------------------ """ # Set up baseline parameterization - # p = Specifications( - # baseline=True, - # num_workers=num_workers, - # baseline_dir=base_dir, - # output_base=base_dir, - # ) - # # Update parameters for baseline from default json file - # p.update_specifications( - # json.load( - # open( - # os.path.join( - # CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" - # ) - # ) - # ) - # ) - # p.tax_func_type = "GS" - # p.age_specific = False - # c = Calibration(p, estimate_tax_functions=True, client=client) - # # close and delete client bc cache is too large - # client.close() - # del client - # client = Client(n_workers=num_workers, threads_per_worker=1) - # d = c.get_dict() - # # # additional parameters to change - # updated_params = { - # "etr_params": d["etr_params"], - # "mtrx_params": d["mtrx_params"], - # "mtry_params": d["mtry_params"], - # "mean_income_data": d["mean_income_data"], - # "frac_tax_payroll": d["frac_tax_payroll"], - # } - # p.update_specifications(updated_params) - # # Run model - # start_time = time.time() - # runner(p, time_path=True, client=client) - # print("run time = ", time.time() - start_time) + p = Specifications( + baseline=True, + num_workers=num_workers, + baseline_dir=base_dir, + output_base=base_dir, + ) + # Update parameters for baseline from default json file + p.update_specifications( + json.load( + open( + os.path.join( + CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" + ) + ) + ) + ) + p.tax_func_type = "GS" + p.age_specific = False + c = Calibration(p, estimate_tax_functions=True, client=client) + # close and delete client bc cache is too large + client.close() + del client + client = Client(n_workers=num_workers, threads_per_worker=1) + d = c.get_dict() + # # additional parameters to change + updated_params = { + "etr_params": d["etr_params"], + "mtrx_params": d["mtrx_params"], + "mtry_params": d["mtry_params"], + "mean_income_data": d["mean_income_data"], + "frac_tax_payroll": d["frac_tax_payroll"], + } + p.update_specifications(updated_params) + # Run model + start_time = time.time() + runner(p, time_path=True, client=client) + print("run time = ", time.time() - start_time) - # """ - # ------------------------------------------------------------------------ - # Run reform policy - # ------------------------------------------------------------------------ - # """ - # # Grab a reform JSON file already in Tax-Calculator - # # In this example the 'reform' is a change to 2017 law (the - # # baseline policy is tax law in 2018) - # reform_url = ( - # "github://PSLmodels:examples@main/psl_examples/" - # + "taxcalc/2017_law.json" - # ) - # ref = Calculator.read_json_param_objects(reform_url, None) - # iit_reform = ref["policy"] + """ + ------------------------------------------------------------------------ + Run reform policy + ------------------------------------------------------------------------ + """ + # Grab a reform JSON file already in Tax-Calculator + # In this example the 'reform' is a change to 2017 law (the + # baseline policy is tax law in 2018) + reform_url = ( + "github://PSLmodels:examples@main/psl_examples/" + + "taxcalc/2017_law.json" + ) + ref = Calculator.read_json_param_objects(reform_url, None) + iit_reform = ref["policy"] - # # create new Specifications object for reform simulation - # p2 = Specifications( - # baseline=False, - # num_workers=num_workers, - # baseline_dir=base_dir, - # output_base=reform_dir, - # ) - # # Update parameters for baseline from default json file - # p2.update_specifications( - # json.load( - # open( - # os.path.join( - # CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" - # ) - # ) - # ) - # ) - # p2.tax_func_type = "GS" - # p2.age_specific = False - # # Use calibration class to estimate reform tax functions from - # # Tax-Calculator, specifying reform for Tax-Calculator in iit_reform - # c2 = Calibration( - # p2, iit_reform=iit_reform, estimate_tax_functions=True, client=client - # ) - # # close and delete client bc cache is too large - # client.close() - # del client - # client = Client(n_workers=num_workers, threads_per_worker=1) - # # update tax function parameters in Specifications Object - # d = c2.get_dict() - # # # additional parameters to change - # updated_params = { - # "cit_rate": [[0.35]], - # "etr_params": d["etr_params"], - # "mtrx_params": d["mtrx_params"], - # "mtry_params": d["mtry_params"], - # "mean_income_data": d["mean_income_data"], - # "frac_tax_payroll": d["frac_tax_payroll"], - # } - # p2.update_specifications(updated_params) - # # Run model - # start_time = time.time() - # runner(p2, time_path=True, client=client) - # print("run time = ", time.time() - start_time) - # client.close() + # create new Specifications object for reform simulation + p2 = Specifications( + baseline=False, + num_workers=num_workers, + baseline_dir=base_dir, + output_base=reform_dir, + ) + # Update parameters for baseline from default json file + p2.update_specifications( + json.load( + open( + os.path.join( + CUR_DIR, "..", "ogusa", "ogusa_default_parameters.json" + ) + ) + ) + ) + p2.tax_func_type = "GS" + p2.age_specific = False + # Use calibration class to estimate reform tax functions from + # Tax-Calculator, specifying reform for Tax-Calculator in iit_reform + c2 = Calibration( + p2, iit_reform=iit_reform, estimate_tax_functions=True, client=client + ) + # close and delete client bc cache is too large + client.close() + del client + client = Client(n_workers=num_workers, threads_per_worker=1) + # update tax function parameters in Specifications Object + d = c2.get_dict() + # # additional parameters to change + updated_params = { + "cit_rate": [[0.35]], + "etr_params": d["etr_params"], + "mtrx_params": d["mtrx_params"], + "mtry_params": d["mtry_params"], + "mean_income_data": d["mean_income_data"], + "frac_tax_payroll": d["frac_tax_payroll"], + } + p2.update_specifications(updated_params) + # Run model + start_time = time.time() + runner(p2, time_path=True, client=client) + print("run time = ", time.time() - start_time) + client.close() """ ------------------------------------------------------------------------