From: Anthony Scopatz Date: Tue, 3 Apr 2012 04:29:41 +0000 (-0500) Subject: Updated testing. X-Git-Url: http://git.tremily.us/?p=swc-testing-nose.git;a=commitdiff_plain;h=cd9721d13d513445dbfcbd34acac212c06de3e87 Updated testing. --- 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 0000000..cb131bf Binary files /dev/null and b/5-Testing/evo_sol1.png differ