Module#

class Module(name=None, label=None, **kwargs)[source]#

Bases: Base

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”)

  • kwargs (dict) – passed to ss.Time() (e.g. start, stop, unit, dt)

Attributes

now

Shortcut to self.t.now()

states

Return a flat list of all states

statesdict

Return a flat dictionary (objdict) of all states

ti

Get the current module timestep

timevec

Shortcut to self.t.timevec

Methods

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

Allow modules to be called like functions

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

Set metadata 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(force=False)[source]#

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

match_time_inds(inds=None)[source]#

Find the nearest matching sim time indices for the current module

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