Python Programming

This guide is mandatory for all students in the computer vision group that use Python.

Tutorials / Books

Good Practice

Please use Python 3. For a general introduction to good programming style in Python please read the Google Python Style Guide. Here are some group-specific additions.

  • Use assertions as often as possible, e.g. to check types.
  • Abstract functionality into classes when your code becomes too complex.
  • Global constants are often useful to avoid unecessary arguments.
  • Write tests of your functions using pytest.
  • Write a command line interface using argh.
  • Use git to manage source code. You can get a repository from the GWDG using your student login.

For now, I suggest to use the directory structure suggested in this blog post.

Write tests

Use pytest to validate that your program works properly. Writing a test is as easy as adding function named with the prefix test_ and containing several assertions. Then run pytest in the same folder as your script.

Command Line Interface

Sometimes you want to run your program in different configurations (e.g. using various datasets), triggered by arguments in the command line. For this please use argh, which is capable of wrapping normal python functions to the command line.

import argh
parser = argh.ArghParser()
parser.add_commands([train, evaluate])

Important Language Aspects

Here is a list of some aspects of Python that I consider useful to know.

  • Script Language
  • Generators
  • with modifier
  • functional programming
  • Mutability Only objects of some, mutable types can be partially changed (e.g. adding an element).
  • Pass by reference/value Primitive datatypes are passed by value while complex types (e.g. dicts) are passed by reference.

Fallacies

Some common python functions do not work as on might expect

  • scipy.ndimage.imresize changes the value range as discussed here

  • could not broadcast input array from shape (10,20) into shape (10): If we want to build a array from multiple arrays that have same shape, numpy assumes all other dimensions will also match. I.e. this code will fail: np.array([np.ones((10,10)), np.ones((10,11))]) while the following, even more incompatible, shapes can be put into an array (with dtype=object, though): np.array([np.ones((10,10)), np.ones((11,11))])

Useful Packages

These packages should always be installed in a scientific context.

  • Numpy Lots of useful and fast matrix and tensor operations
  • Scipy Some scientific tools.
  • Matplotlib Visualization

Computer Vision

  • Scikit-image Library that provides basic (and some sophisticated) image operations
  • OpenCV Another computer vision library. Not available on pip, but most distributions provide a package.

Machine Learning

  • Scikit-learn Many machine learning algorithms
  • Theano Numpy-like API with expressions being compiled to GPU. Provides automatic differentiation which is particularly useful in deep learning.
  • Keras High-level deep learning library.
  • Dlib A C++ Library with a Python interface.

Extensions

  • pip Package Manager Easily fetch libraries from the Internet.
  • IPython Interactive Python console
  • Jupyter Notebooks Web-based Python console. Nice for presenting results but key code should be implemented in separate Python scripts.
  • Cython Compiles Python-like code (enriched with types). Can speed-up your code significantly but is hard to learn.
Computational Neuroscience Group