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

scipy-svn at scipy.org scipy-svn at scipy.org
Fri May 30 16:57:30 EDT 2008


Author: damian.eads
Date: 2008-05-30 15:57:27 -0500 (Fri, 30 May 2008)
New Revision: 4398

Modified:
   trunk/scipy/cluster/hierarchy.py
   trunk/scipy/cluster/tests/test_hierarchy.py
Log:
Fixed a boundary condition bug in hierarchy.squareform with 0-length pdist arrays.

Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py	2008-05-30 20:30:15 UTC (rev 4397)
+++ trunk/scipy/cluster/hierarchy.py	2008-05-30 20:57:27 UTC (rev 4398)
@@ -730,6 +730,9 @@
 
     # X = squareform(v)
     if len(s) == 1 and force != 'tomatrix':
+        if X.shape[0] == 0:
+            return np.zeros((1,1), dtype=np.double)
+
         # Grab the closest value to the square root of the number
         # of elements times 2 to see if the number of elements
         # is indeed a binomial coefficient.
@@ -766,6 +769,9 @@
         # One-side of the dimensions is set here.
         d = s[0]
 
+        if d <= 1:
+            return np.array([], dtype=np.double)
+
         # Create a vector.
         v = np.zeros(((d * (d - 1) / 2),), dtype=np.double)
 
@@ -1662,9 +1668,9 @@
                 raise TypeError('Variable passed as inconsistency matrix is not a numpy array.')
         if R.dtype != np.double:
             if name:
-                raise TypeError('Inconsistency matrix \'%s\' must contain doubles (float64).' % name)
+                raise TypeError('Inconsistency matrix \'%s\' must contain doubles (double).' % name)
             else:
-                raise TypeError('Inconsistency matrix must contain doubles (float64).')
+                raise TypeError('Inconsistency matrix must contain doubles (double).')
         if len(R.shape) != 2:
             if name:
                 raise ValueError('Inconsistency matrix \'%s\' must have shape=2 (i.e. be two-dimensional).' % name)
@@ -1721,9 +1727,9 @@
                 raise TypeError('Variable is not a valid array.')
         if Z.dtype != np.double:
             if name:
-                raise TypeError('Linkage matrix \'%s\' must contain doubles (float64).' % name)
+                raise TypeError('Linkage matrix \'%s\' must contain doubles (double).' % name)
             else:
-                raise TypeError('Linkage matrix must contain doubles (float64).')
+                raise TypeError('Linkage matrix must contain doubles (double).')
         if len(Z.shape) != 2:
             if name:
                 raise ValueError('Linkage matrix \'%s\' must have shape=2 (i.e. be two-dimensional).' % name)
@@ -1782,9 +1788,9 @@
                 raise TypeError('Variable is not a numpy array.')
         if y.dtype != np.double:
             if name:
-                raise TypeError('Condensed distance matrix \'%s\' must contain doubles (float64).' % name)
+                raise TypeError('Condensed distance matrix \'%s\' must contain doubles (double).' % name)
             else:
-                raise TypeError('Condensed distance matrix must contain doubles (float64).')
+                raise TypeError('Condensed distance matrix must contain doubles (double).')
         if len(y.shape) != 1:
             if name:
                 raise ValueError('Condensed distance matrix \'%s\' must have shape=1 (i.e. be one-dimensional).' % name)
@@ -1845,9 +1851,9 @@
         s = D.shape
         if D.dtype != np.double:
             if name:
-                raise TypeError('Distance matrix \'%s\' must contain doubles (float64).' % name)
+                raise TypeError('Distance matrix \'%s\' must contain doubles (double).' % name)
             else:
-                raise TypeError('Distance matrix must contain doubles (float64).')
+                raise TypeError('Distance matrix must contain doubles (double).')
         if len(D.shape) != 2:
             if name:
                 raise ValueError('Distance matrix \'%s\' must have shape=2 (i.e. be two-dimensional).' % name)

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-05-30 20:30:15 UTC (rev 4397)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-05-30 20:57:27 UTC (rev 4398)
@@ -574,6 +574,8 @@
 
     ################### squareform
 
+class TestSquareForm(TestCase):
+
     def test_squareform_empty_matrix(self):
         "Tests squareform on an empty matrix."
         A = numpy.zeros((0,0))
@@ -583,7 +585,8 @@
     def test_squareform_empty_vector(self):
         v = numpy.zeros((0,))
         rv = squareform(numpy.array(v, dtype='double'))
-        self.failUnless(rv.shape == (0,0))
+        self.failUnless(rv.shape == (1,1))
+        self.failUnless(rv[0, 0] == 0)
 
     def test_squareform_1by1_matrix(self):
         "Tests squareform on a 1x1 matrix."
@@ -618,29 +621,29 @@
             Yr = squareform(A)
             s = A.shape
             k = 0
+            print A.shape, Y.shape, Yr.shape
             self.failUnless(len(s) == 2)
             self.failUnless(len(Yr.shape) == 1)
             self.failUnless(s[0] == s[1])
-            #print A.shape, Y.shape, Yr.shape
             for i in xrange(0, s[0]):
                 for j in xrange(i+1, s[1]):
                     if i != j:
                         #print i, j, k, A[i, j], Y[k]
                         self.failUnless(A[i, j] == Y[k])
-                        self.failUnless(Yr[k] == Y[k])
                         k += 1
                     else:
                         self.failUnless(A[i, j] == 0)
 
+class TestNumObs(TestCase):
+
     ############## numobs_dm
-
     def test_numobs_dm_multi_matrix(self):
         "Tests numobs_dm with observation matrices of multiple sizes."
-        for n in xrange(2, 10):
+        for n in xrange(1, 10):
             X = numpy.random.rand(n, 4)
             Y = pdist(X)
             A = squareform(Y)
-            #print A.shape, Y.shape, Yr.shape
+            print A.shape, Y.shape
             self.failUnless(numobs_dm(A) == n)
 
     def test_numobs_y_multi_matrix(self):
@@ -661,8 +664,9 @@
             #print A.shape, Y.shape, Yr.shape
             self.failUnless(numobs_linkage(Z) == n)
 
+class TestLinkage(TestCase):
+
     ################### linkage
-
     def test_linkage_single_tdist(self):
         "Tests linkage(Y, 'single') on the tdist data set."
         Z = linkage(_ytdist, 'single')




More information about the Scipy-svn mailing list