| Reference | Downloads | Github

Fixed stimulus duration chosen randomly


I am new to Psychopy, I have a question about inserting a jitter for a fixation point. It is important to note that while the fixation point must be jittered, there are a fixed number of presentation times that must be used. For example, if there are 5 trials in a block, the fixation presentation time would need to be randomly chosen from a list of 5 times (2s, 2.5s, 3s, 3.5s, 4s). These would be exactly the same for each participant, but the order would be random.
In this experiment, each trial looks like this: Fixation (text file) -> Image with a response required -> wait -> Fixation… and so on.
Suppose the Excel file guiding the block of trials looks like this:

Fixation Image CorrectResponse
2.0 A left
2.5 B right
3.0 C left
3.5 D right
4.0 E left

The values in the fixation column are times in seconds. In the Builder view, the duration of the fixation point (which is a text object) is set to $Fixation. Doing this causes problems because Image A will always have a 2 second fixation preceding it, Image B a 2.5 second fixation preceding it, and so on.
I want the routine to randomly choose a value from the Fixation column for each fixation duration, while keeping the Image/Correct Response columns tied to each other (but still chosen independently from the Fixation column). I am certain I will need to do this in Coder view, but am unsure how to do it. Any help would be appreciated. Thank you.


The easiest way to get independent randomisation of that variable is to remove it from your conditions file and handle the randomisation in code instead. i.e. insert a Code Component on the routine with the fixation stimulus and in the “Begin Routine” tab of its window, put something like this:

if your_loop_name.thisN == 0: # only on the first trial
    jitters = [2.0, 2.5, 3.0, 3.5, 4.0] # create the list
    shuffle(jitters) # randomise its order

current_jitter = jitters.pop() # extract one entry on each trial
thisExp.addData('jitter', current_jitter) # record in the data for this trial

Ensure that the code component appears above your fixation stimulus (so that the variable is produced before it needs to be accessed), and then you can simply insert this in its duration field:



Worked like a charm. Thanks for the help.


I actually have a follow up question. The randomization code works as intended, but I have one more small issue. Between the offset of the random fixation point and the onset of the image, I need a 300ms delay that is just a blank screen. I have an object that does this (a 300ms blank screen), but because each fixation has a random duration, I don’t know how to set the onset of the blank screen to correspond to the offset of each random fixation. Any help would be appreciated. Thank you.


There are several alternatives:

  1. Split the trial into several consecutive routines, then each stimulus can have an onset time of zero, automatically following on from the previous one.
  2. Put the stimuli in the same routine, and explicitly specify their start time as well as the duration. ie the first stimulus has a constant onset of 0 and a duration of current_jitter, so the next stimulus has a variable onset of current_jitter and a constant duration of 0.3 while the last has an onset time of current_jitter + 0.3, and so on.