changeset 916:7345f0d51faa

added display of paths
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 04 Jul 2017 17:36:24 -0400
parents 13434f5017dd
children 89cc05867c4c
files python/ml.py scripts/learn-poi.py
diffstat 2 files changed, 33 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/python/ml.py	Tue Jul 04 17:03:29 2017 -0400
+++ b/python/ml.py	Tue Jul 04 17:36:24 2017 -0400
@@ -266,14 +266,24 @@
             print('Mean overall similarity: {}'.format((similarities[cluster][:,cluster].sum()+n)/(n*(n-1))))
 
 # Gaussian Mixture Models
+def plotGMM(mean, covariance, num, fig, color, alpha = 0.3):
+    v, w = np.linalg.eigh(covariance)
+    angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi
+    v *= 4
+    ell = mpl.patches.Ellipse(mean, v[0], v[1], 180+angle, color=color)
+    ell.set_clip_box(fig.bbox)
+    ell.set_alpha(alpha)
+    fig.axes[0].add_artist(ell)
+    plt.plot([mean[0]], [mean[1]], 'x'+color)
+    plt.annotate(str(num), xy=(mean[0]+1, mean[1]+1))
+
 def plotGMMClusters(model, labels = None, dataset = None, fig = None, colors = utils.colors, nUnitsPerPixel = 1., alpha = 0.3):
     '''plot the ellipse corresponding to the Gaussians
     and the predicted classes of the instances in the dataset'''
     if fig is None:
         fig = plt.figure()
-    axes = fig.get_axes()
-    if len(axes) == 0:
-        axes = [fig.add_subplot(111)]
+    if len(fig.get_axes()) == 0:
+        fig.add_subplot(111)
     for i in xrange(model.n_components):
         mean = model.means_[i]/nUnitsPerPixel
         covariance = model.covariances_[i]/nUnitsPerPixel
@@ -281,16 +291,8 @@
         if dataset is not None:
             tmpDataset = dataset/nUnitsPerPixel
             plt.scatter(tmpDataset[labels == i, 0], tmpDataset[labels == i, 1], .8, color=colors[i])
-        # plot an ellipse to show the Gaussian component                                                  
-        v, w = np.linalg.eigh(covariance)
-        angle = 180*np.arctan2(w[0][1], w[0][0])/np.pi
-        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(alpha)
-        axes[0].add_artist(ell)
-        plt.plot([mean[0]], [mean[1]], 'x'+colors[i])
-        plt.annotate(str(i), xy=(mean[0]+1, mean[1]+1))
+        # plot an ellipse to show the Gaussian component
+        plotGMM(mean, covariance, i, fig, colors[i], alpha)
     if dataset is None: # to address issues without points, the axes limits are not redrawn
         minima = model.means_.min(0)
         maxima = model.means_.max(0)
--- a/scripts/learn-poi.py	Tue Jul 04 17:03:29 2017 -0400
+++ b/scripts/learn-poi.py	Tue Jul 04 17:36:24 2017 -0400
@@ -17,8 +17,9 @@
 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 = 'unitsPerPixel', help = 'number of units of distance per pixel', type = float, default = 1.)
-parser.add_argument('--display', dest = 'display', help = 'display points of interests', action = 'store_true') # default is manhattan distance
-parser.add_argument('--assign', dest = 'assign', help = 'display points of interests', action = 'store_true')
+parser.add_argument('--display', dest = 'display', help = 'displays points of interests', action = 'store_true') # default is manhattan distance
+parser.add_argument('--assign', dest = 'assign', help = 'assigns the trajectories to the POIs and saves the assignments', action = 'store_true')
+parser.add_argument('--display-paths', dest = 'displayPaths', help = 'displays all possible origin destination if assignment is done', action = 'store_true')
 
 # TODO test Variational Bayesian Gaussian Mixture BayesianGaussianMixture
 
@@ -76,6 +77,21 @@
 
 if args.assign:
     storage.savePOIAssignments(args.databaseFilename, objects)
+    if args.displayPaths:
+        for i in xrange(args.nOriginClusters):
+            for j in xrange(args.nDestinationClusters):
+                odObjects = [o for o in objects if o.od[0] == i and o.od[1] == j]
+                if len(odObjects) > 0:
+                    fig = plt.figure()
+                    ax = fig.add_subplot(111)
+                    ml.plotGMM(models['beginning'].means_[i], models['beginning'].covariances_[i], i, fig, 'b')
+                    ml.plotGMM(models['end'].means_[j], models['end'].covariances_[j], j, fig, 'r')
+                    for o in odObjects:
+                        o.plot(withOrigin = True)
+                    plt.title('OD {} to {}'.format(i,j))
+                    plt.axis('equal')
+                    plt.show()
+
 
 if args.display:
     plt.axis('equal')