rolling_mean(cumul_tests['TESTS_ALL'], 14), label="Tests/day") plt.plot(cumul_tests['DATE'], rolling_mean(cumul_tests['TESTS_ALL_POS'], 14), label="Positive/day") print(VACC) print(list(VACC.columns.values)) first_dose = VACC[["DATE", "DOSE", "COUNT"]].query('DOSE == "A"') cumul_vacc_1st_dose = first_dose.groupby("DATE").sum().reset_index() second_dose = VACC[["DATE", "DOSE", "COUNT"]].query('DOSE == "B"') cumul_vacc_2nd_dose = second_dose.groupby("DATE").sum().reset_index() plt.plot(cumul_vacc_1st_dose['DATE'], rolling_mean(cumul_vacc_1st_dose['COUNT'], 14), label="1st dose/day") plt.plot(cumul_vacc_2nd_dose['DATE'], rolling_mean(cumul_vacc_2nd_dose['COUNT'], 14), label="2nd dose/day") plt.title("Sciensano dataset") plt.legend() plt.show() df = load_model_data(True) plt.plot(df['DATE'], df["NUM_POSITIVE"]) plt.plot(df['DATE'], df["NUM_HOSPITALIZED"]) plt.plot(df['DATE'], df["NUM_CRITICAL"]) plt.show()
dHdt = tau * SP - delta * H - gamma2 * H dCdt = delta * H - theta * C - gamma3 * C dFdt = theta * C dRdt = gamma1 * SP + gamma2 * H + gamma3 * C + gamma4 * A - alphaR dHIndt = tau * SP dFIndt = theta * C dSPIndt = sigma * A DTESTEDDT = dSPIndt * mu DTESTEDPOSDT = DTESTEDDT * eta return [dSdt, dEdt, dAdt, dSPdt, dHdt, dCdt, dFdt, dRdt, dHIndt, dFIndt, dSPIndt, DTESTEDDT, DTESTEDPOSDT] if __name__ == "__main__": observations = load_model_data() rows = np.array(observations) days = len(rows) dates = [observations.DATE.iloc[0].date(), date(2020, 3, 13), date(2020, 5, 4), date(2020, 6, 8), date(2020, 7, 25), date(2020, 9, 24), date(2020, 10, 6), date(2020, 11, 2), date(2020, 12, 1), date(2021, 1, 27), date(2021, 3, 1), date(2021, 3, 27), observations.DATE.iloc[-1].date()] # list of tuples (start, end) for each period with significantly distinctive covid-19 measures periods_in_days = periods_in_days(dates) periods_in_days = periods_in_days[1:] # we start fitting from the 2nd period to start with higher values # solution 2, here start from 0. but use the 0 to compute the date so not cool... et marche moins bien que sol 1 # Parameters to keep constant across periods constantParamNames = ("Rho", "Sigma", "Gamma1", "Gamma2", "Gamma4") # Must keep the same order of parameters ! ms = SEIR_HCD(stocha = False, constantParamNames = constantParamNames)
def team_training(): global periods_in_days observations = load_model_data() rows = np.array(observations) days = len(rows) dates = [observations.DATE.iloc[0].date(), date(2020, 3, 13), date(2020, 5, 4), date(2020, 6, 8), date(2020, 7, 25), date(2020, 9, 24), date(2020, 10, 6), date(2020, 11, 2), date(2020, 12, 1), date(2021, 1, 27), date(2021, 3, 1), date(2021, 3, 27), observations.DATE.iloc[-1].date()] # list of tuples (start, end) for each period with significantly distinctive covid-19 measures periods_in_days = periods_in_days(dates) periods_in_days = periods_in_days[1:] # we start fitting from the 2nd period to start with higher values # solution 2, here start from 0. but use the 0 to compute the date so not cool... et marche moins bien que sol 1 ms = ModelOptimizerTestBench() N = 11492641 # population belge en 2020 E0 = 80000 A0 = 14544 SP0 = 9686 H0 = rows[periods_in_days[0][0]][ObsEnum.NUM_HOSPITALIZED.value] C0 = rows[periods_in_days[0][0]][ObsEnum.NUM_CRITICAL.value] R0 = np.sum(rows[:periods_in_days[0][0], ObsEnum.RSURVIVOR.value]) # = 0 F0 = rows[periods_in_days[0][0]][ObsEnum.NUM_FATALITIES.value] S0 = N - E0 - A0 - SP0 - H0 - C0 - R0 - F0 IC = [S0, E0, A0, SP0, H0, C0, F0, R0] print(IC) ms.set_IC(conditions = IC) p_values, p_bounds = ms.get_initial_parameters() initial_params = Parameters() for p_name, p_val in p_values.items(): pmin, pmax = p_bounds[p_name] initial_params.add(p_name, p_val, min=pmin, max=pmax) sres = np.array([]) for ndx_period, period in enumerate(periods_in_days): print(f"Period {ndx_period}/{len(periods_in_days)}: [{period[0]}, {period[1]}]") # optimizer='GLOBAL', ms.fit_parameters(rows[period[0]:period[1], :], initial_params) #,optimizer='GLOBAL', randomPick = False, picks = 1000) sres_temp = ms.predict() if sres_temp.any(): ms.set_IC(conditions = sres_temp[-1, 0:8]) if not np.any(sres): sres = sres_temp[:13,:] * 0 #solution 1, artificielement mettre des 0 pour les X premier jours, où plus propre, mettre IC 13 fois à voir. sres = np.concatenate((sres, sres_temp)) # fait partie de solution 1 # sres = sres_temp else: sres = np.concatenate((sres, sres_temp)) version = 3 """plt.figure() plt.title('HOSPITALIZED / PER DAY fit') t = StateEnum.DHDT plt.plot(sres[:, t.value], label = str(t) + " (model)") u = ObsEnum.DHDT plt.plot(rows[:, u.value], "--", label = str(u) + " (real)") #plt.savefig('img/v{}-dhdt.pdf'.format(version)) plt.show()""" plt.figure() plt.title('Hospitalized') t = StateEnum.HOSPITALIZED plt.plot(sres[:, t.value], label = str(t) + " (model)") u = ObsEnum.NUM_HOSPITALIZED plt.plot(rows[:, u.value], "--", label = str(u) + " (real)") plot_periods(plt, dates) #plt.savefig('img/v{}-hospitalized.pdf'.format(version)) plt.show()
log_file = os.path.join('IlfoLog', log_file) with open(log_file, 'r') as f: data = f.readlines() data = data[11:] data = [time2int(d.split(' ')[1]) for d in data] ilfo_cost = [] for i in range(len(data) - 1): ilfo_cost.append(data[i + 1] - data[i]) gan_model = 'AbuseGanModel/' + task_name + '_hyper0.0001/netG_epoch_90.pth' gan_model = torch.load(gan_model) model = Generator(3, 3) print(task_name, 'load successful') model.load_state_dict(gan_model) gan_model = model.to(device).eval() model, trainSet, testSet = load_model_data(model_name, data_set) model = model.to(device).eval() test_loader = DataLoader(testSet, batch_size=256, shuffle=False) gan_cost = [] for (x, y) in tqdm(test_loader): x = x.to(device) t1 = time.time() x = gan_model(x) + x pred = model(x, device) t2 = time.time() gan_cost.append(t2 - t1) res = [[gan_cost[i], ilfo_cost[i]] for i in range(len(ilfo_cost))] with open(os.path.join(resDir, task_name + "_overhead.csv"), "w") as csvfile: writer = csv.writer(csvfile) writer.writerow(["gan", "ilfo"])
def get_initial_parameters(self, paramNames = None, randomPick = False, picks = 1000): # min_incubation_time = 5 # max_incubation_time = 6 # # min_presymptomatic_time = 1 # max_presymptomatic_time = 3 # # min_symptomatic_time = 5 # max_symptomatic_time = 10 # # mortality_rate_in_ICU = 0.279 # mortality_rate_in_simple_hospital_beds = 0.168 # # avg_stay_in_ICU_in_case_of_death = 19.3 # avg_stay_in_simple_hospital_beds_in_case_of_death = 6.1 # # avg_stay_in_ICU_in_case_of_recovery = 9.9 # avg_stay_in_hospital_simple_beds_in_case_of_recovery = 8 observations = load_model_data() min_incubation_time = 5 max_incubation_time = 6 min_presymptomatic_time = 1 max_presymptomatic_time = 3 min_symptomatic_time = 5 max_symptomatic_time = 10 min_fraction_of_asymptomatic = 0.17 max_fraction_of_asymptomatic = 0.25 mortality_rate_in_ICU = 0.279 mortality_rate_in_simple_hospital_beds = 0.168 avg_stay_in_ICU_in_case_of_death = 19.3 avg_stay_in_simple_hospital_beds_in_case_of_death = 6.1 avg_stay_in_ICU_in_case_of_recovery = 9.9 avg_stay_in_hospital_simple_beds_in_case_of_recovery = 8 fraction_of_hospitalized_not_transfering_to_ICU = 0.753 fraction_of_hospitalized_transfering_to_ICU = 1 - fraction_of_hospitalized_not_transfering_to_ICU # ---------------------------------- # Tau (SP -> H) # -> will probably not be constant over time avg_time_for_transfer_from_SP_to_H = 5.7 tau_0 = 0.01 / avg_time_for_transfer_from_SP_to_H # blind hypothesis: 1 symptomatic out of 100 goes to the hospital tau_min = 0.0001 / avg_time_for_transfer_from_SP_to_H # blind hypothesis: 1 symptomatic out of 10000 goes to the hospital tau_max = 0.1 / avg_time_for_transfer_from_SP_to_H # blind hypothesis: 1 symptomatic out of 10 goes to the hospital # ---------------------------------- # Gamma 4 (A -> R) # -> probably constant over time gamma4_max = max_fraction_of_asymptomatic / min_incubation_time gamma4_min = min_fraction_of_asymptomatic / (max_incubation_time + max_symptomatic_time) gamma4_0 = (gamma4_max + gamma4_min) / 2 # ---------------------------------- # Gamma1 (SP -> R) # -> probably constant over time gamma1_max = (1 - tau_min) / min_symptomatic_time gamma1_min = (1 - tau_max) / max_symptomatic_time gamma1_0 = (gamma1_max + gamma1_min) / 2 # Discuter du bazard en dessous # ---------------------------------- # Beta (S -> E) # -> will vary a lot over time R0_min = 0.1 # should be set < 1 if we want to permit a fall after a peak R0_max = 4 R0_avg = (R0_min + R0_max) / 2 min_infectious_time = min_symptomatic_time + min_presymptomatic_time max_infectious_time = max_symptomatic_time + max_presymptomatic_time avg_infectious_time = (min_infectious_time + max_infectious_time) / 2 beta_0 = R0_avg / avg_infectious_time beta_min = R0_min / max_infectious_time beta_max = R0_max / min_infectious_time # ---------------------------------- # Delta (H -> C) # -> should vary with the influence of the British variant fraction_of_hospitalized_transfering_to_ICU_in_case_of_eventual_recovery = fraction_of_hospitalized_transfering_to_ICU / \ avg_stay_in_ICU_in_case_of_recovery fraction_of_hospitalized_transfering_to_ICU_in_case_of_eventual_death = fraction_of_hospitalized_transfering_to_ICU / \ avg_stay_in_ICU_in_case_of_death fraction_of_hospitalized_not_transfering_to_ICU_in_case_of_eventual_recovery = fraction_of_hospitalized_not_transfering_to_ICU / \ avg_stay_in_hospital_simple_beds_in_case_of_recovery fraction_of_hospitalized_not_transfering_to_ICU_in_case_of_eventual_death = fraction_of_hospitalized_not_transfering_to_ICU / \ avg_stay_in_simple_hospital_beds_in_case_of_death delta_0 = (1 - mortality_rate_in_ICU) * fraction_of_hospitalized_transfering_to_ICU_in_case_of_eventual_recovery + \ mortality_rate_in_ICU * fraction_of_hospitalized_transfering_to_ICU_in_case_of_eventual_death delta_max = 1.5 * fraction_of_hospitalized_transfering_to_ICU_in_case_of_eventual_recovery # hypothesis: all people eventually recover after they transfer to ICU, they have thus on average a shorter stay in ICU, and thus a higher delta delta_min = fraction_of_hospitalized_transfering_to_ICU_in_case_of_eventual_death # hypothesis: all people eventually die after they transfer to ICU, they have thus on average a longer stay in ICU, and thus a lower delta # delta_min = 0.01 # blind hypothesis # delta_max = 0.06 # blind hypothesis # delta_0 = (1 - fraction_of_hospitalized_not_transfering_to_ICU) / \ # ((avg_stay_in_hospital_simple_beds_in_case_of_recovery + avg_stay_in_ICU_in_case_of_death) / 2) # semi-blind hyptohesis # ---------------------------------- # Theta1 (H -> F) # -> should vary with the influence of the British variant # Hypothesis: stay and mortality in simple hospital beds lower bounds the corresponding numbers in ICU theta1_min = 0.7 * mortality_rate_in_simple_hospital_beds / avg_stay_in_ICU_in_case_of_death theta1_max = 1.3 * mortality_rate_in_ICU / avg_stay_in_simple_hospital_beds_in_case_of_death theta1_0 = mortality_rate_in_simple_hospital_beds * fraction_of_hospitalized_not_transfering_to_ICU_in_case_of_eventual_death # ---------------------------------- # Theta2 (C -> F) # -> should vary with the influence of the British variant # Hypothesis: stay and mortality in simple hospital beds lower bounds the corresponding numbers in ICU theta2_min = 0.7 * mortality_rate_in_simple_hospital_beds / avg_stay_in_ICU_in_case_of_death # semi-blind hypothesis theta2_max = 1.3 * mortality_rate_in_ICU / avg_stay_in_simple_hospital_beds_in_case_of_death # semi-blind hypothesis theta2_0 = mortality_rate_in_ICU / avg_stay_in_ICU_in_case_of_death # ---------------------------------- # Gamma2 (H -> R) # -> probably constant over time gamma2_min = 0.001 # blind hypothesis gamma2_0 = (1 - mortality_rate_in_simple_hospital_beds) * fraction_of_hospitalized_not_transfering_to_ICU_in_case_of_eventual_recovery # (1 - mortality_rate_in_simple_hospital_beds) / avg_stay_in_hospital_simple_beds_in_case_of_recovery gamma2_max = fraction_of_hospitalized_not_transfering_to_ICU_in_case_of_eventual_recovery # blind hypothesis # ---------------------------------- # Gamma3 (C -> R) # -> probably constant over time gamma3_min = 0.001 # blind hypothesis gamma3_0 = (1 - mortality_rate_in_ICU) / avg_stay_in_ICU_in_case_of_recovery gamma3_max = 1 / avg_stay_in_ICU_in_case_of_recovery # blind hypothesis: everyone eventually recover in ICU # ---------------------------------- # Rho (E -> A) # -> probably constant over time rho_max = 1 / min_incubation_time rho_0 = 2 / (min_incubation_time + max_incubation_time) rho_min = 1 / max_incubation_time # ---------------------------------- # Sigma (A -> SP) # -> probably constant over time sigma_max = (1 - min_fraction_of_asymptomatic) / min_presymptomatic_time sigma_min = (1 - max_fraction_of_asymptomatic) / max_presymptomatic_time sigma_0 = (sigma_max + sigma_min) / 2 # ---------------------------------- # Mu (A -> T) # -> will vary over time with the test capacity and the testing rules mu1_max = 0.7 # blind hypothesis mu1_min = 0 # 0.4 # blind hypothesis mu1_0 = (mu1_min + mu1_max) / 2 # blind hypothesis # ---------------------------------- # Mu (SP -> T) # -> will vary over time with the test capacity and the testing rules mu2_max = 0.9 # blind hypothesis mu2_min = 0.1 # 0.4 # blind hypothesis mu2_0 = (mu1_min + mu1_max) / 2 # blind hypothesis # ---------------------------------- # Eta (T -> TP) # -> will vary a lot over time with the peak of contamination positivity_rates = observations.NUM_POSITIVE / observations.NUM_TESTED eta_max = np.max(positivity_rates) # 0.3288 # max 32.8 % of positive tests eta_min = np.min(positivity_rates) # 0.009 # min 0.9 % of positive tests eta_0 = np.median(positivity_rates) # 0.08 # on average, 8% of positive tests # ---------------------------------- # Alpha #alpha_min = 0.001 #alpha_max = 0.999 #alpha_0 = 0.01 alpha_min = 0 alpha_max = 0 alpha_0 = 0 #alpha_bounds = [0.001, 0.01, 0.95] # ---------------------------------- gamma1_bounds = (gamma1_min, gamma1_max) gamma2_bounds = (gamma2_min, gamma2_max) gamma3_bounds = (gamma3_min, gamma3_max) gamma4_bounds = (gamma4_min, gamma4_max) beta_bounds = (beta_min, beta_max) tau_bounds = (tau_min, tau_max) delta_bounds = (delta_min, delta_max) sigma_bounds = (sigma_min, sigma_max) rho_bounds = (rho_min, rho_max) theta1_bounds = (theta1_min, theta1_max) theta2_bounds = (theta2_min, theta2_max) mu1_bounds = (mu1_min, mu1_max) mu2_bounds = (mu2_min, mu2_max) eta_bounds = (eta_min, eta_max) bounds = [beta_bounds, rho_bounds, sigma_bounds, tau_bounds, delta_bounds, theta1_bounds, theta2_bounds, gamma1_bounds, gamma2_bounds, gamma3_bounds, gamma4_bounds, mu1_bounds, mu2_bounds, eta_bounds] if not (self._immunity): # alpha_bounds = [alpha_min, bestParams['Alpha'], alpha_max] alpha_bounds = (alpha_min, alpha_max) bounds += [alpha_bounds] bestParams = [beta_0, rho_0, sigma_0, tau_0, delta_0, theta1_0, theta2_0, gamma1_0, gamma2_0, gamma3_0, gamma4_0, mu1_0, mu2_0, eta_0] if not(self._immunity): bestParams += [alpha_0] if randomPick: best = float("inf") for test in range(picks): if (test % (picks/10) == 0): print("Pre test of the parameters: {} of {}".format(test, picks)) gamma1 = random.uniform(gamma1_min, gamma1_max) gamma2 = random.uniform(gamma2_min, gamma2_max) gamma3 = random.uniform(gamma3_min, gamma3_max) gamma4 = random.uniform(gamma4_min, gamma4_max) beta = random.uniform(beta_min, beta_max) tau = random.uniform(tau_min, tau_max) delta = random.uniform(delta_min, delta_max) sigma = random.uniform(sigma_min, sigma_max) rho = random.uniform(rho_min, rho_max) theta1 = random.uniform(theta1_min, theta1_max) theta2 = random.uniform(theta2_min, theta2_max) mu1 = random.uniform(mu1_min, mu1_max) mu2 = random.uniform(mu2_min, mu2_max) eta = random.uniform(eta_min, eta_max) paramValues = [beta, rho, sigma, tau, delta, theta1, theta2, gamma1, gamma2, gamma3, gamma4, mu1, mu2, eta] if not(self._immunity): alpha = random.uniform(alpha_min, alpha_max) paramValues += [alpha] # Pas en dict ici car ça poserait un problème dans fit_parameters() score = self.plumb(paramValues, isMLE = False) if score < best: best = score print("Score preprocessing parameters: {}".format(score)) bestParams = paramValues print('Best preprocessing parameters: {}'.format(dict(zip(self._paramNames, bestParams)))) bestParams = dict(zip(self._paramNames, bestParams)) bounds = dict(zip(self._paramNames, bounds)) bestParams = dict((k, bestParams[k]) for k in paramNames) bounds = dict((k, bounds[k]) for k in paramNames) return bestParams, bounds