Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend parameter values along a specified label #58

Merged
merged 8 commits into from Jun 14, 2019

Conversation

Projects
None yet
1 participant
@hdoupe
Copy link
Collaborator

commented May 29, 2019

Partially resolves #56. Parameters can be extended along one label. This is helpful for filling out a parameter's values without having to specify redundant data in defaults.json. ParamTools uses label validation data to create a discrete list of values for each label. This is used to figure out which values of label_to_extend are missing and the direction in which the values need to be extended.

import paramtools

class Parameters(paramtools.Parameters):
    defaults = "converted.json"
    # extend parameters along "year" label
    label_to_extend = "year"
    # access parameter values as arrays by default
    array_first = True


params = Parameters()

Parameter values will be extended along the "year" label with these values:

params.label_grid["year"]

# [2013,
#  2014,
#  2015,
#  2016,
#  2017,
#  2018,
#  2019,
#  2020,
#  2021,
#  2022,
#  2023,
#  2024,
#  2025,
#  2026,
#  2027,
#  2028,
#  2029]

Check that EITC_c was extended:

params.EITC_c

# array([[ 487., 3250., 5372., 6044.],
#        [ 496., 3305., 5460., 6143.],
#        [ 503., 3359., 5548., 6242.],
#        [ 506., 3373., 5572., 6269.],
#        [ 510., 3400., 5616., 6318.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.],
#        [ 519., 3461., 5716., 6431.]])

Documentation for this feature will be added to this PR over the next couple days.

cc @jdebacker @martinholmer

@hdoupe hdoupe changed the title Extend Extend parameter values along a specified label May 29, 2019

@hdoupe

This comment has been minimized.

Copy link
Collaborator Author

commented May 29, 2019

Run this script to create the converted.json file:

from collections import defaultdict
import json


POLICY_SCHEMA = {
    "labels": {
        "year": {
            "type": "int",
            "validators": {
                "range": {"min": 2013, "max": 2028}
            }
        },
        "MARS": {
            "type": "str",
            "validators": {"choice": {"choices": ["single", "mjoint",
                                                  "mseparate", "headhh",
                                                  "widow"]}}
        },
        "idedtype": {
            "type": "str",
            "validators": {"choice": {"choices": ["med", "sltx", "retx", "cas",
                                                  "misc", "int", "char"]}}
        },
        "EIC": {
            "type": "str",
            "validators": {"choice": {"choices": ["0kids", "1kid",
                                                  "2kids", "3+kids"]}}
        },
    },
    "additional_members": {
        "section_1": {"type": "str"},
        "section_2": {"type": "str"},
        "start_year": {"type": "int"},
        "indexable": {"type": "bool"},
        "indexed": {"type": "bool"}
    }
}

def convert_defaults(pcl):

    type_map = {
        "real": "float",
        "boolean": "bool",
        "integer": "int",
        "string": "str",
    }

    new_pcl = defaultdict(dict)
    new_pcl["schema"] = POLICY_SCHEMA
    for param, item in pcl.items():
        values = []
        pol_val = item["value"]
        min_year = min(item["value_yrs"])
        if isinstance(pol_val[0], list):
            for year in range(len(pol_val)):
                for dim1 in range(len(pol_val[0])):
                    values.append({
                        "year": min_year + year,
                        item["vi_name"]: item["vi_vals"][dim1],
                        "value": pol_val[year][dim1],
                    })
        else:
            for year in range(len(pol_val)):
                values.append({
                    "year": min_year + year,
                    "value": pol_val[year],
                })

        new_pcl[param]['value'] = values
        new_pcl[param]['title'] = pcl[param]["long_name"]
        new_pcl[param]['type'] = type_map[pcl[param]["value_type"]]

        new_pcl[param]["validators"] = {"range": pcl[param]["valid_values"]}

        to_keep = list(POLICY_SCHEMA["additional_members"].keys()) + [
            "description", "notes",
        ]
        for k in to_keep:
            if k in pcl[param]:
                new_pcl[param][k] = pcl[param][k]

    return new_pcl


if __name__ == "__main__":
    with open("Tax-Calculator/taxcalc/policy_current_law.json") as f:
        converted = convert_defaults(json.loads(f.read()))
    
    with open("converted.json", "w") as f:
        f.write(json.dumps(converted, indent=4))

hdoupe added some commits Jun 13, 2019

@hdoupe hdoupe merged commit 3516f29 into master Jun 14, 2019

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@hdoupe hdoupe deleted the extend branch Jun 14, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.