Python Tutorial for Astronomers

On Monday, a group of CfA astronomers (Tom Aldcroft, Brian Refsdal, Gus Muench, and myself) announced the availability of a web tutorial aimed at teaching Python to astronomers through a series of interactive workshops:

Practical Python for Astronomers is a series of hands-on workshops to explore the Python language and the analysis tools it provides. The emphasis is on using Python to solve real-world problems that astronomers are likely to encounter in research. Some features:

  • Workshops immediately use plotting, analysis, and file reading tools.
  • Along the way elements of the Python language are introduced.
  • Workshops are interactive using examples run by participants on their laptops.
  • Comprehensive instructions a given for installing a full Python environment.

There are two goals. First is to provide tutorials suitable for self-study by those wishing to learn Python for astronomy. The greater goal is for those knowledgable in Python to teach the workshop series at their local institutions, adapting the content as desired. To that end we have developed the content in Sphinx RestructuredText and hosted the source on github at Anyone interested can clone the repository or download a tarball and make modifications needed to present the material locally. We would also welcome comments, fixes, or suggestions for improvement. This can be done as a Github issue or pull request, or by sending email to Tom Aldcroft.

The workshop material here was presented in the Spring of 2011 at the Harvard / Smithsonian Center for Astrophysics. A range of about 25 to 50 people participated in the different workshops, which were 1.5 hours in duration. One key accomplishment was installing a working Python with NumPy, SciPy, and IPython on over 50 laptops (MacOS, linux, and Windows) during a single session.

Feedback and suggestions are welcome!

7 comments… add one
  • Doug A May 25, 2011 @ 13:05

    Thanks! I’ll have to take a look at using the tutorials for my astronomy class next year. But why does the fit example use Sherpa? Isn’t that a lot of extra software and overhead to deal with to introduce fitting?

    It’s amazing how hard it is to do basic data fitting in python, and more importantly, calculate sensible error bars. By that I mean error bars that find iso-contours of the fit statistic, delta chi-squared = 1 for example, instead of Gaussian approximations. Scipy has optimize.leastsq, but that is only a Gaussian approximation to the error.

    Not that my solution is any better. I’ve got pyminuit [] installed — I know, I know, the pot calling the kettle black — and have a primitive wrapper [] around it that makes basic tasks quick . Wish scipy or numpy would just build something in!

    • Brian R May 25, 2011 @ 15:43

      I chose Sherpa for the fit examples because of its high level interface to fitting and visualization and its robust method to compute confidence limits. Sherpa succeeds in computing confidence limits with its concepts of “freezing” and “thawing” particular model parameters to evaluate the parameter space. From there, Sherpa calculates the limits by finding the roots of a transformed parameter space. The tricky part for the scipy and numpy authors is implementing these high level concepts at the ndarray level AND providing a generic API. Perhaps NumPy’s masked array module can help.

  • Carolyn Brinkworth May 25, 2011 @ 17:14

    This is fantastic – thanks so much for posting. It’s exactly what I’ve been looking for.

  • Doug A May 27, 2011 @ 12:32

    Hi Brian-

    Thanks for the feedback. Having never used Sherpa myself, I’m treading on dangerous ground making statements about it. That being said, I just had a look over some of the help documentation for Sherpa, and besides having integrated plotting, we both end up having similar interfaces for fitting. Perhaps not surprising, because the fitting problem is encouragingly generic.

    In fact, I think this is rather encouraging for the inclusion of a proper fitter in scipy. If the fitter manages the thawing and freezing of variables in the model (or at least knows how to communicate with a model object), and understands what to do when I tell it to “fit the 95% joint confidence region for parameters A & B, marginalizing over C”, then nothing needs to change about the ndarray interface — data is data is data, and few things hold data better than some ndarrays. I think the key is making sure the barrier to defining new models is low.

    I think we can agree that it would be wonderful to be able to sit down at any computer with scipy installed and already be able to conduct competent data analysis. My email is dapple _@_ stanford _._ edu. Glad to chat directly any time about the intersection of coding and statistics.


  • Matt Mechtley Jun 7, 2011 @ 21:57

    Thanks so much for releasing these materials, guys! As the resident pythonista at Arizona State, I’ve been plotting to do an introductory workshop series for other grad students and faculty here. These tutorials are an excellent base for me to start building content, and seem great for teaching complete beginners!

Leave a Reply

Your email address will not be published. Required fields are marked *