Example #1
0
def SA_testing(values):
    
    # variables to alter and their values
    replace_var = ['TransmissionRateOnART', 'TransmissionRateOffART', 'HIVtestBgAcceptRate'] #
    replace_val = {}
    
    # multiply to attia
    attia_new = np.multiply(parameters['attia'], 1)
    replace_val['TransmissionRateOnART'] = np.multiply(np.reshape(attia_new, (8, 1)), np.ones((8, 6)))
    replace_val['TransmissionRateOffART'] = np.multiply(np.reshape(attia_new, (8, 1)), np.ones((8, 6)))
    
    for city in ['rio', 'salvador', 'manaus']:
        for k in values:
            # change background testing rate
            replace_val['HIVtestBgAcceptRate'] = k
            
            # replace values in .in file
            float_df = deepcopy(in_file[city])
            
            # replace the variables
            for var in replace_var:
                float_df = t_op.replace_values(var, replace_val[var], float_df)
                
            # save float_df
            filename = os.path.join(filepath_new, city + '_Test accept rate_' + str(k) + '.in')
            link.write_cepac_in_file(filename, float_df)
    
    # parallelize files
    c_op.parallelize_input(filepath_new)
    
    return
Example #2
0
def SA_tx_att_hrg(values):
    
    replace_var = ['TransmissionRateOnART', 'TransmissionRateOffART', 'DynamicTransmissionPropHRGAttrib']
    replace_val = {}
    for city in ['rio', 'salvador', 'manaus']:
        for k in values:# this is  to vary % on ART
            
            # multiply to attia
            attia_on_ART = np.multiply(parameters['attia on ART'], 1)
            attia_off_ART = np.multiply(parameters['attia off ART'], 1)
            replace_val['TransmissionRateOnART'] = np.multiply(np.reshape(attia_on_ART, (8, 1)), np.ones((8, 6)))
            replace_val['TransmissionRateOffART'] = np.multiply(np.reshape(attia_off_ART, (8, 1)), np.ones((8, 6)))
            
            # the percentage attributable to hrg
            replace_val['DynamicTransmissionPropHRGAttrib'] = k
            
            # replace values in .in file
            float_df = deepcopy(in_file[city])
            
            # replace the variables
            for var in replace_var:
                float_df = t_op.replace_values(var, replace_val[var], float_df)
                
            # save float_df
            filename = os.path.join(filepath_new, city + '_att_to_hrg_' + str(k) + '.in')
            link.write_cepac_in_file(filename, float_df)
    
    # parallelize files
    c_op.parallelize_input(filepath_new)
    
    return
Example #3
0
 def create_in_files(self, parallel = 1):
     
     # get list of var to replace, value and base in file
     var_list = self.var_list
     val_map = self.val_map
     in_file = deepcopy(self.base_in_file)
     
     # loop over values we have for var
     for var in var_list:
         for val in val_map[var]:
             # this val can either be a float or array
             #if not (isinstance(val, (float, int))):
             #    raise TypeError('The value of variable for OWSA must be np.array/float/int')
             #    return
             
             # replace variable value
             valtype = int if var in ['HIVtestFreqInterval'] else float
             in_file = t_op.replace_values(var, val, in_file, position = self.var_pos[var], valtype = valtype)
             
             # TODO: search dependencies of current variable and change those values
             in_file = self.dep_data_object.replace_dep_value(var, val, self.dep_data_map, in_file)#, self.var_pos[var])
             
             # save file
             try:
                 x = ('%s=%.4f.in')%(var, val)
             except:
                 x = ('%s=%.4f.in')%(var, val[0])
             file_name = os.path.join(self.save_path, x)
             link.write_cepac_in_file(file_name, in_file)
             
     return
Example #4
0
def SA_acute(values):
    # loop over scale down scenarios
    replace_var = ['TransmissionRateOnART', 'TransmissionRateOffART']#, 'TransmissionRateOnART', 'TransmissionRateOffART']
    replace_val = {}
    for city in ['rio', 'salvador', 'manaus']:
        for k in values:
            
            # multiply to attia
            attia_new = parameters['attia']
            attia_new[len(attia_new)-1] = k
            replace_val['TransmissionRateOnART'] = np.multiply(np.reshape(attia_new, (8, 1)), np.ones((8, 6)))
            replace_val['TransmissionRateOffART'] = np.multiply(np.reshape(attia_new, (8, 1)), np.ones((8, 6)))
            
            # replace values in .in file
            float_df = deepcopy(in_file[city])
            
            # replace the variables
            for var in replace_var:
                float_df = t_op.replace_values(var, replace_val[var], float_df)
                
            # save float_df
            filename = os.path.join(filepath_new, city + '_Acute rate_' + str(k) + '.in')
            link.write_cepac_in_file(filename, float_df)
    
    # parallelize files
    c_op.parallelize_input(filepath_new)
