Python for the Advanced Physics Lab
Python is a
widely used programming language with many open source tools and libraries. It is not as computationally fast as pure compiled languages such as FORTRAN or C++, but it is generally considered easier to learn. Python is the preferred beginning language for undergraduate physics computation at the University of Toronto, both in lab and lecture courses. Python is free and can be installed on almost any computer.
Installing Python on your Computer
There are many Python installations available, but scientific users require several packages that are not always included.
- For University of Toronto students we recommend the standard
University of Toronto Physics Python Installation
, which includes numpy, scipy, matplotlib, and VPython.
- Another good (and easy) installation is available from
Enthought, free for Academic users. This installation does not include VPython (although this can be added by experts), but includes many additional packages such as the Python Imaging Library (PIL).
- Sage is a free open-source mathematics software system
using Python, numpy, scipy, matplotlib, and many other packages, but it does not (yet) include VPython or PIL.
- For video/image analysis, you will likely need the
Python Imaging Library (PIL), and sometimes OpenCV. For Mac users wanting to install OpenCV, we recommend a fresh, clean build via MacPorts; this installation can take some time. Because the MacPorts Python interface is a bit clunky, this is not recommended unless you need OpenCV and (like us) have not yet figured out how to install it with a friendlier package.
Experimental Physics Using Python
The SciPy Cookbook for Fitting Data has multiple examples.
These python programs have been developed, modified, or used in the Advanced Physics Lab for fitting, numerical calculation, simulation, and video analysis.
These programs should run on Python 2.6 or 2.7, and usually require numpy, scipy, matplotlib. In some cases, as noted, they may require the Python Imaging Library (PIL), OpenCV, or VPython. In order to help infrequent or new Python users, the programs are usually over-commented.
If you find a bug in one of these programs, please let the
Coordinator know.
-
Data Plotting and Fitting for (x,y) data:
- simple_pylab_data_plot.py is the simplest program to plot data from a file (gauss.txt in this example). For conciseness, it uses pylab instead of explicit numpy, scipy, and matplotlib methods.
- curve_fit_to_data.py fits a gaussian to data in file gauss.txt,
and can be easily modified to fit your own data and model.
Slightly simpler or more complex versions are also available:
simple_curve_fit_to_data.py and extended_curve_fit_to_data.py.
- odr_fit_to_data.py fits data with uncertainties in both x and y, using
Orthogonal Distance Regression (ODR). This is also useful when the data has only x uncertainties, but the fit function, f(x), is not analytically invertible or is not single valued in y.
The residual variance from ODR is a "quasi χ2" which converges to the regular "χ2" for small x uncertainties, but note that if the x uncertainties are large, the cumulative probability distribution is better (but more slowly) estimated by the Monte Carlo option inside the program.
Sample data files include gauss_dx.txt (which is identical to gauss.txt except for the addition of tiny x uncertainties), gauss_xy_errors_ugly.txt which illustrates how large x uncertainties can result in very large parameter uncertainties, and linear_xy_errors.txt.
- Python for Physics from the University of Cape Town Physics Department offers non-linear least squares fitting of a Lorentzian/Breit-Wigner/Cauchy function to data in file lcr.dat. (Note: This example uses an obsolete method "load", which must be replaced by "loadtxt".)
-
helmholtz.py - Calculates the magnetic field near the centre of Helmholtz coils.
-
thermocouple.py - Converts thermocouple voltages into temperature, or vice versa.
-
See comments in code for currently supported thermocouple types.
-
thermocouple_test.py is a crude quick test of thermocouple.py.
-
Tracking beads or other spots in a sequence of images can be done with code available from our Optical Tweezers experiment.
-
Unknotting and motion of a beaded chain on a vibrating plate can be studied with code available from our Unknotting experiment.
-
driven_bouncing_balls.py is a vPython simulation of 3 balls bouncing vertically on an oscillating plate. States from phase stability to chaos can be studied.
-
stopwatcher.py - Record time of keystrokes or position/time of cursor clicks, and record them to both a file and the python terminal.
-
Animated plot examples, expanded from a MatPlotLib example:
- Live_Plot_Simple.py is a simple example, but
it can't be easily stopped until it finishes.
- vonNeumann_elephant.py demonstrates the famous dictum attributed to John von Neumann that: "With four parameters I can fit an elephant, and with five I can make him wiggle his trunk."
- Live_Plot.py plots an exponentially decaying sinusoid with gaussian noise. It includes start and pause buttons, and is selective about what it plots.
-
FaceDetect.py - A simple example of real-time webcam face-recognition using OpenCV. Because of a bug in OpenCV, it may be necessary to "Force Quit" Python to close the camera window when the program finishes.