| Reference | Downloads | Github

Randomising trials within a block

PsychoPy version (e.g. 1.84.x): 3.1.5

What are you trying to achieve?:
In my stop signal task participants complete 16 practice trials, one block of 8 trials in which they make a response, and then one block of 8 trials where they withhold their response.
I want to then run a mixed block where the 16 practice trials are presented in a random order.

What did you try to make it work?:
I used the coin flip function to change the colour of the stimulus that instructs the participant to make a response or not:

coinflip=np.random.randint(3, size=1)
if coinflip==0:
elif coinflip==1:

What specifically went wrong when you tried that?:
This works but the number of trials is not equal, e.g. you could have 10 of one and 6 of another.

I was going to put a loop around the current routines as this would also solve my issue about how to show the correct feedback, but my issue there is that the new loop will contain instructions that I don’t want to be included.

I hope that makes sense to someone.

You most certainly want a loop. In fact, I’d suggest two. An inner loop to run your trials and an outer loop to run your blocks. The outer loop is set to be “sequential” and is connected to a conditions file that simply looks like this:


Then in your routine, insert a code component and put in something like the code below in the “begin routine” tab. Note that instead of randomly sampling a value on each trial, we create a balanced list of conditions and then randomly shuffle if required. This ensures that you don’t get the unbalanced mix of conditions you’re currently finding.

# only on the 1st trial of each block:
if your_trial_loop_name.thisN == 0:
    instruct_colours = ['blue'] * 8 + ['black'] * 8 # fixed (reverse) order

    if block_type == 'mixed': # randomised order

# get a value for this trial:
current_instruct_colour = instruct_colours.pop()
# store it in the data:
thisExp.addData('instruction_colour', current_instruct_colour)

Then in your text stimulus component, simply put $current_instruct_colour in its “Color” field, set to update on every repeat.

Make sure the code component is above the text component, so the latter always receives the latest value for the colour variable

You might also want to consider changing the visibility of the stimulus directly, by controlling its opacity value. This is more flexible (e.g. if you change the background colour, you don’t need to change the code).

Thank you for the response @dvbridges
I have almost no programming experience so please excuse my ignorance, but which routine would you inset the code component into?
I think I would be able to follow these instructions if I just had the one routine in which I changed the stimulus colour, but I currently have 2 routines as each trial has feedback.
As I understand it if I put the loop around the single, stop and mixed routines, it would also repeat the instructions for each condition.

mixed.csv (184 Bytes) TestingMixed.psyexp (32.0 KB)

You haven’t really fully described your task, but I assumed you would just be using one routine, controlling the task by changing the colour of the instructions to be visible or invisible.

You should describe your actual task procedure, and then we can advise on how you should implement it. Often it is possible to do things with just one loop surrounding one routine. If the tasks are actually fundamentally different, then multiple routines might be needed, but all still are housed within the same loop. You’d just need to control which one ran on a given trial.

But the first step is to fully describe your intended task procedure and design.

My apologies.
In my task participants will either be responding or withholding a response to food stimuli (responding to healthy, withholding to unhealthy).
For the moment I’m just trying to complete the practice section in which they will complete 8 trials responding with feedback, 8 trials withholding with feedback, then 16 mixed trials with feedback.
Then for the main experiment it will be continuous mixed blocks with no feedback.

I hope that makes it a little clearer.