_images/title_Appendix.png

Programming Matters

Python

There are three reasons why I have decided to use Python for this lecture.

  1. It is the most elegant programming language that I know.
  2. It is free.
  3. It is powerful.

I have not seen many books on Python that I really liked. My favorite introductory book is Harms, D. & McDonald, K. (2010). The Quick Python Book (2nd Ed). Manning Publications Co.

In general, I suggest that you start out by installing a Python distribution which includes the most important libraries. I suggest that you use Python \(>3.3\) for this course, All the Python packages required for this course are now available for Python 3, so I don’t see a good reason to stay with Python 2.7 . My favorites Python 3.3 distributions are

  1. http://winpython.sourceforge.net/ No admin-rights required. Recommended for Windows users.
  2. https://store.continuum.io/cshop/anaconda/ From Continuum. For Windows, Mac, and Linux. By default installs to Python 2.7.x, but can upgrade to Python 3.x.

which are very good starting points when you are using Windows. winpython does not require administrator rights, and anaconda is a more recent distribution, which is free for educational purposes.

Mac and Unix users should check out the installations tips from Johansson (see Table below).

There are also many tutorials available on the internet. Personally, most of the time I just google; thereby I stick primarily a) to the official pages, and b) to http://stackoverflow.com/. Also, I have found user groups surprisingly active and helpful!

Links

Free Python Books

If you decide to install things manually, you need the following modules in addition to the Python standard library:

  • ipython ... For interactive work.
  • numpy ... For working with vectors and arrays.
  • scipy ... All the essential scientific algorithms, including those for statistics.
  • matplotlib ... The de-facto standard module for plotting and visualization.
  • sympy ... For symbolic computations.

IPython

Make sure that you have a good programming environment! Currently, my favorite way of programming is similar to my old Matlab style: I first get the individual steps worked out interactively in ipython qtconsole. Ipython provides interactive computing with Python, similar to the commandline in Matlab. It comes with a command history, interactive data visualization, command completion, and a lot of features that make it quick and easy to try out code. When ipython is started in pylab mode (which is the typical configuration), it automatically loads numpy and matplotlib.pyplot into the active workspace, and provides a very convenient, Matlab-like programming environment. A very helpful new addition is the browser-based ipython notebook, with support for code, text, mathematical expressions, inline plots and other rich media. Please check out the links to the ipython notebooks in this statistics introduction. I believe that it will help you to get up to speed with python much more quickly.

To write a program, I then go to either Spyder (which is free) or Wing (which is very good, but commercial).

The flexibility of Python has the “disadvantage” that it can come in differnt flavors or coding styles. When you know the different approaches, they are great to use. But when you get started, it can be a bit confusing. The following section from the Matplotlib documentation may help to clarify these things:

Personalizing IPython

When working on a new problem, I always start out with IPython. Once I have the individual steps working, I use the IPython command %history to get the commands I have used, and switch to an integrated development environment (typically Wing or Spyder).

To start up IPython quickly in the location and with the configuration I like, I use the following tricks (the following are the steps on MS Windows, but should be easy to adapt to other operating systems):

To personalize ipython, generate your own profile:

  • run “cmd”

  • In the newly created command shell, execute the following command

        ipython profile create <myName>
    (This generates a folder ".ipython\profile_<myName>\startup")
    
  • Into this folder, place a file with e.g. the name 00_<myName>.py, containing

    import pandas as pd
    import os
    os.chdir(r'C:\<your_favorite_dir>')
    
  • Generate a file “ipython.bat” in your startup-directory, containing

    [Python-directory]\Scripts\ipython3 qtconsole --profile <myName> --pylab=inline
    

Now you can start “your” ipython by just typing “ipython” in the Windows run command.

To see all ipython notebooks for the course, do the following:
  • run “cmd” and/or generate a file “ipynb.bat”

  • Run/Enter the commands

    cd [ipynb-directory]
    [Python-directory]\Scripts\ipython3.exe notebook --pylab=inline
    

Coding Styles in Python

In Python you will find different coding styles and usage patterns. These styles are all perfectly valid, and each have their pros and cons. Just about all of the examples can be converted into another style and achieve the same results. The only caveat is to avoid mixing the coding styles for your own code.

Of the different styles, there are two that are officially supported. Therefore, these are the preferred ways to use matplotlib.

For the preferred pyplot style, the imports at the top of your scripts will typically be:

import matplotlib.pyplot as plt
import numpy as np

Then one calls, for example, np.arange, np.zeros, np.pi, plt.figure, plt.plot, plt.show, etc. So, a simple example in this style would be:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.2)
y = np.sin(x)
plt.plot(x, y)
plt.show()

Note that this example used pyplot’s state-machine to automatically and implicitly create a figure and an axes. For full control of your plots and more advanced usage, use the pyplot interface for creating figures, and then use the object methods for the rest:

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0, 10, 0.2)
y = np.sin(x)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)
plt.show()

Next, the same example using a pure MATLAB-style:

from pylab import *
x = arange(0, 10, 0.2)
y = sin(x)
plot(x, y)

So, why all the extra typing as one moves away from the pure MATLAB-style? For very simple things like this example, the only advantage is academic: the wordier styles are more explicit, more clear as to where things come from and what is going on. For more complicated applications, this explicitness and clarity becomes increasingly valuable, and the richer and more complete object-oriented interface will likely make the program easier to write and maintain.

For interactive work, it is simplest to use the pylab mode.

ipynb 10_getting_started.ipynb shows you how to get started with the Python.

thLib

thLib is a library for scientific data analysis, with a focus on 3d kinematics.

quat, rotmat, and vector contain functions for working with quaternions, rotation matrices, and vectors. To facilitate application to real data, these functions typically also work with arrays of data.

kinematics analysis routines for 3D movements
  • calculation of orientation from velocity, recorded with IMUs or space-fixed systems
  • a function that takes recordings from video-systems (e.g. Optotrak) and calculates position and orientation
  • calculation of joint movements from marker recordings
  • calculation of position and orientation from IMU-signals

fits contains examples for a number of fitting applications (lines, sine-waves, exponential decays, etc.)

signals has functions that manipulate data
  • a Savitzky-Golay for data smoothing and derivatives
  • a power spectrum
  • a function to calculate and show mean and standard error for time series data.
  • a visualization for cross- and auto-correlations
sounds allows you to work with sounds in Python. Note that to use it with (almost) arbitrary sound-files, you need to have FFMPEG installed. The class Sound lets you
  • read sounds
  • play sounds
  • write sounds
  • define Sound-objects

ui provides GUIs for file- and directory selection, a progress bar, and a keyboard-based viewer for matplotlib-plots.

Installation

The simplest way to install thLib is

>>> pip install thLib

For upgrading to the latest version, you have to type

>>> pip install thLib -U

Dependencies

numpy, scipy, matplotlib, pandas, statsmodels, skimage, sympy

If you want to work not only with WAV-sound-files, but also with other sound formats, you need to install FFMPEG (free from http://www.ffmpeg.org).