Introducing PsychoPy
Objectives
- Be able to use psychopy to open a window and draw text to the screen.
- Understand how scripts are executed and how psychopy can be used to control the flow of execution.
In this lesson, we are going to introduce the key components of how we use psychopy to implement and control vision science experiments. We will go into these components in more detail in future lessons, but here we will take a quick look at their basic qualities.
The first step is to make the additional functionality that is provided by the psychopy package available to our code.
We do this by using the import
statement, as usual.
The psychopy package is broken up into a number of subpackages; initially, we will just be interested in the visual
subpackage.
import psychopy.visual
A critical component of a vision science experiment is the drawing window. This is a representation of the screen to which we can draw stimuli and control the environment. We will be looking more into windows in the next lesson, but here let’s define a window that is 400 pixels high and 400 pixels wide and is white in colour. We do that by:
import psychopy.visual
win = psychopy.visual.Window(
size=[400, 400],
units="pix",
fullscr=False,
color=[1, 1, 1]
)
As you can see, we specify the size
via a two-item list, which sets the horizontal and vertical extent, respectively.
We then tell psychopy that we have specified such values in units of pixels.
We then set the argument fullscr
to the boolean value False
; if this was True
(the default), the size parameter would be overridden and the window would occupy the full screen.
This is desirable when actually running experiments, but while we are learning it is useful to just have it in a small window.
Finally, we set its colour (note the American spelling of colour) to white.
Colour in psychopy can be specified in a few different ways, but we will use the RGB format.
Here, each item in the list specifies how much the red, green, and blue components (respectively) of the display should be driven, from -1 to +1.
Tip
You will also notice in the above some slightly different formatting. Many of the psychopy functions take quite a few arguments, and it can get unwieldy to specify them all on one long line. Hence, a useful strategy to improve readability is to specify the arguments on different lines. Note however that the arguments are indented one level “under” the opening and closing parentheses. This tells Python that the arguments “belong” to this function.
Now that we have created our window, let’s create a basic stimulus—some text.
We can do that using psychopy’s TextStim
data type.
Most of its default arguments are fine for our purposes; we will just tell it about our window and what text we would like it to display:
import psychopy.visual
win = psychopy.visual.Window(
size=[400, 400],
units="pix",
fullscr=False,
color=[1, 1, 1]
)
text = psychopy.visual.TextStim(win=win, text="Hello, world!")
Now we have made our text data type, we prepare to show it by using its draw
function.
To actually display what we’ve drawn in the window, we then have to “flip” the window (more on this next lesson).
Finally, we close the window.
import psychopy.visual
win = psychopy.visual.Window(
size=[400, 400],
units="pix",
fullscr=False,
color=[1, 1, 1]
)
text = psychopy.visual.TextStim(win=win, text="Hello, world!")
text.draw()
win.flip()
win.close()
If we run the above code, however, it is difficult to see the results of our handiwork. This is because the Python statements execute sequentially, and once we have drawn our stimulus the program finishes.
Tip
You may see a message in the output window with a warning about a monitor specification. This refers to the (quite useful) functionality in psychopy for managing the physical characteristics of monitors. It can be safely ignored, in this context.
What we need is a way to interrupt our program’s flow.
We can use functionality in psychopy’s event
package to achieve this.
Specifically, we will use a function called waitKeys
, which will halt the execution of our program until you press a key on the keyboard.
import psychopy.visual
import psychopy.event
win = psychopy.visual.Window(
size=[400, 400],
units="pix",
fullscr=False,
color=[1, 1, 1]
)
text = psychopy.visual.TextStim(win=win, text="Hello, world!")
text.draw()
win.flip()
psychopy.event.waitKeys()
win.close()
If we run the above code, we can see that the window stays open while waiting for our keypress.
However, we can’t see any text.
This is because the default colour of text is white—so we can’t see it on our white window background!
We can fix this by giving a color
argument to TextStim
.
import psychopy.visual
import psychopy.event
win = psychopy.visual.Window(
size=[400, 400],
units="pix",
fullscr=False,
color=[1, 1, 1]
)
text = psychopy.visual.TextStim(
win=win,
text="Hello, world!",
color=[-1, -1, -1]
)
text.draw()
win.flip()
psychopy.event.waitKeys()
win.close()