def io_variables(iotype, path, text):
    """
    Read variables for iotype ('read' for input or 'calc' for output)
    from path, integrate them into text, and return the integrated text.
    """
    with open(path) as vfile:
        json_text = vfile.read()
    variables = json_to_dict(json_text)
    assert isinstance(variables, dict)
    # construct variable text
    vtext = ''
    for vname in sorted(variables[iotype].keys()):
        vtext += var_text(vname, iotype, variables[iotype][vname])
    # integrate variable text into text
    old = '<!-- {}@variables -->'.format(iotype)
    text = text.replace(old, vtext)
    return text
def io_variables(iotype, path, text):
    """
    Read variables for iotype ('read' for input or 'calc' for output)
    from path, integrate them into text, and return the integrated text.
    """
    with open(path) as vfile:
        json_text = vfile.read()
    variables = json_to_dict(json_text)
    assert isinstance(variables, dict)
    # construct variable text
    vtext = ''
    for vname in sorted(variables[iotype].keys()):
        vtext += var_text(vname, iotype, variables[iotype][vname])
    # integrate variable text into text
    old = '<!-- {}@variables -->'.format(iotype)
    text = text.replace(old, vtext)
    return text
def assumption_params(ptype, path, text):
    """
    Read assumption parameters of ptype from path, integrate them into text,
    and return the integrated text.
    """
    with open(path) as pfile:
        json_text = pfile.read()
    params = json_to_dict(json_text)
    assert isinstance(params, OrderedDict)
    # construct parameter text for each param
    ptext = ''
    for pname in params:
        param = params[pname]
        ptext += assumption_param_text(pname, ptype, param)
    # integrate parameter text into text
    old = '<!-- {}@parameters -->'.format(ptype)
    text = text.replace(old, ptext)
    return text
def assumption_params(ptype, path, text):
    """
    Read assumption parameters of ptype from path, integrate them into text,
    and return the integrated text.
    """
    with open(path) as pfile:
        json_text = pfile.read()
    params = json_to_dict(json_text)
    assert isinstance(params, OrderedDict)
    # construct parameter text for each param
    ptext = ''
    for pname in params:
        param = params[pname]
        ptext += assumption_param_text(pname, ptype, param)
    # integrate parameter text into text
    old = '<!-- {}@parameters -->'.format(ptype)
    text = text.replace(old, ptext)
    return text
Exemple #5
0
def make_params(path, ptype):
    """ Make string with all parameter information.

    Args:
        path: Path to parameter file.
        ptype: Parameter type. One of 'policy', 'consumption', or 'growdiff'.

    Returns:
        Single string with all parameter information.
    """
    with open(path) as pfile:
        json_text = pfile.read()
    params = tc.json_to_dict(json_text)
    df = pd.DataFrame(params).transpose().drop('schema')
    # Extra metadata for years and values is available for policy parameters.
    if ptype == 'policy':
        df = df.join(pd.DataFrame(reformat_params()).transpose())
    # Add parameter text for policy, consumption, and growdiff parameter types.
    df['content'] = paramtextdf(df, ptype)
    # Only policy parameters have sections.
    if ptype == 'policy':
        df.section_1 = np.where(df.section_1 == '',
                                'Other Parameters (not in Tax-Brain webapp)',
                                df.section_1)
        section_1_order_index = dict(
            zip(SECTION_1_ORDER, range(len(SECTION_1_ORDER))))
        df['section_1_order'] = df.section_1.map(section_1_order_index)
        df.sort_values(['section_1_order', 'section_2'], inplace=True)
        # Add section titles when they change.
        df['new_section_1'] = ~df.section_1.eq(df.section_1.shift())
        df['new_section_2'] = (~df.section_2.eq(df.section_2.shift()) &
                               (df.section_2 > ''))
        df['section_1_content'] = np.where(df.new_section_1,
                                           '## ' + df.section_1 + '\n\n', '')
        df['section_2_content'] = np.where(df.new_section_2,
                                           '### ' + df.section_2 + '\n\n', '')
        # Concatenate section titles with content for each parameter.
        df.content = df.section_1_content + df.section_2_content + df.content
    # Return a single string.
    return '\n\n'.join(df.content)
def policy_params(path, text):
    """
    Read policy parameters from path, integrate them into text, and
    return the integrated text.
    """
    # pylint: disable=too-many-locals
    with open(path) as pfile:
        json_text = pfile.read()
    params = json_to_dict(json_text)
    assert isinstance(params, OrderedDict)
    # construct section dict containing sec1_sec2 titles
    concat_str = ' @ '
    section = OrderedDict()
    using_other_params_section = False
    for pname in params:
        param = params[pname]
        sec1_sec2 = '{}{}{}'.format(param['section_1'],
                                    concat_str,
                                    param['section_2'])
        if sec1_sec2 == concat_str:
            using_other_params_section = True
        elif sec1_sec2 not in section:
            section[sec1_sec2] = 0
    if using_other_params_section:
        section[concat_str] = 0
    # construct parameter text for each sec1_sec2 in section
    for sec1_sec2 in section:
        split_list = sec1_sec2.split(concat_str)
        sec1 = split_list[0]
        sec2 = split_list[1]
        ptext = ''
        for pname in params:
            param = params[pname]
            if sec1 == param['section_1'] and sec2 == param['section_2']:
                ptext += policy_param_text(pname, param)
        # integrate parameter text into text
        old = '<!-- {} -->'.format(sec1_sec2)
        text = text.replace(old, ptext)
    return text
def policy_params(path, text):
    """
    Read policy parameters from path, integrate them into text, and
    return the integrated text.
    """
    # pylint: disable=too-many-locals
    with open(path) as pfile:
        json_text = pfile.read()
    params = json_to_dict(json_text)
    assert isinstance(params, OrderedDict)
    # construct section dict containing sec1_sec2 titles
    concat_str = ' @ '
    section = OrderedDict()
    using_other_params_section = False
    for pname in params:
        param = params[pname]
        sec1_sec2 = '{}{}{}'.format(param['section_1'], concat_str,
                                    param['section_2'])
        if sec1_sec2 == concat_str:
            using_other_params_section = True
        elif sec1_sec2 not in section:
            section[sec1_sec2] = 0
    if using_other_params_section:
        section[concat_str] = 0
    # construct parameter text for each sec1_sec2 in section
    for sec1_sec2 in section:
        split_list = sec1_sec2.split(concat_str)
        sec1 = split_list[0]
        sec2 = split_list[1]
        ptext = ''
        for pname in params:
            param = params[pname]
            if sec1 == param['section_1'] and sec2 == param['section_2']:
                ptext += policy_param_text(pname, param)
        # integrate parameter text into text
        old = '<!-- {} -->'.format(sec1_sec2)
        text = text.replace(old, ptext)
    return text