我试图在Python3.8中使用一个包含以下实体的简单系统来模拟细胞系统的Gillespies算法:酶、底物、酶-底物复合物、产物
我有以下代码,用于计算一系列反应的倾向函数,这些反应表示为数组的行:
propensity = np.zeros(len(LHS))
def propensity_calc(LHS, popul_num, stoch_rate):
for row in range(len(LHS)):
a = stoch_rate[row]
for i in range(len(popul_num)):
if (popul_num[i] >= LHS[row, i]):
binom_rxn = (binom(popul_num[i], LHS[row, i]))
a = a*binom_rxn
else:
a = 0
break
propensity[row] = a
return propensity.astype(float)
输入数组如下所示:
popul_num = np.array([200, 100, 0, 0])
LHS = np.array([[1,1,0,0], [0,0,1,0], [0,0,1,0]])
stoch_rate = np.array([0.0016, 0.0001, 0.1000])
函数按预期工作,直到我尝试在以下while循环中调用它:
while tao < tmax:
propensity_calc(LHS, popul_num, stoch_rate)
a0 = sum(propensity)
if a0 <= 0:
break
else:
t = np.random.exponential(a0)
print(t)
# sample time system stays in given state from exponential distribution of the propensity sum.
if tao + t > tmax:
tao = tmax
break
j = stats.rv_discrete(name="Reaction index", values=(num_rxn, rxn_probability)).rvs() # array of reactions increasing by 1 until they get to the same length/size as rxn_probability
print(j)
tao = tao + t
popul_num = popul_num + state_change_matrix[j] # update state of system/ popul_num
while循环中的其他变量如下所示:
a0 = sum(propensity)
def prob_rxn_fires(propensity, a0):
prob = propensity/a0
return prob
rxn_probability = (prob_rxn_fires(propensity, a0))
num_rxn = np.arange(1, rxn_probability.size + 1).reshape(rxn_probability.shape)
当我在while循环中运行调用calc_-propensity函数的代码时,它通过while循环的第一次迭代,出现以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
该错误首先在calc_倾向函数的下一行抛出:
if (popul_num[i] >= LHS[row, i]):
但是由于某种原因,代码一直在运行,直到它在calc_倾向函数中到达同一行,但是在第二个函数调用(while循环)中,我不明白为什么
干杯
似乎if语句中的值首先不用于if条件。您应该使用.any()或.all()函数。以下链接说明ValueError代表什么:
https://sopython.com/canon/119/the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous/
相关问题 更多 >
编程相关推荐