PsychoPy



In this example we’ll practice using audio stimuli and recording as well as utilizing the useful getDuration() function. Often in a shadowing task the goal is to determine which features or language varieties are most salient by asking people to mimic or repeat what they hear. In this example, we’ll be doing just that.

First, create a slide for instructions using the skills you’ve learned in the previous examples. Then, create a new routine called “shadowing” where we’ll be doing the bulk of the work. It helps to first think about how you want the stimuli presented chronologically and then build. Here’s the order we’ll be using

  1. A short blank screen of 0.5s for transitions
  2. A sound of some duration will play
  3. When the sound ends, a message saying “Please Repeat” will appear
  4. Also, the microphone will turn on at this point
  5. 1 second after the text/mic start, the space bar will start so that participants can move to the next trial.

PsychoPy is an open-source package for creating experiments in behavioral science. It aims to provide a single package that is: precise enough for psychophysics; easy enough for teaching; flexible enough for everything else; able to run experiments in a local Python script or online in JavaScript. Stimuli that move with the mouse¶. It’s the easiest thing in the world to make a stimulus appear at the location of the mouse: add a Mouse Component (let’s call it mouse). Set the position of your stimulus to be at mouse.getPos and update on every frame. As an open source project, PsychoPy has evolved far beyond its original purpose, to the point where it can now interact with a variety of different hardware devices (e.g., eye-trackers, button boxes) and produce online web-based experiments.

  • To meet these goals PsychoPy provides a choice of interface - you can use a simple graphical user interface called Builder, or write your experiments in Python code. The entire application and library are written in Python and is platform independent. There is a range of documentation at.
  • PsychoPy is a free cross-platform package allowing you to run a wide range of experiments in the behavioral sciences (neuroscience, psychology, psychophysics, linguistics.) This is a community project. Users have all the source code. Users are the developers.

You can easily create a blank screen by adding an Image component and changing the size to [2,2].

Next, click on the Sound component and change the following information:

  • Start time – 0.5
  • Stop duration (s) – (leave blank)
  • Sound – $audioStim
  • Sound – set every repeat

Then, create a conditions file like in the previous example. This time, we’ll just have one variable “audioStim”. Each row should contain the file path (absolute or relative) to an individual wav file. Add a loop around shadowing using the conditions file. Make sure to change nReps to 1 (or however many reps you’d like).

Now comes the difficult part, getting components to start directly after the sound file finishes playing. If all of our sound files are the exact or similar duration, we could just use an estimate for defining start and end times. However, a better way is to use the duration of each sound file dynamically.

To demonstrate the usefulness of this, create the text component, leaving the Stop (duration) blank and adding the text “Please Repeat” to the text box. Then we’ll put the expression $sound_1.getDuration() + 0.5 in the Start time (s) field. Notice that we’re using the name of the component, not the variable $audioStim. Your sound component might be named something different (sound_2, etc).

Do the same thing for the microphone component, leaving Stop duration blank and defining the start time as $sound_1.getDuration() + 0.5.

Then, add a key response to end the routine, making sure that stop duration is blank, “Force end of routine” is checked, and the start time is defined as $sound_1.getDuration() + 1.5. Your builder window should look like the figure below:

You’ll notice that the duration/time bars for the text, mic, and key responses are not shown. This is because their start times are being defined by the variable $sound1.getDuration(). If you’d like to add estimate bars for visual completeness, you can add expected values in the Expected Start (s) and Expected Duration (s) field of each component.

Once you’ve run through the experiment, all the wav files you’ve recorded will be stored in a folder inside the data folder.

In the next example we’ll be examining how to hack conditional branching in order to give feedback on a participant’s responses.

Back to (2. PsychoPy Tutorial: Loops and Trials – Lexical Decision Task)

Go to (4. PsychoPy Tutorial: Conditional branching)

PsychoPy is an experiment development program and Python library. It is free and open source software with a community contributing to its development and maintenance. The experiments are written in Python, which is a popular general-purpose programming language with a great deal of information to support users with any issues. These properties make it a strong alternative to proprietary programs.

To install PsychoPy, you should first decide whether you want to install the standalone builder or the Python library.

