[Scipy-svn] r3081 - in trunk/Lib/sandbox/pyem: . tests

scipy-svn@scip... scipy-svn@scip...
Sat Jun 9 01:42:24 CDT 2007


Author: cdavid
Date: 2007-06-09 01:42:03 -0500 (Sat, 09 Jun 2007)
New Revision: 3081

Added:
   trunk/Lib/sandbox/pyem/tests/test_gauss_mix.py
Modified:
   trunk/Lib/sandbox/pyem/densities.py
   trunk/Lib/sandbox/pyem/gauss_mix.py
   trunk/Lib/sandbox/pyem/tests/test_gmm_em.py
Log:
Fail nicely when call wrong plot function (plot1d for multinomial, plot for 1d models).

Modified: trunk/Lib/sandbox/pyem/densities.py
===================================================================
--- trunk/Lib/sandbox/pyem/densities.py	2007-06-09 06:16:08 UTC (rev 3080)
+++ trunk/Lib/sandbox/pyem/densities.py	2007-06-09 06:42:03 UTC (rev 3081)
@@ -1,7 +1,7 @@
 #! /usr/bin/python
 #
 # Copyrighted David Cournapeau
-# Last Change: Sat Jun 09 02:00 PM 2007 J
+# Last Change: Sat Jun 09 03:00 PM 2007 J
 
 import numpy as N
 import numpy.linalg as lin
@@ -183,7 +183,6 @@
     d       = mu.shape[0]
     c       = N.array(dim)
 
-    print c, d
     if N.any(c < 0) or N.any(c >= d):
         raise ValueError("dim elements should be >= 0 and < %d (dimension"\
                 " of the variance)" % d)

Modified: trunk/Lib/sandbox/pyem/gauss_mix.py
===================================================================
--- trunk/Lib/sandbox/pyem/gauss_mix.py	2007-06-09 06:16:08 UTC (rev 3080)
+++ trunk/Lib/sandbox/pyem/gauss_mix.py	2007-06-09 06:42:03 UTC (rev 3081)
@@ -74,6 +74,10 @@
             self.va  = N.zeros((k * d, d))
 
         self.is_valid   = False
+        if d > 1:
+            self.is1d = False
+        else:
+            self.is1d = True
 
     def set_param(self, weights, mu, sigma):
         """Set parameters of the model. Args should
@@ -171,6 +175,10 @@
             Will plot samples X draw from the mixture model, and
             plot the ellipses of equi-probability from the mean with
             fixed level of confidence 0.39.  """
+        if self.is1d:
+            raise ValueError("This function does not make sense for 1d "
+                "mixtures.")
+
         if not self.is_valid:
             raise GmParamError("""Parameters of the model has not been 
                 set yet, please set them using self.set_param()""")
@@ -262,11 +270,14 @@
         the style is red color, and nolegend for all of them.
         
         Does not work for 1d"""
+        if self.is1d:
+            raise ValueError("This function does not make sense for 1d "
+                "mixtures.")
+
         if not self.is_valid:
             raise GmParamError("""Parameters of the model has not been 
                 set yet, please set them using self.set_param()""")
 
-        assert self.d > 1
         k       = self.k
         Xe, Ye  = self.conf_ellipses(dim, npoints, level)
         try:
@@ -288,6 +299,10 @@
             - h['gpdf'] is the line for the global pdf
             - h['conf'] is a list of filling area
         """
+        if not self.is1d:
+            raise ValueError("This function does not make sense for "
+                "mixtures which are not unidimensional")
+
         # This is not optimized at all, may be slow. Should not be
         # difficult to make much faster, but it is late, and I am lazy
         # XXX separete the computation from the plotting
@@ -361,6 +376,9 @@
         """Do all the necessary computation for contour plot of mixture's density.
         
         Returns X, Y, Z and V as expected by mpl contour function."""
+        if self.is1d:
+            raise ValueError("This function does not make sense for 1d "
+                "mixtures.")
 
         # Ok, it is a bit gory. Basically, we want to compute the size of the
         # grid. We use conf_ellipse, which will return a couple of points for
@@ -414,6 +432,7 @@
             return self.va[tidx, dim]
         else:
             raise ValueError("Unkown mode")
+
     # Syntactic sugar
     def __repr__(self):
         repr    = ""

Added: trunk/Lib/sandbox/pyem/tests/test_gauss_mix.py
===================================================================
--- trunk/Lib/sandbox/pyem/tests/test_gauss_mix.py	2007-06-09 06:16:08 UTC (rev 3080)
+++ trunk/Lib/sandbox/pyem/tests/test_gauss_mix.py	2007-06-09 06:42:03 UTC (rev 3081)
@@ -0,0 +1,46 @@
+#! /usr/bin/env python
+# Last Change: Sat Jun 09 03:00 PM 2007 J
+
+# For now, just test that all mode/dim execute correctly
+
+import sys
+from numpy.testing import *
+
+import numpy as N
+
+set_package_path()
+from pyem import GM
+restore_path()
+
+class test_BasicFunc(NumpyTestCase):
+    """Check that basic functionalities work."""
+    def test_conf_ellip(self):
+        """Only test whether the call succeed. To check wether the result is
+        OK, you have to plot the results."""
+        d = 3
+        k = 3
+        w, mu, va = GM.gen_param(d, k)
+        gm = GM.fromvalues(w, mu, va)
+        gm.conf_ellipses()
+
+    def test_1d_bogus(self):
+        """Check that functions which do not make sense for 1d fail nicely."""
+        d = 1
+        k = 2
+        w, mu, va = GM.gen_param(d, k)
+        gm = GM.fromvalues(w, mu, va)
+        try:
+            gm.conf_ellipses()
+            raise AssertionError("This should not work !")
+        except ValueError, e:
+            print "Ok, conf_ellipses failed as expected (with msg: " + str(e) + ")"
+
+        try:
+            gm.density_on_grid()
+            raise AssertionError("This should not work !")
+        except ValueError, e:
+            print "Ok, density_grid failed as expected (with msg: " + str(e) + ")"
+
+
+if __name__ == "__main__":
+    NumpyTest().run()

Modified: trunk/Lib/sandbox/pyem/tests/test_gmm_em.py
===================================================================
--- trunk/Lib/sandbox/pyem/tests/test_gmm_em.py	2007-06-09 06:16:08 UTC (rev 3080)
+++ trunk/Lib/sandbox/pyem/tests/test_gmm_em.py	2007-06-09 06:42:03 UTC (rev 3081)
@@ -1,5 +1,5 @@
 #! /usr/bin/env python
-# Last Change: Tue Oct 24 06:00 PM 2006 J
+# Last Change: Sat Jun 09 03:00 PM 2007 J
 
 # For now, just test that all mode/dim execute correctly
 



More information about the Scipy-svn mailing list