def test_translate_json_reform_suffixes_eic(): # test read_json_param_files(...) using EIC-indexed parameter suffixes json1 = """{"policy": { "_II_em": {"2020": [20000], "2015": [15000]}, "_EITC_c_0kids": {"2018": [510], "2019": [510]}, "_EITC_c_1kid": {"2019": [3400], "2018": [3400]}, "_EITC_c_2kids": {"2018": [5616], "2019": [5616]}, "_EITC_c_3+kids": {"2019": [6318], "2018": [6318]} }}""" pdict1 = Calculator.read_json_param_files(reform_filename=json1, assump_filename=None, arrays_not_lists=True) rdict1 = pdict1['policy'] json2 = """{"policy": { "_EITC_c": {"2019": [[510, 3400, 5616, 6318]], "2018": [[510, 3400, 5616, 6318]]}, "_II_em": {"2020": [20000], "2015": [15000]} }}""" pdict2 = Calculator.read_json_param_files(reform_filename=json2, assump_filename=None, arrays_not_lists=True) rdict2 = pdict2['policy'] assert len(rdict2) == len(rdict1) for year in rdict2.keys(): if '_II_em' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_II_em'], rdict2[year]['_II_em'], atol=0.01, rtol=0.0) if '_EITC_c' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_EITC_c'], rdict2[year]['_EITC_c'], atol=0.01, rtol=0.0)
def test_translate_json_reform_suffixes_mars_non_indexed(): # test read_json_param_files(...) using MARS-indexed parameter suffixes json1 = """{"policy": { "_II_em": {"2020": [20000], "2015": [15000]}, "_AMEDT_ec_joint": {"2018": [400000], "2016": [300000]}, "_AMEDT_ec_separate": {"2017": [150000], "2019": [200000]} }}""" pdict1 = Calculator.read_json_param_files(reform_filename=json1, assump_filename=None, arrays_not_lists=True) rdict1 = pdict1['policy'] json2 = """{"policy": { "_AMEDT_ec": {"2016": [[200000, 300000, 125000, 200000, 200000]], "2017": [[200000, 300000, 150000, 200000, 200000]], "2018": [[200000, 400000, 150000, 200000, 200000]], "2019": [[200000, 400000, 200000, 200000, 200000]]}, "_II_em": {"2015": [15000], "2020": [20000]} }}""" pdict2 = Calculator.read_json_param_files(reform_filename=json2, assump_filename=None, arrays_not_lists=True) rdict2 = pdict2['policy'] assert len(rdict2) == len(rdict1) for year in rdict2.keys(): if '_II_em' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_II_em'], rdict2[year]['_II_em'], atol=0.01, rtol=0.0) if '_AMEDT_ec' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_AMEDT_ec'], rdict2[year]['_AMEDT_ec'], atol=0.01, rtol=0.0)
def test_read_json_reform_file_two_ways(reform_file, assump_file): """ Test when using file name and file contents in read_json_param_files """ pd1 = Calculator.read_json_param_files(reform_file.name, assump_file.name, arrays_not_lists=False) pd2 = Calculator.read_json_param_files(REFORM_CONTENTS, ASSUMP_CONTENTS, arrays_not_lists=False) assert pd1 == pd2
def test_read_bad_json_assump_file(bad1assumpfile, bad2assumpfile, bad3assumpfile): with pytest.raises(ValueError): Calculator.read_json_param_files(None, bad1assumpfile.name) with pytest.raises(ValueError): Calculator.read_json_param_files(None, bad2assumpfile.name) with pytest.raises(ValueError): Calculator.read_json_param_files(None, bad3assumpfile.name) with pytest.raises(ValueError): Calculator.read_json_param_files(None, 'unknown_file_name') with pytest.raises(ValueError): Calculator.read_json_param_files(None, list())
def test_read_json_param_files_with_suffixes_and_errors(): # test interaction of policy parameter suffixes and reform errors # (fails without 0.10.2 bug fix as reported by Hank Doupe in TB PR#641) reform = { u'policy': { u'_II_brk4_separate': {u'2017': [5000.0]}, u'_STD_separate': {u'2017': [8000.0]}, u'_STD_single': {u'2018': [1000.0]}, u'_II_brk2_headhousehold': {u'2017': [1000.0]}, u'_II_brk4_single': {u'2017': [500.0]}, u'_STD_joint': {u'2017': [10000.0], u'2020': [150.0]}, u'_II_brk2_separate': {u'2017': [1000.0]}, u'_II_brk2_single': {u'2017': [1000.0]}, u'_II_brk2_joint': {u'2017': [1000.0]}, u'_FICA_ss_trt': {u'2017': [-1.0], u'2019': [0.1]}, u'_II_brk4_headhousehold': {u'2017': [500.0]}, u'_STD_headhousehold': {u'2017': [10000.0], u'2020': [150.0]}, u'_II_brk4_joint': {u'2017': [500.0]}, u'_ID_BenefitSurtax_Switch_medical': {u'2017': [True]} } } json_reform = json.dumps(reform) params = Calculator.read_json_param_files(json_reform, None) assert isinstance(params, dict) pol = Policy() pol.implement_reform(params['policy']) assert len(pol.reform_errors) > 0 assert len(pol.reform_warnings) > 0
def test_read_bad_json_reform_file(bad1reformfile, bad2reformfile, bad3reformfile): with pytest.raises(ValueError): Calculator.read_json_param_files(bad1reformfile.name, None) with pytest.raises(ValueError): Calculator.read_json_param_files(bad2reformfile.name, None) with pytest.raises(ValueError): Calculator.read_json_param_files(bad3reformfile.name, None) with pytest.raises(ValueError): Calculator.read_json_param_files(list(), None)
def __init__(self, input_filename, reform, exact_calculations, emulate_taxsim_2441_logic, output_records): """ SimpleTaxIO class constructor. """ # pylint: disable=too-many-arguments # check that input_filename is a string if not isinstance(input_filename, six.string_types): msg = 'SimpleTaxIO.ctor input_filename is not a string' raise ValueError(msg) # construct output_filename and delete old output file if it exists # ... construct reform extension to output_filename if reform is None: ref = '' self._using_reform_file = True else: # if reform is not None if isinstance(reform, six.string_types): if reform.endswith('.json'): ref = '-{}'.format(reform[:-5]) else: ref = '-{}'.format(reform) self._using_reform_file = True elif isinstance(reform, dict): ref = '' self._using_reform_file = False else: msg = 'SimpleTaxIO.ctor reform is neither None, str, nor dict' raise ValueError(msg) # ... construct whole output_filename self._using_input_file = True self._output_filename = '{}.out-simtax{}'.format(input_filename, ref) if os.path.isfile(self._output_filename): os.remove(self._output_filename) # check for existence of file named input_filename if not os.path.isfile(input_filename): msg = 'INPUT file named {} could not be found' raise ValueError(msg.format(input_filename)) # read input file contents into self._input dictionary self._read_input(input_filename) self.policy = Policy() # implement reform if reform is specified if reform: if self._using_reform_file: param_dict = Calculator.read_json_param_files(reform, None) r_pol = param_dict['policy'] else: r_pol = reform self.policy.implement_reform(r_pol) # validate input variable values self._validate_input() self.calc = self._calc_object(exact_calculations, emulate_taxsim_2441_logic, output_records)
def test_calculate_baseline_and_reform_error(puf_1991_path, reform_file, assump_file): usermods = Calculator.read_json_param_files(reform_file.name, assump_file.name) usermods['behavior'] = {2016: {'_BE_sub': [0.20]}} usermods['growdiff_response'] = {2020: {'_ABOOK': [0.01]}} usermods['gdp_elasticity'] = dict() tax_data = pd.read_csv(puf_1991_path) with pytest.raises(ValueError): calculate_baseline_and_reform(2, 2015, tax_data, usermods)
def test_translate_json_reform_suffixes_idedtype(): # test read_json_param_files(...) using idedtype-indexed parameter suffixes json1 = """{"policy": { "_ID_BenefitCap_rt": {"2019": [0.2]}, "_ID_BenefitCap_Switch_medical": {"2019": [false]}, "_ID_BenefitCap_Switch_casualty": {"2019": [false]}, "_ID_BenefitCap_Switch_misc": {"2019": [false]}, "_ID_BenefitCap_Switch_interest": {"2019": [false]}, "_ID_BenefitCap_Switch_charity": {"2019": [false]}, "_II_em": {"2020": [20000], "2015": [15000]} }}""" pdict1 = Calculator.read_json_param_files(reform_filename=json1, assump_filename=None, arrays_not_lists=True) rdict1 = pdict1['policy'] json2 = """{"policy": { "_II_em": {"2020": [20000], "2015": [15000]}, "_ID_BenefitCap_Switch": { "2019": [[false, true, true, false, false, false, false]] }, "_ID_BenefitCap_rt": {"2019": [0.2]} }}""" pdict2 = Calculator.read_json_param_files(reform_filename=json2, assump_filename=None, arrays_not_lists=True) rdict2 = pdict2['policy'] assert len(rdict2) == len(rdict1) for year in rdict2.keys(): if '_II_em' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_II_em'], rdict2[year]['_II_em'], atol=0.01, rtol=0.0) if '_ID_BenefitCap_rt' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_ID_BenefitCap_rt'], rdict2[year]['_ID_BenefitCap_rt'], atol=0.01, rtol=0.0) if '_ID_BenefitCap_Switch' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_ID_BenefitCap_Switch'], rdict2[year]['_ID_BenefitCap_Switch'], atol=0.01, rtol=0.0)
def test_translate_json_reform_suffixes_mars_indexed(): # test read_json_param_files(...) using MARS-indexed parameter suffixes json1 = """{"policy": { "_II_em": {"2020": [20000], "2015": [15000]}, "_STD_single": {"2018": [18000], "2016": [16000]}, "_STD_widow": {"2017": [17000], "2019": [19000]} }}""" assump_json = """{ "consumption": {}, "behavior": {}, "growdiff_baseline": { "_ACPIU": {"2013": [0.01]}, "_AWAGE": {"2013": [0.01]}}, "growdiff_response": {} }""" pdict1 = Calculator.read_json_param_files(reform_filename=json1, assump_filename=assump_json, arrays_not_lists=True) rdict1 = pdict1['policy'] json2 = """{"policy": { "_STD": {"2016": [[16000.00, 12600.00, 6300.00, 9300.00, 12600.00]], "2017": [[16524.80, 13013.28, 6506.64, 9605.04, 17000.00]], "2018": [[18000.00, 13432.31, 6716.15, 9914.32, 17547.40]], "2019": [[18592.20, 13874.23, 6937.11, 10240.50, 19000.00]]}, "_II_em": {"2020": [20000], "2015": [15000]} }}""" pdict2 = Calculator.read_json_param_files(reform_filename=json2, assump_filename=assump_json, arrays_not_lists=True) rdict2 = pdict2['policy'] assert len(rdict2) == len(rdict1) for year in rdict2.keys(): if '_II_em' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_II_em'], rdict2[year]['_II_em'], atol=0.01, rtol=0.0) if '_STD' in rdict2[year].keys(): assert np.allclose(rdict1[year]['_STD'], rdict2[year]['_STD'], atol=0.01, rtol=0.0)
def test_check_user_mods(puf_1991_path, reform_file, assump_file): usermods = list() with pytest.raises(ValueError): check_user_mods(usermods) usermods = Calculator.read_json_param_files(reform_file.name, assump_file.name) with pytest.raises(ValueError): check_user_mods(usermods) usermods['gdp_elasticity'] = dict() check_user_mods(usermods) usermods['unknown_key'] = dict() with pytest.raises(ValueError): check_user_mods(usermods)
def test_run_gdp_elast_model(puf_1991_path, rjson, reform_file, assump_file): usermods = Calculator.read_json_param_files(reform_file.name, assump_file.name) usermods['gdp_elasticity'] = {'value': 0.36} fyr = 2016 nyrs = 3 tax_data = pd.read_csv(puf_1991_path) ans = dropq.run_gdp_elast_model(tax_data, start_year=fyr, user_mods=usermods, return_json=rjson, num_years=nyrs) assert len(ans) == (nyrs - 1) # number of annual calculations done
def test_run_nth_year_model(puf_1991_path, reform_file, assump_file): usermods = Calculator.read_json_param_files(reform_file.name, assump_file.name) usermods['gdp_elasticity'] = dict() tax_data = pd.read_csv(puf_1991_path) year_n = 2 start_year = 2016 non_json_results = dropq.run_nth_year_model(year_n, start_year, tax_data, usermods, return_json=False) assert len(non_json_results) == 13
def test_calc_all(reform_file, rawinputfile): cyr = 2016 policy = Policy() param_dict = Calculator.read_json_param_files(reform_file.name, None) policy.implement_reform(param_dict['policy']) policy.set_year(cyr) nonpuf = Records(data=rawinputfile.name, gfactors=None, weights=None, start_year=cyr) assert nonpuf.dim == RAWINPUTFILE_FUNITS calc = Calculator(policy=policy, records=nonpuf, sync_years=False) # keeps raw data unchanged assert calc.current_year == cyr calc.calc_all()
def test_run_model_with_usermods_from_files(puf_1991_path, reform_file, assump_file): usermods = Calculator.read_json_param_files(reform_file.name, assump_file.name) usermods['gdp_elasticity'] = dict() # for this usermods the computed seed = 4109528928 fyr = 2016 tax_data = pd.read_csv(puf_1991_path) (_, _, _, _, _, _, _, _, _, _, fiscal_tots) = dropq.run_model(tax_data, start_year=fyr, user_mods=usermods, return_json=True, num_years=3) assert fiscal_tots is not None
def test_read_json_reform_file_and_implement_reform(reform_file, assump_file, set_year): """ Test reading and translation of reform file into a reform dictionary that is then used to call implement_reform method and Calculate.calc_all() NOTE: implement_reform called when policy.current_year == policy.start_year """ policy = Policy() if set_year: policy.set_year(2015) param_dict = Calculator.read_json_param_files(reform_file.name, assump_file.name) policy.implement_reform(param_dict['policy']) syr = policy.start_year amt_brk1 = policy._AMT_brk1 assert amt_brk1[2015 - syr] == 200000 assert amt_brk1[2016 - syr] > 200000 assert amt_brk1[2017 - syr] == 300000 assert amt_brk1[2018 - syr] > 300000 ii_em = policy._II_em assert ii_em[2016 - syr] == 6000 assert ii_em[2017 - syr] == 6000 assert ii_em[2018 - syr] == 7500 assert ii_em[2019 - syr] > 7500 assert ii_em[2020 - syr] == 9000 assert ii_em[2021 - syr] > 9000 amt_em = policy._AMT_em assert amt_em[2016 - syr, 0] > amt_em[2015 - syr, 0] assert amt_em[2017 - syr, 0] > amt_em[2016 - syr, 0] assert amt_em[2018 - syr, 0] == amt_em[2017 - syr, 0] assert amt_em[2019 - syr, 0] == amt_em[2017 - syr, 0] assert amt_em[2020 - syr, 0] == amt_em[2017 - syr, 0] assert amt_em[2021 - syr, 0] > amt_em[2020 - syr, 0] assert amt_em[2022 - syr, 0] > amt_em[2021 - syr, 0] add4aged = policy._ID_Medical_frt_add4aged assert add4aged[2015 - syr] == -0.025 assert add4aged[2016 - syr] == -0.025 assert add4aged[2017 - syr] == 0.0 assert add4aged[2022 - syr] == 0.0