There are three reasons why I have decided to use Python for this lecture.
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
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!
Free Python Books
If you decide to install things manually, you need the following modules in addition to the Python standard library:
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.
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:
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:
In the newly created command shell, execute the following commandipython 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, containingimport 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.
run “cmd” and/or generate a file “ipynb.bat”
Run/Enter the commands
cd [ipynb-directory] [Python-directory]\Scripts\ipython3.exe notebook --pylab=inline
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.
10_getting_started.ipynb shows you how to get started with the Python.
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.
fits contains examples for a number of fitting applications (lines, sine-waves, exponential decays, etc.)
ui provides GUIs for file- and directory selection, a progress bar, and a keyboard-based viewer for matplotlib-plots.
The simplest way to install thLib is
>>> pip install thLib
For upgrading to the latest version, you have to type
>>> pip install thLib -U