CS 11 Python Track: Assignment 3: Life, NumPy and the Pursuit of Plotting


Goals

In this assignment you will learn some the basics of NumPy (Numeric Python), a standard data manipulation library for Python. You will review plotting, and implement Conway's Game of Life.


Covered this week


Lab3a: Random

Create a file called lab3a.py with these contents:

import random
import numpy as np

# Version without numpy:
def foundation(x, y, z, mu, std):
    '''
    TODO DOCSTRING
    '''
    pass

# Version using numpy:
def foundationNP(x, y, z, mu, std):
    '''
    TODO DOCSTRING
    '''
    pass

Your job will be to replace the pass lines with code as described below. Numbers in bold are time estimates in minutes.

[10] Suppose we are playing a game similar to Minecraft and we want to dig a foundation for a house, so we need a hole that forms a rectangular prism. We want to determine how hard it will be to dig out. Based on the rock types nearby and the quality of our stone pick we know that the number of hits per block is normally distributed with a mean of 3.0 and a standard deviation of 0.5. Fill in the function foundation to model a typical excavation job. Remember that a block takes an integer number of hits to remove. Your foundation function should take the three dimensions of the house (x, y, z lengths, all integers), the average number of hits per block (a float), and the standard deviation (also a float), and returns a triply nested list of stones (i.e. a 3-d array) representing the number of hits it took to dig out each block in the rectangular prism. Remember to use the random library. Make sure to include a good docstring in your function where the TODO DOCSTRING line is above (removing that line, of course!).

Examples:

>>> foundation(2, 3, 4, 3.0, 0.6)
[[[2, 2], [1, 2], [3, 2]], [[2, 2], [2, 3], [3, 3]], [[3, 2], [2, 2], [2, 2]], [[3, 3], [3, 2], [4, 3]]]

[10] Write a function called foundationNP which does the same thing as foundation using the numpy.random submodule, except than instead of returning a list of lists it returns a NumPy array (a true 3d array). This should be much shorter and cleaner. Numpy is meant for these sorts of problems – ones in which you want to work with arrays of data.

In your NumPy code, make sure to specify the element type as int64. Hint: the dtype keyword argument will be useful.

Examples:

>>> foundationNP(2, 3, 4, 3.0, 0.6)
array([[[3 3]
        [4 2]
        [3 3]]

       [[2 2]
        [2 2]
        [3 3]]

       [[1 2]
        [1 3]
        [2 1]]

       [[2 3]
        [3 2]
        [3 3]]])

Note than NumPy arrays display differently than nested lists.

Hopefully by this point in the class, you have realized that the documentation is fairly useful, especially when working with a new library. In addition, in the past few sets, we've given links to the documentation. By now, you hopefully know how to find it yourself. It is expected that you will look up the documentation for the remainder of the course.


Lab3b: Hurricanes

Has the number of hurricanes and tropical storms been increasing in the Atlantic Ocean? Take a look at the data in the file hurricanes.txt.

Before you begin, download the template file lab3b.py. You will edit this file to create your submitted file of the same name. Remove all TODO comments and lines in the code/docstrings and replace them with your own code/docstrings.

[5] Using matplotlib, plot the number of hurricanes versus time. Look at the graph. Do you see a trend? Write your answer in a comment.

[20] Try smoothing out the data by writing a running_mean function. Run the plot with some small amount of smoothing and comment on it. Run the plot with too much smoothing -- to the point where some spikes in the data are lost. What data could be lost? Hint: The numpy.cumsum function might be helpful.

[15] How are Tropical Storms distributed? Are they normally distributed? Plot the Tropical Storms that are not hurricanes with a histogram. Note that named storms include hurricanes and tropical storms. Simulate a normal distribution that matches the average, standard deviation and number of data points as the tropical storm data. Plot that data on the same graph.


Lab3c: Life

Before you begin, download the template file lab3c.py. You will edit this file (remove and replace the lines with TODO) to create your submitted file of the same name.

[30] Take a look at Conway's Game of Life on Wikipedia. In brief, it is a two-dimensional cellular automaton simulator. Every turn, every cell on the board might come to live, remain alive, or have currently living life die depending on how many adjacent cells are currently alive. We are going to work with Life on a torus because it will make the code easier and we won't need to deal with boundaries. This means that cells on the top are adjacent to cells on the bottom and the same is true for the left and right sides. Write a function called life(size, generations) which generates a random board, which is a square of length size (an int), and runs it for generations timesteps. Print every board to the terminal. You will probably want to use helper functions. The supplied code has some suggested helper functions, but you are free to use your own strategy. The only function that actually matters is life.

[15] As seems to be a theme in this class, we are going to repeat the above exercise, but this time using NumPy. Nearly every helper function can be done in a single line with proper applications of NumPy arrays. What was your strategy for determining if a cell should be alive? The naive solution is to check its neighbors. However, suppose you made a few copies of the current board and rotated them. For example:

  1 2 3
  4 5 6
  7 8 9

Might be rotated to the right by one.

  3 1 2
  6 4 5
  7 8 9

If we rotate in all eight directions and then sum up the arrays, we know how many neighbors every cell has. From here, it is fairly easy to get the next generation and then, implement lifeNP. Use the NumPy documentation to find the features that you need. You don't have to get every function into one line (some of them would require messy list comprehensions) but try it out. Note that for these array based tasks, NumPy is very clean.


To hand in

The lab3a.py, lab3b.py, and lab3c.py programs.


Copyright (c) 2017, California Institute of Technology. All rights reserved.