In [1]:
import complexism as cx
In [2]:
import numpy as np
import numpy.random as rd
from complexism.element import StepTicker, Event
from complexism.agentbased import ActiveBehaviour, PassiveBehaviour
import complexism.agentbased.statespace as ss
from complexism.agentbased.statespace.be import StateEnterTrigger, ActiveModBehaviour
import epidag.data as dat
import pandas as pd
In [3]:
mat = pd.read_csv('data/SimFM.csv')
ds = cx.DemographySex()
ds.load_birth_data(mat, i_year='Year', i_f='BirthF', i_m='BirthM')
ds.load_death_data(mat, i_year='Year', i_f='DeathF', i_m='DeathM')
ds.load_migration_data(mat, i_year='Year', i_f='MigrationF', i_m='MigrationM')
ds.load_population_data(mat, i_year='Year', i_f='PopF', i_m='PopM')
ds.complete_loading()
ds
Out[3]:
In [9]:
psc = """
PCore pSLIR {
beta = 0.4
lambda = 0.2
gamma = 0.05
Recov ~ exp(gamma)
Die ~ exp(0.02)
}
"""
dsc = """
CTBN dIR {
life[Alive | Dead]
ir[I | R]
Alive{life:Alive}
Dead{life:Dead}
Inf{life:Alive, ir:I}
Rec{life:Alive, ir:R}
Die -> Dead # from transition Die to state Dead by distribution Die
Inf -- Recov -> Rec
Alive -- Die # from state Alive to transition Die
}
"""
In [10]:
def SL_ODE(y, t, p, x):
life = x['life']
fs, ms, fl, ml = y
brs = life.get_birth_rate(t)
mrs = life.get_migration_rate(t)
drs = life.get_death_rate(t)
return [
fs * (brs['Female'] - drs['Female'] + mrs['Female'] - p['lambda']),
ms * (brs['Male'] - drs['Male'] + mrs['Male'] - p['lambda']),
fl * (brs['Female'] - drs['Female'] + mrs['Female']) + fs * p['lambda'],
ml * (brs['Male'] - drs['Male'] + mrs['Male']) + ms * p['lambda']
]
def measure(y, t, p, x):
return {
'Sus': y[0] + y[1],
'Lat': y[2] + y[3]
}
In [11]:
ctrl = cx.Director()
ctrl.read_bayes_net(psc)
ctrl.read_state_space_model(dsc)
In [12]:
bp = ctrl.new_sim_model('IR', 'StSpABM')
bp.set_agent('dIR')
bp.set_observations(states=['Inf', 'Rec', 'Dead'])
bp.add_behaviour('Life', 'Reincarnation', s_death='Dead', s_birth='Inf')
y0 = bp.get_y0_proto()
y0.define(n=1000, st='Inf')
lyo = ctrl.new_sim_model('MIR', 'MultiModel')
lyo.add_entry('M', 'IR', y0, fr=1, to=3)
lyo.add_summary('#M', 'Inf', 'Inf')
lyo.add_summary('#M', 'Rec', 'Rec')
bp = ctrl.new_sim_model('SL', 'ODEEBM')
bp.set_fn_ode(SL_ODE, ['FS', 'MS', 'FL', 'ML'])
bp.set_external_variables({'life': ds})
bp.set_dt(dt=0.1, odt=1)
bp.add_observing_function(measure)
lyo = ctrl.new_sim_model('SLIR', 'MultiModel')
y0 = bp.get_y0_proto()
ns = {k: v.sum()/100 for k, v in ds.get_population(1990).items()}
y0.define(st='FS', n=ns['Female'])
y0.define(st='MS', n=ns['Male'])
lyo.add_entry('mir', 'MIR')
lyo.add_entry('sl', 'SL', y0)
lyo.set_observations()
In [13]:
model = ctrl.generate_model('M1', 'SLIR', bn='pSLIR')
y0 = ctrl.get_y0s('SLIR')
cx.start_counting()
out = cx.simulate(model, y0, 1990, 2035, 1, log=False)
cx.stop_counting()
In [18]:
cx.get_counting_results().dT
Out[18]:
In [1]:
import epidag as dag
import complexism as cx
import complexism.fn as cmd
In [2]:
psc = """
PCore pSIR {
beta = 0.4
gamma = 0.5
Infect ~ exp(beta)
Recov ~ exp(0.5)
Die ~ exp(0.02)
}
"""
dsc = """
CTBN SIR {
life[Alive | Dead]
sir[S | I | R]
Alive{life:Alive}
Dead{life:Dead}
Inf{life:Alive, sir:I}
Rec{life:Alive, sir:R}
Sus{life:Alive, sir:S}
Die -> Dead # from transition Die to state Dead by distribution Die
Sus -- Infect -> Inf
Inf -- Recov -> Rec
Alive -- Die # from state Alive to transition Die
}
"""
In [3]:
da = cx.Director()
da.read_bayes_net(psc)
da.read_state_space_model(dsc)
In [4]:
mbp = da.new_sim_model('SIR', 'StSpABM')
mbp.set_agent(prefix='Ag', group='agent', dynamics='SIR')
mbp.add_behaviour('Dead', 'Reincarnation', s_death='Dead', s_birth='Sus')
mbp.get_parameter_hierarchy(dc=da.get_state_space_model('SIR'))
Out[4]:
In [5]:
mbp.generate('sir', bn='pSIR', da=da)
Out[5]:
In [31]:
Out[31]:
In [29]:
pc.get_samplers()
Out[29]:
In [25]:
Out[25]:
Three types of agents are supported by PyComplexism
Building an agent-based model
In [3]:
model_blueprint = {
'agent': {
'prefix': 'agent',
'group': 'agent',
'exo': {},
'type': 'stsp',
'dynamics': 'SIR',
'default': {
'st': 'Sus'
}
},
'population': {
'networks': {}
},
'mods': {
}
}
In [4]:
model_blueprint = {
'agent': {
'prefix': 'Helen',
'group': 'agent',
'exo': {},
'type': 'stsp',
'dynamics': 'SIR',
'default': {
'st': 'Sus'
}
}
}
Name = 'M1'
Gene = sm.generate()
proto = Gene.get_prototype('agent')
stsp = dc.generate_model('agent', **proto.get_samplers())
ag = cx.StSpAgent('Helen', stsp['Sus'], proto.get_sibling('Helen'))
model = cx.SingleIndividualABM(Name, ag)
cx.simulate(model, None, 0, 10, 1)
Out[4]:
In [5]:
model_blueprint = {
'agent': {
'prefix': 'agent',
'group': 'agent',
'exo': {},
'type': 'stsp',
'dynamics': 'SIR',
'default': {
'st': 'Sus'
}
},
'population': {
'networks': {}
},
'mods': {
}
}
y0 = [
{'n': 95, 'attributes': {'st': 'Sus'}},
{'n': 5, 'attributes': {'st': 'Inf'}},
]
Name = 'M1'
Gene = sm.generate()
eve = cx.StSpBreeder('Ag ', 'agent', Gene, dc)
pop = cx.Population(eve)
# model = cx.SingleIndividualABM(Name, ag)
# cx.simulate(model, None, 0, 10, 1)
model = cx.AgentBasedModel(Name, Gene, pop)
model.add_observing_event(pop.Eve.DCore.Transitions['Infect'])
model.add_observing_event(pop.Eve.DCore.Transitions['Recov'])
model.add_observing_event(pop.Eve.DCore.Transitions['Die'])
def f(model, tab, ti):
tab['Sus'] = model.Population.count(st='Sus')
tab['Inf'] = model.Population.count(st='Inf')
tab['Rec'] = model.Population.count(st='Rec')
tab['Alive'] = model.Population.count(st='Alive')
tab['Dead'] = model.Population.count(st='Dead')
model.add_observing_function(f)
cx.simulate(model, y0, 0, 10, 1)
Out[5]:
In [8]:
y0 = [
{'n': 995, 'attributes': {'st': 'Sus'}},
{'n': 5, 'attributes': {'st': 'Inf'}},
]
Name = 'M1'
Gene = sm.generate()
eve = cx.StSpBreeder('Ag ', 'agent', Gene, dc)
pop = cx.Population(eve)
# model = cx.SingleIndividualABM(Name, ag)
# cx.simulate(model, None, 0, 10, 1)
model = cx.StSpAgentBasedModel(Name, Gene, pop)
for tr in ['Infect', 'Recov', 'Die']:
model.add_observing_transition(tr)
for st in ['Sus', 'Inf', 'Rec', 'Alive', 'Dead']:
model.add_observing_state(st)
cx.simulate(model, y0, 0, 10, 1)
Out[8]:
In [ ]:
sm1 = dag.as_simulation_core(bn)
sm1.deep_print()
In [ ]:
mc_bp = BlueprintABM('AB_abm', 'pAB', 'AB_mc')
mc_bp.set_observations(states=['N', 'A', 'B', 'AB'])
pc = pc_bp.get_simulation_model().sample_core()
dc = dc_bp.generate_model(pc=pc)
model = mc_bp.generate('A', pc=pc, dc=dc)
out = simulate(model, y0={'N': 500}, fr=0, to=10)
model.output(mid=False)
In [ ]:
par_script = """
PCore pSIR{
transmission_rate = 1.5/200
rec_rate ~ triangle(0.1, 0.2, 0.3)
beta ~ exp(transmission_rate)
gamma ~ exp(rec_rate)
Die ~ exp(0.2)
}
"""
dc_ctbn_script = '''
CTBN SIR_BN {
life[Alive | Dead]
sir[S | I | R]
Alive{life:Alive}
Dead{life:Dead}
Inf{life:Alive, sir:I}
Rec{life:Alive, sir:R}
Sus{life:Alive, sir:S}
Die -> Dead # from transition Die to state Dead by distribution Die
Sus -- Infect_Net(beta) -> Inf
Sus -- Infect(beta) -> Inf
Inf -- Recov(gamma) -> Rec
Alive -- Die # from state Alive to transition Die
}
'''
da = Director()
da.read_pc(par_script)
da.read_dc(dc_ctbn_script)
In [ ]:
pc = da.get_pc('pSIR')
pc = pc.sample_core()
pc.Locus
In [ ]:
cfd = da.new_mc('ABM_M2', 'ABM', tar_pc='pSIR', tar_dc='SIR_BN')
cfd.add_behaviour('cycle', be_type='Reincarnation', s_birth = 'Sus', s_death = 'Dead')
cfd.add_behaviour('foi', be_type='ComFDShock', s_src='Inf', t_tar = 'Infect', dt=0.5)
cfd.add_behaviour('step', be_type='TimeStep', ts=[0.5, 4], ys=[1, 0.1, 5], t_tar = 'Infect')
cfd.set_observations(states=['Sus', 'Inf', 'Rec'])
mod, out = da.simulate('ABM_M2', y0={'Sus': 95, 'Inf': 5}, fr=0 ,to=10, dt=.01)
out.plot()
plt.show()
In [ ]:
cfd = da.new_mc('ABM_M2', 'ABM', tar_pc='pSIR', tar_dc='SIR_BN')
cfd.add_network('IDU', 'BA', m=2)
cfd.add_behaviour('cycle', be_type='Reincarnation', s_birth = 'Sus', s_death = 'Dead')
cfd.add_behaviour('transmission', be_type='NetShock', s_src = 'Inf', t_tar = 'Infect_Net', net='IDU')
cfd.add_behaviour('foi', be_type='ForeignShock', t_tar = 'Infect')
cfd.set_observations(states=['Inf'], behaviours=['foi'])
cfd = da.new_mc('ABM_M5', 'ABM', tar_pc='pSIR', tar_dc='SIR_BN')
cfd.add_network('IDU', 'BA', m=5)
cfd.add_behaviour('cycle', be_type='Reincarnation', s_birth = 'Sus', s_death = 'Dead')
cfd.add_behaviour('transmission', be_type='NetShock', s_src = 'Inf', t_tar = 'Infect_Net', net='IDU')
cfd.add_behaviour('foi', be_type='ForeignShock', t_tar = 'Infect')
cfd.set_observations(states=['Inf'], behaviours=['foi'])
In [ ]:
lyo = da.new_layout('Lyo1')
lyo.add_entry('A', 'ABM_M2', {'Sus': 20, 'Inf': 10})
lyo.add_entry('B', 'ABM_M2', {'Sus': 20}, size=3)
lyo.add_entry('C', 'ABM_M5', {'Sus': 20, 'Inf': 10}, size=2)
lyo.add_relation('*@Inf', 'Lyo1@FOI')
lyo.add_relation('Lyo1@FOI', '*@foi')
lyo.set_observations(['B_2', 'B_1', '*'])
In [ ]:
mod, out = da.simulate('Lyo1', fr=0, to=10)
out.plot()
plt.show()
In [ ]:
pc, dc = generate_pc_dc(da.get_pc('pSIR'), da.get_dc('SIR_BN'))
mc = CoreODE(dc)
mc.Mods['FOI'] = InfectionFD('FOI', 'Infect', 'Inf')
mc.Mods['Dead'] = Reincarnation('Dead', s_death='Dead', s_birth='Sus')
model = ODEModel('SIR', mc, dt=0.01)
model.add_obs_transition('Infect')
model.add_obs_state('Alive')
model.add_obs_state('Inf')
model.add_obs_state('Sus')
model.add_obs_behaviour('Dead')
In [ ]:
cfd = da.new_mc('EBM', 'CoreODE', tar_pc='pSIR', tar_dc='SIR_BN')
cfd.add_behaviour('FOI', be_type='InfectionDD', s_src = 'Inf', t_tar = 'Infect')
cfd.add_behaviour('Dead', be_type='Reincarnation', s_death='Dead', s_birth='Sus')
cfd.set_arguments('dt', 0.5)
cfd.set_observations(states=['Sus', 'Inf', 'Rec', 'Alive'], behaviours=['FOI'])
In [ ]:
mod, out = da.simulate('EBM', y0={'Sus': 95, 'Inf': 5}, fr=0 ,to=10, dt=1)
#da.copy_model(mod_src=mod)
out.plot()
plt.show()
In [ ]:
out = simulate(model, y0={'Sus': 95, 'Inf': 5}, fr=0 ,to=10, dt=0.1)
out.plot()
plt.show()
In [5]:
class A:
def __init__(self, ls):
self.List = ls
def __iter__(self):
return iter(self.List)
In [6]:
a = A([1,2,3])
for i in a:
print(i)
In [14]:
import matplotlib.pyplot as plt
import complexism as cx
import complexism.agentbased.statespace as ss
import epidag as dag
import matplotlib.pyplot as plt
import complexism as cx
import complexism.agentbased.statespace as ss
import epidag as dag
model_name = 'M_SIR'
# Step 1 set a parameter core
bn = cx.read_bn_script(cx.load_txt('example/scripts/pSIR.txt'))
sm = dag.as_simulation_core(bn,
hie={'city': ['agent'],
'agent': ['Infect', 'Recov', 'Die']})
pc = sm.generate(model_name)
# Step 1- set dynamic cores as agents need
dbp = cx.read_dbp_script(cx.load_txt('example/scripts/SIR_BN.txt'))
# Step 2 define at least one type of agent
eve = ss.StSpBreeder('Ag', 'agent', pc, dbp)
pop = cx.Population(eve)
# Step 3 generate a model
model = cx.StSpAgentBasedModel(model_name, pc, pop)
# Step 4 add behaviours to the model
# ss.FDShock.decorate('FOI', model, s_src='Inf', t_tar='Infect')
# ss.FDShockFast.decorate('FOI', model=model, s_src='Inf', t_tar='Infect', dt=0.1)
pc.impulse({'beta': pc['beta']/300})
# ss.DDShock.decorate('FOI', model=model, s_src='Inf', t_tar='Infect')
ss.NetShock.decorate('FOI', model=model, s_src='Inf', t_tar='Infect', net='net')
# Step 5 decide outputs
for tr in ['Infect', 'Recov', 'Die']:
model.add_observing_transition(tr)
for st in ['Sus', 'Inf', 'Rec', 'Alive', 'Dead']:
model.add_observing_state(st)
model.add_observing_behaviour('FOI')
# Step 6 simulate
y0 = [
{'n': 290, 'attributes': {'st': 'Sus'}},
{'n': 10, 'attributes': {'st': 'Inf'}}
]
output = cx.simulate(model, y0, 0, 10, 1)
print(output)
# Step 7 inference, draw figures, and manage outputs
fig, axes = plt.subplots(nrows=3, ncols=1)
output[['Sus', 'Inf', 'Rec']].plot(ax=axes[0])
output[['Recov', 'Infect']].plot(ax=axes[1])
output[['FOI']].plot(ax=axes[2])
plt.show()