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

scipy-svn at scipy.org scipy-svn at scipy.org
Thu Nov 13 16:03:47 EST 2008


Author: damian.eads
Date: 2008-11-13 15:03:45 -0600 (Thu, 13 Nov 2008)
New Revision: 5092

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

Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py	2008-11-13 19:40:00 UTC (rev 5091)
+++ trunk/scipy/cluster/hierarchy.py	2008-11-13 21:03:45 UTC (rev 5092)
@@ -1086,7 +1086,7 @@
     is_valid_linkage(Z, throw=True, name='Z')
 
     # We expect the i'th value to be greater than its successor.
-    return (Z[:-1,2]>=Z[1:,2]).all()
+    return (Z[1:,2]>=Z[:-1,2]).all()
 
 def is_valid_im(R, warning=False, throw=False, name=None):
     """

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-13 19:40:00 UTC (rev 5091)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-13 21:03:45 UTC (rev 5092)
@@ -38,7 +38,7 @@
 import numpy as np
 from numpy.testing import *
 
-from scipy.cluster.hierarchy import linkage, from_mlab_linkage, to_mlab_linkage, num_obs_linkage, inconsistent, cophenet, from_mlab_linkage, fclusterdata, fcluster, is_isomorphic, single, complete, average, weighted, centroid, median, ward, leaders, correspond
+from scipy.cluster.hierarchy import linkage, from_mlab_linkage, to_mlab_linkage, num_obs_linkage, inconsistent, cophenet, from_mlab_linkage, fclusterdata, fcluster, is_isomorphic, single, complete, average, weighted, centroid, median, ward, leaders, correspond, is_monotonic
 from scipy.spatial.distance import squareform, pdist
 
 _tdist = np.array([[0,    662,  877,  255,  412,  996],
@@ -615,6 +615,77 @@
             #print A.shape, Y.shape, Yr.shape
             self.failUnless(num_obs_linkage(Z) == n)
 
+class TestIsMonotonic(TestCase):
+
+    def test_is_monotonic_empty(self):
+        "Tests is_monotonic(Z) on an empty linkage."
+        Z = np.zeros((0, 4))
+        self.failUnlessRaises(ValueError, is_monotonic, Z)
+
+    def test_is_monotonic_1x4(self):
+        "Tests is_monotonic(Z) on 1x4 linkage. Expecting True."
+        Z = np.asarray([[0, 1, 0.3, 2]], dtype=np.double);
+        self.failUnless(is_monotonic(Z) == True)
+
+    def test_is_monotonic_2x4_T(self):
+        "Tests is_monotonic(Z) on 2x4 linkage. Expecting True."
+        Z = np.asarray([[0, 1, 0.3, 2],
+                        [2, 3, 0.4, 3]], dtype=np.double)
+        self.failUnless(is_monotonic(Z) == True)
+
+    def test_is_monotonic_2x4_F(self):
+        "Tests is_monotonic(Z) on 2x4 linkage. Expecting False."
+        Z = np.asarray([[0, 1, 0.4, 2],
+                        [2, 3, 0.3, 3]], dtype=np.double)
+        self.failUnless(is_monotonic(Z) == False)
+
+    def test_is_monotonic_3x4_T(self):
+        "Tests is_monotonic(Z) on 3x4 linkage. Expecting True."
+        Z = np.asarray([[0, 1, 0.3, 2],
+                        [2, 3, 0.4, 2],
+                        [3, 4, 0.6, 4]], dtype=np.double)
+        self.failUnless(is_monotonic(Z) == True)
+
+    def test_is_monotonic_3x4_F1(self):
+        "Tests is_monotonic(Z) on 3x4 linkage (case 1). Expecting False."
+        Z = np.asarray([[0, 1, 0.3, 2],
+                        [2, 3, 0.2, 2],
+                        [3, 4, 0.6, 4]], dtype=np.double)
+        self.failUnless(is_monotonic(Z) == False)
+
+    def test_is_monotonic_3x4_F2(self):
+        "Tests is_monotonic(Z) on 3x4 linkage (case 2). Expecting False."
+        Z = np.asarray([[0, 1, 0.8, 2],
+                        [2, 3, 0.4, 2],
+                        [3, 4, 0.6, 4]], dtype=np.double)
+        self.failUnless(is_monotonic(Z) == False)
+
+    def test_is_monotonic_3x4_F3(self):
+        "Tests is_monotonic(Z) on 3x4 linkage (case 3). Expecting False"
+        Z = np.asarray([[0, 1, 0.3, 2],
+                        [2, 3, 0.4, 2],
+                        [3, 4, 0.2, 4]], dtype=np.double)
+        self.failUnless(is_monotonic(Z) == False)
+
+    def test_is_monotonic_tdist_linkage(self):
+        "Tests is_monotonic(Z) on clustering generated by single linkage on tdist data set. Expecting True."
+        Z = linkage(_ytdist, 'single')
+        self.failUnless(is_monotonic(Z) == True)
+
+    def test_is_monotonic_tdist_linkage(self):
+        "Tests is_monotonic(Z) on clustering generated by single linkage on tdist data set. Perturbing. Expecting False."
+        Z = linkage(_ytdist, 'single')
+        Z[2,2]=0.0
+        self.failUnless(is_monotonic(Z) == False)
+
+    def test_is_monotonic_iris_linkage(self):
+        "Tests is_monotonic(Z) on clustering generated by single linkage on Iris data set. Expecting True."
+        X = eo['iris']
+        Y = pdist(X)
+        Z = linkage(X, 'single')
+        self.failUnless(is_monotonic(Z) == True)
+
+
 def help_single_inconsistent_depth(self, i):
     Y = squareform(_tdist)
     Z = linkage(Y, 'single')




More information about the Scipy-svn mailing list