在静态网络上模拟连续时间阈值模型。
thresholdmodel的Python项目详细描述
在静态网络上模拟连续时间阈值模型 Gillespie的随机模拟算法(SSA)。网络可以是 定向和/或加权。在
与原始的离散时间模型相比 输入超过其各自的阈值后将不会翻转“下一个” “时间步长”,因为没有时间步长。相反,一个节点 超过阈值将进入警报状态 以$gamma=1$进入激活状态。在
安装
git clone https://github.com/benmaier/thresholdmodel.git pip install ./thresholdmodel
示例
在ER随机图上进行模拟。在
^{pr2}$美国石油学会
模拟
给定一个networkx图形对象G(可以是networkx.DiGraph, 以及initially_activated和thresholds的值, 像这样模拟
Thresh=ThreshModel(G,initially_activated,thresholds)t,a=Thresh.simulate()
t是一个numpy.ndarray,包含在哪个节点上的时间 激活发生了。a是一个numpy.ndarray,包含 相应时间的相对级联大小,单位为t。注意 整个过程被建模为一个Poisson过程,使得时间t 将以节点激活率gamma = 1.0为单位给出。如果 要模拟另一个节点的激活率,只需重新缩放 时间为t /= gamma。在
当模拟以^{tt13}开始时$ 标志,则是每次跳跃保存时激活节点的列表 ThreshModel.activated_nodes。在
t,a=Thresh.simulate(save_activated_nodes=True)print(Thresh.activated_nodes)
您可以使用相同的初始条件重复模拟,方法是 再次调用Thresh.simulate(),所有必需的东西都将 自动复位。在
设置初始激活节点
将节点3、5和8设置为初始激活。在
initially_activated=[3,5,8]# this could also be a numpy array
随机选择20%的节点进行初始激活。当 重新启动模拟,将选择相同的节点作为初始节点 条件。在
initially_activated=0.2
选择35个随机选择的节点开始激活。当 重新启动模拟,将选择相同的节点作为初始节点 条件。在
initially_activated=35
设置阈值
可以为所有节点设置激活阈值
thresholds=np.random.rand(G.number_of_nodes())
请注意,阈值需要位于域[0,1]。在
你也可以设置一个通用的阈值
thresholds=0.1
在这里,10%的节点邻居需要被激活,以便 节点也将变为活动状态。在
定向网络
如果有足够数量的节点指向 朝向节点处于活动状态。这意味着节点的度将 是确定这个特定节点是否 变得活跃起来。在
加权网络
如果要在加权网络上进行模拟,请提供weight 关键字
Thresh=ThreshModel(G,initially_activated,thresholds,weight='weight')
类似于networkx文档:weight(string,可选 (默认值=None)—获取边权重的属性名。 E、 g.:G.edges[0,1]['weight']。在
当所有 指向焦点节点的激活节点将到达 > threshold*in_degree。在
文档字符串
这是模型的docstring。在
>>> help(ThreshModel) Help on class ThreshModel in module thresholdmodel.model: class ThreshModel(builtins.object) | ThreshModel(G, initially_activated, thresholds, weight=None) | | A simple simulation class that runs | a threshold-model activation process | on a static network (potentially weighted and directed) | in continuous time using Gillespie's | stochastic simulation algorithm. | | The temporal dimension is fixed by assuming | that every node whose activation threshold | has been exceeded by neighboring inputs | is activated with constant and uniform | rate :math:`\gamma = 1`. | | Parameters | ========== | G : networkx.Graph, networkx.DiGraph | The network on which to simulate. | Nodes must be integers in the range | of ``[0, N-1]``. | initially_activated: float, int, or list of ints | Can be either of three things: | | 1. float of value ``0 < initially_activated < 1``. | In this case, ``initially_activated`` is | interpreted to represent a fraction of nodes | that will be randomly selected from the | set of nodes and set to be activated. | 2. int of value ``1 <= initially_activated < N-1``. | In this case, ``initially_activated`` nodes | will be randomly sampled from the node set | and set to be activated. | 3. list of ints. In this case, ``initially_activated`` | is interpreted to contain indices of nodes | that will be activated initially. | thresholds: float or iterable of floats | Can be either of two things: | | 1. float of value ``0 < thresholds <= 1``. | In this case, every node will have the same | activation threshold. | 2. iterable of values ``0 < thresholds <=1``. | In this case, the function expectes a list, | tuple, or array with length equal to the | number of nodes. Each entry `m` of this list | will be interpreted to be node `m`'s activation | threshold. | weight: str, default = None | A string that represents the weight keyword of a link. | If `None`, the network is assumed to be unweighted. | | Example | ======= | | >>> G = nx.fast_gnp_random_graph(1000,20/(1000-1)) | >>> model = TreshModel(G, 100, 0.1) | >>> t, cascade_size = model.simulate() | | Attributes | ========== | G : nx.Graph or nx.DiGraph | The network on which to simulate. | Nodes must be integers in the range | of ``[0, N-1]``. | N : int | The number of nodes in the network | weight: str | A string that represents the weight keyword of a link. | If `None`, the network is assumed to be unweighted. | in_deg : numpy.ndarray | Contains the in-degree of every node. | thresholds: numpy.ndarray | Each entry `m` of this array | represents node `m`'s activation | threshold. | initially_activated: numpy.ndarray | Each entry of this array contains | a node that will be activated initially. | time: numpy.ndarray | Contains every time point at which a node was | activates (after ``simulation()`` was called). | The temporal dimension is given by assuming | that every node whose activation threshold | has been exceeded by activation inputs | is activated with constant and uniform | rate :math:`\gamma = 1`. | cascade_size: numpy.ndarray | The relative size of the activation cascade | at the corrsponding time value in ``time`` | (relative to the size of the node set). | Only available after ``simulation()`` was called. | activated_nodes: list | A list of lists. | Each entry contains a list of integers representing | the nodes that have been activated | at the corrsponding time value in ``time``. | Each list entry will contain only a single node | for every other time than the initial time. | Only available after ``simulation()`` was called. | | Methods defined here: | | __init__(self, G, initially_activated, thresholds, weight=None) | Initialize self. See help(type(self)) for accurate signature. | | reset(self) | Reset the simulation. | | set_initially_activated(self, initially_activated) | Set the process's initial activation state. | | Parameters | ========== | initially_activated: float, int, or list of ints | Can be either of three things: | | 1. float of value ``0 < initially_activated < 1``. | In this case, ``initially_activated`` is | interpreted to represent a fraction of nodes | that will be randomly selected from the | set of nodes and set to be activated. | 2. int of value ``1 <= initially_activated < N-1``. | In this case, ``initially_activated`` nodes | will be randomly sampled from the node set | and set to be activated. | 3. list of ints. In this case, ``initially_activated`` | is interpreted to contain indices of nodes | that will be activated initially. | | set_thresholds(self, thresholds) | Set node activation thresholds. | | Parameters | ========== | thresholds: float or iterable of floats | Can be either of two things: | | 1. float of value ``0 < thresholds <= 1``. | In this case, every node will have the same | activation threshold. | 2. iterable of values ``0 < thresholds <=1``. | In this case, the function expectes a list, | tuple, or array with length equal to the | number of nodes. Each entry `m` of this list | will be interpreted to be node `m`'s activation | threshold. | | simulate(self, save_activated_nodes=False) | Simulate until all nodes that can be activated | have been activated. | | Parameters | ========== | save_activated_nodes: bool, default = False | If ``True``, write a list of activated nodes | to the class attribute ``activated_nodes`` | every time an activation event happens. | Such a list will contain only a single node | for every other time than the initial time. | | Returns | ======= | time : numpy.ndarray | Time points at which nodes were activated. | cascade_size : numpy.ndarray | The relative size of the activation cascade | at the corrsponding time value in ``time`` | (relative to the size of the node set).
- 项目
标签: