ProcessSequence
: A process built from other processes
- class epydemic.ProcessSequence(ps)
Bases:
Process
A process built from a sequence of other processes. This allows separate process behaviour to be defined independently and then combined in different ways.
The processes can be defined either by a list or a dict. The former generates anonymous processes, which is usually fine; the latter gives each component process a name by which it can be retrieved by other processes. This allows more complex interactions between processes.
Once created, the processes within a process sequence can’t be changed.
If you need to use non-anonymous process sequences, it’s often better to define a sub-class to manage all the names, since the component processes will almost certainly make assumptions about the existence of the processes they need to interact closely with, under a specific name.
Building the process
The process sequence essentially just composes the methods of the individual component processes at each phase of the process’ lifecycle.
- ProcessSequence.reset()
Reset the processes.
- ProcessSequence.build(params)
Build the proceses.
- Parameters:
params (
Dict
[str
,Any
]) – the experimental parameters
- ProcessSequence.setUp(params)
Set up the proceses.
- Parameters:
params (
Dict
[str
,Any
]) – the experimental parameters
- ProcessSequence.tearDown()
Tear down the processes.
- ProcessSequence.results()
Return all the experimental results from all the processes. The dict is created in process order, meaning that later processes may alter or overwrite the results of earlier ones, accidentally or deliberately.
- Return type:
Dict
[str
,Any
]- Returns:
a dict of experimental results
Equilibrium and termination
The default implementations work on timeouts respected across all the component processes. They can be re-defined to provide different equilibrium detection overall.
- ProcessSequence.atEquilibrium(t)
Test for equilibrium. A process sequence is at equilibrium if and when all its component processes are.
- Parameters:
t (
float
) – the simulation time- Returns:
True if all the processes are at equilibrium
- ProcessSequence.setMaximumTime(t)
Set the maximum default simulation time for all processes
- Parameters:
t (
float
) – the maximum simulation time
- ProcessSequence.maximumTime()
Return the maximum assumed simulation time, which is the maximum of all component processes.
- Return type:
float
- Returns:
the maximum simulation time
Anonymous and non-anonymous sequences
Process sequences are built from smaller component processes passed in the constructor. In most cases these processes are anonymous and constructed from a list, and there’s no way to refer to each component process individually. This is the best way as it maximises the independence of process code.
Note
Versions of epydemic
prior to version 1.9.1 had only anonymous
process sequences.
There are however some complicated cases where one component needs access to another. A good example of this is combining addition-deletion and disease processes, where the addition operation needs to affect the compartment of new nodes. In that case, one process needs to be able to access the methods on another.
A non-anonymous process sequence is created using a dict from
component process names (strings) to component processes. There are no
default names, for maximum flexibility. One component process can then
refer to another by using its name, by first using
Process.container()
to acquire its container and then looking-up
the required component process.
- ProcessSequence.processes()
Return a list of component processes.
- Return type:
List
[Process
]- Returns:
a list of processes
- ProcessSequence.processNames()
Return a list of component process names. This will be None for anonymous processes.
- Return type:
List
[str
]- Returns:
a list of keys or None
- ProcessSequence.get(n, v=None)
Return the named component process by name, or the default value if there is no such process. An exception is raised if the process sequence is anonymous.
- ProcessSequence.__getitem__(n)
Retrieve a process by name. An exception is raised if the process sequence is anonymous or if the named process doesn’t exist.
- Parameters:
n (
str
) – the process name- Return type:
- Returns:
the process
- ProcessSequence.allProcesses()
Return a list of all processes in the sequence. This will recurse into any processes that are themselces sequences.
- Return type:
List
[Process
]- Returns:
a list of processes
Note
See the cookbook recipe An epidemic over a changing population for an extended example of using process sequences in different ways.