Presenting stimuli and randomising interstimulus interval for a duration of 40 seconds

Hello everyone,

First time using psychopy but looks reallly cool!

I would like to present a 0.2s stimulus every 6-10 seconds for a total duration of 40 seconds (the duration of a video I am showing on screen).

Now I created the 6-10 seconds stimulus presentation randomiser by creating a code object and including the following code

interstimulus_interval_randomiser_audio = randint(low = 6, high = 10) # choose a value
thisExp.addData('interstimulus_interval_randomiser_audio', interstimulus_interval_randomiser_audio) # record it in the data file

I then call $interstimulus_interval_randomiser_audio at the start box of my auditory stimulus object. My question is how do I loop this stimulus presentation->randomised ISI (6-10s)->stimulus presentation->… for the duration of my video object (40 seconds video)

Hi There,

You are on the right lines for sure! What makes this a bit tricker is that you want a component to be presented multiple times within the same routine as a the video. So, there are 2 solutions:

  1. add multiple sound components each with its own predetermined onset
  2. do this in code (which I think is the approach you might prefer here!)

For solution 2, you would use the code you have but with a few more considerations. On each frame within the routine we want to detect a) is the tone currently being presented and b) are we currently waiting for a tone to be presented c) if neiter a nor b let’s pick an interval to wait then present the tone (and do this continuously whilst our trial is going on in the background.

Since there are a couple of bits of code here, here is a demo to help you on your way. A tone is presented every 3-5 seconds on a lop for the duration of a 10 second long trial.

toneRandom.psyexp (9.2 KB)

Hope this helps,

1 Like

Fantastic, thank you very much Becca for your help! I’ve now modified your code to the ISI and trial duration that is applicable to our experiment. Final question, what lines of code should I add for doing the exact same thing but with a picture stimulus?

For a picture, you would replace the initial creation of the sound component with an image (ImageStim — PsychoPy v2021.2) and replace instances of .play() with .draw() (you also wouldn’t need things like .stop() for an imagestim.

Hope this helps!


PS. As you mention you are new to psychopy, heres something that might be useful. If you want to learn more about the code underlying a component or python in general. Make it in builder, then click compile to code Screenshot 2021-01-21 174202 then search the name of your image component in the coder window that opens! More details here Extending Builder with code — Workshops for PsychoPy 2020 2020

Thank you very much Becca for your help! Got everything to work as intended except for a new issue where the video I’m playing is always infront of the image (brief distractor) I’m trying to display. Any idea how to fix this?

Make sure your video is above your image in the components within the routine (right click > move up). If it isn’t this it’s probably a different issue, so please could you mark the solution for this thread then start a new topic :blush:


Hi Becca,

Thanks so much for this code!

I was able to use your code to present a text stimulus multiple times within the same routine as a video running for 30 seconds. However, I only want the text stimulus to appear twice randomly in the 30-second video sequence. Is there a way to change your code so the text-stim only appears randomly twice during the video sequence?

Hi There,

If you only want something to happen twice I would use a variable to count how many times something occurred and then present no more if that number is met.


Begin Routine tab:

count = 0

Each Frame Tab:

line 15 (last screenshot):

count += 1

indent lines 6 to 24 and add at the start:

if count < 2:

Hi Becca,

Amazing thanks! It works! I just had to put:

count += 1

on line 25 (on the screenshots above). Otherwise, the Fixation wouldn’t draw.