Posts
How to make two mice work with xwindows (x.org)
It’s a real pain to surf the Web when the batteries die in your wireless mouse or trackball. I use some old rechargeables that are no longer fit for digital camera service, so this happens to me fairly often. My batteries just died again, so I had to figure out how to configure X.org to use a backup mouse. It’s actually not hard to have an old 3-button mouse plugged into the PS/2 port and tell X.org to use it as a backup. First, define two input devices:
Lookup tables and spline fitting in Python
Lookup tables and spline fitting are widely used by scientific programmers. A particular function may not have an analytic solution–in other words, it can’t be expressed as an equation of elementary functions. This might happen if the function were empirically determined from experimental data, or if the equation can’t be algebraically solved for one variable. If the analytical function is available, but takes a long time to evaluate, a lookup table or spline approximation can be considerably faster. In a previous post, I showed how to use the function interp1d from scipy.interpolate as a lookup table. In a later post, I showed that interp1d is actually rather slow, and scipy.interpolate.UnivariateSpline is much faster. Now, I will show some benchmark results, and explain a potential pitfall when using UnivariateSpline. The two functions I will use for this demonstration are not very complicated. Each function is a piecewise approximation of a more complex function. Here is one of the functions. For small z, the function uses one approximation, and for large z, it uses a far-field approximation. For intermediate values, a polynomial is used to smoothly match the two solutions.
3D Plotting Software for Python::Part 1::PyX
There are lots of good open-source tools that you can use to make high-resolution, publication-quality 2D plots. Personally, I like to use Python, numpy, and matplotlib. Unfortunately, it is much harder to find a good tools to make 3D plots. Older versions of matplotlib had rudimentary 3D support, but this was removed in version 0.98. In this post, I will review a Python 3D plotting library called PyX.
Preparing the data
Figuring out how to store the data to be plotted was actually the hardest part of learning to use PyX. The data format for 3D plots is not well documented. PyX requires a list of (x,y,z) lists like this:
Pypar Documentation
UPDATE 1/2016: It appears that Pypar is no longer maintained, although it probably still works. For new projects, I advise using mpi4py. The following is an online copy of the documentation of Pypar. I am not the author of the text below. I have merely posted it online to make it more accessible to the community. The latest version of Pypar can be found at: The Pypar page on Sourceforge
PyPAR - Parallel Python, no-frills MPI interface
Author: Ole Nielsen (2001, 2002, 2003) Version: See pypar.__version__ Date: See pypar.__date__
Parallel Programming with Python
Background
As an interpreted language, Python cannot compete directly with C or Fortran in execution speed. As I have written elsewhere, there are ways to make Python code run as fast as you need it to run. However, in scientific computing there is often no single processor that can execute a program in a reasonable amount of time. Computationally intensive problems require the use of parallel computers. A message-passing interface such as MPI is used to communicate between processes running on multiple CPUs. There is a Python interface to MPI called Pypar, which allows your Python programs to run in parallel on a system with an implemention of MPI installed. My experience is mainly with Open MPI, but there are several others. Below are some other resources to learn about about MPI and parallel programming in general. Using MPI with Python works around one of Python’s more serious limitations: the Global Interpreter Lock (GIL). Basically, Python uses the GIL to prevent errors that may occur when two threads try to access the same data. What this means is that Python can only utilize a single CPU thread. Note that this is not a fundamental limitation of the language, but of the standard C implementation of the interpreter. New interpreter implementations may be thread-safe, but for now, Python is stuck in a single thread. The GIL will become more of a problem as ordinary desktop CPU’s gain speed by adding cores rather than increasing clock speed. Rather than exposing my ignorance of Python’s internals, here are some links to posts written by people who are more knowledgable about this subject than I am.
Optimizing fast Python math with Numpy and Scipy
Coming soon! The work is already done–I just have to find time to write it up.
Online Documentation and Resources
The Python configparser: a way to read simple data files
My simulation library, which is written mostly in Python, needs a lot of data and parameters in order to run. In some cases, I just hard-code the values in the script that calls the library, and in other cases I load a pickle file containing a Python objext. What if I want to read in data or configuration parameters from a human-editable text file? If the information is extensive or complex, XML might be a good choice, but XML is overkill for simple configuration or data files. Fortunately, a standard Python library called ConfigParser has already defined a configuration file format, and provides methods to interact with such files. Here is a sample of the format used in a config file:
Updated Python class for writing Paraview (VTK) (.vtu) files
I have released a new version of my Python class that generates VTK data files in the .vtu format, which is compatible with Paraview and other VTK applications. If you have downloaded the old one, please get the latest version, which incorporates some bug fixes and has been more thoroughly tested.
How to put formatted, highlighted code in a Wordpress post
I found two complementary plugins that enable me to put highlighted formatted code in a Wordpress page or post. Here’s an example of what they do:
[sourcecode language="python"]
# Plot flux at continuum boundary
pylab.figure()
pylab.hold(True)
pylab.plot(nd_times, Jl_BD, 'b-', label="Flux from BD simulation")
pylab.plot(nd_times, Jl_BD+Jl_BD_std, 'b.', label="Flux from BD simulation")
pylab.plot(nd_times, Jl_BD-Jl_BD_std, 'b.', label="Flux from BD simulation")
[/sourcecode]Test successful! Note that Javascript must be enabled to see the highlighting. Get the plugins here: