| Reference | Downloads | Github

How to randomize stimuli color in Flanker task

If this template helps then use it. If not then just delete and start from scratch.
I’m on a MacOS Catalina 10.5.5 using PsychoPy 3.2.0. I am creating a flanker task where each block of stimuli (ie: groups of 5 letters) and want the color of the block of letters to be randomized. I am in builder view and figured out how to change the color of all blocks of stimuli to a different color (I did blue), but I cannot figure out how to do multiple colors.

This screenshot shows where I put the colors (I did blue, yellow, and red). However, when I try and run the experiment it won’t let me. But, it will run if I change the stimuli color to just one (ie: blue).

Any thoughts? I greatly appreciate any and all help! :slight_smile:

Please explain the details of what you want to achieve. Think the sort of detail you would need in the methods section of a paper.

Our study is looking into the relation between error and reward processes of the brain. The study is broken up into 3 sections, consisting of 3 flanker tasks. The first is the normal task, where 5 letters (consisting of H and S) are shown on the screen after a ‘+’ indicator. The participant presses ‘slash’ or ‘z’ depending on if there is an ‘H’ or ‘S’ in the middle of the group of letters.
The second task is the reverse of the first, where the participant must respond according to the first and fifth letters (the flanking letters).
The third task follows the layout of the first, however we want to add random colors to each group of letter shown. For example, there could be a block of ‘HHSHH’ that we want to be yellow, while the next block, ‘SSHSS’ might be red. The point of the third block is to add color in a random fashion to the stimuli to confuse the participant into thinking color is somehow significant to their answer, when in reality it isn’t.
So I’m trying to have 6-8 colors that are randomly assigned to the blocks of letters (my stimuli). Thanks! Let me know if I can provide any more info.

This is a massively overloaded term. You need to describe what you mean here. I’m not trying to be obtuse, but this will require a tiny bit of code to implement, and to do that, we need to know what the constraints on the randomisation will be.


  • Is this truly random, or pseudo-random?
  • If the former, then the list of possible colours needs to be specified.
  • If the latter, what is the constraint (e.g. equal numbers of each colour presented?)
  • If there are constraints, then things like the number of colours and the number of trials need to be specified (and consider that the number of trials will need to be a multiple of the number of colours, and so on).

In essence, code is a precise, mathematical-level description of the intended behaviour: we can’t just say “do this thing randomly” and expect that the result will fit what you want to achieve.

It’s pseudo-random. We have 800 trials consisting of 8 letter blocks. We want equal amount of color so there will be 8 colors. What are the next steps?

You could create an array containing 100 of each colour and shuffle it.

Insert a code component from the “custom” component panel. In the “begin experiment” tab, put something like this to create a randomised list of 8 colour names repeated 100 times each:

text_colours = ['red', 'green', 'blue', 'yellow', 'coral', 'firebrick', 'navy', 'papayawhip'] * 100

Then in the “begin routine” tab, something like:

# get this trial's colour:
trial_text_colour = text_colours.pop()

# record it in the data file:
thisExp.addData('text_colour', trial_text_colour)

Make sure the code component is above the text component so that the latter gets to refer to the latest value of the variable, and put $trial_text_colour in the colour field, set to update on every repeat.

thank you for the code tips. After speaking with my advisors, we have a better idea of what the three flanker blocks will look like and what the purpose is of each. The three blocks will be completed in one lab session.

Block 1: Participants will complete the standard flanker task, responding as quickly and as accurately as possible to blocks of 5 letters. The blocks are composed of “H” and “S”, and will press “z” or “slash” depending on which letter is in the middle of the block. For example, I would press “z” if the block was “SSHSS”. We want to add color to these blocks to confuse the participants, as they may think color has something to do with the correct answer. However, it will simply be a distraction. All letters will be the same color in each block. We had the idea of expanding our .excel and adding a ‘color’ column, where each stimuli block is pre-assigned a color. Our question is, how do we get psychopy to read the color column and input it into its display of our stimuli? Below is block 1 excel. It continues until row 100.
Screen Shot 2020-07-20 at 2.19.41 PM

Block 2: Block 2 is the inverse of Block 1, where flanking letters dictate the correct answer. However, we will not tell the participants this change. The purpose of this block is to observe how long it takes participants to figure out the new mapping of the task. Color will still be a distraction technique. We also lengthened this excel sheet to connects each stimuli to a color.

Block 3: Block 3 is based on colors. We have decided that ‘warm’ colors (Orange, Yellow, Red) will mean correct while ‘cool’ colors (Blue, Green, Purple) will be incorrect. Letter order does not matter for this block, however we will not tell the participants this tidbit of information as we, again, will be monitoring how long it takes them to learn the new mapping (ie: warm colors are correct while cool are not). We expanded the .excel to 100 rows and added a ‘text_color’ column. Our question is how to get psychopy to read the excel file and display the color corresponding to that stimuli row. I included a screenshot of part of Block 3’s .excel to give more context.

Thank you in advance for your help. If there is more information I can provide, let me know.

This YouTube PsychoPy Builder tutorial by Jason Ozubko includes a very clear example of how to do exactly what you’re asking about. He uses text stimuli in the Builder and information from a spreadsheet to color the stimuli differently in each trial.

How are you telling PsychoPy to use the information from the Excel sheet to determine what letters to display? If you can already do that then you can use the same technique for specifying colors as well.

Here’s basically what you would enter for the text stimulus component in Builder:

You’d have to make sure that the trial routine is put inside of a loop with specifications something like:

But again, the best thing to do is probably to watch and follow along with Ozubko’s tutorial.

Also, note that if you use the setup you’ve described, all participants will be shown colors in the same order, if I understand you correctly. Design-wise, you might want to think about if that’s really what you want, or if randomisation like Michael described would actually be better.

That was a very helpful youtube video. Thank you for the resource! I was able to randomize the presentation of color within the display box, but am receiving this error code

I ran the experiment a couple of times and received this error message for ‘green’, ‘blue’, and ‘purple’. Do you have any suggestions as to why this might be happening? Thanks

Are you trying to set the colour in a code component?

Have a look at my crib sheet for a method of using colour names as variables.

With error messages, it’s important to read them very carefully and pay close attention to details :slight_smile: Your error message reads

PsychoPy can’t interpret the color string 'blue ’

Do you see the difference between that and the sentence below?

PsychoPy can’t interpret the color string ‘blue’

The issue is that in your spreadsheet/.csv file, you have trailing whitespace, and PsychoPy isn’t “clever enough” to figure out what color 'blue ’ is, as opposed to ‘blue’. Since you shouldn’t have whitespace anywhere in your spreadsheet, you can use Excel’s “find and replace” tool to go through the entire spreadsheet and replace the character ’ ’ (whitespace/space) with ‘’ (nothing). You can do a google search for this if you’re unfamiliar with the tool.

1 Like