0

Essentially I am creating various arrays to elicit tactile stimulation (string of 0s with 1s when stimulation is delivered) for a random condition where the interval between the stimulation is drawn from a uniform distribution. I am using the following code to create the arrays (which all works fine):

def rand_stim_seconds(array):
    lst = list(range(0,10))
    random_ints = []
    for i in lst: # Creating list containing intervals drawn from distribution.
        rand_int = np.random.uniform(0.1, 0.6, 1)
        random_ints.append(rand_int)

    values = [] # Convert list from seconds into values of array.  
    for j in random_ints:
        d = j * 22050 # Mutiply by sampling frequency. 
        e = float(d)
        f = int(np.round(e))
        values.append(f)

    add_lst = [0] # Initialise with a value.
    for k in values: # Adding a '1' at specific intervals of the array.
        add = add_lst[-1] + k
        if add > 44100:
            break
        add_lst.append(add)
    print(add_lst)
    for l in add_lst[1:]:
        array[l] = 1

    return array

I am now trying to create 10 arrays using this function which will then output 10 different arrays (called "stim1" "stim2" etc) which will each have a different pattern of stimulation where each interval is randomly drawn. The following code is what I would like to convert into a loop:

rand1 = np.zeros((44100, 1)) 
rand2 = np.zeros((44100, 1))
rand3 = np.zeros((44100, 1))
rand4 = np.zeros((44100, 1))
rand5 = np.zeros((44100, 1))
rand6 = np.zeros((44100, 1))
rand7 = np.zeros((44100, 1))
rand8 = np.zeros((44100, 1))
rand9 = np.zeros((44100, 1))
rand10 = np.zeros((44100, 1))

stim1 = rand_stim_seconds(rand1)
stim2 = rand_stim_seconds(rand2)
stim3 = rand_stim_seconds(rand3)
stim4 = rand_stim_seconds(rand4)
stim5 = rand_stim_seconds(rand5)
stim6 = rand_stim_seconds(rand6)
stim7 = rand_stim_seconds(rand7)
stim8 = rand_stim_seconds(rand8)
stim9 = rand_stim_seconds(rand9)
stim10 = rand_stim_seconds(rand10)

I need to create new arrays of 0 for each array I wish to create otherwise it overwrites. Does anyone know how to turn the above into a neat loop as I am very aware of how amateur it is (I am new to Python)! Thank you!

sallicap
  • 75
  • 1
  • 7
  • 2
    You're using lists and `for` loops in the function.Why not use them to convert the code to a loop? – ForceBru May 19 '20 at 10:14
  • 1
    Does this answer your question? [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – quamrana May 19 '20 at 10:16
  • I'm confused.. are all of `rand1` through `rand10` the same? You already seem to know how to use for loops and lists. Instead of assigning to variables named `stim1` through `stim10` make a list called `stims` and append to it in a loop (or when you get more intermediate, use a list comprehension, like `stims = [rand_stim_seconds(np.array(...)) for _ in range(10)]`. What you have here is a numerically index sequence of values. This is exactly what a list is. Or an array for that matter. – Iguananaut May 19 '20 at 10:20

2 Answers2

0

If you need references to all rand objects:

rand_list = [np.zeros((44100, 1)) for _ in range(10)]
stim_list = [rand_stim_seconds(r) for r in rand_list]

If you only need stim references:

stim_list = [rand_stim_seconds(np.zeros((44100, 1))) for _ in range(10)]

What was previously stim1 is now stim[0] and so on. I am using list comprehension as it is the shortest to write.

Luka Mesaric
  • 657
  • 6
  • 9
  • Thank you! I don't know why I never think to use list comprehensions! In the end i used an array: ``` stim_list = np.zeros((10, 44100)) for i in stim_list: i = rand_stim_seconds(i) ``` Would you happen to know how to plot each line of an array to a subplot using matplotlib? I can't seem to wrap my head around how subplots in loops work! – sallicap May 19 '20 at 10:36
  • @sallicap I haven't used matplotlib a lot, but this might help you: https://stackoverflow.com/q/31726643/13300960 – Luka Mesaric May 19 '20 at 11:12
0

The following code should do the trick:

stim = [] # Storing stim values in a list
n = 10
for i in range(n):
    rand = np.zeroes((44100,1))
    stim.append(rand_stim_seconds(rand))

you can them access any on your stimulation arrays by their index as stim[0] for the first element

Iteeeh
  • 67
  • 6