Nepidemix学习笔记1
admin
2023-07-31 01:49:17
0

转载请注明原出处,谢谢。

标签(空格分隔): Nepidemix python dynamic_simulation

Nepidemix官网

什么是Nepidemix?

NepidemiX是仿真复杂网络动力学过程(process)的python工具包.可以用来仿真SIS,SIR等经典传染病传播模型。NepidemiX通过一系列配置文件(.ini)来仿真传染病传播动力学过程。配置文件由若干个section组成。


Nepidemix基本概念

ini files
NepidemiX scripts使用.ini文件配置。配置文件由很多section组成,每个section的名字是[]括起。具体每个section是做什么的,下面有介绍。
The command line
NepidemiX scripts是终端程序。没有图形界面,用命令行的方式执行
attribute
属性有name和value。
当跑一个仿真过程时候,我们会修改属性并关注属性的变化。
一个属性可以是年龄,HIV 状态或者性别。一个节点可以有一个或者更多的属性。
state
点/边的状态:所有属性值的字典/向量。举个例子说:一个点N的属性name是status. status有三个value:S,I,R
点/边如果有很多属性,
举例来说:N还有一个属性是gender,属性value是male或者female.这样点N是有六个状态,(S+female, I+female, R+female, S+male, I+male, R+male)
partial state
在选点/边的时候会用到这个概率。
举例:gender = male. 结果就会匹配到 S+male, I+male, R+male.


安装

下载链接
NepdidemiX是用python写的,使用大多数第三方函数(NetworkX和numpy)所以使用NepidemiX之前要安装python,NetworkX和numpy。
下载NepidemiX的安装包之后,在解压文件夹上输入下面命令就安装成功了。

>>python setup.py install

测试是否安装成功:
在python shell下输入(或者cmd python):

>>import nepidemix

如果没报错就说明安装顺利啦…这个工具包比networkx安装简单多了=。=
测试命令,打开cmd,进入nepidmix_runsimulation存储路径,即YourDir\\NepidemiX-0.2\\scripts,输入命令:

>nepidmix_runsimulation

发现错误,此命令未定义。经过分析发现python是识别.py结尾的文件。于是修改此命令的后缀,改为nepidmix_runsimulation.py

>nepidmix_runsimulation.py

这回就成功了。

安装成功之后,创建一个文件夹。在新建文件夹中创建conf和output两个文件夹。
conf存放配置文件。output存放仿真结果。


跑一个仿真程序看看

用nepidemix_runsimulation来配置python 程序。

SIS仿真的配置文件

用仿真配置文件(simulation configuration file)来跑SIS过程。

  1. Process定义函数(类似声明文件)
    创建为SIS_process_def.ini文件.用来存储基本信息。
  2. 具体仿真过程在SIS_example.ini写。
    仿真文件由若干section组成:
    > * [Simulation]
    > * [NetworkParameters]
    > * [Process parameters]
    > * [NodeStateDistribution]
    > * [Output]

[simulation]
存放仿真需要的基本信息
iteration 迭代次数
process_class 负责决定network上跑的process.(下面有讲怎么写一个process)
network_func 负责产生network

network_fuc = barabasi_albert_graph_network  #产生BA preferential attachment 算法的网络
network_fuc = grid_2d_graph_networkx #产生规则栈格网络;
network_fuc = fast_gnp_random_graph_networkx #产生ER随机网络
network_fuc = connected_watts_strogatz_graph_networkx #产生小世界WS网络
network_fuc = load_network # 从文件中load进去一个网络

[NetworkParameters]
在这个section指定network_fuc 的输入参数。
以BA preferntial attachment为例,需要两个参数:

  1. n: 网络节点数
  2. m: 一个点引入网络时候,这个点会连多少边。

[process_parameters section]
这个section负责指定两个问题
1. 指定哪个process script仿真会使用
file参数指定process定义文件名称。

file = SIS_process_def.ini
  1. 定义仿真process参数
    在仿真process模型中使用的参数,在这里定义。比如SIS模型中的δ,β。

