Comprehensive Analyzer Plots Example¶
This notebook demonstrates all available plotting methods in the DwtAnalyzer class from the tbsim package. It is adapted from the original Python script for interactive exploration.
In [1]:
Copied!
import tbsim
import starsim as ss
import matplotlib.pyplot as plt
import tbsim
import starsim as ss
import matplotlib.pyplot as plt
Build a TB simulation with dwell time analyzer¶
In [2]:
Copied!
def build_tbsim(sim_pars=None):
sim_params = dict(
start = ss.date('2013-01-01'),
stop = ss.date('2016-12-31'),
rand_seed=123,
dt=ss.days(7),
)
if sim_pars is not None:
sim_params.update(sim_pars)
pop = ss.People(n_agents=1000)
tb_params = dict(
beta=ss.peryear(0.025),
init_prev=ss.bernoulli(p=0.25),
rel_sus_latentslow=0.2,
)
tb = tbsim.TB_EMOD(tb_params)
net = ss.RandomNet(dict(n_contacts=ss.poisson(lam=5), dur=0))
dwell_analyzer = tbsim.DwtAnalyzer(scenario_name='comprehensive_plots_example')
sim = ss.Sim(
people=pop,
networks=net,
diseases=tb,
pars=sim_params,
analyzers=dwell_analyzer,
)
sim.pars.verbose = 0
return sim
def build_tbsim(sim_pars=None):
sim_params = dict(
start = ss.date('2013-01-01'),
stop = ss.date('2016-12-31'),
rand_seed=123,
dt=ss.days(7),
)
if sim_pars is not None:
sim_params.update(sim_pars)
pop = ss.People(n_agents=1000)
tb_params = dict(
beta=ss.peryear(0.025),
init_prev=ss.bernoulli(p=0.25),
rel_sus_latentslow=0.2,
)
tb = tbsim.TB_EMOD(tb_params)
net = ss.RandomNet(dict(n_contacts=ss.poisson(lam=5), dur=0))
dwell_analyzer = tbsim.DwtAnalyzer(scenario_name='comprehensive_plots_example')
sim = ss.Sim(
people=pop,
networks=net,
diseases=tb,
pars=sim_params,
analyzers=dwell_analyzer,
)
sim.pars.verbose = 0
return sim
Run the simulation and extract the analyzer¶
In [3]:
Copied!
print("Building and running TB simulation...")
sim_tb = build_tbsim()
sim_tb.run()
analyzer = sim_tb.analyzers[0]
print("Building and running TB simulation...")
sim_tb = build_tbsim()
sim_tb.run()
analyzer = sim_tb.analyzers[0]
Building and running TB simulation...
===> Dwell time logs saved to: /home/runner/work/tbsim/tbsim/docs/tutorials/results/comprehensive_plots_example-0224224036.csv
1. Network Graph¶
Directed graph of state transitions.
In [4]:
Copied!
analyzer.plot('network')
analyzer.plot('network')
<Figure size 1500x1000 with 0 Axes>
2. Histograms¶
Histograms with KDE of dwell time distributions per state.
In [5]:
Copied!
analyzer.plot('histogram')
analyzer.plot('histogram')
3. Survival Analysis¶
Kaplan-Meier survival curves for dwell times.
In [6]:
Copied!
analyzer.plot('kaplan_meier')
analyzer.plot('kaplan_meier')
4. Validation¶
Observed dwell-time histograms for validation.
In [7]:
Copied!
analyzer.plot('validation')
analyzer.plot('validation')
<Figure size 1500x1000 with 0 Axes>
5. Loading from file¶
You can also create a DwellTime instance from a saved CSV file.
In [8]:
Copied!
from tbsim.analyzers import DwellTime
file_path = analyzer.file_path
print(f'Generated data file: {file_path}')
plotter = DwellTime(file_path=file_path)
plotter.plot('histogram')
from tbsim.analyzers import DwellTime
file_path = analyzer.file_path
print(f'Generated data file: {file_path}')
plotter = DwellTime(file_path=file_path)
plotter.plot('histogram')
Generated data file: /home/runner/work/tbsim/tbsim/docs/tutorials/results/comprehensive_plots_example-0224224036.csv