Module#

class Module(name=None, label=None, unit=None, dt=None)[source]#

Bases: quickobj

The main base class for all Starsim modules: diseases, networks, interventions, etc.

Parameters:
  • name (str) – a short, key-like name for the module (e.g. “randomnet”)

  • label (str) – the full, human-readable name for the module (e.g. “Random network”)

  • unit (str) – the time unit (e.g. ‘day’, ‘year’); inherits from sim if not supplied

  • dt (float) – the timestep (e.g. 1.0, 0.1); inherits from sim if not supplied

Attributes

now

Return the current time, i.e. the time vector at the current timestep.

states

Return a flat list of all states

statesdict

Return a flat dictionary (objdict) of all states

Methods

__call__(*args, **kwargs)[source]#

Allow modules to be called like functions

disp(output=False)[source]#

Display the full object

set_metadata(name=None, label=None)[source]#

Set metadata for the module

set_time_pars(unit=None, dt=None)[source]#

Set time units for the module

define_pars(inherit=True, **kwargs)[source]#

Create or merge Pars objects

update_pars(pars, **kwargs)[source]#

Pull out recognized parameters, returning the rest

init_pre(sim, force=False)[source]#

Perform initialization steps

This method is called once, as part of initializing a Sim. Note: after initialization, initialized=False until init_vals() is called (which is after distributions are initialized).

init_results()[source]#

Initialize any results required; part of init_pre()

init_post()[source]#

Initialize the values of the states; the last step of initialization

init_time_pars(force=False)[source]#

Initialize all time parameters by ensuring all parameters are initialized; part of init_post()

property now#

Return the current time, i.e. the time vector at the current timestep

start_step()[source]#

Tasks to perform at the beginning of the step

step()[source]#

Define how the module updates over time – the key part of Starsim!!

finish_step()[source]#

Define what should happen at the end of the step; at minimum, increment ti

update_results()[source]#

Perform any results updates on each timestep

finalize()[source]#

Perform any final operations, such as removing unneeded data

finalize_results()[source]#

Finalize results

define_states(*args, check=True)[source]#

Define states of the module with the same attribute name as the state

Parameters:
  • args (states) – list of states to add

  • check (bool) – whether to check that the object being added is a state

define_results(*args, check=True)[source]#

Add results to the module

property states#

Return a flat list of all states

The base class returns all states that are contained in top-level attributes of the Module. If a Module stores states in a non-standard location (e.g., within a list of states, or otherwise in some other nested structure - perhaps due to supporting features like multiple genotypes) then the Module should overload this attribute to ensure that all states appear in here.

property statesdict#

Return a flat dictionary (objdict) of all states

Note that name collisions may affect the output of this function

classmethod create(name, *args, **kwargs)[source]#

Create a module instance by name

Parameters:

name (str) – A string with the name of the module class in lower case, e.g. ‘sir’

classmethod from_func(func)[source]#

Create an module from a function

to_json()[source]#

Export to a JSON-compatible format

plot()[source]#

Plot all results in the module