← Other topics

PsychoPy - Coding experiments (lexical decision task) (#2)

Series contents:

Video Notes

Summary

This is Part 2 of a series on building experiments using PsychoPy.

In Part 1 of the series, I gave a broad overview of getting PsychoPy installed and then explain the difference between the two main modes of designing experiments:

  1. Using the Coder View where you write the entire experiment in Python code
  2. Using the Builder View where you build an experiment using a visual interface

In this part, we’ll zoom in on the Coder View and break down how to build experiments using only code. Our example is a simple lexical decision task. The basic procedure of this experiment involves measuring how quickly people classify stimuli as words or nonwords.

Below you’ll find the code for version 1 of the experiment built in the video.

Docs

Here are links to the PsychoPy API docs referenced in the video:

Version 1 Code

# Import the modules we’ll need
from psychopy import core, visual, event
import random

# Initialize an empty string that will accumulate our results of each trial
results = ''

# Define our conditions
conditions = [
    ['cat', 1],
    ['tea', 1],
    ['bar', 1],
    ['ool', 0],
    ['jul', 0],
    ['pok', 0]
]

# Randomize the conditions
random.shuffle(conditions)

# Define the window
window = visual.Window(size=(800, 700), color='black')

# Define the instructions
welcome = '''
Welcome to the lexical decision task.

You are about to see a series of characters. 

If the characters make up a word, 
press the RIGHT arrow key.

If the characters do not make up a word, 
press the LEFT arrow key.

Press SPACE to begin.
'''

instructions = visual.TextStim(window, color='white', text=welcome, units='pix', height=20)

# Display the instructions and wait for the space bar to be hit to start
instructions.draw()
window.flip()
event.waitKeys(keyList=['space'])

# Begin trials
for condition in conditions:
    
    characters = condition[0]
    isWord = condition[1]

    # Define the stimulus (word text)
    word = visual.TextStim(window, color='white', text=characters, units='pix', height=40)
    
    # Display the stimulus
    word.draw()
    window.flip()

    # Start timer
    startTime = core.getTime()

    # Listen for a left or right key response
    response = event.waitKeys(keyList=['right', 'left'])

    # Check response accuracy
    if(isWord == 1 and response[0] == 'right'):
        correct = 1
    elif(isWord == 0 and response[0] == 'left'):
        correct = 1
    else:
        correct = 0
    
    # Append the results as an CSV string
    results += characters + ',' + str(isWord) + ',' + str(correct) + '\n'
    

# Print the results
print(results)    

# The next two lines ensure the PsychoPy application is properly terminated. 
# If you omit these lines, the window will still close when the script is done, but these
# lines that system resources associated with the window are released properly.
window.close()
core.quit()

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics