def main(graph_name): G = nx.read_gml(graph_name) all_team = "NO" # as adopters or not Niter = 100 dir_real_data = '../Results/' delta_end = 3 # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) # NO RESTRICTION, TO OBTAIN THE COMPLETE PARAMETER LANDSCAPE output_file3 = dir_real_data + "Landscape_parameters_infection_" + str( Niter) + "iter.dat" file3 = open(output_file3, 'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol = [] result_actual_file = csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont = 0 for row in result_actual_file: if cont > 0: # i ignore the first line with the headers num_adopters = row[3] list_actual_evol.append(float(num_adopters)) cont += 1 ################################################################## #../Results/network_final_schedule_withTeam3/infection/Average_time_evolution_Infection_p0.9_Immune0.5_1000iter_2012.dat prob_min = 0.00 prob_max = 1.001 delta_prob = 0.01 prob_Immune_min = 0.0 prob_Immune_max = 1.001 delta_prob_Immune = 0.01 dir = "../Results/network_final_schedule_withTeam3_local/infection/" dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_2012.dat" file2 = open(output_file2, 'wt') file2.close() # i create the empty list of list for the Niter temporal evolutions num_shifts = 0 for n in G.nodes(): G.node[n]["status"] = "S" if G.node[n]['type'] == "shift": num_shifts += 1 # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] list_abs_dist_at_ending_point_fixed_parameters = [] list_final_num_infected = [] for iter in range(Niter): print " iter:", iter #######OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS #file_name_indiv_evol=output_file2.strip("Average_").split('.dat')[0]+"_indiv_iter"+str(iter)+".dat" #file4 = open(file_name_indiv_evol,'wt') #file4.close() ########################################## list_I = [] #list infected doctors list_ordering = [] list_s = [] ########### set I.C. max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": list_s.append(n) if G.node[n]['order'] > max_order: max_order = G.node[n]['order'] else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" # print max_order ################# the dynamics starts: t = 1 while t <= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" list_I.append( G.node[doctor]["label"]) list_single_t_evolution.append(float(len(list_I))) t += 1 ######## end t loop ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS #file4 = open(file_name_indiv_evol,'at') #for i in range(len(list_single_t_evolution)): #ime step by time step # print >> file4, i,list_single_t_evolution[i], prob_infection, prob_Immune #file4.close() ######################################################## list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol, list_single_t_evolution)) list_abs_dist_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_final_num_infected.append(list_single_t_evolution[-1]) ############################# end loop Niter list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_abs_dist_at_ending_point_fixed_parameters)) file3 = open(output_file3, 'at') # i print out the landscape print >> file3, prob_infection, prob_Immune, numpy.mean( list_abs_dist_at_ending_point_fixed_parameters ), numpy.mean(list_dist_fixed_parameters), numpy.mean( list_final_num_infected ), numpy.std(list_final_num_infected), numpy.std( list_final_num_infected) / numpy.mean(list_final_num_infected) file3.close() if ( numpy.mean(list_abs_dist_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end file2 = open(output_file2, 'at') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) file2.close() # file = open(output_file,'at') #print >> file, prob_infection, numpy.mean(list_final_I_values_fixed_p) #file.close() # print list_dist_fixed_parameters # histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,50, "../Results/histogr_distances_indiv_infect_simus_to_the_average_curve_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter.dat") # Nbins=100 prob_infection += delta_prob prob_Immune += delta_prob_Immune compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Infection", all_team, Niter, None) # file3.close() print "printed out landscape file:", output_file3
def main(graph_name): G = nx.read_gml(graph_name) Niter = 100000 dir_real_data = '../Results/' all_team = "NO" # as adopters or not # output_file3=dir_real_data+"Landscape_parameters_persuasion_"+str(Niter)+"iter.dat" #file3 = open(output_file3,'wt') ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol = [] result_actual_file = csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont = 0 for row in result_actual_file: if cont > 0: # i ignore the first line with the headers num_adopters = row[3] list_actual_evol.append(float(num_adopters)) cont += 1 ################################################################## #../Results/network_final_schedule_withTeam3/Time_evolutions_Persuasion_alpha0.1_damping0.3_mutual_encourg0.3_threshold0.2_unif_distr_50iter_2012_seed31Oct_finalnetwork.dat alpha_F = 0.10 # alpha=0: nobody changes their mind alpha_A = 0.5 * alpha_F # alpha=0: nobody changes their mind damping = 0.30 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N mutual_encouragement = 0.30 # when two Adopters meet, they convince each other even more threshold = 0.20 # larger than, to be an Adopte print "\n\nPersuasion process on network, with Niter:", Niter dir = "../Results/network_final_schedule_withTeam3_local/" output_file = dir + "Time_evolutions_Persuasion_alpha" + str( alpha_F) + "_damping" + str(damping) + "_mutual_encourg" + str( mutual_encouragement) + "_threshold" + str( threshold) + "_unif_distr_" + str( Niter) + "iter_2012_seed31Oct_finalnetwork.dat" file = open(output_file, 'wt') file.close() time_evol_number_adopters_ITER = [ ] # list of complete single realizations of the dynamics for iter in range(Niter): print " ", iter list_t = [] time_evol_number_adopters = [ ] # for a single realization of the dynamics num_adopters, seed_shift, max_shift = set_ic( G, threshold ) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) # print "initial number of adopters:", num_adopters list_t.append(0) ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(output_file.split('.dat')[0]+"_indiv_iter"+str(iter)+".dat",'wt') #file4.close() ########################################## # the dynamics starts: t = int(seed_shift) + 1 # the first time step is just IC.??? while t <= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) list_t.append(t) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: # i look for the shift corresponding to that time step flag_possible_persuasion = 0 for doctor in G.neighbors(n): if G.node[doctor][ "status"] == "Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion = 1 break if flag_possible_persuasion == 1: list_doctors = [] for doctor in G.neighbors( n): # for all drs in that shift list_doctors.append(doctor) pairs = itertools.combinations( list_doctors, 2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1 = pair[0] doctor2 = pair[1] if G.node[doctor1]['status'] != G.node[doctor2][ 'status']: # if they think differently, # there will be persuasion persuasion(G, damping, doctor1, doctor2, alpha_A, alpha_F, threshold ) # i move their values of opinion update_opinions( G, threshold, doctor1, doctor2 ) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G, mutual_encouragement, doctor1, doctor2) list_Adopters = [] #count how many i have at this time for n in G.nodes(): try: if G.node[n]["status"] == "Adopter": if G.node[n]["label"] not in list_Adopters: list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute time_evol_number_adopters.append(float(len(list_Adopters))) t += 1 ############## end while loop over t time_evol_number_adopters_ITER.append(time_evol_number_adopters) ##############end loop Niter average_time_evol_number_adopters = [] for i in range(len(time_evol_number_adopters)): #time step by time step list_fixed_t = [] for iteracion in range( Niter): #loop over all independent iter of the process list_fixed_t.append( time_evol_number_adopters_ITER[iteracion] [i]) # i collect all values for the same t, different iter average_time_evol_number_adopters.append( numpy.mean(list_fixed_t)) # i create the mean time evolution list_dist_fixed_parameters = [] for lista in time_evol_number_adopters_ITER: list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( lista, average_time_evol_number_adopters)) lista_tuplas = histograma_bines_gral.histograma_bins( list_dist_fixed_parameters, 75, "../Results/histogr_distances_indiv_pers_simus_to_the_average_curve_alpha" + str(alpha_F) + "_damping" + str(damping) + "_mutual_encourg" + str(mutual_encouragement) + "_threshold" + str(threshold) + "_unif_distr_" + str(Niter) + "iter.dat") # Nbins=50 #print lista_tuplas starting_point = compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol, average_time_evol_number_adopters ) # distance between actual curve and the mean curve prob = calculate_numeric_integral.integral(lista_tuplas, starting_point) print "the probability of having a distance equal or larger than", starting_point, "between actual-average curve is:", prob, "(it is to say, the prob. of the actual evolution being an individual realization of the Persuasion Model)" if all_team == "YES": file = open( "../Results/distance_actual_to_average_curve_persuasion_all_team_as_adopters.dat", 'wt') else: file = open( "../Results/distance_actual_to_average_curve_persuasion.dat", 'wt') print >> file, starting_point, 0. print >> file, starting_point + 0.01, 1. file.close() if all_team == "YES": file2 = open( "../Results/Results_distance_actual_to_average_curve_persuasion_all_team_as_adopters.dat", 'wt') else: file2 = open( "../Results/Results_distance_actual_to_average_curve_persuasion.dat", 'wt') print >> file2, "the probability of having a distance equal or larger than", starting_point, "between actual-average curve is:", prob, "(it is to say, the prob. of the actual evolution being an individual realization of the Infection Model)" file2.close()
def main(graph_name): G = nx.read_gml(graph_name) Niter=1000 delta_end=3 # >= than + or - dr difference at the end of the evolution dir_real_data='../Results/' all_team="NO" # as adopters or not # output_file3=dir_real_data+"Landscape_parameters_persuasion_"+str(Niter)+"iter.dat" #file3 = open(output_file3,'wt') ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team=="YES": filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol=[] result_actual_file= csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont=0 for row in result_actual_file: if cont>0: # i ignore the first line with the headers num_adopters= row[3] list_actual_evol.append(float(num_adopters)) cont+=1 ################################################################## #../Results/network_final_schedule_withTeam3/Time_evolutions_Persuasion_alpha0.2_damping0.0_mutual_encourg0.7_threshold0.4_unif_distr_50iter_2012_seed31Oct_finalnetwork.dat alpha_F_min=0.0 # alpha=0: nobody changes their mind alpha_F_max=1.001 delta_alpha_F=0.1 min_damping=0.0 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping=1.01 delta_damping=0.1 min_mutual_encouragement=0.0 # when two Adopters meet, they convince each other even more max_mutual_encouragement=1.01 delta_mutual_encouragement=0.1 threshold_min=0.0 # larger than, to be an Adopte threshold_max=1.001 delta_threshold=0.1 print "\n\nPersuasion process on network, with Niter:",Niter dict_filenames_tot_distance={} # i will save the filename as key and the tot distance from that curve to the original one threshold=threshold_min while threshold<= threshold_max: print "thershold:",threshold alpha_F=alpha_F_min while alpha_F<= alpha_F_max: # i explore all the parameter space, and create a file per each set of values alpha_A=0.5*alpha_F print " alpha_F:",alpha_F mutual_encouragement=min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:",mutual_encouragement damping=min_damping while damping <= max_damping: print " damping:",damping dir="../Results/network_final_schedule_withTeam3_local/" output_file=dir+"Time_evolutions_Persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_unif_distr_"+str(Niter)+"iter_2012_seed31Oct_finalnetwork.dat" file = open(output_file,'wt') file.close() time_evol_number_adopters_ITER=[] # list of complete single realizations of the dynamics list_dist_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] for iter in range(Niter): print " ",iter list_t=[] time_evol_number_adopters=[] # for a single realization of the dynamics num_adopters , seed_shift ,max_shift= set_ic(G,threshold) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) # print "initial number of adopters:", num_adopters list_t.append(0) ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(output_file.split('.dat')[0]+"_indiv_iter"+str(iter)+".dat",'wt') # file4.close() ########################################## # the dynamics starts: t=int(seed_shift)+1 # the first time step is just IC.??? while t<= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) list_t.append(t) for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: # i look for the shift corresponding to that time step flag_possible_persuasion=0 for doctor in G.neighbors(n): if G.node[doctor]["status"]=="Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion=1 break if flag_possible_persuasion==1: list_doctors=[] for doctor in G.neighbors(n): # for all drs in that shift list_doctors.append(doctor) pairs=itertools.combinations(list_doctors,2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1=pair[0] doctor2=pair[1] if G.node[doctor1]['status'] != G.node[doctor2]['status']: # if they think differently, # there will be persuasion persuasion(G,damping,doctor1,doctor2,alpha_A,alpha_F,threshold) # i move their values of opinion update_opinions(G,threshold,doctor1,doctor2) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G,mutual_encouragement,doctor1,doctor2) list_Adopters=[] #count how many i have at this time for n in G.nodes(): try: if G.node[n]["status"]=="Adopter": if G.node[n]["label"] not in list_Adopters: list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute time_evol_number_adopters.append(float(len(list_Adopters))) t+=1 ############## end while loop over t time_evol_number_adopters_ITER.append(time_evol_number_adopters) list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol,time_evol_number_adopters)) list_dist_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1]-list_actual_evol[-1]) ) ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(output_file.split('.dat')[0]+"_indiv_iter"+str(iter)+".dat",'at') # for i in range(len(time_evol_number_adopters)): #ime step by time step # print >> file4, i,time_evol_number_adopters[i], alpha_F,damping,mutual_encouragement #file4.close() ######################################################## #######################end loop over Niter list_pair_dist_std_delta_end=[] list_pair_dist_std_delta_end.append(numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append(numpy.std(list_dist_fixed_parameters) ) list_pair_dist_std_delta_end.append(numpy.mean(list_dist_at_ending_point_fixed_parameters)) if (numpy.mean(list_dist_at_ending_point_fixed_parameters)) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[output_file]=list_pair_dist_std_delta_end #print >> file3, alpha_F,damping,mutual_encouragement,threshold,dict_filenames_tot_distance[output_file][0],dict_filenames_tot_distance[output_file][1] file = open(output_file,'wt') for i in range(len(time_evol_number_adopters)): #time step by time step list_fixed_t=[] for iteracion in range (Niter): #loop over all independent iter of the process list_fixed_t.append(time_evol_number_adopters_ITER[iteracion][i]) # i collect all values for the same t, different iter print >> file, list_t[i],numpy.mean(list_fixed_t),numpy.std(list_fixed_t), alpha_F,damping,mutual_encouragement file.close() # print len(list_t), len(time_evol_number_adopters) # histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,50, "../Results/histogr_distances_indiv_pers_simus_to_the_average_curve_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_unif_distr_"+str(Niter)+"iter.dat") # Nbins=100 damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F threshold += delta_threshold compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end(dict_filenames_tot_distance,"Persuasion",all_team,Niter)
def main(graph_name): G = nx.read_gml(graph_name) list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) percent_envelope=95. Niter=1000 cutting_day=175 Nbins=20 # for the histogram of sum of distances for_testing_fixed_set="YES" # when YES, fixed values param and get all statistics on final distances etc envelopes="YES" delta_end=3. # >= than + or - dr difference at the end of the evolution dir_real_data='../Results/' all_team="NO" # as adopters or not NO now means i use the file without fellows, only attendings if for_testing_fixed_set=="NO": output_file3="../Results/weight_shifts/Landscape_parameters_persuasion_damping0.1_mutual0.2_"+str(Niter)+"iter_alphaA_eq_alphaF_.dat" file3 = open(output_file3,'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team=="YES": print "remember that now i use the file of adopters without fellows\n../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" exit() else: filename_actual_evol="../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters= float(line.split(" ")[1]) list_actual_evol.append(num_adopters) ################################################################## #../Results/weight_shifts/persuasion/Time_evolutions_Persuasion_training_alpha0.2_damping0.0_mutual_encourg0.5_threshold0.7_unif_distr_1000iter_2012_seed31Oct_finalnetwork_day125.dat #../Results/weight_shifts/persuasion/Time_evolutions_Persuasion_training_alpha0.5_damping0.4_mutual_encourg0.5_threshold0.5_unif_distr_1000iter_2012_seed31Oct_finalnetwork_day125.dat #OJO!!! NECESITO DOS DECIMALES SIEMPRE, PARA QUE CUADRE CON EL NOMBRE DE LOS SUB-DIRECTORIOS DONDE LO GUARDO alpha_F_min=0.50 #0.15 # alpha=0: nobody changes their mind alpha_F_max=0.501 #0.351 delta_alpha_F=0.10 #AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT! min_damping=0.50 #0.0 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping=0.501 #0.451 delta_damping=0.10 min_mutual_encouragement=0.50 #0.50 # when two Adopters meet, they convince each other even more max_mutual_encouragement=0.501 # 0.51 # KEEP THIS FIXED VALUES FOR NOW delta_mutual_encouragement=0.10 threshold_min=0.30 #0.50 # larger than, to be an Adopte threshold_max=0.301 # 0.51 # KEEP THIS FIXED VALUES FOR NOW delta_threshold=0.10 # AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT print "\n\nPersuasion process on network, with Niter:",Niter dict_filenames_tot_distance={} # i will save the filename as key and the tot distance from that curve to the original one threshold=threshold_min while threshold<= threshold_max: print "thershold:",threshold alpha_F=alpha_F_min while alpha_F<= alpha_F_max: # i explore all the parameter space, and create a file per each set of valuesllkl alpha_A=1.0*alpha_F print " alpha_F:",alpha_F mutual_encouragement=min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:",mutual_encouragement damping=min_damping while damping <= max_damping: print " damping:",damping dir="../Results/weight_shifts/persuasion/alpha%.2f_damping%.2f/" % (alpha_F, damping ) if for_testing_fixed_set=="YES": output_file=dir+"Time_evol_Persuasion_train_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_alphaA_eq_alphaF.dat" else: output_file=dir+"Time_evol_Persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_alphaA_eq_alphaF.dat" file = open(output_file,'wt') file.close() time_evol_number_adopters_ITER=[] # list of complete single realizations of the dynamics list_dist_fixed_parameters=[] list_dist_abs_at_ending_point_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_final_num_adopt=[] #list_abs_dist_at_cutting_day=[] for iter in range(Niter): # print " ",iter list_t=[] time_evol_number_adopters=[] # for a single realization of the dynamics num_adopters , seed_shift ,max_shift= set_ic(G,threshold) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) # print "initial number of adopters:", num_adopters list_t.append(0) ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(output_file.split('.dat')[0]+"_indiv_iter"+str(iter)+".dat",'wt') # file4.close() ########################################## # the dynamics starts: t=int(seed_shift)+1 # the first time step is just IC.??? while t<= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) # print 't:',t list_t.append(t) for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: # i look for the shift corresponding to that time step shift_lenght=int(G.node[n]['shift_lenght']) if shift_lenght==2 and n not in list_id_weekends_T3: shift_lenght=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_lenght'] flag_possible_persuasion=0 for doctor in G.neighbors(n): if G.node[doctor]["status"]=="Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion=1 break if flag_possible_persuasion==1: list_doctors=[] for doctor in G.neighbors(n): # for all drs in that shift list_doctors.append(doctor) pairs=itertools.combinations(list_doctors,2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1=pair[0] doctor2=pair[1] if G.node[doctor1]['status'] != G.node[doctor2]['status']: # if they think differently, # there will be persuasion persuasion(G,damping,doctor1,doctor2,alpha_A,alpha_F,threshold,shift_lenght) # i move their values of opinion update_opinions(G,threshold,doctor1,doctor2) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G,mutual_encouragement,doctor1,doctor2,shift_lenght) # else: # print " no persuasion possible during shift (no adopters present)!" list_Adopters=[] #count how many i have at this time for n in G.nodes(): try: if G.node[n]["status"]=="Adopter": if G.node[n]["label"] not in list_Adopters and G.node[n]["type"]=="A": list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute # if for_testing_fixed_set=="YES": # if t==cutting_day: # list_abs_dist_at_cutting_day.append(abs(float(list_actual_evol[-1])-float(len(list_Adopters)))) # print abs(float(list_actual_evol[-1])-float(len(list_Adopters))), float(list_actual_evol[t]),float(len(list_Adopters)) time_evol_number_adopters.append(float(len(list_Adopters))) t+=1 ############## end while loop over t time_evol_number_adopters_ITER.append(time_evol_number_adopters) list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol,time_evol_number_adopters)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1]-list_actual_evol[-1]) ) list_dist_at_ending_point_fixed_parameters.append(time_evol_number_adopters[-1]-list_actual_evol[-1]) list_final_num_adopt.append(time_evol_number_adopters[-1]) ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(output_file.split('.dat')[0]+"_indiv_iter"+str(iter)+".dat",'at') # for i in range(len(time_evol_number_adopters)): #ime step by time step # print >> file4, i,time_evol_number_adopters[i], alpha_F,damping,mutual_encouragement #file4.close() ######################################################## #######################end loop over Niter list_pair_dist_std_delta_end=[] list_pair_dist_std_delta_end.append(numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append(numpy.std(list_dist_fixed_parameters) ) list_pair_dist_std_delta_end.append(numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) if for_testing_fixed_set=="NO": file3 = open(output_file3,'at') # i print out the landscape print >> file3, alpha_F, damping, mutual_encouragement, threshold,numpy.mean(list_dist_abs_at_ending_point_fixed_parameters), numpy.mean(list_dist_fixed_parameters), numpy.mean(list_final_num_adopt),numpy.std(list_final_num_adopt), numpy.std(list_final_num_adopt)/numpy.mean(list_final_num_adopt) file3.close() if (numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[output_file]=list_pair_dist_std_delta_end file = open(output_file,'wt') for i in range(len(time_evol_number_adopters)): #time step by time step list_fixed_t=[] for iteracion in range (Niter): #loop over all independent iter of the process list_fixed_t.append(time_evol_number_adopters_ITER[iteracion][i]) # i collect all values for the same t, different iter print >> file, list_t[i],numpy.mean(list_fixed_t),numpy.std(list_fixed_t), alpha_F,damping,mutual_encouragement file.close() print "printed out: ",output_file if envelopes=="YES": calculate_envelope_set_curves.calculate_envelope(time_evol_number_adopters_ITER,percent_envelope,"Persuasion",[alpha_F,damping,mutual_encouragement,threshold]) if for_testing_fixed_set=="YES": num_valid_endings=0. for item in list_dist_abs_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings+=1. print "average distance of the optimum in the testing segment:",numpy.mean(list_dist_fixed_parameters),numpy.std(list_dist_fixed_parameters),list_dist_fixed_parameters,"\n" print "fraction of realizations that end within delta_doctor:",num_valid_endings/Niter,"mean ending dist:",numpy.mean(list_dist_at_ending_point_fixed_parameters), "SD final dist",numpy.std(list_dist_at_ending_point_fixed_parameters),list_dist_at_ending_point_fixed_parameters histogram_filename="../Results/weight_shifts/histogr_raw_distances_ending_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_alphaA_eq_alphaF_day"+str(cutting_day)+".dat" histograma_gral_negv_posit.histograma(list_dist_at_ending_point_fixed_parameters, histogram_filename) histogram_filename2="../Results/weight_shifts/histogr_sum_dist_traject_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_alphaA_eq_alphaF_day"+str(cutting_day)+".dat" histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,Nbins,histogram_filename2) output_file10="../Results/weight_shifts/Summary_results_train_test_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_alphaA_eq_alphaF_day"+str(cutting_day)+".dat" file10 = open(output_file10,'wt') print >> file10, "Summary results from train-testing infection with",Niter, "iter, and with values for the parameters: alpha ",alpha_F," damping: ",damping," mutual_encourg: ",mutual_encouragement," threshold:",threshold print >> file10, "average distance of the optimum in the testing segment:",numpy.mean(list_dist_fixed_parameters),numpy.std(list_dist_fixed_parameters),list_dist_fixed_parameters print >> file10, "fraction of realizations that end within delta_doctor:",num_valid_endings/Niter,"mean ending dist:",numpy.mean(list_dist_at_ending_point_fixed_parameters), "SD final dist",numpy.std(list_dist_at_ending_point_fixed_parameters),list_dist_at_ending_point_fixed_parameters print >> file10, "written optimum train_test evolution file:",output_file print >> file10,"written histogram file: ",histogram_filename print >> file10,"written histogram file: ",histogram_filename2 file10.close() print "written optimum train_test evolution file:",output_file print "written histogram file: ",histogram_filename print "written histogram file: ",histogram_filename2 damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F threshold += delta_threshold if for_testing_fixed_set=="NO": # only if i am exploring the whole landscape, i need to call this function, otherwise, i already know the optimum compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end(dict_filenames_tot_distance,"Persuasion_weight",all_team,Niter,None) #last argument, cutting day (it doesnt apply) if for_testing_fixed_set=="NO": print "written landscape file:",output_file3
def main(graph_name): cutting_day = 125 # to separate training-testing G = nx.read_gml(graph_name) all_team = "NO" # as adopters or not dir_real_data = '../Results/' delta_end = 3 # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) Niter_training = 100 Niter_testing = 100 output_file3 = dir_real_data + "Landscape_parameters_infection_train_test_" + str( Niter_training) + "iter.dat" file3 = open(output_file3, 'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol = [] result_actual_file = csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont = 0 for row in result_actual_file: if cont > 0: # i ignore the first line with the headers num_adopters = row[3] list_actual_evol.append(float(num_adopters)) cont += 1 list_actual_evol_training = list_actual_evol[:cutting_day] list_actual_evol_testing = list_actual_evol[(cutting_day - 1):] ################################################################## #../Results/network_final_schedule_withTeam3/infection/Average_time_evolution_Infection_p0.9_Immune0.5_1000iter_2012.dat prob_min = 0.0 prob_max = 1.001 delta_prob = 0.01 prob_Immune_min = 0.00 prob_Immune_max = 1.001 delta_prob_Immune = 0.01 dir = "../Results/network_final_schedule_withTeam3_local/infection/" dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_list_dict_network_states = { } # i will save the filename as key and the list of networks at cutting day as value prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_training_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str( Niter_training) + "iter_2012_avg_ic_day" + str( cutting_day) + ".dat" file2 = open(output_file2, 'wt') file2.close() # i create the empty list of list for the Niter temporal evolutions num_shifts = 0 num_Drs = 0. for n in G.nodes(): G.node[n]["status"] = "S" if G.node[n]['type'] == "shift": num_shifts += 1 else: num_Drs += 1. # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_final_num_infected = [] list_dict_network_states = [] for iter in range(Niter_training): print " iter:", iter dict_network_states = {} list_I = [] #list infected doctors list_ordering = [] list_s = [] list_A = [] list_F = [] ########### set I.C. max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": list_s.append(n) if G.node[n]['order'] > max_order: max_order = G.node[n]['order'] else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) if G.node[n]['type'] == "A": list_A.append(n) if G.node[n]['type'] == "F": list_F.append(n) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" # print max_order ################# the dynamics starts: t = 1 while t < cutting_day: # loop over shifts, in order just until cutting day (training segment) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" list_I.append( G.node[doctor]["label"]) list_single_t_evolution.append(float( len(list_I))) #/(len(list_A)+len(list_F))) t += 1 ######## end t loop for n in G.nodes(): if G.node[n]['type'] != "shift": dict_network_states[G.node[n] ["label"]] = G.node[n]["status"] list_dict_network_states.append(dict_network_states) list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training, list_single_t_evolution)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol_training[-1]) ) # i save the distance at the ending point between the current simu and actual evol # print "actual:",len(list_actual_evol_training)," simu:",len(list_single_t_evolution) # 125, 125 list_final_num_infected.append(list_single_t_evolution[-1]) ######## end loop Niter for the training fase list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) file3 = open(output_file3, 'at') # i print out the landscape print >> file3, prob_infection, prob_Immune, numpy.mean( list_dist_abs_at_ending_point_fixed_parameters ), numpy.mean(list_dist_fixed_parameters), numpy.mean( list_final_num_infected ), numpy.std(list_final_num_infected), numpy.std( list_final_num_infected) / numpy.mean(list_final_num_infected) file3.close() if ( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end dict_filenames_list_dict_network_states[ output_file2] = list_dict_network_states file2 = open(output_file2, 'at') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter_training): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) file2.close() prob_infection += delta_prob prob_Immune += delta_prob_Immune list_order_dict = compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Infection_training", all_team, Niter_training, cutting_day) # it returns a list of tuples like this : ('../Results/network_final_schedule_withTeam3_local/infection/Average_time_evolution_Infection_training_p0.7_Immune0.0_2iter_2012.dat', [2540.0, 208.0, 1.0]) the best set of parameters being the fist one of the elements in the list. optimum_filename = list_order_dict[0][0] prob_infection = float(list_order_dict[0][0].split("_p")[1].split("_")[0]) prob_Immune = float( list_order_dict[0][0].split("_Immune")[1].split("_")[0]) print "starting testing fase with:" print "p=", prob_infection, " and Pimmune=", prob_Immune # i already know the optimum, now i run the dynamics with those values, starting from the average state on the cutting point, and test: list_dist_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_dist_at_ending_point_fixed_parameters = [] list_lists_t_evolutions = [] lista_num_infect = [] lista_I_drs = [] dict_tot_I_doctors = {} lista_num_imm = [] lista_Imm_drs = [] dict_tot_Imm_doctors = {} for dictionary in dict_filenames_list_dict_network_states[ optimum_filename]: # dictionary={Dr1:status, Dr2:status,} # one dict per iteration num_I = 0. num_Imm = 0. for key in dictionary: if dictionary[key] == "I": num_I += 1. if key not in lista_I_drs: lista_I_drs.append(key) dict_tot_I_doctors[key] = 1. else: dict_tot_I_doctors[key] += 1. elif dictionary[key] == "Immune": num_Imm += 1. if key not in lista_Imm_drs: lista_Imm_drs.append(key) dict_tot_Imm_doctors[key] = 1. else: dict_tot_Imm_doctors[key] += 1. lista_num_infect.append(num_I) lista_num_imm.append(num_Imm) avg_inf_drs = int( numpy.mean(lista_num_infect)) # i find out the average num I print "I", numpy.mean(lista_num_infect), numpy.mean( lista_num_infect) / num_Drs, avg_inf_drs, numpy.std(lista_num_infect) if numpy.mean(lista_num_infect ) - avg_inf_drs >= 0.5: # correccion de truncamiento avg_inf_drs += 1.0 # print avg_inf_drs avg_imm_drs = int( numpy.mean(lista_num_imm)) # i find out the average num Immune print "Imm", numpy.mean(lista_num_imm), numpy.mean( lista_num_imm) / num_Drs, avg_imm_drs, numpy.std(lista_num_imm) if numpy.mean( lista_num_imm) - avg_imm_drs >= 0.5: # correccion de truncamiento avg_imm_drs += 1.0 # print avg_imm_drs # i sort the list from more frequently infected to less list_sorted_dict = sorted(dict_tot_I_doctors.iteritems(), key=operator.itemgetter(1)) new_list_sorted_dict = list_sorted_dict new_list_sorted_dict.reverse() print "I:", new_list_sorted_dict # i sort the list from more frequently imm to less list_sorted_dict_imm = sorted(dict_tot_Imm_doctors.iteritems(), key=operator.itemgetter(1)) new_list_sorted_dict_imm = list_sorted_dict_imm new_list_sorted_dict_imm.reverse() print "Immunes:", new_list_sorted_dict_imm # raw_input() #list_sorted_dict=[(u'Weiss', 5.0), (u'Wunderink', 5.0), (u'Keller', 4.0), (u'Go', 3.0), (u'Cuttica', 3.0), (u'Rosario', 2.0), (u'Radigan', 2.0), (u'Smith', 2.0), (u'RosenbergN', 2.0), (u'Gillespie', 1.0), (u'Osher', 1.0), (u'Mutlu', 1.0), (u'Dematte', 1.0), (u'Hawkins', 1.0), (u'Gates', 1.0)] lista_avg_I_drs = [ ] # i create the list of Drs that on average are most likely infected by the cutting day i = 1 for item in new_list_sorted_dict: if (item[0] not in lista_avg_I_drs) and (i <= avg_inf_drs): lista_avg_I_drs.append(item[0]) i += 1 print lista_avg_I_drs, len(lista_avg_I_drs), float( len(lista_avg_I_drs)) / num_Drs lista_avg_Imm_drs = [ ] # i create the list of Drs that on average are most likely immune by the cutting day i = 1 for item in new_list_sorted_dict_imm: if (item[0] not in lista_avg_Imm_drs) and (i <= avg_imm_drs) and ( item[0] not in lista_avg_I_drs): lista_avg_Imm_drs.append(item[0]) i += 1 print lista_avg_Imm_drs, len(lista_avg_Imm_drs), float( len(lista_avg_Imm_drs)) / num_Drs # raw_input() for iter in range(Niter_testing): # i establish the initial conditions (as the average of the cutting point) list_I = [] #list infected doctors list_Immune = [] for node in G.nodes(): if G.node[node]['type'] != "shift": label = G.node[node]['label'] G.node[node]["status"] = "S" #by default, all are susceptible if label in lista_avg_I_drs: G.node[node]["status"] = "I" list_I.append(label) elif label in lista_avg_Imm_drs: G.node[node]["status"] = "Immune" list_Immune.append(label) if label in lista_avg_I_drs and label in lista_avg_Imm_drs: print label, "is in the top most infected AND immune!" raw_input() print "# I at the beginning of the testing fase:", len(list_I), float( len(list_I)) / num_Drs, " and # Immune:", len(list_Immune), float( len(list_Immune)) / num_Drs # print " iter:",iter, len(list_I) list_single_t_evolution = [] list_single_t_evolution.append(len(list_I)) t = cutting_day while t <= max_order: # loop over shifts, in order just until cutting day (training segment) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n]['order'] == t: flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" list_I.append(G.node[doctor]["label"]) list_single_t_evolution.append(float(len(list_I))) # print t, len(list_I) t += 1 list_lists_t_evolutions.append(list_single_t_evolution) list_I = [] list_Immune = [] for node in G.nodes(): if G.node[node]['type'] != "shift": label = G.node[node]['label'] if G.node[node]["status"] == "I": list_I.append(label) elif G.node[node]["status"] == "Immune": list_Immune.append(label) print " # I at the END of the testing fase:", len(list_I), float( len(list_I)) / num_Drs, " and # Immune:", len(list_Immune), float( len(list_Immune)) / num_Drs, "\n" list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing, list_single_t_evolution)) # print " dist:",list_dist_fixed_parameters[-1] list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol_testing[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1] - list_actual_evol_testing[-1] ) # i save the distance at the ending point between the current simu and actual evol ############### end loop Niter for the testing num_valid_endings = 0. for item in list_dist_abs_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings += 1. print "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters), list_dist_fixed_parameters print "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter_testing, list_dist_abs_at_ending_point_fixed_parameters histograma_gral_negv_posit.histograma( list_dist_at_ending_point_fixed_parameters, "../Results/histogr_raw_distances_ending_test_train_infection_p" + str(prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str(Niter_training) + "iter_avg_ic_day" + str(cutting_day) + ".dat") output_file8 = "../Results/List_tot_distances_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_avg_ic_day" + str(cutting_day) + ".dat" file8 = open(output_file8, 'wt') for item in list_dist_fixed_parameters: print >> file8, item file8.close() output_file9 = "../Results/List_distances_ending_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_avg_ic_day" + str(cutting_day) + ".dat" file9 = open(output_file9, 'wt') for item in list_dist_abs_at_ending_point_fixed_parameters: print >> file9, item file9.close() output_file5 = dir + "Average_time_evolution_Infection_testing_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_testing) + "iter_2012_avg_ic_day" + str(cutting_day) + ".dat" file5 = open(output_file5, 'wt') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter_testing): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file5, s + cutting_day, numpy.mean(list_fixed_t) # print s+cutting_day,numpy.mean(list_fixed_t) file5.close() print "written training segment file:", optimum_filename print "written testing segment file:", output_file5 print "printed out landscape file:", output_file3 output_file10 = "../Results/Summary_results_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_avg_ic_day" + str(cutting_day) + ".dat" file10 = open(output_file10, 'wt') print >> file10, "Summary results from train-testing persuasion with", Niter_training, Niter_testing, "iter (respectively), using all the individual cutting points as IC, and with values for the parameters: prob_inf ", prob_infection, " prob immune: ", prob_Immune, "\n" print >> file10, "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters), list_dist_fixed_parameters, "\n" print >> file10, "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter_testing, list_dist_abs_at_ending_point_fixed_parameters, "\n" print >> file10, "written training segment file:", optimum_filename print >> file10, "written testing segment file:", output_file5 file10.close()
def main(graph_name): cutting_day=175 # to separate training-testing G = nx.read_gml(graph_name) all_team="NO" # as adopters or not list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) dir_real_data='../Results/' Nbins=20 # for the histogram of sum of distances delta_end=3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) Niter=1000 fixed_param=""#FIXED_Pimm0_" # or "" # for the Results file that contains the sorted list of best parameters ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol="../Results/Actual_evolution_adopters_from_inference.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters= float(line.split("\t")[1]) list_actual_evol.append(num_adopters) list_actual_evol_training=list_actual_evol[:cutting_day] # list_actual_evol_testing=list_actual_evol[(cutting_day-1):] # i dont need this one ################################################################## prob_min=0.10 prob_max=1.01 delta_prob=0.1 prob_Immune_min=0.0 prob_Immune_max=1.001 delta_prob_Immune=0.1 # threshold is not personal, and set randomly to a value (0,1) # of a single encounter with an infected (it cant be zero or it doesnt make sense!) dose_min=0.05 #infect_threshold_min dose_max=1.001 #######infect_threshold_min/10. delta_dose=0.05 ##infect_threshold_min/10. dir="../Results/weight_shifts/infection/" dict_filenames_tot_distance={} # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_prod_distances={} prob_Immune=prob_Immune_min while prob_Immune<= prob_Immune_max: print "prom Immune:",prob_Immune prob_infection=prob_min while prob_infection<= prob_max: print " p:",prob_infection dose=dose_min while dose <= dose_max: print " dose:",dose output_file2=dir+"Average_time_evolution_Infection_memory_training_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_threshold_from_distrib_dose"+str(dose)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred.dat" # file2 = open(output_file2,'wt') # file2.close() list_lists_t_evolutions=[] # i create the empty list of list for the Niter temporal evolutions list_dist_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_dist_abs_at_ending_point_fixed_parameters=[] for iter in range(Niter): # print " iter:",iter ########### set I.C. list_I=[] #list infected doctors max_order=0 for n in G.nodes(): G.node[n]["status"]="S" # all nodes are Susceptible G.node[n]["infec_value"]=0. G.node[n]["personal_threshold"]=random.random() # for a dr to become infected if G.node[n]['type']=="shift": if G.node[n]['order']>max_order: max_order=G.node[n]['order'] # to get the last shift-order for the time loop else: if G.node[n]['label']=="Wunderink" or G.node[n]["label"]=="Weiss": G.node[n]["status"]="I" G.node[n]["infec_value"]=G.node[n]["personal_threshold"]+ 1. list_I.append(G.node[n]['label']) list_single_t_evolution=[] list_single_t_evolution.append(2.0) # I always start with TWO infected doctors!! for n in G.nodes(): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type']=="A") or ( G.node[n]['type']=="F"): if G.node[n]['label']!="Wunderink" and G.node[n]["label"]!="Weiss": rand=random.random() if rand< prob_Immune: G.node[n]["status"]="Immune" ################# the dynamics starts: t=1 while t< cutting_day: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: shift_length=int(G.node[n]['shift_length']) if shift_length==2 and n not in list_id_weekends_T3: shift_length=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) flag_possible_infection=0 for doctor in G.neighbors(n): #first i check if any doctor is infected in this shift if G.node[doctor]["status"]=="I": flag_possible_infection=1 if flag_possible_infection: for doctor in G.neighbors(n): # then the doctors in that shift, gets infected with prob_infection for i in range(shift_length): # i repeat the infection process several times, to acount for shift lenght if G.node[doctor]["status"]=="S": rand=random.random() if rand<prob_infection: # with prob p the infection occurres G.node[doctor]["infec_value"]+=dose # and bumps the infection_value of that susceptible dr if G.node[doctor]["infec_value"]>= G.node[doctor]["personal_threshold"]: # the threshold for infection is personal G.node[doctor]["status"]="I" list_I.append(G.node[doctor]["label"]) list_single_t_evolution.append(float(len(list_I))) t+=1 ######## end t loop list_lists_t_evolutions.append(list_single_t_evolution) #print "actual:",len(list_actual_evol_training)," simu:",len(list_single_t_evolution) list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training,list_single_t_evolution)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1]-list_actual_evol_training[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1]-list_actual_evol_training[-1]) # i save the distance at the ending point between the current simu and actual evol ######## end loop Niter for the training fase list_pair_dist_std_delta_end=[] list_pair_dist_std_delta_end.append(numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append(numpy.std(list_dist_fixed_parameters) ) list_pair_dist_std_delta_end.append(numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) value=numpy.mean(list_dist_fixed_parameters) *numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)# if SD=0, it is a problem, because then that is the minimun value, but not the optimum i am looking for!! dict_filenames_prod_distances[output_file2]= value if (numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[output_file2]=list_pair_dist_std_delta_end histogram_filename="../Results/weight_shifts/histogr_raw_distances_ending_infection_memory_training_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_threshold_from_distrib_dose"+str(dose)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred.dat" histograma_gral_negv_posit.histograma(list_dist_at_ending_point_fixed_parameters,histogram_filename) histogram_filename2="../Results/weight_shifts/histogr_sum_dist_traject_infection_memory_training_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_threshold_from_distrib_dose"+str(dose)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred.dat" histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,Nbins,histogram_filename2) print "written histogram file: ",histogram_filename print "written histogram file: ",histogram_filename2 dose+= delta_dose prob_infection+= delta_prob prob_Immune+= delta_prob_Immune string_name="infection_memory_training_"+fixed_param+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred.dat" # for the "Results" file with the sorted list of files list_order_dict= compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end(dict_filenames_tot_distance,string_name,Niter,cutting_day) # it returns a list of tuples like this : ('../Results/network_final_schedule_withTeam3_local/infection/Average_time_evolution_Infection_training_p0.7_Immune0.0_2iter_2012.dat', [2540.0, 208.0, 1.0]) the best set of parameters being the fist one of the elements in that list. list_order_dict2= compare_real_evol_vs_simus_to_be_called.pick_minimum_prod_distances(dict_filenames_prod_distances,string_name,Niter,cutting_day) prob_infection=float(list_order_dict[0][0].split("_p")[1].split("_")[0]) prob_Immune=float(list_order_dict[0][0].split("_Immune")[1].split("_")[0]) dose=float(list_order_dict[0][0].split("_dose")[1].split("_")[0]) print "\nOptimum parameters (old method) at day",cutting_day," are: p=",prob_infection," Pimmune=",prob_Immune," infection threshold from distribution, and dose=",dose # optimum_filename=list_order_dict2[0][0] prob_infection=float(list_order_dict2[0][0].split("_p")[1].split("_")[0]) prob_Immune=float(list_order_dict2[0][0].split("_Immune")[1].split("_")[0]) dose=float(list_order_dict2[0][0].split("_dose")[1].split("_")[0]) print "Optimum parameters (product of distances and SDs) at day",cutting_day," are: p=",prob_infection," Pimmune=",prob_Immune," infection threshold from distribution, and dose=",dose
def main(graph_name): G = nx.read_gml(graph_name) list_id_weekends_T3 = look_for_T3_weekends( G ) # T3 doesnt share fellows in the weekend (but they are the exception) percent_envelope = 95. Niter = 1001 cutting_day = 175 min_sum_dist = 20 # to compute number of realizations that have a sum of distances smaller than this delta_end = 3. # >= than + or - dr difference at the end of the evolution dir_real_data = '../Results/' ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol = "../Data/Actual_evolution_adopters_NO_fellows_only_attendings_with_list_names.csv" # "../Results/Actual_evolution_adopters_from_inference.dat" file1 = open( filename_actual_evol, 'r' ) ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file = file1.readlines() dict_days_list_empirical_att_adopters = {} list_actual_evol = [] for line in list_lines_file: # [1:]: # i exclude the first row day = int(line.split(" ")[0]) num_adopters = float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_current_att_adopters = [] for element in line.split( " " )[2:]: # i need to ignore the empty columns from the original datafile if element: if element != '\n': list_current_att_adopters.append( element.strip('\n').title()) dict_days_list_empirical_att_adopters[day] = list_current_att_adopters list_actual_evol_testing = list_actual_evol[cutting_day:] ################################################################## alpha_F_min = 0.10 alpha_F_max = 0.101 delta_alpha_F = 0.10 #AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT! min_damping = 0.00 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping = 0.01 delta_damping = 0.10 min_mutual_encouragement = 0.00 # when two Adopters meet, they convince each other even more max_mutual_encouragement = 0.01 delta_mutual_encouragement = 0.10 threshold_min = 0.50 # larger than, to be an Adopter threshold_max = 0.501 # KEEP THIS FIXED VALUES FOR NOW delta_threshold = 0.10 # AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT print "\n\nPersuasion process on network, with Niter:", Niter threshold = threshold_min while threshold <= threshold_max: print "thershold:", threshold alpha_F = alpha_F_min while alpha_F <= alpha_F_max: # i explore all the parameter space, and create a file per each set of valuesllkl alpha_A = 1.0 * alpha_F print " alpha_F:", alpha_F mutual_encouragement = min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:", mutual_encouragement damping = min_damping while damping <= max_damping: print " damping:", damping dir = "../Results/weight_shifts/persuasion/alpha%.2f_damping%.2f/" % ( alpha_F, damping) ########## i read the list of frequent adopters from simulations, to estimate the ic for fellows filename_list_simu_adopt = "../Results/weight_shifts/persuasion/List_adopters_fellows_descending_frequency_persuasion_training_alpha" + str( alpha_F) + "_damping" + str( damping) + "_mutual_encourg" + str( mutual_encouragement) + "_threshold" + str( threshold) + "_1000iter_" + str( cutting_day) + "_Att_only_middle.dat" file_list_simu_adopt = open(filename_list_simu_adopt, 'r') list_lines_file = file_list_simu_adopt.readlines() list_sorted_fellow_adopters = [] for line in list_lines_file[1:]: # i exclude the first row adopter = line.split(" ")[0] list_sorted_fellow_adopters.append(adopter) # print "list sorted fellows:",list_sorted_fellow_adopters num_simu_Fellow_adopters_cutting_day = int( round( float(list_lines_file[0].split("Avg # F adopters ") [1].split(" ")[0]))) # print "avg simu number Fellow adopters:",num_simu_Fellow_adopters_cutting_day,int( round(num_simu_Fellow_adopters_cutting_day)) list_dist_fixed_parameters_testing_segment = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_dist_at_ending_point_fixed_parameters = [] list_final_num_adopt = [] list_abs_dist_point_by_point_indiv_simus_to_actual = [] list_dist_point_by_point_indiv_simus_to_actual = [] list_small_values = [] for iter in range(Niter): # print " ",iter num_realizations_sum_dist_small = 0. time_evol_number_adopters = [ ] # for a single realization of the dynamics num_adopters, max_shift = set_ic( G, threshold, cutting_day, dict_days_list_empirical_att_adopters, list_sorted_fellow_adopters, num_simu_Fellow_adopters_cutting_day) time_evol_number_adopters.append(float(num_adopters)) old_num_adopters = num_adopters # the dynamics starts: shift_length = 5 #i know the first shift (order 0) is of length 5 t = cutting_day while t <= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) # print 't:',t for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: # i look for the shift corresponding to that time step shift_length = int( G.node[n]['shift_length']) if shift_length == 2 and n not in list_id_weekends_T3: shift_length = 1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_length'] flag_possible_persuasion = 0 for doctor in G.neighbors(n): if G.node[doctor][ "status"] == "Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion = 1 break if flag_possible_persuasion == 1: list_doctors = [] for doctor in G.neighbors( n ): # for all drs in that shift list_doctors.append(doctor) pairs = itertools.combinations( list_doctors, 2 ) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1 = pair[0] doctor2 = pair[1] if G.node[doctor1][ 'status'] != G.node[doctor2][ 'status']: # if they think differently, # there will be persuasion persuasion( G, damping, doctor1, doctor2, alpha_A, alpha_F, threshold, shift_length ) # i move their values of opinion update_opinions( G, threshold, doctor1, doctor2 ) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement( G, mutual_encouragement, doctor1, doctor2, shift_length) # else: # print " no persuasion possible during shift (no adopters present)!" list_Adopters = [] for n in G.nodes(): try: if G.node[n]["status"] == "Adopter": if G.node[n][ "label"] not in list_Adopters and G.node[ n]["type"] == "A": list_Adopters.append( G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute new_num_adopters = len(list_Adopters) if shift_length == 5: # i estimate that adoption happens in the middle of the shift if t + 5 < max_shift: time_evol_number_adopters.append( old_num_adopters) if t + 4 < max_shift: time_evol_number_adopters.append( old_num_adopters) if t + 3 < max_shift: time_evol_number_adopters.append( new_num_adopters) if t + 2 < max_shift: time_evol_number_adopters.append( new_num_adopters) if t + 1 < max_shift: time_evol_number_adopters.append( new_num_adopters) t += 5 elif shift_length == 4: if t + 4 < max_shift: time_evol_number_adopters.append( old_num_adopters) if t + 3 < max_shift: time_evol_number_adopters.append( old_num_adopters) if t + 2 < max_shift: time_evol_number_adopters.append( new_num_adopters) if t + 1 < max_shift: time_evol_number_adopters.append( new_num_adopters) t += 4 elif shift_length == 3: if t + 3 < max_shift: time_evol_number_adopters.append( old_num_adopters) if t + 2 < max_shift: time_evol_number_adopters.append( new_num_adopters) if t + 1 < max_shift: time_evol_number_adopters.append( new_num_adopters) t += 3 elif shift_length == 2: if t + 2 < max_shift: time_evol_number_adopters.append( old_num_adopters) if t + 1 < max_shift: time_evol_number_adopters.append( new_num_adopters) t += 2 elif shift_length == 1: if t + 1 < max_shift: time_evol_number_adopters.append( new_num_adopters) t += 1 old_num_adopters = new_num_adopters ############## end while loop over t dist = compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing, time_evol_number_adopters) if dist < min_sum_dist: list_small_values.append(dist) #######################end loop over Niter print "fraction realizations with sum distances smaller than", min_sum_dist, "is ", float( len(list_small_values)) / float( Niter), "namelly:", list_small_values damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F threshold += delta_threshold
def main(graph_name): G = nx.read_gml(graph_name) list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) percent_envelope=95. Niter=1000 cutting_day=125 min_sum_dist=20 # to compute number of realizations that have a sum of distances smaller than this Nbins=200 # for the histogram of sum of distances envelopes="NO" delta_end=3. # >= than + or - dr difference at the end of the evolution dir_real_data='../Results/' ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol="../Data/Attendings_Orders_from_inference_list_adopters_day.dat" # "../Results/Actual_evolution_adopters_from_inference.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() dict_days_list_empirical_adopters={} list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row day=int(line.split(" ")[0]) num_adopters= float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_current_adopters=[] for element in line.split(" ")[2:]: # i need to ignore the empty columns from the original datafile if element: if element != '\n': list_current_adopters.append(element.strip('\n')) dict_days_list_empirical_adopters[day]=list_current_adopters list_actual_evol_testing=list_actual_evol[cutting_day:] ################################################################## #../Results/weight_shifts/persuasion/Time_evolutions_Persuasion_training_alpha0.5_damping0.4_mutual_encourg0.5_threshold0.5_unif_distr_1000iter_2012_seed31Oct_finalnetwork_day125.dat #OJO!!! NECESITO DOS DECIMALES SIEMPRE, PARA QUE CUADRE CON EL NOMBRE DE LOS SUB-DIRECTORIOS DONDE LO GUARDO alpha_F_min=0.50 #0.15 # alpha=0: nobody changes their mind alpha_F_max=0.501 #0.351 delta_alpha_F=0.10 #AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT! min_damping=0.600 #0.0 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping=0.601 #0.451 delta_damping=0.10 min_mutual_encouragement=0.40 #0.50 # when two Adopters meet, they convince each other even more max_mutual_encouragement=0.401 # 0.51 # KEEP THIS FIXED VALUES FOR NOW delta_mutual_encouragement=0.10 threshold_min=0.50 #0.50 # larger than, to be an Adopte threshold_max=0.501 # 0.51 # KEEP THIS FIXED VALUES FOR NOW delta_threshold=0.10 # AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT print "\n\nPersuasion process on network, with Niter:",Niter threshold=threshold_min while threshold<= threshold_max: print "thershold:",threshold alpha_F=alpha_F_min while alpha_F<= alpha_F_max: # i explore all the parameter space, and create a file per each set of valuesllkl alpha_A=1.0*alpha_F print " alpha_F:",alpha_F mutual_encouragement=min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:",mutual_encouragement damping=min_damping while damping <= max_damping: print " damping:",damping dir="../Results/weight_shifts/persuasion/alpha%.2f_damping%.2f/" % (alpha_F, damping ) output_file=dir+"Time_evol_Persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" file = open(output_file,'wt') file.close() time_evol_number_adopters_ITER=[] # list of complete single realizations of the dynamics list_dist_fixed_parameters_testing_segment=[] list_dist_abs_at_ending_point_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_final_num_adopt=[] list_abs_dist_point_by_point_indiv_simus_to_actual=[] list_dist_point_by_point_indiv_simus_to_actual=[] #list_abs_dist_at_cutting_day=[] for iter in range(Niter): # print " ",iter num_realizations_sum_dist_small=0. time_evol_number_adopters=[] # for a single realization of the dynamics num_adopters , max_shift= set_ic(G,threshold,cutting_day,dict_days_list_empirical_adopters) time_evol_number_adopters.append(float(num_adopters)) old_num_adopters=num_adopters # the dynamics starts: shift_length=5 #i know the first shift (order 0) is of length 5 t=cutting_day while t<= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) # print 't:',t for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: # i look for the shift corresponding to that time step shift_length=int(G.node[n]['shift_length']) if shift_length==2 and n not in list_id_weekends_T3: shift_length=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_length'] flag_possible_persuasion=0 for doctor in G.neighbors(n): if G.node[doctor]["status"]=="Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion=1 break if flag_possible_persuasion==1: list_doctors=[] for doctor in G.neighbors(n): # for all drs in that shift list_doctors.append(doctor) pairs=itertools.combinations(list_doctors,2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1=pair[0] doctor2=pair[1] if G.node[doctor1]['status'] != G.node[doctor2]['status']: # if they think differently, # there will be persuasion persuasion(G,damping,doctor1,doctor2,alpha_A,alpha_F,threshold,shift_length) # i move their values of opinion update_opinions(G,threshold,doctor1,doctor2) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G,mutual_encouragement,doctor1,doctor2,shift_length) # else: # print " no persuasion possible during shift (no adopters present)!" list_Adopters=[] for n in G.nodes(): try: if G.node[n]["status"]=="Adopter": if G.node[n]["label"] not in list_Adopters :#and G.node[n]["type"]=="A": list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute new_num_adopters=len(list_Adopters) if shift_length==5: # i estimate that adoption happens in the middle of the shift if t+5 < max_shift: time_evol_number_adopters.append(old_num_adopters) if t+4 < max_shift: time_evol_number_adopters.append(old_num_adopters) if t+3 < max_shift: time_evol_number_adopters.append(new_num_adopters) if t+2 < max_shift: time_evol_number_adopters.append(new_num_adopters) if t+1 < max_shift: time_evol_number_adopters.append(new_num_adopters) t+=5 elif shift_length==4: if t+4 < max_shift: time_evol_number_adopters.append(old_num_adopters) if t+3 < max_shift: time_evol_number_adopters.append(old_num_adopters) if t+2 < max_shift: time_evol_number_adopters.append(new_num_adopters) if t+1 < max_shift: time_evol_number_adopters.append(new_num_adopters) t+=4 elif shift_length==3: if t+3 < max_shift: time_evol_number_adopters.append(old_num_adopters) if t+2 < max_shift: time_evol_number_adopters.append(new_num_adopters) if t+1 < max_shift: time_evol_number_adopters.append(new_num_adopters) t+=3 elif shift_length==2: if t+2 < max_shift: time_evol_number_adopters.append(old_num_adopters) if t+1 < max_shift: time_evol_number_adopters.append(new_num_adopters) t+=2 elif shift_length==1: if t+1 < max_shift: time_evol_number_adopters.append(new_num_adopters) t+=1 old_num_adopters=new_num_adopters ############## end while loop over t time_evol_number_adopters_ITER.append(time_evol_number_adopters) # now i only run the testing segment! dist=compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing,time_evol_number_adopters) list_dist_fixed_parameters_testing_segment.append(dist) if dist < min_sum_dist: num_realizations_sum_dist_small+=1 list_dist_abs_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1]-list_actual_evol_testing[-1]) ) list_dist_at_ending_point_fixed_parameters.append(time_evol_number_adopters[-1]-list_actual_evol_testing[-1]) list_final_num_adopt.append(time_evol_number_adopters[-1]) for index in range(len(time_evol_number_adopters)): list_abs_dist_point_by_point_indiv_simus_to_actual.append(abs(time_evol_number_adopters[index]-list_actual_evol_testing[index])) list_dist_point_by_point_indiv_simus_to_actual.append(time_evol_number_adopters[index]-list_actual_evol_testing[index]) #######################end loop over Niter file = open(output_file,'wt') for i in range(len(time_evol_number_adopters)): #time step by time step list_fixed_t=[] for iteracion in range (Niter): #loop over all independent iter of the process list_fixed_t.append(time_evol_number_adopters_ITER[iteracion][i]) # i collect all values for the same t, different iter print >> file,i+cutting_day,numpy.mean(list_fixed_t),numpy.std(list_fixed_t), alpha_F,damping,mutual_encouragement file.close() print "printed out: ",output_file if envelopes=="YES": calculate_envelope_set_curves.calculate_envelope(time_evol_number_adopters_ITER,percent_envelope,"Persuasion",[alpha_F,damping,mutual_encouragement,threshold]) num_valid_endings=0. for item in list_dist_abs_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings+=1. print "average distance of the optimum in the testing segment:",numpy.mean(list_dist_fixed_parameters_testing_segment),numpy.std(list_dist_fixed_parameters_testing_segment),list_dist_fixed_parameters_testing_segment,"\n" print "fraction of realizations that end within delta_doctor:",num_valid_endings/Niter,"mean ending dist:",numpy.mean(list_dist_at_ending_point_fixed_parameters), "SD final dist",numpy.std(list_dist_at_ending_point_fixed_parameters),list_dist_at_ending_point_fixed_parameters histogram_filename="../Results/weight_shifts/histogr_raw_distances_ending_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_gral_negv_posit.histograma(list_dist_at_ending_point_fixed_parameters, histogram_filename) # histogram_filename2="../Results/weight_shifts/histogr_sum_dist_traject_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_alphaA_eq_alphaF_day"+str(cutting_day)+"_A_F_inferred_middle.dat" # histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,Nbins,histogram_filename2) histogram_filename3="../Results/weight_shifts/histogr_sum_dist_testing_segment_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_bines_gral.histograma_bins_zero(list_dist_fixed_parameters_testing_segment,Nbins,histogram_filename3) print min(list_dist_fixed_parameters_testing_segment),max(list_dist_fixed_parameters_testing_segment) histogram_filename4="../Results/weight_shifts/histogr_abs_dist_point_by_point_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_gral_negv_posit.histograma(list_abs_dist_point_by_point_indiv_simus_to_actual, histogram_filename4) histogram_filename5="../Results/weight_shifts/histogr_dist_point_by_point_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_gral_negv_posit.histograma(list_dist_point_by_point_indiv_simus_to_actual, histogram_filename5) output_file10="../Results/weight_shifts/Summary_results_persuasion_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" file10 = open(output_file10,'wt') print >> file10, "Summary results from best fit persuasion with",Niter, "iter, and with values for the parameters: alpha ",alpha_F," damping: ",damping," mutual_encourg: ",mutual_encouragement," threshold:",threshold print >> file10, "average distance of the optimum in the testing segment:",numpy.mean(list_dist_fixed_parameters_testing_segment),numpy.std(list_dist_fixed_parameters_testing_segment),list_dist_fixed_parameters_testing_segment print >> file10, "fraction of realizations that end within delta_doctor:",num_valid_endings/Niter,"mean ending dist:",numpy.mean(list_dist_at_ending_point_fixed_parameters), "SD final dist",numpy.std(list_dist_at_ending_point_fixed_parameters),list_dist_at_ending_point_fixed_parameters print >> file10, "written optimum train_test evolution file:",output_file print >> file10,"written histogram file: ",histogram_filename # print >> file10,"written histogram file: ",histogram_filename2 file10.close() print "written optimum train_test evolution file:",output_file print "written summary file: ",output_file10 damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F threshold += delta_threshold
def main(graph_name): cutting_day = 243 # to separate training-testing Niter = 1000 G = nx.read_gml(graph_name) list_id_weekends_T3 = look_for_T3_weekends( G ) # T3 doesnt share fellows in the weekend (but they are the exception) Nbins = 20 # for the histogram of sum of distances dir_real_data = '../Results/' dir = "../Results/weight_shifts/infection/" delta_end = 3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol = "../Data/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1 = open( filename_actual_evol, 'r' ) ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file = file1.readlines() list_actual_evol = [] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters = float(line.split("\t")[1]) list_actual_evol.append(num_adopters) list_actual_evol_training = list_actual_evol[:cutting_day] # list_actual_evol_testing=list_actual_evol[(cutting_day-1):] #i dont use this ################################################################## #../Results/network_final_schedule_withTeam3/infection/Average_time_evolution_Infection_p0.9_Immune0.5_1000iter_2012.dat prob_min = 1.00 prob_max = 1.00 delta_prob = 0.1 prob_Immune_min = 0.0 prob_Immune_max = 0.0 delta_prob_Immune = 0.1 fixed_param = "" #FIXED_Pimm0_" # or "" # for the Results file that contains the sorted list of best parameters print_landscape = "NO" #NO"#YES" # for the whole exploration print_training_evol = "YES" #NO" # once i know the best fit for the training segment, i run it again to get the curve if print_landscape == "YES": output_file3 = "../Results/weight_shifts/Landscape_parameters_infection_train_" + str( Niter) + "iter_Att_only_middle_day" + str(cutting_day) + ".dat" file3 = open(output_file3, 'wt') file3.close() list_dist_at_ending_point_fixed_parameters = [] dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_prod_distances = {} prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_training_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str( Niter) + "iter_2012_avg_ic_day" + str( cutting_day) + "_Att_only_middle.dat" # file2 = open(output_file2,'wt') I DONT NEED TO WRITE IT, COS I WILL USE THE WHOLE FILE FROM THE WHOLE FIT, WITH THE PARAMETER VALUES THAT THE TESTING-UP-TODAY-125 TELLS ME # file2.close() output_file4 = dir + "List_adopters_fellows_descending_frequency_Infection_training_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str( Niter) + "iter_2012_avg_ic_day" + str( cutting_day) + "_Att_only_middle.dat" num_Att_adopters = 0. num_F_adopters = 0. dict_att_freq_adoption_end = { } # to keep track of what fellow is an adopter at the end (to use along with the real ic) dict_fellow_freq_adoption_end = { } # to keep track of what fellow is an adopter at the end (to use along with the real ic) for n in G.nodes(): doctor = G.node[n]["label"] if G.node[n]['type'] == "F": dict_fellow_freq_adoption_end[doctor] = 0. elif G.node[n]['type'] == "A": dict_att_freq_adoption_end[doctor] = 0. # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_final_num_infected = [] for iter in range(Niter): # print " iter:",iter list_I = [] #list infected doctors list_ordering = [] ########### set I.C. for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": pass else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! old_num_adopters = 2 for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" # print max_order ################# the dynamics starts: shift_length = 5 #i know the first shift (order 0) is of length 5 t = 0 while t < cutting_day: # loop over shifts, in order just until cutting day (training segment) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: shift_length = int(G.node[n]['shift_length']) effective_shift_length = shift_length if shift_length == 2 and n not in list_id_weekends_T3: effective_shift_length = 1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_length'] flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection for i in range(effective_shift_length): if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" if G.node[doctor][ "type"] == "A": list_I.append( G.node[doctor] ["label"]) new_num_adopters = len(list_I) if shift_length == 5: # i estimate that adoption happens in the middle of the shift if t + 5 < cutting_day: list_single_t_evolution.append(old_num_adopters) if t + 4 < cutting_day: list_single_t_evolution.append(old_num_adopters) if t + 3 < cutting_day: list_single_t_evolution.append(new_num_adopters) if t + 2 < cutting_day: list_single_t_evolution.append(new_num_adopters) if t + 1 < cutting_day: list_single_t_evolution.append(new_num_adopters) t += 5 elif shift_length == 4: if t + 4 < cutting_day: list_single_t_evolution.append(old_num_adopters) if t + 3 < cutting_day: list_single_t_evolution.append(old_num_adopters) if t + 2 < cutting_day: list_single_t_evolution.append(new_num_adopters) if t + 1 < cutting_day: list_single_t_evolution.append(new_num_adopters) t += 4 elif shift_length == 3: if t + 3 < cutting_day: list_single_t_evolution.append(old_num_adopters) if t + 2 < cutting_day: list_single_t_evolution.append(new_num_adopters) if t + 1 < cutting_day: list_single_t_evolution.append(new_num_adopters) t += 3 elif shift_length == 2: if t + 2 < cutting_day: list_single_t_evolution.append(old_num_adopters) if t + 1 < cutting_day: list_single_t_evolution.append(new_num_adopters) t += 2 elif shift_length == 1: if t + 1 < cutting_day: list_single_t_evolution.append(new_num_adopters) t += 1 old_num_adopters = new_num_adopters ######## end t loop list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training, list_single_t_evolution)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol_training[-1]) ) # i save the distance at the ending point between the current simu and actual evol # print "actual:",len(list_actual_evol_training)," simu:",len(list_single_t_evolution) # 125, 125 list_final_num_infected.append(list_single_t_evolution[-1]) list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1] - list_actual_evol_training[-1] ) # i save the distance at the ending point between the current simu and actual evol for n in G.nodes(): doctor = G.node[n]["label"] if G.node[n]['type'] != "shift": if G.node[n]['status'] == "I": if G.node[n]['type'] == "F": dict_fellow_freq_adoption_end[doctor] += 1. num_F_adopters += 1. elif G.node[n]['type'] == "A": dict_att_freq_adoption_end[doctor] += 1. num_Att_adopters += 1. ######## end loop Niter for the training fase list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) if print_landscape == "YES": file3 = open(output_file3, 'at') # i print out the landscape print >> file3, prob_infection, prob_Immune, numpy.mean( list_dist_abs_at_ending_point_fixed_parameters ), numpy.mean(list_dist_fixed_parameters), numpy.mean( list_final_num_infected), numpy.std( list_final_num_infected ), numpy.std(list_final_num_infected) / numpy.mean( list_final_num_infected) file3.close() if print_training_evol == "YES": file2 = open(output_file2, 'wt') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) last_adoption_value = numpy.mean(list_fixed_t) file2.close() print "written evolution file:", output_file2 print "\nFraction of times each fellow was an adopter at the end of the training segment:" for n in G.nodes(): if G.node[n]['type'] == "F": doctor = G.node[n]["label"] dict_fellow_freq_adoption_end[ doctor] = dict_fellow_freq_adoption_end[ doctor] / float(Niter) print doctor, "\t", dict_fellow_freq_adoption_end[ doctor] print "(out of", len( dict_fellow_freq_adoption_end), "fellows)\n" sorted_list_tuples = sorted( dict_fellow_freq_adoption_end.iteritems(), key=operator.itemgetter(1), reverse=True) file4 = open(output_file4, 'wt') print >> file4, last_adoption_value, "(value adoption among Att at cutting day)", "Avg # F adopters", num_F_adopters / Niter, "Avg # A adopters", num_Att_adopters / Niter for pair in sorted_list_tuples: print >> file4, pair[0], pair[1] file4.close() print "written adoption frecuency file for fellows:", output_file4 value = numpy.mean(list_dist_fixed_parameters) * numpy.mean( list_dist_abs_at_ending_point_fixed_parameters ) # if SD=0, it is a problem, because then that is the minimun value, but not the optimum i am looking for!! dict_filenames_prod_distances[output_file2] = value if ( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end prob_infection += delta_prob prob_Immune += delta_prob_Immune if print_training_evol == "NO": #if i am exploring the whole space string_name = "infection_training_" + fixed_param + str( Niter ) + "iter_day" + str( cutting_day ) + "_Att_only_middle.dat" # for the "Results" file with the sorted list of files list_order_dict = compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, string_name, Niter, cutting_day) # it returns a list of tuples like this : ('../Results/network_final_schedule_withTeam3_local/infection/Average_time_evolution_Infection_training_p0.7_Immune0.0_2iter_2012.dat', [2540.0, 208.0, 1.0]) the best set of parameters being the fist one of the elements in the list. list_order_dict2 = compare_real_evol_vs_simus_to_be_called.pick_minimum_prod_distances( dict_filenames_prod_distances, string_name, Niter, cutting_day) # i already know the optimum, now i run the dynamics with those values, starting from the average state on the cutting point, and test: optimum_filename = list_order_dict2[0][0] prob_infection = float( list_order_dict2[0][0].split("_p")[1].split("_")[0]) prob_Immune = float( list_order_dict2[0][0].split("_Immune")[1].split("_")[0]) print "Optimum parameters (product of distances along_traject and at the end) at day", cutting_day, " are: p=", prob_infection, " and Pimmune=", prob_Immune print "Run that simulation with the optimum parameter set:", optimum_filename output_file10 = "../Results/weight_shifts/Summary_results_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter) + "iter_avg_ic_day" + str( cutting_day) + "_Att_only_middle.dat" file10 = open(output_file10, 'wt') print >> file10, "Summary results from train-testing persuasion with", Niter, "iter , using all the individual cutting points as IC, and with values for the parameters: prob_inf ", prob_infection, " prob immune: ", prob_Immune, "\n" print >> file10, "Look for the file (or run that simulation) with the optimum parameter set:", optimum_filename file10.close() if print_landscape == "YES": print "printed out landscape file:", output_file3
def dynamics( parameters): prob_infection=parameters[0] prob_Immune=parameters[1] graph_name ="../Results/Doctors_shifts_network_withT3.gml" G = nx.read_gml(graph_name) Niter=10000 # i get the list corresponding to the ACTUAL evolution dir_real_data='../Results/' # filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_SIMPLER.csv" list_actual_evol=[] result_actual_file= csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont=0 for row in result_actual_file: if cont>0: # i ignore the first line with the headers num_adopters= row[3] list_actual_evol.append(float(num_adopters)) cont+=1 ##################3 list_dist_fixed_parameters=[] for i in range(Niter): list_I=[] #list infected doctors list_ordering=[] list_s=[] list_A=[] list_F=[] ########### set I.C. max_order=0 # 243 for n in G.nodes(): G.node[n]["status"]="S" # all nodes are Susceptible if G.node[n]['type']=="shift": list_s.append(n) if G.node[n]['order']>max_order: max_order=G.node[n]['order'] else: if G.node[n]['label']=="Wunderink" or G.node[n]["label"]=="Weiss": G.node[n]["status"]="I" list_I.append(G.node[n]['label']) ######################## WHAT ABOUT SMITH AND SPORN??? if G.node[n]['type']=="A": list_A.append(n) if G.node[n]['type']=="F": list_F.append(n) list_single_t_evolution=[] list_single_t_evolution.append(2.0) # I always start with TWO infected doctors!! for n in G.nodes(): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type']=="A") or ( G.node[n]['type']=="F"): if G.node[n]['label']!="Wunderink" and G.node[n]["label"]!="Weiss": rand=random.random() if rand< prob_Immune: G.node[n]["status"]="Immune" ################# the dynamics starts: t=1 while t<= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: flag_possible_infection=0 for doctor in G.neighbors(n): #first i check if any doctor is infected in this shift if G.node[doctor]["status"]=="I": flag_possible_infection=1 if flag_possible_infection: for doctor in G.neighbors(n): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"]=="S": rand=random.random() if rand<prob_infection: G.node[doctor]["status"]="I" list_I.append(G.node[doctor]["label"]) list_single_t_evolution.append(float(len(list_I)))#/(len(list_A)+len(list_F))) t+=1 ######## end loop time list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol,list_single_t_evolution)) ######## end Niter print prob_infection, prob_Immune, " --> ",numpy.mean(list_dist_fixed_parameters), numpy.std(list_dist_fixed_parameters) return(numpy.mean(list_dist_fixed_parameters))
def main(graph_name): G = nx.read_gml(graph_name) all_team = "NO" # as adopters or not dir_real_data = '../Results/' delta_end = 3 # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) Niter = 1000 list_id_weekends_T3 = look_for_T3_weekends( G ) # T3 doesnt share fellows in the weekend (but they are the exception) output_file3 = "../Results/weight_shifts/Landscape_parameters_infection_memory_distrib_" + str( Niter) + "iter.dat" file3 = open(output_file3, 'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": print "remember that now i use the file of adopters without fellows\n../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" exit() else: filename_actual_evol = "../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1 = open( filename_actual_evol, 'r' ) ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file = file1.readlines() list_actual_evol = [] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters = float(line.split(" ")[1]) list_actual_evol.append(num_adopters) ################################################################################ #../Results/network_final_schedule_withTeam3/infection/Average_time_evolution_Infection_p0.9_Immune0.5_1000iter_2012.dat prob_min = 0.00 prob_max = 1.01 delta_prob = 0.1 prob_Immune_min = 0.0 prob_Immune_max = 1.0 delta_prob_Immune = 0.1 dir = "../Results/weight_shifts/infection/" dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_memory_distrb_dose_thr_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter.dat" file2 = open(output_file2, 'wt') file2.close() num_shifts = 0 for n in G.nodes(): G.node[n]["status"] = "S" G.node[n][ "infec_value"] = 0. # when this value goes over the infect_threshold, the dr is infected G.node[n]["personal_threshold"] = random.random( ) # for a dr to become infected if G.node[n]['type'] == "shift": num_shifts += 1 list_lists_t_evolutions = [ ] # i create the empty list of list for the Niter temporal evolutions list_dist_fixed_parameters = [] list_abs_dist_at_ending_point_fixed_parameters = [] list_final_num_infected = [] for iter in range(Niter): print " iter:", iter list_I = [] #list infected doctors list_ordering = [] list_s = [] ########### set I.C. max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": list_s.append(n) if G.node[n]['order'] > max_order: max_order = G.node[n]['order'] else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" ################# the dynamics starts: t = 1 while t <= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: shift_lenght = int(G.node[n]['shift_lenght']) if shift_lenght == 2 and n not in list_id_weekends_T3: shift_lenght = 1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_lenght'] flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection for i in range( shift_lenght ): # i repeat the infection process several times, to acount for shift lenght if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: # with prob p the infection occurres G.node[doctor][ "infec_value"] += random.random( ) # and bumps the infection_value of that susceptible dr (the dose is random, not fixed) if G.node[doctor][ "infec_value"] >= G.node[ doctor][ "personal_threshold"]: # the threshold for infection is personal G.node[doctor][ "status"] = "I" if G.node[doctor][ "type"] == "A": # fellows participate in the dynamics, but i only consider the attendings as real adopters list_I.append( G.node[doctor] ["label"]) list_single_t_evolution.append(float(len(list_I))) t += 1 ######## end t loop list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol, list_single_t_evolution)) list_abs_dist_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_final_num_infected.append(list_single_t_evolution[-1]) ######## end loop Niter list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_abs_dist_at_ending_point_fixed_parameters)) file3 = open(output_file3, 'at') # i print out the landscape print >> file3, prob_infection, prob_Immune, numpy.mean( list_abs_dist_at_ending_point_fixed_parameters ), numpy.mean(list_dist_fixed_parameters), numpy.mean( list_final_num_infected ), numpy.std(list_final_num_infected), numpy.std( list_final_num_infected) / numpy.mean(list_final_num_infected) file3.close() if ( numpy.mean(list_abs_dist_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end file2 = open(output_file2, 'at') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) file2.close() print list_dist_fixed_parameters # histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,50, "../Results/histogr_distances_indiv_infect_memory_simus_to_the_average_curve_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_threshold"+str(infect_threshold)+"_dose"+str(dose)+"_"+str(Niter)+"iter.dat" ) # Nbins=100 prob_infection += delta_prob prob_Immune += delta_prob_Immune compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Infection_memory_distrib", all_team, Niter, None) print "written landscape file:", output_file3
def dynamics( parameters): alpha_F=parameters[0] damping=parameters[1] mutual_encouragement=parameters[2] threshold=parameters[3] alpha_A=0.5*alpha_F # MAKE THIS ALSO A PARAMETER???? graph_name ="../Results/Doctors_shifts_network_withT3.gml" G = nx.read_gml(graph_name) Niter=10000 # i get the list corresponding to the ACTUAL evolution dir_real_data='../Results/' # filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_SIMPLER.csv" list_actual_evol=[] result_actual_file= csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont=0 for row in result_actual_file: if cont>0: # i ignore the first line with the headers num_adopters= row[3] list_actual_evol.append(float(num_adopters)) cont+=1 ################## list_dist_fixed_parameters=[] for i in range(Niter): list_t=[] time_evol_number_adopters=[] # for a single realization of the dynamics num_adopters , seed_shift ,max_shift= set_ic(G,threshold) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) # print "initial number of adopters:", num_adopters list_t.append(0) # the dynamics starts: t=int(seed_shift)+1 # because the first time step is just IC. while t<= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) list_t.append(t) for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: # i look for the shift corresponding to that time step flag_possible_persuasion=0 for doctor in G.neighbors(n): if G.node[doctor]["status"]=="Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion=1 break if flag_possible_persuasion==1: list_doctors=[] for doctor in G.neighbors(n): # for all drs in that shift list_doctors.append(doctor) pairs=itertools.combinations(list_doctors,2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1=pair[0] doctor2=pair[1] if G.node[doctor1]['status'] != G.node[doctor2]['status']: # if they think differently, # there will be persuasion persuasion(G,damping,doctor1,doctor2,alpha_A,alpha_F,threshold) # i move their values of opinion update_opinions(G,threshold,doctor1,doctor2) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G,mutual_encouragement,doctor1,doctor2) list_Adopters=[] #count how many i have at this time for n in G.nodes(): try: if G.node[n]["status"]=="Adopter": if G.node[n]["label"] not in list_Adopters: list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute time_evol_number_adopters.append(float(len(list_Adopters))) t+=1 ######################### end while loop over t list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol,time_evol_number_adopters)) ######## end Niter print alpha_F, damping, mutual_encouragement,threshold, " --> ", numpy.mean(list_dist_fixed_parameters), numpy.std(list_dist_fixed_parameters) return(numpy.mean(list_dist_fixed_parameters))
def main(graph_name): G = nx.read_gml(graph_name) dir_real_data = '../Results/' Niter = 1000 output_file3 = dir_real_data + "Landscape_parameters_infection_" + str( Niter) + "iter.dat" file3 = open(output_file3, 'wt') ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### #OJO! CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAS!! # filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_SIMPLER.csv" list_actual_evol = [] result_actual_file = csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont = 0 for row in result_actual_file: if cont > 0: # i ignore the first line with the headers num_adopters = row[3] list_actual_evol.append(float(num_adopters)) cont += 1 ################################################################## prob_min = 0.0 prob_max = 1.01 delta_prob = 0.1 prob_Immune_min = 0.0 prob_Immune_max = 1.01 delta_prob_Immune = 0.1 dir = "../Results/network_final_schedule_withTeam3/infection/" dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_2012.dat" file2 = open(output_file2, 'wt') file2.close() # i create the empty list of list for the Niter temporal evolutions num_shifts = 0 for n in G.nodes(): G.node[n]["status"] = "S" if G.node[n]['type'] == "shift": num_shifts += 1 # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] for iter in range(Niter): # print " iter:",iter #######OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file_name_indiv_evol=output_file2.strip("Average_").split('.dat')[0]+"_indiv_iter"+str(iter)+".dat" # file4 = open(file_name_indiv_evol,'wt') # file4.close() ########################################## list_I = [] #list infected doctors list_ordering = [] list_s = [] list_A = [] list_F = [] ########### set I.C. max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": list_s.append(n) if G.node[n]['order'] > max_order: max_order = G.node[n]['order'] else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) ######################## WHAT ABOUT SMITH AND SPORN??? if G.node[n]['type'] == "A": list_A.append(n) if G.node[n]['type'] == "F": list_F.append(n) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" # print max_order ################# the dynamics starts: t = 1 while t <= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" list_I.append( G.node[doctor]["label"]) list_single_t_evolution.append(float( len(list_I))) #/(len(list_A)+len(list_F))) t += 1 ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(file_name_indiv_evol,'at') # for i in range(len(list_single_t_evolution)): #ime step by time step # print >> file4, i,list_single_t_evolution[i], prob_infection, prob_Immune # file4.close() ######################################################## list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol, list_single_t_evolution)) ######## end Niter list_pair_dist_std = [] list_pair_dist_std.append( numpy.mean(list_dist_fixed_parameters )) # average dist between the curves over Niter list_pair_dist_std.append(numpy.std(list_dist_fixed_parameters)) dict_filenames_tot_distance[output_file2] = list_pair_dist_std print >> file3, prob_infection, prob_Immune, dict_filenames_tot_distance[ output_file2][0], dict_filenames_tot_distance[output_file2][1] file2 = open(output_file2, 'at') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) file2.close() # file = open(output_file,'at') #print >> file, prob_infection, numpy.mean(list_final_I_values_fixed_p) #file.close() prob_infection += delta_prob prob_Immune += delta_prob_Immune compare_real_evol_vs_simus_to_be_called.pick_minimum( dict_filenames_tot_distance, "Infection", Niter) file3.close()
def main(graph_name): cutting_day = 175 # to separate training-testing G = nx.read_gml(graph_name) list_id_weekends_T3 = look_for_T3_weekends( G ) # T3 doesnt share fellows in the weekend (but they are the exception) all_team = "NO" # as adopters or not Nbins = 20 # for the histogram of sum of distances dir_real_data = '../Results/' dir = "../Results/weight_shifts/infection/" delta_end = 3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) Niter_training = 1000 fixed_param = "" #"FIXED_Pimm0_" # or "" # for the Results file that contains the sorted list of best parameters output_file3 = "../Results/weight_shifts/Landscape_parameters_infection_train_test_" + str( Niter_training) + "iter.dat" file3 = open(output_file3, 'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": print "remember that now i use the file of adopters without fellows\n../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" exit() else: filename_actual_evol = "../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1 = open( filename_actual_evol, 'r' ) ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file = file1.readlines() list_actual_evol = [] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters = float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_actual_evol_training = list_actual_evol[:cutting_day] # list_actual_evol_testing=list_actual_evol[(cutting_day-1):] #i dont use this ################################################################## #../Results/network_final_schedule_withTeam3/infection/Average_time_evolution_Infection_p0.9_Immune0.5_1000iter_2012.dat prob_min = 0.0 prob_max = 1.01 delta_prob = 0.1 prob_Immune_min = 0.00 prob_Immune_max = 1.01 delta_prob_Immune = 0.1 list_dist_at_ending_point_fixed_parameters = [] dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_prod_distances = {} prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_training_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str( Niter_training) + "iter_2012_avg_ic_day" + str( cutting_day) + ".dat" # file2 = open(output_file2,'wt') I DONT NEED TO WRITE IT, COS I WILL USE THE WHOLE FILE FROM THE WHOLE FIT, WITH THE PARAMETER VALUES THAT THE TESTING-UP-TODAY-125 TELLS ME # file2.close() # i create the empty list of list for the Niter temporal evolutions num_shifts = 0 num_Drs = 0. for n in G.nodes(): G.node[n]["status"] = "S" if G.node[n]['type'] == "shift": num_shifts += 1 else: num_Drs += 1. # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_final_num_infected = [] for iter in range(Niter_training): # print " iter:",iter list_I = [] #list infected doctors list_ordering = [] list_s = [] list_A = [] list_F = [] ########### set I.C. max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": list_s.append(n) if G.node[n]['order'] > max_order: max_order = G.node[n]['order'] else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) if G.node[n]['type'] == "A": list_A.append(n) if G.node[n]['type'] == "F": list_F.append(n) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" # print max_order ################# the dynamics starts: t = 1 while t < cutting_day: # loop over shifts, in order just until cutting day (training segment) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: shift_lenght = int(G.node[n]['shift_lenght']) if shift_lenght == 2 and n not in list_id_weekends_T3: shift_lenght = 1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_lenght'] flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection for i in range(shift_lenght): if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" if G.node[doctor][ "type"] == "A": list_I.append( G.node[doctor] ["label"]) list_single_t_evolution.append(float( len(list_I))) #/(len(list_A)+len(list_F))) t += 1 ######## end t loop list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training, list_single_t_evolution)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol_training[-1]) ) # i save the distance at the ending point between the current simu and actual evol # print "actual:",len(list_actual_evol_training)," simu:",len(list_single_t_evolution) # 125, 125 list_final_num_infected.append(list_single_t_evolution[-1]) list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1] - list_actual_evol_training[-1] ) # i save the distance at the ending point between the current simu and actual evol ######## end loop Niter for the training fase list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) file3 = open(output_file3, 'at') # i print out the landscape print >> file3, prob_infection, prob_Immune, numpy.mean( list_dist_abs_at_ending_point_fixed_parameters ), numpy.mean(list_dist_fixed_parameters), numpy.mean( list_final_num_infected ), numpy.std(list_final_num_infected), numpy.std( list_final_num_infected) / numpy.mean(list_final_num_infected) file3.close() histogram_filename = "../Results/weight_shifts/histogr_raw_distances_ending_test_train_infection_p" + str( prob_infection) + "_Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_day" + str(cutting_day) + ".dat" histograma_gral_negv_posit.histograma( list_dist_at_ending_point_fixed_parameters, histogram_filename) histogram_filename2 = "../Results/weight_shifts/histogr_sum_dist_traject_infection_training_p" + str( prob_infection ) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_day" + str(cutting_day) + ".dat" histograma_bines_gral.histograma_bins(list_dist_fixed_parameters, Nbins, histogram_filename2) print "written histogram file: ", histogram_filename print "written histogram file: ", histogram_filename2 value = numpy.mean(list_dist_fixed_parameters) * numpy.mean( list_dist_abs_at_ending_point_fixed_parameters ) # if SD=0, it is a problem, because then that is the minimun value, but not the optimum i am looking for!! dict_filenames_prod_distances[output_file2] = value if ( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end print numpy.mean(list_dist_abs_at_ending_point_fixed_parameters ), "added scenario:", output_file2 # file2 = open(output_file2,'at') #for s in range(len(list_single_t_evolution)): # list_fixed_t=[] # for iter in range (Niter_training): # list_fixed_t.append(list_lists_t_evolutions[iter][s]) #print >> file2, s,numpy.mean(list_fixed_t) #file2.close() prob_infection += delta_prob prob_Immune += delta_prob_Immune list_order_dict = compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Infection_training_weight", all_team, Niter_training, cutting_day) # it returns a list of tuples like this : ('../Results/network_final_schedule_withTeam3_local/infection/Average_time_evolution_Infection_training_p0.7_Immune0.0_2iter_2012.dat', [2540.0, 208.0, 1.0]) the best set of parameters being the fist one of the elements in the list. string_name = "infection_training_" + fixed_param + str( Niter_training) + "iter_day" + str( cutting_day ) + ".dat" # for the "Results" file with the sorted list of files list_order_dict2 = compare_real_evol_vs_simus_to_be_called.pick_minimum_prod_distances( dict_filenames_prod_distances, string_name, all_team, Niter_training, cutting_day) optimum_filename = list_order_dict[0][0] prob_infection = float(list_order_dict[0][0].split("_p")[1].split("_")[0]) prob_Immune = float( list_order_dict[0][0].split("_Immune")[1].split("_")[0]) print "Optimum parameters (old method) at day", cutting_day, " are: p=", prob_infection, " and Pimmune=", prob_Immune # i already know the optimum, now i run the dynamics with those values, starting from the average state on the cutting point, and test: optimum_filename = list_order_dict2[0][0] prob_infection = float(list_order_dict2[0][0].split("_p")[1].split("_")[0]) prob_Immune = float( list_order_dict2[0][0].split("_Immune")[1].split("_")[0]) print "Optimum parameters (product of distances along_traject and at the end) at day", cutting_day, " are: p=", prob_infection, " and Pimmune=", prob_Immune print "Run that simulation with the optimum parameter set:", optimum_filename print "printed out landscape file:", output_file3 output_file10 = "../Results/weight_shifts/Summary_results_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_avg_ic_day" + str(cutting_day) + ".dat" file10 = open(output_file10, 'wt') print >> file10, "Summary results from train-testing persuasion with", Niter_training, "iter , using all the individual cutting points as IC, and with values for the parameters: prob_inf ", prob_infection, " prob immune: ", prob_Immune, "\n" print >> file10, "Look for the file (or run that simulation) with the optimum parameter set:", optimum_filename file10.close()
def main(graph_name): G = nx.read_gml(graph_name) prob_infection=0.9 prob_Immune=0.5 Niter=100000 dir_real_data='../Results/' all_team="NO" # as adopters or not ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team=="YES": filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol=dir_real_data+"HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol=[] result_actual_file= csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont=0 for row in result_actual_file: if cont>0: # i ignore the first line with the headers num_adopters= row[3] list_actual_evol.append(float(num_adopters)) cont+=1 ################################################################## # i create the empty list of list for the Niter temporal evolutions num_shifts=0 for n in G.nodes(): G.node[n]["status"]="S" if G.node[n]['type']=="shift": num_shifts+=1 # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions=[] for iter in range(Niter): print " iter:",iter list_I=[] #list infected doctors list_ordering=[] list_s=[] list_A=[] list_F=[] ########### set I.C. max_order=0 for n in G.nodes(): G.node[n]["status"]="S" # all nodes are Susceptible if G.node[n]['type']=="shift": list_s.append(n) if G.node[n]['order']>max_order: max_order=G.node[n]['order'] else: if G.node[n]['label']=="Wunderink" or G.node[n]["label"]=="Weiss": G.node[n]["status"]="I" list_I.append(G.node[n]['label']) ######################## WHAT ABOUT SMITH AND SPORN??? if G.node[n]['type']=="A": list_A.append(n) if G.node[n]['type']=="F": list_F.append(n) list_single_t_evolution=[] list_single_t_evolution.append(2.0) # I always start with TWO infected doctors!! for n in G.nodes(): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type']=="A") or ( G.node[n]['type']=="F"): if G.node[n]['label']!="Wunderink" and G.node[n]["label"]!="Weiss": # these particular two cant be immune rand=random.random() if rand< prob_Immune: G.node[n]["status"]="Immune" # print max_order ################# the dynamics starts: t=1 while t<= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: flag_possible_infection=0 for doctor in G.neighbors(n): #first i check if any doctor is infected in this shift if G.node[doctor]["status"]=="I": flag_possible_infection=1 if flag_possible_infection: for doctor in G.neighbors(n): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"]=="S": rand=random.random() if rand<prob_infection: G.node[doctor]["status"]="I" list_I.append(G.node[doctor]["label"]) list_single_t_evolution.append(float(len(list_I)))#/(len(list_A)+len(list_F))) t+=1 list_lists_t_evolutions.append(list_single_t_evolution) ######## end Niter ##############end loop Niter average_t_evolution=[] for i in range(len(list_single_t_evolution)): #time step by time step list_fixed_t=[] for iteracion in range (Niter): #loop over all independent iter of the process list_fixed_t.append(list_lists_t_evolutions[iteracion][i]) # i collect all values for the same t, different iter average_t_evolution.append(numpy.mean(list_fixed_t)) # i create the mean time evolution list_dist_fixed_parameters=[] for lista in list_lists_t_evolutions: list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( lista,average_t_evolution)) lista_tuplas=histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,75, "../Results/histogr_distances_indiv_infect_simus_to_the_average_curve_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_2012.dat") # Nbins=50 #print lista_tuplas starting_point=compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol,average_t_evolution) # distance between actual curve and the mean curve prob=calculate_numeric_integral.integral(lista_tuplas, starting_point) print "the probability of having a distance equal or larger than",starting_point, "between actual-average curve is:", prob, "(it is to say, the prob. of the actual evolution being an individual realization of the Infection Model)" if all_team=="YES": file = open("../Results/distance_actual_to_average_curve_infection_all_team_as_adopters.dat",'wt') else: file = open("../Results/distance_actual_to_average_curve_infection.dat",'wt') print >> file,starting_point, 0. print >> file,starting_point+0.1, 1. file.close() if all_team=="YES": file2 = open("../Results/Results_distance_actual_to_average_curve_infection_all_team_as_adopters.dat",'wt') else: file2 = open("../Results/Results_distance_actual_to_average_curve_infection.dat",'wt') print >> file2, "the probability of having a distance equal or larger than",starting_point, "between actual-average curve is:", prob, "(it is to say, the prob. of the actual evolution being an individual realization of the Infection Model)" file2.close()
def main(graph_name): G = nx.read_gml(graph_name) list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) cutting_day=243 # to separate training-testing Niter=1000 delta_end=3 # >= than + or - dr difference at the end of the evolution dir_real_data='../Results/' dir="../Results/weight_shifts/persuasion/" Nbins=20 # for the histogram of sum of distances ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol="../Data/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters= float(line.split("\t")[1]) list_actual_evol.append(num_adopters) list_actual_evol_training=list_actual_evol[:cutting_day] ################################################################## #../Results/network_final_schedule_withTeam3/Time_evolutions_Persuasion_alpha0.2_damping0.0_mutual_encourg0.7_threshold0.4_unif_distr_50iter_2012_seed31Oct_finalnetwork.dat alpha_F_min=0.100 # # alpha=0: nobody changes their mind alpha_F_max=0.1001 delta_alpha_F=0.10 #AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT! min_damping=0.000 #0.0 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping=0.0001 #0.451 delta_damping=0.10 min_mutual_encouragement=0.300 # # when two Adopters meet, they convince each other even more max_mutual_encouragement=0.301 delta_mutual_encouragement=0.10 threshold_min=0.500 # # larger than, to be an Adopter threshold_max=0.501 delta_threshold=0.10 # AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT fixed_param="FIXED_threshold0.5_damping0_" # or "" # for the Results file that contains the sorted list of best parameters print_landscape="NO"#NO" # for the whole exploration print_training_evol= "YES"#"#YES" # once i know the best fit for the training segment, i run it again to get the curve if print_landscape =="YES": output_file3="../Results/weight_shifts/Landscape_parameters_persuasion_train_"+fixed_param+str(Niter)+"iter_Att_only_middle_day"+str(cutting_day)+".dat" file3 = open(output_file3,'wt') file3.close() print "\n\nPersuasion process on network, with Niter:",Niter dict_filenames_tot_distance={} # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_prod_distances={} threshold=threshold_min while threshold<= threshold_max: print "thershold:",threshold alpha_F=alpha_F_min while alpha_F<= alpha_F_max: # i explore all the parameter space, and create a file per each set of values alpha_A=1.0*alpha_F print " alpha_F:",alpha_F mutual_encouragement=min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:",mutual_encouragement damping=min_damping while damping <= max_damping: print " damping:",damping # dir="../Results/weight_shifts/persuasion/alpha%.2f_damping%.2f/" % (alpha_F, damping ) output_file=dir+"Time_evolutions_Persuasion_training_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_"+str(cutting_day)+"_Att_only_middle.dat" output_file4=dir+"List_adopters_fellows_descending_frequency_persuasion_training_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_"+str(Niter)+"iter_"+str(cutting_day)+"_Att_only_middle.dat" num_Att_adopters=0. num_F_adopters=0. dict_att_freq_adoption_end={} # to keep track of what fellow is an adopter at the end (to use along with the real ic) dict_fellow_freq_adoption_end={} # to keep track of what fellow is an adopter at the end (to use along with the real ic) for n in G.nodes(): doctor=G.node[n]["label"] if G.node[n]['type'] =="F": dict_fellow_freq_adoption_end[doctor]=0. elif G.node[n]['type'] =="A": dict_att_freq_adoption_end[doctor]=0. time_evol_number_adopters_ITER=[] # list of complete single realizations of the dynamics list_dist_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_dist_abs_at_ending_point_fixed_parameters=[] list_networks_at_cutting_day=[] list_final_num_adopt=[] for iter in range(Niter): # print " ",iter time_evol_number_adopters=[] # for a single realization of the dynamics num_adopters , seed_shift ,max_shift= set_ic(G,threshold) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) old_num_adopters=num_adopters ########### the dynamics starts: shift_length=5 #i know the first shift (order 0) is of length 5 t=0 while t< cutting_day: # loop over shifts, in chronological order (the order is the day index since seeding_day) for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: # i look for the shift corresponding to that time step (not all t's exists as 'orders' in the network!! just the days corresponding to the beginning of each shift) shift_length=int(G.node[n]['shift_length']) effective_shift_length=shift_length if shift_length==2 and n not in list_id_weekends_T3: effective_shift_length=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) flag_possible_persuasion=0 for doctor in G.neighbors(n): if G.node[doctor]["status"]=="Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion=1 break if flag_possible_persuasion==1: list_doctors=[] for doctor in G.neighbors(n): # for all drs in that shift list_doctors.append(doctor) pairs=itertools.combinations(list_doctors,2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1=pair[0] doctor2=pair[1] if G.node[doctor1]['status'] != G.node[doctor2]['status']: # if they think differently, # there will be persuasion persuasion(G,damping,doctor1,doctor2,alpha_A,alpha_F,threshold,effective_shift_length) # i move their values of opinion update_opinions(G,threshold,doctor1,doctor2) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G,mutual_encouragement,doctor1,doctor2,shift_length) list_ALL_Adopters=[] list_Adopters=[] for n in G.nodes(): try: if G.node[n]["status"]=="Adopter": if G.node[n]["label"] not in list_Adopters and G.node[n]["type"]=="A": list_Adopters.append(G.node[n]["label"]) list_ALL_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute new_num_adopters=len(list_Adopters) if shift_length==5: # i estimate that adoption happens in the middle of the shift if t+5 < cutting_day: time_evol_number_adopters.append(old_num_adopters) if t+4 < cutting_day: time_evol_number_adopters.append(old_num_adopters) if t+3 < cutting_day: time_evol_number_adopters.append(new_num_adopters) if t+2 < cutting_day: time_evol_number_adopters.append(new_num_adopters) if t+1 < cutting_day: time_evol_number_adopters.append(new_num_adopters) t+=5 elif shift_length==4: if t+4 < cutting_day: time_evol_number_adopters.append(old_num_adopters) if t+3 < cutting_day: time_evol_number_adopters.append(old_num_adopters) if t+2 < cutting_day: time_evol_number_adopters.append(new_num_adopters) if t+1 < cutting_day: time_evol_number_adopters.append(new_num_adopters) t+=4 elif shift_length==3: if t+3 < cutting_day: time_evol_number_adopters.append(old_num_adopters) if t+2 < cutting_day: time_evol_number_adopters.append(new_num_adopters) if t+1 < cutting_day: time_evol_number_adopters.append(new_num_adopters) t+=3 elif shift_length==2: if t+2 < cutting_day: time_evol_number_adopters.append(old_num_adopters) if t+1 < cutting_day: time_evol_number_adopters.append(new_num_adopters) t+=2 elif shift_length==1: if t+1 < cutting_day: time_evol_number_adopters.append(new_num_adopters) t+=1 old_num_adopters=new_num_adopters ############## end while loop over t time_evol_number_adopters_ITER.append(time_evol_number_adopters) list_final_num_adopt.append(time_evol_number_adopters[-1]) list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training,time_evol_number_adopters)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1]-list_actual_evol_training[-1]) ) list_dist_at_ending_point_fixed_parameters.append( time_evol_number_adopters[-1]-list_actual_evol_training[-1]) for n in G.nodes(): doctor= G.node[n]["label"] if G.node[n]['type'] != "shift": if G.node[n]['status'] =="Adopter": if G.node[n]['type'] =="F": dict_fellow_freq_adoption_end[doctor] += 1. num_F_adopters+=1. elif G.node[n]['type'] =="A": dict_att_freq_adoption_end[doctor] += 1. num_Att_adopters+=1. ####################### end loop Niter for the training fase list_pair_dist_std_delta_end=[] list_pair_dist_std_delta_end.append(numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append(numpy.std(list_dist_fixed_parameters) ) list_pair_dist_std_delta_end.append(numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) value=numpy.mean(list_dist_fixed_parameters) *numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) # if SD=0, it is a problem, because then that is the minimun value, but not the optimum i am looking for!! dict_filenames_prod_distances[output_file]= value if (numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[output_file]=list_pair_dist_std_delta_end if print_landscape =="YES": file3 = open(output_file3,'at') # i print out the landscape print >> file3, alpha_F, damping, mutual_encouragement, threshold,numpy.mean(list_dist_abs_at_ending_point_fixed_parameters), numpy.mean(list_dist_fixed_parameters), numpy.mean(list_final_num_adopt),numpy.std(list_final_num_adopt), numpy.std(list_final_num_adopt)/numpy.mean(list_final_num_adopt) file3.close() if print_training_evol=="YES": file = open(output_file,'wt') for i in range(len(time_evol_number_adopters)): #time step by time step list_fixed_t=[] for iteracion in range (Niter): #loop over all independent iter of the process list_fixed_t.append(time_evol_number_adopters_ITER[iteracion][i]) # i collect all values for the same t, different iter print >> file, i,numpy.mean(list_fixed_t),numpy.std(list_fixed_t), alpha_F,damping,mutual_encouragement last_adoption_value=numpy.mean(list_fixed_t) file.close() print "written evolution file:", output_file print "\nFraction of times each fellow was an adopter at the end of the training segment:" for n in G.nodes(): if G.node[n]['type'] =="F": doctor= G.node[n]["label"] dict_fellow_freq_adoption_end[doctor]= dict_fellow_freq_adoption_end[doctor]/float(Niter) print doctor,"\t", dict_fellow_freq_adoption_end[doctor] print "(out of",len(dict_fellow_freq_adoption_end),"fellows)\n" sorted_list_tuples=sorted(dict_fellow_freq_adoption_end.iteritems(), key=operator.itemgetter(1),reverse=True) file4 = open(output_file4,'wt') print >> file4,last_adoption_value, "(value adoption among Att at cutting day)","Avg # F adopters",num_F_adopters/Niter, "Avg # A adopters",num_Att_adopters/Niter for pair in sorted_list_tuples: print >> file4, pair[0], pair[1] file4.close() print "written adoption frecuency file for fellows:", output_file4 damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F threshold += delta_threshold string_name="persuasion_training_"+fixed_param+str(Niter)+"iter_"+str(cutting_day)+"_Att_only_middle.dat" # for the "Results" file with the sorted list of files list_order_dict= compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end(dict_filenames_tot_distance,string_name,Niter,cutting_day) list_order_dict2= compare_real_evol_vs_simus_to_be_called.pick_minimum_prod_distances(dict_filenames_prod_distances,string_name,Niter,cutting_day) if print_landscape =="YES": print "printed out landscape file:",output_file3 print "\n\n"
def main(graph_name): G = nx.read_gml(graph_name) cutting_day=175 # i use this only for the filenames for_testing_fixed_set="YES" # when YES, fixed values param, to get all statistics on final distances etc # change the range for the parameters accordingly envelopes="YES" Niter=1000 # 100 iter seems to be enough (no big diff. with respect to 1000it) percent_envelope=95. list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) Nbins=1000 # for the histogram of sum of distances all_team="NO" # as adopters or not dir_real_data='../Results/' dir="../Results/weight_shifts/infection/" delta_end=3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol="../Data/Attendings_Orders_from_inference_list_adopters_day.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() dict_days_list_empirical_adopters={} list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row day=int(line.split(" ")[0]) num_adopters= float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_current_adopters=[] for element in line.split(" ")[2:]: # i need to ignore the empty columns from the original datafile if element: if element != '\n': list_current_adopters.append(element.strip('\n')) dict_days_list_empirical_adopters[day]=list_current_adopters list_actual_evol_testing=list_actual_evol[cutting_day:] ################################################################## #../Results/weight_shifts/infection/Average_time_evolution_Infection_training_p0.8_Immune0.3_1000iter_2012_avg_ic_day125.dat ESTOS VALORES SON EL OPTIMUM FIT THE 152-DIAS prob_min=0.1 prob_max=0.101 delta_prob=0.1 prob_Immune_min=0.00 prob_Immune_max=0.01 delta_prob_Immune=0.1 prob_Immune=prob_Immune_min while prob_Immune<= prob_Immune_max: print "prom Immune:",prob_Immune prob_infection=prob_min while prob_infection<= prob_max: print " p:",prob_infection output_file2=dir+"Average_time_evolution_Infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" file2 = open(output_file2,'wt') file2.close() list_lists_t_evolutions=[] list_dist_fixed_parameters_testing_segment=[] list_abs_dist_at_ending_point_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_final_num_infected=[] list_abs_dist_point_by_point_indiv_simus_to_actual=[] list_dist_point_by_point_indiv_simus_to_actual=[] for iter in range(Niter): # print " iter:",iter ########### set I.C. according to the empirical data list_I=[] max_order=0 for n in G.nodes(): G.node[n]["status"]="S" # all nodes are Susceptible if G.node[n]['type']=="shift": if G.node[n]['order']>max_order: max_order=G.node[n]['order'] # to get the last shift-order for the time loop else: if G.node[n]['label'] in dict_days_list_empirical_adopters[cutting_day]: G.node[n]["status"]="I" list_I.append(G.node[n]['label']) list_single_t_evolution=[] old_num_adopters=len(dict_days_list_empirical_adopters[cutting_day]) list_single_t_evolution.append(old_num_adopters) # I always start with TWO infected doctors!! for n in G.nodes(): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type']=="A") or ( G.node[n]['type']=="F"): if G.node[n]['label'] not in dict_days_list_empirical_adopters[cutting_day]: rand=random.random() if rand< prob_Immune: G.node[n]["status"]="Immune" ################# the dynamics starts: shift_length=5 #i know the first shift (order 0) is of length 5 t=cutting_day while t<= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: shift_length=int(G.node[n]['shift_length']) if shift_length==2 and n not in list_id_weekends_T3: shift_length=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_length'] flag_possible_infection=0 for doctor in G.neighbors(n): #first i check if any doctor is infected in this shift if G.node[doctor]["status"]=="I": flag_possible_infection=1 if flag_possible_infection: for doctor in G.neighbors(n): # then the doctors in that shift, gets infected with prob_infection for i in range(shift_length): # i repeat the infection process several times, to acount for shift length if G.node[doctor]["status"]=="S": rand=random.random() if rand<prob_infection: G.node[doctor]["status"]="I" # if G.node[doctor]["type"]=="A": # fellows participate in the dynamics, but i only consider the attendings as real adopters list_I.append(G.node[doctor]["label"]) new_num_adopters=len(list_I) if shift_length==5: # i estimate that adoption happens in the middle of the shift if t+5 < max_order: list_single_t_evolution.append(old_num_adopters) if t+4 < max_order: list_single_t_evolution.append(old_num_adopters) if t+3 < max_order: list_single_t_evolution.append(new_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=5 elif shift_length==4: if t+4 < max_order: list_single_t_evolution.append(old_num_adopters) if t+3 < max_order: list_single_t_evolution.append(old_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=4 elif shift_length==3: if t+3 < max_order: list_single_t_evolution.append(old_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=3 elif shift_length==2: if t+2 < max_order: list_single_t_evolution.append(old_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=2 elif shift_length==1: if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=1 old_num_adopters=new_num_adopters ######## end t loop list_lists_t_evolutions.append(list_single_t_evolution) # now i only run the testing segment! list_dist_fixed_parameters_testing_segment.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing,list_single_t_evolution)) list_abs_dist_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1]-list_actual_evol_testing[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1]-list_actual_evol_testing[-1]) # i save the distance at the ending point between the current simu and actual evol list_final_num_infected.append(list_single_t_evolution[-1]) for index in range(len(list_single_t_evolution)): list_abs_dist_point_by_point_indiv_simus_to_actual.append(abs(list_single_t_evolution[index]-list_actual_evol_testing[index])) list_dist_point_by_point_indiv_simus_to_actual.append(list_single_t_evolution[index]-list_actual_evol_testing[index]) ######## end loop Niter file2 = open(output_file2,'at') for s in range(len(list_single_t_evolution)): list_fixed_t=[] for iter in range (Niter): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s+cutting_day,numpy.mean(list_fixed_t) file2.close() print "printed out: ", output_file2 if envelopes=="YES": calculate_envelope_set_curves.calculate_envelope(list_lists_t_evolutions,percent_envelope,"Infection",[prob_infection,prob_Immune]) num_valid_endings=0. for item in list_abs_dist_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings+=1. print "average distance of the optimum in the testing segment:",numpy.mean(list_dist_fixed_parameters_testing_segment),numpy.std(list_dist_fixed_parameters_testing_segment),list_dist_fixed_parameters_testing_segment,"\n" print "fraction of realizations that end within delta_doctor:",num_valid_endings/Niter,"mean ending dist:",numpy.mean(list_dist_at_ending_point_fixed_parameters), "SD final dist",numpy.std(list_dist_at_ending_point_fixed_parameters) ,list_dist_at_ending_point_fixed_parameters,"\n" histogram_filename="../Results/weight_shifts/histogr_raw_distances_ending_infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_gral_negv_posit.histograma(list_dist_at_ending_point_fixed_parameters,histogram_filename) # histogram_filename2="../Results/weight_shifts/histogr_sum_dist_traject_infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" # histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,Nbins,histogram_filename2) histogram_filename3="../Results/weight_shifts/histogr_sum_dist_testing_segment_infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_bines_gral.histograma_bins_zero(list_dist_fixed_parameters_testing_segment,Nbins,histogram_filename3) print min(list_dist_fixed_parameters_testing_segment),max(list_dist_fixed_parameters_testing_segment) histogram_filename4="../Results/weight_shifts/histogr_abs_dist_point_by_point_infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_gral_negv_posit.histograma(list_abs_dist_point_by_point_indiv_simus_to_actual,histogram_filename4) histogram_filename5="../Results/weight_shifts/histogr_dist_point_by_point_infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" histograma_gral_negv_posit.histograma(list_dist_point_by_point_indiv_simus_to_actual,histogram_filename5) output_file10="../Results/weight_shifts/Summary_results_infection_p"+str(prob_infection)+"_"+"Immune"+str(prob_Immune)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" file10 = open(output_file10,'wt') print >> file10, "Summary results from best fit infection with",Niter, "iter, and with values for the parameters: prob_inf ",prob_infection," prob immune: ",prob_Immune,"\n" print >> file10, "average distance of the optimum in the testing segment:",numpy.mean(list_dist_fixed_parameters_testing_segment),numpy.std(list_dist_fixed_parameters_testing_segment),list_dist_fixed_parameters_testing_segment,"\n" print >> file10, "fraction of realizations that end within delta_doctor:",num_valid_endings/Niter,"mean ending dist:",numpy.mean(list_dist_at_ending_point_fixed_parameters), "SD final dist",numpy.std(list_dist_at_ending_point_fixed_parameters) ,list_dist_at_ending_point_fixed_parameters,"\n" print >> file10, "written optimum best fit evolution file:",output_file2 print >> file10,"written histogram file: ",histogram_filename file10.close() print "written Summary file: ",output_file10 prob_infection+= delta_prob prob_Immune+= delta_prob_Immune
def main(graph_name): G = nx.read_gml(graph_name) for_testing_fixed_set = "YES" # when YES, fixed values param, to get all statistics on final distances etc # change the range for the parameters accordingly envelopes = "YES" Niter = 1000 # 100 iter seems to be enough (no big diff. with respect to 1000it) percent_envelope = 95. list_id_weekends_T3 = look_for_T3_weekends( G ) # T3 doesnt share fellows in the weekend (but they are the exception) Nbins = 20 # for the histogram of sum of distances cutting_day = 175 # i use this only for the filenames all_team = "NO" # as adopters or not dir_real_data = '../Results/' dir = "../Results/weight_shifts/infection/" delta_end = 3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) if for_testing_fixed_set == "NO": output_file3 = "../Results/weight_shifts/Landscape_parameters_infection_" + str( Niter) + "iter.dat" file3 = open(output_file3, 'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": print "remember that now i use the file of adopters without fellows\n../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" exit() else: filename_actual_evol = "../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1 = open( filename_actual_evol, 'r' ) ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file = file1.readlines() list_actual_evol = [] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters = float(line.split(" ")[1]) list_actual_evol.append(num_adopters) ################################################################## #../Results/weight_shifts/infection/Average_time_evolution_Infection_training_p0.8_Immune0.3_1000iter_2012_avg_ic_day125.dat ESTOS VALORES SON EL OPTIMUM FIT THE 152-DIAS prob_min = 0.1 prob_max = 0.101 delta_prob = 0.1 prob_Immune_min = 0.00 prob_Immune_max = 0.001 delta_prob_Immune = 0.1 dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection if for_testing_fixed_set == "YES": output_file2 = dir + "Average_time_evolution_Infection_train_test_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_2012.dat" else: output_file2 = dir + "Average_time_evolution_Infection_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_2012.dat" file2 = open(output_file2, 'wt') file2.close() # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] list_abs_dist_at_ending_point_fixed_parameters = [] list_dist_at_ending_point_fixed_parameters = [] list_final_num_infected = [] # list_abs_dist_at_cutting_day=[] for iter in range(Niter): #print " iter:",iter #######OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file_name_indiv_evol=output_file2.strip("Average_").split('.dat')[0]+"_indiv_iter"+str(iter)+".dat" # file4 = open(file_name_indiv_evol,'wt') # file4.close() ########################################## ########### set I.C. list_I = [] #list infected doctors max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": if G.node[n]['order'] > max_order: max_order = G.node[n][ 'order'] # to get the last shift-order for the time loop else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" ################# the dynamics starts: t = 1 while t <= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: shift_lenght = int(G.node[n]['shift_lenght']) if shift_lenght == 2 and n not in list_id_weekends_T3: shift_lenght = 1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_lenght'] flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection for i in range( shift_lenght ): # i repeat the infection process several times, to acount for shift lenght if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" if G.node[doctor][ "type"] == "A": # fellows participate in the dynamics, but i only consider the attendings as real adopters list_I.append( G.node[doctor] ["label"]) # if for_testing_fixed_set=="YES": # if t==cutting_day: # list_abs_dist_at_cutting_day.append(abs(float(list_actual_evol[-1])-float(len(list_I)))) # print abs(float(list_actual_evol[-1])-float(len(list_I))), float(list_actual_evol[t]),float(len(list_I)) list_single_t_evolution.append(float(len(list_I))) t += 1 ######## end t loop ########OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file4 = open(file_name_indiv_evol,'at') #for i in range(len(list_single_t_evolution)): #time step by time step # print >> file4, i,list_single_t_evolution[i], prob_infection, prob_Immune #file4.close() ######################################################## list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol, list_single_t_evolution)) list_abs_dist_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1] - list_actual_evol[-1] ) # i save the distance at the ending point between the current simu and actual evol list_final_num_infected.append(list_single_t_evolution[-1]) ######## end loop Niter list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_abs_dist_at_ending_point_fixed_parameters)) if for_testing_fixed_set == "NO": file3 = open(output_file3, 'at') # i print out the landscape print >> file3, prob_infection, prob_Immune, numpy.mean( list_abs_dist_at_ending_point_fixed_parameters ), numpy.mean(list_dist_fixed_parameters), numpy.mean( list_final_num_infected), numpy.std( list_final_num_infected) file3.close() if ( numpy.mean(list_abs_dist_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end file2 = open(output_file2, 'at') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) file2.close() print "printed out: ", output_file2 # raw_input() if envelopes == "YES": calculate_envelope_set_curves.calculate_envelope( list_lists_t_evolutions, percent_envelope, "Infection", [prob_infection, prob_Immune]) if for_testing_fixed_set == "YES": num_valid_endings = 0. for item in list_abs_dist_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings += 1. print "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters ), list_dist_fixed_parameters, "\n" print "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter, "mean ending dist:", numpy.mean( list_dist_at_ending_point_fixed_parameters ), "SD final dist", numpy.std( list_dist_at_ending_point_fixed_parameters ), list_dist_at_ending_point_fixed_parameters, "\n" histogram_filename = "../Results/weight_shifts/histogr_raw_distances_ending_infection_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_day" + str( cutting_day) + ".dat" histograma_gral_negv_posit.histograma( list_dist_at_ending_point_fixed_parameters, histogram_filename) histogram_filename2 = "../Results/weight_shifts/histogr_sum_dist_traject_infection_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_day" + str( cutting_day) + ".dat" histograma_bines_gral.histograma_bins( list_dist_fixed_parameters, Nbins, histogram_filename2) output_file10 = "../Results/weight_shifts/Summary_results_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter) + "iter_day" + str( cutting_day) + ".dat" file10 = open(output_file10, 'wt') print >> file10, "Summary results from train-testing infection with", Niter, "iter, and with values for the parameters: prob_inf ", prob_infection, " prob immune: ", prob_Immune, "\n" print >> file10, "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters ), list_dist_fixed_parameters, "\n" print >> file10, "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter, "mean ending dist:", numpy.mean( list_dist_at_ending_point_fixed_parameters ), "SD final dist", numpy.std( list_dist_at_ending_point_fixed_parameters ), list_dist_at_ending_point_fixed_parameters, "\n" print >> file10, "written optimum train_test evolution file:", output_file2 print >> file10, "written histogram file: ", histogram_filename file10.close() print "written Summary file: ", output_file10 print "written histogram file: ", histogram_filename print "written histogram file: ", histogram_filename2 prob_infection += delta_prob prob_Immune += delta_prob_Immune if for_testing_fixed_set == "NO": # only if i am exploring the whole landscape, i need to call this function, otherwise, i already know the optimum compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Infection_weight", all_team, Niter, None) # last argument doesnt apply (cutting day) if for_testing_fixed_set == "NO": print "written landscape file:", output_file3
def main(graph_name): G = nx.read_gml(graph_name) cutting_day=75 # i use this only for the filenames for_testing_fixed_set="YES" # when YES, fixed values param, to get all statistics on final distances etc # change the range for the parameters accordingly Niter=1000000 # 100 iter seems to be enough (no big diff. with respect to 1000it) min_sum_dist=100 # to compute number of realizations that have a sum of distances smaller than this percent_envelope=95. list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) all_team="NO" # as adopters or not dir_real_data='../Results/' dir="../Results/weight_shifts/infection/" delta_end=3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol="../Data/Attendings_Orders_from_inference_list_adopters_day.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() dict_days_list_empirical_adopters={} list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row day=int(line.split(" ")[0]) num_adopters= float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_current_adopters=[] for element in line.split(" ")[2:]: # i need to ignore the empty columns from the original datafile if element: if element != '\n': list_current_adopters.append(element.strip('\n')) dict_days_list_empirical_adopters[day]=list_current_adopters list_actual_evol_testing=list_actual_evol[cutting_day:] ################################################################## #../Results/weight_shifts/infection/Average_time_evolution_Infection_training_p0.8_Immune0.3_1000iter_2012_avg_ic_day125.dat ESTOS VALORES SON EL OPTIMUM FIT THE 152-DIAS prob_min=0.1 prob_max=0.101 delta_prob=0.1 prob_Immune_min=0.00 prob_Immune_max=0.01 delta_prob_Immune=0.1 prob_Immune=prob_Immune_min while prob_Immune<= prob_Immune_max: print "prom Immune:",prob_Immune prob_infection=prob_min while prob_infection<= prob_max: print " p:",prob_infection list_lists_t_evolutions=[] list_dist_fixed_parameters_testing_segment=[] list_abs_dist_at_ending_point_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_final_num_infected=[] list_abs_dist_point_by_point_indiv_simus_to_actual=[] list_dist_point_by_point_indiv_simus_to_actual=[] list_small_values=[] for iter in range(Niter): # print " iter:",iter ########### set I.C. according to the empirical data list_I=[] max_order=0 for n in G.nodes(): G.node[n]["status"]="S" # all nodes are Susceptible if G.node[n]['type']=="shift": if G.node[n]['order']>max_order: max_order=G.node[n]['order'] # to get the last shift-order for the time loop else: if G.node[n]['label'] in dict_days_list_empirical_adopters[cutting_day]: G.node[n]["status"]="I" list_I.append(G.node[n]['label']) list_single_t_evolution=[] old_num_adopters=len(dict_days_list_empirical_adopters[cutting_day]) list_single_t_evolution.append(old_num_adopters) # I always start with TWO infected doctors!! for n in G.nodes(): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type']=="A") or ( G.node[n]['type']=="F"): if G.node[n]['label'] not in dict_days_list_empirical_adopters[cutting_day]: rand=random.random() if rand< prob_Immune: G.node[n]["status"]="Immune" ################# the dynamics starts: shift_length=5 #i know the first shift (order 0) is of length 5 t=cutting_day while t<= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: shift_length=int(G.node[n]['shift_length']) if shift_length==2 and n not in list_id_weekends_T3: shift_length=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) # print "one-day weekend", G.node[n]['label'],G.node[n]['shift_length'] flag_possible_infection=0 for doctor in G.neighbors(n): #first i check if any doctor is infected in this shift if G.node[doctor]["status"]=="I": flag_possible_infection=1 if flag_possible_infection: for doctor in G.neighbors(n): # then the doctors in that shift, gets infected with prob_infection for i in range(shift_length): # i repeat the infection process several times, to acount for shift length if G.node[doctor]["status"]=="S": rand=random.random() if rand<prob_infection: G.node[doctor]["status"]="I" # if G.node[doctor]["type"]=="A": # fellows participate in the dynamics, but i only consider the attendings as real adopters list_I.append(G.node[doctor]["label"]) new_num_adopters=len(list_I) if shift_length==5: # i estimate that adoption happens in the middle of the shift if t+5 < max_order: list_single_t_evolution.append(old_num_adopters) if t+4 < max_order: list_single_t_evolution.append(old_num_adopters) if t+3 < max_order: list_single_t_evolution.append(new_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=5 elif shift_length==4: if t+4 < max_order: list_single_t_evolution.append(old_num_adopters) if t+3 < max_order: list_single_t_evolution.append(old_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=4 elif shift_length==3: if t+3 < max_order: list_single_t_evolution.append(old_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=3 elif shift_length==2: if t+2 < max_order: list_single_t_evolution.append(old_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=2 elif shift_length==1: if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=1 old_num_adopters=new_num_adopters ######## end t loop # now i only run the testing segment! dist=compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing,list_single_t_evolution) if dist < min_sum_dist: list_small_values.append(dist) ######## end loop Niter print "fraction realizations with sum distances smaller than", min_sum_dist,"is ",float(len(list_small_values))/float(Niter),"namelly:",list_small_values prob_infection+= delta_prob prob_Immune+= delta_prob_Immune
def main(graph_name): G = nx.read_gml(graph_name) cutting_day = 125 # to separate training-testing Niter_training = 100 Niter_testing = 100 delta_end = 3 # >= than + or - dr difference at the end of the evolution dir_real_data = '../Results/' all_team = "NO" # as adopters or not # output_file3=dir_real_data+"Landscape_parameters_persuasion_"+str(Niter)+"iter.dat" #file3 = open(output_file3,'wt') ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol = [] result_actual_file = csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont = 0 for row in result_actual_file: if cont > 0: # i ignore the first line with the headers num_adopters = row[3] list_actual_evol.append(float(num_adopters)) cont += 1 list_actual_evol_training = list_actual_evol[:cutting_day] list_actual_evol_testing = list_actual_evol[(cutting_day - 1):] ################################################################## #../Results/network_final_schedule_withTeam3/Time_evolutions_Persuasion_alpha0.2_damping0.0_mutual_encourg0.7_threshold0.4_unif_distr_50iter_2012_seed31Oct_finalnetwork.dat alpha_F_min = 0.0 # alpha=0: nobody changes their mind alpha_F_max = 1.001 delta_alpha_F = 0.1 min_damping = 0.0 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping = 1.01 delta_damping = 0.1 min_mutual_encouragement = 0.0 # when two Adopters meet, they convince each other even more max_mutual_encouragement = 1.01 delta_mutual_encouragement = 0.1 print "\n\nPersuasion process on network, with Niter:", Niter_training dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_list_dict_network_states = {} alpha_F = alpha_F_min while alpha_F <= alpha_F_max: # i explore all the parameter space, and create a file per each set of values alpha_A = 0.5 * alpha_F print " alpha_F:", alpha_F mutual_encouragement = min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:", mutual_encouragement damping = min_damping while damping <= max_damping: print " damping:", damping dir = "../Results/network_final_schedule_withTeam3_local/" output_file = dir + "Time_evolutions_Persuasion_training_alpha" + str( alpha_F ) + "_damping" + str(damping) + "_mutual_encourg" + str( mutual_encouragement) + "_" + str( Niter_training) + "iter_distributed_thresholds.dat" file = open(output_file, 'wt') file.close() time_evol_number_adopters_ITER = [ ] # list of complete single realizations of the dynamics list_dist_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_dict_network_states = [] list_networks_at_cutting_day = [] for iter in range(Niter_training): print " ", iter list_t = [] time_evol_number_adopters = [ ] # for a single realization of the dynamics dict_network_states = {} num_adopters, seed_shift, max_shift = set_ic( G ) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) list_t.append(0) ########### the dynamics starts: t = int( seed_shift) + 1 # the first time step is just IC.??? while t < cutting_day: # loop over shifts, in chronological order (the order is the day index since seeding_day) list_t.append(t) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: # i look for the shift corresponding to that time step flag_possible_persuasion = 0 for doctor in G.neighbors(n): if G.node[doctor][ "status"] == "Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion = 1 break if flag_possible_persuasion == 1: list_doctors = [] for doctor in G.neighbors( n): # for all drs in that shift list_doctors.append(doctor) pairs = itertools.combinations( list_doctors, 2 ) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1 = pair[0] doctor2 = pair[1] if G.node[doctor1]['status'] != G.node[ doctor2][ 'status']: # if they think differently, # there will be persuasion persuasion( G, damping, doctor1, doctor2, alpha_A, alpha_F ) # i move their values of opinion update_opinions( G, doctor1, doctor2 ) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement( G, mutual_encouragement, doctor1, doctor2) list_Adopters = [] #count how many i have at this time for n in G.nodes(): try: if G.node[n]["status"] == "Adopter": if G.node[n]["label"] not in list_Adopters: list_Adopters.append( G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute time_evol_number_adopters.append( float(len(list_Adopters))) t += 1 ############## end while loop over t for n in G.nodes(): if G.node[n]['type'] != "shift": dict_network_states[ G.node[n]["label"]] = G.node[n]["status"] list_dict_network_states.append(dict_network_states) time_evol_number_adopters_ITER.append( time_evol_number_adopters) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called. compare_two_curves(list_actual_evol_training, time_evol_number_adopters)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1] - list_actual_evol_training[-1])) ####################### end loop Niter for the training fase list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) if ( numpy.mean( list_dist_abs_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file] = list_pair_dist_std_delta_end #print >> file3, alpha_F,damping,mutual_encouragement,threshold,dict_filenames_tot_distance[output_file][0],dict_filenames_tot_distance[output_file][1] dict_filenames_list_dict_network_states[ output_file] = list_dict_network_states file = open(output_file, 'wt') for i in range(len( time_evol_number_adopters)): #time step by time step list_fixed_t = [] for iteracion in range( Niter_training ): #loop over all independent iter of the process list_fixed_t.append( time_evol_number_adopters_ITER[iteracion][i] ) # i collect all values for the same t, different iter print >> file, list_t[i], numpy.mean( list_fixed_t), numpy.std( list_fixed_t ), alpha_F, damping, mutual_encouragement file.close() damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F list_order_dict = compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Persuasion_training_distributed_thresholds", all_team, Niter_training) #./Results/network_final_schedule_withTeam3_local/Time_evolutions_Persuasion_alpha0.4_damping0.4_mutual_encourg0.6_threshold0.5_unif_distr_2iter_2012_seed31Oct_finalnetwork.dat optimum_filename = list_order_dict[0][0] alpha_F = float(list_order_dict[0][0].split("_alpha")[1][0:3]) alpha_A = 0.5 * alpha_F damping = float(list_order_dict[0][0].split("_damping")[1][0:3]) mutual_encouragement = float( list_order_dict[0][0].split("_mutual_encourg")[1][0:3]) # raw_input() print "starting testing fase with:" print "alpha=", alpha_F, " damping=", damping, " mutual encourag=", mutual_encouragement, " distributed threshold" # i already know the optimum, now i run the dynamics with those values, starting from the average state on the cutting point, and test: time_evol_number_adopters_ITER = [ ] # list of complete single realizations of the dynamics list_dict_network_states = [] list_dist_fixed_parameters = [] list_dist_at_ending_point_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_lists_t_evolutions = [] lista_num_adopters = [] lista_Adopters = [] dict_tot_Adopters = {} for dictionary in dict_filenames_list_dict_network_states[ optimum_filename]: # dictionary={Dr1:status, Dr2:status,} # one dict per iteration num_Adopters = 0. for key in dictionary: if dictionary[key] == "Adopter": num_Adopters += 1. if key not in lista_Adopters: lista_Adopters.append(key) dict_tot_Adopters[key] = 1. else: dict_tot_Adopters[key] += 1. lista_num_adopters.append(num_Adopters) avg_adopters = int( numpy.mean(lista_num_adopters)) # i find out the average num Adopters print numpy.mean(lista_num_adopters), avg_adopters, numpy.std( lista_num_adopters) if numpy.mean(lista_num_adopters) - avg_adopters >= 0.5: avg_adopters += 1.0 print avg_adopters # i sort the list from more frequently infected to less list_sorted_dict = sorted(dict_tot_Adopters.iteritems(), key=operator.itemgetter(1)) new_list_sorted_dict = list_sorted_dict new_list_sorted_dict.reverse() print "Adopters:", new_list_sorted_dict #list_sorted_dict=[(u'Weiss', 5.0), (u'Wunderink', 5.0), (u'Keller', 4.0), (u'Go', 3.0), (u'Cuttica', 3.0), (u'Rosario', 2.0), (u'Radigan', 2.0), (u'Smith', 2.0), (u'RosenbergN', 2.0), (u'Gillespie', 1.0), (u'Osher', 1.0), (u'Mutlu', 1.0), (u'Dematte', 1.0), (u'Hawkins', 1.0), (u'Gates', 1.0)] lista_avg_Adopters = [ ] # i create the list of Drs that on average are most likely infected by the cutting day i = 1 for item in new_list_sorted_dict: if (item[0] not in lista_avg_Adopters) and (i <= avg_adopters): lista_avg_Adopters.append(item[0]) i += 1 print lista_avg_Adopters for iter in range(Niter_testing): print " ", iter dict_dr_status_current_iter = dict_filenames_list_dict_network_states[ optimum_filename][iter] time_evol_number_adopters = [ ] # for a single realization of the dynamics dict_network_states = {} list_t = [] ############### # NECESITO GUARDAR RECORD DE LOS THERSHOLDS PERSONALES PARA USARLOS LUEGO aki??? ######## list_Adopters = [] #set initial conditions for node in G.nodes(): if G.node[node]['type'] != "shift": # personal threshold have been established for each dr at the beginning of the simu: set_ic() G.node[node][ "status"] = "NonAdopter" # by default, non-Adopters label = G.node[node]['label'] if label in lista_avg_Adopters: G.node[node]["status"] = "Adopter" G.node[node]["adoption_vector"] = random.random() * ( 1.0 - G.node[node]["personal_threshold"]) + G.node[node][ "personal_threshold"] #values from (threshold,1] if G.node[node]["adoption_vector"] > 1.0: G.node[node]["adoption_vector"] = 1.0 list_Adopters.append(G.node[node]["label"]) time_evol_number_adopters.append(float(len(list_Adopters))) list_t.append(cutting_day) ################# the dynamics starts for the testing fase: t = cutting_day while t <= max_shift: # loop over shifts, in chronological order (the order is the day index since seeding_day) list_t.append(t) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: # i look for the shift corresponding to that time step flag_possible_persuasion = 0 for doctor in G.neighbors(n): if G.node[doctor][ "status"] == "Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion = 1 break if flag_possible_persuasion == 1: list_doctors = [] for doctor in G.neighbors( n): # for all drs in that shift list_doctors.append(doctor) pairs = itertools.combinations( list_doctors, 2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1 = pair[0] doctor2 = pair[1] if G.node[doctor1]['status'] != G.node[doctor2][ 'status']: # if they think differently, # there will be persuasion persuasion( G, damping, doctor1, doctor2, alpha_A, alpha_F) # i move their values of opinion update_opinions( G, doctor1, doctor2 ) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G, mutual_encouragement, doctor1, doctor2) list_Adopters = [] #count how many i have at this time for n in G.nodes(): try: if G.node[n]["status"] == "Adopter": if G.node[n]["label"] not in list_Adopters: list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute time_evol_number_adopters.append(float(len(list_Adopters))) print t, len(list_Adopters) t += 1 ############## end while loop over t #raw_input() for n in G.nodes(): if G.node[n]['type'] != "shift": dict_network_states[G.node[n]["label"]] = G.node[n]["status"] list_dict_network_states.append(dict_network_states) time_evol_number_adopters_ITER.append(time_evol_number_adopters) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing, time_evol_number_adopters)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1] - list_actual_evol_testing[-1])) list_dist_at_ending_point_fixed_parameters.append( time_evol_number_adopters[-1] - list_actual_evol_testing[-1]) #######################end loop over Niter for the testing fase list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters )) # average dist between the curves over Niter list_pair_dist_std_delta_end.append(numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) if ( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[output_file] = list_pair_dist_std_delta_end dict_filenames_list_dict_network_states[ output_file] = list_dict_network_states num_valid_endings = 0. for item in list_dist_abs_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings += 1. print "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters), list_dist_fixed_parameters print "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter_testing, list_dist_abs_at_ending_point_fixed_parameters histograma_gral_negv_posit.histograma( list_dist_at_ending_point_fixed_parameters, "../Results/histogr_raw_distances_ending_test_train_persuasion_avg_ic_" + str(Niter_testing) + "iter_distributed_thresholds.dat") output_file8 = "../Results/List_tot_distances_training_segment_persuasion_alpha" + str( alpha_F) + "_damping" + str(damping) + "_mutual_encourg" + str( mutual_encouragement) + "_" + str( Niter_training) + "iter_avg_ic_distributed_thresholds.dat" file8 = open(output_file8, 'wt') for item in list_dist_fixed_parameters: print >> file8, item file8.close() output_file9 = "../Results/List_distances_ending_training_segment_persuasion_alpha" + str( alpha_F) + "_damping" + str(damping) + "_mutual_encourg" + str( mutual_encouragement) + "_" + str( Niter_training) + "iter_avg_ic_distributed_thresholds.dat" file9 = open(output_file9, 'wt') for item in list_dist_abs_at_ending_point_fixed_parameters: print >> file9, item file9.close() output_file = dir + "Time_evolutions_Persuasion_testing_avg_ic_alpha" + str( alpha_F ) + "_damping" + str(damping) + "_mutual_encourg" + str( mutual_encouragement ) + "_unif_distr_" + str( Niter_training ) + "iter_2012_seed31Oct_finalnetwork_avg_ic_distributed_thresholds.dat" file = open(output_file, 'wt') for i in range(len(time_evol_number_adopters)): #time step by time step list_fixed_t = [] for iteracion in range( Niter_training ): #loop over all independent iter of the process list_fixed_t.append( time_evol_number_adopters_ITER[iteracion] [i]) # i collect all values for the same t, different iter print >> file, list_t[i], numpy.mean(list_fixed_t), numpy.std( list_fixed_t), alpha_F, damping, mutual_encouragement file.close() print "written training segment file:", optimum_filename print "written testing segment file:", output_file output_file10 = "../Results/Summary_results_train_test_persuasion_alpha" + str( alpha_F) + "_damping" + str(damping) + "_mutual_encourg" + str( mutual_encouragement) + "_" + str( Niter_training) + "iter_avg_ic_distributed_thresholds.dat" file10 = open(output_file10, 'wt') print >> file10, "Summary results from train-testing persuasion with", Niter_training, Niter_testing, "iter (respectively), using the avg of the cutting points as IC, and with values for the parameters: alpha ", alpha_F, " damping: ", damping, " mutual_encourg: ", mutual_encouragement, " distributed threshold" print >> file10, "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters), list_dist_fixed_parameters print >> file10, "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter_testing, list_dist_at_ending_point_fixed_parameters print >> file10, "written training segment file:", optimum_filename print >> file10, "written testing segment file:", output_file file10.close()
def main(graph_name): G = nx.read_gml(graph_name) list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) cutting_day=175 # to separate training-testing Niter_training=1000 delta_end=3 # >= than + or - dr difference at the end of the evolution dir_real_data='../Results/' dir="../Results/weight_shifts/persuasion/" all_team="NO" # as adopters or not Nbins=20 # for the histogram of sum of distances fixed_param="FIXED_mutual0.5_damping.5_" # or "" # for the Results file that contains the sorted list of best parameters # fixed_parameters="mutual_encoug0.5_threshold0.5" # for the Landscape text file CHANGE PARAMETERS ACCORDINGLY!!! #output_file3="../Results/weight_shifts/Landscape_parameters_persuasion_train_test_"+str(fixed_parameters)+"_"+str(Niter_training)+"iter.dat" output_file3="../Results/weight_shifts/Landscape_parameters_persuasion_train_FIXED_damping0.1_threshold0.7_"+str(Niter_training)+"iter_alphaA_eq_alphaF.dat" file3 = open(output_file3,'wt') file3.close() ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team=="YES": print "remember that now i use the file of adopters without fellows\n../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" exit() else: filename_actual_evol="../Results/Actual_evolution_adopters_NO_fellows_only_attendings.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row num_adopters= float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_actual_evol_training=list_actual_evol[:cutting_day] ################################################################## #../Results/network_final_schedule_withTeam3/Time_evolutions_Persuasion_alpha0.2_damping0.0_mutual_encourg0.7_threshold0.4_unif_distr_50iter_2012_seed31Oct_finalnetwork.dat alpha_F_min=0.10 # # alpha=0: nobody changes their mind alpha_F_max=0.9 delta_alpha_F=0.10 #AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT! min_damping=0.500 #0.0 #its harder to go back from YES to NO again. =1 means no effect, =0.5 half the movement from Y->N than the other way around, =0 never go back from Y to N max_damping=0.501 #0.451 delta_damping=0.10 min_mutual_encouragement=0.50 # # when two Adopters meet, they convince each other even more max_mutual_encouragement=0.501 delta_mutual_encouragement=0.10 threshold_min=0.10 # # larger than, to be an Adopter threshold_max=0.901 delta_threshold=0.10 # AVOID 1.0 OR THE DYNAMICS GETS TOTALLY STUCK AND IT IS NOT ABLE TO PREDICT SHIT print "\n\nPersuasion process on network, with Niter:",Niter_training dict_filenames_tot_distance={} # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_prod_distances={} threshold=threshold_min while threshold<= threshold_max: print "thershold:",threshold alpha_F=alpha_F_min while alpha_F<= alpha_F_max: # i explore all the parameter space, and create a file per each set of values alpha_A=1.0*alpha_F print " alpha_F:",alpha_F mutual_encouragement=min_mutual_encouragement while mutual_encouragement <= max_mutual_encouragement: print " mutual_encouragement:",mutual_encouragement damping=min_damping while damping <= max_damping: print " damping:",damping # dir="../Results/weight_shifts/persuasion/alpha%.2f_damping%.2f/" % (alpha_F, damping ) output_file=dir+"Time_evolutions_Persuasion_training_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_unif_distr_"+str(Niter_training)+"iter_alphaA_eq_alphaF"+"_"+str(cutting_day)+".dat" # file = open(output_file,'wt') # i am not saving the train file, because i will just want to know #file.close() # the optimum parameter set and go look for the whole-250-day file time_evol_number_adopters_ITER=[] # list of complete single realizations of the dynamics list_dist_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_dist_abs_at_ending_point_fixed_parameters=[] list_networks_at_cutting_day=[] list_final_num_adopt=[] for iter in range(Niter_training): # print " ",iter list_t=[] time_evol_number_adopters=[] # for a single realization of the dynamics num_adopters , seed_shift ,max_shift= set_ic(G,threshold) # i establish who is Adopter and NonAdopter initially, and count how many shifts i have total time_evol_number_adopters.append(float(num_adopters)) list_t.append(0) ########### the dynamics starts: t=int(seed_shift)+1 # the first time step is just IC.??? while t< cutting_day: # loop over shifts, in chronological order (the order is the day index since seeding_day) list_t.append(t) for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: # i look for the shift corresponding to that time step shift_lenght=int(G.node[n]['shift_lenght']) if shift_lenght==2 and n not in list_id_weekends_T3: shift_lenght=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) flag_possible_persuasion=0 for doctor in G.neighbors(n): if G.node[doctor]["status"]=="Adopter": #first i check if any doctor is an adopter in this shift flag_possible_persuasion=1 break if flag_possible_persuasion==1: list_doctors=[] for doctor in G.neighbors(n): # for all drs in that shift list_doctors.append(doctor) pairs=itertools.combinations(list_doctors,2) # cos the shift can be 2 but also 3 doctors for pair in pairs: doctor1=pair[0] doctor2=pair[1] if G.node[doctor1]['status'] != G.node[doctor2]['status']: # if they think differently, # there will be persuasion persuasion(G,damping,doctor1,doctor2,alpha_A,alpha_F,threshold,shift_lenght) # i move their values of opinion update_opinions(G,threshold,doctor1,doctor2) # i update status and make sure the values of the vectors stay between [0,1] else: # if two Adopters meet, they encourage each other (if two NonAdopters, nothing happens) mutual_reinforcement(G,mutual_encouragement,doctor1,doctor2,shift_lenght) list_all_Adopters=[] #including fellows list_Adopters=[] #NOT including fellows for n in G.nodes(): try: if G.node[n]["status"]=="Adopter": if G.node[n]["label"] not in list_Adopters and G.node[n]["type"]=="A": list_Adopters.append(G.node[n]["label"]) except: pass # if the node is a shift, it doesnt have a 'status' attribute time_evol_number_adopters.append(float(len(list_Adopters))) t+=1 ############## end while loop over t time_evol_number_adopters_ITER.append(time_evol_number_adopters) list_final_num_adopt.append(time_evol_number_adopters[-1]) list_dist_fixed_parameters.append(compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training,time_evol_number_adopters)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(time_evol_number_adopters[-1]-list_actual_evol_training[-1]) ) list_dist_at_ending_point_fixed_parameters.append( time_evol_number_adopters[-1]-list_actual_evol_training[-1]) ####################### end loop Niter for the training fase list_pair_dist_std_delta_end=[] list_pair_dist_std_delta_end.append(numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append(numpy.std(list_dist_fixed_parameters) ) list_pair_dist_std_delta_end.append(numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) value=numpy.mean(list_dist_fixed_parameters) *numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) # if SD=0, it is a problem, because then that is the minimun value, but not the optimum i am looking for!! dict_filenames_prod_distances[output_file]= value file3 = open(output_file3,'at') # i print out the landscape print >> file3, alpha_F, damping, mutual_encouragement, threshold,numpy.mean(list_dist_abs_at_ending_point_fixed_parameters), numpy.mean(list_dist_fixed_parameters), numpy.mean(list_final_num_adopt),numpy.std(list_final_num_adopt), numpy.std(list_final_num_adopt)/numpy.mean(list_final_num_adopt) file3.close() histogram_filename="../Results/weight_shifts/histogr_raw_distances_ending_test_train_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_unif_distr_"+str(Niter_training)+"iter_alphaA_eq_alphaF"+"_"+str(cutting_day)+".dat" histograma_gral_negv_posit.histograma(list_dist_at_ending_point_fixed_parameters,histogram_filename) histogram_filename2="../Results/weight_shifts/histogr_sum_dist_traject_infection_training_alpha"+str(alpha_F)+"_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_threshold"+str(threshold)+"_unif_distr_"+str(Niter_training)+"iter_alphaA_eq_alphaF"+"_"+str(cutting_day)+".dat" histograma_bines_gral.histograma_bins(list_dist_fixed_parameters,Nbins,histogram_filename2) print "written histogram file: ",histogram_filename print "written histogram file: ",histogram_filename2 if (numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[output_file]=list_pair_dist_std_delta_end # file = open(output_file,'wt') # for i in range(len(time_evol_number_adopters)): #time step by time step # list_fixed_t=[] # for iteracion in range (Niter_training): #loop over all independent iter of the process # list_fixed_t.append(time_evol_number_adopters_ITER[iteracion][i]) # i collect all values for the same t, different iter # print >> file, list_t[i],numpy.mean(list_fixed_t),numpy.std(list_fixed_t), alpha_F,damping,mutual_encouragement #file.close() damping += delta_damping mutual_encouragement += delta_mutual_encouragement alpha_F += delta_alpha_F threshold += delta_threshold list_order_dict= compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end(dict_filenames_tot_distance,"Persuasion_training_land_weight",all_team,Niter_training,cutting_day) string_name="_persuasion_training_"+fixed_param+str(Niter_training)+"iter_"+str(cutting_day)+".dat" # for the "Results" file with the sorted list of files list_order_dict2= compare_real_evol_vs_simus_to_be_called.pick_minimum_prod_distances(dict_filenames_prod_distances,string_name,all_team,Niter_training,cutting_day) #./Results/network_final_schedule_withTeam3_local/Time_evolutions_Persuasion_alpha0.4_damping0.4_mutual_encourg0.6_threshold0.5_unif_distr_2iter_2012_seed31Oct_finalnetwork.dat optimum_filename=list_order_dict[0][0] print optimum_filename alpha_F=float(list_order_dict[0][0].split("_alpha")[1].split("_")[0]) alpha_A=0.5*alpha_F damping=float(list_order_dict[0][0].split("_damping")[1].split("_")[0]) mutual_encouragement=float(list_order_dict[0][0].split("_mutual_encourg")[1].split("_")[0]) threshold=float(list_order_dict[0][0].split("_threshold")[1].split("_")[0]) print "Optimum (old method) alpha=", alpha_F, " damping=",damping," mutual encourag=",mutual_encouragement," threshold",threshold optimum_filename=list_order_dict2[0][0] print optimum_filename alpha_F=float(list_order_dict2[0][0].split("_alpha")[1].split("_")[0]) alpha_A=0.5*alpha_F damping=float(list_order_dict2[0][0].split("_damping")[1].split("_")[0]) mutual_encouragement=float(list_order_dict2[0][0].split("_mutual_encourg")[1].split("_")[0]) threshold=float(list_order_dict2[0][0].split("_threshold")[1].split("_")[0]) print "Optimum (product distances and SDs) alpha=", alpha_F, " damping=",damping," mutual encourag=",mutual_encouragement," threshold",threshold output_file10="../Results/weight_shifts/Summary_results_train_test_persuasion_alpha"+str(alpha_F)+"_FIXED_damping"+str(damping)+"_mutual_encourg"+str(mutual_encouragement)+"_FIXED_threshold"+str(threshold)+"_"+str(Niter_training)+"iter_alphaA_eq_alphaF_day"+str(cutting_day)+".dat" file10 = open(output_file10,'wt') print >> file10, "Summary results from train-testing persuasion with",Niter_training, "iter, using the avg of the cutting points as IC, and with values for the parameters: alpha ",alpha_F," damping: ",damping," mutual_encourg: ",mutual_encouragement," threshold:",threshold print >> file10, "Look for optimum the file set of parameters (or run those simulations):",optimum_filename file10.close() print "Look for optimum the file set of parameters (or run those simulations):",optimum_filename print "printed out landscape file:",output_file3
def main(graph_name): G = nx.read_gml(graph_name) cutting_day=125 # i use this only for the filenames for_testing_fixed_set="YES" # when YES, fixed values param, to get all statistics on final distances etc # change the range for the parameters accordingly envelopes="YES" Niter=1000000 # 100 iter seems to be enough (no big diff. with respect to 1000it) min_sum_dist=100 # to compute number of realizations that have a sum of distances smaller than this percent_envelope=95. list_id_weekends_T3=look_for_T3_weekends(G) # T3 doesnt share fellows in the weekend (but they are the exception) all_team="NO" # as adopters or not dir_real_data='../Results/' dir="../Results/weight_shifts/infection/" delta_end=3. # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### filename_actual_evol="../Data/Attendings_Orders_from_inference_list_adopters_day.dat" file1=open(filename_actual_evol,'r') ## i read the file: list_dates_and_names_current_adopters.txt (created with: extract_real_evolution_number_adopters.py) list_lines_file=file1.readlines() dict_days_list_empirical_adopters={} list_actual_evol=[] for line in list_lines_file: # [1:]: # i exclude the first row day=int(line.split(" ")[0]) num_adopters= float(line.split(" ")[1]) list_actual_evol.append(num_adopters) list_current_adopters=[] for element in line.split(" ")[2:]: # i need to ignore the empty columns from the original datafile if element: if element != '\n': list_current_adopters.append(element.strip('\n')) dict_days_list_empirical_adopters[day]=list_current_adopters list_actual_evol_testing=list_actual_evol[cutting_day:] ################################################################## prob_min=0.7 prob_max=0.701 delta_prob=0.1 prob_Immune_min=0.0 prob_Immune_max=0.01 delta_prob_Immune=0.1 ########## KEEP FIXED TO ONE infect_threshold_min=1.00 # i can define the dose in units of the threshold infect_threshold_max=1.001 delta_infect_threshold=0.1 ############ dose_min=0.2 # of a single encounter with an infected (starting from zero doesnt make sense) dose_max=0.201 delta_dose=0.01 prob_Immune=prob_Immune_min while prob_Immune<= prob_Immune_max: print "prom Immune:",prob_Immune prob_infection=prob_min while prob_infection<= prob_max: print " p:",prob_infection infect_threshold=infect_threshold_min dose=dose_min while dose <= dose_max: print " dose:",dose output_file2=dir+"Average_time_evolution_Infection_memory_p"+str(prob_infection)+"_Immune"+str(prob_Immune)+"_FIXED_threshold"+str(infect_threshold)+"_dose"+str(dose)+"_"+str(Niter)+"iter_day"+str(cutting_day)+"_A_F_inferred_middle_real_ic.dat" file2 = open(output_file2,'wt') file2.close() list_small_values=[] # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions=[] list_dist_fixed_parameters_testing_segment=[] list_abs_dist_at_ending_point_fixed_parameters=[] list_dist_at_ending_point_fixed_parameters=[] list_final_num_infected=[] list_abs_dist_point_by_point_indiv_simus_to_actual=[] list_dist_point_by_point_indiv_simus_to_actual=[] # list_abs_dist_at_cutting_day=[] for iter in range(Niter): # print " iter:",iter ########### set I.C. list_I=[] #list infected doctors max_order=0 for n in G.nodes(): G.node[n]["status"]="S" # all nodes are Susceptible G.node[n]["infec_value"]=0. # when this value goes over the infect_threshold, the dr is infected if G.node[n]['type']=="shift": if G.node[n]['order']>max_order: max_order=G.node[n]['order'] # to get the last shift-order for the time loop else: if G.node[n]['label'] in dict_days_list_empirical_adopters[cutting_day]: G.node[n]["infec_value"]=infect_threshold + 1. G.node[n]["status"]="I" list_I.append(G.node[n]['label']) list_single_t_evolution=[] old_num_adopters=len(dict_days_list_empirical_adopters[cutting_day]) list_single_t_evolution.append(old_num_adopters) # I always start with TWO infected doctors!! for n in G.nodes(): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type']=="A") or ( G.node[n]['type']=="F"): if G.node[n]['label'] not in dict_days_list_empirical_adopters[cutting_day]: rand=random.random() if rand< prob_Immune: G.node[n]["status"]="Immune" ################# the dynamics starts: shift_length=5 #i know the first shift (order 0) is of length 5 t=cutting_day while t<= max_order: # loop over shifts, in order for n in G.nodes(): if G.node[n]['type']=="shift" and G.node[n]['order']==t: shift_length=int(G.node[n]['shift_length']) if shift_length==2 and n not in list_id_weekends_T3: shift_length=1 # because during weekends, the fellow does rounds one day with Att1 and the other day with Att2. (weekend shifts for T3 are two day long, with no sharing fellows) flag_possible_infection=0 for doctor in G.neighbors(n): #first i check if any doctor is infected in this shift if G.node[doctor]["status"]=="I": flag_possible_infection=1 if flag_possible_infection: for doctor in G.neighbors(n): # then the doctors in that shift, gets infected with prob_infection for i in range(shift_length): if G.node[doctor]["status"]=="S": rand=random.random() if rand<prob_infection: # with prob p the infection occurres G.node[doctor]["infec_value"]+=dose # and bumps the infection_value of that susceptible dr if G.node[doctor]["infec_value"]>= infect_threshold: # becomes infected G.node[doctor]["status"]="I" # if G.node[doctor]["type"]=="A": # fellows participate in the dynamics, but i only consider the attendings as real adopters list_I.append(G.node[doctor]["label"]) new_num_adopters=len(list_I) if shift_length==5: # i estimate that adoption happens in the middle of the shift if t+5 < max_order: list_single_t_evolution.append(old_num_adopters) if t+4 < max_order: list_single_t_evolution.append(old_num_adopters) if t+3 < max_order: list_single_t_evolution.append(new_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=5 elif shift_length==4: if t+4 < max_order: list_single_t_evolution.append(old_num_adopters) if t+3 < max_order: list_single_t_evolution.append(old_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=4 elif shift_length==3: if t+3 < max_order: list_single_t_evolution.append(old_num_adopters) if t+2 < max_order: list_single_t_evolution.append(new_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=3 elif shift_length==2: if t+2 < max_order: list_single_t_evolution.append(old_num_adopters) if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=2 elif shift_length==1: if t+1 < max_order: list_single_t_evolution.append(new_num_adopters) t+=1 old_num_adopters=new_num_adopters ######## end t loop dist=compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing,list_single_t_evolution) if dist < min_sum_dist: list_small_values.append(dist) ######## end loop Niter print "fraction realizations with sum distances smaller than", min_sum_dist,"is ",float(len(list_small_values))/float(Niter),"namelly:",list_small_values dose+= delta_dose prob_infection+= delta_prob prob_Immune+= delta_prob_Immune
def main(graph_name): cutting_day = 125 # to separate training-testing G = nx.read_gml(graph_name) all_team = "NO" # as adopters or not dir_real_data = '../Results/' delta_end = 3 # >= than + or - dr difference at the end of the evolution (NO realization ends up closer than this!!!! if 2, i get and empty list!!!) Niter_training = 1000 Niter_testing = 1000 ###################################################################################### # I read the file of the actual evolution of the idea spreading in the hospital: ## ###################################################################################### if all_team == "YES": filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_all_team_as_adopters_SIMPLER.csv" else: filename_actual_evol = dir_real_data + "HospitalModel_august1_adoption_counts_SIMPLER.csv" #ya no necesito CAMBIAR TB EL NOMBRE DEL ARCHIVO EN EL CODIGO PARA COMPARAR CURVAs list_actual_evol = [] result_actual_file = csv.reader(open(filename_actual_evol, 'rb'), delimiter=',') cont = 0 for row in result_actual_file: if cont > 0: # i ignore the first line with the headers num_adopters = row[3] list_actual_evol.append(float(num_adopters)) cont += 1 list_actual_evol_training = list_actual_evol[:cutting_day] list_actual_evol_testing = list_actual_evol[(cutting_day - 1):] ################################################################## #../Results/network_final_schedule_withTeam3/infection/Average_time_evolution_Infection_p0.9_Immune0.5_1000iter_2012.dat prob_min = 0.09 prob_max = 1.01 delta_prob = 0.1 prob_Immune_min = 0.00 prob_Immune_max = 1.01 delta_prob_Immune = 0.1 dir = "../Results/network_final_schedule_withTeam3_local/infection/" dict_filenames_tot_distance = { } # i will save the filename as key and the tot distance from that curve to the original one dict_filenames_list_dict_network_states = { } # i will save the filename as key and the list of networks at cutting day as value prob_Immune = prob_Immune_min while prob_Immune <= prob_Immune_max: print "prom Immune:", prob_Immune prob_infection = prob_min while prob_infection <= prob_max: print " p:", prob_infection output_file2 = dir + "Average_time_evolution_Infection_training_p" + str( prob_infection) + "_" + "Immune" + str( prob_Immune) + "_" + str(Niter_training) + "iter_2012.dat" file2 = open(output_file2, 'wt') file2.close() # i create the empty list of list for the Niter temporal evolutions num_shifts = 0 for n in G.nodes(): G.node[n]["status"] = "S" if G.node[n]['type'] == "shift": num_shifts += 1 # list_final_I_values_fixed_p=[] # i dont care about the final values right now, but about the whole time evol list_lists_t_evolutions = [] list_dist_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_dict_network_states = [] for iter in range(Niter_training): print " iter:", iter dict_network_states = {} #######OJO~!!!!!!!!!! COMENTAR ESTO CUANDO ESTOY BARRIENDO TOOOOOOOOOODO EL ESPACIO DE PARAMETROS # file_name_indiv_evol=output_file2.strip("Average_").split('.dat')[0]+"_indiv_iter"+str(iter)+".dat" # file4 = open(file_name_indiv_evol,'wt') # file4.close() ########################################## list_I = [] #list infected doctors list_ordering = [] list_s = [] list_A = [] list_F = [] ########### set I.C. max_order = 0 for n in G.nodes(): G.node[n]["status"] = "S" # all nodes are Susceptible if G.node[n]['type'] == "shift": list_s.append(n) if G.node[n]['order'] > max_order: max_order = G.node[n]['order'] else: if G.node[n]['label'] == "Wunderink" or G.node[n][ "label"] == "Weiss": G.node[n]["status"] = "I" list_I.append(G.node[n]['label']) ######################## WHAT ABOUT SMITH AND SPORN??? if G.node[n]['type'] == "A": list_A.append(n) if G.node[n]['type'] == "F": list_F.append(n) list_single_t_evolution = [] list_single_t_evolution.append( 2.0) # I always start with TWO infected doctors!! for n in G.nodes( ): # i make some DOCTORs INMUNE (anyone except Weiss and Wunderink) if (G.node[n]['type'] == "A") or (G.node[n]['type'] == "F"): if G.node[n]['label'] != "Wunderink" and G.node[n][ "label"] != "Weiss": rand = random.random() if rand < prob_Immune: G.node[n]["status"] = "Immune" # print max_order ################# the dynamics starts: t = 1 while t < cutting_day: # loop over shifts, in order just until cutting day (training segment) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n][ 'order'] == t: flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" list_I.append( G.node[doctor]["label"]) list_single_t_evolution.append(float( len(list_I))) #/(len(list_A)+len(list_F))) t += 1 ######## end t loop for n in G.nodes(): if G.node[n]['type'] != "shift": dict_network_states[G.node[n] ["label"]] = G.node[n]["status"] list_dict_network_states.append(dict_network_states) # print "number infected at the cutting point:", len(list_I), list_I list_lists_t_evolutions.append(list_single_t_evolution) list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_training, list_single_t_evolution)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol_training[-1]) ) # i save the distance at the ending point between the current simu and actual evol # print "distance at ending point:", abs(list_single_t_evolution[-1]-list_actual_evol_training[-1]) ######## end loop Niter for the training fase list_pair_dist_std_delta_end = [] list_pair_dist_std_delta_end.append( numpy.mean(list_dist_fixed_parameters) ) # average dist between the curves over Niter list_pair_dist_std_delta_end.append( numpy.std(list_dist_fixed_parameters)) list_pair_dist_std_delta_end.append( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters)) if ( numpy.mean(list_dist_abs_at_ending_point_fixed_parameters) ) <= delta_end: # i only consider situations close enough at the ending point dict_filenames_tot_distance[ output_file2] = list_pair_dist_std_delta_end dict_filenames_list_dict_network_states[ output_file2] = list_dict_network_states file2 = open(output_file2, 'at') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter_training): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file2, s, numpy.mean(list_fixed_t) file2.close() prob_infection += delta_prob prob_Immune += delta_prob_Immune list_order_dict = compare_real_evol_vs_simus_to_be_called.pick_minimum_same_end( dict_filenames_tot_distance, "Infection_training", all_team, Niter_training) # it returns a list of tuples like this : ('../Results/network_final_schedule_withTeam3_local/infection/Average_time_evolution_Infection_training_p0.7_Immune0.0_2iter_2012.dat', [2540.0, 208.0, 1.0]) the best set of parameters being the fist one of the elements in the list. optimum_filename = list_order_dict[0][0] prob_infection = float(list_order_dict[0][0].split("_p")[1][0:3]) prob_Immune = float(list_order_dict[0][0].split("_Immune")[1][0:3]) # raw_input() print "starting testing fase with:" print "p=", prob_infection, " and Pimmune=", prob_Immune # i already know the optimum, now i run the dynamics with those values, starting from the average state on the cutting point, and test: list_dist_fixed_parameters = [] list_dist_at_ending_point_fixed_parameters = [] list_dist_abs_at_ending_point_fixed_parameters = [] list_lists_t_evolutions = [] for iter in range(Niter_testing): dict_dr_status_current_iter = dict_filenames_list_dict_network_states[ optimum_filename][iter] # print dict_dr_status_current_iter list_I = [] #list infected doctors list_Immune = [] for node in G.nodes(): if G.node[node]['type'] != "shift": label = G.node[node]['label'] G.node[node]["status"] = "S" #by default, all are susceptible G.node[node]["status"] = dict_dr_status_current_iter[label] if G.node[node]["status"] == "I": list_I.append(G.node[node]["label"]) elif G.node[node]["status"] == "Immune": list_Immune.append(G.node[node]["label"]) print "# I at the beginning of the testing fase:", len(list_I), float( len(list_I)) / 36., " and # Immune:", len(list_Immune), float( len(list_Immune)) / 36. # print " iter:",iter list_single_t_evolution = [] list_single_t_evolution.append(len(list_I)) t = cutting_day while t <= max_order: # loop over shifts, in order just until cutting day (training segment) for n in G.nodes(): if G.node[n]['type'] == "shift" and G.node[n]['order'] == t: flag_possible_infection = 0 for doctor in G.neighbors( n ): #first i check if any doctor is infected in this shift if G.node[doctor]["status"] == "I": flag_possible_infection = 1 if flag_possible_infection: for doctor in G.neighbors( n ): # then the doctors in that shift, gets infected with prob_infection if G.node[doctor]["status"] == "S": rand = random.random() if rand < prob_infection: G.node[doctor]["status"] = "I" list_I.append(G.node[doctor]["label"]) list_single_t_evolution.append(float(len(list_I))) t += 1 list_lists_t_evolutions.append(list_single_t_evolution) list_I = [] #list infected doctors list_Immune = [] for node in G.nodes(): if G.node[node]['type'] != "shift": label = G.node[node]['label'] if G.node[node]["status"] == "I": list_I.append(G.node[node]["label"]) elif G.node[node]["status"] == "Immune": list_Immune.append(G.node[node]["label"]) print " # I at the END of the testing fase:", len(list_I), float( len(list_I)) / 36., " and # Immune:", len(list_Immune), float( len(list_Immune)) / 36., "\n" list_dist_fixed_parameters.append( compare_real_evol_vs_simus_to_be_called.compare_two_curves( list_actual_evol_testing, list_single_t_evolution)) list_dist_abs_at_ending_point_fixed_parameters.append( abs(list_single_t_evolution[-1] - list_actual_evol_testing[-1]) ) # i save the distance at the ending point between the current simu and actual evol list_dist_at_ending_point_fixed_parameters.append( list_single_t_evolution[-1] - list_actual_evol_testing[-1] ) # i save the distance at the ending point between the current simu and actual evol ############### end loop Niter for the testing num_valid_endings = 0. for item in list_dist_abs_at_ending_point_fixed_parameters: if item <= delta_end: # i count how many realizations i get close enough at the ending point num_valid_endings += 1. print "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters), list_dist_fixed_parameters print "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter_testing, list_dist_abs_at_ending_point_fixed_parameters histograma_gral_negv_posit.histograma( list_dist_at_ending_point_fixed_parameters, "../Results/histogr_raw_distances_ending_test_train_infection_p" + str(prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str(Niter_training) + "iter_end_point.dat") output_file8 = "../Results/List_tot_distances_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_end_point.dat" file8 = open(output_file8, 'wt') for item in list_dist_fixed_parameters: print >> file8, item file8.close() output_file9 = "../Results/List_distances_ending_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter_end_point.dat" file9 = open(output_file9, 'wt') for item in list_dist_abs_at_ending_point_fixed_parameters: print >> file9, item file9.close() output_file5 = dir + "Average_time_evolution_Infection_testing_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_testing) + "iter_2012.dat" file5 = open(output_file5, 'wt') for s in range(len(list_single_t_evolution)): list_fixed_t = [] for iter in range(Niter_testing): list_fixed_t.append(list_lists_t_evolutions[iter][s]) print >> file5, s + cutting_day, numpy.mean(list_fixed_t) # print s+cutting_day,numpy.mean(list_fixed_t) file5.close() print "written training segment file:", optimum_filename print "written testing segment file:", output_file5 output_file10 = "../Results/Summary_results_training_segment_infection_p" + str( prob_infection) + "_" + "Immune" + str(prob_Immune) + "_" + str( Niter_training) + "iter.dat" file10 = open(output_file10, 'wt') print >> file10, "Summary results from train-testing persuasion with", Niter_training, Niter_testing, "iter (respectively), using all the individual cutting points as IC, and with values for the parameters: prob_inf ", prob_infection, " prob immune: ", prob_Immune print >> file10, "average distance of the optimum in the testing segment:", numpy.mean( list_dist_fixed_parameters), numpy.std( list_dist_fixed_parameters), list_dist_fixed_parameters print >> file10, "fraction of realizations that end within delta_doctor:", num_valid_endings / Niter_testing, list_dist_at_ending_point_fixed_parameters print >> file10, "written training segment file:", optimum_filename print >> file10, "written testing segment file:", output_file5 file10.close()