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
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
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
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)
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
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)
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)
# 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])
# 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) # save feature tensor if not os.path.exists(savepath_rnn): os.makedirs(savepath_rnn) h_fun1.dump_json(feature_tensor, os.path.join(savepath_rnn, 'RNN_source.json')) # stop timer stop = timeit.default_timer() print('Time: ', stop - start)
# write excel file for the CEPAC output link.export_output_to_excel(final_path, final_path) elif os.path.exists(path_dict['output']['intervention']): # collect parallelized output try: c_op.collect_output( os.path.join(path_dict['output']['intervention'])) except: 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)
def parallelize(self, parallel = 1): # parallelize the files c_op.parallelize_input(self.save_path, parallel) return