[Numpy-svn] r8415 - in trunk/numpy/lib: . tests

numpy-svn at scipy.org numpy-svn at scipy.org
Sun May 16 04:31:03 EDT 2010


Author: stefan
Date: 2010-05-16 03:31:03 -0500 (Sun, 16 May 2010)
New Revision: 8415

Modified:
   trunk/numpy/lib/function_base.py
   trunk/numpy/lib/tests/test_function_base.py
Log:
BUG: Correctly handle in-place output in percentile.

Modified: trunk/numpy/lib/function_base.py
===================================================================
--- trunk/numpy/lib/function_base.py	2010-05-16 08:30:47 UTC (rev 8414)
+++ trunk/numpy/lib/function_base.py	2010-05-16 08:31:03 UTC (rev 8415)
@@ -2964,9 +2964,15 @@
 # handle sequence of q's without calling sort multiple times
 def _compute_qth_percentile(sorted, q, axis, out):
     if not isscalar(q):
-        return [_compute_qth_percentile(sorted, qi, axis, out) 
-                    for qi in q]
-    q = q / 100.0    
+        p = [_compute_qth_percentile(sorted, qi, axis, None)
+             for qi in q]
+
+        if out is not None:
+            out.flat = p
+
+        return p
+
+    q = q / 100.0
     if (q < 0) or (q > 1):
         raise ValueError, "percentile must be either in the range [0,100]"
 

Modified: trunk/numpy/lib/tests/test_function_base.py
===================================================================
--- trunk/numpy/lib/tests/test_function_base.py	2010-05-16 08:30:47 UTC (rev 8414)
+++ trunk/numpy/lib/tests/test_function_base.py	2010-05-16 08:31:03 UTC (rev 8415)
@@ -968,7 +968,26 @@
 
 
 def test_percentile_list():
-    assert_equal(np.percentile([1,2,3], 0), 1)
+    assert_equal(np.percentile([1, 2, 3], 0), 1)
 
+def test_percentile_out():
+    x = np.array([1, 2, 3])
+    y = np.zeros((3,))
+    p = (1, 2, 3)
+    np.percentile(x, p, out=y)
+    assert_equal(y, np.percentile(x, p))
+
+    x = np.array([[1, 2, 3],
+                  [4, 5, 6]])
+
+    y = np.zeros((3, 3))
+    np.percentile(x, p, axis=0, out=y)
+    assert_equal(y, np.percentile(x, p, axis=0))
+
+    y = np.zeros((3, 2))
+    np.percentile(x, p, axis=1, out=y)
+    assert_equal(y, np.percentile(x, p, axis=1))
+
+
 if __name__ == "__main__":
     run_module_suite()




More information about the Numpy-svn mailing list