diff trafficintelligence/events.py @ 1256:56d0195d043e

cleaning indicators (no more time interval) and runtimeerror with arccos
author Nicolas Saunier <nicolas.saunier@polymtl.ca>
date Wed, 03 Apr 2024 14:41:20 -0400
parents fe35473acee3
children 39740c4668ac
line wrap: on
line diff
--- a/trafficintelligence/events.py	Wed Apr 03 12:30:36 2024 -0400
+++ b/trafficintelligence/events.py	Wed Apr 03 14:41:20 2024 -0400
@@ -6,6 +6,7 @@
 from trafficintelligence.base import VideoFilenameAddable
 
 import numpy as np
+from matplotlib.pyplot import subplot, figure, ylabel
 
 import multiprocessing
 import itertools, logging
@@ -168,6 +169,20 @@
         self.roadUser1.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs)
         self.roadUser2.plotOnWorldImage(nPixelsPerUnitDistance, options, withOrigin, timeStep, **kwargs)
 
+    def plotIndicators(self, _indicatorNames = indicatorNames):
+        nrows = int(np.ceil(len(_indicatorNames)/2))
+        ncols = 2
+        #subplot(nrows, 2)
+        for i, indicatorName in enumerate(_indicatorNames):
+            if i==0:
+                ax = subplot(nrows, ncols, i+1)
+            else:
+                subplot(nrows, ncols, i+1, sharex = ax)
+            ind = self.getIndicator(indicatorName)
+            if ind is not None:
+                ind.plot()
+                ylabel(indicatorName)
+        
     def play(self, videoFilename, homography = None, undistort = False, intrinsicCameraMatrix = None, distortionCoefficients = None, undistortedImageMultiplication = 1., allUserInstants = False):
         if self.roadUser1 is not None and self.roadUser2 is not None:
             if allUserInstants:
@@ -196,14 +211,14 @@
             v1Norm = v1.norm2()
             v2Norm = v2.norm2()
             if v1Norm != 0. and v2Norm != 0.:
-                velocityAngles[instant] = np.arccos(moving.Point.dot(v1, v2)/(v1Norm*v2Norm))
+                velocityAngles[instant] = np.arccos(max(-1, min(1, moving.Point.dot(v1, v2)/(v1Norm*v2Norm))))
             collisionCourseDotProducts[instant] = moving.Point.dot(deltap, deltav)
             distances[instant] = deltap.norm2()
             speedDifferentials[instant] = deltav.norm2()
             if collisionCourseDotProducts[instant] > 0:
                 interactionInstants.append(instant)
             if distances[instant] != 0 and speedDifferentials[instant] != 0:
-                collisionCourseAngles[instant] = np.arccos(collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant]))
+                collisionCourseAngles[instant] = np.arccos(max(-1, min(1, collisionCourseDotProducts[instant]/(distances[instant]*speedDifferentials[instant])))) # avoid values slightly higher than 1.0
 
         if len(interactionInstants) >= 2:
             self.interactionInterval = moving.TimeInterval(interactionInstants[0], interactionInstants[-1])