From d50b61b699c72d74d4fac595459ccefe94cd7762 Mon Sep 17 00:00:00 2001 From: John Blischak Date: Sun, 16 Jun 2013 21:11:48 -0500 Subject: [PATCH] Removed Fibonacci and close_line testing examples. --- 09-testing/Readme.md | 190 +-------------------------------------- 09-testing/close_line.py | 69 -------------- 09-testing/evo_sol1.png | Bin 5494 -> 0 bytes 3 files changed, 1 insertion(+), 258 deletions(-) delete mode 100644 09-testing/close_line.py delete mode 100644 09-testing/evo_sol1.png diff --git a/09-testing/Readme.md b/09-testing/Readme.md index 1026ac5..4aa3da6 100644 --- a/09-testing/Readme.md +++ b/09-testing/Readme.md @@ -337,7 +337,7 @@ list of strings? **Example**: - nosetests test_mean.py + nosetests -v test_mean.py # Test Driven Development @@ -357,192 +357,4 @@ style was put forth most strongly by [Kent Beck in ## A TDD Example -Say you want to write a fib() function which generates values of the -Fibonacci sequence of given indexes. You would - of course - start by -writing the test, possibly testing a single value: - -```python -from nose.tools import assert_equal - -from pisa import fib - -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) -``` - -You would *then* go ahead and write the actual function: - -```python -def fib(n): - # you snarky so-and-so - return 1 -``` - -And that is it right?! Well, not quite. This implementation fails for -most other values. Adding tests we see that: - -```python -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) - - -def test_fib2(): - obs = fib(0) - exp = 0 - assert_equal(obs, exp) - - obs = fib(1) - exp = 1 - assert_equal(obs, exp) -``` - -This extra test now requires that we bother to implement at least the -initial values: - -```python -def fib(n): - # a little better - if n == 0 or n == 1: - return n - return 1 -``` - -However, this function still falls over for `2 < n`. Time for more -tests! - -```python -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) - - -def test_fib2(): - obs = fib(0) - exp = 0 - assert_equal(obs, exp) - - obs = fib(1) - exp = 1 - assert_equal(obs, exp) - - -def test_fib3(): - obs = fib(3) - exp = 2 - assert_equal(obs, exp) - - obs = fib(6) - exp = 8 - assert_equal(obs, exp) -``` - -At this point, we had better go ahead and try do the right thing... - -```python -def fib(n): - # finally, some math - if n == 0 or n == 1: - return n - else: - return fib(n - 1) + fib(n - 2) -``` - -Here it becomes very tempting to take an extended coffee break or -possibly a power lunch. But then you remember those pesky negative -numbers and floats. Perhaps the right thing to do here is to just be -undefined. - -```python -def test_fib1(): - obs = fib(2) - exp = 1 - assert_equal(obs, exp) - - -def test_fib2(): - obs = fib(0) - exp = 0 - assert_equal(obs, exp) - - obs = fib(1) - exp = 1 - assert_equal(obs, exp) - - -def test_fib3(): - obs = fib(3) - exp = 2 - assert_equal(obs, exp) - - obs = fib(6) - exp = 8 - assert_equal(obs, exp) - - -def test_fib3(): - obs = fib(13.37) - exp = NotImplemented - assert_equal(obs, exp) - - obs = fib(-9000) - exp = NotImplemented - assert_equal(obs, exp) -``` - -This means that it is time to add the appropriate case to the function -itself: - -```python -def fib(n): - # sequence and you shall find - if n < 0 or int(n) != n: - return NotImplemented - elif n == 0 or n == 1: - return n - else: - return fib(n - 1) + fib(n - 2) -``` - -# Quality Assurance Exercise - -Can you think of other tests to make for the fibonacci function? I promise there -are at least two. - -Implement one new test in test_fib.py, run nosetests, and if it fails, implement -a more robust function for that case. - -And thus - finally - we have a robust function together with working -tests! - -# Exercise - -**The Problem:** In 2D or 3D, we have two points (p1 and p2) which -define a line segment. Additionally there exists experimental data which -can be anywhere in the domain. Find the data point which is closest to -the line segment. - -In the `close_line.py` file there are four different implementations -which all solve this problem. [You can read more about them -here.](http://inscight.org/2012/03/31/evolution_of_a_solution/) However, -there are no tests! Please write from scratch a `test_close_line.py` -file which tests the closest\_data\_to\_line() functions. - -*Hint:* you can use one implementation function to test another. Below -is some sample data to help you get started. - -![image](https://github.com/thehackerwithin/UofCSCBC2012/raw/scopz/5-Testing/evo_sol1.png) -> - - -```python -import numpy as np - -p1 = np.array([0.0, 0.0]) -p2 = np.array([1.0, 1.0]) -data = np.array([[0.3, 0.6], [0.25, 0.5], [1.0, 0.75]]) -``` diff --git a/09-testing/close_line.py b/09-testing/close_line.py deleted file mode 100644 index a6a5c5e..0000000 --- a/09-testing/close_line.py +++ /dev/null @@ -1,69 +0,0 @@ -import numpy as np -from scipy.optimize import fmin - -# -# Attempt 1 -# - -def point_on_line1(x, p1, p2): - y = p1[1] + (x - p1[0])*(p2[1] - p1[1]) / (p2[0] - p1[0]) - return np.array([x, y]) - - -def dist_from_line1(x, pdata, p1, p2): - pline = point_on_line1(x, p1, p2) - return np.sqrt(np.sum((pline - pdata)**2)) - - -def closest_data_to_line1(data, p1, p2): - dists = np.empty(len(data), dtype=float) - for i, pdata in enumerate(data): - x = fmin(dist_from_line1, p1[0], (pdata, p1, p2), disp=False)[0] - dists[i] = dist_from_line1(x, pdata, p1, p2) - imin = np.argmin(dists) - return imin, data[imin] - - -# -# Attempt 2 -# - -def dist_from_line2(pdata, p1, p2): - a = np.sqrt(np.sum((p1 - pdata)**2)) - b = np.sqrt(np.sum((p2 - pdata)**2)) - c = np.sqrt(np.sum((p2 - p1)**2)) - h = a * np.sqrt(1.0 - ((a**2 + c**2 - b**2) / (2.0 * a * c))**2) - return h - -def closest_data_to_line2(data, p1, p2): - dists = np.empty(len(data), dtype=float) - for i, pdata in enumerate(data): - dists[i] = dist_from_line2(pdata, p1, p2) - imin = np.argmin(dists) - return imin, data[imin] - -# -# Attempt 3 -# - -def perimeter3(pdata, p1, p2): - a = np.sqrt(np.sum((p1 - pdata)**2)) - b = np.sqrt(np.sum((p2 - pdata)**2)) - c = np.sqrt(np.sum((p2 - p1)**2)) - return (a + b + c) - -def closest_data_to_line3(data, p1, p2): - peris = np.empty(len(data), dtype=float) - for i, pdata in enumerate(data): - peris[i] = perimeter3(pdata, p1, p2) - imin = np.argmin(peris) - return imin, data[imin] - -# -# Attempt 4 -# - -def closest_data_to_line4(data, p1, p2): - return data[np.argmin(np.sqrt(np.sum((p1 - data)**2, axis=1)) + \ - np.sqrt(np.sum((p2 - data)**2, axis=1)))] - diff --git a/09-testing/evo_sol1.png b/09-testing/evo_sol1.png deleted file mode 100644 index cb131bf70346caec2665b7dd04aa7d0a359b4463..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 5494 zcmcIoXIN9|woapjCV_}bl@K5Z0!pt&jAAG;hz03o=u!*;X~Je41VlhL9Vs!6q9anI zivkA)Ce&b~2-XNuL^L!52zTRn=iWKzp6A>@^W@21?^^3!d+&VTx3j-@rBNx?;v%vl zFc?gne2{b)27|Mq?+sx=D6*(kR{>n=zwzJ;M-S zMRuw8@iTxR3cw*6LIk!Mg@x@9{y_*IKbBB2nV!8G4u+9=2IN+A)&sKT zMNSCWJ(u$);40E{y64g+N@G69C_8?4`s}a|c@4)pq{i>WuQ$c7T7gQkffrz6v!J^O zH&(lMZRtZP-XoSXm#^L&$LFux^D@j76kyMB3^~)S>@efkCWmZzzw=CHM~9kVBiir@ zwm)(?Q?GPMsCUYulq1?`L4Y-UqUyLMx3mu8`IY!R;azie2e}u-2X>5Fcj8dyQAJ<*Ae1n_jPcTjN<+W^-=JwaUQWUY^kyFISBj<-PTmS+8m< z;tb%|OE|6gwtP<6o%Q=)XYggm`3XuVTDx9`>IcD`cRdFb>6Yp1!_beTg8qv9RomM2 zZ_$yR(;jDDO($ND&4JaCh_y;=kn@}pJs_7~wths$x@!7s&AZ_>7f|Px91fO&VSf~w zN0ysU@kiH7@6j@b6KudN|C)bvG#<;`1S|>%EFzaD9ZTmPeoQ>HM(-S+u;r!nR`K79 zcL@>WQ2HzIGvBrit~}($%({Gu^k|M>Q#zU@1{_0g)D=O)G^!b>H#JWuv)~w*}GCvx&nxz2h+r|(;X2KmB=&8yUG$1z(f8um?(T*>&#uL#6Ax$NXwju}*T}75{H0-4KKD{pT&Tz*);6j1CgW#qc<1o5 z3SmyLlfAB!#Iih&Z*NxmiPg3jZL70N6R#qdz}1s7ay#Tt3w-hi@8MX@;xCZEm;NY~ z=Mi~zE+%T81GB5X?{B{~ zs4}Y>GqiXP(XNf=`jytcn?kY)@QJ?c;rKPjhTZ2WIh-E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