Using Python to create a map to simulate the spread of plague gif _python

Source: Internet
Author: User
Tags vmin

Inspired by Jason's almost Looks like Work, I showed some viral propagation models. It should be noted that this model does not reflect the reality of the situation, so don't be mistaken for a terrible infectious disease in West Africa. Instead, it should be seen as some kind of fictional zombie eruption. So let's go to the subject.

This is the Sir model, where the letters S, I, and r reflect the different states in which the individual may be in a zombie epidemic.

    • S represents a susceptible group, that is, the number of potential changes in a healthy individual.
    • I represent the infected group, that is, the number of zombies.
    • R represents the amount of removal, that is, the number of zombies who quit the game as a result of death, or the number of people who have been infected. But there is no cure for zombies, so let's not fool ourselves (if we're going to apply the Sir model to flu infections, there are healers).
    • As for beta (Beta) and gamma (gamma):
    • Beta (Beta) indicates the infectious extent of the disease, as long as the bite will infect.
    • Gamma (gamma) says that the rate of death from zombies depends on the average working rate of the zombie hunter, which, of course, is not a perfect model, please be patient with me.
    • S′=?βis tells us the rate at which healthy people become zombies, s′ is the derivative of time.
    • I′=βis?γi tells us how the infection has increased and how fast the corpse enters the removal rate (puns).
    • R′=γi just Add (gamma I), which is negative in the previous equation.

The above model does not consider the S/I/R space distribution, the following to fix it!

One approach is to divide the Swedish and Nordic countries into grids, each of which can infect adjacent units, as described below:

For the unit, and is the four units around it. (Don't get brain fatigue because of diagonal units, we need our brains not to be eaten).

Initialize some of the stuff.

Import NumPy as NP
import Math
import matplotlib.pyplot as Plt  
%matplotlib inline from
matplotlib import Rcparams
import matplotlib.image as mpimg
rcparams[' font.family '] = ' serif ' rcparams[' font.size '
] = 16
rcparams[' figure.figsize '] = 8 from
pil import Image

Appropriate beta and gamma values can destroy most of the land.

Beta = 0.010
gamma = 1

Do you remember the definition of the derivative? When the derivative is known, assuming that the ΔT is small, it can be used to approximate the next value of the function, and we have already declared u′ (t).

Initialize some of the stuff.

Import NumPy as NP
import Math
import matplotlib.pyplot as Plt  
%matplotlib inline from
matplotlib Import rcparams
import matplotlib.image as mpimg
rcparams[' font.family ' = ' serif ' rcparams[
' Font.Size '] =
rcparams[' figure.figsize '] = 8 from
pil import Image

Appropriate beta and gamma values can destroy most of the land.

Beta = 0.010
gamma = 1

Do you remember the definition of the derivative? When the derivative is known, assuming that the ΔT is small, it can be used to approximate the next value of the function, and we have already declared u′ (t).

This method is called Euler method, and the code is as follows:

Def euler_step (U, F, dt): Return
  U + dt * F (U)

We need the function f (u). The friendly NumPy provides a concise array operation. I might review it in another article because it's too powerful and it needs more explanation, but now it works:

 
def f (u):
  S = u[0]
  I = u[1]
  R = u[2]
 
  new = Np.array ([-beta* (S[1:-1, 1:-1]*i[1:-1, 1:-1] +
              s[0:-2, 1:-1 ]*i[0:-2, 1:-1] +
              s[2:, 1:-1]*i[2:, 1:-1] +
              s[1:-1, 0:-2]*i[1:-1, 0:-2] +
              s[1:-1, 2:]*i[1:-1, 2:],
           beta * (S[1:-1, 1:-1]*i[1:-1, 1:-1] +
              s[0:-2, 1:-1]*i[0:-2, 1:-1] +
              s[2:, 1:-1]*i[2:, 1:-1] +
              s[1:-1, 0:-2]*i[1 :-1, 0:-2] +
              s[1:-1, 2:]*i[1:-1, 2:])-gamma*i[1:-1, 1:-1],
           gamma*i[1:-1, 1:-1]
          ]
 
  padding = Np.zeros_like (U)
  padding[:,1:-1,1:-1] = new
  padding[0][padding[0] < 0] = 0
  padding[0][padding[0] > 255] = 255
  padding[1][padding[1] < 0] = 0
  padding[1][padding[1] > 255] = 255
  padding[2][padding [2] < 0] = 0
  padding[2][padding[2] > 255] = 255 return
 
  padding

Import population density maps of Nordic countries and sample them to get results faster

From PIL import Image
img = image.open (' popdens2.png ')
img = img.resize ((IMG.SIZE[0]/2,IMG.SIZE[1]/2))
img = 255-np.asarray (img)
imgplot = Plt.imshow (img)
imgplot.set_interpolation (' nearest ')

Population density map of the Nordic countries (not included in Denmark)

The S-matrix, which is the susceptible individual, should approximate the population density. The initial infection was 0, and we took Stockholm as the first source.

S_0 = img[:,:,1]
i_0 = np.zeros_like (s_0)
i_0[309,170] = 1 # Patient Zero

Since no one has died, the matrix is also set to 0.

R_0 = Np.zeros_like (s_0)

Then initialize the simulation time and so on.

t = 900             # final time
dt = 1             # time increment
N = Int (T/DT) + 1        # Number of time-steps
T = NP.LINSP Ace (0.0, T, N)   # time discretization
 
# Initialize the array containing the solution for each time-step
u = NP. Empty ((N, 3, s_0.shape[0], s_0.shape[1])
u[0][0] = s_0 u[0][1
] = i_0 u[0][2
] = r_0

We need to customize a color table so that the infection matrix can be displayed on the map.

Import matplotlib.cm as cm
thecm = cm.get_cmap ("Reds")
Thecm._init ()
Alphas = np.abs (Np.linspace (0, 1, THECM.N))
thecm._lut[:-3,-1] = Alphas

Sit down and enjoy it ...

 
For n in range (N-1):
  u[n+1] = Euler_step (U[n], F, DT)

Let's do the image rendering, make it gif, everybody likes gifs!

From images2gif import writegif
 
keyframes = []
frames = 60.0
 
to I in range (0, N-1, int (n/frames)):
  Imgplo t = Plt.imshow (IMG, vmin=0, vmax=255)
  imgplot.set_interpolation ("nearest")
  Imgplot = Plt.imshow (u[i][1), Vmin =0, CMAP=THECM)
  imgplot.set_interpolation ("nearest")
  filename = "outbreak" + str (i) + ". png"
  plt.savefig ( FileName)
  keyframes.append (filename)
 
images = [Image.open (FN) for FN in keyframes]
giffilename = " Outbreak.gif "
writegif (giffilename, images, duration=0.3)
PLT.CLF ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.