From cd9721d13d513445dbfcbd34acac212c06de3e87 Mon Sep 17 00:00:00 2001 From: Anthony Scopatz Date: Mon, 2 Apr 2012 23:29:41 -0500 Subject: [PATCH] Updated testing. --- 5-Testing/Readme.md | 22 +++++++++++-- 5-Testing/Readme.rst | 22 +++++++++++-- 5-Testing/close_line.py | 69 ++++++++++++++++++++++++++++++++++++++++ 5-Testing/evo_sol1.png | Bin 0 -> 5494 bytes 4 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 5-Testing/close_line.py create mode 100644 5-Testing/evo_sol1.png diff --git a/5-Testing/Readme.md b/5-Testing/Readme.md index 7a4c7d2..1b58882 100644 --- a/5-Testing/Readme.md +++ b/5-Testing/Readme.md @@ -10,7 +10,7 @@ Documentation](https://github.com/thehackerwithin/UofCSCBC2012/tree/master/6-Doc **Based on materials by Katy Huff, Rachel Slaybaugh, and Anthony Scopatz** -![image](test_prod.jpg) +![image](https://github.com/thehackerwithin/UofCSCBC2012/raw/scopz/5-Testing/test_prod.jpg) # What is testing? Software testing is a process by which one or more expected behaviors @@ -518,4 +518,22 @@ tests! # Exercise -![image](http://s3.amazonaws.com/inscight/img/blog/evo_sol1.png) +**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. + +![image](https://github.com/thehackerwithin/UofCSCBC2012/raw/scopz/5-Testing/evo_sol1.png) +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 implentation to test another. Below is some sample data to +help you get started. + +```python +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/5-Testing/Readme.rst b/5-Testing/Readme.rst index 9e06959..b22f7d5 100644 --- a/5-Testing/Readme.rst +++ b/5-Testing/Readme.rst @@ -9,7 +9,7 @@ **Based on materials by Katy Huff, Rachel Slaybaugh, and Anthony Scopatz** -.. image:: test_prod.jpg +.. image:: https://github.com/thehackerwithin/UofCSCBC2012/raw/scopz/5-Testing/test_prod.jpg What is testing? @@ -497,4 +497,22 @@ And thus - finally - we have a robust function together with working tests! Exercise ======== -.. image:: http://s3.amazonaws.com/inscight/img/blog/evo_sol1.png +**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. + +.. image:: https://github.com/thehackerwithin/UofCSCBC2012/raw/scopz/5-Testing/evo_sol1.png + +In the ``close_line.py`` file there are four different implementations which all +solve this problem. `You can read more about them here.`_ 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 implentation to test another. Below is some sample data +to help you get started. + +.. _You can read more about them here.: http://inscight.org/2012/03/31/evolution_of_a_solution/ + +.. code-block:: python + + 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/5-Testing/close_line.py b/5-Testing/close_line.py new file mode 100644 index 0000000..bed15db --- /dev/null +++ b/5-Testing/close_line.py @@ -0,0 +1,69 @@ +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) + 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/5-Testing/evo_sol1.png b/5-Testing/evo_sol1.png new file mode 100644 index 0000000000000000000000000000000000000000..cb131bf70346caec2665b7dd04aa7d0a359b4463 GIT binary patch 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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