Mercurial > hg > nsaunier > traffic-intelligence
comparison trafficintelligence/moving.py @ 1290:9012fb72d79a
update isstationary
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Fri, 26 Jul 2024 21:53:03 +0200 |
| parents | 86122f5fe500 |
| children | f3574e43c238 |
comparison
equal
deleted
inserted
replaced
| 1289:86122f5fe500 | 1290:9012fb72d79a |
|---|---|
| 1764 '''compute velocities, smoothed if halfwidth is not None ''' | 1764 '''compute velocities, smoothed if halfwidth is not None ''' |
| 1765 if halfWidth is None: | 1765 if halfWidth is None: |
| 1766 self.velocities = self.getPositions().differentiate(True) | 1766 self.velocities = self.getPositions().differentiate(True) |
| 1767 else: | 1767 else: |
| 1768 self.velocities = self.getPositions().differentiate().filterMovingWindow(halfWidth) | 1768 self.velocities = self.getPositions().differentiate().filterMovingWindow(halfWidth) |
| 1769 self.velocities.addPosition(self.velocities[-1]) | 1769 self.velocities.addPosition(self.velocities[-1]) |
| 1770 | 1770 |
| 1771 def smoothPositions(self, halfWidth, replace = False): | 1771 def smoothPositions(self, halfWidth, replace = False): |
| 1772 'Returns the smoothed positions (or replaces them)' | 1772 'Returns the smoothed positions (or replaces them)' |
| 1773 smoothed = self.getPositions().filterMovingWindow(halfWidth) | 1773 smoothed = self.getPositions().filterMovingWindow(halfWidth) |
| 1774 if replace: | 1774 if replace: |
| 1860 else: | 1860 else: |
| 1861 indices = flatnonzero(speeds < speedThreshold).tolist() | 1861 indices = flatnonzero(speeds < speedThreshold).tolist() |
| 1862 if len(indices) >= 2: | 1862 if len(indices) >= 2: |
| 1863 i = 0 | 1863 i = 0 |
| 1864 j = len(indices)-1 | 1864 j = len(indices)-1 |
| 1865 #incrementI = True | 1865 incrementI = True |
| 1866 while i<j and (quantile(speeds[indices[i]:indices[j]+1], 0.5) > speedThreshold or Point.distanceNorm2(self.getPositionAt(indices[i]),self.getPositionAt(indices[j])) > distanceThreshold): | 1866 while i<j and (quantile(speeds[indices[i]:indices[j]+1], 0.5) > speedThreshold or Point.distanceNorm2(self.getPositionAt(indices[i]),self.getPositionAt(indices[j])) > distanceThreshold): |
| 1867 #if incrementI: | 1867 #if incrementI: |
| 1868 i+=1 | 1868 i+=1 |
| 1869 #else: | 1869 #else: |
| 1870 j-=1 | 1870 j-=1 |
| 1871 #incrementI = not incrementI | 1871 #incrementI = not incrementI |
| 1872 if i<j: # we found a smaller subset | 1872 if i<j: # we found a smaller subset |
| 1873 # try to grow the subset | |
| 1874 i-=1 | |
| 1875 while i>=0 and (mean(speeds[indices[i]:indices[j]+1]) <= speedThreshold and Point.distanceNorm2(self.getPositionAt(indices[i]),self.getPositionAt(indices[j])) <= distanceThreshold): | |
| 1876 i-=1 # it will go 1 too far | |
| 1877 i+=1 | |
| 1878 j+=1 | |
| 1879 while j<len(indices) and (mean(speeds[indices[i]:indices[j]+1]) <= speedThreshold and Point.distanceNorm2(self.getPositionAt(indices[i]),self.getPositionAt(indices[j])) <= distanceThreshold): | |
| 1880 j+=1 # it will go 1 too far | |
| 1881 j-=1 | |
| 1873 firstInstant = self.getFirstInstant() | 1882 firstInstant = self.getFirstInstant() |
| 1874 return True, [indices[i]+firstInstant, indices[j]+firstInstant] | 1883 return True, [indices[i]+firstInstant, indices[j]+firstInstant] |
| 1875 else: | 1884 else: |
| 1876 return False, None | 1885 return False, None |
| 1877 else: | 1886 else: |
