fcp2021
README.md

1 EMAT10006: Further Computer Programming

This repository stores the coursework for Further Computer Programming

Table of Contents


1.1 Members of the team


1.2 About the Project

We decided to create a simulation of how a pandemic might spread in any community of the world. To do this, we fetched real data from the UK governments's API, transformmed it into a pandas frame, and visualized it using an animated graph whose lines move along an axis. After having a rough idea of how COVID-19 spreaded in the UK, we moved on to code two detailed models.

The first model is obtained by creating an object (e.g. simulation) from the Simulation class in Project_Classes. It will generate a numpy matrix, with its size equal to the simulated population Each "person" in the matrix contains the following attributes (states):

  1. Susceptible
  2. Infected (asymptomatic)
  3. Infected (symptomatic)
  4. Recovered
  5. Hospitalized
  6. Dead
  7. Incubated

This first class will work in pair with the Animation class, which will create a grid and a line animation. Those two will be updated to show the evolution of the simulation object in a size*size matrix, with the latter's data being recorded on the line plot for each status.

The second model involves an animation of many small dots as real people, in an attempt to model the realistic physical interaction between humans in a neighbourhood. This model is contained in the folder test_infection_simulation, and stores 2 files, person.py and simulate_people.py. The former file contains the object "person". The latter file imports the object, and creates an animation with a subplot based on the following global constants(attributes):

  1. Death Rate
  2. Transmission Rate
  3. Transmission Radius
  4. Isolation Rate
  5. Recovery time
  6. Re-infect rate
  7. Initial percentage of infection

To combine everything in a tidy manner, we coded a Graphical User Interface (GUI) to wrap the two models together, taking reference from a youtube tutorial. The GUI is stored under GUI.py. The first window allows the user to change the following parameters:

  1. Death Rate
  2. Transmission Rate
  3. Chances of Symptoms
  4. Social Interaction
  5. Recovery Time

These parameters are passed down to the Simulation and Animation class. The results are plotted on It can be used to open 6 graphs, 3 of which is animated, and the other 3 are stationary. The second window then allows the user to choose their desired graph to open.

If the user wants to navigate the repository, they can enter command lines and run individual files locally. Command lines are found below.


1.3 First Model

1.3.1 Project Classes

The Project_Classes.py script gathers all the classes and function used in the fcp.py file.

  • Simulation class: runs simulations of an epidemic (e.g. coronavirus) spreading around people on 2-dimensional form. Objects can be created from this class to:

    • Model the different interactions between people and calculate the state of the sample for the next 'day'
    • Store the data for all status each day in a dictionary for the duration of the simulation (This will be used with the Animation Class see below)
    • Create Excel files storing all the data from the simulation object
  • Animation Class: the object simulation can also be used with the Animation class to create a new object (e.g. animation). It will display an rgb matrix defined by the simulation object. Each square's color will represent the status of the corresponding slot (or person) in the matrix. They will be modified according to the Simulation class functions for every day of the animation. In addition, animation will also display a line plot of the data of all statuses collected from the rgb matrix (e.g. how many infected people at time T).

  • evolution_simulation function : shows the evolution in time of the virus spreading. The duration of the simulation will be divided into intervals and each interval will be represented by an image of the simulation's rgb matrix. Finally, all those images will be displayed on a figure with their days number as titles.


1.3.2 UK_2020_simulation.py

This script attempts to run a simulation of COIVD-19 on the entire UK population, through the year 2020. Due to the size and complexity of this task, this script takes a few hours to run. Run at your own risk.


1.3.3 Bristol_Students_simulation.py

This script attempts to run a simulation of COVID-19 on the University of Bristol student community.


1.3.4 Graphical User Interface

GUI.py:

This script runs a GUI where user can choose any value for the inputs to run the simulation and see data visualization of UK's COVID-19 data. This script can be used to:

  1. Take user input and run the simulation
  2. Save the simulation results in csv files
  3. Show and save an animation of real data graph
  4. Show still image of real data graph

NOTE: This script must be run with the UK_full_covid_data_may.csv on the same path with it.


1.3.5 Command Line

fcp.py: This script uses the argparser method to allow the user to change any arguments from the command line. Thus, no script editing are needed to obtain a different simulation object.

