-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSIRS_plotter.py
More file actions
174 lines (161 loc) · 6.91 KB
/
Copy pathSIRS_plotter.py
File metadata and controls
174 lines (161 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from SIRS import SIRS
import numpy as np
import sys
import matplotlib.pyplot as plt
import math
def main():
if len(sys.argv) != 2:
print("Wrong number of arguments.")
print("Usage: " + sys.argv[0] + " <parameters file>")
quit()
else:
infile_parameters = sys.argv[1]
# Open input file and assinging parameters.
with open(infile_parameters, "r") as input_file:
# Read the lines of the input data file.
line = input_file.readline()
items = line.split(", ")
# Lattice size.
lattice_size = (int(items[0]), int(items[0]))
desired_plot = str(items[1]) # Desired plot.
ini_cond = str(items[2]) # Initial conditions.
p2 = float(items[3]) # P(I --> R).
p_step = float(items[4]) # Probability steps.
eqm_sweeps = int(items[5]) # Equilibrium sweeps.
sweeps = int(items[6]) # No. of sweeps.
# Heatmap plot.
if desired_plot == 'heatmap':
# Initialising probability domains.
p1s = np.arange(0.0, 1.0 + p_step, p_step)
#print(p1s.size)
p3s = np.arange(0.0, 1.0 + p_step, p_step)
# Initialising phase matrix.
phase_matrix = np.zeros((p1s.size, p3s.size))
var_matrix = np.zeros((p1s.size, p3s.size))
# Simulation begins.
for p1 in p1s:
print(p1)
# Data storage.
psi_per_p1 = []
var_data = []
for p3 in p3s:
simulation = SIRS(size=lattice_size,
ini=ini_cond, p1=p1, p2=p2, p3=p3)
psi_per_p3 = []
# Sweep over lattice.
for sweep in range(sweeps):
for j in range(simulation.size[0]*simulation.size[1]):
simulation.update_SIRS()
# Get data.
if sweep >= eqm_sweeps and simulation.get_infected() != 0:
psi_per_p3.append(simulation.get_infected())
# Stop when absorbing state reached.
elif sweep >= eqm_sweeps:
break
# Data collection.
if len(psi_per_p3) != 0:
psi_per_p1.append(simulation.get_avg_obs(
psi_per_p3) / (simulation.size[0]*simulation.size[1]))
var_data.append(simulation.get_infected_var(
psi_per_p3) / (simulation.size[0]*simulation.size[1]))
else:
psi_per_p1.append(0.0)
var_data.append(0.0)
# Update matrix columns.
phase_matrix[:, int(p1*(p1s.size-1))] = psi_per_p1
var_matrix[:, int(p1*(p1s.size-1))] = var_data
# Plotting.
simulation.plot_phase_diagram(phase_matrix, p_step)
simulation.plot_variance_contour(var_matrix, p_step)
# Writing to file.
np.savetxt("phase_data.dat", phase_matrix, fmt='%1.5f', delimiter=' ',
newline = '\n# p1 = [0.0, 0.025, ..., 1.0]' + ' p3 = [0.0, 0.025, ..., 1.0]\n',
header = 'Phase Diagram Raw Data'
)
np.savetxt("var_data.dat", var_matrix, fmt='%1.5f', delimiter=' ',
newline = '\n# p1 = [0.0, 0.025, ..., 1.0]' + ' p3 = [0.0, 0.025, ..., 1.0\n',
header = 'Phase Diagram Variance Raw Data'
)
# Variance cut plot.
elif desired_plot == 'variance_plot':
# Initialising probability domains.
p1s = np.arange(0.2, 0.51, 0.01)
p3 = 0.5
# Data storage.
var_array = np.zeros(p1s.size)
error_array = np.zeros(p1s.size)
# Simulation begins.
for i in range(p1s.size):
print(p1s[i])
# Data storage.
psis = []
# New simulation.
simulation = SIRS(size=lattice_size,
ini=ini_cond, p1=p1s[i], p2=p2, p3=p3)
# Sweeping.
for sweep in range(10000):
for j in range(simulation.size[0]*simulation.size[1]):
simulation.update_SIRS()
if sweep >= eqm_sweeps:
psis.append(simulation.get_infected())
# Update arrays.
var_array[i] = simulation.get_infected_var(psis) / \
(simulation.size[0] * simulation.size[1])
error_array[i] = simulation.bootstrap(psis, 100)
# Plotting.
simulation.plot_figure(p1s, var_array, error_array)
# Writing to a file.
with open("var_cut.dat", "w+") as f:
f.writelines(map("{}, {}, {}\n".format, p1s, var_array, error_array))
# Immunity plot.
elif desired_plot == 'immunity':
# Initialising probabilities.
p1 = 0.5
p3 = 0.5
# Initialising x domain.
im_fracs = np.arange(0.0, 0.525, 0.025)
# Data storage.
overall_psis = []
im_errors = []
# Looping to generate errorbars.
for k in range(5):
print(k)
# Data storage.
psi_per_k = []
# New simulation.
for frac in im_fracs:
psi_per_frac = []
simulation = SIRS(size=lattice_size,
ini=ini_cond, p1=p1, p2=p2, p3=p3)
# Creating immune sites.
for i in range(int(simulation.size[0]*simulation.size[1]*frac)):
indices = (np.random.randint(0, simulation.size[0]),
np.random.randint(0, simulation.size[1]))
simulation.lattice[indices] = 2
# Sweeping.
for sweep in range(sweeps * 10):
for j in range(simulation.size[0]*simulation.size[1]):
simulation.update_SIRS()
if sweep >= eqm_sweeps:
# Storing infected sites per frac.
psi_per_frac.append(simulation.get_infected() / (simulation.size[0] * simulation.size[1]))
# Storing averages.
psi_per_k.append(simulation.get_avg_obs(psi_per_frac))
# Storing data from each simulation.
overall_psis.append(psi_per_k)
# Computing errors.
for vals in np.array(overall_psis).T:
im_errors.append(np.std(vals)/math.sqrt(len(vals)))
# Generating y_data.
infected_fracs = np.mean(overall_psis, axis = 0)
# Plotting.
plt.title('Infected Sites vs. Immune Fraction')
plt.xlabel('Immune Fraction')
plt.ylabel('Infected Fraction')
plt.errorbar(im_fracs, infected_fracs, yerr = im_errors)
plt.savefig("immunity_plot.png")
plt.show()
# Writing to file.
with open("immunity.dat", "w+") as f:
f.writelines(map("{}, {}, {}\n".format, im_fracs, infected_fracs, im_errors))
main()