Example #5
0
def SA_testing_rate(values):
    
    replace_var = ['HIVtestBgAcceptRate']
    replace_val = {}
    for city in ['rio', 'salvador', 'manaus']:
        for k in values:# this is  to vary % on ART
            
            # dict of values
            replace_val['HIVtestBgAcceptRate'] = k
            
            # replace values in .in file
            float_df = deepcopy(in_file[city])
            
            # replace the variables
            for var in replace_var:
                float_df = t_op.replace_values(var, replace_val[var], float_df)
                
            # save float_df
            filename = os.path.join(filepath_new, city + '_testing_rate_' + str(k) + '.in')
            link.write_cepac_in_file(filename, float_df)
    
    # parallelize files
    c_op.parallelize_input(filepath_new)
    
    return
Example #6
0
 def create_in_files(self, parallel = 1):
     
     # get variable list, value map and base in_file
     var_list = self.var_list
     var0, var1 = var_list[0], var_list[1]
     val_map = self.val_map
     in_file = deepcopy(self.base_in_file)
     
     # first start loop over var1 values
     for var0_val in val_map[var0]:
         # this val can either be a float or array
         if not (isinstance(var0_val, float) or isinstance(var0_val, int) or isinstance(var0_val, np.array)):
             raise TypeError('The value of variable for TWSA must be np.array/float/int')
             return
         
         # replace variable value
         in_file = t_op.replace_values(var0, var0_val, in_file, position = self.var_pos[var0])
         
         # TODO: find and replace dependent variables of var0
         in_file = self.dep_data_object.replace_dep_value(var0, var0_val, self.dep_data_map, in_file, self.var_pos)
         
         # now start loop over var2
         for var1_val in val_map[var1]:
             # this val can either be a float or array
             if not (isinstance(var0_val, float) or isinstance(var0_val, int) or isinstance(var0_val, np.array)):
                 raise TypeError('The value of variable for TWSA must be np.array/float/int')
                 return
             
             # replace variable value
             in_file = t_op.replace_values(var1, var1_val, in_file, position = self.var_pos[var1])
             
             # TODO: find and replace dependent variables of var1
             in_file = self.dep_data_object.replace_dep_value(var1, var1_val, self.dep_data_map, in_file, self.var_pos)
             
             # save in file
             x = ('%s=%.4f_%s=%.4f.in')%(var0, var0_val, var1, var1_val)
             file_name = os.path.join(self.save_path, x)
             link.write_cepac_in_file(file_name, in_file)
     
     # parallelize 
     c_op.parallelize_input(self.save_path, parallel)
     
     return
     
     
     
     
     
     
     
     
 
     
     
