Computer Programming

for Experimental Psychologists

Launching multiple experiments from one script


Problem description

In this post I will address the situation where you have multiple experiments, each one implemented in a different script, and you want to run all these experiments for a single participant. In that case, there are two issues that you will face:

1. You want to use the same PsychoPy window for each of the experiments

2. You want to provide the participant id only once and use it in each of the experiments

One solution would be to combine your experiments in a single script, where you provide the id at the top of your script. This works, but it is not the most flexible way if you still want to occassionaly run an experiment in isolation.

An alternative approach is to see your individual experiments as modules. These modules can be imported in a 'main' script where you first get the id of your participant and then pass this id on to each of the individual experiments.

Step 1: Adapt the individual experiment

The first thing you do is make sure that each experiments works in isolation. That way you know that if any errors pop up while implementing the procedure that I'll describe next are not related to your experimental code. For this demonstration I will assume that you have two experiments that are in the files experiment_1.py and experiment_2.py

When the experiment works, the first thing you do is encapsulate your experiment in a function. All the code that follows your import statements can be wrapped in a function with the following signature:

def run_experiment(win, subject_id):
   # The code for your experiment

There are two parameters here: win, which refers to the PsychoPy window that you will be using, and the subject_id. If you start from a working experiment, the PsychoPy window will of course still be defined within the function. The next step is therefore to remove this line from your code. If at this point you run your script again, nothing will happen. This is because you have only defined a function in your script, but you are not calling that function. To make sure that the experiment still runs if you run the script itself, add the following lines to the bottom of your script:

if __name__ == '__main__':
   win  = visual.Window()
   subject_id = 12
   run_experiment(win, subject_id)

A Python script can be executed in two different ways: it can be run as the main program, which is when you run the script directly, or it can be imported as a module. The if-statement in this code can be used to check if you are executing the script itself. In that case, you create a PsychoPy window, define a subject id, and pass that information to the function that you created.

Step 2: Running multiple experiments

After following this procedure for your second experiment, it is time to create a main script. A minimal main script would look as follows:

# Import statements
from psychopy import visual
import experiment_1
import experiment_2

# Run all experiments
win = visual.Window()
subject_id = 12
experiment_1.run_experiment(win, subject_id)
experiment_2.run_experiment(win, subject_id)

Note how we are now importing our experiments as if they were modules. We then create the necessary variables (a window and a subject id), that are needed in each of these modules. Finally, we call the run_experiment() function from each of these modules, passing on the variables that we just created.

This is just a minimal example of the approach and could be extended in different ways. For example, your main script could use the PsychoPy window to provide some general instructions before running each of the experiments, and can also be used to present information between each experiment.