# SEIR model example
\begin{align}
\dot{S} & = S_f - \alpha\beta\frac{SI}{N+k_I I+k_R R} + r_{R\_S} R\\
\dot{E} & = E_f + \alpha\beta\frac{SI}{N+k_I I+k_R R} - E\frac{1}{t_{E\_I}} \\
\dot{I} & = E\frac{1}{t_{E\_I}} - I\frac{1}{t_{I\_R}} \\
\dot{R} & = I\frac{1}{t_{I\_R}} - r_{I\_R} R\\
\end{align}
Where:  
* $S:$ Susceptible
* $E:$ Exposed
* $I:$ Infectious
* $R:$ Removed
* $\alpha:$ Mobilty
* $\beta:$ Infection rate
* $N:$ Total population
* $t_{E\_I}:$ # Transition time between exposed and infectious
* $t_{I\_R}:$ # Transition time between infectious and recovered
* $r_{R\_S}:$ Immunity loss rate ($\frac{1}{t_{R\_S}}$)  
* $S_f,E_f,I_f,R_f:$ External flux
* $k_I:$ Infected saturation   
* $k_R:$ Immunity shield    



```python
# Util libraries
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
# Adding lib paths
import sys
sys.path.insert(1, '../src2/models/')

# cv19 libraries
from SEIR import SEIR
import toml
import copy
```


```python
# For pop-up plots execute this code (optional)
import platform
OS = platform.system()

if OS == 'Linux':    
    %matplotlib tk
    print('Linux')
elif OS == 'Windows':
    %matplotlib qt
    print('Windows')
elif OS == 'Darwin':
    %matplotlib tk
    print('Mac (Funciona?)')
```

    Linux



```python

```


```python
# Input configuration file
config = 'cfg/SEIR.toml'
```


```python
cfg = toml.load(config)
```

# Beta

## Beginning


```python
var = [0.8,0.9,1,1.1,1.2]
simcfg = [copy.deepcopy(cfg) for i in var]
```


```python
for i in range(len(simcfg)):
    simcfg[i]['parameters']['dynamic']['beta']=cfg['parameters']['dynamic']['beta']*var[i]
```


```python
%%capture
model = SEIR(config=config)
model.integrate()
peak = max(model.I)
```


```python
# Build simulation object
sim = []
for i in simcfg:
    sim.append(SEIR(config = i))
```

    No external data added
    SEIR object created
    No external data added
    SEIR object created
    No external data added
    SEIR object created
    No external data added
    SEIR object created
    No external data added
    SEIR object created



```python
%%capture
# Simulate (solve ODE)
for i in sim:
    i.integrate()
```


```python
for i in range(len(sim)):
    plt.plot(sim[i].t,sim[i].I/peak,label='Beta: '+str(int(var[i]*100))+'%')
plt.xlim(0,300)
plt.legend(loc=0)
plt.title('Beta')
plt.ylabel('Infectious')
plt.xlabel('day')
plt.show()
```

## During Simulation


```python
var2 = [0.8,0.9,1,1.1,1.2]
simcfg2 = [copy.deepcopy(cfg) for i in var2]
```


```python
alpha = []
for i in var2:
    alpha.append('{"function":"Events","values":[1,'+str(i)+'],"days":[[0,30],[30,500]]}')
```


```python
for i in range(len(simcfg2)):
    simcfg2[i]['parameters']['dynamic']['alpha']=alpha[i]
```


```python
%%capture
# Build simulation object
sim2 = []
for i in simcfg2:
    sim2.append(SEIR(config = i))
```


```python
%%capture
# Simulate (solve ODE)
for i in sim2:
    i.integrate()
```


```python
for i in range(len(sim2)):
    plt.plot(sim2[i].t,sim2[i].I/peak,label='Beta: '+str(int(var2[i]*100))+'%')
plt.xlim(0,300)
plt.legend(loc=0)
plt.title('Beta')
plt.ylabel('Infectious')
plt.xlabel('day')
plt.axvline(20,linestyle='dashed',color='grey')
plt.show()
```

# Population

## Beginning


```python
population = cfg['initialconditions']['population']
```


```python
var3 = [0.8,0.9,1,1.1,1.2]
simcfg3 = [copy.deepcopy(cfg) for i in var3]
```


```python
for i in range(len(simcfg3)):
    simcfg3[i]['initialconditions']['population']=population*var3[i]
```


```python
%%capture
# Build simulation object
sim3 = []
for i in simcfg3:
    sim3.append(SEIR(config = i))
```


```python
%%capture
# Simulate (solve ODE)
for i in sim3:
    i.integrate()
```


```python
sim3
```




    [<SEIR.SEIR at 0x7f874cf8ae80>,
     <SEIR.SEIR at 0x7f874cffebb0>,
     <SEIR.SEIR at 0x7f874cffeee0>,
     <SEIR.SEIR at 0x7f874cffec10>,
     <SEIR.SEIR at 0x7f874cffe100>]




```python
for i in range(len(sim3)):
    plt.plot(sim3[i].t,sim3[i].I/peak,label='Population: '+str(int(var3[i]*100))+'%')
plt.xlim(0,300)
plt.legend(loc=0)
plt.title('Population')
plt.ylabel('Infectious')
plt.xlabel('day')
plt.show()
```


```python

```

## During Simulation


```python
var4 = [-0.03,-0.015,0,0.015,0.03]
```


```python
S_f = []
for i in var4:
    S_f.append('{"function":"Events","values":['+str(population*i)+'],"days":[[20,25]]}')
```


```python
simcfg4 = [copy.deepcopy(cfg) for i in var4]
for i in range(len(simcfg4)):
    simcfg4[i]['parameters']['dynamic']['S_f']=S_f[i]
```


```python
# Build simulation object
sim4 = []
for i in simcfg4:
    sim4.append(SEIR(config = i))
```

    Executing Events
    No external data added
    SEIR object created
    Executing Events
    No external data added
    SEIR object created
    Executing Events
    No external data added
    SEIR object created
    Executing Events
    No external data added
    SEIR object created
    Executing Events
    No external data added
    SEIR object created



```python
%%capture
# Simulate (solve ODE)
for i in sim4:
    i.integrate()
```


```python
for i in range(len(sim)):
    plt.plot(sim4[i].t,sim4[i].I/peak,label='Population: '+str(int(var4[i]*100*10))+'%',linestyle='dashed')
plt.xlim(0,300)
plt.legend(loc=0)
plt.title('Population')
plt.ylabel('Infectious')
plt.xlabel('day')
plt.axvline(20,linestyle='dashed',color='grey')
plt.show()
```


```python

```

7dfc5495058cd885dbfee1f2ad31affabf5887a3
ipynb
Jupyter Notebook
Examples/SEIR-deepstudy.ipynb
DLab/covid19geomodeller
a3a9eedf064078b21be0928ee41b41c902938eff
MIT
Examples/SEIR-deepstudy.ipynb
DLab/covid19geomodeller
a3a9eedf064078b21be0928ee41b41c902938eff
MIT
Examples/SEIR-deepstudy.ipynb
DLab/covid19geomodeller
a3a9eedf064078b21be0928ee41b41c902938eff
MIT
Qwen/Qwen-72B
1. YES
2. YES