PsychoPy: Changing background colors selectively

Say that you want to have an experiment where the background color changes based on a particular condition. This is a fairly common thing to do, but it’s necessarily straightforward in PsychoPy. Since every time I write a new experiment, I end up googling this and going through the usual steps to try and find a “good” solution – I figured it’s time to write it down.

In this particular experiment I wanted to have a type of video stimulus that was surrounded by a background color representing additional information to the participant. You can think of this as “Category 1” vs. “Category 2” just to help participants keep track of the stimulus sets they’re seeing. An example stimulus might look like this (borrowed from this amazing clip of Skye and Naomi dancing):

Skye and Naomi Dancing Lindy Hop

Now with any well designed and modular PsychoPy experiment, we have a list that that conveys the stimuli and in that list we might want to specify titles for the videos and background colors:

List file containing stimuli and related information (including color)

The next step is to construct a fairly simple PsychoPy experiment that has a loop that will contain the trials. This loop has two components, the first is just a title of the video (Title from above) and the second is the actual video with the background color (green or red):

Experiment Flow

Turning our attention to the second “ShowVid1” object/routine, we have two components, the first is a grating object, and the second is our movie object. My understanding of PsychoPy is that it draws in order, so you want the background first and the video second, so that the video appears on top of the background – yes I tried it the other way and as expected the video was underneath.

The components of ShowVid1

Now we define the grating object as follows, the duration, color, size (2,2), and units to “norm”. Under “Advanced” make sure you change the Texture to “None” and the mask to “None”. Also don’t forget to set the “Color” component to “set every repeat” otherwise PsychoPy will be angry with you and likely won’t run, definitely won’t change with each stimulus.

And there you have it, a PsychoPy experiment with a background that is dynamically set and does so at the start of a routine (unlike using the setColor() function, which would change it after the start of the routine.

This content kindly provided on the PsychoPy message boards courtesy of Jonathan Peirce, the developer of PsychoPy. Hopefully putting it here makes it easier to find for everyone, and obviously for me!

Comments are closed.