Python更新numpy数组:类型错误numpy.int32不可迭代

我编写了一个函数和while循环,旨在模拟以下随时间变化的化学反应系统:

系统中的化学方程式:

E + S-> ES == 1E + 1S + 0ES + 0P-> 0E + 0S + 1ES + 0P

ES-> E + S == 0E + 0S + 1ES + 0P-> 1E + 1S + 0ES + 0P

ES-> E + P == 0E + 0S + 1ES + 0P-> 1E + 0S + 0ES + 1P

该系统使用以下数组表示: 酶,底物,酶-底物复合物,产物的离散种群数(在时间= 0时):

popul_num = np.array([200, 100, 0, 0])

反应物比例:

LHS = np.array([[1,1,0,0], [0,0,1,0], [0,0,1,0]])

产品比例:

RHS = np.matrix([[0,0,1,0], [1,1,0,0], [1,0,0,1]])

系统中三个反应的速率:

stoch_rate = np.array([0.0016, 0.0001, 0.1])

状态更改数组:

state_change_array = RHS - LHS

最大模拟时间: tmax = 20

以及模拟的开始时间: 陶= 0.0

然后,我具有以下函数和while循环,以计算在上述反应过程中popul_num中离散分子的数量如何随时间变化。

在反应运行了一定时间后,popul_num数组将使用新值进行更新:

def propensity_calc(LHS, popul_num, stoch_rate):
propensity = np.zeros(len(LHS))
for row in range(len(LHS)):
        a = stoch_rate[row]     # type = numpy.float64
        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     # type = numpy.ndarray
return propensity

propensity = np.zeros(len(LHS))
while tao < tmax:
  propensity = propensity_calc(LHS, popul_num, stoch_rate)
  a0 = (sum(propensity))
  if a0 == 0.0:
      break
  t = np.random.exponential(1/a0)
  rxn_probability = propensity / a0   # propensity = array a0 = number --> Error
  num_rxn = np.arange(rxn_probability.size)
  if tao + t > tmax:
      tao = tmax
      break
  j = stats.rv_discrete(values=(num_rxn, rxn_probability)).rvs()
  print(tao, t)
  tao = tao + t
  popul_num = popul_num + np.squeeze(np.asarray(state_change_array[j]))

我想使用matplotlib在popul_num中的每个物种的折线图上绘制一条单独的线,以显示其随时间变化的数字。

我尝试将每个新值附加到popul_num中,并使用numpy.append()绘制新数组,但没有任何运气。

我试着写一个循环来遍历数组的每个元素,如下所示:

for i in range(4):
plt.plot(list(enumerate(popul_num[i])))  # error numpy.int32 object is not iterable
plt.show()

但我收到以下错误: TypeError:“ numpy.int32”对象不可迭代

有没有一种方法可以解决这个问题,或者有其他方法可以为每个物种随时间创建popul_num图?

EDIT: At the moment this is the plot that the code is producing enter image description here

这是想要的绘图类型,但我希望popul_num数组的每个元素都有一条单独的线,以显示该分子种类随时间的变化。

干杯

评论
  • ousman
    ousman 回复

    The popul_num are 4 values that are overwritten in each step. To draw curves, you need to save all these values in a separate array. The following code shows an example:

    import matplotlib.pyplot as plt
    import numpy as np
    from scipy.special import binom
    from scipy import stats
    
    popul_num = np.array([200, 100, 0, 0])
    
    LHS = np.array([[1, 1, 0, 0], [0, 0, 1, 0], [0, 0, 1, 0]])
    RHS = np.array([[0, 0, 1, 0], [1, 1, 0, 0], [1, 0, 0, 1]])
    stoch_rate = np.array([0.0016, 0.0001, 0.1])
    state_change_array = RHS - LHS
    
    def propensity_calc(LHS, popul_num, stoch_rate):
        propensity = np.zeros(len(LHS))
        for row in range(len(LHS)):
            a = stoch_rate[row]  # type = numpy.float64
            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  # type = numpy.ndarray
        return propensity
    
    tmax = 20
    tao = 0.0
    
    popul_num_all = [popul_num]
    
    propensity = np.zeros(len(LHS))
    while tao < tmax:
        propensity = propensity_calc(LHS, popul_num, stoch_rate)
        a0 = (sum(propensity))
        if a0 == 0.0:
            break
        t = np.random.exponential(1 / a0)
        rxn_probability = propensity / a0  # propensity = array a0 = number --> Error
        num_rxn = np.arange(rxn_probability.size)
        if tao + t > tmax:
            tao = tmax
            break
        j = stats.rv_discrete(values=(num_rxn, rxn_probability)).rvs()
        print(tao, t)
        tao = tao + t
        popul_num = popul_num + np.squeeze(np.asarray(state_change_array[j]))
        popul_num_all.append(popul_num)
    
    popul_num_all = np.array(popul_num_all)
    for i, (label, color) in enumerate(zip(['Enzyme', 'Substrate', 'Enzyme-Substrate complex', 'Product'],
                                           ['limegreen', 'dodgerblue', 'orange', 'crimson'])):
        plt.plot(popul_num_all[:, i], label=label, color=color)
    plt.legend()
    plt.tight_layout()
    plt.show()
    

    添加了图例以及每个曲线的名称。

    example plot

  • WOW
    WOW 回复

    You are trying to enumerate an integer number, popul_num[i], hence the error thrown. Did you mean this instead of your for loop:

    plt.plot(popul_num)
    plt.show()