analyzers
Analyzers, which record more than a sim records by default, as it runs
For example, the number infected by age or by sex, or who infected whom (ss.infection_log).
Used with: agents and compartments; declared and coded modules
Classes
| Name | Description |
|---|---|
| Analyzer | Base class for analyzers, which record more about a sim than it records by default: for |
| infection_log | Log who infected whom – see ss.diseases.InfectionLog for detail |
Analyzer
analyzers.Analyzer(name=None, label=None, **kwargs)Base class for analyzers, which record more about a sim than it records by default: for example, a state of the people on each step, read before the next step changes it.
An analyzer records results as any module does: those about the state at the start of each step (stocks) in record_states(), and those about what happened in it (flows) in record_events() (both with self.ti the step’s, as in every module). Its step(), if it has one, is called at the end of each step, after the flows are recorded, when the people are as they are at the next time point.
Examples
class BySex(ss.Analyzer):
def init_results(self):
super().init_results()
self.define_results(ss.Result('infected', columns=['female', 'male'], kind='stock'))
def record_states(self):
infected, female = self.sim.diseases.sis.I, self.sim.people.female
self.results.infected[self.ti] = [np.count_nonzero(infected & female), np.count_nonzero(infected & ~female)]An analyzer that reads who infected whom sets needs_infection_log = True: each disease then keeps its infection log (disease.infection_log, see ss.infection_log), as with an ss.infection_log in the sim.
Methods
| Name | Description |
|---|---|
| step | Nothing by default: an analyzer usually only records results (see record_states() and record_events()) |
step
analyzers.Analyzer.step()Nothing by default: an analyzer usually only records results (see record_states() and record_events())
infection_log
analyzers.infection_log(**kwargs)Log who infected whom – see ss.diseases.InfectionLog for detail
This analyzer turns on the infection log of each disease, rather than recording anything itself, since who infected whom is known only inside the disease’s transmit() step.
Each entry has the time (t), the person infected (target), who infected them (source), and the route (network). The source is known only for transmission over a network, where each infection is drawn along an edge: seeds have a source of -1, and infections by a mixing pool (ss.MixingPool), a contact matrix, or mixing with everyone (no network) have a source and network of NaN, since they come from the fraction infectious in a group rather than from a particular person (by events, solver=ss.solvers.abm(step='events'), each contact is with a particular person, so these have a source and route too). For a disease with strains (ss.Strains), each entry also has the strain transmitted (strain; for seeds, the strains they start with). As agents only.
The log stays on the disease (sim.diseases.sir.infection_log) as well, so several analyzers can read it, during the run or after. To follow transmission as the sim runs, an analyzer reads the entries of each step with disease.infection_log.to_df(ti=self.ti) (see ss.diseases.InfectionLog.to_df()).
Note: CRN safe.
Examples
import starsim as ss
sim = ss.Sim(n_agents=1000, diseases='sir', networks='random', analyzers='infection_log')
sim.run()
sim.analyzers[0].plot()Attributes
| Name | Description |
|---|---|
| log | The infection log, if there is only one disease |
Methods
| Name | Description |
|---|---|
| animate | Animate the infection log – mostly for amusement! |
| finalize_results | Collect the infection logs from each of the diseases (the same logs, so several analyzers can share them) |
| plot | Plot all of the infection logs |
| to_df | Convert the infection log to a dataframe, if there is only one disease |
animate
analyzers.infection_log.animate(
key=0,
framerate=10,
clear=False,
cmap='parula',
**kwargs,
)Animate the infection log – mostly for amusement!
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| key | int / str |
which disease to animate the infection log of | 0 |
| framerate | float |
how many frames per second to display | 10 |
| clear | bool |
whether to clear the frame on each step | False |
| cmap | str |
the colormap to use | 'parula' |
| kwargs | dict |
passed to ss.plot_args() |
finalize_results
analyzers.infection_log.finalize_results()Collect the infection logs from each of the diseases (the same logs, so several analyzers can share them)
plot
analyzers.infection_log.plot(**kwargs)Plot all of the infection logs
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| kwargs | dict |
passed to ss.plot_args(), for the figure and plot options |
to_df
analyzers.infection_log.to_df()Convert the infection log to a dataframe, if there is only one disease