Example #7
0
def SA_treatment(values):
    # loop over scale down scenarios
    replace_var = ['DynamicTransmissionNumTransmissionsHRG', 'TransmissionRiskMultiplier_T1', 'TransmissionRiskMultiplier_T2', 'TransmissionRiskMultiplier_T3', 'TransmissionRateOnART', 'TransmissionRateOffART']
    replace_val = {}
    for city in ['rio', 'salvador', 'manaus']:
        for k in values:# this is  to vary % on ART
            
            # multiply to attia
            attia_on_ART = np.multiply(parameters['attia on ART'], 1)
            attia_off_ART = np.multiply(parameters['attia off ART'], 1)
            replace_val['TransmissionRateOnART'] = np.multiply(np.reshape(attia_on_ART, (8, 1)), np.ones((8, 6)))
            replace_val['TransmissionRateOffART'] = np.multiply(np.reshape(attia_off_ART, (8, 1)), np.ones((8, 6)))
            
            # calculate community viral load distibution
            on_art = np.multiply(k, parameters['viral load distribution on ART'])
            off_art = np.multiply((1-k), parameters['viral load distribution off ART'])
            community_vl = np.add(on_art, off_art)
            
            # calculate average transmission rate
            tx_avg = np.sum(np.multiply(parameters['attia off ART'], community_vl))
            replace_val['DynamicTransmissionNumTransmissionsHRG'] = tx_avg
            
            # calculate transmission rate calibrated to the incidence rate
            tx = np.multiply(parameters[city]['incidence'], np.divide(parameters[city]['index negative'], parameters[city]['index positive']))
            
            # calculate multiplier
            tx_mul = np.divide(tx, tx_avg)
            replace_val['TransmissionRiskMultiplier_T1'] = tx_mul[0]
            replace_val['TransmissionRiskMultiplier_T2'] = tx_mul[0]
            replace_val['TransmissionRiskMultiplier_T3'] = tx_mul[1]
            
            
            # replace values in .in file
            float_df = deepcopy(in_file[city])
            
            # replace the variables
            for var in replace_var:
                float_df = t_op.replace_values(var, replace_val[var], float_df)
                
            # save float_df
            filename = os.path.join(filepath_new, city + '_On treatment_' + str(k) + '.in')
            link.write_cepac_in_file(filename, float_df)
    
    # parallelize files
    c_op.parallelize_input(filepath_new)
Example #8
0
def SA_attia(factors):
    # loop over scale down scenarios
    replace_var = ['DynamicTransmissionNumTransmissionsHRG', 'TransmissionRiskMultiplier_T1', 'TransmissionRiskMultiplier_T2', 'TransmissionRiskMultiplier_T3', 'TransmissionRateOnART', 'TransmissionRateOffART']
    replace_val = {}
    for city in ['rio', 'salvador', 'manaus']:
        for k in factors:# this us scale down factor for Attia rates
            
            # multiply to attia
            attia_new = np.multiply(parameters['attia'], k)
            replace_val['TransmissionRateOnART'] = np.multiply(np.reshape(attia_new, (8, 1)), np.ones((8, 6)))
            replace_val['TransmissionRateOffART'] = np.multiply(np.reshape(attia_new, (8, 1)), np.ones((8, 6)))
            
            # calculate average transmission rate
            tx_avg = np.sum(np.multiply(attia_new, parameters[city]['viral load distribution']))
            replace_val['DynamicTransmissionNumTransmissionsHRG'] = tx_avg
            
            # calculate transmission rate calibrated to the incidence rate
            tx = np.multiply(parameters[city]['incidence'], np.divide(parameters[city]['index negative'], parameters[city]['index positive']))
            
            # calculate multiplier
            tx_mul = np.divide(tx, tx_avg)
            replace_val['TransmissionRiskMultiplier_T1'] = tx_mul[0]
            replace_val['TransmissionRiskMultiplier_T2'] = tx_mul[0]
            replace_val['TransmissionRiskMultiplier_T3'] = tx_mul[1]
            
            
            # replace values in .in file
            float_df = deepcopy(in_file[city])
            
            # replace the variables
            for var in replace_var:
                float_df = t_op.replace_values(var, replace_val[var], float_df)
                
            # save float_df
            filename = os.path.join(filepath_new, city + '_Attia scale down_' + str(k) + '.in')
            link.write_cepac_in_file(filename, float_df)
    
    # parallelize files
    c_op.parallelize_input(filepath_new)
Example #9
0
 def create_in_files(self):
     
     # get list of var to replace, value and base in file
     var_list = self.var_list
     val_map = self.value_map
     in_file = deepcopy(self.base_in_file)
     
     for var in var_list:
         # var should be string
         if not isinstance(var, str):
             raise TypeError('The each key of value map should be string, string of CEPAC variable name')
         
         # replace variable value
         in_file = t_op.replace_values(var, val_map[var], in_file, position = self.var_pos[var])
         
         # TODO: search dependencies of current variable and change those values
         # in_file = find and replace dependent variables
     
     # after we are done changing values for all variables, save .in file
     file_name = os.path.join(self.save_path, 'NewInputFile.in')
     link.write_cepac_in_file(file_name, in_file)
     
     return
