Nesting loops or randomizing order of routines?

326 views
Skip to first unread message

ACC

unread,
Apr 25, 2014, 7:42:16 PM4/25/14
to psychop...@googlegroups.com
I'm new to PsychoPy, downloaded it today, and I like it very much so far.

I'm trying to figure out how to use it for a categorical perception study with:
-20 trials in random order,
-Each trial has a separate set of audio stimuli, only one of which will be randomly selected for that participant (no others from the set will be used for that participant),
-Two images will be shown for the participant to choose between in response, I would like to randomize the side they appear on from one participant to the next.

Can this be accomplished through nesting loops?

My thought is to have 20 different routines (one for each trial) and for the order of the routines to be randomized, is that possible?

Michael MacAskill

unread,
Apr 26, 2014, 6:39:08 AM4/26/14
to psychop...@googlegroups.com
Dear Unnamed User,

Welcome to PsychoPy.

> My thought is to have 20 different routines (one for each trial) and for the order of the routines to be randomized, is that possible?
We need to find the most efficient way of bending PsychoPy to fit your design. The moment you start thinking of making that many different routines, it is time to think of another way :-)
A key goal is to eliminate duplication: if you find an error or want to make a change, it will need to be done 20 times…

> I'm trying to figure out how to use it for a categorical perception study with:
> -20 trials in random order,
What information is need for each trial? i.e. what will you be putting in your conditions file?

> -Each trial has a separate set of audio stimuli, only one of which will be randomly selected for that participant (no others from the set will be used for that participant),
How many possible stimuli are in each set?

> -Two images will be shown for the participant to choose between in response, I would like to randomize the side they appear on from one participant to the next.
Insert a Code Component, *above the Image component*. In its "Begin experiment" field, put:

imageSide = [-0.5, 0.5] # depending on units. in pix, could be, say: [-200, 200]

In the "Begin Routine" tab, put this:

shuffle(imageSide)

In your image components, put these in their position fields, and select "set every repeat":

[imageside[0], 0] # the first stimulus

[imageSide[1], 0] # the second stimulus

Lastly, you might want to record which side was chosen in your data file. In the "End routine" tab, put this:

thisExp.addData("Side", imageSide[0]) # only need to save one value, as the other is determined


> Can this be accomplished through nesting loops?
That might not be necessary. It depends on the number of audio files in each per-trial set.

Regards,

Mike


ACC

unread,
Apr 26, 2014, 8:42:57 AM4/26/14
to psychop...@googlegroups.com
Mike, thanks for the response.

I should mention I'm not familiar with Python, and have limited programming experience.

Each trial is exactly the same in terms of layout, so you're right, it doesn't make sense to create 20 routines. But, I'm not sure how to get each repetition of the routine to draw from a discrete set of stimuli and not to draw from it again (for each participant). Is there a way to do a stratified random sample of the conditions file? If so, this still wouldn't seem to solve the issue of randomizing the trials (in itself).

Here's the basic layout:
1 audio stimulus is played from a set of 4-7 audio stimuli specific to that trial
2 images (specific to that trial) are displayed after audio is played (w/ side randomized -- thanks for the tip on this).
Keyboard response to select one of the images as matching the stimuli

In the datafile, I need to know for each trial:
1. what audio file was played
2. what images were shown and on which side
3. which key response (of 2) was selected

In the conditions file I have columns for audio, image a, image b. 
Each image pair should only be used once. So perhaps the image pair could be randomly selected and then there could be a condition to select an audio stimuli from a certain set based on the image pair that was selected. 

Thanks again,
Aaron


Michael MacAskill

unread,
Apr 26, 2014, 2:18:09 PM4/26/14
to psychop...@googlegroups.com
Hi Aaron,

> Each trial is exactly the same in terms of layout, so you're right, it doesn't make sense to create 20 routines. But, I'm not sure how to get each repetition of the routine to draw from a discrete set of stimuli and not to draw from it again (for each participant).

That detailed description makes it easier to figure out what to do. I'll suggest a way, but it may not be the best way. For your first experience with PsychoPy, you've chosen a more complicated design than it is built for "out of the box", but it is flexible enough that we can tweak things to accommodate. What complicates matters is the selection of a random audio stimulus from a variable number of trial-specific stimuli.

In essence, though, I think you need just need one loop, and a csv/xlsx conditions file of just 20 rows.

You will have two columns for the images, and a column for the audio. The tricky thing is that the number of audio files can vary from trial to trial, so we are going to need to squeeze them into a single column. They'll need to have a special format, as follows:

image_A, image_B, audio
cat.png, dog.png, "[1.wav, 2.wav, 3.wav]"
bat.png, rat.png, "[4.wav, 5.wav, 6.wav, 7.wav]"

i.e. the audio cells need to be surrounded by quotes as they contain commas (which otherwise would be used to separate cells). They also need square brackets, as these can be used to indicate a "list" object in Python.

image_A and image_B can be used as standard Builder variables, but "audio" will need some extra steps to become usable for you.

Again, in a code component, in the "Begin Routine" tab, put this (it will interpret the string of characters into actual Python code):

audio = audio[1:-1] # we need to strip off the quotes at each end of the string
audio = eval(audio) # evaluate the string, making audio into a list of filenames
shuffle(audio) # randomise that list
audio = audio.pop() # just take one item from the original list

Again, make sure this code component is above any sound object.You can then just put "$audio" (no quotes) in the sound component's Sound field.

Lastly, in the "End routine" tab of the Code component, put:

thisExp.addData("SoundFile", audio) # save the chosen filename for this trial.
# not using the name "audio" to label this column, as it may conflict with the original variable?

> If so, this still wouldn't seem to solve the issue of randomizing the trials (in itself).
This is now handled simply by setting the loop to cycle randomly through your 20-row condition file.

> In the datafile, I need to know for each trial:
> 1. what audio file was played
As above.

> 2. what images were shown and on which side
The image names are automatically saved, as they appear in the conditions file.
The side was described in the previous message.

> 3. which key response (of 2) was selected
That should also be stored automatically. See the online Stroop demo for that stuff.

Regards,

Michael


Reply all
Reply to author
Forward
0 new messages