#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

# How to accurately calculate reaction time in Python?

Hello, guys!

I hope you're doing amazing.

Today I have a problem with the reaction time calculation. I have read in several comments that it is enough just doing a subtraction between "t2" which is often the kb.get_key() and "t1" which is often "canvas_1.show()". Meaning the time when the key is registered minus the time when the 1st stimulus appears. However, doing this calculation over and over again, I obtain just the duration of my two canvases regardless of the real reaction time.

As you can see in this screenshot, I have: (a) one canvas lasting 695ms, (b) a second canvas (which is just a white mask) lasting 239, and (c) my kb_response with a timeout = 100. With this, participants should have 1,034ms available to respond. The thing is that when I do the subtraction t2 - t1, I always obtain around 934ms meaning the duration of my two canvases. Normally I should obtain between 400ms and 600ms.

I have already tried to give 339ms to my second canvas and 0ms to my kb_response timeout, but it is always the same.

In the 1st screenshot, you can see the code and in the second one, you can see the subtraction.

Another thing is that pressing the key should not make the next stimulus appear. It means that all the canvases should last what I want regardless of the participant's reaction time. But, I have the impression that pressing the key shortens the displaying time of my canvases.

• Hi @Gio38000,

I don't program in Python but looking at your code, there are things I'm unsure of. If you want to measure response times from the onset of Canvas 1, it is odd to define the keyboard event after canvases 1 and 2 and to give a time out of just 100ms (that means your subject has just 100 ms to respond from when the keyboard object is activated). I don't really understand the logic of the code you're presenting or the structure of your trial. Could you possibly upload your task or a short version of it?

You might want to go back to the basics explained on this page: https://osdoc.cogsci.nl/3.3/manual/python/keyboard/. See if you can measure a simple RT to a single canvas, then work your way up to build your trial with the additional elements.

Hope this helps.

Fabrice.

• Hi Gio,

Yes, Fabrice is right. You're logic doesn't check out.

I have read in several comments that it is enough just doing a subtraction between "t2" which is often the kb.get_key() and "t1" which is often "canvas_1.show()"

Yes, that is the correct approach to measure the time between two events. Of course there are some things to consider to guarantee RTs to be as accurate as possible, but they are not relevant here.

They main issue with your code is its linearity. If you have a `clock.sleep` statement, your experiment will halt and nothing else can happen, i.e. also no response. Same goes for your 100 ms timeout in the keyboard response. In addition, only during those 100 ms a response can be processed. So, in your example, the quickest RT possible is, big surprise, 934 ms!

To achieve what you described you have to poll responses all the while your canvas presentation lasts. So, canvas and keyboard have to act in parallel. With linear code that is not possible. With a while loop it is pretty easy though. See the example script attached. I think this should get you started. If not, let me know.

Good luck,

Eduard

• Hello,

@eduard your script was very useful. But, there are a couple of things I barely understood. I know that what you suggested is the correct thing to do, I'll try many other options and yours is the more optimal. But I would like to understand better. For instance, you stated :

t0 = cv1.show()

while clock.time() - t0 < total_timeout:

if clock.time() - t0 < timeout_1:

cv1.show()

I thought that "clock.time()" recorded the amount of time an event takes and that "t0 = cv1.show" recorded the moment (time) when an event occurs. Am I wrong? If not, shouldn't I state firstly the beginning of "clock.time()" ? Perhaps this is a silly question, but the mechanisms behind clock.time and also clock.sleep are not that obvious to me.

Then you stated.

elif clock.time() - t0 < (timeout_1 + timeout_2):

cv2.show()

Here I wonder why you stated "timeout_1 + timeout_2", instead of using "total_timeout", a variable that you had previously set.

Thanks once again.

• Hi Gio,

I thought that "clock.time()" recorded the amount of time an event takes

No, `clock.time()` returns the current time (relative to the start of the experiment). So it tells you when something happens, not how long it lasted. To get a duration you have to have two time stamps and then take the difference.

and that "t0 = cv1.show" recorded the moment (time) when an event occurs

That is true. Both functions (clock.time(), and cv.show()) return the same Object, a time stamp.

Here I wonder why you stated "timeout_1 + timeout_2", instead of using "total_timeout", a variable that you had previously set.

That wasn't necessary maybe. It essentially gives you the option to have a third phase of responding when both stimuli had disappeared. I based that on your description in the first post (I believe). But sure, if you want your trial to be over one the second stimulus disappears, you could replace timeout_1 + timeout_2 with total_timeout.

Does that make sense?

Eduard