infection.util package

Submodules

infection.util.circular_button module

This module defines the CircularButton class and all of its properties and methods.

class infection.util.circular_button.CircularButton(individual: Individual, simulation: Simulation, **kwargs)[source]

Bases: ButtonBehavior, Label

This is the definition of the CircularButton class. It inherits

from the ButtonBehavior and Label Kivy classes, and represents an individual in the canvas.

Args:
individual: Instance of a HealthyIndividual or InfectedIndividual

classes to control and access its status.

simulation: Instance of the simulation class to be able to access

all the simulation parameters.

Attributes:

The direction_x, direction_y, and direction attributes are Kivy properties required to track the position of the button in the canvas. These are the only Kivy properties used because of their flexibility and automatic reference. The program will not work if regular Python properties are used. For more information visit https://kivy.org/doc/stable/api-kivy.properties.html#kivy.properties.ReferenceListProperty

direction_x: Kivy NumericProperty that stores the direction in the

‘x’ axis. Initialized to 0.

direction_y: Kivy NumericProperty that stores the direction in the

‘y’ axis. Initialized to 0.

direction: Kivy ReferenceListProperty linked to the direction_x

and direction_y Kivy properties, to update its value automatically whenever either of them changes, and vice versa.

individual: Stores an instance of a HealthyIndividual class or

an InfectedIndividual class.

simulation: To store the instance of the Simulation class. speed: Float property with a value from 0 to 1 that determines

the speed of the button in the canvas.

direction

ReferenceListProperty(*largs, **kw) Property that allows the creation of a tuple of other properties.

For example, if x and y are NumericPropertys, we can create a ReferenceListProperty for the pos. If you change the value of pos, it will automatically change the values of x and y accordingly. If you read the value of pos, it will return a tuple with the values of x and y.

For example:

class MyWidget(EventDispatcher):
    x = NumericProperty(0)
    y = NumericProperty(0)
    pos = ReferenceListProperty(x, y)
direction_x

NumericProperty(defaultvalue=0, **kw) Property that represents a numeric value.

It only accepts the int or float numeric data type or a string that can be converted to a number as shown below. For other numeric types use ObjectProperty or use errorhandler to convert it to an int/float.

It does not support numpy numbers so they must be manually converted to int/float. E.g. widget.num = np.arange(4)[0] will raise an exception. Numpy arrays are not supported at all, even by ObjectProperty because their comparison does not return a bool. But if you must use a Kivy property, use a ObjectProperty with comparator set to np.array_equal. E.g.:

>>> class A(EventDispatcher):
...     data = ObjectProperty(comparator=np.array_equal)
>>> a = A()
>>> a.bind(data=print)
>>> a.data = np.arange(2)
<__main__.A object at 0x000001C839B50208> [0 1]
>>> a.data = np.arange(3)
<__main__.A object at 0x000001C839B50208> [0 1 2]
Parameters:
defaultvalue: int or float, defaults to 0

Specifies the default value of the property.

>>> wid = Widget()
>>> wid.x = 42
>>> print(wid.x)
42
>>> wid.x = "plop"
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "properties.pyx", line 93, in kivy.properties.Property.__set__
   File "properties.pyx", line 111, in kivy.properties.Property.set
   File "properties.pyx", line 159, in kivy.properties.NumericProperty.check
 ValueError: NumericProperty accept only int/float

Changed in version 1.4.1: NumericProperty can now accept custom text and tuple value to indicate a type, like “in”, “pt”, “px”, “cm”, “mm”, in the format: ‘10pt’ or (10, ‘pt’).

direction_y

NumericProperty(defaultvalue=0, **kw) Property that represents a numeric value.

It only accepts the int or float numeric data type or a string that can be converted to a number as shown below. For other numeric types use ObjectProperty or use errorhandler to convert it to an int/float.

It does not support numpy numbers so they must be manually converted to int/float. E.g. widget.num = np.arange(4)[0] will raise an exception. Numpy arrays are not supported at all, even by ObjectProperty because their comparison does not return a bool. But if you must use a Kivy property, use a ObjectProperty with comparator set to np.array_equal. E.g.:

