Randomize 4 blocks of randomly selected images

OS (e.g. Win10): macOS Catalina
PsychoPy version (e.g. 1.84.x): v2020.1.2
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:

My study is basically presenting either a positive, negative, or no sound with a mix of positive and negative images with valence and arousal ratings. I would like the positive and negative images to be split into 8 blocks of 12 pictures each, with 4 blocks of each valence. I am not sure on how to create blocks where PsychoPy will randomly selected 12 images out of the possible 48 with no repeats.

What did you try to make it work?:

I manually tried to force randomized blocks by making 8 conditions file for the 8 blocks with random images in each. I used a nested loop to randomly present the blocks.

What specifically went wrong when you tried that?:

  1. The sound loop keeps restarting while simultaneously continuing (sounds like two sound files playing at once) no matter what I do. I used this custom code component under “Begin Routine” :
    if blocks.thisRepN == 0:
    and “End Routine”
    if blocks.thisRepN == 100:

The sound only replays in Pavlovia, not PsychoPy.

  1. If there is an easier way to have random blocks, I would love to know! My python skills are pretty weak, so I have been sticking to builder.

Hi There,

Just to clarify the nesting of your design there before advising. Am I accurate in interpreting you want 4 blocks positive valence, 4 blocks negative valence, 12 trials per block? or do you want positive and negative valence to be randomly mixed within the same block? (i.e. 8 mixed valence blocks, each containing 12 trials, 6 positive 6 negative?)


Hi Becca,

Your interpretation is correct!
Block 1: 12 pictures of positive images
Block 2: 12 pictures negative
Block 3: 12 pictures positive… and so forth, for 8 blocks total.

OK! that was a really fun one to think about - a great example of how to structure nested loops though!! (but I can understand why it’s a tricky one!)

So here is a demo builder file randomBlocks_2.psyexp (30.9 KB) and three conditions files:

  1. the “outer loop”, i.e. whether we should present a block of positive or negative words conditions_outer.xlsx (8.5 KB)
  2. positive words (for you, you replace these with corresponding wav files instead, i programmed this with written words for ease of the demo) positiveConditions.xlsx (9.5 KB)
  3. negative words negativeConditions.xlsx (9.5 KB)

To talk through what we have here in our flow

First, in the “break_2” routine, we specify “this_range” which corresponds to the rows of your conditions file you want to use from your positiveConditions and negativeConditions .csv files (comments outline other variables below).

if blockType==1: #blockType is derived from your "conditions_outer.csv" file
    this_range = range(pos_count, pos_count+trials_per_block)#trials_perblock is in the "begin experiment" tab of this routine
elif blockType ==2:
    this_range = range(neg_count, neg_count+trials_per_block)#pos_count and neg_count are to keep track of how many positive and negative trials there have been - see end routine of the code components in routine_1 and routine_2

We then feed “this_range” into your “positive” and “negative” loops, so that we know which rows we want.

Finally, we only want EITHER routine_1 or routine_2 on each iteration of out “BlockSelect” loop. to do this we use the following:

if blockType!=1:
    continueRoutine = False #end the routine
    positive.finished = True #break the loop

so we end the routine and break the loop if this routine is not needed this time.

For your experiment, you will of course want to adapt this for with sound stimuli, but this should solve your trial structure query (and this is a great example thanks!)

Thanks and hope this helps,

PS. you might want to use better (less generic) routine and component names than I have in this quick demo :joy:

Hi Becca,

Wow, thank you so much!

So I tried to adapt this demo to my study, but there seems to an error (but definitely better than the version I had initially, you are a life saver!)
This is what I did:

  1. I added the custom code components, making new condition files with the images I wanted to use.
  2. I added my custom code component for my sound.

It seems like it works in the beginning, but then after 12 positive images, valence and arousal ratings show up without any pictures. Additionally, the cross I put in never shows up → does this mean the second block is never occurring?

Please let me know if I explained that clearly, thank you so much for your help again!

Thanks a bunch,


ok so there are a few checks you can try.

  1. if you are concerned the second block is never happening it is possible you need to double check the second code component is adapted to your block.

so for your negative valence loop check the code reads:

if blockType!=2:#rather than blockType1
    continueRoutine = False #end the routine
    negative.finished = True # rather than positive
  1. you can also add print commands to check routines are working e.g. in your “Begin routine” tab enter
print('this blockType is %s'%(blockType)')

That will show you if the selected block is 1 or 2 on each iteration of the loop by printing out some text to the “stdout” part of your “runner view” (the box with the minus, plus and red cross symbols).

it is possible that the above might fix the issue you mention of no images presenting after 12 positive images (because if your negImage is ended - because of issue 1, it will skip that routine and go straight ahead to your rating routines.

Hopefully that made sense, let us know how you get on.