PythonBasics

From CASA Guides
Revision as of 15:54, 10 October 2011 by Aleroy (talk | contribs) (→‎Basic Math)
Jump to navigationJump to search

We are porting, updating, and editing this [1]

Preface

This guide is intended as a practical introduction to python for astronomers, in particular CASA users interested in scripting data reduction or integrating analysis, observation planning, and reduction. Despite this rough focus, we hope that this guide is generally useful to any astronomer looking for a first dive into python. Similar, more rigorous material exists across the web. If this guide whets your appetite then we recommend digging into the pages linked off PythonOverview.

Environment

We could write a lot on setting up python, a nice shell, installing key packages, etc. Because this is a CASA guide, we can short circuit some of this. CASA installs with it's own nice iPython shell and a core set of third-party packages (numpy, scipy, pylab, matplotlib). We will assume that you have downloaded and installed CASA from here and return to the issue of additional third-party software down the road.

Once you have CASA installed you can start a new session from your shell prompt by just typing

casapy

Depending on your computing environment you likely also have some version of python and perhaps iPython installed, but the versions of these (especially the pre-installed version on the Mac) can vary widely. If you are interested in a non-CASA distribution that folds in a similar (in fact more extensive) suite of packages you might look at the free academic version here (no promises). In the interests of version control, we focus our discussion on the version of python that comes with CASA (2.6).

Pasting Code

We will fairly rapidly reach the point where we want to cut and paste somewhat complex blocks of code. Python's use of indentation to implement code structure can make for awkward interactions with the shell at times. Even when it goes well after cutting and pasting a 'for' loop you often have to press return twice to execute. In very complex cases, you may end up needing to paste a line at a time.

Fortunately, the iPython shell offers a very nice way around these challenges. You can type

cpaste

to initiate a 'code paste'. You will then be able to paste code directly into the shell and the code will appear exactly as you copied it. When you have pasted all of your code got to a new line and type "--". This ends the code paste and the pasted code should then execute.

Getting Help, Exploring Objects, the Shell

CASA's shell (and iPython shells in general) has a lot of useful functionality. For example, CASA knows some basic operating system commands like "ls", "cat", etc. CASA only knows a few of these but you can issue any commands to your Unix/Linux shell directly by prefacing those commands with an exclamation point. So this works:

ls

and so does this:

!ls -lh

this does not:

df -h

but this does:

!df -h

The shell has many other nice features. For example, if you type the name of a variable, it will simply print the variable value.

You can readily get help on most object via "help thisorthat," for example

help list

Along with "help" the other major exploration capability in the shell is tab-completion. Taking any object, module, etc. you can append a ".", hit tab, and the shell will show you list of possible completions. Try this now by typing "list." and hitting tab. You should be able to a list of the things that a list can do. In addition to a bunch of system-defined names that begin and end with "__" (e.g., see the equality operator "__eq__") you can see that list has a set of methods "append", "count", "extend", "index", ... "sort".

If you saw that list.append exists and wanted to learn a bit about it you could type

help list.append

We're getting a bit ahead of ourselves but try to bear these exploratory capabilities as you look through the guide.

One very important thing to bear in mind as you begin more advanced programming is that the shell specific capabilities are not available outside the shell.' In practice this means that you cannot, for example, print the name of a variable by just listing the variable inside a program.

Simple Variables

Let's begin by creating an manipulating some basic variables. At the python shell prompt create your first variable by typing

my_first_var = 1

That's it, you've created your first variable. Note that you didn't have to specify the data type, python works this out from context. To see the value of your variable type

print my_first_var

or (if and only if you are at the shell) you can see the value by just typing the variable name

my_first_var

(read: use 'print' when programming).

You can see the type of the the variable by typing

print type(my_first_var)

Now try the same thing with some other data types.

# Make and print a string
a_string = "A String"
print a_string, type(a_string) # a string

Note that this first line does nothing. Python's comment string is "#" and it ignores everything in a line that comes after this character.

Some similar examples for floating point numbers:

type(3.14159) # a float
a_float = 1e-10
print type(a_float) # scientific notation

Notice the in-line comment and the on-the-fly creation of a variable in the first line.

Finally, python has a 'boolean' type that is either 'True' or 'False' (shorthand 'T' or 'F' in CASA, though not iPython in general).

type(True) # a boolean
boo = False
print boo, type(boo)

You can chain variable creation:

x = y = 1
print x, y
x = y = v = u = t = 0
print u, t

Basic Math

We have seen how to create variables. Now we will manipulate them with basic math operations.

Begin by creating a new variable with value 1.

x=1

and print the value after we add 2.

print x+2

We can also assign the output of the operation to a new variable

y = x + 2
print, y

z = x - 3
print, z

Implicit operations work

x = 1
print x
x += 3
print x
x *= 10
print x
x /= 20
print x

Ints and Floats

Booleans

Deleting Variables

Checking Whether a Variable Exists

Data Collections: Lists

Lists are one of the fundamental data collections in python (we will also discuss dictionaries and you may want to read about tuples and sets).

Data Collections: Dictionaries

Dictionaries are another basic python data collection. Dictionaries store data in pairs, with a set of unique keys each mapped to some value (the values do not have to be unique). Dictionaries are extremely useful in scripting. For example, imagine that we are reducing a complex set of observations and want to be able to look up a the calibrator associated with a given source.

Control Flow

If

While

For

Break/Continue

More Complex Programs

Executing Scripts

The most basic way to execute a set of python commands (aside from just copying and pasting to the shell) is to use the execfile command. Calling execfile('myscript.py') from inside a python shell will execute 'myscript.py' one line at a time. You can use this to run a series of reduction commands or other simple scripts. In fact, calling execfile on one or more scripts will almost certainly be sufficient to script most basic CASA data reductions.

A simple example

You can combine the control flow that we learned above with execfile to refine your scripts. For example, you might have a sophisticated reduction path that requires a few user inputs, which could be collected at the top of the script as variables. The reduction might have several discrete parts, which you could turn on or off using booleans and if statements. As an example, try creating a file that holds the following:

An example with a bit of control flow.

As you edit the variables and booleans, various parts of the script will run tuned by the variables you set. This simple but powerful approach can (if you desire) form the infrastructure for a lot of your CASA reduction scripting.

Functions

Python allows you to define functions either from the command line (or an execfile call) or as part of modules.

Modules

Object Oriented Aspects