没有项目描述
stochastic-arrow的Python项目详细描述
箭头
“……即使前一毫秒比宇宙诞生时离我们更近,也同样遥不可及。” ―让·克里斯托夫·瓦尔塔,《明亮的混沌》
概念
这个库实现了Gillespie
Algorithm的一个通用版本,一个随机的
离散系统的数值求解方法。每次迭代,算法
将计算给定速率和计数的每个反应的倾向性
在系统当前状态下的反应物中,选择一个
反应发生的时间间隔
当前的反应。迭代此操作将生成以下对象的轨迹(或history
)
仿真过程中的状态向量。
安装
在您的requirements.txt
中添加以下内容,或者
pip install stochastic-arrow
:
stochastic-arrow==0.1.4
用法
arrow
库将单个类表示为接口,
StochasticSystem
,对一组反应(编码为numpy
)进行操作。
化学计量系数矩阵)和相关反应速率:
fromarrowimportStochasticSystemimportnumpyasnp# Each row is a reaction and each column is a molecular species (or other# entity). The first reaction here means that the first and second elements# combine to create the third, while the fourth is unaffected.stoichiometric_matrix=np.array([[1,1,-1,0],[-2,0,0,1],[-1,-1,1,0]],np.int64)# Each reaction has an associated rate for how probable that reaction is.rates=np.array([3.0,1.0,1.0])# Once we have a matrix of reactions and their associated rates, we can# construct the system.system=StochasticSystem(stoichiometric_matrix,rates)
现在系统已经被实例化了,我们可以用任何初始的 状态向量,然后在给定的时间间隔内运行它:
# This gives the initial state of the system (counts of each molecular species,# for instance).state=np.array([1000,1000,0,0])# We also specify how long we want the simulation to run. Here we set it to one# second.duration=1
一旦我们有了初始状态和持续时间,我们就可以运行
给定的持续时间。evolve
返回带五个键的字典:
- 步骤-模拟所采取的步骤数
- 时间-每个事件发生的时间点
- 事件-发生的事件
- 发生次数-每个事件发生的次数(直接从
events
派生) - 结果-系统的最终状态
result=system.evolve(state,duration)
如果你对绘制或其他的州的历史感兴趣,可以是 从事件列表和化学计量矩阵以及初始 国家。为您提供了一个函数``来执行此操作:
fromarrowimportreenact_eventshistory=reenact_events(stoichiometry,result['events'],state)
测试
arrow
使用pytest:https://docs.pytest.org/en/latest/以便您可以简单地测试
通过调用:
> pytest
另外,我们还有一个测试,它生成了可以运行的各种系统的图 喜欢这样:
> python arrow/test/test_arrow.py --plot