Howdy, Stranger!

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

Supported by

comparing timestamps

Hello everyone!

I am trying to compare the time of a keyboard response with the begining of a trial. So i am using time.time() function to record the begining of my trial in ms (t0 = time.time()*1000) and i also have later t1 which is the time of the keypress (key, t1 = my_keyboard.get_key()). However i am a bit confused. The t0 value is a number similar to 1709552679902 while the t1 is like  25712. How can i compare these two?


Thank you in advance!

Rania

Comments

  • Hi @rania_tach,

    I believe that you are measuring time using different references. Your t1 is the response time measure by the keyboard object. The time.time() function in Python measures the current time in seconds since the Unix epoch, which is defined as 00:00:00 Coordinated Universal Time (UTC) on January 1, 1970. It returns a floating-point number representing the elapsed time since that epoch. Your 1709552679902 is a number of seconds and corresponds to 54 years, 228 days, 9 hours, 51 minutes, and 42 seconds.

    There are multiple ways of measuring time in Python. Make sure that you verify what each method is actually doing and measuring in reference to.

    Your can achieve what you're after by establishing time stamps using clock.time(). So, for example, you can establish a time stamps at the very beginning of a trial, and then right after the keyboard object, when a response has been registered. If you subtract the first time stamp from the second, you'll have the time elapsed in ms. Note that clock.time() works in the current version of Open Sesame 4.0 because the latter uses Python 3.6. The clock.time() method has been taken out of Python 3.8 and so you may prefer to use the time.perf_counter() function, which works in Python 3.6 and later (that way, if future implementations of Open Sesame upgrade to a later version of Python, your task should still work).

    Alternatively, a simpler method is to use the built-in time stamps of all objects in Open Sesame. You can, using a very simple line of code, calculate the time elapsed between the beginning of the trial and a response using something like this, where you place that calculation right after your keyboard event.

    I attach this basic example where I demonstrate both methods. You'll see that the time.perf_counter() method and that using the time_ property of objects in Open Sesame produce almost exactly the same values (but not exactly, because the time.perf_counter() method establishes the first time stamp when the trial sequence has started, while the previous method starts measuring the time elapsed from the onset of the trial's sequence. There is a difference of about 2 ms on my computer, but this may vary depending on the computer's speed to run the code. In any case, whether you chose one method or the other and stick to it, you shoudl get consistent performance.

    Hope this helps,

    Fabrice.

    Buy Me A Coffee

  • Dear Fab,

    Thank you so much for your response! I understand now the difference between those time measurements. Before trying time.time() function, i had also tried the clock.time() but i got this as a measurement this: <bound method Legacy.time of <openexp._clock.legacy.Legacy object at 0x000001BED70B5A50>>. I use opensesame 4.0 so as you mentioned it should work but i don't know what was the issue. Could it be the back end (i use expyriment)?


    Anyways, the time.perf_counter() function works perfectly so i will probably go with that. And also thank you for the examples and for the alternative method you proposed. I wasn't aware of it and i will definately use it in my experiments.


    Cheers,


    Rania

  • Hi @rania_tach,

    Glad I could help! Good luck with your experiment!

    Cheers,

    Fabrice.

    Buy Me A Coffee

Sign In or Register to comment.