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.
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.
While some articles will discuss specific tools or packages, it is always assumed that you have Python installed with the Scipy and Matplotlib packages.
- 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.
- 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.
- 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.
- 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.