Python + GestaltReVision logo

Python for Vision Research

Welcome to the introductory series to using Python in vision research brought to you by the GestaltReVision group (KU Leuven, Belgium).

The series consist of seven IPython notebooks meant as a three-day crash course for vision researchers in programming with Python, building experiments with PsychoPy and psychopy_ext, learning the fMRI multi-voxel pattern analysis with PyMVPA, and understading image processing in Python.

There are more extensive resources for vision scientists on our GestaltReVision wiki.

Please report any bugs or share ideas on our GitHub repo.

Available tutorials


Authors: Maarten Demeyer, Jonas Kubilius
Year: 2014
Copyright: Public Domain as in CC0 (except where noted otherwise). Feel free to reuse these materials for your teaching or any other purposes.

Contents of this notebook

What you'll need

Unfortunately, getting Python and all necessary packages is one of the major difficulties for beginners. So please be patient and try to get everything in order because once you get it running, magic can start.

Tutorial code

All materials, including this notebook, are available here.

Python and its packages


The easiest method is to install our own Python distribution that has all necessary packages included.


Linux user? Awesome! Go to the NeuroDebian website, follow the instructions on how to add their repositories, and just sit back and relax while your system installs packages through sudo apt-get install <package_name> and sudo pip install <package_name>, as listed below:

sudo apt-get install python-pip python-numpy python-scipy python-imaging python-matplotlib
sudo apt-get install psychopy spyder ipython-notebook
sudo apt-get install python-pandas python-docutils python-nibabel python-mvpa2 python-h5py
sudo pip install pillow
sudo pip install seaborn svgwrite
sudo pip install psychopy_ext

Mac OS

Unfortunately, we have a very limited in-house Mac experience. We do offer instructions how to install most packages but you may (or are quite likely to) run into problems and we will probably not know how to help you.

What? I don't want to mess with my machine!

If you are having troubles installing Python and its packages, or you just don't want to change anything in your current setup, an excellent alternative is to install the NeuroDebian Virtual Machine. This will create a Linux desktop environment within your operating system, allowing you to follow the much simpler Linux installation procedures instead. Moreover, this Linux environment will be entirely separate from any other Python installation you may have on your machine, so you won't mess anything up by trying. Detailed instructions can be found here.

Alternatively, if you are only interested in Scietific Python and or PsychoPy (sessions 1-4 and 7), the Standalone PsychoPy installation will largely suffice. You will not have IPython installed, but all examples should be working otherwise.

For even more options, check our wiki.

Checking your installation

Run the following cell (by pressing the ▶ button above or Ctrl+Enter) to check whether your computer contains all needed packages. This will generate a txt output file which you can e-mail to us, should a problem arise. Press a key when asked to, and close the new IPython Notebook window that will be opened. If the cell keeps running for up to 10 seconds after closing the notebook, just wait; this is normal.

In [2]:
import os


    f = open('workshop_req_check.txt','r')
    for line in f.readlines():
        print line,
except IOError:
    print 'The script could not be executed!'
['D:\\Dropbox\\Destymas\\python_for_visres', 'C:\\Miniconda32\\lib\\site-packages\\pymvpa2-2.3.1-py2.7-win32.egg', 'C:\\Miniconda32\\lib\\site-packages\\tabular-0.1-py2.7.egg', 'C:\\Miniconda32\\lib\\site-packages\\psychopy-1.80.06-py2.7.egg', 'C:\\Miniconda32\\', 'C:\\Miniconda32\\DLLs', 'C:\\Miniconda32\\lib', 'C:\\Miniconda32\\lib\\plat-win', 'C:\\Miniconda32\\lib\\lib-tk', 'C:\\Miniconda32', 'C:\\Miniconda32\\lib\\site-packages', 'C:\\Miniconda32\\lib\\site-packages\\PIL', 'C:\\Miniconda32\\lib\\site-packages\\win32', 'C:\\Miniconda32\\lib\\site-packages\\win32\\lib', 'C:\\Miniconda32\\lib\\site-packages\\Pythonwin', 'C:\\Miniconda32\\lib\\site-packages\\setuptools-3.6-py2.7.egg', 'C:\\Miniconda32\\lib\\site-packages\\wx-3.0-msw']


    Python: base installation
    Spyder: IDE
    NumPy: numerical computing
    SciPy: scientific functions
    Matplotlib: plot graphs
    PsychoPy_ext: streamline research
    Seaborn: statistical data visualization
    Docutils: documentation utilities
    Svgwrite: create svg images
    Pandas: data analysis toolkit
    NiBabel: access neuroimaging files
    h5py: store huge amounts of numerical data
    PyMVPA: fMRI MVPA package
    Pillow: handle images
    PsychoPy: build experiments
C:\Miniconda32\lib\site-packages\psychopy-1.80.06-py2.7.egg\psychopy\preferences\ DeprecationWarning: The compiler package is deprecated and removed in Python 3.x.
  import compiler

*                                                           *
* A new window will open. Please follow instructions on it. *
*                                                           *

0.0716 	WARNING 	Creating new monitor...
0.0718 	WARNING 	Creating new monitor...
    IPython: interactive notebooks

*                                                           *
* An IPython notebook should open in your browser.          *
* Please wait for this test to finish. Do not hit Control-C *
*                                                           *



Session: Introduction to Python

Session: Introduction to PsychoPy

Session: Transitioning from MATLAB to Python

Session: More practice with PsychoPy

Session: Streamline research with psychopy_ext

Session: Natural image statistics

Session: Multi-voxel pattern analysis


1. Check in the list above how well you're prepared for the sessions
   you signed up.
2. Ideally, you should be fully prepared. Mostly prepared might
   still suffice but not everything may work. Minimally prepared means
   you will not be able to execute significant parts of the code.
3. If you're underprepared, download and install missing packages,
   and rerun this script. You may find information at useful.
4. A file `workshop_req_check.txt` was generated in the same folder
   where this script is. When ready, please **email** it to 
   <[email protected]> so that we can verify that 
   you're ready for the workshop.


Getting started

Opening IPython notebooks


  1. Open command-line (Start button > Type cmd > Enter)
  2. Navigate to the folder where the IPython notebook is using the cd command e.g. cd C:\Users\u000001\Documents\python_for_visres.

    Tip: if you need to switch partitions, first type partition letter (no cd) and hit enter, e.g., D:

Mac OS / Linux

  1. Open command line (look for or xterm, or similar)
  2. Navigate to the folder where the IPython notebook is using the cd command, e.g. cd ~/python_for_visres.


  1. Type ipython notebook and hit enter. A new tab on your default browser should open with notebook choices listed. Doesn't work properly? Make sure you're not using an outdated or ridiculous browser (like some old Internet Explorer).
  2. Click on the notebook you want to open and play with.

Editing Python scripts

Python scripts are just text files. If you want, you can open them using any text editor, even Notepad. However, it is best to use specialized text editors for the task because they help you to code. Although in this tutorial we rely exclusively on IPython to write and run our scripts, in real life people usually use other text editors or integrated developments environments (IDEs) to create and run their scripts. Here is a list of some of our favorites: