Manually Coding Approximate Refresh Rate Python

Hi all,

I am trying to create a variable that calculates the approximate refresh rate and frame rate of my experiment. I’m trying to calculate those rates through a ‘frame_count’ variable, but it outputs values that are incorrect (e.g., ~9.00 for frame rate, and ~110 refresh rate). Can anyone spot something wrong with my code (pasted below)? Alternatively, is there a way to use ‘frameN’ as a way to output approximate frame and refresh rates?

Many thanks for the help!
Cait

Begin Routine

Start clocks

duration_clock = core.Clock()

Initialize the frame counters

frame_count = 0

Each Frame

if started == False and duration_clock.getTime() >= 1:
duration_clock.reset()
frame_count = 0
started=True
picture_stim.autoDraw = True
word_stim.autoDraw=True

if started == True and masked == False and picture_finished == False and (duration_clock.getTime()*1000 >= PICTURE_PRESENTATION_TIME_MS):
thisExp.addData(“picture_duration”, duration_clock.getTime()*1000)
picture_stim.autoDraw=False
picture_finished = True

if started == True and masked == False and (duration_clock.getTime() * 1000 >= WORD_PRESENTATION_TIME_MS):
thisExp.addData(“word_duration”, duration_clock.getTime()*1000)
word_stim.autoDraw = False
mask_stim.autoDraw = True
masked = True

if started == True and masked == True and (duration_clock.getTime() * 1000 >= WORD_PRESENTATION_TIME_MS + 100):
mask_stim.autoDraw = False

frame_count += 1

End Routine

Calculate average refresh duration

average_refresh_duration = (duration_clock.getTime()*1000)/frame_count
thisExp.addData(‘average_refresh_duration’, average_refresh_duration)

Approx frame duration

approx_frame_rate = 1000/average_refresh_duration
thisExp.addData(“approx_frame_rate”, approx_frame_rate)

Reset the stimuli

picture_stim.setAutoDraw(False)
word_stim.setAutoDraw(False)
mask_stim.setAutoDraw(False)

Reset the frame

frame_count = 0

I’m having a little trouble being 100% sure the math is what you want. I think this might be more straightforward:

approx_frame_rate = frame_count/(duration_clock.getTime()*1) # frames per second, e.g., 60
average_refresh_duration = 1000/approx_frame_rate # Should give you the refresh rate in ms

An alternative approach would be to have a clock that you checked on every frame to see how long has passed since the last frame and added that value to a list, then just average across the list at the end.

Begin routine

frame_count = 0
duration_clock = core.Clock()
frame_times = []

Each Frame

if frame_count > 0:
    frame_times.append(duration_clock.getTime())
    duration_clock.reset()

# all of your code controlling the display goes here

frame_count += 1

End routine

average_refresh_time = (sum(frame_times)/len(frame_times))*1000 # time/frame
approx_frame_rate = 1000/average_refresh_time

Something like that should work in principle, and will at least give you convergent information as to whether something is off in the results you’re getting now.