Example #10
0
         
         # read respective .in file
         filepath = os.path.join(readpaths[var], file) + '.in'
         float_in = link.read_cepac_in_file(filepath)
         
         # replace few values
         r_val_map = {'UseHIVIncidReduction': 1, 
                      'HIVIncidReductionStopTime': HORIZON, 
                      'HIVIncidReductionCoefficient': coeff}
         for r_var in r_val_map:
             #
             if not r_var in position:
                 position[r_var] = t_op.search_var(r_var, float_in)
             
             # replace value
             float_in = t_op.replace_values(r_var, r_val_map[r_var], float_in, position[r_var])
         
         # write the file
         varval = file.split('=')[1]
         addstr = 'OWSA_final_run_var=%s_val=%s'%(var, varval) + '.in'
         if not os.path.exists(finalpaths[var]): os.makedirs(finalpaths[var])
         savepath = os.path.join(finalpaths[var], addstr)
         link.write_cepac_in_file(savepath, float_in)
     
     # parallelize
     c_op.parallelize_input(finalpaths[var])
 
 
 
 
 
Example #11
0
def write_final_runs(value_grid, path_dict, STOP_TIME = 60):
    
    # aux function for creating final run files
    def get_reduction_coeff(percentage_red, stop_time):
        # reduction coefficient will be as follows
        if ((0.01*percentage_red) < 1) and (percentage_red > 0):
            red_coeff = -1*stop_time/(np.log(1 - (0.01*percentage_red)))
        elif (percentage_red <= 0) or (percentage_red <= 0.0):
            red_coeff = 10000
        else:
            # this says percentage reduction is >= 100%
            red_coeff = -1*stop_time/np.log(10**-100)
        
        return red_coeff
    
    # check if both inputs are dictionaries
    if not (isinstance(value_grid, dict) or isinstance(path_dict, dict)):
        raise TypeError("Input needs to be in dictionary format.")
        return
    #
    val_to_replace = value_grid
    del value_grid
    
    # import out files
    #out_path = os.path.join(path_dict['output']['intervention'], r'results')
    cepac_out = {}
    for k in path_dict['output']:
        cepac_out[k] = link.import_all_cepac_out_files(os.path.join(path_dict['output'][k], r'results'), module = "regression")
    
    # set stop time and PrEP duration
    #SQ_DF = link.import_all_cepac_in_files(path_dict['output']['status quo'])['SQ']
    #STOP_TIME = int(t_op.read_values('HIVIncidReductionStopTime', SQ_DF))
    #del SQ_DF
    
    # create sets of runs ABC
    out = {}
    name_map = {}
    for k in cepac_out['intervention']:
        if k == 'popstats':
            continue
        key_name = aux.get_digit_for_file_name(k, val_to_replace)
        if not key_name in out.keys():
            out[key_name] = {}
        name_map[key_name] = k
        if "RunB" in k:
            out[key_name]['B'] = cepac_out['intervention'][k]
        elif "RunC" in k:
            out[key_name]['C'] = cepac_out['intervention'][k]
        else:
            continue
    
    # TODO: following value will change according to city, need to take care of this
    inp = {}
    for k in out:
        out[k]['A'] = cepac_out['status quo']['SQ']#['A']
        inp[k] = {'PrEPCoverage': 0, 'prep_efficacy': 0.96, 'CohortSize': 10000000,
           'PrEPDuration': 0, 'HIVmthIncidMale': 0.00357692085607886, 'prep_usage_at_initialization': 'n'}
    
    row_idx = 0
    percentage_decline = []
    for i in val_to_replace['PrEPCoverage']:
        for j in val_to_replace['PrEPDuration']:
            if i == 0.0:
                j_idx = np.where(val_to_replace['PrEPDuration'] == j)[0][0]
                out[j_idx] = {}
                out[j_idx]["A"] = cepac_out["status quo"]["SQ"]
                out[j_idx]["B"] = cepac_out["status quo"]["SQ"]
                out[j_idx]["C"] = cepac_out["status quo"]["SQ"]
                inp[j_idx] = inp[1]
            inp[row_idx]['PrEPCoverage'] = i
            inp[row_idx]['PrEPDuration'] = STOP_TIME #60 #600
            percentage_decline.append(tx_algo.get_percentage_decline(out[row_idx], inp[row_idx]))
            row_idx += 1
    
    # plot results
    plot_heatmap(val_to_replace, percentage_decline, path_dict)
    
    # write results to excel file
    export_abc_out_to_excel(cepac_out, out, val_to_replace, path_dict)
        
    # create directory to write the final run files
    if not os.path.exists(path_dict['output']['final runs']):
        os.makedirs(path_dict['output']['final runs'])
    final_path = path_dict['output']['final runs']
    
    # read one .in file (B, as that is intervention file) and alter following values
    # 1. reduction coefficient
    # 2. disable dynamic transmission module
    
    # import base file for B
    base_int = link.import_all_cepac_in_files(path_dict['input'])
    base_int = base_int['B']
    
    # first we'll find the indices of all the required variables
    idx = {}
    var_list = ["UseHIVIncidReduction", "HIVIncidReductionStopTime", "HIVIncidReductionCoefficient", 
                "UseDynamicTransmission", "PrEPCoverage", "PrEPDuration"]
    for k in var_list:
        idx[k] = base_int.loc[base_int.loc[:, 0] == k, :].index.values
    
    # replace required values
    for run in name_map:
        # deepcopy
        float_int = deepcopy(base_int)
        
        # following value of stop time might not be correct
        float_int.loc[idx["HIVIncidReductionStopTime"], 1] = STOP_TIME #480#120
        coeff = get_reduction_coeff(percentage_decline[run], float_int.loc[idx["HIVIncidReductionStopTime"], 1].values[0])
        if coeff <= 0:
            # disable incidence reduction 
            float_int.loc[idx["UseHIVIncidReduction"], 1] = 0
            #float_int.loc[idx["HIVIncidReductionCoefficient"], 1] = coeff
        else:
            # enable incidence reduction 
            float_int.loc[idx["UseHIVIncidReduction"], 1] = 1
            float_int.loc[idx["HIVIncidReductionCoefficient"], 1] = coeff
        
        # disable dynamic transmission module
        # TODO: we need to take care that the values for risk multiplier are correct
        float_int.loc[idx["UseDynamicTransmission"], 1] = 1
        
        # replace coverage and coverage time
        float_int.loc[idx["PrEPCoverage"], 1:2] = 0.01*(aux.get_coverage_level_from_file_name(name_map[run]))
        float_int.loc[idx["PrEPDuration"], 1:2] = aux.get_coverage_time_from_file_name(name_map[run])
        
        # write the file
        save_path = os.path.join(final_path, name_map[run].split('_')[1]) + '.in'
        link.write_cepac_in_file(save_path, float_int)
