We have created a script for our fMRI experiment where one part of it involves showing participants a story and each word of the story is displayed on the screen for 500ms. To create the words and display each of them on the screen I have written this code:
def display_story():
"""Display stories to participants."""
with open(story_path, mode='r', encoding='utf-8') as file:
story = file.read()
words = story.split()
# Prepare stimuli from the story words
word_list = [{'duration': 0.5, 'stimulus': word} for word in words]
# Display each word for 500ms
for word in word_list:
word_to_screen = psychopy.visual.TextStim(win, pos=[0, 0.01], text=word["stimulus"])
word_to_screen.draw()
# Show the text on the window
win.flip()
core.wait(word["duration"])
The problem is that, for a story with 480 words, the total time should be exactly 4 minutes (480 x 0.5s == 240s, 240s/60s == 4m) but when timing the code using python’s time function as well as a stopwatch the loop takes 4m 5s. I assume this is just an accumulation of the time it takes to run necessary code before each word is displayed but I’m wondering if there is a way to account for this extra run time in order to make the total time 4 minutes exactly.
Right now we have just reduced the duration to 0.49s instead of 0.5s but I am hoping there is a better way because this is not exact.