annotate trafficintelligence/objectsmoothing.py @ 1212:af329f3330ba

work in progress on 3D safety analysis
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Tue, 02 May 2023 17:11:24 -0400
parents cc5cb04b04b0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1028
cc5cb04b04b0 major update using the trafficintelligence package name and install through pip
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 998
diff changeset
1 from trafficintelligence import storage, moving, utils
665
15e244d2a1b5 corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 661
diff changeset
2
650
d74e8c175d6b minor cleaning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 644
diff changeset
3 from math import atan2, degrees, sin, cos, pi
d74e8c175d6b minor cleaning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 644
diff changeset
4 from numpy import median
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
5
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
6 import matplotlib.pyplot as plt
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
7
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
8 def findNearest(feat, featureSet,t,reverse=True):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
9 dist={}
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
10 for f in featureSet:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
11 if reverse:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
12 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t+1),f.getPositionAtInstant(t))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
13 else:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
14 dist[f]= moving.Point.distanceNorm2(feat.getPositionAtInstant(t-1),f.getPositionAtInstant(t))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
15 return min(dist, key=dist.get) # = utils.argmaxDict(dist)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
16
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
17 def getFeatures(obj, featureID):
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
18 currentFeature = obj.getFeature(featureID)
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
19 first = currentFeature.getFirstInstant()
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
20 last = currentFeature.getLastInstant()
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
21 featureList=[[currentFeature,first,last,moving.Point(0,0)]]
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
22 # find the features to fill in the beginning of the object existence
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
23 while first != obj.getFirstInstant():
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
24 delta=featureList[-1][3]
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
25 featureSet = [f for f in obj.getFeatures() if f.existsAtInstant(first-1)]
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
26 feat = findNearest(currentFeature,featureSet,first-1,reverse=True)
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
27 if feat.existsAtInstant(first):
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
28 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first))+delta])
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
29 else:
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
30 featureList.append([feat,feat.getFirstInstant(),first-1,(currentFeature.getPositionAtInstant(first)-feat.getPositionAtInstant(first-1))+delta])
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
31 currentFeature = feat
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
32 first= feat.getFirstInstant()
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
33 # find the features to fill in the end of the object existence
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
34 delta=moving.Point(0,0)
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
35 currentFeature = obj.getFeature(featureID) # need to reinitialize
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
36 while last!= obj.getLastInstant():
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
37 featureSet = [f for f in obj.getFeatures() if f.existsAtInstant(last+1)]
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
38 feat = findNearest(currentFeature,featureSet,last+1,reverse=False)
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
39 if feat.existsAtInstant(last):
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
40 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last))+delta])
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
41 else:
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
42 featureList.append([feat,last+1,feat.getLastInstant(),(currentFeature.getPositionAtInstant(last)-feat.getPositionAtInstant(last+1))+delta])
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
43 currentFeature = feat
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
44 last= feat.getLastInstant()
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
45 delta=featureList[-1][3]
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
46 return featureList
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
47
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
48 def buildFeature(obj, featureID, num = 1):
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
49 featureList= getFeatures(obj, featureID)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
50 tmp={}
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
51 delta={}
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
52 for i in featureList:
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 665
diff changeset
53 for t in range(i[1],i[2]+1):
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
54 tmp[t]=[i[0],i[3]]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
55 newTraj = moving.Trajectory()
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
56
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
57 for instant in obj.getTimeInterval():
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
58 newTraj.addPosition(tmp[instant][0].getPositionAtInstant(instant)+tmp[instant][1])
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
59 newFeature= moving.MovingObject(num,timeInterval=obj.getTimeInterval(),positions=newTraj)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
60 return newFeature
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
61
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
62 def getBearing(p1,p2,p3):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
63 angle = degrees(atan2(p3.y -p1.y, p3.x -p1.x))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
64 bearing1 = (90 - angle) % 360
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
65 angle2 = degrees(atan2(p2.y -p1.y, p2.x -p1.x))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
66 bearing2 = (90 - angle2) % 360
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
67 dist= moving.Point.distanceNorm2(p1, p2)
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
68 return [dist,bearing1,bearing2,bearing2-bearing1]
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
69
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
70 #Quantitative analysis "CSJ" functions
650
d74e8c175d6b minor cleaning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 644
diff changeset
71 def computeVelocities(obj, smoothing=True, halfWidth=3): #compute velocities from positions
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
72 velocities={}
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
73 for i in list(obj.timeInterval)[:-1]:
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
74 p1= obj.getPositionAtInstant(i)
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
75 p2= obj.getPositionAtInstant(i+1)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
76 velocities[i]=p2-p1
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
77 velocities[obj.getLastInstant()]= velocities[obj.getLastInstant()-1] # duplicate last point
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
78 if smoothing:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
79 velX= [velocities[y].aslist()[0] for y in sorted(velocities.keys())]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
80 velY= [velocities[y].aslist()[1] for y in sorted(velocities.keys())]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
81 v1= list(utils.filterMovingWindow(velX, halfWidth))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
82 v2= list(utils.filterMovingWindow(velY, halfWidth))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
83 smoothedVelocity={}
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
84 for t,i in enumerate(sorted(velocities.keys())):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
85 smoothedVelocity[i]=moving.Point(v1[t], v2[t])
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
86 velocities=smoothedVelocity
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
87 return velocities
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
88
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
89 def computeAcceleration(obj,fromPosition=True):
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
90 acceleration={}
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
91 if fromPosition:
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
92 velocities=computeVelocities(obj,False,1)
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
93 for i in sorted(velocities.keys()):
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
94 if i != sorted(velocities.keys())[-1]:
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
95 acceleration[i]= velocities[i+1]-velocities[i]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
96 else:
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
97 for i in list(obj.timeInterval)[:-1]:
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
98 v1= obj.getVelocityAtInstant(i)
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
99 v2= obj.getVelocityAtInstant(i+1)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
100 acceleration[i]= v2-v1
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
101 return acceleration
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
102
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
103 def computeJerk(obj,fromPosition=True):
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
104 jerk={}
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
105 acceleration=computeAcceleration(obj,fromPosition=fromPosition)
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
106 for i in sorted(acceleration.keys()):
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
107 if i != sorted(acceleration.keys())[-1]:
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
108 jerk[i] = (acceleration[i+1]-acceleration[i]).norm2()
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
109 return jerk
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
110
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
111 def sumSquaredJerk(obj,fromPosition=True):
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
112 jerk= computeJerk(obj,fromPosition=fromPosition)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
113 t=0
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
114 for i in sorted(jerk.keys()):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
115 t+= jerk[i]* jerk[i]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
116 return t
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
117
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
118 def smoothObjectTrajectory(obj, featureID,newNum,smoothing=False,halfWidth=3,create=False):
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
119 results=[]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
120 bearing={}
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
121 if create:
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
122 feature = buildFeature(obj, featureID , num=1) # why num=1
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
123 else:
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
124 feature = obj.getFeature(featureID)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
125 for t in feature.getTimeInterval():
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
126 p1= feature.getPositionAtInstant(t)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
127 p2= obj.getPositionAtInstant(t)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
128 if t!=feature.getLastInstant():
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
129 p3= feature.getPositionAtInstant(t+1)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
130 else:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
131 p1= feature.getPositionAtInstant(t-1)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
132 p3= feature.getPositionAtInstant(t)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
133 bearing[t]= getBearing(p1,p2,p3)[1]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
134 results.append(getBearing(p1,p2,p3))
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
135
650
d74e8c175d6b minor cleaning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 644
diff changeset
136 medianResults=median(results,0)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
137 dist= medianResults[0]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
138 angle= medianResults[3]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
139
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
140 for i in sorted(bearing.keys()):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
141 bearing[i]= bearing[i]+angle
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
142
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
143 if smoothing:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
144 bearingInput=[]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
145 for i in sorted(bearing.keys()):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
146 bearingInput.append(bearing[i])
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
147 import utils
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
148 bearingOut=utils.filterMovingWindow(bearingInput, halfWidth)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
149 for t,i in enumerate(sorted(bearing.keys())):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
150 bearing[i]=bearingOut[t]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
151
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
152 #solve a smoothing problem in case of big drop in computing bearing (0,360)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
153 for t,i in enumerate(sorted(bearing.keys())):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
154 if i!= max(bearing.keys()) and abs(bearingInput[t] - bearingInput[t+1])>=340:
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 665
diff changeset
155 for x in range(max(i-halfWidth,min(bearing.keys())),min(i+halfWidth,max(bearing.keys()))+1):
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
156 bearing[x]=bearingInput[t-i+x]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
157
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
158 translated = moving.Trajectory()
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
159 for t in feature.getTimeInterval():
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
160 p1= feature.getPositionAtInstant(t)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
161 p1.x = p1.x + dist*sin(bearing[t]*pi/180)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
162 p1.y = p1.y + dist*cos(bearing[t]*pi/180)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
163 translated.addPosition(p1)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
164
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
165 #modify first and last un-smoothed positions (half width)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
166 if smoothing:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
167 d1= translated[halfWidth]- feature.positions[halfWidth]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
168 d2= translated[-halfWidth-1]- feature.positions[-halfWidth-1]
998
933670761a57 updated code to python 3 (tests pass and scripts run, but non-executed parts of code are probably still not correct)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 665
diff changeset
169 for i in range(halfWidth):
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
170 p1= feature.getPositionAt(i)+d1
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
171 p2= feature.getPositionAt(-i-1)+d2
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
172 translated.setPosition(i,p1)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
173 translated.setPosition(-i-1,p2)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
174
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
175 newObj= moving.MovingObject(newNum,timeInterval=feature.getTimeInterval(),positions=translated)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
176 return newObj
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
177
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
178 def smoothObject(obj, newNum, minLengthParam = 0.7, smoothing = False, plotResults = True, halfWidth = 3, _computeVelocities = True, optimize = True, create = False):
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
179 '''Computes a smoother trajectory for the object
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
180 and optionnally smoother velocities
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
181
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
182 The object should have its features in obj.features
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
183 TODO: check whether features are necessary'''
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
184 if not obj.hasFeatures():
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
185 print('Object {} has an empty list of features: please load and add them using obj.setFeatures(features)'.format(obj.getNum()))
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
186 from sys import exit
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
187 exit()
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
188
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
189 featureList=[i for i,f in enumerate(obj.getFeatures()) if f.length() >= minLengthParam*obj.length()]
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
190 if featureList==[]:
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
191 featureList.append(utils.argmaxDict({i:f.length() for i,f in enumerate(obj.getFeatures())}))
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
192 create = True
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
193 newObjects = []
644
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
194 for featureID in featureList: # featureID should be the index in the list of obj.features
e54751e71d61 more cleanup
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 641
diff changeset
195 newObjects.append(smoothObjectTrajectory(obj, featureID, newNum, smoothing = smoothing, halfWidth = halfWidth, create = create))
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
196
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
197 newTranslated = moving.Trajectory()
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
198 newInterval = []
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
199 for t in obj.getTimeInterval():
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
200 xCoord=[]
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
201 yCoord=[]
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
202 for i in newObjects:
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
203 if i.existsAtInstant(t):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
204 p1= i.getPositionAtInstant(t)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
205 xCoord.append(p1.x)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
206 yCoord.append(p1.y)
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
207 if xCoord != []:
650
d74e8c175d6b minor cleaning
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 644
diff changeset
208 tmp= moving.Point(median(xCoord), median(yCoord))
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
209 newInterval.append(t)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
210 newTranslated.addPosition(tmp)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
211
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
212 newObj= moving.MovingObject(newNum, timeInterval = moving.TimeInterval(min(newInterval),max(newInterval)),positions=newTranslated)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
213
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
214 if _computeVelocities:
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
215 tmpTraj = moving.Trajectory()
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
216 velocities= computeVelocities(newObj,True,5)
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
217 for i in sorted(velocities.keys()):
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
218 tmpTraj.addPosition(velocities[i])
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
219 newObj.velocities=tmpTraj
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
220 else:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
221 newObj.velocities=obj.velocities
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
222
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
223 if optimize:
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
224 csj1= sumSquaredJerk(obj,fromPosition=True)
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
225 csj2= sumSquaredJerk(newObj,fromPosition=True)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
226 if csj1<csj2:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
227 newObj=obj
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
228 newObj.velocities=obj.velocities
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
229 if _computeVelocities and csj1>=csj2:
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
230 csj3= sumSquaredJerk(obj,fromPosition=False)
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
231 csj4= sumSquaredJerk(newObj,fromPosition=False)
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
232 if csj4<=csj3:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
233 newObj.velocities= obj.velocities
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
234
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
235 newObj.featureNumbers=obj.featureNumbers
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
236 newObj.features=obj.getFeatures()
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
237 newObj.userType=obj.userType
641
9fe254f11743 cleaning under way and renaming
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 619
diff changeset
238
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
239 if plotResults:
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
240 plt.figure()
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
241 plt.title('objects_id = {}'.format(obj.num))
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
242 for i in featureList:
661
dc70d9e711f5 some method name change and new methods for features in objects (MovingObject) and methods to access indicator values in interactions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 650
diff changeset
243 obj.getFeature(i).plot('cx-')
619
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
244 obj.plot('rx-')
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
245 newObj.plot('gx-')
dc2d0a0d7fe1 merged code from Mohamed Gomaa Mohamed for the use of points of interests in mation pattern learning and motion prediction (TRB 2015)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents: 612
diff changeset
246 return newObj