Example #12
0
def write_abc(value_map, path_dict):
    
    # check if both inputs are dictionaries
    if not (isinstance(value_map, dict) or isinstance(path_dict, dict)):
        raise TypeError("Input needs to be in dictionary format.")
        return
    # check keys in the input dictionary
    for k in value_map:
        if not k in ["PrEPCoverage", "PrEPDuration"]:
            raise ValueError("Keys of the input dictionary should only contain PrEPCoverage and PrEPDuration")
            return
        
    # Lazyness encounter: remaning input because rest of the code was written with different name
    var_to_replace = value_map
    del value_map
    
    # create mesh grid for values
    val_to_replace = {}
    val_to_replace['PrEPCoverage'], val_to_replace['PrEPDuration'] = np.meshgrid(var_to_replace['PrEPCoverage'], var_to_replace['PrEPDuration'])
    
    # import the base files
    cepac_in = link.import_all_cepac_in_files(path_dict['input'])
    
    # find indices of the required variables
    idx = {}
    for k in var_to_replace:
        idx[k] = cepac_in['B'].loc[cepac_in['B'].loc[:, 0] == k, :].index.values
    
    # replace all the indices with respective values
    float_b = cepac_in['B']
    float_c = cepac_in['C']
    for row in range(len(val_to_replace['PrEPCoverage'])):
        #
        for col in range(val_to_replace['PrEPCoverage'].shape[1]):
            # don't need to create files for zero coverage, just use SQ results
            if float(val_to_replace["PrEPCoverage"][(row, col)]) == 0:
                continue
            #
            for var in var_to_replace: 
                float_b.loc[idx[var], 1:2] = val_to_replace[var][(row, col)]
                float_c.loc[idx[var], 1:2] = val_to_replace[var][(row, col)]
            
            # make new dir
            if not os.path.exists(path_dict['output']['intervention']):
                os.makedirs(path_dict['output']['intervention'])
            
            # name the input file
            path = path_dict['output']['intervention']
            # B
            name = "RunB_Coverage=%d, Duration=%d"%(100 * val_to_replace["PrEPCoverage"][(row, col)], val_to_replace["PrEPDuration"][(row, col)]) + r".in"
            float_path = os.path.join(path, name)
            link.write_cepac_in_file(float_path, float_b)
            # C
            name = "RunC_Coverage=%d, Duration=%d"%(100 * val_to_replace["PrEPCoverage"][(row, col)], val_to_replace["PrEPDuration"][(row, col)]) + r".in"
            float_path = os.path.join(path, name)
            link.write_cepac_in_file(float_path, float_c)
    
    # create a folder for status quo
    if not os.path.exists(path_dict['output']['status quo']):
        os.makedirs(path_dict['output']['status quo'])
    
    # write a status quo .in file in the stsus quo folder
    link.write_cepac_in_file(os.path.join(path_dict['output']['status quo'], 'SQ.in'), cepac_in['SQ'])

    return
