Skip to content

Commit

Permalink
Fix scenarios extra argument parsing + tweak variant scenario (10)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshtonIzmev committed Mar 15, 2021
1 parent 4dd720f commit 32c691c
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 21 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
```bash
pip install pandemic-simulation # Last stable version
# or
apt-get install python3-venv gcc python3-dev
python3 -m venv ./venv
source ./venv/bin/activate
pip install --upgrade -r requirements.txt
pip install -r requirements.txt


### To plot new daily cases
Expand Down
32 changes: 25 additions & 7 deletions scenario/example/sc10_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
random.seed(specific_seed)
np.random.seed(specific_seed)

if len(params[additional_scenario_params_key]) < 1:
if len(params[additional_scenario_params_key]) < 3:
raise AssertionError("Need more additional_scenario parameter")
else:
assert(params[additional_scenario_params_key][2] in ["False", "True"])
rate_daily_vaccinated = int(params[additional_scenario_params_key][0])
variant_kind = params[additional_scenario_params_key][1]
restrict_genetic_cost = params[additional_scenario_params_key][2] == "True"

if rate_daily_vaccinated < 0:
# Morrocan daily rate of vaccination
Expand All @@ -36,14 +39,23 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
variant_immunization = 1
variant_mortality = 1 # tradeoff parameter
variant_hospital = 1 # tradeoff parameter
restrict_genetic_cost = False

death_stat = []

for param_variant_iter in range(params[nvariant_key]):
run_stats = get_zero_run_stats(params)
# Update parameters
# Range [0.5, 1.5] with a 1/param stem (+/- 50%)
variant_hospital = 0.5 + param_variant_iter/params[nvariant_key]
# Range [0.25, 1.75] with a 1/param step (+/- 75%)
if variant_kind == "C":
variant_contagiosity = 0.25 + 1.5 * param_variant_iter / params[nvariant_key]
if variant_kind == "I":
variant_immunization = 1 + 2 * param_variant_iter / params[nvariant_key]
if variant_kind == "M":
variant_mortality = 0.25 + 1.5 * param_variant_iter / params[nvariant_key]
if variant_kind == "H":
variant_hospital = 0.25 + 1.5 * param_variant_iter / params[nvariant_key]
if variant_kind == "MH" or variant_kind == "HM":
variant_mortality = 0.25 + 1.5 * param_variant_iter / params[nvariant_key]
variant_hospital = 1.75 - 1.5 * param_variant_iter / params[nvariant_key]

if restrict_genetic_cost:
variant_total_cost = variant_contagiosity + variant_immunization + variant_mortality + variant_hospital
Expand All @@ -56,16 +68,22 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
params[work_infection_key] = 0.05 * variant_contagiosity
params[store_infection_key] = 0.02 * variant_contagiosity
params[transport_infection_key] = 0.01 * variant_contagiosity

params[variant_mortality_k] = variant_mortality
params[death_bounds_key] = (8 / variant_mortality, 31 / variant_mortality)

params[variant_hospitalization_k] = variant_hospital
params[immunity_bounds_key] = (270, 450) * variant_immunization # assuming about a year of immunity (~flu)
params[hospitalization_bounds_key] = (8 / variant_hospital, 16 / variant_hospital)

# assuming about a year of immunity (~flu)
params[immunity_bounds_key] = (int(270/variant_immunization), int(450/variant_immunization))
virus_dic = get_virus_simulation_t0(params)

for day in range(params[nday_key]):
pba.update.remote(1)
old_healthy = [(k, env_dic[IAG_K][k]) for k, v in virus_dic[STA_K].items() if v == HEALTHY_V]
nb_indiv_vaccinated = max(0, int(params[nindividual_key] * rate_daily_vaccinated * (1-day/100)))
if len(old_healthy) > nb_indiv_vaccinated:
if len(old_healthy) > nb_indiv_vaccinated and day <= 100:
old_sorted = sorted(old_healthy, key=lambda kv: -kv[1])
old_lucky = [o[0] for o in old_sorted[:nb_indiv_vaccinated]]
virus_dic[STA_K].update((o, IMMUNE_V) for o in old_lucky)
Expand Down
2 changes: 1 addition & 1 deletion scenario/example/sc1_simple_lockdown_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
if len(params[additional_scenario_params_key]) < 1:
raise AssertionError("Need an additional_scenario parameter")

days_to_lockdown_loosening = params[additional_scenario_params_key][0]
days_to_lockdown_loosening = float(params[additional_scenario_params_key][0])
days_with_no_cases = 0
first_day_lockdown_loosening = -1
virus_dic = get_virus_simulation_t0(params)
Expand Down
6 changes: 3 additions & 3 deletions scenario/example/sc2_yoyo_lockdown_removal.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):

if len(params[additional_scenario_params_key]) < 3:
raise AssertionError("Need more additional_scenario parameter")
days_to_lockdown_loosening = params[additional_scenario_params_key][0]
factor_bed = params[additional_scenario_params_key][1]
max_lockdown = params[additional_scenario_params_key][2]
days_to_lockdown_loosening = float(params[additional_scenario_params_key][0])
factor_bed = float(params[additional_scenario_params_key][1])
max_lockdown = float(params[additional_scenario_params_key][2])

days_with_no_cases = 0
days_to_lockdown_change = 0
Expand Down
2 changes: 1 addition & 1 deletion scenario/example/sc3_loose_lockdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
if len(params[additional_scenario_params_key]) < 1:
raise AssertionError("Need more additional_scenario parameter")

percent_increase = params[additional_scenario_params_key][0]
percent_increase = float(params[additional_scenario_params_key][0])

params[store_preference_key] = 0.3
params[remote_work_key] = 0.58
Expand Down
2 changes: 1 addition & 1 deletion scenario/example/sc4_rogue_citizen.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
raise AssertionError("Need more additional_scenario parameter")

nb_rogue = int(params[additional_scenario_params_key][0])
rogue_factor = params[additional_scenario_params_key][1]
rogue_factor = float(params[additional_scenario_params_key][1])

params[store_preference_key] = 0.95
params[remote_work_key] = 0.98
Expand Down
2 changes: 1 addition & 1 deletion scenario/example/sc5_rogue_neighborhood.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
raise AssertionError("Need more additional_scenario parameter")

nb_bloc = int(params[additional_scenario_params_key][0])
rogue_factor = params[additional_scenario_params_key][1]
rogue_factor = float(params[additional_scenario_params_key][1])

rogues_blocks_x = np.random.choice(range(params[nb_1d_block_key]), nb_bloc)
rogues_blocks_y = np.random.choice(range(params[nb_1d_block_key]), nb_bloc)
Expand Down
2 changes: 1 addition & 1 deletion scenario/example/sc9_vaccination.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def do_parallel_run(env_dic, params, run_id, specific_seed, pba: ActorHandle):
pba.update.remote(1)
old_healthy = [(k, env_dic[IAG_K][k]) for k, v in virus_dic[STA_K].items() if v == HEALTHY_V]
nb_indiv_vaccinated = max(0, int(params[nindividual_key] * rate_daily_vaccinated * (1-day/100)))
if len(old_healthy) > nb_indiv_vaccinated:
if len(old_healthy) > nb_indiv_vaccinated and day <= 100:
old_sorted = sorted(old_healthy, key=lambda kv: -kv[1])
old_lucky = [o[0] for o in old_sorted[:nb_indiv_vaccinated]]
virus_dic[STA_K].update((o, IMMUNE_V) for o in old_lucky)
Expand Down
2 changes: 1 addition & 1 deletion simulator/helper/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_parser():
action='store_true')

# Scenarios related
parser.add_argument('--extra-scenario-params', type=float, nargs="*", help='Additional scenario parameters',
parser.add_argument('--extra-scenario-params', type=str, nargs="*", help='Additional scenario parameters',
dest=additional_scenario_params_key)

return parser
9 changes: 7 additions & 2 deletions simulator/helper/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def draw_new_daily_cases(stats_arg, show_plot, x_tick=10):
def draw_meta_simulation(death_stat_arg, show_plot):
fig, ax = plt.subplots(figsize=(15, 10))
set_ax_meta_simulation(ax, death_stat_arg)
print("ok")
if show_plot:
plt.show()
else:
Expand Down Expand Up @@ -325,12 +326,16 @@ def set_ax_meta_simulation(ax, death_stat_arg):

p1 = ax.bar(indices, death_serie, width, yerr=err, align='center', alpha=0.5, ecolor="#808080", color="#44A1A0")

bottom_y = int(min(death_serie) * 0.8)
top_y = int(1+max(death_serie) * 1.2)

ax.set_ylim(bottom=bottom_y, top=top_y)
ax.set_ylabel('Total death')
ax.set_xlabel('Variant parameter')
ax.set_title('Total death / covid-19 variant type')
ax.set_xticks(np.arange(0, n_variant_arg, 1))
ax.set_xticklabels(tuple([str(i-n_variant_arg/2) for i in range(n_variant_arg)]))
ax.set_yticks(np.arange(0, int(1+max(death_serie) * 1.1), int(1+max(death_serie) / 10)))
ax.set_xticklabels([round(s, 2) for s in np.arange(0.25, 1.75, 1.5 / n_variant_arg)])
ax.set_yticks(np.arange(bottom_y, top_y, int(1+max(death_serie) / 10)))
ax.legend((p1[0],), ('Death',))


Expand Down
4 changes: 2 additions & 2 deletions simulator/helper/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def get_infection_params():
DEA_INIT_K: copy.deepcopy(time_to_death),
IMM_INIT_K: copy.deepcopy(time_to_end_immunity),

variant_mortality_k: 1,
variant_hospitalization_k: 1,
variant_mortality_k: params_arg[variant_mortality_k] if variant_mortality_k in params_arg else 1,
variant_hospitalization_k: params_arg[variant_hospitalization_k] if variant_hospitalization_k in params_arg else 1,

STA_K: life_state,
NC_K: 0
Expand Down

0 comments on commit 32c691c

Please sign in to comment.