<commit_before>import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import rcParams
import matplotlib.ticker as ticker
import projection_funcs as pf
from collections import namedtuple
import inspect


def spend_mult(sav_rate, k1=None, k2=None, ratio=None):
    '''Returns the multiplier to apply to baseline spend 
    to reflect a change in threshold (k1->k2), given the savings (drugs substitution)
    as a proportion r of the original costs

    k1 = (C1 - s)/dQ : k is cost/QALY, c is cost of drug, s is savings
    s = rC1, r=s/C1  : r is savings as proportion of initial costs
    k1 = (1-r)C1     : assume dQ is 1 as effects are all relative
    C1 = k1/(1-r)
    k2 = C2 -rC1; C2 = k2 + rC1 = k2 + rk1/(1-r)
    C2/C1 = (k2 + rk1/(1-r))*(k1/(1-r)) = (1-r)(k2/k1) + r
    '''
    
    r = sav_rate

    if ratio is None:
        if (k1 is None) or (k2 is None): 
            print('need a ratio or k1 and k2')
            return
        
        else: ratio = k2/k1

    return ((1-r)*(ratio)) + r


##_________________________________________________________________________##



def dump_to_xls(res_df, outfile, in_dict=None, shapes=None, log=False, _debug=False):
    '''Dump a results DataFrame to an xls, with option to make shapes from a  
     dict of scenarios or from a scenario alone, and/or passing shapes
    '''
    if _debug: print("\nIN FUNCTION:  ".ljust(20), inspect.stack()[0][3])
    if _debug: print("..called by:  ".ljust(20), inspect.stack()[1][3], end="\n\n")

    # initialise vars
    params_header = None
    shapes_body = None

    # first get the params header and shapes, depending on input
    # if a policy is passed
    params_header = make_params_table(in_dict, log=log, _debug=_debug)
    shapes_body = make_shapes1(in_dict, flat=True, multi_index=True)

    if _debug: print('\ngot header and shapes from in_dict:\n')
    if _debug: print("params_header\n", params_header, end="\n\n")
    if _debug: print("shapes_body\n", shapes_body.head(), end="\n")


    # if shapes are passed
    if shapes is not None:
        if _debug: print('preparing to overwrite shapes')
        shapes_body = shapes
        if _debug: print("new shapes_body\n", shapes_body.head(), end="\n")


    # assemble outputs
    shapes_out = params_header.append(shapes_body)
    main_out = params_header.append(res_df)
    annual_out = params_header.append(res_df.groupby(res_df.index.year).sum())

    # write out
    writer = pd.ExcelWriter(outfile)
    shapes_out.to_excel(writer, 'shapes')
    main_out.to_excel(writer, 'main')
    annual_out.to_excel(writer, 'annual')

    writer.save()

    if _debug: print("\LEAVING:  ", inspect.stack()[0][3])

<commit_msg>dump_to_xls now supports append<commit_after>import pandas as pd
import numpy as np
from openpyxl import load_workbook
from matplotlib import pyplot as plt
from matplotlib import rcParams
import matplotlib.ticker as ticker
import projection_funcs as pf
from collections import namedtuple
import inspect
import os


def spend_mult(sav_rate, k1=None, k2=None, ratio=None):
    '''Returns the multiplier to apply to baseline spend 
    to reflect a change in threshold (k1->k2), given the savings (drugs substitution)
    as a proportion r of the original costs

    k1 = (C1 - s)/dQ : k is cost/QALY, c is cost of drug, s is savings
    s = rC1, r=s/C1  : r is savings as proportion of initial costs
    k1 = (1-r)C1     : assume dQ is 1 as effects are all relative
    C1 = k1/(1-r)
    k2 = C2 -rC1; C2 = k2 + rC1 = k2 + rk1/(1-r)
    C2/C1 = (k2 + rk1/(1-r))*(k1/(1-r)) = (1-r)(k2/k1) + r
    '''
    
    r = sav_rate

    if ratio is None:
        if (k1 is None) or (k2 is None): 
            print('need a ratio or k1 and k2')
            return
        
        else: ratio = k2/k1

    return ((1-r)*(ratio)) + r


##_________________________________________________________________________##



def dump_to_xls(res_df, outfile, append=False, prefix="", in_dict=None, shapes=None, log=False, _debug=False):
    '''Dump a results DataFrame to an xls, with option to make shapes from a  
     dict of scenarios or from a scenario alone, and/or passing shapes
    '''
    if _debug: print("\nIN FUNCTION:  ".ljust(20), inspect.stack()[0][3])
    if _debug: print("..called by:  ".ljust(20), inspect.stack()[1][3], end="\n\n")

    # initialise vars
    params_header = None
    shapes_body = None

    # first get the params header and shapes, depending on input
    # if a policy is passed
    params_header = make_params_table(in_dict, log=log, _debug=_debug)
    shapes_body = make_shapes1(in_dict, flat=True, multi_index=True)

    if _debug: print('\ngot header and shapes from in_dict:\n')
    if _debug: print("params_header\n", params_header, end="\n\n")
    if _debug: print("shapes_body\n", shapes_body.head(), end="\n")


    # if shapes are passed
    if shapes is not None:
        if _debug: print('preparing to overwrite shapes')
        shapes_body = shapes
        if _debug: print("new shapes_body\n", shapes_body.head(), end="\n")


    # assemble outputs
    shapes_out = params_header.append(shapes_body)
    main_out = params_header.append(res_df)
    annual_out = params_header.append(res_df.groupby(res_df.index.year).sum())

    # write out
    if append:
        if os.path.isfile(outfile):
            book = load_workbook(outfile)
            writer = pd.ExcelWriter(outfile, engine="openpyxl")
            writer.book = book
        else: 
            print("could not find file to append to.  will make a new one")
            writer = pd.ExcelWriter(outfile)

    else: writer = pd.ExcelWriter(outfile)

    shapes_out.to_excel(writer, prefix + 'shapes')
    main_out.to_excel(writer, prefix + 'main')
    annual_out.to_excel(writer, prefix + 'annual')

    writer.save()

    if _debug: print("\LEAVING:  ", inspect.stack()[0][3])


{'fraction_non_alphanumeric': 0.11428086262523349, 'fraction_numerical': 0.017660044150110375, 'mean_word_length': 3.0761245674740483, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2173025', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1957, 'n_words': 605}