# 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.

Note: Advanced users may install several versions simultaneously without conflict.

## Experimental Physics Using Python

The SciPy Cookbook for Fitting Data has multiple examples.

## Python Code Repository

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.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.

• 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 a straight line (linear_xy_errors.txt) and a semicircle (semicircle.txt).

• odr_fit_extended.py extends the ODR fitter so it can fit data with systematic uncertainties or uncertainties that are not gaussian standard deviations. Monte Carlo methods are used to calculate the output parameter uncertainties and Confidence Interval for the fit. The example in the code fits a Gaussian to data in file gauss_ODR.txt . For more information, see the ODR Fitter Description.
• scipy_2d_gaussian_fit.py is a simple fit and plot of a 2 dimensional distribution. It is a minor modification of a scipy example.
1. histogram_1D.py - Makes one dimensional histogram of a list of numbers.

• The resulting histogram is both displayed and saved as an image, and also output to a text file which can be input to a curve fitting program.
2. minimize_example.py - A simple example of how to use scipy.optimize.minimize

• Several possible functions are provided, only one of which has a simple single minimum, to illustrate some simple pitfalls.
3. Image_folder_to_PDF.py - Converts a folder of images into a single compact pdf file. For example, photos of your lab notebook pages can be combined into a single file for electronic submission.

4. 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.
5. helmholtz.py - Calculates the magnetic field near the centre of Helmholtz coils.

6. Tracking beads or other spots in a sequence of images can be done with code available from our Optical Tweezers experiment .

7. Unknotting and motion of a beaded chain on a vibrating plate can be studied with code available from our Unknotting experiment .

8. Laue diffraction spots for a cubic crystal can be identified using Laue_Spot_Identify.py .

9. 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.

10. stopwatcher.py - Record time of keystrokes or position/time of cursor clicks, and record them to both a file and the python terminal.

11. 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.
12. 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.

Last updated on 13 September 2020