WordPress.com



# -*- coding: utf-8 -*-"""Created on Sat Mar 14 11:22:40 2020@author: japa"""# basic model according state diagram: Covid-19 transicion de estados# includes extraordinary_measurement# includes mmean & standard deviation# includes export data to excel fileimport numpy as npimport pandas as pdimport random as rdimport matplotlib.pyplot as pltfrom scipy.stats import norm# coronavirus simulation# especimen data definitionpopulation = int(2e5)specimen_df = pd.DataFrame(index=np.arange(population), columns=['days_process', 'state', 'n_infected', 'next_state'])# days_process: days in de state under process# n_infected: number of infected individulas by index individual# data initializationspecimen_df.days_process = 0specimen_df.state = 'healthy'specimen_df.n_infected = 0 # number of people infected by the specimenspecimen_df.next_state = 'healthy'# periodes of time for state trasition p_size = 100 # size on proximity# mean valuesinfd2asymp = 4 # average number of days from infected to develop the diseaseasymp2recovered = 20 # average number of days from asymptomatic to recoveredasymp2symp = 11 # average number of days from asymptomatic to symptomaticsymp2diagn = 6 # average number of aditional days to be diagnosed. go to quarantinediagn2hosp = 6 # average number of days to enter the hospitalsymp2recovered = 20 # average number of days from symptomatic to recoveredsymp_diag2recovered = 16 # average number of days from symptomatic to recoveredhosp2ICU = 4 # average number of days to ener ICUhosp2recovered = 16 # average number of days from hospital to recoveredICU2death = 4 # average number of days till dieICU2recovered = 16 # averagenumber of days from ICU to recovered# standard deviationsd_infd2asymp = 2 # standard deviation of days from infected to develop the diseasesd_asymp2symp = 6 # standard deviation of days from asymptomatic to symptomaticsd_asymp2recovered = 6 # standard deviation of days from asymptomatic to recoveredsd_symp2diagn = 3 # standard deviation of aditional days to be diagnosed. go to quarantinesd_diagn2hosp = 3 # standard deviation of days to enter the hospitalsd_symp2recovered = 8 # standard deviation of days from symptomatic to recoveredsd_symp_diag2recovered = 8 # standard deviation of days from symptomatic to recoveredsd_hosp2ICU = 2 # standard deviationr of days to ener ICUsd_hosp2recovered = 8 # standard deviationr of days from hospital to recoveredsd_ICU2death = 2 # standard deviation of days till diesd_ICU2recovered = 16 # standard deviation of days from ICU to recovered# transmission parametershealthy_immune = 0.6 # immunes or not being infected by same reasonnear_specimens = 10 # number of potencial infectedmovility = 0.4 # 40% of people moves from zone of proximity every daymov_rate = 0.25 # exponential factor for distace calculationtransm_rate = 4 * population / 1e5# mean number of specimens infected by somebodysd_transm_rate = 3 * population / 1e5# standard deviation of being infected by somebodytransmission_prob = 0.1 # probability of infection when in contact# probabilities of state transitionssymptomatic_prob = 0.3 # probability of being symptomatic from asymptomaticdiagnosed_prob = 0.3 # probability of symptomatic being diagnosedhospitalization_prob = 0.4 # probability of hospitalization once symptomaticICU_prob = 0.4 # probability of ICU once in hosspital. death_prob = 0.5 # probability of death once in ICU# especimen state definition"""healthyinfected # infected incubating. infected_diagnosedasymptomatic # asymptomatic_diagnosedsymptomatic #symptomatic_diagnosedsymptomatic_hospitalsymptomatic_ICUdeathasymptomatic_recoveredasymptomatic_diagnosed_recoveredsymptomatic_recoveredsymptomatic_diagnosed_recoveredICU_recovered"""# patient zero initializationesp_indx = int(rd.random() * population)specimen_df.loc[esp_indx, 'state'] = 'infected'max_days = 200# model data frame columnscolumns = ['healthy', 'infected', 'asymptomatic', 'symptomatic', 'symptomatic_diagnosed', 'symptomatic_hospital', 'symptomatic_ICU', 'deaths', 'asymptomatic_recovered', 'symptomatic_recovered', 'ICU_recovered', 'mean_infected', 'sd_infected']model_data_df = pd.DataFrame(index=np.arange(max_days), columns=columns)model_data_df['healthy'] = np.zeros((max_days))model_data_df['infected'] = np.zeros((max_days))model_data_df['asymptomatic'] = np.zeros((max_days))model_data_df['symptomatic'] = np.zeros((max_days))model_data_df['symptomatic_diagnosed'] = np.zeros((max_days))model_data_df['symptomatic_hospital'] = np.zeros((max_days))model_data_df['symptomatic_ICU'] = np.zeros((max_days))model_data_df['deaths'] = np.zeros((max_days))model_data_df['asymptomatic_recovered'] = np.zeros((max_days))model_data_df['symptomatic_hospital_recovered'] = np.zeros((max_days))model_data_df['symptomatic_recovered'] = np.zeros((max_days))model_data_df['symptomatic_diagnosed_recovered'] = np.zeros((max_days))model_data_df['ICU_recovered'] = np.zeros((max_days))model_data_df['mean_infected'] = np.zeros((max_days))model_data_df['sd_infected'] = np.zeros((max_days))def transition(val, loc , scale): cumm_prob = norm.cdf(val, loc, scale) decision = True if (rd.random() > cumm_prob): decision = False return decision# day of extraordinary measurementsextraordinary_measurements = max_days + 1for days in range(max_days): print(days, ', ') if days == extraordinary_measurements: #Contention measurements near_specimens = 1 # number of potencial infected movility = 0.2 # 40% of people moves from zone of proximity every day mov_rate = 0.15 # exponential factor for distace calculation # cuarentena perc_quarentine = 0.8 indx_healthy = specimen_df[specimen_df['state'] == 'healthy'].index num_cand_healthy2quarentine = int(perc_quarentine * len(indx_healthy)) indx_healthy2quarentine = specimen_df.loc[indx_healthy].sample(n = num_cand_healthy2quarentine).index specimen_df.loc[indx_healthy2quarentine, 'state'] = 'quarentine' # infection process for esp_indx in range(population): state = specimen_df.loc[esp_indx, 'state'] infection = not transition(specimen_df.loc[esp_indx, 'n_infected'], transm_rate, sd_transm_rate) if (((state == 'asymptomatic') or (state == 'symptomatic')) and infection): # distace to specimen to bo potencialy infected if rd.random() > movility: #local influence dist = int(rd.random() * p_size) else: #distant influence dist = int(0.5 * population * np.exp(- rd.random()/mov_rate)) for near_indx in range(near_specimens): target_indx = (esp_indx + int(rd.random() * dist)) % population if (rd.random() < transmission_prob and (specimen_df.loc[target_indx, 'state'] == 'healthy')): specimen_df.loc[target_indx, 'state'] = 'infected' specimen_df.loc[esp_indx, 'n_infected'] += 1 # state transition for esp_indx in range(population): state = specimen_df.loc[esp_indx, 'state'] # ----------------------------------------- if state == 'infected': if transition(specimen_df.loc[esp_indx, 'days_process'], infd2asymp, sd_infd2asymp): specimen_df.loc[esp_indx, 'days_process'] = 0 # start new process specimen_df.loc[esp_indx, 'state'] = 'asymptomatic' if rd.random() < symptomatic_prob: # next symptomatic cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic' else: # next asymptomatic cases specimen_df.loc[esp_indx, 'next_state'] = 'asymptomatic_recovered' else: specimen_df.loc[esp_indx, 'days_process'] += 1 # ----------------------------------------- elif state == 'asymptomatic': days_process = specimen_df.loc[esp_indx, 'days_process'] specimen_df.loc[esp_indx, 'days_process'] += 1 if specimen_df.loc[esp_indx, 'next_state'] == 'symptomatic': if transition(days_process, asymp2symp, sd_asymp2symp): specimen_df.loc[esp_indx, 'days_process'] = 0 specimen_df.loc[esp_indx, 'state'] = 'symptomatic' if rd.random() < diagnosed_prob: # next symptomatic to diagnosed cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic_diagnosed' else: # next asymptomatic recovered cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic_recovered' elif transition(days_process, asymp2recovered, sd_asymp2recovered): # asimptomatic cases specimen_df.loc[esp_indx, 'days_process'] = 0 specimen_df.loc[esp_indx, 'state'] = 'asymptomatic_recovered' # ----------------------------------------- elif state == 'symptomatic': days_process = specimen_df.loc[esp_indx, 'days_process'] specimen_df.loc[esp_indx, 'days_process'] += 1 if specimen_df.loc[esp_indx, 'next_state'] == 'symptomatic_diagnosed': # symptomatic to diagnosed cases if transition(days_process, symp2diagn, sd_symp2diagn) : specimen_df.loc[esp_indx, 'days_process'] = 0 specimen_df.loc[esp_indx, 'state'] = 'symptomatic_diagnosed' if rd.random() < hospitalization_prob: # next diagnosed to hospital cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic_hospital' else: # next symptomatic diagnosed recovered cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic_diagnosed_recovered' elif transition(days_process, symp2recovered, sd_symp2recovered): # symptomatic_recovered cases specimen_df.loc[esp_indx, 'state'] = 'symptomatic_recovered' specimen_df.loc[esp_indx, 'days_process'] = 0 # ----------------------------------------- elif state == 'symptomatic_diagnosed': days_process = specimen_df.loc[esp_indx, 'days_process'] specimen_df.loc[esp_indx, 'days_process'] += 1 if specimen_df.loc[esp_indx, 'next_state'] == 'symptomatic_hospital': # symptomatic diagnosed to hospital cases if transition(days_process, diagn2hosp, sd_diagn2hosp): specimen_df.loc[esp_indx, 'state'] = 'symptomatic_hospital' specimen_df.loc[esp_indx, 'days_process'] = 0 if rd.random() < ICU_prob: # next hospital to ICU cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic_ICU' else: # next symptomatic hospital recovered cases specimen_df.loc[esp_indx, 'next_state'] = 'symptomatic_hospital_recovered' elif transition(days_process, symp_diag2recovered, sd_symp_diag2recovered): # symptomatic_recovered cases specimen_df.loc[esp_indx, 'state'] = 'symptomatic_diagnosed_recovered' specimen_df.loc[esp_indx, 'days_process'] = 0 # ----------------------------------------- elif state == 'symptomatic_hospital': days_process = specimen_df.loc[esp_indx, 'days_process'] specimen_df.loc[esp_indx, 'days_process'] += 1 if specimen_df.loc[esp_indx, 'next_state'] == 'symptomatic_ICU': # hospital to ICU cases if transition(days_process, hosp2ICU, sd_hosp2ICU): specimen_df.loc[esp_indx, 'state'] = 'symptomatic_ICU' specimen_df.loc[esp_indx, 'days_process'] = 0 if rd.random() < death_prob: # next ICU to death cases specimen_df.loc[esp_indx, 'next_state'] = 'death' else: # next ICU recovered cases specimen_df.loc[esp_indx, 'next_state'] = 'ICU_recovered' elif transition(days_process, hosp2recovered, sd_hosp2recovered): # symptomatic hospital recovered cases specimen_df.loc[esp_indx, 'state'] = 'symptomatic_hospital_recovered' specimen_df.loc[esp_indx, 'days_process'] = 0 # ----------------------------------------- elif state == 'symptomatic_ICU': days_process = specimen_df.loc[esp_indx, 'days_process'] specimen_df.loc[esp_indx, 'days_process'] += 1 if specimen_df.loc[esp_indx, 'next_state'] == 'death': # ICU to death cases if transition(days_process, ICU2death, sd_ICU2death): specimen_df.loc[esp_indx, 'state'] = 'death' specimen_df.loc[esp_indx, 'days_process'] = 0 elif transition(days_process, ICU2recovered, sd_ICU2recovered): # ICU recovered cases specimen_df.loc[esp_indx, 'state'] = 'ICU_recovered' specimen_df.loc[esp_indx, 'days_process'] = 0 # statistics model_data_df.loc[days, 'healthy'] = specimen_df['state'][specimen_df['state'] == 'healthy'].count() model_data_df.loc[days, 'infected'] = specimen_df['state'][specimen_df['state'] == 'infected'].count() model_data_df.loc[days, 'asymptomatic'] = specimen_df['state'][specimen_df['state'] == 'asymptomatic'].count() model_data_df.loc[days, 'symptomatic'] = specimen_df['state'][specimen_df['state'] == 'symptomatic'].count() model_data_df.loc[days, 'symptomatic_diagnosed'] = specimen_df['state'][specimen_df['state'] == 'symptomatic_diagnosed'].count() model_data_df.loc[days, 'symptomatic_hospital'] = specimen_df['state'][specimen_df['state'] == 'symptomatic_hospital'].count() model_data_df.loc[days, 'symptomatic_ICU'] = specimen_df['state'][specimen_df['state'] == 'symptomatic_ICU'].count() model_data_df.loc[days, 'deaths'] = specimen_df['state'][specimen_df['state'] == 'death'].count() model_data_df.loc[days, 'asymptomatic_recovered'] = specimen_df['state'][specimen_df['state'] == 'asymptomatic_recovered'].count() model_data_df.loc[days, 'symptomatic_recovered'] = specimen_df['state'][specimen_df['state'] == 'symptomatic_recovered'].count() model_data_df.loc[days, 'symptomatic_diagnosed_recovered'] = specimen_df['state'][specimen_df['state'] == 'symptomatic_diagnosed_recovered'].count() model_data_df.loc[days, 'symptomatic_hospital_recovered'] = specimen_df['state'][specimen_df['state'] == 'symptomatic_hospital_recovered'].count() model_data_df.loc[days, 'ICU_recovered'] = specimen_df['state'][specimen_df['state'] == 'ICU_recovered'].count() num_vectors = specimen_df['n_infected'][specimen_df['n_infected'] > 0].count() indx_n_infec = specimen_df['n_infected'][specimen_df['n_infected'] > 0].index if num_vectors > 0: mean_infected = specimen_df['n_infected'].sum() / num_vectors model_data_df.loc[days, 'neam_infected'] = mean_infected model_data_df.loc[days, 'sd_infected'] = np.sqrt(np.power(specimen_df.loc[indx_n_infec,'n_infected'] - mean_infected, 2).sum() / num_vectors) # totals total_pop = population / (1 - healthy_immune)num_asymptomatic = model_data_df['asymptomatic']num_symptomatic = model_data_df['symptomatic']num_sympt_diagn = model_data_df['symptomatic_hospital'] + model_data_df['symptomatic_diagnosed'] + model_data_df['symptomatic_ICU']num_asymp_symp_recov = model_data_df['asymptomatic_recovered'] + model_data_df['symptomatic_recovered']num_symp_recov = model_data_df['symptomatic_recovered']num_symp_diag_recov = model_data_df['symptomatic_diagnosed_recovered']num_symp_hosp_recov = model_data_df['symptomatic_hospital_recovered']num_hosp = model_data_df['symptomatic_hospital'] + model_data_df['symptomatic_ICU']num_symptomatic_ICU = model_data_df['symptomatic_ICU']num_ICU_recovered = model_data_df['ICU_recovered']num_deaths = model_data_df['deaths']# versionversion = '_day_' + str(extraordinary_measurements) + '_2e5'if extraordinary_measurements > max_days: version = '_2e5'# save proposed resultsmodel_data_df.to_excel('covid19_basic_version_v2' + version +'.xls', index = False)model_data_df.to_csv('covid19_basic_version_v2' + version +'.csv') # plot model dynamicDPI = plt.gcf().get_dpi()plt.gcf().set_size_inches(2048.0/float(DPI),1536.0/float(DPI))plt.rcParams.update({'font.size': 32})plt.figure(1, figsize = (20 , 15))plt.plot(num_asymptomatic/population, 'r-', num_symptomatic/population, 'b-', num_sympt_diagn/total_pop, 'g-' )plt.legend(('Asymptomatic', 'Symptomatic', 'Diagnosed Symptomatic'), shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize = 24)plt.xlabel('Days')plt.ylabel('Cases/Total Infected')plt.grid(True)plt.savefig('fig1_basic_v2' + version + '.png')plt.figure(2, figsize = (20 , 15))plt.plot(num_hosp /population, 'c-', num_symptomatic_ICU /population, 'g-')plt.legend(('Hospitalized', 'ICU'), shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize = 24)plt.xlabel('Days')plt.ylabel('Cases/Total Infected')plt.grid(True)plt.savefig('fig2_basic_v2' + version + '.png')plt.figure(3, figsize = (20 , 15))plt.plot(num_asymp_symp_recov/population, 'r-', num_symp_recov/population, 'b-', num_symp_hosp_recov/population, 'g-')plt.legend(('Asymptomatic + Symptomatic Recovered', 'Symptomatic Diagnosed Recovered', 'Symptomatic Hospital Recovered'), shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=24)plt.xlabel('Days')plt.ylabel('Cases/Total Infected')plt.grid(True)plt.savefig('fig3_basic_v2' + version + '.png')plt.figure(4, figsize = (20 , 15))plt.plot(num_ICU_recovered /population, 'c-', num_deaths / population, 'g-')plt.legend(('ICU_Recovered', 'Deaths'), shadow=True, loc=(0.01, 0.48), handlelength=1.5, fontsize=24)plt.xlabel('Days')plt.ylabel('Cases/Total Infected')plt.grid(True)plt.savefig('fig4_basic_v2' + version + '.png') #pd.set_option('display.max_columns', 999)#pd.set_option('display.max_rows', 999) """peri = 30perf = 46print(num_healthy[peri:perf])print(num_infected[peri:perf])print(num_asymptomatic[peri:perf])print(num_symptomatic[peri:perf])print(num_symptomatic_quarantine[peri:perf])print(num_symptomatic_quar_cont[peri:perf])print(num_symptomatic_hospital[peri:perf])print(num_symptomatic_hospital_cont[peri:perf])print(num_symptomatic_ICU[peri:perf])print(num_deaths[peri:perf])print(num_ICU_recovered[peri:perf])print(num_asymptomatic_recovered[peri:perf])print(num_symptomatic_recovered[peri:perf])""" ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download