| Reference | Downloads | Github

Getting the time in each loop iteration

Hello all,

I’m sure this is a super easy question to someone that speaks Python, but sadly I am not a member of that group. I want to include timestamps in the output CSV file for each iteration of a loop to confirm that everything is running on the right time. (My colleague is having issues with this).

I have tried the following code element:
from psychopy import core
clock = core.getAbsTime()

This does not put anything in my output file. How can I get the results of this call into my output file?

Thank you!

You need to define what time that is. Time in current trial? Time in overall experiment? If the latter, what is the zero point and does it matter? Currently you are using core.getAbsTime(), the number of seconds since Jan 1970, which might be overkill.

You need to tell us what tab in the code component any code is in (i.e. does it run at the start of the experiment, start of the trial, etc)

 # not necessary. Builder already imports this:
from psychopy import core

# this returns a time value in seconds, not a clock object:
clock = core.getAbsTime()

Note that Builder already creates a few clocks for you, which you can query the time from:

globalClock = core.Clock()  # to track the time since experiment started

trialClock = core.Clock() # resets to zero at the start of each trial

You can add a time stamp to the data file like this:

thisExp.addData('time_at_some_point_of_interest', globalClock.getTime())`

but you really need to tell us more about exactly what times you want to record and when.

This is fantastic thank you very much!!!

I’m sorry I should have been more clear. The “time” in question is really the duration of each trial. My colleague has said that it is not coming out to the duration that it is supposed to be. I suspect that it is a problem in how he is measuring the timing of each trial length. The timestamps should determine whether this is correct or not.

OK, so in the End routine tab, put something like this:

thisExp.addData('trial_duration', t)

t is a time value updated by Builder on each screen refresh, starting at 0 from the first screen refresh in the trial.