[AstroPy] Practical Python for Astronomers web tutorial
jh at physics.ucf.edu
Tue May 24 20:42:37 EDT 2011
First, this is a great idea and I encourage your work in it!
Second, I've been teaching a class in Python (the one for which I
started the SciPy Doc Project). Feel free to look at the class web
site for additional ideas and demos. The demos are in the lecture
directories, organized as numbered subdirectories. For solutions and
large datasets, we can talk offline (I have to be careful about
circulating solutions for obvious reasons).
(Follow the tiny files link in the first block of normal type.)
Third, comments on the workshops.
1. Don't use:
from whatever import *
or any shortcuts like 'ipython -pylab' that do that. It's poor style
and against the practice of the numpy community in all its docs and
demos. Remember that the numpy, scipy, and matplotlib developers can
in the future put anything they want in their namespaces, and a new
release could break your working code because of a namespace conflict
that doesn't exist when you write it. While that may not bite you for
your workshops, it will eventually bite anyone who writes enough code
like that. Those problems can be hard to find.
In my experience, the "import *" habit is a very tough one to break,
so it's particularly important never to teach it. I categorically do
not use in my course any books or other resources that do "import *".
from whatever import foo, bar, baz
is ok, though not great just because of the bookkeeping required at
the top of every file. But, it's a low-impact way of rescuing old
2. Unless you are specifically teaching how to use the features of
ipython in a particular demo, make sure all demos run in plain python,
so they can be used in programs. At the very least, give exactly your
startup options (e.g., "ipython -pylab") and imports so the user can
run your demo. Also make sure you test them with no startup files
(see #5 below for bug report).
3. Provide the data you use in your demos as a hyperlink above the
demo (referring to "Reading a table and plotting" demo). In general,
I've found it simpler just to generate data or embed a simple dataset,
except when teaching how to read from a table.
4. The "Reading a table" demo is far too complicated for a first table
demo. Remember that most of your audience will want to copy the demo
and edit for their own purposes, so give them something short, like a
simple ASCII file with two columns, read with a numpy function and
plot. Then go for the asciitable, fancy histogram, real data
thing. They will be more impressed with the simplicity of plotting in
3 lines than with the fanciness of 23 lines.
5. In this demo, random.uniform() isn't imported by pylab:
$ ipython -pylab
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
Type "copyright", "credits" or "license" for more information.
IPython 0.10 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.
Welcome to pylab, a matplotlib-based Python environment.
For more information, type 'help(pylab)'.
In : n = 100
In : x = random.uniform(-10., 10., n)
AttributeError Traceback (most recent call last)
/home/jh/<ipython console> in <module>()
AttributeError: 'builtin_function_or_method' object has no attribute 'uniform'
Do you really want to exit ([y]/n)? y
6. In the "Curve fitting with SciPy", don't forget the "2" in the
denominator of the Gaussian exponent. In your example, sigma should
be sqrt(2), not 2 (since 2 * sigma^2 = 4 in the input). Add a comment
that you're passing a function in the fit and that it queries the
function for how many args there are after the x data to find out how
many parameters to fit.
7. In "Synthetic images", put a note in the for loop that there are
more efficient, vector-based ways to do this without loops or
conditionals (e.g., with np.where() or making a bigger image and then
cropping it). Why make a FITS rather than a matplotlib image?
8. In "Running existing compiled codes", put in a note that there are
straightforward ways to get compiled code to execute directly as
Python functions, which is what most people call "glue". Why write
the data to a file at all? It would be simpler to write the output to
a file with shell redirection and then read the file, if you must deal
with a file here. Close the file either way.
Far as I got...
Btw, I use that xkcd in my class as well. Note that it is the one ON
THE GROUND who says the other guy is flying. So it's not a
Also, the title of each workshop has the first word on the end of a
breadcrumb trail and the rest on a second line. Maybe put in a line
break before the title.
Alsoalso, in the table of workshops, give an estimate of the time each
Again, this is a fantastic resource, nicely put together!
More information about the AstroPy