# Best pratice for presenting a stimulus during X ms

**URL:** <https://discourse.psychopy.org/t/best-pratice-for-presenting-a-stimulus-during-x-ms/9911>\
**Category:** Coding\
**Created:** [November 16, 2019, 5:36pm UTC](https://discourse.psychopy.org/t/best-pratice-for-presenting-a-stimulus-during-x-ms/9911 "2019-11-16T17:36:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servant\_Mathieu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/servant_mathieu/32/4637_2.png) [@Servant\_Mathieu](https://discourse.psychopy.org/u/Servant_Mathieu)\
**Post date:** [November 16, 2019, 5:36pm UTC](https://discourse.psychopy.org/t/best-pratice-for-presenting-a-stimulus-during-x-ms/9911/1 "2019-11-16T17:36:53Z")

</div>

Hi,  
I would like some advice about the best pratice to present a stimulus during X ms. I have used 2 methods in the past, and each seems to have advantages and drawbacks. In addition, PsychoPy has evolved considerably during the past 2 years, and there might be more efficient methods.

Imagine that we want to present a stimulus during 100 ms.  
Method 1: present the stimulus during X frames

```auto
framerate = win.getActualFrameRate()
frameDur = 1.0 /round(framerate)
resp = keyboard.Keyboard()
Nframes_100 = int(round(.1/frameDur))
for i in range (Nframes_100):
   stim.draw()
   win.flip()

```

Advantage: precision of presentation duration  
Drawback: if you loose some frames for whatever reason, timing can be bad.

Method 2: determine presentation duration with a clock:

```auto

resp = keyboard.Keyboard()
win.callOnFlip(resp.clock.reset)
win.callOnFlip(resp.clearEvents, eventType='keyboard') 
check = 0
While True:
    if check == 1:
        t = resp.clock.getTime()
        if t >= .1:
            break
   stim.draw()
   win.flip()
   if check == 0:
      check =1

```

Advantage : can maintain a reasonable timing, even if you drop frames

Would you then advise to use Method 2 ?  
Is there a way to refine the above code for Method 2 to increase the precision of presentation duration ?  
Thanks for the advice.

---

<div class="post-metadata">

**Author:** ![Michael](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/michael/32/16_2.png) [@Michael](https://discourse.psychopy.org/u/Michael)\
**Post date:** [November 17, 2019, 1:02am UTC](https://discourse.psychopy.org/t/best-pratice-for-presenting-a-stimulus-during-x-ms/9911/2 "2019-11-17T01:02:16Z")

</div>

Hi, yes, in most cases it is probably best now to use the timing approach, as it is more robust to frame-dropping issues. A good guide can be to look at how the latest version fo PsychoPy Builder (\>= 3.2.0) controls the timing of stimuli. In particular, search for `tThisFlip`, the estimated time of the next screen refresh, given by `win.getFutureFlipTime()`, and `frameTolerance`.

Frame counting can still be a useful approach in certain situations, for example if you want to guarantee that a stimulus (especially a very brief one) is actually displayed, despite something that may have intruded on keeping up with the overall absolute timeline.

---

<div class="post-metadata">

**Author:** ![Servant\_Mathieu](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/servant_mathieu/32/4637_2.png) [@Servant\_Mathieu](https://discourse.psychopy.org/u/Servant_Mathieu)\
**Post date:** [November 18, 2019, 12:01am UTC](https://discourse.psychopy.org/t/best-pratice-for-presenting-a-stimulus-during-x-ms/9911/3 "2019-11-18T00:01:06Z")

</div>

@Michael I just studied the code generated by the Builder (\> 3.2). Really cool. A few comments:  
Since the win.getFutureFlipTime() is computed as win.\_lastFrameTime + refreshInterval, it looks like this approach is not robust to frame-dropping issues. If you drop a frame, the win.getFutureFlipTime() estimate will be inaccurate right ? I am worried that this approach is essentially similar to the traditional frame counting approach. Why not using a simple timing approach like:

```auto
resp = keyboard.Keyboard()
win.callOnFlip(resp.clock.reset)
win.callOnFlip(resp.clearEvents, eventType='keyboard') 
check = 0
framerate = win.getActualFrameRate()
frameDur = 1.0 /round(framerate) 
While True:
    if check == 1:
        t = resp.clock.getTime()
        if t > (.1-frameDur/2.):
            break
   stim.draw()
   win.flip()
   if check == 0:
      check =1

```
