|
| 1 | + |
| 2 | +import argparse |
| 3 | + |
| 4 | +parser = argparse.ArgumentParser(description='Auxiliar script to generate Zhong-based CA models') |
| 5 | + |
| 6 | +parser.add_argument('-i', '--incubation_steps', type=int, default=2, help='Number of days for the incubation state') |
| 7 | +parser.add_argument('-f', '--infected_steps', type=int, default=2, help='Number of days for the infected state') |
| 8 | +parser.add_argument('-l', '--latent_steps', type=int, default=2, help='Number of days for the latent state') |
| 9 | +parser.add_argument('-r', '--recovered_steps', type=int, default=3, help='Number of days for the recovered state') |
| 10 | +parser.add_argument('-t', '--template_ma', type=str, default="template_sirs_zhong_ma", help='Path to zhong .ma template') |
| 11 | +parser.add_argument('-m', '--template_macros', type=str, default="template_sirs_zhong_macros", help='Path to zhong macros template') |
| 12 | +parser.add_argument('-o', '--out_ma_file', type=str, default="out.ma", help='Output path of the .ma file') |
| 13 | +parser.add_argument('-p', '--out_macros_file', type=str, default="sirs_zhong_macros.inc", help='Output path of the macros file') |
| 14 | + |
| 15 | +args = parser.parse_args() |
| 16 | + |
| 17 | +i = 1 |
| 18 | +states = ["sus_0"] |
| 19 | +neighbors = ["(1,0)", "(0,-1)", "(0,1)", "(-1,0)"] |
| 20 | + |
| 21 | +while i <= args.incubation_steps: |
| 22 | + states.append("inc_%d" % i) |
| 23 | + i += 1 |
| 24 | + |
| 25 | +while i <= args.incubation_steps + args.infected_steps: |
| 26 | + states.append("inf_%d" % i) |
| 27 | + i += 1 |
| 28 | + |
| 29 | +while i <= args.incubation_steps + args.infected_steps + args.latent_steps: |
| 30 | + states.append("lat_%d" % i) |
| 31 | + i += 1 |
| 32 | + |
| 33 | +while i <= args.incubation_steps + args.infected_steps + args.latent_steps + args.recovered_steps: |
| 34 | + states.append("rec_%d" % i) |
| 35 | + i += 1 |
| 36 | + |
| 37 | +############################## |
| 38 | +# GENERATION OF THE .MA FILE |
| 39 | +############################## |
| 40 | +content = "" |
| 41 | +with open(args.template_ma, "r") as f: |
| 42 | + content = f.read() |
| 43 | + |
| 44 | +content = content.replace("[[internal_vars]]", " ".join(["i_%s" % s for s in states])) |
| 45 | +content = content.replace("[[internal_vars_values]]", " ".join(["1.0"] + ["0.0"] * (len(states)-1))) |
| 46 | +update_ports_str = " ".join(["~%s := $i_%s;" % (s, s) for s in states]) |
| 47 | +content = content.replace("[[initial_ports_set_up]]", update_ports_str) |
| 48 | +content = content.replace("[[update_ports_step]]", update_ports_str) |
| 49 | +content = content.replace("[[ports]]", " ".join(states)) |
| 50 | + |
| 51 | +update_rules = "" |
| 52 | +idx_first_rec = len(states)-args.recovered_steps |
| 53 | +for i in range(len(states)-1, idx_first_rec, -1): |
| 54 | + update_rules += "$i_%s := $i_%s;\n" % (states[i], states[i-1]) |
| 55 | +update_rules += "$i_%s := $i_%s + #macro(local_cured);\n\n" % (states[idx_first_rec], states[idx_first_rec-1]) |
| 56 | + |
| 57 | +for i in range(idx_first_rec-1, 1, -1): |
| 58 | + update_rules += "$i_%s := trunc(min((1 - $cured_rate) * $i_%s, 1)*100)/100;\n" % (states[i], states[i - 1]) |
| 59 | +update_rules += "$i_%s := #macro(internal_infected) + #macro(external_infected);\n\n" % states[1] |
| 60 | + |
| 61 | +update_rules += "$i_%s := 1 " % states[0] |
| 62 | +update_rules += " ".join(["- $i_%s" % s for s in states[1:]]) |
| 63 | +update_rules += ";\n" |
| 64 | + |
| 65 | +content = content.replace("[[update_vars_step]]", update_rules) |
| 66 | + |
| 67 | +with open(args.out_ma_file, "w") as f: |
| 68 | + f.write(content) |
| 69 | + |
| 70 | +################################# |
| 71 | +# GENERATION OF THE MACROS FILE |
| 72 | +################################# |
| 73 | + |
| 74 | +content = "" |
| 75 | +with open(args.template_macros, "r") as f: |
| 76 | + content = f.read() |
| 77 | + |
| 78 | +local_cured_rules = [] |
| 79 | +for i in range(idx_first_rec-2, 0, -1): |
| 80 | + local_cured_rules.append("trunc(min($cured_rate * $i_%s, 1)*100)" % states[i]) |
| 81 | + |
| 82 | +content = content.replace("[[local_cured]]", "(( %s ) / 100 )" % " +\n".join(local_cured_rules)) |
| 83 | +content = content.replace("[[infected_vars]]", " + ".join(["i_%s" % s for s in states[idx_first_rec:0]])) |
| 84 | + |
| 85 | +external_infected_comp = \ |
| 86 | +"""( trunc(min(( $connection * |
| 87 | +$contact_rate * |
| 88 | +(($i_sus_0 * $population) / $area) * |
| 89 | +$i_sus_0 * |
| 90 | +([[external_infected]]) |
| 91 | +) / $p, 1)*100) / 100 ) |
| 92 | +""" |
| 93 | + |
| 94 | +external_infected_rules = [] |
| 95 | +for nei in neighbors: |
| 96 | + inf = " + ".join(["%s~%s" % (nei, s) for s in states[idx_first_rec-1:0:-1]]) |
| 97 | + external_infected_rules.append(external_infected_comp.replace("[[external_infected]]", inf)) |
| 98 | + |
| 99 | +content = content.replace("[[external_infected]]", "+\n".join(external_infected_rules)) |
| 100 | + |
| 101 | +with open(args.out_macros_file, "w") as f: |
| 102 | + f.write(content) |
0 commit comments