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

License

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

Windows

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

Linux

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

os.system('python check_install.py')

try:
    f = open('workshop_req_check.txt','r')
    for line in f.readlines():
        print line,
    f.close()
except IOError:
    print 'The script could not be executed!'
win32
['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\\python27.zip', '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']

===============================================================================
MODULE CHECK

    Python: base installation
        OK
    Spyder: IDE
        OK
    NumPy: numerical computing
        OK
    SciPy: scientific functions
        OK
    Matplotlib: plot graphs
        OK
    PsychoPy_ext: streamline research
        OK
    Seaborn: statistical data visualization
        OK
    Docutils: documentation utilities
        OK
    Svgwrite: create svg images
        OK
    Pandas: data analysis toolkit
        OK
    NiBabel: access neuroimaging files
        OK
    h5py: store huge amounts of numerical data
        OK
    PyMVPA: fMRI MVPA package
        OK
    Pillow: handle images
        OK
    PsychoPy: build experiments
C:\Miniconda32\lib\site-packages\psychopy-1.80.06-py2.7.egg\psychopy\preferences\configobj.py:28: 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...
        OK
    IPython: interactive notebooks

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

        OK

===============================================================================
HOW WELL ARE YOU PREPARED?

Session: Introduction to Python
    FULLY PREPARED

Session: Introduction to PsychoPy
    FULLY PREPARED

Session: Transitioning from MATLAB to Python
    FULLY PREPARED

Session: More practice with PsychoPy
    FULLY PREPARED

Session: Streamline research with psychopy_ext
    FULLY PREPARED

Session: Natural image statistics
    FULLY PREPARED

Session: Multi-voxel pattern analysis
    FULLY PREPARED

===============================================================================
WHAT TO DO NOW?

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
   http://gestaltrevision.be/wiki/python/check_install useful.
4. A file `workshop_req_check.txt` was generated in the same folder
   where this script is. When ready, please **email** it to 
   <Maarten.Demeyer@ppw.kuleuven.be> so that we can verify that 
   you're ready for the workshop.

===============================================================================

Getting started

Opening IPython notebooks

Windows

  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 Terminal.app or xterm, or similar)
  2. Navigate to the folder where the IPython notebook is using the cd command, e.g. cd ~/python_for_visres.

Then:

  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: