Drag and drop files into a GUI using python and pyside

As an illustration, I've added a simple example of the python / pyside code for dragging an image file onto (a very simple) GUI. There are a number of examples on the web but I could find a simple demo of dragging and dropping files in a cross platform way.

This code includes a workaround for OSx because newer versions don't play that well with dragging and dropping in Qt4 and pyside doesn't support Qt5 (it's on the way)

Obviously a python installation, pyside and Qt4 is required. On OSx I would recommend homebrew for installing python and Qt.

To workaround the issues with dragging and dropping on OSx with Qt4, NSURL is required. For this pyobjc is required. OSx specific install:

Installation notes

There are common small issues that come up during my work that could be useful to note down


Generally python libraries can be easily installed in a virtualenv using pip but certain libraries have been problematic. This post just provides a list of packages that I've had problems with and the workarounds

1) Matplotlib

Matplotlib in virtualenv - Ubuntu 12.04 
  • Installing matplotlib requires python-tkinter to be installed for the standard backend to be used. No pip version of tkinter available
  • Install matplotlib before pyside
Matplotlib in virtualenv - Ubuntu 14.04

Error: No window showing
Issue: The default tk backend requires the tk libraries to be present
  • Install: python-tk, tk, tk8.6-dev
  • pip install matplotlib

2) Mayavi

Mayavi in a virtualenv - Ubuntu 14.04

Installing Mayavi in a virtualenv doesn't seem to be able to find the python vtk libraries
# Install python-vtk library
sudo apt-get install python-vtk
# Copy library to virtualenv
cp -r /usr/lib/pymodules/python2.7/vtk /PATH/TO/YOUR/VIRTUALENV/lib/python2.7/site-packages
# Sorted
pip install mayavi

Mayavi should work fine now in the virtualenv. This great workaround was reported by hennadii-maden at Stackoverflow

3) numba

numba - Ubuntu 14.04
(llvmlite 0.5.0, numba 0.19.2, llvm 3.5)
sudo apt-get install zlib1g zlib1g-dev libedit2 libedit-dev llvm-3.5 llvm-3.5-dev llvm-dev
pip install enum34 funcsigs
LLVM_CONFIG=/usr/bin/llvm-config-3.5 pip install llvmlite==0.5.0
LLVM_CONFIG=/usr/bin/llvm-config-3.5 pip install numba==0.19.2
from Stackoverflow

numba - OSx Yosemite
(llvmlite 0.5.0, numba 0.19.2, llvm 3.5)
brew install homebrew/versions/llvm35
pip install enum34 funcsigs
LLVM_CONFIG=/usr/local/lib/llvm-3.5/bin/llvm-config pip install llvmlite==0.5.0
LLVM_CONFIG=/usr/local/lib/llvm-3.5/bin/llvm-config pip install numba==0.19.2

Interactive visualisations for research papers

I work in the medical imaging field and very often work with three or four dimensional data, which could include 3D mesh models.

Static images in research papers often don't give the reader enough of a 'feel' for the research and I find providing supplementary interactive visualisations helps to convey the message. I create these by exporting volumes and meshes to x3dom and using jquery for added interactivity.

Below are some recent examples with links to the interactive visualisations.

Publication 1:Computer assisted detection of abnormal airway variation in CT scans related to paediatric tuberculosis


Supplementary material with interactive visualisations


Publication 2: Automated Colorectal Tumour Segmentation in DCE-MRI using Supervoxel Neighbourhood Contrast Characteristics (MICCAI 2014, accepted)

Supplementary material with interactive visualisations


Passing numpy arrays between python and c++ using cython

Passing numpy arrays between Python and c++ using Cython is a handy way of taking advantage of the ease and flexibility of python with the speed of c++.

This code illustrates how simple it is to pass n-dimensional (or in this case 2D) numpy arrays from python to c++ vectors and back, allowing computationally expensive code to be easily written in c++ and linked to high level python code.

The example code is hosted as a gist here and is an adaption from the standard Cython example  but also includes the 2D numpy example

Rectangle.cpp and Rectangle.h is a an example c++ class to be wrapped

This is wrapped using cython with an interface file rect.pyx and compiled using setup.py in the standard cython way using the command:

python setup.py build_ext --inplace

Then run the demo:

python python_test.py

Shows the standard examples as well as how to pass 2D arrays between python and cython