>>> class A(EventDispatcher):
...     data = ObjectProperty(comparator=np.array_equal)
>>> a = A()
>>> a.bind(data=print)
>>> a.data = np.arange(2)
<__main__.A object at 0x000001C839B50208> [0 1]
>>> a.data = np.arange(3)
<__main__.A object at 0x000001C839B50208> [0 1 2]
Parameters:
defaultvalue: int or float, defaults to 0

Specifies the default value of the property.

>>> wid = Widget()
>>> wid.x = 42
>>> print(wid.x)
42
>>> wid.x = "plop"
 Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "properties.pyx", line 93, in kivy.properties.Property.__set__
   File "properties.pyx", line 111, in kivy.properties.Property.set
   File "properties.pyx", line 159, in kivy.properties.NumericProperty.check
 ValueError: NumericProperty accept only int/float

Changed in version 1.4.1: NumericProperty can now accept custom text and tuple value to indicate a type, like “in”, “pt”, “px”, “cm”, “mm”, in the format: ‘10pt’ or (10, ‘pt’).

distance(coord: ObservableReferenceList) float[source]
This is a simple formula to calculate the ecludian distance

between the button’s current coordinate (self.pos) and a second button’s coordinate (coord) in the canvas.

Args:
coord (kivy.properties.ObservableReferenceList):

The position of the second button to measure the distance.

Returns:

float: The distance between the two buttons.

property individual: Individual
move(sim: Simulation) None[source]
Method that moves the button across the canvas one step each

refresh cycle. If the button is at the edge of the canvas, its direction is inverted by multiplying it by -1 to simulate a “bounce” against the edge.

Args:

sim (simulation): Instance of the main simulation app class to access the menu_bottom’s height and the root widget’s height and width to check if the button is at the edge of the canvas.

property simulation: Simulation
property speed: float

infection.util.individual module

This module defines the Individual abstract class, and its HeallthyIndividual and InfectedIndividual subclasses and all of their properties and methods.

class infection.util.individual.HealthyIndividual(simulation: Simulation, infection_probability: float, **kwargs)[source]

Bases: Individual

This is the definition of the HealthyIndividual class. It inherits

from the Individual abstract class.

Args:

simulation (Simulation): Instance of the Simulation class. infection_probability (Float): A value from 0 to 1 that

determines how likely is an individual to get infected.

Attributes:

simulation: To store the instance of the simulation class. infection_probability: To store the infection_probability. recovered: Boolean property used to track when the individual has

recovered from an infection. False by default. True when it has recovered from an infection. An individual that has recovered from an infection can no longer get infected.

time_infected: Integer property that tracks during how many cycles

the individual has been infected.

max_time_infected: Integer property that defines how many cycles need

to pass until the individual recovers. This private property can’t be modified since it doesn’t have a setter method. Set to MAX_TIME_INFECTED.

cooldown: Integer property that tracks how many cycles have passed

after the last infection evaluation to avoid evaluating against the same individual multiple times after crossing paths.

max_cooldown: Integer property that defines how many cycles need

to pass until the infection evaluations restart. This private property can’t be modified since it doesn’t have a setter method. Set to MAX_COOLDOWN.

status: String property that tracks the infection status of the

individual. “healthy” by default.

property cooldown: int
count_infected_neighbors(circular_button: CircularButton, quad_tree: QuadTree) int[source]
Method that searches the quad_tree to find the number of infected

individuals within the provided radius and returns the count in the infected_neighbor_count variable.

Args:
circular_button (CircularButton): Instance of the button

containing the individual.

quad_tree (QuadTree): A quadtree structure that contains the

positions of all the individuals in the simulation for fast neighbor search.

Returns:
infected_neighbor_count (int): The number of infected individuals

within the provided radius.

evaluate_infection(circular_button: CircularButton, quad_tree: QuadTree) tuple[int, int][source]
Method that calls the count_infected_neighbors method and if

there’s one or more infected neighbors, a formula is used to randomly calculate if the individual should get infected based on the infection_probability and the number of infected neighbors.

