[Scipy-svn] r5006 - in trunk/scipy/cluster: . src tests

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Nov 7 14:41:46 EST 2008


Author: damian.eads
Date: 2008-11-07 13:41:42 -0600 (Fri, 07 Nov 2008)
New Revision: 5006

Modified:
   trunk/scipy/cluster/hierarchy.py
   trunk/scipy/cluster/src/common.h
   trunk/scipy/cluster/src/hierarchy.c
   trunk/scipy/cluster/src/hierarchy_wrap.c
   trunk/scipy/cluster/tests/test_hierarchy.py
Log:
Added tests for from_mlab_linkage function.

Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py	2008-11-07 19:02:46 UTC (rev 5005)
+++ trunk/scipy/cluster/hierarchy.py	2008-11-07 19:41:42 UTC (rev 5006)
@@ -1033,7 +1033,7 @@
        - ZS : ndarray
            A linkage matrix compatible with this library.
     """
-    Z = np.asarray(Z, order='c')
+    Z = np.asarray(Z, dtype=np.double, order='c')
     Zs = Z.shape
 
     # If it's empty, return it.
@@ -1047,16 +1047,13 @@
     if Zs[0] == 0:
         return Z.copy()
 
-    Zpart = Z[:,0:2]
-    Zd = Z[:,2].reshape(Zs[0], 1)
-    if Zpart.min() != 1.0 and Zpart.max() != 2 * Zs[0]:
+    Zpart = Z.copy()
+    if Zpart[:, 0:2].min() != 1.0 and Zpart[:, 0:2].max() != 2 * Zs[0]:
         raise ValueError('The format of the indices is not 1..N');
-    CS = np.zeros((Zs[0], 1), dtype=np.double)
-    Zpart = Zpart - 1
-    _hierarchy_wrap.calculate_cluster_sizes_wrap(np.hstack([Zpart, \
-                                                             Zd]).copy(), \
-                                               CS, int(Zs[0]) + 1)
-    return np.hstack([Zpart, Zd, CS]).copy()
+    Zpart[:, 0:2] -= 1.0
+    CS = np.zeros((Zs[0],), dtype=np.double)
+    _hierarchy_wrap.calculate_cluster_sizes_wrap(Zpart, CS, int(Zs[0]) + 1)
+    return np.hstack([Zpart, CS.reshape(Zs[0], 1)])
 
 def to_mlab_linkage(Z):
     """

Modified: trunk/scipy/cluster/src/common.h
===================================================================
--- trunk/scipy/cluster/src/common.h	2008-11-07 19:02:46 UTC (rev 5005)
+++ trunk/scipy/cluster/src/common.h	2008-11-07 19:41:42 UTC (rev 5006)
@@ -60,7 +60,6 @@
                             ((double)((x)/(y))) ? ((x)/(y)) : ((x)/(y) + 1))
 #endif
 
-
 #ifdef CPY_DEBUG
 #define CPY_DEBUG_MSG(...) fprintf(stderr, __VA_ARGS__)
 #else

Modified: trunk/scipy/cluster/src/hierarchy.c
===================================================================
--- trunk/scipy/cluster/src/hierarchy.c	2008-11-07 19:02:46 UTC (rev 5005)
+++ trunk/scipy/cluster/src/hierarchy.c	2008-11-07 19:41:42 UTC (rev 5006)
@@ -1067,7 +1067,7 @@
 }
 
 void calculate_cluster_sizes(const double *Z, double *CS, int n) {
-  int i, j, k;
+  int i, j, k, q;
   const double *row;
   for (k = 0; k < n - 1; k++) {
     row = Z + (k * 3);
@@ -1075,21 +1075,23 @@
     j = (int)row[CPY_LIN_RIGHT];
     /** If the left node is a non-singleton, add its count. */
     if (i >= n) {
-      CS[k] = CS[i - n];
+      q = i - n;
+      CS[k] += CS[q];
     }
     /** Otherwise just add 1 for the leaf. */
     else {
-      CS[k] = 1.0;
+      CS[k] += 1.0;
     }
     /** If the right node is a non-singleton, add its count. */
     if (j >= n) {
-      CS[k] = CS[k] + CS[j - n];
+      q = j - n;
+      CS[k] += CS[q];
     }
     /** Otherwise just add 1 for the leaf. */
     else {
-      CS[k] = CS[k] + 1.0;
+      CS[k] += 1.0;
     }
-    /**    CPY_DEBUG_MSG("i=%d, j=%d, CS[%d]=%d\n", i, j, n+k, (int)CS[k]);**/
+    CPY_DEBUG_MSG("i=%d, j=%d, CS[%d]=%d\n", i, j, k, (int)CS[k]);
   }
 }
 

Modified: trunk/scipy/cluster/src/hierarchy_wrap.c
===================================================================
--- trunk/scipy/cluster/src/hierarchy_wrap.c	2008-11-07 19:02:46 UTC (rev 5005)
+++ trunk/scipy/cluster/src/hierarchy_wrap.c	2008-11-07 19:41:42 UTC (rev 5006)
@@ -122,7 +122,7 @@
     return 0;
   }
   calculate_cluster_sizes((const double*)Z->data, (double*)CS_->data, n);
-  return Py_BuildValue("d", 0.0);
+  return Py_BuildValue("");
 }
 
 extern PyObject *get_max_dist_for_each_cluster_wrap(PyObject *self,

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-07 19:02:46 UTC (rev 5005)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-07 19:41:42 UTC (rev 5006)
@@ -202,6 +202,20 @@
         ZP = from_mlab_linkage(Z)
         return self.failUnless((ZP == expectedZP).all())
 
+    def test_from_mlab_linkage_multiple_rows(self):
+        "Testing from_mlab_linkage on linkage array with multiple rows."
+        Z = np.asarray([[3, 6, 138], [4, 5, 219],
+                        [1, 8, 255], [2, 9, 268], [7, 10, 295]])
+        expectedZS = np.array([[   2.,    5.,  138.,    2.],
+                               [   3.,    4.,  219.,    2.],
+                               [   0.,    7.,  255.,    3.],
+                               [   1.,    8.,  268.,    4.],
+                               [   6.,    9.,  295.,    6.]],
+                              dtype=np.double)
+        ZS = from_mlab_linkage(Z)
+        print expectedZS, ZS
+        self.failUnless((expectedZS == ZS).all())
+
 def help_single_inconsistent_depth(self, i):
     Y = squareform(_tdist)
     Z = linkage(Y, 'single')




More information about the Scipy-svn mailing list