| Reference | Downloads | Github

How to get reaction time within a frame by frame loop?

Hello everyone, how you all doing?

I have a (perhaps) simple question but I cannot find a solution!
I am coding a task similar to Posner, and I need to control not only the presentation of stimuli ( cue and target for example) by frames but also time intervals between these stimuli. This precise control is for a EEG experiment.

I just know how to do it by using a loop (using FOR or WHILE) frame by frame. But that means that if I put functions like clock or a mouse.reset or mouse.getpressed it will reset every frame (I think that is obvious). And I need to reset the mouse in the beginning of a stimulus presentation and to get the time of a button press from that presentation, without resetting every frame.

Can anybody help me?

(Just an observation: a function “core.wait(number of frames)” would be amazing if anyone feels like coding ;))

Here is the code for an one trial example:

from __future__ import print_function
from psychopy import visual, core, event, monitors

meuMonitor = monitors.Monitor('testMonitor')

win = visual.Window(meuMonitor.getSizePix(), monitor=meuMonitor,units='deg',fullscr=True,color=(-1, -1, -1))

mouse = event.Mouse(visible=False, win=win)

fixpoint = visual.Circle(win, fillColor=(1, 1, 1), units='deg', radius=0.1, pos=(0, 0))

grat1 = visual.GratingStim(win, tex='sqr', units='deg', pos=(11.5, -3.5), size=5, 
	sf=(1/0.53,0), phase = 0.50, color=(1.0, 1.0, 1.0), contrast=1.0, opacity=1.0)

grat2 = visual.GratingStim(win, tex='sqr', units='deg', pos=(-11.5, -3.5), size=5, 
	sf=(1/0.59,0), phase = 1, color=(1.0, 1.0, 1.0), contrast=1.0, opacity=1.0)

CueR = visual.Rect(win, fillColor=(1,1,1),width=1, height=1, pos=(0, 1)) #cue to indicate right
CueL = visual.Polygon(win, edges=3, radius=0.5, fillColor=(1,1,1), pos=(0, 1)) #cue to indicate left

StimDict = {
	'1': grat1,
	'2': grat2,
	'3': CueL,
	'4': CueR

# start trial

frameN = 0
press= False
while frameN <=250 and not press:

#for frameN in range(250):

    if 5<=frameN<250:#present fixation point for all frames

    if 50<=frameN<100:   #present cue for a different subset
        button = mouse.getPressed()
        if button != [0, 0, 0]:
        	press = True   # finish trial

    if 150<=frameN<200:  #present target for a different subset
        button,tresp = mouse.getPressed(getTime = True)
        if button != [0, 0, 0]:
        	press = True
        	print(tresp) #only gets time from current frame!!!
    win.flip()#flip the screen every frame


Thank you!!

Please enclose all of your code in triple backticks to make it more readable.

And also describe exactly what you want to achieve: you’ve only described the problem in generalities.

That’s a great idea!

As for your question: Shouldn’t it be possible to move the mouse.clickReset() call outside your loop so the timing doesn’t get reset each frame?

Hi Michael,
thanks for the tip about the code, didn’t know about the backtick.

Sorry I wasn’t specific about my problem…
I would like to get the reaction time from a response (mouse press) counting from the presentation of a stimulus.
For example, in my code a stimulus (grat1) is presented from the 150th to 200th frame (around 833 msec in a 60Hz refresh rate). I want to be able to get the time a mouse button is pressed counting from the first frame this stimulus was presented (150th frame).
For this I think I would need to reset the mouse from the beginning of the stimulus presentation and let the time count continuously until a button is pressed.
The idea of the coding is to know, for example, how long it took for a person to respond to a grating stimulus presented in the left side of the screen.

Is that more clear? Does that make sense?

@jan.freyberg Thanks for your suggestion! But putting the mouse.clickReset() outside the loop would only get the reaction time from the beginning of the trial, and I would like to get the reaction time from each stimuli presentation or from a specific stimulus.


In your case you can simply add a conditional to test for something that will only happen once (i.e. exactly on frame 150).


if frameN == 150:

In your case, your conditionals are tested serially so this doesn’t interfere with the other conditional statements, such as if 150<=frameN<200:.

Yeah! It works perfectly!
Thank you!