---
# System prepended metadata

title: Abstract Module
tags: [others]

---

# Abstract Module
###### tags: `others`

[TOC]

## Initializaion Module
- node attribute(person)
    - age(int)
    - group: $\{community_i, household_i, highschool_i, \ldots \}$(array of int)
        - group_day
        - group_night
    - state(char) + counter(int, cycle)
        - 1 day = 2 cycles(day&night)
    - location on day(int)
        - table lookup
    - location on night(int)
        - table lookup
- edge attribute(redundant)
    - weight
        - infectious probability(double)
- tables
    - location table

- population generating flow
    - load data
        - 鄉鎮地區list
            - age distribution
        - group structure
            - community, neighborhood, and so on
                - number of people each
        - worker info
            - % of worker with diff day and night?
            - distribution to assign location of day and night?
        - contact matrix
        - init infectors
            - number of people in each 鄉鎮市區
            - age?
    - for each鄉鎮市區
        - 建人數的node
        - 根據年齡分布給age
        - 根據contact group給group attribute
        - 根據contact matrix給contact group內的node建邊
    - 鄉鎮市區間的worker
        - day night不同的人的比例
        - day night分別的位置
    - assign state
        - initial infectors
        - suspected, otherwise

:::spoiler template
```python=
class _base_initialization_module:
    def __init__(self, fname):
        self.data = load_data()
        
        pass
        
    def load_data(self, fname):
        '''
        '''
        pass
    
    def build_graph(self):
        ''' Build the graph.
        Return: the graph
        '''
        G = nx.DiGraph()
        _assign_attr_in_each_tract(G)
        _assign_worker_edge_between_tract(G)
        _assign_initial_infectors(G)
        return G
    
    def _assign_attr_in_each_tract(self, G):
        '''
        - 建人數的node
        - 根據年齡分布給age
        - 根據contact group給group attribute
        - 根據contact matrix給contact group內的node建邊
        pseudo code:
        for tract in tracts:
            for contact_group in tract:
                K = nx.complete_graph(contact_group.population, nx.DiGraph())
                for age, ratio in tracts.ages:
                    age_population = contact_group.population*ratio
                    for _ in range(age_population):
                        K.nodes[cnt]['age'] = age
                        cnt += 1
        '''
        
        pass
    
    def _assign_worker_edge_between_tract(self, G):
        '''
        - % of worker with diff day and night?
        - distribution to assign location of day and night?
        '''
        pass
    
    def _assign_initial_infectors(self, G):
        pass
```
:::

## Simulation Module

- param
    - 初始的graph
- simulation
    - 天數
    - 必要的access function


### Pseudo code of simulation

#### Naive algorithm
```
for each day d:
    for each time period p:
        for each I individual u:
            for each contact group g of u at p:
                for each S v in g:
                    let x be trial among U(0, 1)
                    let p_uv be contact probability from u to v
                    if trial of x < p_uv:
                        update v
            
```

#### Improved algorithm

- 原理
    - naive algorithm
        - iterate all candidates
        - $u$ 傳給 $v$ 的機率為 $p_{uv}$
        - $O(N)$
    - Sieve algorithm
        - $u$ 傳給 $v$ 的機率為 被選為$K\times$判定成功
        - $\frac{K}{N}\times\frac{p_{uv}}{p_{max}}=p_{max}\times\frac{p_{uv}}{p_{max}}=p_{uv}$
        - 誤差在$K$要ceiling成整數
        - $O(K)=O(p_{max}*N)$
        - 需要維護$p_{max}$和$N$
            - how?
```python
for each day d:
    for each time period p:
        for each infectious individual u:
            for each contact group g of u at p:
                let p_max be max([p_uv for v be S in g])
                let N be size([v for v be S in g])
                let K be p_max*N
                choose K candidates among N
                for each v among K:
                    let x be a trail among U(0, 1)
                    let p_uv be contact probability from u to v
                    if trial of x < p_uv/p_max:
                        update v
```

## Visualization Module

Python Map Visualization libraries
* Plotly
* Folium

```python

```