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

scipy-svn@scip... scipy-svn@scip...
Fri Nov 7 13:02:48 CST 2008


Author: damian.eads
Date: 2008-11-07 13:02:46 -0600 (Fri, 07 Nov 2008)
New Revision: 5005

Modified:
   trunk/scipy/cluster/hierarchy.py
   trunk/scipy/cluster/tests/test_hierarchy.py
Log:
Added more tests.

Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py	2008-11-07 18:46:34 UTC (rev 5004)
+++ trunk/scipy/cluster/hierarchy.py	2008-11-07 19:02:46 UTC (rev 5005)
@@ -1035,6 +1035,18 @@
     """
     Z = np.asarray(Z, order='c')
     Zs = Z.shape
+
+    # If it's empty, return it.
+    if len(Zs) == 0 or (len(Zs) == 1 and Zs[0] == 0):
+        return Z.copy()
+
+    if len(Zs) != 2:
+        raise ValueError("The linkage array must be rectangular.")
+
+    # If it contains no rows, return it.
+    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]:

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-07 18:46:34 UTC (rev 5004)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-07 19:02:46 UTC (rev 5005)
@@ -38,7 +38,7 @@
 import numpy as np
 from numpy.testing import *
 
-from scipy.cluster.hierarchy import linkage, from_mlab_linkage, numobs_linkage, inconsistent, cophenet
+from scipy.cluster.hierarchy import linkage, from_mlab_linkage, numobs_linkage, inconsistent, cophenet, from_mlab_linkage
 from scipy.spatial.distance import squareform, pdist, numobs_dm, numobs_y
 
 _tdist = np.array([[0,    662,  877,  255,  412,  996],
@@ -187,6 +187,21 @@
         self.failUnless(np.abs(c - expectedc) <= eps)
         self.failUnless(within_tol(M, expectedM, eps))
 
+class TestFromMLabLinkage(TestCase):
+
+    def test_from_mlab_linkage_empty(self):
+        "Testing from_mlab_linkage on empty linkage array."
+        X = np.asarray([])
+        R = from_mlab_linkage([])
+        self.failUnless((R == X).all())
+
+    def test_from_mlab_linkage_single_row(self):
+        "Testing from_mlab_linkage on linkage array with single row."
+        expectedZP = np.asarray([[ 0.,  1.,  3.,  2.]])
+        Z = [[1,2,3]]
+        ZP = from_mlab_linkage(Z)
+        return self.failUnless((ZP == expectedZP).all())
+
 def help_single_inconsistent_depth(self, i):
     Y = squareform(_tdist)
     Z = linkage(Y, 'single')



More information about the Scipy-svn mailing list