Python introductory tutorial for undergraduates from C. Villforth

This is a short tutorial to introduce Python programming to undergraduate students. It is not meant to formally teach programming skills, but rather show students what they can achieve using Python. In the course of the tutorial, the students will read a text file, perform calculations on arrays, define a function and fit a line to d... View More

    6 Love

Google's Introduction to Python

This tutorial is based on the introductory Python course offered internally at Google. https://developers.google.com/edu/python/introduction#prelude

View More
    8 Love

Introduction to Python Tutorial from the Python Software Foundation

This tutorial on getting started with Python is the "officially sanctioned" guide hosted by python.org: https://docs.python.org/3.4/tutorial/

View More
    3 Love

Software Carpentry: Data Analysis with Python

The best way to learn how to program is to do something useful, so this introduction to Python is built around a common scientific task: data analysis. http://swcarpentry.github.io/python-novice-inflammation/

View More
    4 Love

Practical Python for Astronomers

Practical Python for Astronomers is a series of hands-on workshops to explore the Python language and the powerful analysis tools it provides. The emphasis is on using Python to solve real-world problems that astronomers are likely to encounter in research. The workshops immediately make use of the full suite of plotting, analysis, a... View More

    7 Love

STScI Scientific Python Course 2015

This is a data-oriented approach to Python. The focus is on showing one how to quickly get up and running reading, manipulating and displaying data learning the minimum amount of Python initially. Gradually, more Python language is introduced as more complex examples are worked through. No Python background is required. https://githu... View More

    5 Love

Minimal example of wrapping a C function using Cython

The notes and example at https://github.com/aphearin/cython_c_extension_example provide a simple, quickstart example that beginner's can use to pattern-match into their python code, saving them the trouble of having to wade through extensive, technical documentation when there is just a simple C function that needs to be wrapped ... View More

    10 Love

Practical Python for Astronomers (aka python4astronomers)

Practical Python for Astronomers is a series of hands-on workshops to explore the Python language and the powerful analysis tools it provides. The emphasis is on using Python to solve real-world problems that astronomers are likely to encounter in research. The workshops immediately make use of the full suite of plotting, analysis, a... View More

    8 Love

Astropy Tutorials

The Astropy Tutorials walk through some typical software tasks and demonstrate the features in Astropy sub-packages in the context of a story or standard workflow.

View More
    7 Love

Scientific Python Lecture Notes

This is a comprehensive set of IPython notebook tutorials covering the key parts of the scientific Python software stack: NumPy, Matplotlib, and SciPy.  It also gives an overview of the Python language itself.http://scipy-lectures.github.io/

View More
    6 Love

Stopping a script for debugging

Dylan Gregersen brings us another nice code snippet which might be especially welcome for IDL users who miss the STOP procedure:Here's a snippet I'm using often while writing scripts, executing via run -i <file> or execfile(<file>), and I want to quickly truncate the code so that the remaining par... View More

    4 Love

Convert IPython notebook to ApJ or A&A article

This guest post was contributed by Moritz Guenther:When I first encountered the IPython notebook, I thought this was a solution looking for a problem. However, I have since been converted! The tipping point for me was this: I want to version control my papers and I always had multiple directories for analysis code, plotting code, La... View More

    5 Love

Machine Learning for Astronomy with scikit-learn

The online tutorial Machine Learning for Astronomy with scikit-learn offers an introduction to the fields of machine learning and statistical data analysis, and their application to several problems in the field of astronomy. These learning tasks are enabled by the tools available in the open-source package scikit-learn.... View More

    10 Love

Installing scientific Python on Fedora

As part of a recent Python for Astronomers workshop on installation, Jon Chappell created a nice bash script to do a root install of scientific Python that works on recent Fedora machines.  You might find it useful!#!/bin/bash ### This script installs a number of base and addon packages to create a # scientific python distributio... View More

    6 Love

Python interactive tutorial: How to think like a Computer Scientist

The "How to think like a Computer Scientist" books (web based or physical) have long been a recommended starting place for scientists learning to code.  This interactive, javascript driven, video laced edition of the Python book just took things to the next level and is a super place to start learning Python:How to think... View More

    8 Love

Skipping inconsistent rows in asciitable

The asciitable module provides a way to deal with tables that have one or more lines which don't match the format of the rest of the file.  This is done by overriding the asciitable.BaseReader.inconsistent_handler function with your own custom function.  The very simplest action is to just ignore the line entirely by returning N... View More

    8 Love

Installing Python via MacPorts on Mac

Installing Python via MacPorts on Mac.The following page has instructions for setting up a Python distribution for Astronomy using the MacPorts package manager on Mac:http://astrofrog.github.com/macports-python/MacPorts has several advantages over other installation methods, including: No Licensing issues Dependencies are taken care... View More

    6 Love

Practical Python for Astronomers

Practical Python for Astronomers is a series of hands-on workshops to explore the Python language and the powerful analysis tools it provides. The emphasis is on using Python to solve real-world problems that astronomers are likely to encounter in research.The workshops immediately make use of the full suite of plotting, analysis, a... View More

    11 Love

Fix the WCS for a FITS image file

The code snippet which follows will allow you to fix an error in the astrometry of a FITS image.  This is especially common with HST images because the astrometrical errors can be an arcsecond or more.  The method used assumes that you have extracted the positions (RA, Dec) for at least 2 image point sources for which yo... View More

    7 Love