Args:
circular_button (CircularButton): Instance of the button

containing the individual.

quad_tree (QuadTree): A quadtree structure that contains the

positions of all the individuals in the simulation for fast neighbor search.

Returns:

infected (int): 0 if not infected, 1 or more if infected. infected_neighbor_count: The number of infected individuals

within the provided radius.

infection(circular_button: CircularButton, quad_tree: QuadTree) None[source]
Method that controls if the individual will get infected by

being around one or more infected individuals in the provided quad_tree, or if the individual is now recovered because self.max_time_infected cycles have passed after infection.

Args:
circular_button (CircularButton): The instance of the button

containing the individual to change its properties.

quad_tree (QuadTree): A quadtree structure that contains the

position of all the individuals in the canvas for fast neighbor search.

property infection_probability: float
property max_cooldown: int
property max_time_infected: int
recover(circular_button: CircularButton) None[source]

Method to set the individual’s properties when it recovers.

Args:
circular_button (CircularButton): Instance of the button

containing the individual.

property recovered: int

Boolean property used to track when the individual has recovered from an infection.

sick(circular_button: CircularButton) None[source]

Method to set the individual’s properties when it gets sick.

Args:
circular_button (CircularButton): Instance of the button

containing the individual.

property simulation: Simulation

To store an instance of the simulation class.

property status: str

String property used to track the infection status of the individual.

property time_infected: int

Integer property to track how long the individual has been infected.

class infection.util.individual.Individual[source]

Bases: ABC

This is the definition of the Individual abstract class. It inherits from the ABC class to make it an abstract class.

abstract infection() None[source]

Abstract method that will control the infection status of the individual and decide if it will get infected or if it will recover.

abstract recover() None[source]

Abstract method that will set the properties when the individual recovers from infection

abstract property recovered: bool

Boolean property used to track when the individual has recovered from an infection.

abstract property simulation: Simulation

To store an instance of the simulation class.

abstract property status: str

String property used to track the infection status of the individual.

abstract property time_infected: int

Integer property to track how long the individual has been infected.

class infection.util.individual.InfectedIndividual(simulation: Simulation, **kwargs)[source]

Bases: Individual

This is the definition of the InfectedIndividual class. It inherits from the Individual abstract class.

Args:

simulation (Simulation): Instance of the Simulation class.

Attributes:

simulation: To store the instance of the simulation class. recovered: Boolean property used to track when the individual has

recovered from an infection. False by default. True when it has recovered from an infection. An individual that has recovered from an infection can no longer get infected.

time_infected: Integer property that tracks during how many cycles

the individual has been infected.

max_time_infected: Integer property that defines how many cycles need

to pass until the individual recovers. This private property can’t be modified since it doesn’t have a setter method. Set to MAX_TIME_INFECTED.

status: String property that tracks the infection status of the

individual. “infected” by default.

infection(circular_button: CircularButton, quad_tree: QuadTree) None[source]
Method that controls if the individual is now recovered because

self.max_time_infected cycles have passed after infection.

Args:
circular_button (CircularButton): The instance of the button

containing the individual to change its properties.

quad_tree (QuadTree): A quadtree structure that contains the

position of all the individuals in the canvas for fast neighbor search. Ignored in the InfectedIndividual class.

property max_time_infected: int
recover(circular_button: CircularButton) None[source]

Method to set the individual’s properties when it recovers.

Args:
circular_button (CircularButton): Instance of the button

containing the individual.

property recovered: bool

Boolean property used to track when the individual has recovered from an infection.

property simulation: Simulation

To store an instance of the simulation class.

property status: str

String property used to track the infection status of the individual.

property time_infected: int

Integer property to track how long the individual has been infected.

infection.util.menu_bottom module

The following module is the definition of the MenuBottom class, its properties, and its methods.

class infection.util.menu_bottom.MenuBottom(simulation: Simulation, **kwargs)[source]

Bases: BoxLayout

This is the definition of the Menu class. It inherits from the

BoxLayout Kivy class.

Attributes:

