{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MCS 275 Spring 2022 Worksheet 10\n",
"\n",
"* Course instructor: David Dumas"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Topics\n",
"\n",
"The main topics of this worksheet are:\n",
"* Pillow\n",
"* numpy\n",
"\n",
"The problems are labeled according to which of these topics they cover. Make sure to get some practice with each topic.\n",
"\n",
"## Resources\n",
"\n",
"These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.\n",
"\n",
"* [Lecture 24](http://dumas.io/teaching/2022/spring/mcs275/slides/lecture24.html)\n",
"* [Lecture 25](http://dumas.io/teaching/2022/spring/mcs275/slides/lecture25.html)\n",
"* [Lecture 26](http://dumas.io/teaching/2022/spring/mcs275/slides/lecture26.html)\n",
"* [Chapter 2 of VanderPlas](https://jakevdp.github.io/PythonDataScienceHandbook/index.html)\n",
"* [Downey's book](https://greenteapress.com/thinkpython2/html/)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1. Made to order (Pillow and numpy)\n",
"\n",
"#### A. Here is a 256x256 PNG image:\n",
"\n",
"![colorful image](images/ws10prob1goal.png)\n",
"\n",
"Make a Python script that uses Pillow to create an image that is identical to this one (or as close as you can get). There are lots of ways to do this, and the problem is testing your ability to see a pattern in the image and express it in a Python program that uses the Pillow API.\n",
"\n",
"#### B. Here is a 16x16 numpy matrix of integers (dtype int64):\n",
"\n",
"```\n",
"array([[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
" [20, -1, 30, -1, 40, -1, 50, -1, 60, -1, 70, -1, 80, -1, 90, -1],\n",
" [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
" [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
" [-1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1],\n",
" [20, 10, 30, -1, 40, -1, 50, 10, 60, -1, 70, -1, 80, 10, 90, -1],\n",
" [-1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1],\n",
" [-1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1],\n",
" [-1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1],\n",
" [20, 10, 30, -1, 40, -1, 50, 10, 60, -1, 70, -1, 80, 10, 90, -1],\n",
" [-1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1],\n",
" [-1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1, 10, -1, -1],\n",
" [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
" [20, -1, 30, -1, 40, -1, 50, -1, 60, -1, 70, -1, 80, -1, 90, -1],\n",
" [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],\n",
" [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]])\n",
"```\n",
"Find a way to produce this matrix and store it in a variable named `A`. \n",
"\n",
"This is easy to do if you just copy the matrix into your source code. But you can do it with much less code using `numpy` indexing. (You'll want to examine the matrix carefully to spot all the patterns!) What's the shortest way to do it? I was able to do it in 3 lines, 70 characters total, including newline characters (but not counting the line that imports `numpy`). Can you do better?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Needle in a haystack, part 1 (Pillow)\n",
"\n",
"Download and extract this zip file:\n",
"* [haystacks.zip](https://dumas.io/teaching/2022/spring/mcs275/data/haystacks.zip)\n",
"\n",
"It contains 1024 image files in two directories (`haystack1` and `haystack2`). If you look at them, they all appear to contain black and white \"static\". These files are used in this problem and the next one.\n",
"\n",
"Let's say that a pixel is **bluish** if its blue component is larger than its red or green components. So, for example, (62,30,62) is not bluish, but (5,6,7) is bluish.\n",
"\n",
"The files in directory `haystack1` are numbered `000` to `511`. They are PNG image files, all of the same size. Among all of these image files, there is only a single bluish pixel. Which file is it in, and at what coordinates?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Needle in a haystack, part 2 (Pillow)\n",
"\n",
"This is a more complex version of the previous problem. Only work on it if you will still have enough time to do the numpy problems below. (If unsure, skip ahead for now and come back to this later.)\n",
"\n",
"The files in directory `haystack2` have 36-character filenames (plus an extension \".png\"), and have varying sizes. They contain a secret message, which you can decode as follows:\n",
"\n",
"Examine all of the image files in alphabetical order. In each one, look for bluish pixels. There may be no bluish pixels at all, or a single one, or many of them. Whenever a bluish pixel is found, let (x,y) be its position and let r be the remainder of dividing x+y by 36. Take the character in the filename that appears at index r. If that character is a digit, ignore it. Otherwise, add that character to the end of the message.\n",
"\n",
"*Hint: It may be helpful to use `os.listdir` from the `os` module. It takes a directory name and returns a list of filenames in that directory.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4. Max, Min, Mean and RMS (numpy)\n",
"\n",
"Download and unzip this file to obtain a single CSV file:\n",
"\n",
"* [widecsv.zip](https://dumas.io/teaching/2022/spring/mcs275/data/widecsv.zip)\n",
"\n",
"It has no header row. There are 30 rows of data, each of which has 10,000 fields which are floating-point numbers.\n",
"\n",
"Read the file and put each line into a numpy vector. Then, compute the following four quantities related to the entries in that vector:\n",
"* The maximum of the entries\n",
"* The minimum of the entries\n",
"* The average (arithmetic mean) of the entries\n",
"* The square root of the sum of the squares of the entries (sometimes called the *norm* or the *$\\ell^2$ norm*)\n",
"\n",
"Write these rowwise summary statistics to an output CSV file that has the following header row:\n",
"```\n",
"max,min,mean,norm\n",
"```\n",
"which is then followed by 30 lines, one for each line in the input file.\n",
"\n",
"Once you have a row of the input file converted to a numpy array, there shouldn't be any need for explicit iteration over its entries. \n",
"\n",
"#### IMPORTANT NOTES\n",
"\n",
"Remember that the CSV module always gives you individual items from a row as strings, and so you'll need to convert them to floats.\n",
"\n",
"Also, in our in-class use of the CSV module we used `csv.DictReader` to read data from CSV files, and that class requires a header row to be present. For this problem you need to instead use `csv.reader`. It operates similarly but returns a list for each row (rather than a dictionary). Here's a sample of its use:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"\n",
"with open(\"series.csv\",\"r\",encoding=\"UTF-8\",newline=\"\") as fp:\n",
" rdr = csv.reader(fp)\n",
" for row in rdr:\n",
" print(\"Got a row with {} items in it.\".format(len(row)))\n",
" print(\"The item at index 8 in this row is:\",row[8])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## 5. Impact crater (numpy)\n",
"\n",
"Write a function `crater(n)` that takes a single argument, an integer `n`, and returns a numpy array of shape (n,n) whose entry at position (i,j) is equal to\n",
"$$ \\sin \\left ( \\frac{\\pi i}{n-1} \\right ) \\sin \\left ( \\frac{\\pi j}{n-1} \\right ) $$\n",
"unless that quantity is larger than 0.7, in which case the entry should instead have the value 0.\n",
"\n",
"Use numpy arrays, and try to avoid loops.\n",
"\n",
"**Hint: numpy as the constant $\\pi$ built in as `np.pi`**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here's an example of the expected result for n=8."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[0. 0. 0. 0. 0. 0. 0. 0. ]\n",
" [0. 0.188 0.339 0.423 0.423 0.339 0.188 0. ]\n",
" [0. 0.339 0.611 0. 0. 0.611 0.339 0. ]\n",
" [0. 0.423 0. 0. 0. 0. 0.423 0. ]\n",
" [0. 0.423 0. 0. 0. 0. 0.423 0. ]\n",
" [0. 0.339 0.611 0. 0. 0.611 0.339 0. ]\n",
" [0. 0.188 0.339 0.423 0.423 0.339 0.188 0. ]\n",
" [0. 0. 0. 0. 0. 0. 0. 0. ]]\n"
]
}
],
"source": [
"# The next line asks numpy to alter the way it prints arrays\n",
"# so only three decimal digits are shown, without scientific\n",
"# notation. That will make the next test result easier to\n",
"# read.\n",
"np.set_printoptions(precision=3,suppress=True)\n",
"\n",
"# Show the 8x8 return value\n",
"print(crater(8))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also, can you explain the name of this problem?"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 4
}