Hello Python

Previously, I made the decision to transition away from Matlab towards Python. Converting over to a new language is a big deal and there are many questions and issues to be addressed. In the future I will get a bit more detailed and discuss how I set up and use my Python environment – there seems to be many options here. But for now, I will just highlight the advantages of Python as I see it at the moment.

#1. It’s free. Any data/code I release, associated with a paper for example, can be examined by pretty much anyone who wants to do so.

#2. It is very popular and many people are actively developing cool stuff. Ranked by number of projects on GitHub and StackOverflow we can see that it’s among the big boys, with R and Matlab looking on enviously.


And here is a nice little talk about the rise in popularity of Python, and a discussion of its future.

#3. iPython. As far as I can tell, iPython is a completely novel way of producing, executing and sharing code. It is developed by scientists who want a more productive, open, and efficient workflow. In short, it allows you to write, document, and execute your code within your internet browser, complete with inline graphics. These form notebooks which can be highly readable documented code (with Markdown formatting of text and LaTeX equations, and embedded images and video) that other people can see and execute – ideal for sharing code and making your work transparent. I’ll write more about the advantages of this in the future, but see this short introductory video.

#4. There are complete, ready to use, reliable packages that will let me do what I want. This is not some sketchy half-working collection of beta software. The big ones that I know I will be using are:

SciPy, which is actually a collection of packages including some below, as well as a set of functions for file IO, statistics, linear algebra, optimisation, etc.

Numpy for fun with N-dimensional arrays.

Pandas, which provides more data structures, such as DataFrames which are used in R.

Matplotlib, for plotting. They have a great gallery of examples, and just taking a look you can find examples such as this that are very hard (if not impossible) to do in Matlab.

PyMC or PyStan for conducting MCMC inference with probabilistic models.

Some interesting things to a present/ex Matlabber


Modules and Packages.module is a file called something like myfunctions.py. And it could contain a number of different function definitions (as opposed to just one function per file in Matlab). I imagine this will bias your code to consist of more, but shorter functions. In order for Python to know about the module and the functions within, then you need to import the module (e.g. import myfunctions.py). The nice thing about a module is that it can also contain code that executes when it is imported. A good example would be to import dependencies.

As far as I can tell, packages are collections of multiple modules, and these could be loaded independently.

Modules and packages are good because it keeps things neat, tidy and which make loading dependent modules easier. This contrasts with the Matlab approach of just adding lots of paths to your environment. Good luck bundling up some code that you might want to send to someone and then setting up the paths on their machine.

More stuff, but for the future

And just by way of showing the breadth of directions this could go in, here are some toolboxes to use when I’ve got a bit more confidence with Python:

  • In terms of running psychophysics experiments, switch from Psychtoolbox (for Matlab) and use PsychPy.
  • Speed up computational intense code with either Numba or Cython or play with GPU computing with PyOpenCL.

Finally, Python has nothing to do with snakes. Its name was inspired from Monty Python.

Leave a Reply