Part 2: Demonstration Exercises

Here are some sample exercises to work through. They demonstrate many techniques that we use all the time.

Beginner Level

This exercise is designed for those who are fairly new to python and coding in general. It asks you to read in a list of numbers from a file and to write an algorithm to sort the list.

  1. Using the techniques described in the example code above, read in the "beginner.txt" file in this directory and store its contents as a list (f.readlines() will be useful). Print your list.
  2. The list you've read in will be a list of strings. Write a for loop that converts each string in the list to an integer (using range(len(list))...). Print your updated list.
  3. Next, create a second, empty list to store the sorted data in.
  4. Now write a for loop that loops over the list you read in from file and:
    • stores the first entry
    • looks at each successive entry in the list and compares it to the stored entry.
    • If an entry is less than the stored entry, replace the stored entry with this new lowest value.
  5. Congratulations, you've now found the lowest value in the list. Take the value stored in your for loop and add it to your second list (using the list.append() method). Use the list.remove(x) method to remove the value you've just added to the second list from the first list.
  6. Now repeat the process in steps 4 and 5 for each value in the initial list (do this by embedding steps 4 and 5 in a for loop; the syntax range(len(list)) will be useful here). [Note, you also could use a while statement, but we'll stick with for loops].
  7. Print out your newly sorted list to make sure your algorithm worked.
  8. If time permits, add a variable verbose, that when it's true you print out the list at each step of the way.
  9. If time permits, come up with a more efficient method for sorting the list (there are many: it's fine to use google to see what sorting algorithms are out there. And of course, there's a python sort command - see if you can figure out how it works).

In [2]:
# Put your code here
pass

In [ ]:
# only run this cell after you finished writing your code
%load beginner_soln.py

Intermediate Level

This exercise is designed for those who are already somewhat comfortable with python and want to learn more about exploiting its capabilities. It asks you to read in a file containing 10 time series, each containing a gaussian radio pulse. Then, using numpy and matplotlib, it asks you to plot the pulse, measure the pulse's signal to noise ratio, and output values in a nicely formatted table.

  1. Read in the file "intermediate.txt" in this directory.
  2. The file contains 10 rows of comma separated numbers. Each row represents the amount of signal output from a radio antenna as a function of time (in 1 second time intervals). Loop through the lines in the file (f.readlines() will be useful here). For each line, do the following:
    1. Convert the line from one long string into a numpy array of floats.
    2. Using matplotlib.pyplot make a plot of the data you just read in as a function of time (hint: you'll have to figure out how many time steps are present in the data).
    3. Using the capabilities of numpy, find the value of the maximum flux in your time series.
    4. Excluding your pulse, (the pulse is in the first half of the time series, so you can cheat and just limit yourself to the second half of the time series) calculate the rms noise in your spectrum. (Recall that the rms is the root mean square - find the mean of the squares of all the points, then take the square root. You might also use np.std() and compare the results (and think about why they are different, if they are different)).
    5. Do a simple estimate of the signal to noise ratio of the pulse as peakflux/rms.
    6. Using a formatted string, print the output signal to noise, peakflux and rms to a descriptive table, rounding each number to two decimal places.
  3. If time permits figure out how to display all your time series on top of one another at the end, rather than having the plots pop up one at a time.
  4. If time permits mess around with fitting the gaussian pulse and come up with other estimates of the signal to noise ratio.

In [ ]:
# Put your code here
pass

In [ ]:
# only run this cell after you finished writing your code
%load beginner_soln.py

Exercise with APLpy and plotting fits images

In this exercise, you will use aplpy ("apple pie") to make an image of a field of ALFALFA data.

  1. Read in the fits file "HI1020_21.mapPYM2.5b.fits" in this directory, and plot it in inverted greyscale.
  2. Overplot a contour at 0.13 mJy/beam.
  3. There are two groups of galaxies in the image. Put a box around each one.
  4. Label the lower left group NGC 3227 group, and the upper right group the NGC 3190 group
  5. Make your axis labels bold, and give the figure a thick border
  6. Save a .png and .eps version of the figure

This is a piece of code used to make a figure from Leisman et al. 2016!


In [ ]:
# put your code here....

only click this after you finished writing your code

WholeField.ipynb


In [ ]:


In [ ]:


In [ ]: