Self-Paced Reading: get response times for each word

OS (e.g. Win10): Windows 10
PsychoPy version (e.g. 1.84.x): 2020.2.3

I’m working on a self-paced reading task where I’m presenting sentences one word at a time and participants press ‘space’ to see the next word. I want to get the response times for each time ‘space’ is pressed for each word in the sentence. Right now, the response times are additive for each sentence, so by the last word in the sentence, the response time appears to be several seconds long. I’m very new to PsychoPy and have very limited Python experience and I’ve tried to apply some solutions from a few other posts (like this one and this one) but haven’t been able to figure it out yet. In my custom code component in the builder here’s what I have (a colleague helped with this):

Begin Routine

# Clear the buffer
event.clearEvents()

# Parse stimulus
words = sentence.split(' ')

#Create individual word stimuli
sentPos = 0
stimulus = words[sentPos]

# Create a clock
clock = core.Clock()

# Update stimulusDisplay before drawing to the screen
stimulusDisplay.setText(stimulus)

Each Frame

theseKeys = event.getKeys()

n = len(theseKeys)
i = 0

# Update stimulus on spacebar press
while n > i:
    if theseKeys[i] == "esc":
        core.quit()
        break
    elif theseKeys[i] == "space":
        thisExp.addData("readingRT", clock.getTime())
        thisExp.addData("sentPos", sentPos)
        thisExp.addData("keypress", theseKeys[i])
        thisExp.nextEntry()
        sentPos = sentPos + 1
        if sentPos == len(words):
            continueRoutine = False
            break
        stimulus = words[sentPos]
        i = i + 1

Does anyone know how I can get the response times relative to each word that’s presented, not relative to the first word in the sentence)? I’ve included an example of the data output in case I’m not being clear. Thanks so much in advance! :slightly_smiling_face:
Psychopy_test_stimuli.csv (762 Bytes) self-pacedDemo.psyexp (22.6 KB) test01_slef-pacedDemo_2020_Sep_25_1625.csv (26.0 KB)

This might not even need a Pythonic solution! You could transform the data in Excel after the fact like so:
image

But to save you doing that, you could implement it in Psychopy by replacing:

thisExp.addData("readingRT", clock.getTime())

with:

if sentPos == 0:
    # If this is the first word, use the time
    wordTime = clock.getTime() 
else:
    # If this is further in, use the time minus the time from the last word
    wordTime = clock.getTime() - thisExp.entries[-1]["readingRT"]
# Store time
thisExp.addData("readingRT", wordTime)

Thank you for the help! I tried the code but in my output there still seems to be an issue with recording the response times for each button press. I understand I can transform the data afterwards but I’m trying to save myself the frustration in the long run. I’ve uploaded the current experiment, an output file, and how I implemented the new code. Maybe I added it incorrectly?

Begin Routine

# Clear the buffer
event.clearEvents()

# Parse stimulus
words = sentence.split(' ')

#Create individual word stimuli
sentPos = 0
stimulus = words[sentPos]

# Create a clock
clock = core.Clock()

# Update stimulusDisplay before drawing to the screen
stimulusDisplay.setText(stimulus)

Each Frame

# Check for keys
theseKeys = event.getKeys()

n = len(theseKeys)
i = 0

# Update stimulus on spacebar press
while n > i:
    if theseKeys[i] == "esc":
        core.quit()
        break
    elif theseKeys[i] == "space":
        if sentPos == 0:
    # If this is the first word, use the time
          wordTime = clock.getTime() 
        else:
    # If this is further in, use the time minus the time from the last word
          wordTime = clock.getTime() - thisExp.entries[-1]["readingRT"]
# Store time
        thisExp.addData("readingRT", wordTime)
        thisExp.addData("sentPos", sentPos)
        thisExp.addData("keypress", theseKeys[i])
        thisExp.nextEntry()
        sentPos = sentPos + 1
        if sentPos == len(words):
            continueRoutine = False
            break
        stimulus = words[sentPos]
        i = i + 1

Psychopy_test_stimuli.csv (762 Bytes) self-pacedDemo.psyexp (22.9 KB) test02_slef-pacedDemo_2020_Sep_29_1054.csv (25.9 KB)