简体   繁体   English

为什么我的 function 第二次从 python while 循环中调用时崩溃?

[英]Why does my function crash the second time it's called from within a python while loop?

I'm trying to simulate Gillespies algorithm for cellular systems in python 3.8 using a simple system with the following entities Enzyme, Substrate, Enzyme-Substrate complex, Product.我正在尝试使用具有以下实体酶、底物、酶-底物复合物、产品的简单系统来模拟 python 3.8 中蜂窝系统的 Gillespies 算法。

I have the following code that calculates propensity functions for a series of reactions which are represented as rows of an array:我有以下代码计算一系列反应的倾向函数,这些反应表示为数组的行:

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)

The input arrays are as follows:输入arrays如下:

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])

The function works as expected, until I try to call it from within the following while loop: function 按预期工作,直到我尝试从以下 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

The other variables in the while loop are as follows: 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)

When I run the code calling the calc_propensity function from within the while loop, it makes it through the first iteration of the while loop with the following error:当我从 while 循环中运行调用 calc_propensity function 的代码时,它会通过 while 循环的第一次迭代并出现以下错误:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The error is first thrown on the following line of the calc_propensity function:错误首先在 calc_propensity function 的以下行中引发:

if (popul_num[i] >= LHS[row, i]):

but for some reason the code keeps running until it reaches the same line in the calc_propensity function but in the second function call (the while loop) and I dont understand why?但由于某种原因,代码一直运行,直到它到达 calc_propensity function 但在第二个 function 调用(while 循环)中的同一行,我不明白为什么?

cheers干杯

It appears that the values in the if statement are not to be used in the if conditional in the first place.看来 if 语句中的值首先不应该在 if 条件中使用。 You should use the.any() or.all() functions.您应该使用 .any() 或 .all() 函数。 Here is a link to illustrate what the ValueError stands for:这是一个链接来说明 ValueError 代表什么:

https://sopython.com/canon/119/the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous/ https://sopython.com/canon/119/the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:为什么在while循环中调用的函数返回的值不同于单独调用时的返回值? - Python: Why would a function called within a while loop return something different than when called alone? 自定义绘图功能在交互式提示中第二次调用时不执行任何操作 - Custom plotting function does nothing the second time it's called from interactive prompt 为什么每次运行 OpenCV 函数时我的 python 脚本都会崩溃? - Why does my python script crash every time I run OpenCV function? 为什么我的 python while 无限循环? - why does my python while loop infinitely? 为什么我的 Python while 循环不会终止? - Why does my Python while loop not terminate? 为什么我的Python函数在控制台中起作用,但在代码中调用时却不能起作用? - Why does my Python function work in the console, but not when called within code? Python - 为什么第二个 for 循环从第二行开始 - Python - Why does the second for loop start from the second row 如何防止 function 在 while 循环中被多次调用 - PYTHON - How to prevent a function from being called more than one time in a while loop - PYTHON Python:为什么函数中的对象根据调用的方式返回不同的id()? - Python: Why does the object in my function return differing id()'s depending on how it is called? Python lambda function 没有从 for 循环中正确调用 - Python lambda function is not being called correctly from within a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM