Skip to content

Class Structure

tomcodewizard edited this page Oct 27, 2023 · 54 revisions

Our code has been written in an Object-Oriented framework, so our code is broken up into scripts which contain classes. These classes interact with one-another in order to execute the task at hand. A summary break-down of the main classes is provided below:

DataGenerator

Location: abm-project-2023/abm_model/generate_data.py

The DataGenerator class inherits from Generator. The class checks for errors amongst the command-line inputs. The class also creates a data frame and saves to a .csv file.

Methods:

__init__(help_string): initialize default attributes

update_parameters(): check for errors amongst the command input

create_csv(): create Pandas data frame

PlotGenerator

Location: abm-project-2023/abm_model/generate_plots.py

The PlotGenerator class inherits from Generator. The class checks for plot command calls. The class also calls Plotter in order for a plot to be made.

Methods:

__init__(help_string): initialize default attributes

update_parameters(): check for --help command call

create_plots(): call Plotter class

Generator

Location: abm-project-2023/abm_model/generator.py

The Generator class contains methods required to allow the user to enter commands in the command line and for it to be interpreted into calling functions in the other abm_model classes.

Methods:

__init__(help_string): initialize the attributes (incl help_string; this is the string displayed when help command called)

get_options(): uses the getopt module to parse command line options entered by the user

update_parameters(): raises an error when called

Point

Location: abm-project-2023/abm_model/gif_plot.py

The Point class contains infection history and information for a person.

Methods:

__init__(position): initialize position of person on the plot

__repr__(): return instance

__eq__(other): check if instances have equivalent arguments

Gif_plotter

Location: abm-project-2023/abm_model/gif_plot.py

The Gif_plotter class .creates a .gif animation by stitching several plots together over time.

Methods:

__init__(cell): initialize the cell used

points_manipulation(): set-up coordinates of gif plot

gif_plotter(path, name): set-up gif figure

update(time, list): contained in gif_plotter - modifies location of image

plot(path, name): creates a .gif animation

Minicell

Location: abm-project-2023/abm_model/minicell.py

The Minicell class contains the agents and tracks them throughout the simulation. This class also writes attribute data to the .csv file, so that the status history can be accessed later by the Plotter class.

Methods:

update(dt): changes the status of each person in the Minicell to match the model at every time-step

write_csv(path): upload the infection history to the .csv file

handle(event): update the events that are to be handled at the end of the time step

Person

Location: abm-project-2023/abm_model/person.py

The Person class contains the attributes describing each agent. The population is made up of instances of the Person class, where each person has a Status object that is updated throughout the simulation. The SIR model acts upon the agent within this class.

Attributes:

.name(str): a unique id/name for each agent

.status(status object): use composition with status class, indicating the status for each person

.history(dict): a dictionary containing the date of infection and date of recovery

Methods:

update(cell): triggers the change of status with an input of 'Minicell' object for originally susceptible people: pass for originally infected people: check whether it is the time to recover: if yes, recover by adding to '.events' to be handled by '.handle()'; if no, generate list of susceptible people to be infected for originally recovered people: pass

read_infection_history(): print out the date of infection and recovery(if exist) based on '.history' attribute

Plotter

Location: abm-project-2023/abm_model/plot.py

The Plotter class handles file-reading and plotting. The data is read from the file and manipulated such that it can be plotted accordingly. The style of the plots is also handled here.

Methods:

__init__(): initializes the class with some default class parameters that will then be read from a file

plot_data(): the main method for this class that opens and reads a file. It then creates a dictionary containing lists of all the different observed values; keyed by the status headers "Time", "Susceptible", "Infected" and "Recovered". Finally, it calls the desired methods to plot the data.

convert_to_ints(str_values_list): the method checks that str_values_list is of the correct length and that the internal values are valid. If this is not the case, then errors will be raised.

create_plot_files: send the files to the correct location

create_plot_legend: create the plot headings, legend and title

Run_tests

Location: abm-project-2023/abm_model/run_tests.py

The Run_tests class will run the unit tests located in abm-project-2023/abm_model/tests/. We aim for the highest amount of code coverage, so the unit tests have been designed to try and cover every block of code. All methods have unit tests.

Methods:

run_unit_tests(): run unit tests (without subprocesses)

Status

Location: abm-project-2023/abm_model/status.py

The Status class acts as a parental class to group the following three status classes: Susceptible, Recovered and Infected. Each person is given a status by assigning them a Status object and updating this object through the simulation.

Child Classes:

Susceptible: class indicates a susceptible status with suitable representation

Recovered: class indicates a recovered status with suitable representation

Infected: class indicates a infected status with suitable representation

Child Methods

__repr__(): returns the string representation of the child's status

__init__(recovery_period: float = 1, current_time: int = 0, threshold: float = 0): initializer function only within Infected class. Checks whether the recovery_period is a valid input. Saves current_time and threshold as attributes.

Clone this wiki locally