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

scipy-svn@scip... scipy-svn@scip...
Wed Nov 12 16:20:39 CST 2008


Author: damian.eads
Date: 2008-11-12 16:20:34 -0600 (Wed, 12 Nov 2008)
New Revision: 5064

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

Modified: trunk/scipy/cluster/hierarchy.py
===================================================================
--- trunk/scipy/cluster/hierarchy.py	2008-11-12 19:33:13 UTC (rev 5063)
+++ trunk/scipy/cluster/hierarchy.py	2008-11-12 22:20:34 UTC (rev 5064)
@@ -154,43 +154,42 @@
 
 """
 
-_copyingtxt="""
-cluster.py
 
-Author: Damian Eads
-Date:   September 22, 2007
+# hierarchy.py (derived from cluster.py, http://scipy-cluster.googlecode.com)
+#
+# Author: Damian Eads
+# Date:   September 22, 2007
+#
+# Copyright (c) 2007, 2008, Damian Eads
+#
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+#   - Redistributions of source code must retain the above
+#     copyright notice, this list of conditions and the
+#     following disclaimer.
+#   - Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer
+#     in the documentation and/or other materials provided with the
+#     distribution.
+#   - Neither the name of the author nor the names of its
+#     contributors may be used to endorse or promote products derived
+#     from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-Copyright (c) 2007, 2008, Damian Eads
-
-All rights reserved.
-
-Redistribution and use in source and binary forms, with or without
-modification, are permitted provided that the following conditions
-are met:
-  - Redistributions of source code must retain the above
-    copyright notice, this list of conditions and the
-    following disclaimer.
-  - Redistributions in binary form must reproduce the above copyright
-    notice, this list of conditions and the following disclaimer
-    in the documentation and/or other materials provided with the
-    distribution.
-  - Neither the name of the author nor the names of its
-    contributors may be used to endorse or promote products derived
-    from this software without specific prior written permission.
-
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-"""
-
 import numpy as np
 import _hierarchy_wrap, types
 import scipy.spatial.distance as distance

Modified: trunk/scipy/cluster/tests/test_hierarchy.py
===================================================================
--- trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-12 19:33:13 UTC (rev 5063)
+++ trunk/scipy/cluster/tests/test_hierarchy.py	2008-11-12 22:20:34 UTC (rev 5064)
@@ -479,6 +479,73 @@
         print L, Lright, T
         self.failUnless((L[0] == Lright[0]).all() and (L[1] == Lright[1]).all())
 
+class TestIsIsomorphic(TestCase):
+
+    def test_is_isomorphic_1(self):
+        "Tests is_isomorphic on test case #1 (one flat cluster, different labellings)"
+        a = [1, 1, 1]
+        b = [2, 2, 2]
+        self.failUnless(is_isomorphic(a, b) == True)
+        self.failUnless(is_isomorphic(b, a) == True)
+        
+    def test_is_isomorphic_2(self):
+        "Tests is_isomorphic on test case #2 (two flat clusters, different labelings)"
+        a = [1, 7, 1]
+        b = [2, 3, 2]
+        self.failUnless(is_isomorphic(a, b) == True)
+        self.failUnless(is_isomorphic(b, a) == True)
+
+    def test_is_isomorphic_3(self):
+        "Tests is_isomorphic on test case #3 (no flat clusters)"
+        a = []
+        b = []
+        self.failUnless(is_isomorphic(a, b) == True)
+
+    def test_is_isomorphic_4A(self):
+        "Tests is_isomorphic on test case #4A (3 flat clusters, different labelings, isomorphic)"
+        a = [1, 2, 3]
+        b = [1, 3, 2]
+        self.failUnless(is_isomorphic(a, b) == True)
+        self.failUnless(is_isomorphic(b, a) == True)
+
+    def test_is_isomorphic_4B(self):
+        "Tests is_isomorphic on test case #4B (3 flat clusters, different labelings, nonisomorphic)"
+        a = [1, 2, 3, 3]
+        b = [1, 3, 2, 3]
+        self.failUnless(is_isomorphic(a, b) == False)
+        self.failUnless(is_isomorphic(b, a) == False)
+
+    def test_is_isomorphic_4C(self):
+        "Tests is_isomorphic on test case #4C (3 flat clusters, different labelings, isomorphic)"
+        a = [7, 2, 3]
+        b = [6, 3, 2]
+        self.failUnless(is_isomorphic(a, b) == True)
+        self.failUnless(is_isomorphic(b, a) == True)
+
+    def test_is_isomorphic_5A(self):
+        "Tests is_isomorphic on test case #5A (1000 observations, 2 random clusters, random permutation of the labeling). Run 3 times."
+        for k in xrange(0, 3):
+            self.help_is_isomorphic_randperm(1000, 2)
+
+    def test_is_isomorphic_5B(self):
+        "Tests is_isomorphic on test case #5B (1000 observations, 3 random clusters, random permutation of the labeling). Run 3 times."
+        for k in xrange(0, 3):
+            self.help_is_isomorphic_randperm(1000, 3)
+
+    def test_is_isomorphic_5C(self):
+        "Tests is_isomorphic on test case #5C (1000 observations, 5 random clusters, random permutation of the labeling). Run 3 times."
+        for k in xrange(0, 3):
+            self.help_is_isomorphic_randperm(1000, 5)
+
+    def help_is_isomorphic_randperm(self, nobs, nclusters):
+        a = np.int_(np.random.rand(nobs) * nclusters)
+        b = np.zeros(a.size, dtype=np.int_)
+        q = {}        
+        P = np.random.permutation(nclusters)
+        for i in xrange(0, a.shape[0]):
+            b[i] = P[a[i]]
+        self.failUnless(is_isomorphic(a, b) == True)
+
 def help_single_inconsistent_depth(self, i):
     Y = squareform(_tdist)
     Z = linkage(Y, 'single')



More information about the Scipy-svn mailing list