Wilbert's website at SocSci

has a subtitle

psychopy/02hello.html 2017-04-11

Donald E. Knuth: The computer will often say that you are wrong. But do not worry. It is not angry with you.

Programming with PsychoPy, 2 Hello World in PsychoPy

In this chapter we will explain your very first PsychoPy experiment line by line.

Experiment header, what will we use

Experiments in PsychoPy are really computer programs written in the Python programming language. To make sure that the computer understands this and understands which parts of PsychoPy to make available, we must start our experiment with a header.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The first line is called the hash bang. It helps operating systems 
# understand what to do with this script. The second line describes 
# the encoding of this file. UTF-8 is the only one used nowadays.
# It supports all alphabeths, even runes and linear B.

# Import the PsychoPy libraries that you want to use
from psychopy import core, visual

The very first line will tell operating systems that observe the POSIX convention that this computer program is using the Python language. The line starts with a # but is not a comment.

The second line, which is also not a comment, allows us to use characters that are not in the limited ASCII set, such as the € character.

The last line imports the submodules core and visual from the PsychoPy module. PsychoPy consists of 20 submodules. They are well documented: API documentation: http://www.psychopy.org/api/api.html

Stimulus Definition: What to present

For most experiments, we want to present visual or acoustic stimuli or a combination of both. It is also possible to present videos, although this topic is outside the scope of this course. Thus, what we need to do is to define visual and acoustic stimuli.

For a visual stimulus, we have a Window on which we present stimuli. This window can be a full screen, a window with borders around it, or a videowall the size of a football stadium. On this window we present some stimulus, for instance a text stimulus that says Hello World! In PsychoPy a visual stimulus definition looks like this:

# Create a window
win = visual.Window([400,300], monitor="testMonitor")

# Create a stimulus for a certain window
message = visual.TextStim(win, text="Hello World!")

As we can see, our window has the name win, it is 400 pixels wide and 300 pixels high. Our visual stimulus has the name message. it defines, what our stimulus consists of. In this case, it consists of the text Hello World!

Control over stimuli: When to present

Two more steps are required to present our stimulus on the screen.

# Draw the stimulus to the window.
message.draw()

# Flip backside of the window.
win.flip()

# Pause 5 s, so you get a chance to see it!
core.wait(5.0)

First we draw message on its window back buffer, then we flip the back and front buffer. The flipping is the moment that the text is really presented.

The function flip() puts the picture immediately on the screen. We would see it appearing, however, it wouldn’t stay there for long (just for a fraction of a second). We explicitly have to tell the program to keep on running without doing anything. Another built-in function doing exactly that is core.wait(). It keeps the program as it is and waits for the amount of seconds we command to wait.

Cleaning up, closing window and experiment.

Finally we close the window and close the experiment.

# Close the window
win.close()

# Close PsychoPy
core.quit()

Assignment 2: Putting it all together

Lets put it all together. Copy this experiment into PsychoPy and run it.

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# The first line is called the hash bang. It helps operating systems 
# understand what to do with this script. The second line describes 
# the encoding of this file. UTF-8 is the only one used nowadays.
# It supports all alphabeths, even runes and linear B.

# Import the PsychoPy libraries that you want to use
from psychopy import core, visual

# Create a window
win = visual.Window([400,300], monitor="testMonitor")

# Create a stimulus for a certain window
message = visual.TextStim(win, text="Hello World!")

# Draw the stimulus to the window. We always draw at the back buffer of the window.
message.draw()

# Flip back buffer and front  buffer of the window.
win.flip()

# Pause 5 s, so you get a chance to see it!
core.wait(5.0)

# Close the window
win.close()

# Close PsychoPy
core.quit()

It should give you the following stimulus:

Assignment 2

  1. Can you change the waiting time?
  2. Can you change the text? You can look up how to change the text of an existing stimulus in the API documentation: http://www.psychopy.org/api/api.html
  3. More difficult: Can you first present the original text stimulus, then change the text and present the new stimulus for 3 seconds?.

Continue with the next lesson