About Python

In OpenSesame you can create complex experiments using only the graphical user interface (GUI). But you will sometimes encounter situations in which the functionality provided by the GUI is insufficient. In these cases you can add Python code to your experiment.

Python in the OpenSesame GUI

Screencast

Video 1. A screencast that the demonstrates the basics of using Python inline script in OpenSesame.

A single Python workspace

All Python code is executed in a single Python workspace. This means that variables that have been defined in one inline_script are accessible in all other inline_scripts, as well as in Python statements that are embedded in run-if statements and text strings. The same principle applies to modules: once imported, they are available everywhere.

For example, you can simply construct the Canvas in one inline_script ...

my_canvas=Canvas()my_canvas.fixdot()

... and show it in another inline_script ...

my_canvas.show()

Inline_script items

In order to use Python code you need to add an inline_script item to your experiment. You can do this by dragging the Python icon (the blue/yellow icon) from the item toolbar into the experiment sequence. After you have done this you will see something like Figure 1.

Figure 1. The inline_script item.

As you can see, the inline_script item consists of two tabs: one for the Prepare phase and one for the Run phase. The Prepare phase is executed first, to allow items to prepare for the time-critical run phase. It is good practice to construct Canvas objects, Sampler objects, etc. during the Prepare phase, so that they can be presented without delay during the Run phase. But this is only convention; you can execute arbitrary Python code during both phases.

Loop tables and conditional ("if") statements

You can use single-line Python statements also where you would normally type static values, or would use the OpenSesame square-brackets notation to indicate values (i.e. [my_var]). To do so, you need to prefix an =. For example, you can use the following Python script as a run-if statement (see also Figure 2):

=var.correct==1andvar.response_time<1000

Figure 2. Using Python script in the run-if statement of a sequence item.

Similarly, you can use single-line Python statements to define variables in loop tables. Let's say that you want to assign a random value between 0 and 1000 to a variable. You could this by first importing the random in an inline_script. Once the random module is available, you could use random.randint() to obtain a random variable in a loop item:

=random.randint(0,1000)

Figure 3. Using Python script to define variables in a loop table.

Python in text strings

You can embed Python statements in text strings using the [=...] syntax. For example, you could the following text to a sketchpad:

The resolution is [=var.width] x [=var.height] px

Depending on your experiment's resolution, this might evaluate to:

The resolution is 1024 x 768 px

The IPython debug window

OpenSesame reroutes the standard output to the debug window, which you can activate using Control + D or through the menu (Menu -> View -> Show debug window; see Figure 4). You can print to the debug window using print():

print('This will appear in the debug window!')

If available, the debug window is an IPython terminal. IPython is a powerful interactive Python terminal. If IPython is not available, a simple fallback terminal will be used.

Figure 4. The debug window.

Things to know

Common functions

Many common functions are directly available in an inline_script item, without the need to import anything. For example:

# `Canvas()` is a factory function that returns a `Canvas` objectfixdot_canvas=Canvas()ifsometimes():# Sometimes the fixdot is greenfixdot_canvas.fixdot(color='green')else:# Sometimes it is redfixdot_canvas.fixdot(color='red')fixdot_canvas.show()