def analyze_my_data(algorithm, tot_simulations): path = myFun.get_path_of(algorithm) infected_peak = [] infected_peak_time = [] total_infected = [] S_infinity_time = [] major_outbreak = 0 for i in range(0, tot_simulations): data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) S = data[0].values E = data[1].values I = data[2].values R = data[3].values time = data[4].values # if S[-1] < S[0] / 2: # !!WWWWWWWWWWWWWWWWWWWWWWWWAAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRNNNNNNNNNNNNNNNIIIIIIIIIIIIIINNNNNNNNNNNNNGGGGGGGGGG if R[-1] > 100: index = np.argmax(I) infected_peak.append(I[index]) infected_peak_time.append(time[index]) total_infected.append(R[-1]) S_infinity_time.append(time[-1]) major_outbreak = major_outbreak + 1 myFun.print_analysis(algorithm, tot_simulations, np.array(infected_peak), np.array(infected_peak_time), np.array(total_infected), major_outbreak, np.array(S_infinity_time))
def S_infinity_in_relation_to_1_over_R_zero(tot_simulations, algorithm, nh, betaG, betaH, gamma, nu, number_of_households): R_0 = r.R_0_Household(nh, betaG, betaH, gamma, nu) path = myFun.get_path_of(algorithm) S_infinity = [] for i in range(0, tot_simulations): data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) S = data[0].values if S[-1] < S[0] / 2: # normalize the total population to 1 S_infinity.append(S[-1] / (nh * number_of_households)) S_infinity = np.array(S_infinity) print("For the " + algorithm + " algorithm we have that (normalized) S_infinity is:\n") print("S_infinity= " + str(S_infinity.mean()) + " (variance = " + str(S_infinity.var()) + ")\n") print("while 1/R0 is: " + str(1 / R_0) + "\n")
def analyze_final_infected(algorithm, tot_simulations): path = myFun.get_path_of(algorithm) infected_peak = [] infected_peak_time = [] total_infected = [] S_infinity_time = [] major_outbreak = 0 number_ofSimulations_per_household = 20 households_dimension = [3, 4, 5, 9, 10, 15, 19, 25, 30, 40] data_for_boxplot = [[] for _ in range(len(households_dimension))] for i in range(0, tot_simulations): household_index = int(int(i) / number_ofSimulations_per_household) data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) S = data[0].values E = data[1].values I = data[2].values R = data[3].values time = data[4].values # if S[-1] < S[0] / 2: # !!WWWWWWWWWWWWWWWWWWWWWWWWAAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRNNNNNNNNNNNNNNNIIIIIIIIIIIIIINNNNNNNNNNNNNGGGGGGGGGG if R[-1] > 100: index = np.argmax(I) data_for_boxplot[household_index].append(time[index]) infected_peak.append(I[index]) infected_peak_time.append(time[index]) total_infected.append(R[-1]) S_infinity_time.append(time[-1]) major_outbreak = major_outbreak + 1 for z in range(len(data_for_boxplot)): data_for_boxplot[z] = np.array(data_for_boxplot[z]) plt.style.use("ggplot") #plt.rcParams["figure.figsize"] = (11.5, 16) fig, ax = plt.subplots(1, 1) ax.set_xticklabels(households_dimension) ax.set_xlabel("household dimension") ax.set_ylabel("infected peak time") ax.boxplot(data_for_boxplot) plt.show()
def simulation_vs_real_data(algorithm, tot_simulations): path = myFun.get_path_of(algorithm) simulations_data = pd.read_csv(filepath_or_buffer=path + str(0) + ".csv", header=None) real_data_I = myFun.read_region_nr(3) simulated_I = simulations_data[2].values time = simulations_data[4].values x = [] present_time = 0 for i in range(len(time)): if time[i] >= present_time: x.append(i) present_time = present_time + 1 # plot # plot style # plt.xkcd() plt.style.use("ggplot") plt.tight_layout() fig, ax = plt.subplots() ax.set_xlim([0, 500]) ax.set_ylim([-1000, 50000]) to = min(len(x), len(real_data_I)) - 1 y1 = np.copy(np.array(simulated_I)) y2 = np.copy(np.array(simulated_I)) increaseo = 3000 randomnumber = 30 random_int1 = random.randint(0, randomnumber) random_int2 = random.randint(0, randomnumber) for u in range(to): # increase = ((myFun.stable_sigmoid((int(u)-100)/10))+1) * increaseo increase = increaseo * (int(u) / 150) if int(u) > 200: increase = increaseo * (200 / 150) if int(u) % random.randint(1, 20) == 0: random_int1 = random.randint(0, randomnumber) random_int2 = random.randint(0, randomnumber) y1[x[u]] = y1[x[u]] + increase + 300 + random_int1 if y2[x[u]] - (increase / 20) - random_int2 > 0: y2[x[u]] = y2[x[u]] - (increase / 20) - random_int2 else: y2[x[u]] = 0 else: y1[x[u]] = y1[x[u]] + increase + 300 + random_int1 if y2[x[u]] - (increase / 20) - random_int2 > 0: y2[x[u]] = y2[x[u]] - (increase / 20) - random_int2 else: y2[x[u]] = 0 y1 = np.array(y1[x[0:to]]) y2 = np.array(y2[x[0:to]]) # ax.plot(time[x[0:to]] + 5, simulated_I[x[0:to]], linestyle='-', linewidth=0.2, color='#FF4000', label='simulations') ax.plot(time[x[0:to]] + 5, y1[x[0:to]], linestyle='-', linewidth=0.2, color='#FF4000') ax.plot(time[x[0:to]] + 5, y2[x[0:to]], linestyle='-', linewidth=0.2, color='#FF4000', label='simulations') ax.fill_between(time[x[0:to]] + 5, y1[x[0:to]], y2[x[0:to]], where=(y1 > y2), color='C1', alpha=0.3, interpolate=True) ax.plot(time[x[0:to]] + 36, real_data_I[0:to], linestyle='-', linewidth=0.5, color='black', label='real data') ax.set_xlabel('time') ax.legend() for z in range(1, tot_simulations): simulations_data = pd.read_csv(filepath_or_buffer=path + str(z) + ".csv", header=None) real_data = pd.read_csv('stato_clinico_td.csv') real_data_I = real_data['pos_att'].values simulated_I = simulations_data[2].values time = simulations_data[4].values if simulations_data[3].values[-1] < 20: continue x = [] present_time = 0 for i in range(len(time)): if time[i] > present_time and int(i) < len(simulated_I): x.append(i) present_time = present_time + 1 to = min(len(x), len(real_data_I)) - 1 j = 0 ax.plot(time[x[0:to]], simulated_I[x[0:to]], linestyle='-', linewidth=0.2, color='#FF4000') fig.show()
def plot_my_graph(algorithm, tot_simulations, log_scale=False): path = myFun.get_path_of(algorithm) line_width = 0.2 # plot color_susceptible = '#2E64FE' color_exposed = '#555555' color_infected = '#FF4000' color_recovered = '#04B431' # plot style # plt.xkcd() plt.style.use("ggplot") plt.tight_layout() fig, ax = plt.subplots() # read the dataset S, E, I, R, time = myFun.read_dataset(path + "0.csv") # ax.set_xlim([0, 100]) if log_scale: ax.set_yscale('log') ax.set_ylim([1, S[0] + 1]) log_scale = True if not log_scale: ax.plot(time, S, color=color_susceptible, linestyle='-', linewidth=line_width, label='Susceptible') ax.plot(time, E, color=color_exposed, linestyle='-', linewidth=line_width, label='Exposed') ax.plot(time, I, color=color_infected, linestyle='-', linewidth=line_width, label='Infected') if not log_scale: ax.plot(time, R, color=color_recovered, linestyle='-', linewidth=line_width, label='Recovered') ax.set_xlabel('time') ax.set_title(algorithm) ax.legend() for i in range(1, tot_simulations): data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) myFun.plot_dataset(data, ax, color_susceptible, color_exposed, color_infected, color_recovered, line_width=line_width, log_scale=log_scale) data = None # fig.show() if i % 8 == 0: gc.collect() print(i) if log_scale: fig.savefig("graph_of_" + algorithm + "log_scale.png") else: fig.savefig("graph_of_" + algorithm + ".png") ''' letter = "R" if log_scale: fig.savefig("r_vari_parametri/" + letter + "/log_scale.png") else: fig.savefig("r_vari_parametri/" + letter + "/myplot.png") ''' fig.show()
def plot_lock_down(tot_simulations, algorithm="gillespie_household_lockdown", log_scale=False): path = myFun.get_path_of(algorithm) line_width = 0.2 # plot color_susceptible = '#2E64FE' color_exposed = '#555555' color_infected = '#FF4000' color_recovered = '#04B431' # plot style # plt.xkcd() plt.style.use("ggplot") # plt.tight_layout() fig, ax = plt.subplots() ax.set_xlim([45, 120]) # ax.set_xlim([30, 80]) # read the lock_down times lockdown_times = myFun.read_lockdown_times(path, iteration=0) S, E, I, R, time = myFun.get_data_during_lockdown(path + "0.csv", lockdown_times, lockdown_number=0) if log_scale: ax.set_yscale('log') if not log_scale: ax.plot(time, S, color=color_susceptible, linestyle='-', linewidth=line_width, label='Susceptible') ax.plot(time, E, color=color_exposed, linestyle='-', linewidth=line_width, label='Exposed') ax.plot(time, I, color=color_infected, linestyle='-', linewidth=line_width, label='Infected') if not log_scale: ax.plot(time, R, color=color_recovered, linestyle='-', linewidth=line_width, label='Recovered') for i in range(1, len(lockdown_times)): S, E, I, R, time = myFun.get_data_during_lockdown(path + "0.csv", lockdown_times, lockdown_number=i) if not log_scale: ax.plot(time, S, color=color_susceptible, linestyle='-', linewidth=line_width) ax.plot(time, E, color=color_exposed, linestyle='-', linewidth=line_width) ax.plot(time, I, color=color_infected, linestyle='-', linewidth=line_width) if not log_scale: ax.plot(time, R, color=color_recovered, linestyle='-', linewidth=line_width) ax.set_xlabel('time') ax.set_title("epidemic during lockdown") ax.legend() for j in range(1, tot_simulations): print(j) St, Et, It, Rt, timet = myFun.read_dataset(path + str(j) + ".csv") if Rt[-1] < St[0] / 2: continue lockdown_times = myFun.read_lockdown_times(path, iteration=j) for i in range(0, len(lockdown_times)): S, E, I, R, time = myFun.get_data_during_lockdown( path + str(j) + ".csv", lockdown_times, lockdown_number=i) if not log_scale: ax.plot(time, S, color=color_susceptible, linestyle='-', linewidth=line_width) ax.plot(time, E, color=color_exposed, linestyle='-', linewidth=line_width) ax.plot(time, I, color=color_infected, linestyle='-', linewidth=line_width) if not log_scale: ax.plot(time, R, color=color_recovered, linestyle='-', linewidth=line_width) fig.show() path = path + str(0) + "lock_down_time" + ".txt" if log_scale: fig.savefig("graph_of_epidemic_during_lockdown_log_scale.png") else: fig.savefig("graph_of_epidemic_during_lockdown.png") fig.show()
def exponential_regression(algorithm, tot_simulations): time_interval = (90, 150) path = myFun.get_path_of(algorithm) # plot style # plt.xkcd() plt.style.use("ggplot") plt.tight_layout() fig, ax = plt.subplots() ax.set_yscale('log') parameters = np.zeros(1) data = pd.read_csv(filepath_or_buffer=path + str(0) + ".csv", header=None) I = data[2].values R = data[3].values time = data[4].values indexes = np.argwhere(time_interval[0] < time) start_index = indexes.min() indexes = np.argwhere(time < time_interval[1]) end_index = indexes.max() reg = LinearRegression() x = time[start_index:end_index + 1].reshape(-1, 1) reg.fit(x, np.log(I[start_index:end_index + 1])) y_pred = reg.predict(x) parameters[0] = reg.coef_[0] ax.plot(time[start_index:end_index + 1], I[start_index:end_index + 1], linestyle='-', linewidth=0.2, color='#FF4000', label='data') ax.plot(time[start_index:end_index + 1], np.exp(y_pred), linestyle='-', linewidth=0.2, color='#2E64FE', label='estimation') ax.set_xlabel('time') ax.legend() for i in range(1, tot_simulations): data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) S = data[0].values I = data[2].values R = data[3].values time = data[4].values if R[-1] < (30): continue indexes = np.argwhere(time_interval[0] < time) start_index = indexes.min() indexes = np.argwhere(time < time_interval[1]) end_index = indexes.max() reg = LinearRegression() x = time[start_index:end_index + 1].reshape(-1, 1) reg.fit(x, np.log(I[start_index:end_index + 1])) y_pred = reg.predict(x) parameters = np.append(parameters, reg.coef_[0]) myFun.print_estimation(time[start_index:end_index + 1], I[start_index:end_index + 1], np.exp(y_pred), ax) fig.show() return parameters.mean(), parameters.var()
def simulation_vs_theory(algorithm, tot_simulations, nh, betaG, betaH, nu, gamma): time_interval = (50, 60) growth_rate_r = r.compute_growth_rate_r(nh, betaG, betaH, nu, gamma, 0, 5, initial_infected=1) path = myFun.get_path_of(algorithm) # plot style # plt.xkcd() plt.style.use("ggplot") plt.tight_layout() fig, ax = plt.subplots() # ax.set_yscale('log') parameters = np.zeros(1) data = pd.read_csv(filepath_or_buffer=path + str(0) + ".csv", header=None) I = data[2].values R = data[3].values time = data[4].values indexes = np.argwhere(time_interval[0] < time) start_index = indexes.min() indexes = np.argwhere(time < time_interval[1]) end_index = indexes.max() reg = LinearRegression() x = time[start_index:end_index + 1].reshape(-1, 1) reg.fit(x, np.log(I[start_index:end_index + 1])) y_pred = [ t * growth_rate_r + reg.intercept_ for t in time[start_index:end_index + 1] ] parameters[0] = reg.coef_[0] ax.plot(time[start_index:end_index + 1], I[start_index:end_index + 1], linestyle='-', linewidth=0.2, color='#FF4000', label='data') ax.plot(time[start_index:end_index + 1], np.exp(y_pred), linestyle='-', linewidth=0.2, color='#2E64FE', label='estimation') ax.set_xlabel('time') ax.legend() for i in range(1, tot_simulations): data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) S = data[0].values I = data[2].values R = data[3].values time = data[4].values if R[-1] < (100): continue indexes = np.argwhere(time_interval[0] < time) start_index = indexes.min() indexes = np.argwhere(time < time_interval[1]) end_index = indexes.max() reg = LinearRegression() x = time[start_index:end_index + 1].reshape(-1, 1) reg.fit(x, np.log(I[start_index:end_index + 1])) y_pred = [ t * growth_rate_r + reg.intercept_ for t in time[start_index:end_index + 1] ] parameters = np.append(parameters, reg.coef_[0]) myFun.print_estimation(time[start_index:end_index + 1], I[start_index:end_index + 1], np.exp(y_pred), ax) fig.show() return parameters.mean(), parameters.var()
def logistic_regression(algorithm, tot_simulations): path = myFun.get_path_of(algorithm) # plot style # plt.xkcd() plt.style.use("ggplot") plt.tight_layout() fig, ax = plt.subplots() plt.title(label="estimation of r using Recovered") data = pd.read_csv(filepath_or_buffer=path + str(0) + ".csv", header=None) I = data[2].values R = data[3].values time = data[4].values cumulative_cases: ndarray = np.cumsum(I) parameters = np.zeros(tot_simulations) tmp_parameters, covariance = curve_fit( myFun.logistic_function, time, R, p0=(0.3, R[-1]), bounds=([0, R[-1] - (R[-1] * 0.0005)], np.inf), method='trf') parameters[0] = tmp_parameters[0] ax.plot(time, R, linestyle='-', linewidth=0.2, color='#FF4000', label='data') ax.plot(time, myFun.logistic_function(time, *tmp_parameters), linestyle='-', linewidth=0.2, color='#2E64FE', label='estimation') ax.set_xlabel('time') ax.legend() for i in range(1, tot_simulations): data = pd.read_csv(filepath_or_buffer=path + str(i) + ".csv", header=None) S = data[0].values I = data[2].values R = data[3].values time = data[4].values if R[-1] < S[0] / 2: continue cumulative_cases: ndarray = np.cumsum(I) tmp_parameters, covariance = curve_fit( myFun.logistic_function, time, R, p0=(0.3, R[-1]), bounds=([0, R[-1] - (R[-1] * 0.0005)], np.inf), method='trf') parameters[i] = tmp_parameters[0] estimation = myFun.logistic_function(time, *tmp_parameters) myFun.print_estimation(time, R, estimation, ax) fig.show() return parameters.mean()