I want to print my trial onset time to my logfile. However, I need to write to the logfile within a while (timer) loop, which means what whatever I do in that loop will be done for every screen refresh.
The problem is that I only want to write the result of the first clock.getTime()
call to the logfile. If I do this:
while timer.getTime() >0: # while time isn't up (turns neg when time's up)
for key in event.getKeys():
if key in ['escape']:
core.quit() # quit if they press escape
timeText.draw(window)
timeline.draw(window)
cursorImage.draw(window)
## flip so it actually appears
window.flip()
OnsetTime = clock.getTime()
logfile.write('OnsetTime, %s' % OnsetTime)
I get a bunch of lines of my logfile that say 'OnsetTime' and the time - one for every refresh.
I only want the first one to be printed, but I'm not sure how to do that.
This is just another way of doing what CasualDemon's proposing, but one which I think is more elegant (three lines of code for the logging instead of 5):
def logOnsetTime():
"""Function which allows launching this code right after a window.flip()"""
logfile.write('OnsetTime, %s' % clock.getTime())
window.callOnFlip(logOnsetTime) # runs on first flip
while timer.getTime() >0: # while time isn't up (turns neg when time's up)
for key in event.getKeys():
if key in ['escape']:
core.quit() # quit if they press escape
timeText.draw(window)
timeline.draw(window)
cursorImage.draw(window)
## flip so it actually appears.
window.flip()
If you want a log for every keypress, put the window.callOnFlip(logOnsetTime)
inside the while loop. There's also a window.logOnFlip
method specifically for logging, but that just saves an input string to the log, timestamped to a global clock, so it wouldn't save the time of your clock
.