Quasi random selection of some stimuli and random selection of others

Hi! I am very new to PsychoPy and I am stumped as to two specific (but interrelated) problems in organising the experiment I am compiling. I would be hugely grateful for some guidance!

Problem 1 – quasi-random presentation of stimuli:

I have five conditions (SynchronyTypes). Each SynchronyType has two associated speeds (‘Fast’ and ‘Slow’). I have 10 unique sound files, representing the Fast and Slow versions of each SynchronyType.

Each trial is broadly the same (in that a sound file is presented, then the participant responds to two questions). In selecting which sound file to play, I want the experiment to cycle through each of the five conditions/SynchronyTypes twice, giving 10 trials in total. Within each cycle, the order in which the SynchronyType is presented should be random (without replacement). In the first cycle, for each condition, the experiment should randomly select and present either the Fast or Slow variant (and play the associated sound file). In the second cycle, I want the experiment to select the Fast or Slow variant which was not selected in the first cycle, so that all 10 sound files are presented once.
Can I achieve this with loops/conditions files, or do I need to add some code? Previously I have just fudged the problem by generating ‘Order A’ and ‘Order B’ versions which meet these constraints but I would like to avoid this.

Problem 2:

I want each sound file to be presented as if they were made by a (different) child. So I want the experiment to randomly select one of 10 children to associate with each sound file/trial. In the course of each trial, I want to present an image of the child and the name of the child in the text.
This would obviously be straightforward to do if it were the only thing I wanted to vary from trial to trial, but I am unsure how to combine this with the above set up. When I built a previous version of the experiment in ePrime, I created a nested variable called ‘Pairs’ and put the names and images in a separate nested list, but am unsure how to achieve the same thing in PsychoPy.

Thanks again for any guidance you can offer!

Hi @CBM ,

Re: Problem 1: I can’t see a possibility to solve this without using code. Here is one option (there might be more elegant solutions which I can’t currently think of…):

Create a list of lists with all sound files:

soundsList = [["sound1_fast", "sound1_slow"], ["sound2_fast", "sound2_slow"], ["sound3_fast", "sound3_slow"]]  # and so on

Then shuffle the nested lists:

for sounds in soundsList:

Then unzip the lists (code taken from here):

soundsUnzipped = [list(i) for i in zip(*soundsList)]

Finally, shuffle the new nested lists:

for j in soundsUnzipped:

You could now use your loop counter and your trial counter to select the sound to play in the Sound field of your sound component:

$soundsUnzipped[trials.thisRepN][trials.thisTrialN]  + ".wav" # assuming your loop is called trials

Hope this helps.


Here is a possible solution for Problem 2:

Create a list of sound names:

sounds = ["sound1", "sound2", "sound3"]  # and so on

Shuffle it:


Create a list of dictionaries with names and images:

people = [{"name":"Tim", "pic":"tim.png"}, {"name":"Amy", "pic":"amy.png"}, {"name":"Joe", "pic":"joe.png"}]

Zip the two together and create a dictionary of dictionaries:

soundsPeople = dict(zip(sounds, people))

Then, in your text component:

# split to get rid of _fast/_slow and just keep sound1 etc.

And in your image component:


I think this should work, but I haven’t tested it.


Hi Jan

I haven’t had time to try this out yet (and I think it will take me some time to work through it!), but I just wanted to say thanks so much for your help and time with this, I really appreciate it. I will let you know how I get on.

Thanks again!

1 Like


I wanted to let you know how I solved this problem in the builder in the end, and also to ask for some help getting it online (see next post).

In getting this running in the builder, I used a lot of what you proposed but also simplified things a bit to save my sanity!

To solve (fudge) problem 1, I made two conditions files (Order A and Order B) for the trial loop, and used the Experiment Info box to direct participants to one or the other. So the sound files play sequentially in one of two set orders.

To solve problem 2 I inserted a code component before the trial loop with a list of dictionaries containing the children and their associated images and text. Then I shuffled this list. In the conditions file I added a column called “childNo” with numbers 0-9. For each trial, the image/sound component draws upon the dictionary and the conditions file e.g. the image of the child displayed at the start of each trial is set as people[$childNo][‘pic’].

So the end result is that the sound files are presented sequentially from trial to trial (in either Order A or Order B) and, since the order of items in the dictionary is randomly shuffled for each participant, the child associated with each sound file is set randomly every time.

My next problem is how to get this online. I have uploaded the experiment to Pavlovia.

To shuffle the list, I followed the crib sheet suggestion to insert a code component in the first routine (shuffle=util.shuffle;) (PsychoPy Python to Javascript crib sheet - Google Docs)

However, when it come to start the trial loop, the stimuli do not load and I get the following error message :

  • when setting the image of ImageStim: Child_Image
  • when getting the value of resource: people[$childNo][‘pic’]
  • unknown resource

Any ideas as to how I can get the experiment to locate the images (and other associated files) when set up in this way?

Thanks so much again for all the help so far!