episimmer.model
Base Model API
- class BaseModel(name)[source]
Base class for disease models in Episimmer.
- Parameters
name (str) – Name of Disease Model
- state_proportion_checker()[source]
Checks whether the state proportions add up to 1.
- Return type
None
- initialize_states(agents)[source]
Initializes the states of the agents based on state proportions.
- Parameters
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Return type
None
- find_next_state(agent, agents)[source]
Returns next state of the agent according to the transition functions between the states and the schedule time left.
- Parameters
agent (episimmer.agent.Agent) – The agent whose next state is to be determined
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
The new state of the agent, Schedule time left for the agent in current state
- Return type
Tuple[str, int]
- set_state_color(state, infectious)[source]
Sets the state color based on whether the state is infectious or not.
- Parameters
state (str) – State that needs color assignment
infectious (bool) – Whether the state is infectious or not
- Return type
None
- update_event_infection(event_info, location, agents_obj)[source]
Updates the agents with event probabilities of all the events the agents attended.
- Parameters
event_info (Dict[str, Union[float, str, List[str]]]) – Dictionary containing location and participating agents of an event
location (episimmer.location.Location) – Location object
agents_obj (episimmer.read_file.ReadAgents) – An object of class
ReadAgentscontaining all agents
- Return type
None
- set_event_contribution_fn(fn)[source]
Sets the event contribute function specifying the contribution of an agent to the ambient infection of an event. It must be set to a Callable function with four parameters : agent, event_info, location and time_step. agent refers to the current agent responsible for contribution to ambient infection. event_info and location refer to the current event’s parameters from the event and location files. time_step refers to the current time step.
An example with the Stochastic Model is given below
1def event_contribute_fn(agent,event_info,location,current_time_step): 2 if agent.state=='Infected': 3 return 1 4 return 0 5 6class UserModel(model.StochasticModel): 7 def __init__(self): 8 . 9 . 10 . 11 self.set_event_contribution_fn(event_contribute_fn)
- Parameters
fn (Callable) – User-defined function used to determine the contribution of an agent to an ambient infection
- Return type
None
- set_event_receive_fn(fn)[source]
Sets the event receive function specifying the probability of infection for an agent from the ambient infection of an event. It must be set to a Callable function with four parameters : agent, ambient_infection, event_info, location and time_step. agent refers to the current agent receiving infection from participating in the event, ambient_infection is the total infection accumulated by all the agents in the event (calculated by event_contribution_fn). event_info and location refer to the current event’s parameters from the event and location files. time_step refers to the current time step.
An example with the Stochastic Model is given below
1def event_receive_fn(agent,ambient_infection,event_info,location,current_time_step): 2 beta=0.001 3 return ambient_infection*beta 4 5class UserModel(model.StochasticModel): 6 def __init__(self): 7 . 8 . 9 . 10 self.set_event_receive_fn(event_receive_fn)
- Parameters
fn (Callable) – User-defined function used to determine the probability of an agent receiving an ambient infection
- Return type
None
- set_external_prevalence_fn(fn)[source]
Sets the external prevalence function to specify probability of infection due to external prevalence. It must be set to a Callable function with two parameters : agent and time_step. agent refers to the current agent affected by external prevalence and time_step refers to the current time step.
An example with the Stochastic Model is given below
1def external_prevalence(agent, current_time_step): 2 if(agent.info['Compliance'] == 'High'): 3 return 0.1 4 elif(agent.info['Compliance'] == 'Medium'): 5 return 0.2 6 elif(agent.info['Compliance'] == 'Low'): 7 return 0.35 8 9class UserModel(model.ScheduledModel): 10 def __init__(self): 11 . 12 . 13 . 14 self.set_external_prevalence_fn(external_prevalence)
- Parameters
fn (Callable) – User-defined function for specifying probability of infection due to external prevalence
- Return type
None
- states_checker(states)[source]
Checks whether the states list passed belong to the states in the disease model created.
- Parameters
states (List[str]) – List of states to be checked
- Returns
Boolean representing whether valid states have been passed
- Return type
bool
- set_symptomatic_states(states)[source]
Sets the symptomatic states of the disease model. These agents in these states have visible symptoms of the disease.
An example with the Stochastic Model is given below
1class UserModel(model.StochasticModel): 2 def __init__(self): 3 individual_types=['Susceptible','Exposed','Asymptomatic','Symptomatic','Recovered'] 4 infected_states=['Asymptomatic','Symptomatic'] 5 state_proportion={ 6 'Susceptible':0.99, 7 'Exposed':0, 8 'Recovered':0, 9 'Asymptomatic':0, 10 'Symptomatic':0.01 11 } 12 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 13 self.set_transition('Susceptible', 'Exposed', self.p_infection()) 14 self.set_transition('Exposed', 'Symptomatic', self.p_standard(0.15)) 15 self.set_transition('Exposed', 'Asymptomatic', self.p_standard(0.2)) 16 self.set_transition('Symptomatic', 'Recovered', self.p_standard(0.1)) 17 self.set_transition('Asymptomatic', 'Recovered', self.p_standard(0.1)) 18 19 self.set_event_contribution_fn(event_contribute_fn) 20 self.set_event_receive_fn(event_receive_fn) 21 self.set_symptomatic_states(['Symptomatic'])
- Parameters
states (List[str]) – List of disease model states that are symptomatic
- Return type
None
- get_final_infection_prob(fn, p_infected_states_list, agent, agents)[source]
Returns the final infection probability for an agent based on
Interactions (Individual and Probabilistic)
Events (Regular and One-Time)
External Prevalence
- Parameters
fn (Optional[Callable]) – User-defined function defining the probability of infection based on individual/probabilistic interactions
p_infected_states_list (Optional[List[float]]) – List of probabilities that can be used in the user-defined function fn
agent (episimmer.agent.Agent) – Current agent object
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Return type
float
Stochastic Model API
- class StochasticModel(individual_state_types, infected_states, state_proportion)[source]
Class for the Stochastic model.
A simple example of a UserModel.py file with a Stochastic disease model is given below
1import episimmer.model as model 2 3def event_contribute_fn(agent,event_info,location,current_time_step): 4 if agent.state=='Symptomatic': 5 return 1 6 elif agent.state=='Asymptomatic': 7 return 0.5 8 return 0 9 10def event_receive_fn(agent,ambient_infection,event_info,location,current_time_step): 11 #Example 1 12 beta=0.001 13 return ambient_infection*beta 14 15 16class UserModel(model.StochasticModel): 17 def __init__(self): 18 individual_types=['Susceptible','Exposed','Asymptomatic','Symptomatic','Recovered'] 19 infected_states=['Asymptomatic','Symptomatic'] 20 state_proportion={ 21 'Susceptible':0.99, 22 'Exposed':0, 23 'Recovered':0, 24 'Asymptomatic':0, 25 'Symptomatic':0.01 26 } 27 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 28 self.set_transition('Susceptible', 'Exposed', self.p_infection()) 29 self.set_transition('Exposed', 'Symptomatic', self.p_standard(0.15)) 30 self.set_transition('Exposed', 'Asymptomatic', self.p_standard(0.2)) 31 self.set_transition('Symptomatic', 'Recovered', self.p_standard(0.1)) 32 self.set_transition('Asymptomatic', 'Recovered', self.p_standard(0.1)) 33 34 self.set_event_contribution_fn(event_contribute_fn) 35 self.set_event_receive_fn(event_receive_fn)
- Parameters
individual_state_types (List[str]) – The states in the compartment model
infected_states (List[str]) – The states that are infectious
state_proportion (Dict[str, Union[float, int]]) – Starting proportions of each state
- set_transition(s1, s2, fn)[source]
Adds a transition probability function between the specified states. The user must specify one of the following functions for the transition function
1def rec_to_dead_fn(time_step): 2 return 0.1 3 4class UserModel(model.StochasticModel): 5 def __init__(self): 6 individual_types=['Susceptible','Infected','Recovered','Dead'] 7 infected_states=['Infected'] 8 state_proportion={ 9 'Susceptible':0.99, 10 'Infected':0.01, 11 'Recovered':0 12 'Dead':0 13 } 14 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 15 self.set_transition('Susceptible', 'Infected', self.p_infection()) 16 self.set_transition('Infected', 'Recovered', self.p_standard(0.2)) 17 self.set_transition('Recovered', 'Dead', self.p_function(rec_to_dead_fn))
- Parameters
s1 (str) – The first state
s2 (str) – The second state
fn (Callable) – The function to be used for calculating the probability of transition
- Return type
None
- initialize_states(agents)[source]
Initializes the states of the agents based on state proportions.
- Parameters
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Return type
None
- find_next_state(agent, agents)[source]
Returns next state of the agent according to the transition functions between the states stored in transmission_prob.
- Parameters
agent (episimmer.agent.Agent) – The current agent whose next state is to be determined
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
The new state of the agent
- Return type
Tuple[str, None]
- full_p_standard(p, agent, agents)[source]
Returns a fixed probability of transition.
- Parameters
p (float) – Probability of transition
agent (episimmer.agent.Agent) – An agent object
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
Probability of transition
- Return type
float
- p_standard(p)[source]
This function can be used by the user in
UserModel.pyto specify an independent transition with a fixed probability. It returns a partial function offull_p_standard().1class UserModel(model.StochasticModel): 2 def __init__(self): 3 individual_types=['Susceptible','Infected','Recovered'] 4 infected_states=['Infected'] 5 state_proportion={ 6 'Susceptible':0.99, 7 'Infected':0.01, 8 'Recovered':0 9 } 10 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 11 . 12 . 13 . 14 self.set_transition('Infected', 'Recovered', self.p_standard(0.2))
- Parameters
p (float) – Probability of transition
- Returns
Partial function
- Return type
Callable
- full_p_function(fn, agent, agents)[source]
Returns the probability of transition that is specified by a user-defined function.
- Parameters
fn (Callable) – User-defined function defining the probability of transition
agent (episimmer.agent.Agent) – An agent object
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
Probability of transition
- Return type
float
- p_function(fn)[source]
This function can be used by the user in
UserModel.pyto specify an independent transition with a user-defined function defining the probability of transition. This user-defined function must have a single parameter defining the current time step of the simulation. It returns a partial function offull_p_function().1def rec_to_dead_fn(time_step): 2 return 0.1 3 4class UserModel(model.StochasticModel): 5 def __init__(self): 6 individual_types=['Susceptible','Infected','Recovered','Dead'] 7 infected_states=['Infected'] 8 state_proportion={ 9 'Susceptible':0.99, 10 'Infected':0.01, 11 'Recovered':0 12 'Dead':0 13 } 14 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 15 . 16 . 17 . 18 self.set_transition('Recovered', 'Dead', self.p_function(rec_to_dead_fn))
- Parameters
fn (Callable) – User-defined function defining the probability of transition
- Returns
Partial function
- Return type
Callable
- full_p_infection(fn, p_infected_states_list, agent, agents)[source]
This function returns the probability of infection based on all types of interaction between the current agent and other agents.
- Parameters
fn (Optional[Callable]) – User-defined function defining the probability of infection based on individual/probabilistic interactions
p_infected_states_list (Optional[List[float]]) – List of probabilities that can be used in the user-defined function fn
agent (episimmer.agent.Agent) – Current agent object
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
Probability of getting an infection
- Return type
float
- p_infection(fn=None, p_infected_states_list=None)[source]
This function can be used by the user in
UserModel.pyto specify a dependent transition. The transition probability is based on all the types of interactions between the agents. For individual and probabilistic interactions, we must pass two parameters. The first parameter defines the user-defined function fn which returns the probability of infection for every agent considering these two types of interactions. The second parameter is an optional list of infected state probabilities to be used in the user-defined function fn. The parameters to be passed in the user-defined function are p_infected_states_list, contact_agent, c_dict and current_time_step. p_infected_states_list is the above-mentioned list of probabilities, contact_agent is the agent that the current agent is in contact with (through an individual or probabilistic interaction), c_dict is a dictionary defining the interaction and current_time_step is the current time step. If you do not have any of these types of interactions, you need not pass anything. Returns a partial function offull_p_infection().1class UserModel(model.StochasticModel): 2 def __init__(self): 3 individual_types=['Susceptible','Infected','Recovered'] 4 infected_states=['Infected'] 5 state_proportion={ 6 'Susceptible':0.99, 7 'Infected':0.01, 8 'Recovered':0 9 } 10 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 11 self.set_transition('Susceptible', 'Infected', self.p_infection())
In case of individual or probabilistic interactions, a callable function must be passed. A list can also be passed optionally. In the following example, we use the list.
1def probability_of_infection_fn(p_infected_states_list, contact_agent ,c_dict, current_time_step): 2 if contact_agent.state=='Infected': 3 return p_infected_states_list[0] 4 return 0 5 6class UserModel(model.StochasticModel): 7 def __init__(self): 8 individual_types=['Susceptible','Infected','Recovered'] 9 infected_states=['Infected'] 10 state_proportion={ 11 'Susceptible':0.99, 12 'Infected':0.01, 13 'Recovered':0 14 } 15 model.StochasticModel.__init__(self,individual_types,infected_states,state_proportion) 16 self.set_transition('Susceptible', 'Infected', self.p_infection(probability_of_infection_fn, [0.1]))
- Parameters
fn (Optional[Callable]) – User-defined function defining the probability based on individual/probabilistic interactions
p_infected_states_list (Optional[List[float]]) – List of probabilities that can be used in the user-defined function fn
- Returns
Partial function
- Return type
Callable
Scheduled Model API
- class ScheduledModel[source]
Class for the Scheduled model.
A simple example of a UserModel.py file with a Scheduled disease model is given below
1import episimmer.model as model 2 3def event_contribute_fn(agent,event_info,location,current_time_step): 4 if agent.state=='Infected': 5 return 1 6 return 0 7 8def event_receive_fn(agent,ambient_infection,event_info,location,current_time_step): 9 #Example 1 10 beta=0.001 11 return ambient_infection*beta 12 13 14class UserModel(model.ScheduledModel): 15 def __init__(self): 16 model.ScheduledModel.__init__(self) 17 self.insert_state('Susceptible',None, None,self.p_infection({'Infected':1}),False,0.99) 18 self.insert_state('Infected',6,3,self.scheduled({'Recovered':1}),True,0.01) 19 self.insert_state('Recovered',0, 0,self.scheduled({'Recovered':1}),False,0) 20 21 self.set_event_contribution_fn(event_contribute_fn) 22 self.set_event_receive_fn(event_receive_fn)
- insert_state(state, mean, vary, transition_fn, infected_state, proportion)[source]
Inserts a state into the model and schedules the agent for this state using a Normal distribution. The mean and variance are passed here as parameters to the Normal distribution. The user must specify one of the following functions for the transition function
The last two parameters define whether the state is an infected state and the initial proportion of agents belonging to this state.
1class UserModel(model.ScheduledModel): 2 def __init__(self): 3 model.ScheduledModel.__init__(self) 4 self.insert_state('Susceptible', 2, 1, self.p_infection({'Infected':1}), False, 0.99) 5 self.insert_state('Infected', 6, 3, self.scheduled({'Recovered':1}), True, 0.01) 6 self.insert_state('Recovered', 0, 0, self.scheduled({'Recovered':1}), False, 0)
- Parameters
state (str) – The state to be inserted
mean (Optional[int]) – The mean parameter of the Normal distribution
vary (Optional[int]) – The variance parameter of the Normal distribution
transition_fn (Callable) – A function that encodes the states the agent can transition into from the current state
infected_state (bool) – Defines whether the state is infectious
proportion (Union[float, int]) – Initial proportion of the state
- Return type
None
- insert_state_custom(state, fn, transition_fn, infected_state, proportion)[source]
Inserts a state into the model and schedules the agent for this state using a custom distribution specified by the user-defined function. The user must specify one of the following functions for the transition function
The last two parameters define whether the state is an infected state and the initial proportion of agents belonging to this state.
1def fn1(current_time_step): 2 r=random.random() 3 if r<0.2: 4 return 2 5 elif r<0.8: 6 return 3 7 else: 8 return 4 9 10class UserModel(model.ScheduledModel): 11 def __init__(self): 12 model.ScheduledModel.__init__(self) 13 . 14 . 15 . 16 self.insert_state_custom('Recovered', fn1, self.scheduled({'Recovered':1}), False, 0)
- Parameters
state (str) – The state to be inserted
fn (Callable) – User-defined function that encodes the scheduled time step for transition
transition_fn (Callable) – A function that encodes the states an agent can transition into from the current state
infected_state (bool) – Defines whether the state is infectious
proportion (Union[float, int]) – Initial proportion of the state
- Return type
None
- initialize_states(agents)[source]
Initializes the states of the agents based on state proportions.
- Parameters
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Return type
None
- find_scheduled_time(state)[source]
Returns the scheduled time of transition for a state.
- Parameters
state (str) – The state for which the scheduled time is returned
- Returns
The scheduled time of transition for the state
- Return type
int
- find_next_state(agent, agents)[source]
Returns next state of the agent according to the transition functions between the states and the schedule time left.
- Parameters
agent (episimmer.agent.Agent) – The agent whose next state is to be determined
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
The new state of the agent, Schedule time left for the agent in current state
- Return type
Tuple[str, int]
- choose_one_state(state_dict)[source]
Returns a new state from state_dict according to its proportion in the state_dict.
- Parameters
state_dict (Dict[str, float]) – A dictionary mapping states to proportions of current state that can transition into
- Returns
A new state
- Return type
str
- full_scheduled(new_states, agent, agents)[source]
This function specifies that the state change is scheduled. Returns a new state from new_states and its scheduled time for an agent.
- Parameters
new_states (Dict[str, float]) – A dictionary mapping states to proportions an agent from the current state can transition to
agent (episimmer.agent.Agent) – An agent object
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
The new state of the agent and its scheduled time
- Return type
Tuple[str, int]
- scheduled(new_states)[source]
This function can be used by the user in
UserModel.pyto specify an independent transition. The transition will occur based on a calculated scheduled time of stay for the state. The only parameter required here is a dictionary mapping new states to transition proportions. Returns a partial function offull_scheduled().1class UserModel(model.ScheduledModel): 2 def __init__(self): 3 model.ScheduledModel.__init__(self) 4 . 5 . 6 . 7 self.insert_state('Infected', 6, 3, self.scheduled({'Recovered':1}), True, 0.01)
- Parameters
new_states (Dict[str, float]) – A dictionary mapping states to proportions an agent from the current state can transition to
- Returns
A partial function of
full_scheduled()- Return type
Callable
- full_p_infection(new_states, fn, p_infected_states_list, agent, agents)[source]
This function returns a new state from new_states and its scheduled time based on all types of interaction between the current agent and other agents.
- Parameters
new_states (Dict[str, float]) – A dictionary mapping states to proportions an agent from the current state can transition to
fn (Callable) – User-defined function defining the probability of infection based on individual/probabilistic interactions
p_infected_states_list (Optional[List[float]]) – List of probabilities that can be used in the user-defined function fn
agent (episimmer.agent.Agent) – Current agent object
agents (Dict[str, episimmer.agent.Agent]) – A dictionary mapping from agent indices to agent objects
- Returns
The new state of the agent and its scheduled time
- Return type
Tuple[str, int]
- p_infection(new_states, fn=None, p_infected_states_list=None)[source]
This function can be used by the user in
UserModel.pyto specify a dependent transition. The transition probability is based on all the types of interactions between the agents. It takes three parameters. The first is a dictionary mapping new states to transition proportions. The next two parameters are the same parameters as in the p_infection function of the Stochastic Model. The fn parameter defines the user-defined function for probability of infection from individual and probabilistic interactions and the p_infected_states_list parameter is a list of float probabilities that can be used in the user-defined function. The parameters to be passed in the user-defined function are p_infected_states_list, contact_agent, c_dict and current_time_step. p_infected_states_list is the above-mentioned list of probabilities, contact_agent is the agent that the current agent is in contact with (through an individual or probabilistic interaction), c_dict is a dictionary defining the interaction and current_time_step is the current time step. If you do not have any of these types of interactions, you need not pass the second and third parameters. Returns a partial function offull_p_infection().1class UserModel(model.ScheduledModel): 2 def __init__(self): 3 model.ScheduledModel.__init__(self) 4 self.insert_state('Susceptible',None, None,self.p_infection({'Infected':1}),False,0.99)
In case of individual or probabilistic interactions, a callable function must be passed. A list can also be passed optionally. In the following example, we use the list.
1def probability_of_infection_fn(p_infected_states_list, contact_agent ,c_dict, current_time_step): 2 if contact_agent.state=='Infected': 3 return p_infected_states_list[0] 4 return 0 5 6class UserModel(model.ScheduledModel): 7 def __init__(self): 8 model.ScheduledModel.__init__(self) 9 self.insert_state('Susceptible', None, None, self.p_infection({'Infected':1}, probability_of_infection_fn, [0.1]), False, 0.99)
- Parameters
new_states (Dict[str, float]) – A dictionary mapping states to proportions an agent from the current state can transition to
fn (Optional[Callable]) – User-defined function defining the probability of infection based on individual/probabilistic interactions
p_infected_states_list (Optional[List[float]]) – List of probabilities that can be used in the user-defined function fn
- Returns
A partial function of
full_p_infection()- Return type
Callable