simulation: Here we store the instance of the Simulation class. lbl_infection_probability: Kivy Label to identify the infection

probability in the menu.

lbl_sldr_infection_probability: Kivy Label that displays the value

of the simulation’s infection probility.

sldr_infection_probability: Kivy Slider that allows the user to

set the infection probability. Its value is bound to the Label that displays the value of the simulation’s infection probility, by the on_value_infection_probability method.

lbl_population: Kivy Label to identify the Total Individual

population count in the menu.

lbl_value_population: Kivy Label that displays the total Individual

population count.

lbl_healthy: Kivy Label to identify the healthy Individual count in

the menu.

lbl_value_healthy: Kivy Label that displays the healthy Individual

count.

lbl_infected: Kivy Label to identify the infected Individual count

in the menu.

lbl_value_infected: Kivy Label that displays the infected Individual

count.

on_value_infection_probability(instance: Slider, probality: float) None[source]
Method that updates the text of the infection probability value

Label to the value of the Slider when it changes.

Args:
instance (kivy.uix.slider.Slider): The infection probability

Slider’s instance.

probality (float): The value of the new infection probability set

in the Slider. Goes from 0.0 to 1.0

infection.util.menu_right module

The following module is the definition of the MenuRight class, its properties, and its methods.

class infection.util.menu_right.MenuRight(simulation: Simulation, **kwargs)[source]

Bases: BoxLayout

This is the definition of the MenuRight class. It inherits from the

BoxLayout Kivy class.

Args:

simulation (Simulation): Instance of the Simulation class.

Attributes:

simulation: Here we store the instance of the Simulation class. btn_add_healthy: Kivy Button that allows the user to add a healthy

individual to the simulation.

btn_add_infected: Kivy Button that allows the user to add an

infected individual to the simulation.

btn_reset: Kivy Button that allows the user to reset the

simulation, removing all healthy and infected individuals.

btn_healthy_color: Kivy Button that shows the

popup_healthy_color_picker.

btn_cancel_healthy_color: Kivy Button that closes the

popup_healthy_color_picker.

btn_save_healthy_color: Kivy Button to save the Healthy Individual

color.

healthy_clr_pkr: Kivy ColorPicker to allow the user to change the

color of the Healthy Individual.

popup_healthy_color_picker: Kivy Popup widget that contains the

healthy_clr_pkr, btn_cancel_healthy_color and btn_save_healthy_color widgets.

btn_infected_color: Kivy Button that shows the

popup_infected_color_picker.

btn_cancel_infected_color: Kivy Button that closes the

popup_infected_color_picker.

btn_save_infected_color: Kivy Button to save the Infected Individual

color.

infected_clr_pkr: Kivy ColorPicker to allow the user to change the

color of the Infected Individual.

popup_infected_color_picker: Kivy Popup widget that contains the

infected_clr_pkr, btn_cancel_infected_color and btn_save_infected_color widgets.

btn_recovered_color: Kivy Button that shows the

popup_recovered_color_picker.

btn_cancel_recovered_color: Kivy Button that closes the

popup_recovered_color_picker.

btn_save_recovered_color: Kivy Button to save the Recovered Individual

color.

recovered_clr_pkr: Kivy ColorPicker to allow the user to change

the color of the Recovered Individual.

popup_recovered_color_picker: Kivy Popup widget that contains the

recovered_clr_pkr, btn_cancel_recovered_color and btn_save_recovered_color widgets.

cancel_color(instance: Button, type: str) None[source]

Method that closes the corresponding color picker popup.

Args:
instance (kivy.uix.button.Button): The clicked Button’s

instance.

type (str): A String that indicates the individual type.

save_color(instance: Button, type: str) None[source]
Method that saves the value of the color picker to its

corresponding value in the simulation.

Args:
instance (kivy.uix.button.Button): The clicked Button’s

instance.

type (str): A String that indicates the individual type.

show_color_picker(instance: Button, type: str) None[source]

Method that opens the corresponding color picker popup.

Args:
instance (kivy.uix.button.Button): The clicked Button’s

instance.

type (str): A String that indicates the individual type.

Module contents