Clocks and trial counters§

1

Clocks§

Keeping track of time is really important to most experiments. In PsychoPy there are many useful clocks that live “under the hood”, which we can use in our experiment:

2

Making custom clocks§

If we want to use a custom clock (e.g. to measure the time across several routines) we can always make a clock wihin a code component:

myClock = core.Clock()

Note

If we are working online we will need to change our code component “type” to be “Both” and use myClock = util.Clock() on the JS side - this is because the ‘Clock’ class lives in different modules in PsychoPy and PsychoJS.

3

Useful methods for use with clocks§

Once we have our clock there are several useful methods we might want to know about.

4

Adding a timer to your experiment§

Now that we know how to make a clock and how to access the time on it, we can easily add a timer to our experiment to show our participant how far into a trial, or the experiment they are.

5

Adding a timer to your experiment§

Add a text component to your trial routine and position it in the top right corner (in height units you will want something like pos = (0.4,0.4). Then in the text field all we need is to write $t and set every frame.

Note

Depending on your PsychoPy version you might need to use t - omiiting the dollar sign. Remember that you only need a $ at the start of a field if there is not already a $ int he parameter name.

6

Adding a timer to your experiment§

The properties of the text component should look like this:

../../_images/timer_properties.png
7

Adding a timer to your experiment§

OK now you should have a timer to show participants how far through a trial they are!! If you wanted to show them how far into the experiment they are you could add a code component and in the Begin Experiment tab write expClock = core.Clock(). Then in your text component replace t with expClock.getTime().

8

Ending a trial early§

Imagine we want to end the experiment early if your participant takes too long to respond (this might be particularly important for online studies!). Now we know how to time each trial we can use those clocks to end the task early if a trial takes too long.

9

Ending a trial early§

Add a code component and in the “Each Frame” tab write:

threshold = 10 # number of seconds before we end the experiment
if t > threshold:
        continueRoutine = False # end the current routine
        trials.finished = True # exit the current loop
10

Ending a trial early§

If we have any routines that follow this we will also want to make sure we end those too, so we might also want to extend this code a little:

threshold = 10 # number of seconds before we end the experiment
endTask = False
if t > threshold:
        continueRoutine = False # end the current routine
        trials.finished = True # exit the current loop
        endTask = True

Then add a code component to all routines following this and int he “Begin Routine” tab type:

if endTask:
        continueRoutine = False

This way all following routines would also be ended if the participant took too long to respond.

11

Trial counters§

12

How is PsychoPy counting trials?§

Each loop we add to our experiment will automatically be counting the number of trials that have occured already (as well as how many repetitions of our trials list have occured!). Run your experiment and have a loop at the output. You will see some useful information in the headers of your file.

13

How is PsychoPy counting trials?§

14

Adding a trial counter§

Now we know how PsychoPy counts trials, we can use this info to add a trial counter and show how far through the experiment participants are. Add a text component and position it in the top left (in height units pos = (-0.4, 0.4)). In the text field add $trials.thisN.

15

trial counters online§

Currently (PscyhoPy version 2021.1.4) the trial counter variables exist in PsychoPy but not PsychoJS (but they will do soon!). Instead, we can use our own custom trial counters. Add a code component and in the “Begin Experiment” tab type trialCounter = 0, then in the “Begin Routine” tab type trialCounter += 1. Finally, replace the text in your text component with $trialCounter and set every repeat.

16

Quick Exercise§

Try to end the experiment on trial 5 using a code component.

17

What next?§

We can make more flexible and dynamic experiments using code, including:
18