[State distribution section]
这个section负责网络中节点状态的分布。比如S状态的点有95%,I状态的点有5%

[output section]
这个setction负责输出配置。

最后在YourDir\\NepidemiX-0.2\\scripts\\
文件夹下需要有三个文件,一个文件夹。
nepidemix_runsimulation.pySIS_example.iniSIS_process_def.ini还有output文件夹

这样想跑一下程序 只需要在cmd相应路径下 nepidemix_runsimulation.py SIS_example.ini

结果保存在output 文件夹

SIS_example.ini文件源代码

# This is the simulation section.
[Simulation]
# Run the simulation this many iterations.
iterations = 500

# The time step taken each iteration.
dt = .1
# This is the name of the process object.
process_class = ScriptedProcess

# This is the name of the network generation function.
network_func = barabasi_albert_graph_networkx

# Network settings.
[NetworkParameters]
# Number of nodes.
n = 1000
# Number of edges to add in each iteration.
m = 2

# Defining the process parameter values.
# The contents of this section is dependent on
# the parameters of the process class as specified by
# the option process_class in the Simulation section.
[ProcessParameters]
# File name of the process description.
file = SIS_process_def.ini
# Infection rate.
beta = .9e-2
# Death rate.
delta = 0.0076

# The fraction of nodes, alternatively the number of nodes, that will be  assigned to each state initially.
# The state names must match those specified by the network process class.
[NodeStateDistribution]
# 95% S
{status:S} = 0.95
# 5% I
{status:I} = 0.05

# Result output settings.
[Output]

# Output directory:
output_dir = YourDir\\NepidemiX-0.2\\scripts\\

# This is the base name of all files generated by the run.
base_name = test_SIS

# If unique is defined as true, yes, 1, or on, unique file names will be created (time stamp added)
unique = yes

# If this is true, yes, 1, or on, a copy of the full program config, plus an Info
# section will be saved.
save_config = yes

# If this is true/yes/on, the network node states will be counted and saved as a csv file.
# Default value True.
# Note only valid if the current process support updates. If not nothing will be saved.
save_state_count = yes

# Count nodes every ... iterations. Value should be integer >= 1.
# Default value 1.
save_state_count_interval = 1

# If this is true, yes, 1, or on, a copy of the network will be saved.
# Save interval may be set using the save_network_interval key.
save_network = yes

# This control how often the network will be saved.
# A value <= 0 means only the initial network will be saved. A positive value
# n> 0, results in the initial network being saved plus every n:th iteration
# thereafter, as well as the last network.
# Default value 0.
save_network_interval = 0

相关内容

热门资讯

Mobi、epub格式电子书如... 在wps里全局设置里有一个文件关联,打开,勾选电子书文件选项就可以了。
定时清理删除C:\Progra... C:\Program Files (x86)下面很多scoped_dir开头的文件夹 写个批处理 定...
scoped_dir32_70... 一台虚拟机C盘总是莫名奇妙的空间用完,导致很多软件没法再运行。经过仔细检查发现是C:\Program...
500 行 Python 代码... 语法分析器描述了一个句子的语法结构,用来帮助其他的应用进行推理。自然语言引入了很多意外的歧义,以我们...
小程序支付时提示:appid和... [Q]小程序支付时提示:appid和mch_id不匹配 [A]小程序和微信支付没有进行关联,访问“小...
pycparser 是一个用... `pycparser` 是一个用 Python 编写的 C 语言解析器。它可以用来解析 C 代码并构...
微信小程序使用slider实现... 众所周知哈,微信小程序里面的音频播放是没有进度条的,但最近有个项目呢,客户要求音频要有进度条控制,所...
65536是2的几次方 计算2... 65536是2的16次方:65536=2⁶ 65536是256的2次方:65536=256 6553...
Apache Doris 2.... 亲爱的社区小伙伴们,我们很高兴地向大家宣布,Apache Doris 2.0.0 版本已于...
项目管理和工程管理的区别 项目管理 项目管理,顾名思义就是专注于开发和完成项目的管理,以实现目标并满足成功标准和项目要求。 工...