URL of experiment:
Description of the problem:
Hi,
I have tried to upload my experiment to Pavlovia, but it does not work, I have to split a sentence into words, and measure time for some words, at this code : begin routine:
words = sentence.split()
i = 0
frameCounter = 0 #it all happens within the same routine so make our own counter
each frame:
Assuming you have loaded the current trial from your conditions file
Get all words from the columns like word_1, word_2, etc.
word_columns = [col for col in thisTrial.keys() if col.startswith(‘word_’)] # Dynamically fetch word columns
words = [thisTrial[word] for word in word_columns] # List of all words in the sentence
Initialize the currentWordIndex
currentWordIndex = 0
Initialize a list to store response times for each word
response_times = [None] * len(words) # Store response times for all words
Main loop to update the word display based on key press
while currentWordIndex < len(words): # Loop through all words
# Display the current word and get the time
text_desplay.setText(words[currentWordIndex])
text_desplay.draw()
win.flip()
start_time = core.getTime() # Start time when the word is shown
keys = event.waitKeys(keyList=['space', 'escape']) # Wait for key press
# If the spacebar is pressed, record response time for the current word
if 'space' in keys:
response_time = core.getTime() - start_time # Calculate response time
response_times[currentWordIndex] = response_time # Store the response time
currentWordIndex += 1 # Move to the next word
if 'escape' in keys: # To allow exiting the experiment early
core.quit()
After the loop, retrieve response times for all words
For example, you can save response times for target_word, after_word, last_word
thisExp.addData(‘TargetWordRT’, response_times[0]) # Assuming target is word_1 for example
thisExp.addData(‘AfterWordRT’, response_times[1]) # Assuming after word is word_2 for example
thisExp.addData(‘LastWordRT’, response_times[-1]) # Last word in the sentence
thisExp.nextEntry() # Move to the next entry in the data file
it worked well offline but not online, I got this error: Unfortunately we encountered the following error:
- ReferenceError: thisTrial is not defined
Try to run the experiment again. If the error persists, contact the experiment designer.
Thank you for your help!
Ok
Hello
Your error description is difficult to read because of its formatting. Surround code by triple `, e.g.
print
instead of
print
You surrounded your main loop code.
None of your code seems to be related to the error message. When do you use thisTrial? BTW, you cannot use a trial related variable, e.g. thisTrial.thisN, before the routine has started.
Best wishes Jens
hi,
can you help me detect the error, my experiment works offline but not online
Thank you!
Rola
בתאריך יום א׳, 27 באוק׳ 2024 ב-15:05 מאת Jens Bölte via PsychoPy <notifications@psychopy.discoursemail.com>:
(attachments)
asaf - Copy - 1.psyexp (14.7 KB)
sentences.xlsx.xlsx (8.92 KB)
Why do you have
text_desplay.setText(words[currentWordIndex])
text_desplay.draw()
win.flip()
in Each Frame when you could have a text component in the same routine and simply use text_display.text = words[currentWordIndex] ?
Avoid using win.flip() in Builder experiments. There is a flip at the end of each frame.
I tried to replace my code with your suggestion, but it didn’t work offline or online.
When I put the code in the same routine it doesn’t work offline, so I put it into a separate routine.
may you explain more please?
thank you!
Hello
@wakecarter has a self-paced reading example in PsychoPy Online Demos. You might to use this as a start for a word by word display with saving RT for each word.
The attached example is a simplified self-paced reading experiment.
SelfPacedShortVersion.psyexp (33.4 KB)
warmUp.xlsx (8.0 KB)
Both sources might give you some ideas how to implement a self-paced reading experiment.
You define trial as a column in your Excel-file and as routine. Avoid this, use unique names.
Best wishes Jens
While @JensBoelte was making a demo for you, I’ve also modified your code to something that words offline and should work online.
asaf - Copy - 2.psyexp (20.1 KB)
and a test data file
test2_asaf_2024-11-06_11h20.03.409.csv (2.2 KB)
You had a words list being created two ways. I’ve kept your more complex 5 word columns but the result is that the word is appears three times because two of the cells are blank.
I got this erro at pavlovia also: Unfortunately we encountered the following error:
- ReferenceError: thisTrial is not defined
Try to run the experiment again. If the error persists, contact the experiment designer.
may this because my psychopy version is not same as yours?
Thank you!
Hello,
the experiment didn’t work at my psychopy version so that I couldn’t sync to pavlovia. maybe I need another version ? mine is v.2021.1.4
Hello
No, this error
is not related to the PsychoPy-version. But you might want to update to either 2024.1.4 or 2024.2.4. anyway.
The error tells you that you refer to a variable called thisTrial, however, there is no such variable. Usually, such variable refers to a loop-name.
Best wishes Jens
You have Python code
word_columns = [col for col in thisTrial.keys() if col.startswith('word_')] # Dynamically fetch word columns
This may work locally but thisTrial isn’t understood by Pavlovia.
Personally I would go back to words = sentence.split()