Created
October 5, 2017 20:00
-
-
Save mbeyeler/dbfe060889af931d22ada96a86724957 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Recap on indexing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"horrible_list_name = [3]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"data = numpy.loadtxt?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"data = numpy.loadtxt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"data = numpy.loadtxt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"data = numpy.loadtxt('data/inflammation-01.csv', delimiter=',')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(60, 40)" | |
] | |
}, | |
"execution_count": 6, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"data.shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(40,)" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Shape of first row\n", | |
"data[0].shape" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1.0" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Third row, second column\n", | |
"data[2,1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"row0 = data[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 0., 1., 1.])" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Indexing: row0[start:stop:step]\n", | |
"row0[0:5:2]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 0., 0., 1., 3., 1.])" | |
] | |
}, | |
"execution_count": 11, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"row0[0:5]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.0" | |
] | |
}, | |
"execution_count": 12, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# last element\n", | |
"row0[-1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"0.0" | |
] | |
}, | |
"execution_count": 13, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# second to last element\n", | |
"row0[-2]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 0., 0., 1., 3., 1., 2., 4., 7., 8., 3., 3.,\n", | |
" 3., 10., 5., 7., 4., 7., 7., 12., 18., 6., 13.,\n", | |
" 11., 11., 7., 7., 4., 6., 8., 8., 4., 4., 5.,\n", | |
" 7., 3., 4., 2., 3., 0., 0.])" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"row0[:]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 15, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"array([ 0., 0., 3., 2., 4., 3., 7., 5., 4., 4., 8.,\n", | |
" 8., 6., 4., 7., 7., 11., 11., 13., 6., 18., 12.,\n", | |
" 7., 7., 4., 7., 5., 10., 3., 3., 3., 8., 7.,\n", | |
" 4., 2., 1., 3., 1., 0., 0.])" | |
] | |
}, | |
"execution_count": 15, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Reverse order of columns in row\n", | |
"row0[::-1]" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Recap on for loops" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"o\n", | |
"x\n", | |
"y\n", | |
"g\n", | |
"e\n", | |
"n\n" | |
] | |
} | |
], | |
"source": [ | |
"word = 'oxygen'\n", | |
"for char in word:\n", | |
" print(char)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"oxygen\n", | |
"o\n", | |
"x\n", | |
"y\n", | |
"g\n", | |
"e\n", | |
"n\n", | |
"nitrogen\n", | |
"n\n", | |
"i\n", | |
"t\n", | |
"r\n", | |
"o\n", | |
"g\n", | |
"e\n", | |
"n\n" | |
] | |
} | |
], | |
"source": [ | |
"words = ['oxygen', 'nitrogen']\n", | |
"for wrd in words:\n", | |
" print(wrd)\n", | |
" for char in wrd:\n", | |
" print(char)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"char\t data\t horrible_list_name\t numpy\t row0\t word\t words\t wrd\t \n" | |
] | |
} | |
], | |
"source": [ | |
"who" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Variable Type Data/Info\n", | |
"-----------------------------------------\n", | |
"char str n\n", | |
"data ndarray 60x40: 2400 elems, type `float64`, 19200 bytes\n", | |
"horrible_list_name list n=1\n", | |
"numpy module <module 'numpy' from 'c:\\<...>ges\\\\numpy\\\\__init__.py'>\n", | |
"row0 ndarray 40: 40 elems, type `float64`, 320 bytes\n", | |
"word str oxygen\n", | |
"words list n=2\n", | |
"wrd str nitrogen\n" | |
] | |
} | |
], | |
"source": [ | |
"whos" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" # Recap on conditionals" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 20, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"num = 0.5238239844329" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 21, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0 is positive\n" | |
] | |
} | |
], | |
"source": [ | |
"if num > 0:\n", | |
" print(\"%d is positive\" % num)\n", | |
"elif num == 0:\n", | |
" print(\"%.2f is zero\" % num)\n", | |
"else:\n", | |
" print(num, \"is negative\")" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Recap on functions" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 22, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def print_num(num):\n", | |
" print(num)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 23, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5\n" | |
] | |
} | |
], | |
"source": [ | |
"print_num(5)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 24, | |
"metadata": { | |
"collapsed": true, | |
"deletable": true, | |
"editable": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Default arguments:\n", | |
"def print_num(num1, num2=6, num3=17):\n", | |
" print('num1:', num1, 'num2:', num2, 'num3:', num3)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 25, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"num1: 1 num2: 2 num3: 17\n" | |
] | |
} | |
], | |
"source": [ | |
"print_num(1, 2)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 26, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"num1: 1 num2: 6 num3: 17\n" | |
] | |
} | |
], | |
"source": [ | |
"print_num(1)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"num1: 1 num2: 6 num3: 22\n" | |
] | |
} | |
], | |
"source": [ | |
"print_num(1, num3=22)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 28, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def fahr_to_kelvin(t_fahr):\n", | |
" t_kelv = (t_fahr - 32.0) * (5/9) + 273.15\n", | |
" return t_kelv" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 29, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"255.3722222222222" | |
] | |
}, | |
"execution_count": 29, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"fahr_to_kelvin(0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 30, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"273.15" | |
] | |
}, | |
"execution_count": 30, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# Better example\n", | |
"fahr_to_kelvin(32)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 31, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def kelvin_to_celsius(kelv):\n", | |
" return kelv - 273.15" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 32, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# The naive way:\n", | |
"def fahr_to_celsius(t_fahr):\n", | |
" t_cels = (t_fahr - 32.0) * (5/9)\n", | |
" return t_cels" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 33, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.0 C\n" | |
] | |
} | |
], | |
"source": [ | |
"# More meaningful print:\n", | |
"print(fahr_to_celsius(32), 'C')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 34, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Better: Reuse code\n", | |
"def fahr_to_celsius_v2(t_fahr):\n", | |
" t_kelv = fahr_to_kelvin(t_fahr)\n", | |
" t_cels = kelvin_to_celsius(t_kelv)\n", | |
" return t_cels" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 35, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"0.0 C\n" | |
] | |
} | |
], | |
"source": [ | |
"print(fahr_to_celsius_v2(32), 'C')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Writing test functions\n", | |
"\n", | |
"Use assertion statements:\n", | |
"\n", | |
" assert condition, msg\n", | |
"\n", | |
"`condition` has to evaluate to True or False:\n", | |
"- If condition is True, nothing happens\n", | |
"- If condition is False, throws error message `msg`\n", | |
"\n", | |
"`condition` could be `a > 3`, `func(x) == 7`, etc." | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "AssertionError", | |
"evalue": "-3 is not greater than 0", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-36-a93728b77f3c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'-3 is not greater than 0'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;31mAssertionError\u001b[0m: -3 is not greater than 0" | |
] | |
} | |
], | |
"source": [ | |
"assert -3 > 0, '-3 is not greater than 0'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def test_fahr_to_celsius_v2():\n", | |
" assert fahr_to_celsius_v2(32.0) == 0.0" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# No output == every assert passed == everything good\n", | |
"test_fahr_to_celsius_v2()" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Defensive programming\n", | |
"\n", | |
"Usual approach:\n", | |
"1. Write function\n", | |
"2. Write test function\n", | |
"\n", | |
"Defensive programming\n", | |
"1. Write test function\n", | |
"2. Write function" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Thus write test function first" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# A dummy function\n", | |
"def range_overlap(ranges):\n", | |
" \"\"\"Returns the common overlap among a set of (low, high) ranges\"\"\"\n", | |
" return 0\n", | |
"\n", | |
"def test_range_overlap():\n", | |
" assert range_overlap([(0.0, 1.0)]) == (0.0, 1.0)\n", | |
" assert range_overlap([(0.0, 10.0), (2.0, 3.0)]) == (2.0, 3.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "AssertionError", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-40-cf9215c96457>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtest_range_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32m<ipython-input-39-365d16466402>\u001b[0m in \u001b[0;36mtest_range_overlap\u001b[1;34m()\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mtest_range_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 8\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m2.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m2.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mAssertionError\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"test_range_overlap()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy\n", | |
"def range_overlap(ranges):\n", | |
" \"\"\"Finds the common overlap among a set of (low, high) ranges\n", | |
" \n", | |
" This is a longer description of the function etc.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" ranges : list\n", | |
" List of ranges with (low, high) bounds\n", | |
" \n", | |
" Returns\n", | |
" -------\n", | |
" Returns the common overlap etc.\n", | |
" \n", | |
" \"\"\"\n", | |
" # Initialize lower/upper bounds\n", | |
" highest_of_lower_bounds = -numpy.inf\n", | |
" lowest_of_higher_bounds = numpy.inf\n", | |
" \n", | |
" for (low, high) in ranges:\n", | |
" # Iteratively find the max of all lower bounds\n", | |
" highest_of_lower_bounds = max(low, highest_of_lower_bounds)\n", | |
" \n", | |
" # Iteratively find the min of all upper bounds\n", | |
" lowest_of_higher_bounds = min(high, lowest_of_higher_bounds)\n", | |
" \n", | |
" return (highest_of_lower_bounds, lowest_of_higher_bounds)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"test_range_overlap()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Test the example from the lecture notes\n", | |
"r1 = (-3.0, 5.0)\n", | |
"r2 = (0.0, 4.5)\n", | |
"r3 = (-1.5, 2)\n", | |
"ranges = [r1, r2, r3]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.0, 2)" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"range_overlap(ranges)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## Dealing with edge cases, improving the tests" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"def test_range_overlap():\n", | |
" assert range_overlap([(0.0, 1.0)]) == (0.0, 1.0)\n", | |
" assert range_overlap([(0.0, 10.0), (2.0, 3.0)]) == (2.0, 3.0)\n", | |
" assert range_overlap([(1.0, 0.0)]) == None\n", | |
" assert range_overlap([(0.0, 1.0), (2.0, 10.0)]) == None" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 46, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"ename": "AssertionError", | |
"evalue": "", | |
"output_type": "error", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", | |
"\u001b[1;31mAssertionError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-46-f2236e633213>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;31m# Edge cases break the function:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mtest_range_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32m<ipython-input-45-395cbf84d0a0>\u001b[0m in \u001b[0;36mtest_range_overlap\u001b[1;34m()\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m2.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m2.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 5\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mrange_overlap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m1.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m2.0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10.0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mAssertionError\u001b[0m: " | |
] | |
} | |
], | |
"source": [ | |
"# Edge cases break the function:\n", | |
"test_range_overlap()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(1.0, 0.0)" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# How to fix?\n", | |
"# Investigate what happens for nonsensical input, then fix the function:\n", | |
"range_overlap([(1.0, 0.0)])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy\n", | |
"def range_overlap(ranges):\n", | |
" \"\"\"Finds the common overlap among a set of (low, high) ranges\n", | |
" \n", | |
" This is a longer description of hte function.\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" ranges : list\n", | |
" List of ranges with (low, high) bounds\n", | |
" \n", | |
" Returns\n", | |
" -------\n", | |
" Returns the common overlap ....\n", | |
" \n", | |
" \"\"\"\n", | |
" # Initialize lower/upper bounds\n", | |
" highest_of_lower_bounds = -numpy.inf\n", | |
" lowest_of_higher_bounds = numpy.inf\n", | |
" \n", | |
" for (low, high) in ranges:\n", | |
" if low >= high:\n", | |
" return None\n", | |
" #raise ValueError('low must be lower than high')\n", | |
"\n", | |
" # Iteratively find the max of all lower bounds\n", | |
" highest_of_lower_bounds = max(low, highest_of_lower_bounds)\n", | |
" \n", | |
" # Iteratively find the min of all upper bounds\n", | |
" lowest_of_higher_bounds = min(high, lowest_of_higher_bounds)\n", | |
" \n", | |
" if highest_of_lower_bounds > lowest_of_higher_bounds:\n", | |
" return None\n", | |
" \n", | |
" return (highest_of_lower_bounds, lowest_of_higher_bounds)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Now the errors are gone: no output\n", | |
"test_range_overlap()" | |
] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment