| Reference | Downloads | Github

Repeat Trials and Separating an Excel Sheet into 2 for Further Randomization


I am creating an experiment where participants listen to an audio file ending in one of two words (goat or coat) and then see two pictures (a goat and a coat) and must select which word they heard by selecting the corresponding picture. Currently I have two main trials of this (56 items/trials in each) as well as a 4 item practice trial. I am using builder view, and I have each of the three total trials on loop in a routine with a break… so it would look like this:

What I want to do is two things:

  1. For the practice trial, I want to make it to where if the participant wants to repeat an item, they are able to right away. I assume this would involve a nested loop of some sort and a Code Component, and I have tinkered around with these things but still can’t quite figure it out. So, they would hear the audio, have the photos pop up, make their response, and then if they wanted to repeat that item before moving on to the next one, they could just press a button or something to do so. (If they need to respond a certain way to allow for the repeat, this is perfectly fine, as I am testing kids and an experimenter will be marking their responses. So, the experimenter could just mark their response a certain way to allow for a repeat if necessary.)

  2. For the main trials, I am wanting to have the 56 audio files play randomly (only 1 time through), and then when the two photos of a goat and a coat pop up, I would like for these to be randomized by PsychoPy whether the goat is on the left or the right… to where the order of these is random in a different way for each participant. I believe this would involve having two separate excel sheets, one for the audio files and one for the two photos, but I am not sure if this is possible. Further, I would like to be able to trace back the keyed in response after seeing the photos to the audio file that was played before the photos. I would also like to see the photos that were presented with each audio file (e.g. whether or not the goat was on the left or the right to the screen) so that when looking at the responses I can know what their response was, according to what they saw on the screen (since it would be randomized), for each audio file. Again, I really don’t know if this is even possible, but I would appreciate any help!!
    I currently have my experiment set up with the main trials using one excel sheet each, but I would like to be able to separate the photos from the audio so the same photo order is not presented with the same audio for every participant.

I am using the most current version of PsychoPy.

1 Like

Correct. Create an inner loop that is not connected to your conditions file, and give a it a very high nReps number (like 99). That way, it will keep running the routine with the current variables determined by the conditions file attached to the outer loop. Add a code component and in its “end routine” tab, put something like:

if yourKeyboardComponent.keys != 'space': # or whatever works for you
    yourInnerLoop.finished = True

i.e. if the subject pushes space, the loop will repeat. If not, it will terminate and the next iteration of the outer loop will go ahead.

Code component, “begin routine” tab:

locations = [-200, 200] # use whatever units suit

# place the stimuli where needed:
goat_stim.pos = (locations[0], 0) 
coat_stim.pos = (locations[1], 0)

# store the locations in the data:
thisExp.addData('goat_x', locations[0])
thisExp.addData('coat_x', locations[1])

No information about location is needed in the conditions file, as the randomisation and assignment is happening entirely in code.

Hello Michael,

I implemented the loop and code component you mentioned in order for the participant to be able to repeat trials, and once I got to the first trial in my experiment, now I am unable to press any key to respond (so normally you would be able to either press ‘p’ or ‘q’ to go on to the next item, and now ‘p’ and ‘q’ do nothing and space also does nothing)… do you know what could be the problem here? Could it have to do with the fact that I also have a Break incorporated in my loops, as such (see below)?

If this helps at all, this is what I entered into the code component.

Additionally, could you explain a little more the Code component you said to enter for randomizing the goat and coat photos? I’m sorry, I am very new to PsychoPy and still trying to figure everything out.

For example, is “goat_stim” where I enter the name of the file I am using for my photo of a goat (which is “goat.png”) and the same for “coat_stim” obviously? Or… would I just replace the word “goat” with the my file name for my goat photo?

Also, I would replace “thisExp” with the name of my experiment (which is “GC3kids.psyexp”), correct?

And where it says ‘goat_x’ and ‘coat_x’, what exactly am I supposed to be inputting here?

Finally, I am trying to figure out what exactly the order/placement (e.g. being between [ ]) of the numbers for locations signifies.

Only you know whether the inner loop should just be encasing the trial1 routine, and when the key presses occur to end a trial, and hence when the code to check for the space key should be. But at the moment it seems that you are checking for this at the end of trial1, so currently the pause will happen regardless of what gets pushed. Try drawing a flow diagram of what you want to happen, and then the arrangement of loops and when code checks happen should fall out easily from that.

No, these are to represent the names of your Builder stimulus components. (i.e. filenames don’t have spatial coordinates). It seems you are currently using uninformative names like image and image_2, etc. Just make the names match (but it is a good idea to use meaningful rather than arbitrary ones).

No, thisExp is a special name Builder creates for all experiments.

Whatever you want to use as a label for the corresponding columns in your data file. I just chose something concise but descriptive.

There are two values in the list of locations. The first (-200) is indexed as 0, the second (200) as 1. When we shuffle the order of items in the list, the relationship between the value and its index might or might not change. i.e. after the shuffle, the entries have either flipped places or stayed the same.

This is the error message I am getting.

OK, drop the brackets, like this:

if yourKeyboardComponent.keys != 'space':

Just tried that, and I got this response

if yourKeyboardComponent.keys != 'space':

Oh, I didn’t see that, my bad! I just removed all brackets, the experiment responds to me pressing ‘p’ or ‘q’ and moves forward to the blank screen (Break), but when I press space to move on to the next item, the experiment quits and pops up with this, which is weird because I do not have those brackets in the coding component anymore:

Do you think the fact that I have a break in with the Loop is affecting it somehow? Because I have it set to where you press space (after you have made a selection by pressing ‘p’ or ‘q’) to move on from the break to the next item.

But I also want to make it to where you press space (instead of ‘p’ or ‘q’) to repeat the item before getting to the break, obviously.

Clearly you do (they are being inserted in line 1000 of the generated script). Check for duplicated code components, or in other tabs in each code component, that you’ve saved any changes, and so on.

Sorry, I found where I had that still. This part is working now!

I am still trying to figure out how to shuffle images, though.
When I get to the loop of trials where I am wanting to have the images shuffle, the experiment just quits before they begin, and I get this message:

This is what I have entered in for the code component (the -.5 and .5 are inserted because of the units I am using):

OK, sorry that was my fault. shuffle() is an unusual function for Python because it doesn’t return anything, but just acts on an object in place. So I was using it the wrong way in the code originally suggested (so it was returning None rather than the shuffled list). I’ve edited it above, but for clarity, the shuffling should be done like this:

locations = [-200, 200] # use whatever units suit

Okay, I think I have figured out everything I had questions about now! Thank you!

Okay, I came across one more quick question. So, in my data folder, a csv file of the data only saves when my experiment is run all the way through, so if I quit the experiment part way through, I am only left with a log and sometimes a psydat file in the data folder – no csv file. Shouldn’t I be able to get a csv file when quitting the experiment part way through as well, so I could see the data from the portion of the experiment that the participant did complete? This is what my experiment data settings look like:


Yes, as long as you quit in a civilised way (like by pushing the escape key). That allows the required tidy-up procedures to happen as the experiment closes down.

Pushing the red “kill switch” button in the toolbar is not a civilised way to end an experiment: it’s more like just pulling the plug, so the code is forced to quit abruptly, without a graceful exit.