samedi 19 mars 2022

Python random walks for COVID infections

I'm trying to implement a random walk algorithm in 2D to model COVID infections at a party/festival. The main body of my code is shown below, excluding some plots and initial conditions where I define all the initial positions (using a random uniform sampling) and initial parameters. My problem is, when I run my code and plot the various populations at different steps, for whatever reason the population of moving infected people ends up looking like the green data after several steps: Why does this happen? Is this something to do with the random uniform sampling? Any help is appreciated.

import numpy as np
import pandas as pd
from numpy import random
import matplotlib.pyplot as plt
from random import uniform
import itertools as itr
import time

nsteps = 10
xlim = ylim = [10, 0]
npeople = 100
npeople_move = 90
ninfected = 2
ninfected_move = 1
size = 5


fig, axs = plt.subplots(1, nsteps, figsize = [30, 5])
plt.rcParams['axes.grid'] = True
plt.setp(axs, xlim=[xlim[-1], xlim[0]], ylim=[ylim[-1], ylim[0]])

peoplex_move = np.array([uniform(xlim[-1], xlim[0]) for i in np.arange(0, npeople_move, 1)])
peopley_move = np.array([uniform(ylim[-1], ylim[0]) for i in np.arange(0, npeople_move, 1)])

peoplepos_move = np.sqrt(np.square(peoplex_move), np.square(peopley_move))

peoplex = np.array([uniform(xlim[-1], xlim[0]) for i in np.arange(0, npeople - npeople_move, 1)])
peopley = np.array([uniform(ylim[-1], ylim[0]) for i in np.arange(0, npeople - npeople_move, 1)])

peoplepos = np.sqrt(np.square(peoplex), np.square(peopley))


infx_move = np.array([uniform(xlim[-1], xlim[0]) for i in np.arange(0, ninfected_move, 1)])
infy_move = np.array([uniform(ylim[-1], ylim[0]) for i in np.arange(0, ninfected_move, 1)])

infpos_move = np.sqrt(np.square(infx_move), np.square(infy_move))


infx = np.array([uniform(xlim[-1], xlim[0]) for i in np.arange(0, ninfected - ninfected_move, 1)])
infy = np.array([uniform(ylim[-1], ylim[0]) for i in np.arange(0, ninfected - ninfected_move, 1)])

infpos = np.sqrt(np.square(infx), np.square(infy))
df = pd.DataFrame({'Number of people':[], 'Number of infected':[], 'Step Number':[]})

cough_sneeze = False
cough_sneeze_loc = []
size = 10

start = time.process_time()



for step in np.arange(1, nsteps, 1):
    
    
    theta = 2*np.pi*np.random.uniform(0, 1, npeople_move + ninfected_move)
    
    if bool(cough_sneeze_loc)!=False:
        
        for item in cough_sneeze_loc:
            
            item[1]-=1
            
            if item[1]==0:
                
                cough_sneeze_loc.remove(item)
                
            else:
                
                continue
        
                
                
    peoplex_move = peoplex_move + size*np.cos(theta[0:npeople_move])
    peopley_move = peopley_move + size*np.sin(theta[0:npeople_move])
    
    peoplex_move = np.where(peoplex_move>=xlim[0], peoplex_move - size, peoplex_move)
    peoplex_move = np.where(peoplex_move<=xlim[-1], peoplex_move + size, peoplex_move)
    peopley_move = np.where(peoplex_move>=ylim[0], peopley_move - size, peopley_move)
    peopley_move = np.where(peoplex_move<=ylim[-1], peopley_move + size, peopley_move)
    
    infx_move = infx_move + size*np.cos(theta[npeople_move:npeople_move + ninfected_move])
    infy_move = infx_move + size*np.sin(theta[npeople_move:npeople_move + ninfected_move])
    
    infx_move = np.where(infx_move>=xlim[0], infx_move - size, infx_move)
    infx_move = np.where(infx_move<=xlim[-1], infx_move + size, infx_move)
    infy_move = np.where(infy_move>=ylim[0], infy_move - size, infy_move)
    infy_move = np.where(infy_move<=ylim[-1], infy_move + size, infy_move)

    peoplepos_move = np.sqrt(np.square(peoplex_move), np.square(peopley_move))
    
    infpos_move = np.sqrt(np.square(infx_move), np.square(infy_move))
    

            

                
    if bool(cough_sneeze_loc)!=False:
        
        for item in peoplepos:
            
            if np.any(np.abs(item - np.stack(cough_sneeze_loc)[:,0]) <= 5):
                
                
                
                bad = np.where(peoplepos == item)
                
                infpos = np.append(infpos, item)
                
                peoplepos = np.delete(peoplepos, bad)
                
                infx = np.append(infx, peoplex[bad])
                
                infy = np.append(infy, peopley[bad])
                
                peoplex = np.delete(peoplex, bad)
                
                peopley = np.delete(peopley, bad)
                
                npeople-=1
                ninfected+=1
                

                
            else:

            
                continue
                
                
        for item in peoplepos_move:
            
            if np.any(np.abs(item - np.stack(cough_sneeze_loc)[:,0]) <=5):
                
                
                bad = np.where(peoplepos_move == item)
                
                infpos_move = np.append(infpos_move, item)
                
                peoplepos_move = np.delete(peoplepos_move, bad)
                
                infx_move = np.append(infx_move, peoplex_move[bad])
                
                infy_move = np.append(infy_move, peopley_move[bad])
                
                peoplex_move = np.delete(peoplex_move, bad)
                
                peopley_move = np.delete(peopley_move, bad)
                
                ninfected_move+=1
                npeople_move-=1
                npeople-=1
                ninfected+=1
                
            else:
                
                continue
                
        
    cough_sneeze1 = np.random.choice(infpos_move, 2)

    cough_sneeze2 = np.random.choice(infpos, 2)
    
    

        
    cough_sneeze_loc.append([cough_sneeze1.astype(float)[0], 2])
        
    cough_sneeze_loc.append([cough_sneeze2.astype(float)[0], 2])

        
    for pos in peoplepos_move:
            
        if np.any(np.abs(pos - infpos)<=1) or np.any(np.abs(pos - infpos_move)<=1):
                
            bad = np.where(peoplepos_move == pos)
                
            infpos_move = np.append(infpos_move, pos)
                
            peoplepos_move = np.delete(peoplepos_move, bad)
                
            infx_move = np.append(infx_move, peoplex_move[bad])
                
            infy_move = np.append(infy_move, peopley_move[bad])
                
            peoplex_move = np.delete(peoplex_move, bad)
                
            peopley_move = np.delete(peopley_move, bad)
                
            npeople_move-=1
            ninfected_move+=1
            npeople-=1
            ninfected+=1
                
        else:
                
            continue
                
                
    for pos in peoplepos:
            
        if np.any(np.abs(pos - infpos)<=1) or np.any(np.abs(pos - infpos_move)<=1):
                
            bad = np.where(peoplepos == pos)
                
            infpos = np.append(infpos, pos)
                
            peoplepos = np.delete(peoplepos, bad)
                
            infx = np.append(infx, peoplex[bad])
                
            infy = np.append(infy, peopley[bad])
                
            peoplex = np.delete(peoplex, bad)
                
            peopley = np.delete(peopley, bad)
        
            npeople-=1
            ninfected+=1
                
        else:
                
            continue
            



Aucun commentaire:

Enregistrer un commentaire