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

scipy-svn at scipy.org scipy-svn at scipy.org
Tue Oct 24 04:37:33 CDT 2006


Author: cdavid
Date: 2006-10-24 04:37:27 -0500 (Tue, 24 Oct 2006)
New Revision: 2296

Modified:
   trunk/Lib/sandbox/pyem/Changelog
   trunk/Lib/sandbox/pyem/gmm_em.py
   trunk/Lib/sandbox/pyem/tests/test_gmm_em.py
Log:
 * sandbox.pyem: fix a bug which prevents full covariance
 from being used + tests to avoid this issue 
 from being unnoticed



Modified: trunk/Lib/sandbox/pyem/Changelog
===================================================================
--- trunk/Lib/sandbox/pyem/Changelog	2006-10-23 10:52:27 UTC (rev 2295)
+++ trunk/Lib/sandbox/pyem/Changelog	2006-10-24 09:37:27 UTC (rev 2296)
@@ -1,3 +1,10 @@
+pyem (0.5.5) Tue, 24 Oct 2006 18:30:54 +0900
+
+	* Fix a bug inmultiple_gaussian_den which prevents 
+	full covariance mode to work in GMM.
+	* Add consistency tests in tests/test_gmm_em.py to avoid the 
+	above bug from being detected from now on.
+
 pyem (0.5.5) Mon, 23 Oct 2006 18:48:15 +0900
 
 	* Bump to 0.5.5

Modified: trunk/Lib/sandbox/pyem/gmm_em.py
===================================================================
--- trunk/Lib/sandbox/pyem/gmm_em.py	2006-10-23 10:52:27 UTC (rev 2295)
+++ trunk/Lib/sandbox/pyem/gmm_em.py	2006-10-24 09:37:27 UTC (rev 2296)
@@ -1,5 +1,5 @@
 # /usr/bin/python
-# Last Change: Mon Oct 23 06:00 PM 2006 J
+# Last Change: Tue Oct 24 06:00 PM 2006 J
 
 # TODO:
 #   - which methods to avoid va shrinking to 0 ? There are several options, 
@@ -318,11 +318,10 @@
         return y.T
     else:
         for i in range(K):
-            y[:, i] = densities.gauss_den(data, mu[i, :], 
+            y[i] = densities.gauss_den(data, mu[i, :], 
                         va[d*i:d*i+d, :])
+        return y.T
 
-    return y
-
 if __name__ == "__main__":
     import copy
     #=============================

Modified: trunk/Lib/sandbox/pyem/tests/test_gmm_em.py
===================================================================
--- trunk/Lib/sandbox/pyem/tests/test_gmm_em.py	2006-10-23 10:52:27 UTC (rev 2295)
+++ trunk/Lib/sandbox/pyem/tests/test_gmm_em.py	2006-10-24 09:37:27 UTC (rev 2296)
@@ -1,11 +1,92 @@
 #! /usr/bin/env python
-# Last Change: Fri Oct 20 11:00 AM 2006 J
+# Last Change: Tue Oct 24 06:00 PM 2006 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.densities import gauss_den
+from pyem import GMM, GM, EM
 restore_path()
+
+class EmTest(NumpyTestCase):
+    def _create_model_and_run_em(self, d, k, mode, nframes):
+        #+++++++++++++++++++++++++++++++++++++++++++++++++
+        # Generate a model with k components, d dimensions
+        #+++++++++++++++++++++++++++++++++++++++++++++++++
+        w, mu, va   = GM.gen_param(d, k, mode, spread = 1.5)
+        gm          = GM.fromvalues(w, mu, va)
+        # Sample nframes frames  from the model
+        data        = gm.sample(nframes)
+
+        #++++++++++++++++++++++++++++++++++++++++++
+        # Approximate the models with classical EM
+        #++++++++++++++++++++++++++++++++++++++++++
+        # Init the model
+        lgm = GM(d, k, mode)
+        gmm = GMM(lgm, 'kmean')
+
+        em  = EM()
+        lk  = em.train(data, gmm)
+
+class test_full(EmTest):
+    def check_1d(self, level = 1):
+        d       = 1
+        k       = 2
+        mode    = 'full'
+        nframes = int(1e2)
+
+        #seed(1)
+        self._create_model_and_run_em(d, k, mode, nframes)
+
+    def check_2d(self, level = 1):
+        d       = 2
+        k       = 2
+        mode    = 'full'
+        nframes = int(1e2)
+
+        #seed(1)
+        self._create_model_and_run_em(d, k, mode, nframes)
+
+    def check_5d(self, level = 1):
+        d       = 5
+        k       = 3
+        mode    = 'full'
+        nframes = int(1e2)
+
+        #seed(1)
+        self._create_model_and_run_em(d, k, mode, nframes)
+
+class test_diag(EmTest):
+    def check_1d(self, level = 1):
+        d       = 1
+        k       = 2
+        mode    = 'diag'
+        nframes = int(1e2)
+
+        #seed(1)
+        self._create_model_and_run_em(d, k, mode, nframes)
+
+    def check_2d(self, level = 1):
+        d       = 2
+        k       = 2
+        mode    = 'diag'
+        nframes = int(1e2)
+
+        #seed(1)
+        self._create_model_and_run_em(d, k, mode, nframes)
+
+    def check_5d(self, level = 1):
+        d       = 5
+        k       = 3
+        mode    = 'diag'
+        nframes = int(1e2)
+
+        #seed(1)
+        self._create_model_and_run_em(d, k, mode, nframes)
+
+if __name__ == "__main__":
+    NumpyTest().run()



More information about the Scipy-svn mailing list