简体   繁体   中英

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.

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:

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:

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:

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:

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:

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?

cheers

It appears that the values in the if statement are not to be used in the if conditional in the first place. You should use the.any() or.all() functions. Here is a link to illustrate what the ValueError stands for:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM