{ "metadata": { "name": "", "signature": "sha256:af2e35feac78e826e64c2754c44049da93dc293fb29f4586280506c058657394" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![Python + GestaltReVision logo](logo.png)" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Python for Vision Research" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welcome to the introductory series to using [Python in vision research](http://gestaltrevision.be/wiki/python/python) brought to you by the [GestaltReVision](http://gestaltrevision.be) group (KU Leuven, Belgium).\n", "\n", "The series consist of seven IPython notebooks meant as a three-day crash course for vision researchers in programming with [Python](http://python.org), building experiments with [PsychoPy](http://psychopy.org) and [psychopy_ext](http://psychopy_ext.klab.lt/), learning the fMRI multi-voxel pattern analysis with [PyMVPA](http://www.pymvpa.org/), and understading image processing in Python.\n", "\n", "There are more extensive resources for vision scientists on [our GestaltReVision wiki](http://gestaltrevision.be/wiki/python/python).\n", "\n", "Please report any bugs or share ideas on our [GitHub repo](https://github.com/gestaltrevision/python_for_visres/issues)." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Available tutorials" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. [An introduction to Python](Part1/Part1_Intro_to_Python.ipynb)\n", "2. [Introduction to PsychoPy for creating experiments](Part2/Part2_PsychoPy.ipynb)\n", "3. [Transitioning from MATLAB to Python](Part3/Part3_Scientific_Python.ipynb)\n", "4. [More practice with PsychoPy](Part4/Part4_Practice_with_PsychoPy.ipynb)\n", "5. [Streamline research with psychopy_ext](Part5/Part5_psychopy_ext.ipynb)\n", "6. [Multi-voxel pattern analysis](Part6/Part6_MVPA.ipynb)\n", "7. [Natural image statistics](Part7/Part7_Image_Statistics.ipynb)" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "License" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Authors:** Maarten Demeyer, [Jonas Kubilius](http://klab.lt) \n", "**Year:** 2014 \n", "**Copyright:** Public Domain as in [CC0](https://creativecommons.org/publicdomain/zero/1.0/) (except where noted otherwise). Feel free to reuse these materials for your teaching or any other purposes." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Contents of this notebook" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- [What you'll need](#What-you'll-need)\n", " - [Tutorial code](#Tutorial-code)\n", " - [Python and its packages](#Python-and-its-packages)\n", " - [Windows](#Windows)\n", " - [Linux](#Linux)\n", " - [Mac OS](#Mac-OS)\n", " - [What? I don't want to mess with my machine!](#What?-I-don't-want-to-mess-with-my-machine!)\n", "- [Checking your installation](#Checking-your-installation)\n", "- [Getting started](#Getting-started)\n", " - [Opening IPython notebooks](#Opening-IPython-notebooks)\n", " - [Windows](#Windows)\n", " - [Mac OS / Linux](#Mac-OS-/-Linux)\n", " - [Then:](#Then:)\n", " - [Editing Python scripts](#Editing-Python-scripts)" ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "What you'll need" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Tutorial code" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "All materials, including this notebook, are available [here](http://github.com/gestaltrevision/python_for_visres)." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Python and its packages" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Windows" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The easiest method is to [install our own Python distribution](https://drive.google.com/file/d/0B8XUj38c04GBbThuc0V6eG9maEU/edit?usp=sharing) that has all necessary packages included." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Linux" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Linux user? Awesome! Go to the [NeuroDebian](http://neuro.debian.net/) 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 `` and ``sudo pip install ``, as listed below:\n", "\n", "```bash\n", "sudo apt-get install python-pip python-numpy python-scipy python-imaging python-matplotlib\n", "sudo apt-get install psychopy spyder ipython-notebook\n", "sudo apt-get install python-pandas python-docutils python-nibabel python-mvpa2 python-h5py\n", "sudo pip install pillow\n", "sudo pip install seaborn svgwrite\n", "sudo pip install psychopy_ext\n", "```" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Mac OS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unfortunately, we have a very limited in-house Mac experience. We do offer instructions [how to install most packages](http://gestaltrevision.be/pdfs/workshops/python_summer_school_Mac.pdf) but you may (or are quite likely to) run into problems and we will probably not know how to help you." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "What? I don't want to mess with my machine!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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](http://gestaltrevision.be/wiki/python/ndvm).\n", "\n", "Alternatively, if you are only interested in Scietific Python and or PsychoPy (sessions 1-4 and 7), the [Standalone PsychoPy installation](http://sourceforge.net/projects/psychpy/files/PsychoPy/) will largely suffice. You will not have IPython installed, but all examples should be working otherwise.\n", "\n", "For even more options, [check our wiki](http://gestaltrevision.be/wiki/python/pythoninstall)." ] }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Checking your installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Run the following cell (by pressing the \u25b6 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." ] }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "\n", "os.system('python check_install.py')\n", "\n", "try:\n", " f = open('workshop_req_check.txt','r')\n", " for line in f.readlines():\n", " print line,\n", " f.close()\n", "except IOError:\n", " print 'The script could not be executed!'" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "win32\n", "['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']\n", "\n", "===============================================================================\n", "MODULE CHECK\n", "\n", " Python: base installation\n", " OK\n", " Spyder: IDE\n", " OK\n", " NumPy: numerical computing\n", " OK\n", " SciPy: scientific functions\n", " OK\n", " Matplotlib: plot graphs\n", " OK\n", " PsychoPy_ext: streamline research\n", " OK\n", " Seaborn: statistical data visualization\n", " OK\n", " Docutils: documentation utilities\n", " OK\n", " Svgwrite: create svg images\n", " OK\n", " Pandas: data analysis toolkit\n", " OK\n", " NiBabel: access neuroimaging files\n", " OK\n", " h5py: store huge amounts of numerical data\n", " OK\n", " PyMVPA: fMRI MVPA package\n", " OK\n", " Pillow: handle images\n", " OK\n", " PsychoPy: build experiments\n", "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.\n", " import compiler\n", "\n", "*************************************************************\n", "* *\n", "* A new window will open. Please follow instructions on it. *\n", "* *\n", "*************************************************************\n", "\n", "0.0716 \tWARNING \tCreating new monitor...\n", "0.0718 \tWARNING \tCreating new monitor...\n", " OK\n", " IPython: interactive notebooks\n", "\n", "*************************************************************\n", "* *\n", "* An IPython notebook should open in your browser. *\n", "* Please wait for this test to finish. Do not hit Control-C *\n", "* *\n", "*************************************************************\n", "\n", " OK\n", "\n", "===============================================================================\n", "HOW WELL ARE YOU PREPARED?\n", "\n", "Session: Introduction to Python\n", " FULLY PREPARED\n", "\n", "Session: Introduction to PsychoPy\n", " FULLY PREPARED\n", "\n", "Session: Transitioning from MATLAB to Python\n", " FULLY PREPARED\n", "\n", "Session: More practice with PsychoPy\n", " FULLY PREPARED\n", "\n", "Session: Streamline research with psychopy_ext\n", " FULLY PREPARED\n", "\n", "Session: Natural image statistics\n", " FULLY PREPARED\n", "\n", "Session: Multi-voxel pattern analysis\n", " FULLY PREPARED\n", "\n", "===============================================================================\n", "WHAT TO DO NOW?\n", "\n", "1. Check in the list above how well you're prepared for the sessions\n", " you signed up.\n", "2. Ideally, you should be fully prepared. Mostly prepared might\n", " still suffice but not everything may work. Minimally prepared means\n", " you will not be able to execute significant parts of the code.\n", "3. If you're underprepared, download and install missing packages,\n", " and rerun this script. You may find information at\n", " http://gestaltrevision.be/wiki/python/check_install useful.\n", "4. A file `workshop_req_check.txt` was generated in the same folder\n", " where this script is. When ready, please **email** it to \n", " so that we can verify that \n", " you're ready for the workshop.\n", "\n", "===============================================================================\n", "\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Getting started" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Opening IPython notebooks" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Windows" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Open command-line (``Start button > Type cmd > Enter``)\n", "2. Navigate to the folder where the IPython notebook is using the ``cd`` command\n", " e.g. ``cd C:\\Users\\u000001\\Documents\\python_for_visres``.\n", " \n", " *Tip:* if you need to switch partitions, first type partition letter (no ``cd``) and hit enter, e.g., ``D:``" ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Mac OS / Linux" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Open command line (look for ``Terminal.app`` or `xterm`, or similar)\n", "2. Navigate to the folder where the IPython notebook is using the ``cd`` command, e.g. ``cd ~/python_for_visres``." ] }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Then:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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).\n", "2. Click on the notebook you want to open and play with." ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Editing Python scripts" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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:\n", "\n", "- Old school:\n", " - [Spyder](https://code.google.com/p/spyderlib/) - Matlab-like environment, good for beginners.\n", " - [Gedit](https://wiki.gnome.org/Apps/Gedit) with plugins\n", " - [NinjaIDE](http://ninja-ide.org/)\n", " - [Geany](http://www.geany.org/) (Linux/Windows)\n", " - [Notepad++](http://notepad-plus-plus.org/) (Windows)\n", " - [Textmate](http://macromates.com/) (Mac)\n", " - [Kod](http://kodapp.com/) (Mac)\n", " \n", "- Modern:\n", " - [SublimeText](http://www.sublimetext.com/)\n", " - [Zed](http://zedapp.org/)\n", " - [Atom](https://atom.io/)\n", " - [Brackets](http://brackets.io/)\n", " - [LightTable](http://www.lighttable.com/)" ] } ], "metadata": {} } ] }