Example #13
0
             # replace value in the .in file
             float_df = t_op.replace_values(var, val, float_df)
             float_df = t_op.replace_values('PrepIncidMale', val_prep, float_df)
             
             del val, val_prep#, solution
             
         else:
             # print
             #print('This variable has been altered in .in file: %s'%(var))
             
             # replace value in the .in file
             float_df = t_op.replace_values(var, samples[var][run], float_df)
     
     # save the file
     name = os.path.join(savepath_cepac, 'sample_' + str(run) + '.in')
     link.write_cepac_in_file(name, float_df)
     
     # featurizarion of the .in file
     feature_mat = h_fun1.featurize_in_file(samples, sample_bounds, run, SEQ_LEN)
     feature_tensor.append(feature_mat)
     
     # print
     print(run)
     
 # save condensed cepac input files
 CEPAC_input_condensed = CEPAC_input_condensed.to_dict()
 h_fun1.dump_json(CEPAC_input_condensed, os.path.join(savepath_rnn, 'CEPAC_input.json'))
 
 # parallelize input files
 c_op.parallelize_input(savepath_cepac, parallel = 5)
 
Example #14
0
        for var2 in replace_val["PrEPDuration"]:
            
            # replace coverage time
            run_b = find_and_replace(run_b, "PrEPDuration", var2)
            run_c = find_and_replace(run_c, "PrEPDuration", var2) 
            
            # create directory
            dir_name = "Coverage time - %d"%(var2)
            if not os.path.exists(os.path.join(path_save, dir_name)):
                path_float = os.path.join(path_save, dir_name)
                os.makedirs(path_float)
            
            # save files
            path_b = os.path.join(path_float, "B.in")
            path_c = os.path.join(path_float, "C.in")
            link.write_cepac_in_file(path_b, run_b)
            link.write_cepac_in_file(path_c, run_c)


# read output and write final files       
path = {"output": r"C:\Users\Vijeta\Documents\Projects\Brazil PrEP\Transmission runs\Input files\Runs asked by Ken"}
dir_list = {"coverage": os.listdir(path["output"]), "coverage time": []}

cepac_out = {}
percentage_decline = {}
for p_coverage in dir_list["coverage"]:
    # create dictionary
    cepac_out[p_coverage] = {}
    percentage_decline[p_coverage] = {}
    
    # get all the subfolders
Example #15
0
            pass

        # write all final run files
        abc_output.write_final_runs(var_to_replace, path_dict, HORIZON)

        # paralellize
        c_op.parallelize_input(path_dict['output']['final runs'])
    else:
        # write all abc run files
        abc_input.write_abc(var_to_replace, path_dict)

        # parallelize
        c_op.parallelize_input(path_dict['output']['intervention'])

# compare New CEPAC with Old CEPAC
"""
#
new_path = r'/Users/vijetadeshpande/Downloads/MPEC/Brazil/Rio/ART CHECK1/Compare different ART parameter setting1/2-way SA/Measurement of community benefit_R10/Status quo_R10/SQ.in'
old_path = r'/Users/vijetadeshpande/Downloads/MPEC/Brazil/Rio/2-way SA_10 year/Measurement of community benefit_R10/Status quo_R10/SQ.in'
new_sq = link.read_cepac_in_file(new_path)
old_sq = link.read_cepac_in_file(old_path)

#
mismatch_idx, mismatch_idx1 = [], []
mismatch_data, mismatch_data1 = {}, {}
sq = deepcopy(new_sq)
for row in range(0, 1005):
    x = old_sq.iloc[row, :].dropna()
    y = new_sq.iloc[row, :].dropna()
    if (x != y).values.astype(int).sum() > 0:
        mismatch_idx.append(row)
Example #16
0
    def get_sensitivity_analysis_files(self, path):

        # create a new folder which can directly be uploaded on cluster
        run_folder_name = r'Runs prepared for sensitivity analysis'
        if not os.path.exists(os.path.join(path, run_folder_name)):
            os.makedirs(os.path.join(path, run_folder_name))
            path = os.path.join(path, run_folder_name)
        else:
            path = os.path.join(path, run_folder_name)
        
        # define a number to replace na in df
        end_string = 'stop'

        # get input data i.e. cepac input for the intervention scenario
        for i in self.base_files:
            if 'intervention' in i:
                intervention = deepcopy(self.base_files[i])
                break
            elif 'RunB' in i:
                intervention = deepcopy(self.base_files[i])

        # variable perturbation
        perturbation = deepcopy(self.perturbation)
        perturbation['Variable names'] = perturbation['Variable names'].fillna(end_string)

        # create structured perturbation data
        row_index = -1
        for var in perturbation['Variable names']['variable name']:

            # update row index
            row_index += 1
            if var == perturbation['Variable names']['variable name'][0]:
                continue

            # break the loop when there's no variable left to do S.A. over
            if var == end_string:
                break

            # get the values of for S.A. for respective variable
            var_float = perturbation['Variable names']['float name'][row_index]
            var_sheet = perturbation[var_float].iloc[2:, :].reset_index(drop = True)
            var_sheet = var_sheet.iloc[var_sheet[1].first_valid_index():var_sheet[1].last_valid_index() + 1, :].dropna(
                axis=1)
            
            #%% if variable under consideration is efficacy or adherance, you
            # won't find in .in file
            
            ##################################################################
            # find index of current variable in the CEPAC input file 
            replace_index = intervention.index[intervention[0] == var]
            
            #%%

            # check if there are multiple rows for current variable
            if len(replace_index) != 1:

                # select the specific dimension for S.A.

                # take dimension value from perturbation
                dim1 = perturbation['Variable names']['dim1'].loc[
                    perturbation['Variable names'].loc[:, 'variable name'] == var]

                # if that dimension is not availble in CEPAC_input the thre's 
                # something wrong
                if dim1.values[0] not in intervention.loc[replace_index, 1].values[:]:

                    print('Error')

                # if that specific value is available in CEPAC_input then see 
                # if there's any dim2 mentioned in the perturbation file
                else:

                    dim2 = perturbation['Variable names']['dim2'].loc[
                        perturbation['Variable names'].loc[:, 'variable name'] == var]

                    # %%
                    # if there's no second dimention mentioned then we'll 
                    # replace the entire array of inputs corresponding to dim1
                    if dim2.values[0] == end_string:

                        # write the file
                        self.write_files(dim1=dim1,
                                         intervention_file=intervention,
                                         index_of_current_var=replace_index,
                                         path=path,
                                         index_value_in_perturbation=row_index,
                                         variable_under_consideration=var,
                                         variable_values_sheet_perturbation=var_sheet)


                    # %%
                    # if there's a second dimension mentioned in the perturbation 
                    # file then we'll replace only one value in the array
                    # of inputs corresponding to dim1
                    else:

                        # if that dimension is not available in CEPAC_input the thre's
                        # something wrong
                        if dim2.values[0] not in intervention.loc[replace_index, 1].values[:]:

                            # this section is not written correctly
                            print('Error')

                        # if that specific value is available in CEPAC_input then see 
                        # if there's any dim2 mentioned in the perturbation file
                        else:

                            # this section is not written correctly
                            print('Error')

            # %%
            # if there's only one element in replace_index 
            else:

                # write the file
                self.write_files(dim1=pd.DataFrame(),
                                 intervention_file=intervention,
                                 index_of_current_var=replace_index,
                                 path=path,
                                 index_value_in_perturbation=row_index,
                                 variable_under_consideration=var,
                                 variable_values_sheet_perturbation=var_sheet)
                
        # shift files for basecase and intervention to a folder
        data = self.basecase_intervention
        new_path = os.path.join(path, r'Basecase and Intervention')
        if not os.path.exists(new_path):
            os.makedirs(new_path)
        for file in data:
            link.write_cepac_in_file(os.path.join(new_path, file+r'.in'), data[file])
            #os.remove(os.path.join(path, file+r'.in'))

        return True
Example #17
0
    def write_files(self, dim1,
                    intervention_file,
                    index_of_current_var,
                    path,
                    index_value_in_perturbation,
                    variable_under_consideration,
                    variable_values_sheet_perturbation):

        # in this case there will be an array of replace_index
        # and we have to find out which row to select for
        # replacing data
        if len(index_of_current_var) > 1:
            index_of_current_var = intervention_file.iloc[index_of_current_var, :].loc[
                                   intervention_file[1] == dim1.values[0], :].index.values

        replace_col = intervention_file.iloc[index_of_current_var, :].dropna(axis=1).columns[
                      2 - int(dim1.empty):].values

        # using var_sheet replace the row-col (replace_index-replace_col)
        # with values mentioned in the perturbation file,
        # one by one

        # create a folder with name of current var
        new_path = path + r'\/' + str(index_value_in_perturbation) + '. ' + variable_under_consideration
        if not os.path.exists(new_path):
            os.makedirs(new_path)

        # iterate over the values mentioned in perturbation file 
        # for sensitivity analysis
        for i in variable_values_sheet_perturbation.index:
            # copy main intervention file
            intervention_float = deepcopy(intervention_file)
            
            # replace values with perturbation values
            intervention_float = self.check_dependancies_and_replace()
            
            #%% it'll be better we create a central function for replacing values.
            # whenever you have to changeany value in .in file, you have to call 
            # that function.
            # Most importantly, function should take care of the dependancies 
            # between different variables 
            
            ##################################################################
            
            if variable_under_consideration in self.independant_var:
                val = {'x_cepac': {}, 'x_user_in': {}}
                val['x_user_in'][variable_under_consideration] = variable_values_sheet_perturbation.iloc[i,2:].values
                if variable_under_consideration == 'percenatge_reduction':
                    intervention_float = link.calculations_for_depemdent_var(intervention_float, self.dependent_var_map[0], val)
                elif variable_under_consideration == 'prep_efficacy':
                    print('\n\n================================================')
                    val['x_user_in']['prep_adherence'] = input('\nPlease enter PrEP adherence value for basecase: \n')
                    intervention_float = link.calculations_for_depemdent_var(intervention_float, self.dependent_var_map[1], val)
                elif variable_under_consideration == 'prep_adherence':
                    print('\n\n================================================')
                    val['x_user_in']['prep_eficacy'] = input('\nPlease enter PrEP efficacy value for basecase: \n')
                    intervention_float = link.calculations_for_depemdent_var(intervention_float, self.dependent_var_map[1], val)
            else:
                intervention_float.iloc[index_of_current_var, replace_col] = variable_values_sheet_perturbation.iloc[i,
                                                                         2:].values
            
            #%%
            
            # save the file
            save_path = new_path + r'\/' + str(variable_under_consideration) + r' value ' + str(i + 1) + r'.in'
            link.write_cepac_in_file(save_path, intervention_float)
Example #18
0
                float_df = find_and_replace(float_df, var, sample[var][row])
                prep_incid = sample[var][row] * (1 - efficacy * adherence)
                float_df = find_and_replace(float_df, 'PrepIncidMale',
                                            prep_incid)

            else:
                float_df = find_and_replace(float_df, var, sample[var][row])

        # write file
        if row % 10 == 0:
            name_extension = "CEPAC input set %d" % (row / 10)
        run_path = os.path.join(path["Input"], name_extension)
        if not os.path.exists(run_path):
            os.makedirs(run_path)
        name = 'cepac_run' + str(row) + r'.in'
        link.write_cepac_in_file(os.path.join(run_path, name), float_df)

# stop timer
stop = timeit.default_timer()

print('Time: ', stop - start)

#%%
#def weib(t, shape):
#    coverage = 0.01
#    duration = 120
#    scale = -np.log(1 - coverage)/(duration**shape)
#    tp = 1 - np.exp(scale*(math.pow(t, shape) - math.pow(t+1, shape)))

#    return tp