'Trying to get a loop to restart when using a shuffled array

Dear Stackoverflow fam,

I wish to run some experiments whereby I import some parameters for a stimulus (random dot kinematogram) and I chose to do this by creating a CSV with said parameters (total of 8 parameters I wish to cycle through). I need to pseudo-randomise the trials so I used the numpy.random.shuffle function to create this array. With this new array "shuffledRDK" i made the following loop

for b in range(2):
    for fps in range(120):
        blank_trial.draw()
        mywin.flip()
        time.sleep(6) 
        print("Trial: " +str(trial_counter) + " , Orientation " +str(shuffledRDK[x,0]) + " Coherence " +str(shuffledRDK[x,1]))
        trial_counter += 1
        if x in range (9):
            x += 1
            for fps in range(120):
                SignalDots = visual.DotStim(win=mywin, color=(1, 1, 1), dir=(shuffledRDK[x-1,0]), coherence=(shuffledRDK[x-1,1]), fieldSize=(20,20), speed=1, dotSize=5, dotLife=3, nDots=50, fieldShape='circle')
                SignalDots.draw()
                mywin.flip()

My issue is that at the end of the 8th parameter I wish for the cycle to start again. I get IndexError: index 8 is out of bounds for axis 0 with size 8 which I understand is out of the boundaries of the array but I don't know how to restart it. I've tried putting in if x > 8 statements or similar lines but I still get the index error. Would someone be able to help me out please?

Many thanks in advance



Solution 1:[1]

You shouldn't have to increment x up each time; it will do it on it's own as it iterates through the loop. I suppose you are doing this because you want your x to start at 1 and end at 9? you can specify that range explicitly.

for b in range(2):
    for fps in range(120):
        blank_trial.draw()
        mywin.flip()
        time.sleep(6) 
        print("Trial: " +str(trial_counter) + " , Orientation " +str(shuffledRDK[x,0]) + " Coherence " +str(shuffledRDK[x,1]))
        trial_counter += 1
        if x in range (1, 9):
            for fps in range(120):
                SignalDots = visual.DotStim(win=mywin, color=(1, 1, 1), dir=(shuffledRDK[x-1,0]), coherence=(shuffledRDK[x-1,1]), fieldSize=(20,20), speed=1, dotSize=5, dotLife=3, nDots=50, fieldShape='circle')
                SignalDots.draw()
                mywin.flip()

Solution 2:[2]

here is a variant using the data.createFactorialTrialList() permutation method and a .yaml configuration file
i edited the for loops to my understanding, as i cannot deduct what you are trying to do completely

from psychopy import core, visual, event, data
from psychopy.iohub.client import launchHubServer
import yaml

io = launchHubServer()
display = io.devices.display
mywin = visual.Window([640, 480], units='pix')


factors_info = yaml.load(open('stim_factors.yaml'))
stim_permutation = data.createFactorialTrialList(factors_info)
trial_handler = data.TrialHandler(trialList = stim_permutation, method ='random', nReps = 1)

for b in range(1):
    for fps in range(1):
        for stim in trial_handler:
            print(f'stim {trial_handler.thisN + 1:d}/{trial_handler.nTotal:d}')
            signal_dots = visual.DotStim(win=mywin, color=(1, 1, 1),
                                         dir        = trial_handler.thisTrial['dir'],
                                         coherence  = trial_handler.thisTrial['coher'],
                                         fieldSize  = trial_handler.thisTrial['field_size'],
                                         speed      = trial_handler.thisTrial['speed'],
                                         dotSize    = trial_handler.thisTrial['dot_size'],
                                         dotLife    = trial_handler.thisTrial['dot_life'],
                                         nDots      = trial_handler.thisTrial['n_dots'],
                                         fieldShape = 'circle')
            signal_dots.draw()
mywin.flip(); event.waitKeys()


io.quit(); mywin.close()
core.quit()

you can see the contents of the .yaml file on the picture's left enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shawn Ramirez
Solution 2