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

scipy-svn@scip... scipy-svn@scip...
Tue Jun 24 02:59:52 CDT 2008


Author: cdavid
Date: 2008-06-24 02:59:43 -0500 (Tue, 24 Jun 2008)
New Revision: 4470

Modified:
   trunk/scipy/cluster/tests/test_vq.py
   trunk/scipy/cluster/vq.py
Log:
Handle bogus number of clusters better + test.

Modified: trunk/scipy/cluster/tests/test_vq.py
===================================================================
--- trunk/scipy/cluster/tests/test_vq.py	2008-06-24 07:46:46 UTC (rev 4469)
+++ trunk/scipy/cluster/tests/test_vq.py	2008-06-24 07:59:43 UTC (rev 4470)
@@ -175,5 +175,11 @@
         except ValueError:
             pass
 
+        try:
+            kmeans2(X, N.array([]))
+            raise AssertionError("kmeans2 with 0 clusters should fail.")
+        except ValueError:
+            pass
+
 if __name__ == "__main__":
     nose.run(argv=['', __file__])

Modified: trunk/scipy/cluster/vq.py
===================================================================
--- trunk/scipy/cluster/vq.py	2008-06-24 07:46:46 UTC (rev 4469)
+++ trunk/scipy/cluster/vq.py	2008-06-24 07:59:43 UTC (rev 4470)
@@ -654,7 +654,14 @@
                         data")
         clusters = k.copy()
     else:
-        nc = int(k)
+        try:
+            nc = int(k)
+        except TypeError:
+            raise ValueError("k (%s) could not be converted to an integer " % str(k))
+
+        if nc < 1:
+            raise ValueError("kmeans2 for 0 clusters ? (k was %s)" % str(k))
+
         if not nc == k:
             warnings.warn("k was not an integer, was converted.")
         try:



More information about the Scipy-svn mailing list