psychopy.org | Reference | Downloads | Github

Self-paced Reading - Collecting IRI values

OS (e.g. Win10): Windows 10
PsychoPy version (e.g. 1.84.x): 2020.1.3
Standard Standalone? (y/n) If not then what?: yes
Hi all,

I was trying to build a self-paced reading task from scratch. To do so, I followed all the instructions from the thread created on the Google Forum and the older posts on this forum. I inserted the code in the required tabs to mask the sentences and collect the inter response interval (IRI) every time the subject presses the space bar. Then, I tried to run the experiment: everything worked fine, except for the collection of IRIs. In my data file, the only registere IRI value is called IRI_0, and I think it refers to the first time the space bar is pressed, at the very beginning of every loop. I created an Excel file which contains 3 cells, each of them containing one sentence, all under the same column called “sentence”. Here’s the code I inserted in the trial Routine:

*Begin Experiment Tab*

# now define a function which we can use here and later on to replace letters with '-': 
def replaceWithdash(textList, currentWordNumber):
    
    dashSentence = ''
    for index, word in enumerate(textList): # cycle through the words and their index numbers 
        if index != currentWordNumber: 
            dashSentence = dashSentence + '-' * len(word) + '-' # add two strings of dash characters
        else:
            dashSentence = ' ' + dashSentence + word #except for current word
            
    return dashSentence # yields 
*Begin Routine Tab*

# remove any keypresses remaining in the buffer from 
# before  this routine started:
event.clearEvents()

#this breaks your sentence's single string of characters into a list of individual
#words, e.g. 'The quick brown fox.' becomes ['The', 'quick', 'brown', 'fox.']
sentenceList = sentence.split()

#keep track of which word we are up to:
wordNumber = -1 #-1 as we haven't started yet

# now at the very beginning of the trial, we need to run this function for the 
# first time. As the current word number is -1, it should make all words '-'. 
# Use the actual name of your Builder text component here: 

sentences.text = replaceWithdash(sentenceList, wordNumber)


# In the Builder interface, specify a "constant" value for the text content, e.g. 
# 'test', so it doesn't conflict with our code.
*Each Frame Tab*

keypresses = event.getKeys() # returns a list of keypresses

if len(keypresses) > 0: # at least one key was pushed

    if 'space' in keypresses:

        thisResponseTime = t #the current time in the trial
        wordNumber = wordNumber + 1
        if wordNumber < len(sentenceList):
            
            if wordNumber == 0: #need to initialise a variable
                timeOfLastResponse = 0
                
                #save the inter response interval for this keypress,
                #in variables called IRI_0, IRI_1, etc:
                thisExp.addData('IRI_'+str(wordNumber), thisResponseTime - timeOfLastResponse)
                timeOfLastResponse = thisResponseTime
                
                #update the text by masking all but current word
            sentences.text = replaceWithdash(sentenceList, wordNumber)
        else:
            continueRoutine = False #time to go on to the next trial

    elif 'escape' in keypresses:

        core.quit() # I think you'll need to handle quitting manually now.

Can somebody help me, please? I’m kind of stuck at this point :sweat_smile:
Thank you in advance!

Edit: so far I’ve tried making small changes in the code I inserted in the movingWindow Routine, but without improvements for what concern the collection of the reaction time for single words. I can’t understand how to register the rt every time I press the space bar while reading, and to print them in single columns in the output data file.
If it helps, attached to this post you can find the template I’m working on and a CSV file containing the data collected during one run of the experiment.

Again, thank you to whoever will help me!

Martina

_movingWindow_2020_Mar_17.csv (1.7 KB) movingWindow.psyexp (17.0 KB) movWin_stim.xlsx (8.4 KB)

Hi, the issue is that the code to save the IRI_x variable in the data file is embedded inside a check that only runs if it is the first word (i.e. if wordNumber == 0:). So that addData() call needs to be moved outside that check, so that it gets executed on every keypress.

This looks like code that I might have written originally, but either I was having a vary bad day, or it seems to have been edited quite a bit, because as well as that logic error, there are lines in it that contradict each other (e.g. re-setting timeOfLastResponse on nearby lines). Could you point to the original post that you got that code from?

Hi Michael, thank you so much for your answer!
I just started to learn how Python and its syntax work, so I apologize in advance if I’m going to ask you and the other users silly questions :slight_smile:

So, the code I posted here has some differences from the original code you wrote back in 2015.
First of all, I implemented your code to my template following the instructions you gave @paulaluegi on the Google Forum. Everything worked fine on the visual side, but not for the collection of the IRI.
Then, I searched some more information here on the forum, because I wanted to display a line of dashes to cover the words without revealing the number of letters of each word of the sentence. I followed the instructions I found in the threads started by @tjmccormick5 https://discourse.psychopy.org/t/self-paced-reading-space-doesnt-always-move-window/4075 and @Kyung https://discourse.psychopy.org/t/how-do-i-get-moving-window-presentation-in-self-paced-reading-task/2250. I edited my code and tried to run the experiment: again, I achieved my goal on the visual side by masking all the words, but I couldn’t collect any IRI value except for the IRI_0.

I think that I definitely wrote something wrong while editing my code :sweat_smile:

Again, thank you for your help Michael, I really appreciate it!

OK, the important thing is that in Python, white space (i.e. indentation) has semantic meaning. In your code above, the thisExp.addData() line is indented so that it appears within the if wordNumber == 0: check. That also means that the timeOfLastResponse = thisResponseTime line immediately counteracts the timeOfLastResponse = 0 above it.

So you need to “de-indent” those two lines by four spaces each leftwards, so that they are no longer encompassed within the if wordNumber == 0: clause. That way, those two lines will run for each key press, not just the first.

1 Like

Great, I see what I did wrong! I “de-indented” the lines like you told me to do and now everything is working fine, I can collect the IRIs (see the attached .csv file) :smiley:
I learned the lesson, and I will continue to study Python to improve my skills :muscle:

Thanks again Michael!

_movWindow_2020_Mar_19.csv (1.5 KB)