Python in Computational Neuroscience

By Byron Galbraith | December 20, 2010

Python (http://www.python.org/) is a programming language that has gained a significant amount of traction in the scientific computing community over the last few years. It combines the rapid prototyping and expressiveness of MATLAB with the power and object-oriented nature of C++ or Java. In the fields of Computational Neuroscience and Neuroinformatics, Python has seen its star rise especially fast. The journal Frontiers in Neuroinformatics dedicated a special topics issue to Python in 2008, with papers from several tool developers and integrators. At the recent 2010 SfN meeting in San Diego, a workshop was devoted to current state of many of these tools and initiatives with no sign that the pace is slowing down.

What is so exciting about Python in the Neurosciences? In this blog series, I will endeavor to cover all things Python as they relate to the field of Neuroscience. Topics will include single cell and microcircuit neural modeling, simulating large-scale neural networks, neurally-inspired robotics, Neuroscience education, and reviews of current tools and techniques. In general, articles will focus on how Python can be used to solve a particular computational problem, with emphasis placed on discussing actual code examples. The goal is for you to be able to take what is presented here and replicate it with minimal difficulty. The range of programming experience necessary is from beginner to advanced, though for many examples, all you will need to know are basic programming concepts and how to edit and run Python scripts.

Why Python?

I think Python is a great programming language, but it's not a panacea. Like most options out there, it does some things very well and some things not so well. Specifically, it is important to evaluate why you would make the jump from MATLAB to Python in the first place, especially if you are already fluent in MATLAB.

  • Python is free. The Python language is open-source and community driven. From a pragmatic perspective, this means that all the tools that we will be discussing are immediately available to you without the need to purchase software or obtain licenses. The downside to this is that while the core Python language team may handle rigorous platform testing and compatibility, the same is not necessarily true across the various packages and libraries provided by community developers. A feature that works out-of-the-box in MATLAB on any platform may only work with a certain version of Python running on Linux. The level of user interface polish in MATLAB also greatly exceeds most of what you can find in Python.
  • Python is a general purpose programming language. Unlike MATLAB, which is designed first and foremost to be a software package for numerical mathematics, Python is for more general computing like C++ or Perl. The benefits to this include a much wider range of features and options, more intuitive object-oriented programming ability, greater syntactical flexibility, and a much simpler ability to interact with existing C/C++/FORTRAN codes. On the other hand, because it's not an integrated, special purpose software package like MATLAB, it lacks features and tools taken for granted by MATLAB users. Attempting to replicate these features in Python can take hours.
  • Python is scalable. Python has a significant amount of support for distributed and parallel computing. MATLAB does have the Parallel Computing Toolbox, but it's another paid resource and licensing issues come into play when attempting to distribute jobs across cluster nodes.

In summary, MATLAB is an excellent tool for numerical computation with extensive toolbox support. It is also relatively expensive and constrained by its own environment. Python (with the right additional packages) gets you most of MATLAB's functionality, does so for free, and offers additional powerful programming features and external tool integration.

Getting Started

While some articles will discuss specific tools or packages, it is always assumed that you have Python installed with the Scipy and Matplotlib packages.

  1. Download and Install Python - Python can be obtained here. Download Python 2.7 and *not* Python 3.1. Due to backwards compatibility issues, most of the tools we will be using currently only work under Python 2.x. Don't worry though, 2.7 is up-to-date and actively maintained, so we aren't limiting ourselves to old technology.
  2. Download and Install SciPy - SciPy (Scientific Tools for Python) can be obtained here. It actually consists of two separate installations: NumPy and SciPy. NumPy (Numerical Python) is arguably the most useful library for scientific computing in Python, providing several MATLAB-style vector and matrix manipulation tools. SciPy proper provides a host of additional common scientific computing routine such as numerical integration, linear algebra, sparse matrices, and statistical functions. SciPy shares a lot in common with MATLAB, so experienced MATLAB users can transition relatively easily to Python, though there are some important differences. The NumPy for MATLAB Users guide provides an excellent resource for finding the Python way to implement several common MATLAB operations.
  3. Download and Install Matplotlib - Matplotlib can be obtained here and is a library for generating a wide range of 2D plots. It has syntactical similarity to MATLAB's plotting functions which, like SciPy, allows for rapid transition from one environment to the other.
  4. Explore! - If you are familiar with programming, but new to Python, the official Python Tutorial can get you up to speed while Google provides an online class with lecture videos and exercises. If you have limited or no programming experience, a list of several introductory tutorials can be found here. Finally, once you feel comfortable and want to dig deeper, the book Dive Into Python is an excellent free resource available here.

4 Responses to Python in Computational Neuroscience

  1. Apurv Mittal says:

    Hi
    I am currently doing research in action selection mechanisms based on the models of basal ganglia, and one very very good resource I found for models is on the following website:

    http://senselab.med.yale.edu/

    Quite a few models are coded using Python, which gives one more reason for people in computational neuroscience to learn Python 🙂

    An example of the Basal Ganglia model given by Benoit Girard is:
    http://senselab.med.yale.edu/ModelDb/showmodel.asp?model=124111

    Regards
    Apurv Mittal
    JRF, IIT Delhi

  2. neil says:

    It’s also worthwhile considering that the choices we make for our tools today are the choices made for our students in the future. Would you rather a research environment where your tools were closed to improvement, locked behind licences, only leased to you, and ultimately inscrutable, or one where the tools were developed and improved by the community?

    I’d also like to add that I’ve had the experience of teaching young biologists both matlab and python. Without a strong background in linear algebra, it is very difficult to get comfortable around matlab. Python is far easier to grasp.

  3. Nikita says:

    Actually, current version of NumPy 1.6.1 does not install with Python 2.7, and strictly requires Python 2.6.. 🙁

  4. Matt Wall says:

    Personally am hoping that some modern neuroimaging analysis tools will be written (or re-written?) in Python soon. I’d do it myself, but I’m a rubbish programmer and don’t have the time… ;o)

Leave a Reply

Your email is never published nor shared. Required fields are marked *

*

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>