no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | miriad_in_python [2008/09/23 21:42] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | This is documentation on my system for interacting with MIRIAD data via the Python language. | ||
+ | |||
+ | ==== Berkeley Quickstart ==== | ||
+ | |||
+ | If you're at Berkeley and want to use Miriad-Python scripts, hopefully all you need to do is: | ||
+ | |||
+ | < | ||
+ | source / | ||
+ | </ | ||
+ | |||
+ | If you're unlucky, the above commands will tell you that there' | ||
+ | |||
+ | Peter' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Relevant Systems ==== | ||
+ | |||
+ | Here are some links for the relevant software: | ||
+ | |||
+ | * We use [[http:// | ||
+ | * We want to use the Python language ([[http:// | ||
+ | * We'll interact with code and data using the nice [[http:// | ||
+ | * Python itself doesn' | ||
+ | * The rest is my software, which generally isn't too well-documented. The modules I use include '' | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Exploring with IPython ==== | ||
+ | |||
+ | Here are some generals tips for using IPython. These may not make much sense until you get a basic sense of the elements of the Python language. | ||
+ | |||
+ | * **Use the built-in help**! At the prompt, you can type '' | ||
+ | * **Use tab-completion**! If you're typing a line, you can hit the Tab key in the middle of a word to have IPython automatically finish it for you. This is kind of fun in itself, but what's more important is that if there' | ||
+ | * **Use the source**. One useful magic function is '' | ||
+ | * There are a lot more fun features in IPython: logging, saving and restoration of variables, interaction with the Unix shell, and more. Check out the magic functions mentioned above and read their documentation. | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Numpy Tips ==== | ||
+ | |||
+ | To actually work with the data, you'll need to know a little bit of the things that Numpy supports. The main tool provided by Numpy is its N-dimensional array, or '' | ||
+ | |||
+ | Numpy '' | ||
+ | |||
+ | <code python> | ||
+ | import numpy as N | ||
+ | |||
+ | a = N.linspace (1, 100, 30) # create an array of 30 elements spanning from 1 to 100 linearly | ||
+ | a = a + 1 # now ' | ||
+ | a += 1 # shorthand for the above | ||
+ | a = a / 3 # now ' | ||
+ | a *= 3 # now ' | ||
+ | a = a**2 # square every element of a | ||
+ | |||
+ | b = N.logspace (0, 3, 30) # create an array of 30 elements spanning from 10**0 to 10**3 logarithmically | ||
+ | |||
+ | a = a / b # divide each element of ' | ||
+ | |||
+ | print a.ndim # print the number of dimensions of ' | ||
+ | print a.size # print the number of elemens in ' | ||
+ | print a.shape # print the size of each dimension of ' | ||
+ | print a.dtype # print the kind of data the ' | ||
+ | </ | ||
+ | |||
+ | It's important to remember that, unlike in IDL, you can do fun things with array objects directly: | ||
+ | |||
+ | <code python> | ||
+ | print a.mean () # compute the mean of the elements of ' | ||
+ | print a.std () # standard deviation of the above | ||
+ | print a.min (), a.max (), a.sum () # and so on | ||
+ | a.sort () # sort ' | ||
+ | if a.any (): print ' | ||
+ | if a.all (): print ' | ||
+ | </ | ||
+ | |||
+ | Numpy also has many complex ways that you can index into arrays: | ||
+ | |||
+ | <code python> | ||
+ | a[0] # first element of a | ||
+ | a[-1] # last element of a | ||
+ | a[-2] # second-to-last element of a | ||
+ | a[1:3] # a subarray starting at a[1] and going up to, BUT NOT INCLUDING, a[3] | ||
+ | a[p:q] # more generally: a[p:q] has q - p elements and starts at a[p] | ||
+ | a[1:-1] # every element of ' | ||
+ | a[0: | ||
+ | a[-1:3] # this gives an empty array, since we're trying to go from the end to the beginning | ||
+ | |||
+ | b = N.zeros ((20, 10)) # create a 2-dimensional 20-by-10 array of zeros | ||
+ | b.shape # = (20, 10) | ||
+ | b.size # = 200 | ||
+ | b.ndims # = 2 | ||
+ | |||
+ | b[0] # the first row of ' | ||
+ | b[0,0] # the first element of the first row of ' | ||
+ | b[:,0] # the first column of ' | ||
+ | b[:,0] += 3 # add 3 to each element in the first column of b | ||
+ | b[1:5,2:8] # a 4-by-6 subarray of b | ||
+ | |||
+ | c = N.ones ((4, 3, 2, 6, 2)) # create a 5-dimensional 4-by-3-by-2-by-6-by-2 array of ones ... | ||
+ | </ | ||
+ | |||
+ | Numpy supports IDL-style where statements, with a similar implementation in fact: | ||
+ | |||
+ | <code python> | ||
+ | import numpy as N | ||
+ | x = N.linspace (0, 10, 100) | ||
+ | |||
+ | x[N.where (x < 4)] = 4 # set every element of x that is less than four, to four. | ||
+ | |||
+ | y = x > 8 # y is an array of 100 booleans, True where x > 8, false where not. | ||
+ | x[N.where(y)] = -1 | ||
+ | |||
+ | condition = N.logical_and (x < 4, x > 2) | ||
+ | x[N.where (condition)] = -17 | ||
+ | |||
+ | a = N.linspace (0, 10, 100) | ||
+ | b = N.linspace (0, 100, 100) | ||
+ | w = N.where (a > 5) # you can save the result of a ' | ||
+ | a[w] = 0 | ||
+ | b[w] = -1 | ||
+ | print w[0] # shows the indices where the condition was true | ||
+ | </ | ||
+ | |||
+ | Finally, a rundown of the interesting functions in the '' | ||
+ | |||
+ | <code python> | ||
+ | import numpy as N | ||
+ | |||
+ | print N.pi, N.e # constants. | ||
+ | |||
+ | x = N.linspace (0, 10, 100) # 100-element array going from 0 to 10 | ||
+ | |||
+ | N.sin (x) # returns 100-element array of sin values | ||
+ | N.cos (x) # similar. N.tan (x), N.abs (x), N.log (x), etc. | ||
+ | |||
+ | counts, edges = N.histogram (x) # compute a histogram of x: see the builtin help on N.histogram | ||
+ | |||
+ | b = N.zeros_like (x) # create an array of zeros in the same shape as x | ||
+ | </ | ||