The standalone builder is available for Windows and MacOSX. It allows you to build the experiments without installing any additional dependencies. It is based around a graphical user interface (GUI) that allows you to drag-and-drop elements of your experiment without needing to code everything yourself, though it also has a code editor for fine-tuning. It is the recommended option if you do not already have a Python development environment set up on your computer and do not have immediate plans to set one up.

The Python library is available for all operating systems. It is a lighter-weight alternative which is recommended if you already have Python installed on your computer, or want to get one set up now. The library does include a GUI builder (though it takes a couple extra steps to make the GUI readily accessible), but also allows you to build or edit your experiments in the text editor of your choice.

Standalone builder: Windows & MacOSX

Go to the PsychoPy installation page and download and run the installer for your operating system.

Python library

There are a few options for installing the Python library. If you are using Linux, your distribution might have a package ready for you to install. Otherwise, jump to Other systems.

Debian-based systems (e.g., Debian, Ubuntu, Linux Mint)

Add the NeuroDebian repository by following the instructions for your system on the NeuroDebian website. Remember to run sudo apt-get update after you have added the repository.

Then, install PsychoPy by running:

Arch-based systems (e.g., Arch Linux, Manjaro)

Install PsychoPy from the Arch User Repository by running

This will install PsychoPy to /usr/bin/psychopyApp.py. It should include a desktop file, meaning that you should be able to find it in your dmenu. But, you will not be able to run the builder from your terminal by typing psychopy. If you wish to do so, you should add an alias to your rcfile (e.g., ~/.bashrc or ~/.zshrc):

(the -b flag means 'run the builder').

Psychopy3

Other systems

You will need Python 2.7 on your computer in order to install the Python library. The easiest way to install and manage Python and all of the dependencies for PsychoPy is to use Anaconda Python. Even if you already have Python on your computer, you will likely want to install Anaconda Python, because not all of PsychoPy’s dependencies are available on PyPI.

Psychopy Sound File

Installing Anaconda Python

To install Anaconda Python, go to the Anaconda downloads page and download and run the installer for your system. You can choose either Python 2.7 (recommended if you do not plan on using Python outside of PsychoPy) or Python 3.x (recommended if you do plan on using Python for other purposes); you will be able to run a Python 2.7 environment either way.

Installing the PsychoPy library

PsychoPy

Once you have Anaconda Python installed on your computer, you will need to create an environment for PsychoPy.

Open a command prompt (Windows) or a terminal (Mac/Linux) and enter the following:

which means:

Psychopy Builder

  • conda create: use (Ana)conda to create a Python environment
  • -n psychopy: named 'psychopy'
  • --channel https://conda.anaconda.org/CogSci: in addition to the normal package sources, look in the 'CogSci' channel for the packages I need
  • psychopy pyglet wxpython pygame: install these packages and any other packages that they depend on
  • python=2.7: use Python 2.7 in this environment

Starting PsychoPy

To start the PsychoPy builder, enter the following in your terminal/command prompt:

PsychoPy
Mac/Linux

Then, when you are done, enter:

to leave the PsychoPy environment.

Windows

and when you are done, leave the environment by entering:

Running a PsychoPy script

If you have a PsychoPy experiment script that you would like to run, you do not need to open the builder to do so. You can run it from the command line.

First, open a command prompt or terminal.

'Change directories' (cd) into the folder containing the script file by entering cd <folder-path>. For example, cd C:UsersYOURNAMEDocumentsExperiments or cd /home/Users/YOURNAME/Documents/Experiments. (Hint: you can type the first few letters of each part and then hit the tab key to complete it).

Then start up your PsychoPy environment by entering activate psychopy (Windows) or source activate psychopy (Mac and Linux).

Then you can run the script by entering python experiment.py (replace experiment.py) with the actual file name.

When you’re done working on it, enter deactivate (Windows) or source deactivate (Mac or Linux) to leave the PsychoPy environment.

Psychopy Tutorial

Troubleshooting

  • When you try to run the builder, if you installed the Python library, you might get errors that some packages are not installed. You can install these by entering conda install packagename or pip install packagename.

  • If the builder (with any installation) consistently crashes when you start it, there may be a problem with the configuration files. You can try editing them in a text editor, or you can delete them completely—they will be regenerated the next time you start the program, hopefully not containing any issues that would cause PsychoPy to crash. (See PsychoPy’s troubleshooting page for more information.) On Mac and Linux, those files can be found:

And on Windows: