psychopy.org | Reference | Downloads | Github

Assign different colors to a list of words of variable length

Hello there!

This might seem like a stupid question, however im not able to solve it myself.

The experiment: A randomized list of words (one word at a time) is presented to the user where every word is colored either in red, yellow, green or blue. Depending on the color of the word the user needs to press a specific key (so it’s similar to a stroop test).

What I’ve done: Created an Excel spreadsheet with the following columns: “words”, “colors”, “correctKey”. It looks like this:

words    colors    correctKey
dog      red       d
dog      yellow    f
dog      green     j
dog      blue      k    
cat      red       d
cat      yellow    f
cat      green     j
cat      blue      k    
bird     red       d
bird     yellow    f
bird     green     j
bird     blue      k    
<..etc..>

In the Builder I then create a Routine (displaying text with $words, color with $colors and correct answer with $correctKey). I put a loop around that routine (conditions is set to the spreadsheet). nReps is set to 2.
It works just fine.

Every word is displayed randomly in every color, twice.

However: is there an easier way of organizing the words and colors? Because in the future other people will use this experiment with their own lists of words. Depending on the number of words and colors it will be rather time consuming to create the excel spreadsheet.

Is there some way of having just one list of words (dog, cat, bird, etc) and one list of colors (red, yellow, green, blue) without writing down every possible combination of those two lists in the spreadsheet?

Yes. It is called programming. You just need to weigh up whether it is really more inconvenient to use the capacities of your spreadsheet to create the list compared to learning the required skills to generate it programatically.

There will be a few examples in the forum of creating conditions in code, but in essence you want to create lists of the same length that are balanced and sorted appropriately.

e.g.

colour_list = ['red', 'yellow', 'green', 'blue'] # etc
word_list = ['dog', 'cat', 'bird'] # etc

# create lists to be the same lengths:
colours = colour_list * len(word_list)
words = word_list * len(colour_list)

# make the words cycle more slowly:
words.sort()

Then you can zip those lists together into a list of dictionaries, containing named key:value pairs of conditions for each trial like this:

 conditions = [{'word':word, 'colour':colour} for word, colour in zip(words, colours)]

And then use the DictWriter class from Python’s csv module to write that list of dictionaries to a .csv file to use as your conditions list.

2 Likes

So, okay this actually helped a lot. Thanks!

Now, I’ve got a “setup.csv” where I have three columns:
“word”, “color” and “correctKey”

This is my attempt at hacking it all together:

words = []
colors = []
keys = []

try:
    with open('setup.csv', newline='', encoding='utf8') as csvfile_in:
        reader = csv.DictReader(csvfile_in)
        for row in reader:
            if row['word'] is not "":
                words.append(row['word'])
            if row['color'] is not "":
                colors.append(row['color'])
            if row['correctKey'] is not "":
                keys.append(row['correctKey'])

words_ordered = []
for word in words:
    for color in colors:
        words_ordered.append(word)

colors_ordered = colors*(len(words))
keys_ordered = keys*(len(words))


conditions = [{'words':word, 'colors':color, 'correctKey':key} for word, color, key in zip(words_ordered, colors_ordered, keys_ordered)]

try:
    with open('tmp_conditions.csv', 'w', newline='', encoding='utf8') as csvfile_out:
        fieldnames = ['words', 'colors', 'correctKey']
        writer = csv.DictWriter(csvfile_out, fieldnames=fieldnames)
        writer.writeheader()
        for row in conditions:
            writer.writerow(row)

I did some clean up (renamed some variables and removed the exceptions, some other parts) so it’s more readable.
The code isn’t beautiful but it works.

So a big Thank You for this really quick answer!

1 Like