Fast fractals with python and numpy: Mandelbrot sets

Source: http://thesamovar.wordpress.com/2009/03/22/fast-fractals-with-python-and-numpy/ # Python code heredef mandel(n, m, itermax, xmin, xmax, ymin, ymax): ix, iy = mgrid[0:n, 0:m] x = linspace(xmin, xmax, n)[ix] y = linspace(ymin, ymax, m)[iy] c = x+complex(0,1)*y del x, y img = zeros(c.shape, dtype=int) ix.shape = n... View More

    2 Love

External Python packages in CIAO or Sherpa analysis

Using Python packages that are not included within the CIAO distribution is straightforward.  The key is that you need to use the CIAO python executable to install those packages into a location where CIAO python will find them.  In general the CIAO python will NOT find packages that are included in your system python installation -... View More

    7 Love

Basic Sherpa SED Fitting

This code snippet goes through the basic steps of importing and fitting a spectral energy distribution with Sherpa.  The data were saved from NED in XML (VO Table) format and are imported into python using the vo module.  After a little data processing, the SED is fit with a simple 2 power-law function. The script uses Sherpa's ... View More

    8 Love

Interpolation without SciPy

This code snippet shows a simple way to do linear or nearest-neighbor interpolation using only NumPy.  This is handy if you don't have SciPy installed or don't want to introduce a dependency on SciPy in distributed code.  By using the numpy.searchsorted() method and vectorized operations it is reasonably fast, though I have ... View More

    5 Love

PyFITS: FITS files in Python

PyFITS: FITS files in PythonIn this article, we provide examples of using the python module PyFITS for working with FITS data. We first go through a brief overview of the FITS standard, and then we describe ways for accessing information in FITS files, using convenience functions defined in PyFITS. PyFITS offers facilities that prov... View More

    6 Love

Fast Lomb-Scargle algorithm

Astropython.org visitor "Morgan" contributed a Python implementation of Lomb-Scargle via the comments to [Question] period-finding packages in python.  This script is based on:    Press, W. H. & Rybicki, G. B. 1989    ApJ vol. 338, p. 277-280.    Fast algorithm for spectral analysis of unevenly sampled data    bib ... View More

    7 Love

AstroAsciiData - working with ASCII tables

Prasanth at the Comfort at 1 AU blog has posted a nice review and tutorial on the AstroAsciiData module.  ContentsIntroductionRequirementsAn ExampleConceptsWorking with data   Creating tables      Creating table from data in file      Creating empty table   Adding and manipulating data   Adding meta data   Writing to filesAn applica... View More

    14 Love

Save sherpa fit and conf results to a JSON file

After doing a fit with Sherpa you may want to dump all the results to a file for later processing or inspection.  The following snippet writes the results from model fitting and confidence estimation to a file using the JSON format for data serialization.  This format is widely supported in other programming languages an... View More

    5 Love

Parallel map using multiprocessing

This module (courtesy Brian Refsdal at SAO) implements a parallelized version of the native Python map function that utilizes the Python multiprocessing module to divide and conquer an iterable.  It takes advantage of the nifty NumPy function array_split() to divide an input iterable into approximately equal chunks.  It ... View More

    8 Love

Easier python logging

The python logging module is a very good option for producing output in most code and especially in modules that will distributed.  It is incredibly flexible but that comes with the price of a steep learning curve.  The developers recognized this and included a one-stop method logging.basicConfig() to set up a basic logger that will... View More

    5 Love

User (root/sudo free) installation of Python modules.

For this tutorial I will assume user installation of new packages by building from downloaded source, ie "python setup.py install." While this tutorial also applies to user installations with the easy_install command line tool, I reserve easy_install related notes to the "Gotcha's" section at the ... View More

    4 Love

Installing Sherpa 4.5 standalone for X-ray analysis

This tutorial covers the steps to build a "complete" standalone installation of Sherpa which includes everything needed to do X-ray analysis. This was tested on Mac OS 10.6 with EPD Python and also includes specific issues that came up when installing on a CentOS 5.4 linux system (with no root access) using a custom-built ... View More

    7 Love

Getting started with python in astronomy

Taking your first steps into python can be daunting and there are a bewildering number of tutorials available if you start googling.  Here we try to single out a few of the best that can get you going and show the basic tools that are available for scientific analysis in python.Python languageOne good (though somewhat un... View More

    8 Love

Basic numpy array manipulation

NumPy is a Python library for working with multidimensional arrays. The main data type is an array. An array is a set of elements, all of the same type, indexed by a vector of nonnegative integers. Arrays can be created in different ways: >>> from numpy import *>>> a = array( [ 10, 20, 30, 4... View More

    7 Love

Plotting in Matplotlib

Matplotlib is the preferred 2D plotting library for Python users. The following is a collection of very simple programs from Matplotlib's examples directory. For more examples you may want to check out the Matplotlib Cookbook and the official matplotlib tutorial at http://matplotlib.sf.net/tutorial.html Simple plot #!/usr/bin/... View More

    5 Love

chdir context manager

This context manager restores the value of the current working directory (cwd) after the enclosed code block completes or raises an exception.  If a directory name is supplied to the context manager then the cwd is changed prior to running the code block.  from __future__ import with_statement # only for python 2.5 impo... View More

    6 Love