A simulation object can be modified according to the following arguments:

  • --size: determines the dimension of the matrix. The matrix is 2-dimensioned therefore the number of people is size x size. In the default case, size is 50, therefore number of people is 2500. This makes the matrix large enough to see the spread of the virus (of course this will also depend on the other parameters). Its value should be comprised between 10 (to see a minimum of interactions) and 100 (the animation's colours become very small, making it hard to see individual interactions. This should be used to have an overall idea of the virus model)

  • --days: determines the number of days the simulation will run for. Depending on the arguments you will want to lengthen or shorten the duration of the simulation object. In general, the value should be between 20 and 200.

  • --asymp_cases: number of asymptomatic cases introduced in the matrix. It can be any positive integer. The higher the introduced cases, the shorter the simulation will last.

  • --symp_cases: same as before but for symptomatic cases.

  • --inter_asymp: determines the average interactions of asymptomatic cases with other people. The value can be a float number: 0.1 would be equivalent to a strict lockdown 2 would be equivalent to normal life (even if this number can go a lot higher)

  • --inter_symp: same as before for symptomatic cases

  • --range_asymp: determines the maximum range at which infected people (asymptomatic) can infect others at. It would make sense to make this number smaller than --range_symp as asymptomatic cases do not know they can transmit the virus. In general, the value should be between 1 and 4 (higher values will make the animation a lot shorter)

  • --range_symp: same as before except that this value will generally be 1 or 2 less than --range_asymp (positive value always)

  • lockdown: instaures a lockdown in the population in the matrix. Greatly reduces the infection rate and thus make the animation very short. Value: True or False (default)

The simulation object obtained can then be displayed on the screen (default) or saved to a file with:

  • --file: specify the format to save the file in (see example below). Choose between a video format or image format depending on the argument --plot.

  • --plot: allows the simulation to be plotted at different intervals in time (days) and outputs a figure with n number of axes, each showing the simulation at different stage of the virus in the matrix. To select this option simply type $ python fcp.py --plot (see below more example)

  • --width: determines how many images will be displayed on the width of the figure in the plot of the simulation This value can be anything. The higher the value, the more images of the simulation at different days in time and thus the smaller the images will be.

  • --height: similarly to the previous argument except this one determines the images per rows.

Here are a few examples of commands to run on the terminal:

$ python fcp.py                                          # run simulation with default settings
$ python fcp.py --help                                   # show all command line arguments (both optional and required)
$ python fcp.py --size=50 --plot --width=3 --height=5    # Plots 15 images of the simulation (3x5) at equally spaced days in time of a matrix 
                                                          (i.e. 2500 people)
$ python fcp.py --file='Animation.mp4'                   # Saves the animation of the simulation as a mp4 file named Animation
$ python fcp.py --plot --file='Simulation_Plot.pdf'      # Saves the simulation as successive images of the animation at different days in time

GUI.py: This script can be run directly on the terminal. The command line is as follow:

$ python GUI.py                                          # Open the GUI window

1.4 Second Model

1.4.1 test_infection_simulation

This folder stores 2 script that run an animation of COVID-19 in a small neighbourhood. The first script person.py gives the "person" object some attributes, both intrinsic properties and COVID-19 related properties. They include:

  • Intrinsic
    • Index (used for identification purposes)
    • position x,y (current position in the x and y directions)
    • objective x,y (targeted displacement in the x and y directions)
    • velocity (person's velocity in vector form)
  • COVID-19 related
    • infection time (time it takes for the virus to stay in the person)
    • fixed position (in quarantine, boolean state)
    • susceptible (boolean state)
    • dead (boolean state)
    • infected (boolean state)

The second script simulate_people.py codes the movement and interaction for every instance of "person". At the start of each frame, each "person" is given intrinsic attributes at random, while COVID-19 attributes are determined by the function update(). The function graphics() updates everything visusally. The simulation runs for infinitely long, but the subplot axis is only set to show 1000 days.

Unlike aforementioned simulations, this script doesn't take user input, therefore no arguments are needed in the command line. However, all above parameters are stored in the __init__() function. To change these parameters, directly access test_infection_simulation/simulate_people.py. The script took reference from this article about COVID-19.

To run the simulation, cd into the folder test_infection_simulation/ and copy the line:

$ python simulate_people.py                              # Initiates the animation and subplot

1.5 Requirements

A requirements.txt file is included to install required python dependencies.

To install python dependencies, run:

pip3 install -r requirements.txt    

NOTE: ffmpeg AND MiKTeX must be installed on PATH to run the program.