[Numpy-svn] r2859 - in trunk/numpy: core/include/numpy core/src lib lib/tests

numpy-svn at scipy.org numpy-svn at scipy.org
Thu Jul 20 18:58:41 EDT 2006


Author: oliphant
Date: 2006-07-20 17:58:35 -0500 (Thu, 20 Jul 2006)
New Revision: 2859

Modified:
   trunk/numpy/core/include/numpy/arrayobject.h
   trunk/numpy/core/src/arrayobject.c
   trunk/numpy/core/src/scalartypes.inc.src
   trunk/numpy/lib/tests/test_twodim_base.py
   trunk/numpy/lib/twodim_base.py
Log:
Fixed a memory-leak in EnsureArray.  Added __array_priority__ to array scalars.  Added bug-fixes part of david huard's histogramdd ticket.

Modified: trunk/numpy/core/include/numpy/arrayobject.h
===================================================================
--- trunk/numpy/core/include/numpy/arrayobject.h	2006-07-20 22:48:49 UTC (rev 2858)
+++ trunk/numpy/core/include/numpy/arrayobject.h	2006-07-20 22:58:35 UTC (rev 2859)
@@ -127,6 +127,9 @@
 /* default subtype priority */
 #define NPY_SUBTYPE_PRIORITY 1.0
 
+/* default scalar priority */
+#define NPY_SCALAR_PRIORITY -1000000.0
+
 /* How many floating point types are there */
 #define NPY_NUM_FLOATTYPE 3
 

Modified: trunk/numpy/core/src/arrayobject.c
===================================================================
--- trunk/numpy/core/src/arrayobject.c	2006-07-20 22:48:49 UTC (rev 2858)
+++ trunk/numpy/core/src/arrayobject.c	2006-07-20 22:58:35 UTC (rev 2859)
@@ -8233,8 +8233,8 @@
 	if (PyArray_Check(op)) {
 		new = PyArray_View((PyArrayObject *)op, NULL, &PyArray_Type);
 		Py_DECREF(op);
+		return new;
 	}
-	
         if (PyArray_IsScalar(op, Generic)) {
                 new = PyArray_FromScalar(op, NULL);
                 Py_DECREF(op);

Modified: trunk/numpy/core/src/scalartypes.inc.src
===================================================================
--- trunk/numpy/core/src/scalartypes.inc.src	2006-07-20 22:48:49 UTC (rev 2858)
+++ trunk/numpy/core/src/scalartypes.inc.src	2006-07-20 22:58:35 UTC (rev 2859)
@@ -742,6 +742,12 @@
 }
 
 static PyObject *
+gentype_priority_get(PyObject *self)
+{
+	return PyFloat_FromDouble(NPY_SCALAR_PRIORITY);
+}
+
+static PyObject *
 gentype_typestr_get(PyObject *self)
 {
 	PyArrayObject *arr;
@@ -1000,8 +1006,10 @@
          (getter)gentype_struct_get,
          NULL,
          "Array protocol: struct"},
-	/* Does not have __array_priority__ because it is not a subtype.
-	 */
+	{"__array_priority__",
+	 (getter)gentype_priority_get,
+	 NULL,
+	 "Array priority."},
        	{NULL, NULL, NULL, NULL}  /* Sentinel */
 };
 

Modified: trunk/numpy/lib/tests/test_twodim_base.py
===================================================================
--- trunk/numpy/lib/tests/test_twodim_base.py	2006-07-20 22:48:49 UTC (rev 2858)
+++ trunk/numpy/lib/tests/test_twodim_base.py	2006-07-20 22:58:35 UTC (rev 2859)
@@ -138,7 +138,7 @@
         y = array([ 0.09233859,  0.18626021,  0.34556073,  0.39676747,  0.53881673])
         xedges = np.linspace(0,1,10)
         yedges = np.linspace(0,1,10)
-        H = np.histogram2d(x,y, (xedges, yedges))[0]
+        H = np.histogram2d(x, y, (xedges, yedges))[0]
         answer = np.array([[0, 0, 0, 1, 0, 0, 0, 0, 0],
                            [0, 0, 0, 0, 0, 0, 1, 0, 0],
                            [0, 0, 0, 0, 0, 0, 0, 0, 0],

Modified: trunk/numpy/lib/twodim_base.py
===================================================================
--- trunk/numpy/lib/twodim_base.py	2006-07-20 22:48:49 UTC (rev 2858)
+++ trunk/numpy/lib/twodim_base.py	2006-07-20 22:58:35 UTC (rev 2859)
@@ -124,10 +124,6 @@
         X[:,i] = x**(N-i-1)
     return X
 
-
-# David Huard, June 2006
-# Adapted for python from the matlab function hist2d written by Laszlo Balkay, 2006.
-# Numpy compatible license. 
 def  histogram2d(x,y, bins, normed = False):
     """Compute the 2D histogram for a dataset (x,y) given the edges or
          the number of bins. 
@@ -151,21 +147,23 @@
             xedges = np.linspace(x.min(), x.max(), xnbin+1)
             
         else:
-            xedges = bins[0]
+            xedges = asarray(bins[0], float)
             xnbin = len(xedges)-1
         
         if np.isscalar(bins[1]):
             ynbin = bins[1]
             yedges = np.linspace(y.min(), y.max(), ynbin+1)
         else:
-            yedges = bins[1]
+            yedges = asarray(bins[1], float)
             ynbin = len(yedges)-1
     elif N == 1:
         ynbin = xnbin = bins[0]
         xedges = np.linspace(x.min(), x.max(), xnbin+1)
         yedges = np.linspace(y.max(), y.min(), ynbin+1)
+        xedges[-1] *= 1.0001
+        yedges[-1] *= 1.0001         
     else:
-        yedges = asarray(bins)
+        yedges = asarray(bins, float)
         xedges = yedges.copy()
         ynbin = len(yedges)-1
         xnbin = len(xedges)-1
@@ -186,18 +184,16 @@
     # Compute the sample indices in the flattened histogram matrix.
     if xnbin >= ynbin:
         xy = ybin*(xnbin) + xbin
-        shift = xnbin
+        shift = xnbin + 1
     else:
         xy = xbin*(ynbin) + ybin
-        shift = ynbin
+        shift = ynbin + 1
        
     # Compute the number of repetitions in xy and assign it to the flattened
     #  histogram matrix.
-    edges = np.unique(xy) 
-    edges.sort()
-    flatcount = np.histogram(xy, edges)[0]
-    indices = edges - shift - 1
-    hist[indices] = flatcount
+    flatcount = np.bincount(xy)
+    indices = np.arange(len(flatcount)-shift)
+    hist[indices] = flatcount[shift:]
 
     # Shape into a proper matrix
     histmat = hist.reshape(xnbin, ynbin)




More information about the Numpy-svn mailing list