Update SIR_model.py
Browse files- SIR_model.py +52 -26
SIR_model.py
CHANGED
|
@@ -9,8 +9,8 @@ from modeci_mdf.mdf import*
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import os
|
| 11 |
import sys
|
| 12 |
-
import streamlit as st
|
| 13 |
from matplotlib.animation import FuncAnimation
|
|
|
|
| 14 |
|
| 15 |
# Rest of the code remains the same
|
| 16 |
|
|
@@ -191,41 +191,67 @@ def main(total_population=1000, initial_infected=1, initial_recovered=0, beta=0.
|
|
| 191 |
plt.tight_layout() # Adjust layout to prevent overlap
|
| 192 |
plt.show()
|
| 193 |
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
ax.xaxis.set_major_locator(plt.MaxNLocator(6))
|
| 201 |
-
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '{:.1f}'.format(x)))
|
| 202 |
-
ax.set_xlabel('Time')
|
| 203 |
-
ax.set_ylabel('Population')
|
| 204 |
-
ax.set_title('Population over time')
|
| 205 |
-
ax.legend()
|
| 206 |
-
ax.grid(True)
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
def animate(frame):
|
| 210 |
-
ax.clear()
|
| 211 |
ax.plot(times[:frame+1], s[:frame+1], label='Susceptible', color='blue')
|
| 212 |
-
ax.plot(times[:frame+1], i[:frame+1], label='Infected', color='orange')
|
| 213 |
ax.plot(times[:frame+1], r[:frame+1], label='Recovered', color='green')
|
| 214 |
ax.xaxis.set_major_locator(plt.MaxNLocator(6))
|
| 215 |
-
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x,
|
| 216 |
ax.set_xlabel('Time')
|
| 217 |
ax.set_ylabel('Population')
|
| 218 |
ax.set_title('Population over time')
|
| 219 |
ax.legend()
|
| 220 |
ax.grid(True)
|
| 221 |
-
return[ax]
|
| 222 |
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
-
return [fig1, anim]
|
|
|
|
| 229 |
|
| 230 |
elif mode=="graph":
|
| 231 |
|
|
|
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
import os
|
| 11 |
import sys
|
|
|
|
| 12 |
from matplotlib.animation import FuncAnimation
|
| 13 |
+
import imageio
|
| 14 |
|
| 15 |
# Rest of the code remains the same
|
| 16 |
|
|
|
|
| 191 |
plt.tight_layout() # Adjust layout to prevent overlap
|
| 192 |
plt.show()
|
| 193 |
|
| 194 |
+
# Prepare an array to hold the rendered frames
|
| 195 |
+
frames = []
|
| 196 |
+
|
| 197 |
+
# Generate frames
|
| 198 |
+
for frame in range(len(times)):
|
| 199 |
+
fig, ax = plt.subplots()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
ax.plot(times[:frame+1], s[:frame+1], label='Susceptible', color='blue')
|
| 201 |
+
ax.plot(times[:frame+1], i[:frame+1], label='Infected', color='orange')
|
| 202 |
ax.plot(times[:frame+1], r[:frame+1], label='Recovered', color='green')
|
| 203 |
ax.xaxis.set_major_locator(plt.MaxNLocator(6))
|
| 204 |
+
ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, _: '{:.1f}'.format(x)))
|
| 205 |
ax.set_xlabel('Time')
|
| 206 |
ax.set_ylabel('Population')
|
| 207 |
ax.set_title('Population over time')
|
| 208 |
ax.legend()
|
| 209 |
ax.grid(True)
|
|
|
|
| 210 |
|
| 211 |
+
# Convert the Matplotlib figure to a RGB array and close the figure to free memory
|
| 212 |
+
fig.canvas.draw()
|
| 213 |
+
image = np.frombuffer(fig.canvas.tostring_rgb(), dtype='uint8')
|
| 214 |
+
image = image.reshape(fig.canvas.get_width_height()[::-1] + (3,))
|
| 215 |
+
frames.append(image)
|
| 216 |
+
plt.close(fig)
|
| 217 |
+
|
| 218 |
+
# Save the frames as a GIF
|
| 219 |
+
imageio.mimsave('animated_plot.gif', frames, fps=20)
|
| 220 |
+
# # Create an animated line graph
|
| 221 |
+
# fig2, ax = plt.subplots()
|
| 222 |
+
# ax.plot(times, s, label='Susceptible', color='blue')
|
| 223 |
+
# ax.plot(times, i, label='Infected', color='orange')
|
| 224 |
+
# ax.plot(times, r, label='Recovered', color='green')
|
| 225 |
+
# ax.xaxis.set_major_locator(plt.MaxNLocator(6))
|
| 226 |
+
# ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '{:.1f}'.format(x)))
|
| 227 |
+
# ax.set_xlabel('Time')
|
| 228 |
+
# ax.set_ylabel('Population')
|
| 229 |
+
# ax.set_title('Population over time')
|
| 230 |
+
# ax.legend()
|
| 231 |
+
# ax.grid(True)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# def animate(frame):
|
| 235 |
+
# ax.clear()
|
| 236 |
+
# ax.plot(times[:frame+1], s[:frame+1], label='Susceptible', color='blue')
|
| 237 |
+
# ax.plot(times[:frame+1], i[:frame+1], label='Infected', color='orange')
|
| 238 |
+
# ax.plot(times[:frame+1], r[:frame+1], label='Recovered', color='green')
|
| 239 |
+
# ax.xaxis.set_major_locator(plt.MaxNLocator(6))
|
| 240 |
+
# ax.xaxis.set_major_formatter(plt.FuncFormatter(lambda x, p: '{:.1f}'.format(x)))
|
| 241 |
+
# ax.set_xlabel('Time')
|
| 242 |
+
# ax.set_ylabel('Population')
|
| 243 |
+
# ax.set_title('Population over time')
|
| 244 |
+
# ax.legend()
|
| 245 |
+
# ax.grid(True)
|
| 246 |
+
# return[ax]
|
| 247 |
+
|
| 248 |
+
# anim = FuncAnimation(fig2, animate, frames=times, interval=50, blit=True)
|
| 249 |
+
# # Save the animated plot
|
| 250 |
+
# anim.save('animated_plot.gif', writer='imagemagick')
|
| 251 |
+
# plt.show()
|
| 252 |
|
| 253 |
+
# return [fig1, anim]
|
| 254 |
+
return [fig1]
|
| 255 |
|
| 256 |
elif mode=="graph":
|
| 257 |
|