Skip to content

Commit

Permalink
Merge pull request #41 from hdoupe/checkbox-update
Browse files Browse the repository at this point in the history
Checkbox update
  • Loading branch information
Peter-Metz authored Sep 2, 2020
2 parents ea48bd3 + c121fd3 commit ee4c33f
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 10 deletions.
17 changes: 8 additions & 9 deletions cs-config/cs_config/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import inspect
from .outputs import credit_plot, rate_plot, liability_plot
from .constants import MetaParameters
from . import inputs
from bokeh.models import ColumnDataSource
from taxcrunch.cruncher import Cruncher, CruncherParams
from taxcrunch.multi_cruncher import Batch
Expand Down Expand Up @@ -53,13 +54,11 @@ def get_inputs(meta_params_dict):
policy_params.set_state(
year=metaparams.year.tolist())

filtered_pol_params = OrderedDict()
policy_params._schema["operators"].update(
{"label_to_extend": None, "uses_extend_func": False, "array_first": False}
)
for k, v in policy_params.dump().items():
if k =="schema" or v.get("section_1", False):
filtered_pol_params[k] = v
policy_params.array_first = False
# Hack to work smoothly with convert_policy_defaults since
# it expects a data_source attribute.
metaparams.data_source = "CPS"
filtered_pol_params = inputs.convert_policy_defaults(metaparams, policy_params)

keep = [
"mstat",
Expand Down Expand Up @@ -108,7 +107,7 @@ def validate_inputs(meta_params_dict, adjustment, errors_warnings):
params.adjust(adjustment["Tax Information"], raise_errors=False)
errors_warnings["Tax Information"]["errors"].update(params.errors)

policy_adj = fix_checkbox(adjustment["Policy"])
policy_adj = inputs.convert_policy_adjustment(adjustment["Policy"])

policy_params = Policy()
policy_params.adjust(policy_adj, raise_errors=False, ignore_warnings=True)
Expand All @@ -126,7 +125,7 @@ def run_model(meta_params_dict, adjustment):
params.adjust(adjustment["Tax Information"], raise_errors=False)
newvals = params.specification()

policy_mods = fix_checkbox(adjustment["Policy"])
policy_mods = inputs.convert_policy_adjustment(adjustment["Policy"])

crunch = Cruncher(inputs=newvals, custom_reform=policy_mods)

Expand Down
104 changes: 104 additions & 0 deletions cs-config/cs_config/inputs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
from typing import Union

from paramtools import Parameters


def convert_policy_defaults(meta_params: Parameters, policy_params: Parameters):
"""
Convert defaults for taxcalc's Policy class to work with C/S.
"""
policy_params.array_first = False
policy_params.uses_extend_func = False
init = policy_params.dump()
defaults = convert_sections(init)
defaults = convert_data_source(defaults, meta_params.data_source)
defaults = convert_indexed_to_checkbox(defaults)
return defaults


def convert_policy_adjustment(policy_adjustment: dict):
"""
Convert adjutments for taxcalc's Policy class to work with C/S.
"""
return convert_checkbox(policy_adjustment)


def convert_behavior_adjustment(adj):
"""
Convert a C/S behavioral adjustment to work with the Behavioral-Responses
package
"""
behavior = {}
if adj:
for param, value in adj.items():
behavior[param] = value[0]["value"]
return behavior


def convert_checkbox(policy_params):
"""
Replace param_checkbox with param-indexed.
"""
params = {}
# drop checkbox parameters.
for param, data in policy_params.items():
if param.endswith("checkbox"):
base_param = param.split("_checkbox")[0]
params[f"{base_param}-indexed"] = data
else:
params[param] = data

return params


def convert_data_source(defaults: dict, data_source: Union["PUF", "CPS"]):
"""
Handle parameters that are incompatible with the selected dataset.
"""
new_defaults = {}
for param, data in defaults.items():
if (
defaults.get("compatible_data") is not None
and not defaults["compatible_data"][data_source.lower()]
):
new_defaults[param] = dict(data, value=[])
else:
new_defaults[param] = data
return new_defaults


def convert_indexed_to_checkbox(defaults: dict):
"""
C/S expects there to be a checkbox attribute instead of
indexed in the defaults.
"""
new_defaults = {}
for param, data in defaults.items():

if param == "schema":
data["additional_members"]["checkbox"] = dict(
data["additional_members"]["indexed"]
)
new_defaults["schema"] = data

elif data["indexable"] and data.get("indexed", None) is True:
new_defaults[param] = dict(data, checkbox= True)

elif data["indexable"] and not data.get("indexed", None) is False:
new_defaults[param] = dict(data, checkbox=False)

else:
new_defaults[param] = data

return new_defaults


def convert_sections(defaults):
"""
Drop parameters that are missing section_1.
"""
filtered_pol_params = {}
for k, v in defaults.items():
if k == "schema" or v.get("section_1", False):
filtered_pol_params[k] = v
return filtered_pol_params
2 changes: 1 addition & 1 deletion cs-config/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

git clone https://github.com/PSLmodels/Tax-Cruncher
cd Tax-Cruncher
conda install PSLmodels::taxcalc PSLmodels::behresp conda-forge::paramtools "bokeh<2.0.0" ipython
conda install PSLmodels::taxcalc PSLmodels::behresp "conda-forge::paramtools>=0.15.1" "bokeh<2.0.0" ipython
pip install -e .

0 comments on commit ee4c33f

Please sign in to comment.