changeset 787:0a428b449b80 dev

improved script to display over world image
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Thu, 17 Mar 2016 16:01:19 -0400
parents 1f2b2d1f4fbf
children 5b970a5bc233
files python/ml.py scripts/learn-poi.py
diffstat 2 files changed, 21 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/python/ml.py	Fri Mar 11 17:38:48 2016 -0500
+++ b/python/ml.py	Thu Mar 17 16:01:19 2016 -0400
@@ -195,23 +195,26 @@
     return clusterSizes
 
 # Gaussian Mixture Models
-def plotGMMClusters(model, dataset = None, colors = utils.colors):
+def plotGMMClusters(model, dataset = None, fig = None, colors = utils.colors, nPixelsPerUnit = 1., alpha = 0.3):
     '''plot the ellipse corresponding to the Gaussians
     and the predicted classes of the instances in the dataset'''
-    fig = plt.figure()
+    if fig is None:
+        fig = plt.figure()
     labels = model.predict(dataset)
+    tmpDataset = nPixelsPerUnit*dataset
     for i in xrange(model.n_components):
-        mean = model.means_[i]
+        mean = nPixelsPerUnit*model.means_[i]
+        covariance = nPixelsPerUnit*model.covars_[i]
         if dataset is not None:
-            plt.scatter(dataset[labels == i, 0], dataset[labels == i, 1], .8, color=colors[i])
+            plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i])
         plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1))
 
         # Plot an ellipse to show the Gaussian component                                                  
-        v, w = np.linalg.eigh(model.covars_[i])
+        v, w = np.linalg.eigh(covariance)
         angle = np.arctan2(w[0][1], w[0][0])
         angle = 180*angle/np.pi  # convert to degrees                                             
 	v *= 4
         ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=colors[i])
         ell.set_clip_box(fig.bbox)
-        ell.set_alpha(.5)
+        ell.set_alpha(alpha)
         fig.axes[0].add_artist(ell)
--- a/scripts/learn-poi.py	Fri Mar 11 17:38:48 2016 -0500
+++ b/scripts/learn-poi.py	Thu Mar 17 16:01:19 2016 -0400
@@ -13,6 +13,8 @@
 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object')
 parser.add_argument('-n', dest = 'nClusters', help = 'number of point clusters', required = True, type = int)
 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full")
+parser.add_argument('-w', dest = 'worldImageFilename', help = 'filename of the world image')
+parser.add_argument('-u', dest = 'pixelsPerUnit', help = 'number pixels per unit of distance', type = float, default = 1.)
 
 args = parser.parse_args()
 
@@ -32,12 +34,20 @@
 gmm = mixture.GMM(n_components=args.nClusters, covariance_type = args.covarianceType)
 endModel=gmm.fit(ends)
 
-ml.plotGMMClusters(beginningModel, beginnings)
+fig = plt.figure()
+if args.worldImageFilename is not None and args.pixelsPerUnit is not None:
+    img = plt.imread(args.worldImageFilename)
+    plt.imshow(img)
+ml.plotGMMClusters(beginningModel, beginnings, fig, nPixelsPerUnit = args.pixelsPerUnit)
 plt.axis('equal')
 plt.title('Origins')
 print('Origin Clusters:\n{}'.format(ml.computeClusterSizes(beginningModel.predict(beginnings), range(args.nClusters))))
 
-ml.plotGMMClusters(endModel, ends)
+fig = plt.figure()
+if args.worldImageFilename is not None and args.pixelsPerUnit is not None:
+    img = plt.imread(args.worldImageFilename)
+    plt.imshow(img)
+ml.plotGMMClusters(endModel, ends, fig, nPixelsPerUnit = args.pixelsPerUnit)
 plt.axis('equal')
 plt.title('Destinations')
 print('Destination Clusters:\n{}'.format(ml.computeClusterSizes(endModel.predict(ends), range(args.nClusters))))