comparison scripts/learn-poi.py @ 914:f228fd649644

corrected bugs in learn-pois.py
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 28 Jun 2017 23:43:52 -0400
parents 1cd878812529
children 13434f5017dd
comparison
equal deleted inserted replaced
913:1cd878812529 914:f228fd649644
6 from sklearn import mixture 6 from sklearn import mixture
7 import matplotlib.pyplot as plt 7 import matplotlib.pyplot as plt
8 8
9 import storage, ml 9 import storage, ml
10 10
11 parser = argparse.ArgumentParser(description='The program learns and displays Gaussians fit to beginnings and ends of object trajectories (based on Mohamed Gomaa Mohamed 2015 PhD). TODO: save the data') 11 parser = argparse.ArgumentParser(description='The program learns and displays Gaussians fit to beginnings and ends of object trajectories (based on Mohamed Gomaa Mohamed 2015 PhD).')
12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True) 12 parser.add_argument('-d', dest = 'databaseFilename', help = 'name of the Sqlite database file', required = True)
13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object') 13 parser.add_argument('-t', dest = 'trajectoryType', help = 'type of trajectories to display', choices = ['feature', 'object'], default = 'object')
14 parser.add_argument('-n', dest = 'nObjects', help = 'number of objects to display', type = int)
14 parser.add_argument('-norigins', dest = 'nOriginClusters', help = 'number of clusters for trajectory origins', required = True, type = int) 15 parser.add_argument('-norigins', dest = 'nOriginClusters', help = 'number of clusters for trajectory origins', required = True, type = int)
15 parser.add_argument('-ndestinations', dest = 'nDestinationClusters', help = 'number of clusters for trajectory destinations (=norigins if not provided)', type = int) 16 parser.add_argument('-ndestinations', dest = 'nDestinationClusters', help = 'number of clusters for trajectory destinations (=norigins if not provided)', type = int)
16 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full") 17 parser.add_argument('--covariance-type', dest = 'covarianceType', help = 'type of covariance of Gaussian model', default = "full")
17 parser.add_argument('-w', dest = 'worldImageFilename', help = 'filename of the world image') 18 parser.add_argument('-w', dest = 'worldImageFilename', help = 'filename of the world image')
18 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units of distance per pixel', type = float, default = 1.) 19 parser.add_argument('-u', dest = 'unitsPerPixel', help = 'number of units of distance per pixel', type = float, default = 1.)
19 parser.add_argument('--display', dest = 'display', help = 'display points of interests', action = 'store_true') # default is manhattan distance 20 parser.add_argument('--display', dest = 'display', help = 'display points of interests', action = 'store_true') # default is manhattan distance
20 parser.add_argument('--assign', dest = 'display', help = 'display points of interests', action = 'store_true') # default is manhattan distance 21 parser.add_argument('--assign', dest = 'assign', help = 'display points of interests', action = 'store_true')
21 22
22 args = parser.parse_args() 23 args = parser.parse_args()
23 24
24 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType) 25 objects = storage.loadTrajectoriesFromSqlite(args.databaseFilename, args.trajectoryType, args.nObjects)
25 26
26 beginnings = [] 27 beginnings = []
27 ends = [] 28 ends = []
28 for o in objects: 29 for o in objects:
29 beginnings.append(o.getPositionAt(0).aslist()) 30 beginnings.append(o.getPositionAt(0).aslist())
43 # estimation 44 # estimation
44 gmm = mixture.GaussianMixture(n_components=nClusters, covariance_type = args.covarianceType) 45 gmm = mixture.GaussianMixture(n_components=nClusters, covariance_type = args.covarianceType)
45 model=gmm.fit(points) 46 model=gmm.fit(points)
46 if not model.converged_: 47 if not model.converged_:
47 print('Warning: model for '+gmmType+' points did not converge') 48 print('Warning: model for '+gmmType+' points did not converge')
49 if args.display or args.assign:
50 labels = model.predict(points)
48 # plot 51 # plot
49 if args.display: 52 if args.display:
50 fig = plt.figure() 53 fig = plt.figure()
51 if args.worldImageFilename is not None and args.unitsPerPixel is not None: 54 if args.worldImageFilename is not None and args.unitsPerPixel is not None:
52 img = plt.imread(args.worldImageFilename) 55 img = plt.imread(args.worldImageFilename)
53 plt.imshow(img) 56 plt.imshow(img)
54 labels = model.predict(points) 57 ml.plotGMMClusters(model, labels, points, fig, nUnitsPerPixel = args.unitsPerPixel)
55 labels = ml.plotGMMClusters(model, labels, points, fig, nUnitsPerPixel = args.unitsPerPixel)
56 plt.axis('image') 58 plt.axis('image')
57 plt.title(gmmType) 59 plt.title(gmmType)
58 print(gmmType+' Clusters:\n{}'.format(ml.computeClusterSizes(labels, range(model.n_components)))) 60 print(gmmType+' Clusters:\n{}'.format(ml.computeClusterSizes(labels, range(model.n_components))))
59 # save 61 # save
60 storage.savePOIs(args.databaseFilename, model, gmmType, gmmId) 62 storage.savePOIs(args.databaseFilename, model, gmmType, gmmId)
61 # save assignments 63 # save assignments
62 if args.assign: 64 if args.assign:
63 pass 65 pass # savePOIAssignments(
64 gmmId += 1 66 gmmId += 1
65 67
66 if args.display: 68 if args.display:
67 plt.axis('equal') 69 plt.axis('equal')
68 plt.show() 70 plt.show()