Laila Van Ments Laila Van Ments - 6 months ago 11
Python Question

Accessing a value from an np.array from a while loop

I have the following piece of code:



import math
import numpy as np
from pylab import plot, show

def model():
Dt = 1
time_step = 50

#initializing the connection weights
w1 = 1

#initializing parameter values for the alogistic function
steepness_SS_a=1
speed_SS_a=1
threshold_SS_a=1

WS_a=1
SS_a=0
new_SS_a=np.array([SS_a])
t=0.0
timesteps=[t]

while t<30.0:

new = SS_a + speed_SS_a * (
(
(1/(1+math.exp(-steepness_SS_a * (w1 * WS_a - threshold_SS_a))))
-
(1/(1+math.exp(steepness_SS_a *threshold_SS_a)))
)
*
(1+math.exp(-steepness_SS_a*threshold_SS_a))
-
SS_a
)
new_SS_a = np.append(new_SS_a,new)

SS_a=new
t = t + Dt


timesteps.append(t)

print( timesteps )
plot(new_SS_a)
show()

model()


It is a computational model (I actually have more variables, this is a simplified version of the code), and during a certain period of time, the states of the model like SS_a have to update. Right now, the code works, but I want to make it more transparant, so you can extract a value of a certain state for a certain timestep.
I tried to do this with a for loop, and to give an index to each state for each timestep like this:

for i in range (30)
SS_a[i+1] = SS_a[i]+ speedfactor*(….. - SS_a(i))*Dt


, but this did not work because I can't add those values to a list.
However, I wondered whether there is a possibility in the code I have now, with the while loop, to extract these values and make the model more declarative and transparant?

Answer

as you want to put in a list a new value that depend in the previous value put there, you can do it like in this example

result=[0] #we put the initial value in the list
for _ in range(30): # the _ is to signal that we don use that value
    result.append( 10 + result[-1] ) # negative indices access the element in reverse order, -1 is the last, -2 is the previous to that etc
print(result)

with append we add a element to the end of the list

so in your code that is

...
def model():
    ...
    SS_a = [0]  
    for _ in range(30):
        SS_a.append( SS_a[-1]+ speedfactor*(... - SS_a[-1])*Dt )
    ...
    plot(SS_a)
    show()

you can even make your model more flexible by using arguments and default values, that way if you want to make some change just change how you call the model and not your code, for example like this

import math
#import numpy as np 
from pylab import plot, show
def model(Dt=1, time_step = 50, w1 = 1, steepness_SS_a=1, 
          speed_SS_a=1, threshold_SS_a=1, WS_a=1, SS_a=0, 
          t=0, t_final=30):
    result = [SS_a]
    timesteps =[]
    for ti in range(t,t_final+1,Dt):
        result.append( ... ) #<- put here your complicate formula 
        timesteps.append(ti)
    print( timesteps )  
    plot(result)
    show()
    #return result  

model() 

with all those default values we preserve the original behavior, and if we want to do only a minor change like run that model until time 40 instead of 30 we simple call it as model(t_final=40), you can also put a return of result so you can do some other manipulation later on, and we can also put a doct text to remember what each thing is in a easy way, like this

def model(Dt=1, time_step = 50, w1 = 1, steepness_SS_a=1, 
          speed_SS_a=1, threshold_SS_a=1, WS_a=1, SS_a=0, 
          t=0, t_final=30):
    """This is my model of ...
       where
       Dt: ...
       time_step: ... 
       w1: ...
       steepness_SS_a: ...
       speed_SS_a: ...
       threshold_SS_a: ...
       WS_a: ...
       SS_a: ...
       t: initial time
       t_final: final time
    """
    ...

and that we tell you a nice message that explain the model and how to used it is you call help on it help(model)