Mercurial Hosting > traffic-intelligence
annotate trafficintelligence/indicators.py @ 1265:0f5bebd62a55
minor modifications
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 24 May 2024 16:15:38 -0400 |
parents | 56d0195d043e |
children | ad60e5adf084 |
rev | line source |
---|---|
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
2 '''Class for indicators, temporal indicators, and safety indicators''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
3 |
691
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
4 from matplotlib.pyplot import plot, ylim |
1240
bb14f919d1cb
cleaned use of centile (np only) and added info in classify-objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1121
diff
changeset
|
5 from numpy import array, arange, mean, floor, mean, percentile |
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
6 |
1029
c6cf75a2ed08
reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
7 from trafficintelligence import moving |
c6cf75a2ed08
reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
8 from trafficintelligence.utils import LCSS as utilsLCSS |
c6cf75a2ed08
reorganization of imports
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1028
diff
changeset
|
9 |
727
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
10 def multivariateName(indicatorNames): |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
11 return '_'.join(indicatorNames) |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
12 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
13 # need for a class representing the indicators, their units, how to print them in graphs... |
665
15e244d2a1b5
corrected bug with circular import for VideoFilenameAddable, moved to base module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
661
diff
changeset
|
14 class TemporalIndicator(object): |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
15 '''Class for temporal indicators |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
16 i.e. indicators that take a value at specific instants |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
17 |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
18 values is a dict, for the values at specific time instants |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
19 it should have more information like name, unit''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
20 |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
21 def __init__(self, name, values, maxValue = None): |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
22 self.name = name |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
23 self.values = values |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
24 instants = sorted(self.values.keys()) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
25 if len(instants) > 0: |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
26 self.timeInterval = moving.TimeInterval(instants[0], instants[-1]) |
693
5ee22bf7e4d5
corrected bug when loading indicator time intervals and updated how queries are created for better legibility
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
692
diff
changeset
|
27 else: |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
28 self.timeInterval = moving.TimeInterval() |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
29 # else: |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
30 # assert len(values) == timeInterval.length() |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
31 # self.timeInterval = timeInterval |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
32 # self.values = {} |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
33 # for i in range(int(round(self.timeInterval.length()))): |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
34 # self.values[self.timeInterval[i]] = values[i] |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
35 self.maxValue = maxValue |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
36 |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
37 def __len__(self): |
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
38 return len(self.values) |
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
39 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
40 def empty(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
41 return len(self.values) == 0 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
42 |
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:
636
diff
changeset
|
43 def __getitem__(self, t): |
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:
636
diff
changeset
|
44 'Returns the value at time t' |
724
b75d0c258ca9
update to indicator class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
693
diff
changeset
|
45 return self.values.get(t) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
46 |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
47 def getIthValue(self, i): |
285
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
48 sortedKeys = sorted(self.values.keys()) |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
49 if 0<=i<len(sortedKeys): |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
50 return self.values[sortedKeys[i]] |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
51 else: |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
52 return None |
5957aa1d69e1
Integrating Mohamed's changes
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
282
diff
changeset
|
53 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
54 def __iter__(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
55 self.iterInstantNum = 0 # index in the interval or keys of the dict |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
56 return self |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
57 |
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:
727
diff
changeset
|
58 def __next__(self): |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
59 if self.iterInstantNum >= len(self.values):#(self.timeInterval and self.iterInstantNum>=self.timeInterval.length())\ |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
60 # or (self.iterInstantNum >= self.values) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
61 raise StopIteration |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
62 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
63 self.iterInstantNum += 1 |
287
66691c06928c
first version of indicator LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
64 return self.getIthValue(self.iterInstantNum-1) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
65 |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
66 def min(self): |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
67 return min(self.values.values()) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
68 |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
69 def max(self): |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
70 return max(self.values.values()) |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
71 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
72 def getTimeInterval(self): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
73 return self.timeInterval |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
74 |
341
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
75 def getName(self): |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
76 return self.name |
2f39c4ed0b62
first version of indicator saving to sqlite
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
340
diff
changeset
|
77 |
1121
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
78 def getValues(self, withNone = True): |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
79 result = [self.__getitem__(t) for t in self.timeInterval] |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
80 if withNone: |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
81 return result |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
82 else: |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
83 return [x for x in result if x is not None] |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
84 |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
85 def getInstants(self): |
6baa915dd8bd
added functionalities for TemporalIndicator (for Lionel)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1112
diff
changeset
|
86 return list(self.values.keys()) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
87 |
408
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
389
diff
changeset
|
88 def plot(self, options = '', xfactor = 1., yfactor = 1., timeShift = 0, **kwargs): |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
89 if self.getTimeInterval().length() == 1: |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
90 marker = 'o' |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
91 else: |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
92 marker = '' |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
93 time = sorted(self.values.keys()) |
408
365d8dee44f3
last changes for TRB14
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
389
diff
changeset
|
94 plot([(x+timeShift)/xfactor for x in time], [self.values[i]/yfactor for i in time], options+marker, **kwargs) |
269
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
95 if self.maxValue: |
a9988971aac8
removed legacy code + tweaks
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
265
diff
changeset
|
96 ylim(ymax = self.maxValue) |
727
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
97 |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
98 @classmethod |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
99 def createMultivariate(cls, indicators): |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
100 '''Creates a new temporal indicator where the value at each instant is a list |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
101 of the indicator values at the instant, in the same order |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
102 the time interval will be the union of the time intervals of the indicators |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
103 name is concatenation of the indicator names''' |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
104 if len(indicators) < 2: |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
105 print('Error creating multivariate indicator with only {} indicator'.format(len(indicators))) |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
106 return None |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
107 |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
108 timeInterval = moving.TimeInterval.unionIntervals([indic.getTimeInterval() for indic in indicators]) |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
109 values = {} |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
110 for t in timeInterval: |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
111 tmpValues = [indic[t] for indic in indicators] |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
112 uniqueValues = set(tmpValues) |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
113 if len(uniqueValues) >= 2 or uniqueValues.pop() is not None: |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
114 values[t] = tmpValues |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
115 return cls(multivariateName([indic.name for indic in indicators]), values) |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
116 |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
117 # TODO static method avec class en parametre pour faire des indicateurs agrege, list par instant |
282
abbd4bc13dac
modified indicator class (same interface)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
269
diff
changeset
|
118 |
442
eb8baa080470
generalized indicator LCSS with similarityFunc (thanks Mohamed)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
408
diff
changeset
|
119 def l1Distance(x, y): # lambda x,y:abs(x-y) |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
120 if x is None or y is None: |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
121 return float('inf') |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
122 else: |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
123 return abs(x-y) |
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
124 |
727
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
125 def multiL1Matching(x, y, thresholds, proportionMatching=1.): |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
126 n = 0 |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
127 nDimensions = len(x) |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
128 for i in range(nDimensions): |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
129 if l1Distance(x[i], y[i]) <= thresholds[i]: |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
130 n += 1 |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
131 return n >= nDimensions*proportionMatching |
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
132 |
369
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
133 class LCSS(utilsLCSS): |
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
134 '''Adapted LCSS class for indicators, same pattern''' |
442
eb8baa080470
generalized indicator LCSS with similarityFunc (thanks Mohamed)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
408
diff
changeset
|
135 def __init__(self, similarityFunc, delta = float('inf'), minLength = 0, aligned = False, lengthFunc = min): |
727
c6d4ea05a2d0
adding ability to deal with multivariate indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
724
diff
changeset
|
136 utilsLCSS.__init__(self, similarityFunc = similarityFunc, delta = delta, aligned = aligned, lengthFunc = lengthFunc) |
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
137 self.minLength = minLength |
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
138 |
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
139 def checkIndicator(self, indicator): |
636
3058e00887bc
removed all issues because of tests with None, using is instead of == or !=
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
631
diff
changeset
|
140 return indicator is not None and len(indicator) >= self.minLength |
321
a5e40bd04cf4
rearranged LCSS indicator functions
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
313
diff
changeset
|
141 |
375
2ea8584aa80a
making indicator LCSS work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
370
diff
changeset
|
142 def compute(self, indicator1, indicator2, computeSubSequence = False): |
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
143 if self.checkIndicator(indicator1) and self.checkIndicator(indicator2): |
375
2ea8584aa80a
making indicator LCSS work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
370
diff
changeset
|
144 return self._compute(indicator1.getValues(), indicator2.getValues(), computeSubSequence) |
369
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
145 else: |
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
146 return 0 |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
147 |
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
148 def computeNormalized(self, indicator1, indicator2, computeSubSequence = False): |
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
149 if self.checkIndicator(indicator1) and self.checkIndicator(indicator2): |
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
150 return self._computeNormalized(indicator1.getValues(), indicator2.getValues(), computeSubSequence) |
369
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
151 else: |
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
152 return 0. |
322
28661c5887d3
Corrected a major bug for LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
321
diff
changeset
|
153 |
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
154 def computeDistance(self, indicator1, indicator2, computeSubSequence = False): |
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
155 if self.checkIndicator(indicator1) and self.checkIndicator(indicator2): |
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
156 return self._computeDistance(indicator1.getValues(), indicator2.getValues(), computeSubSequence) |
369
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
157 else: |
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
158 return 1. |
027e254f0b53
lcss subclass for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
368
diff
changeset
|
159 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
160 class SeverityIndicator(TemporalIndicator): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
161 '''Class for severity indicators |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
162 field mostSevereIsMax is True |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
163 if the most severe value taken by the indicator is the maximum''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
164 |
1256
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
165 def __init__(self, name, values, mostSevereIsMax=True, maxValue = None): |
56d0195d043e
cleaning indicators (no more time interval) and runtimeerror with arccos
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1240
diff
changeset
|
166 TemporalIndicator.__init__(self, name, values, maxValue) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
167 self.mostSevereIsMax = mostSevereIsMax |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
168 |
1042
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
169 def getMostSevereValue(self, minNInstants=None, centile=None): |
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:
727
diff
changeset
|
170 '''if there are more than minNInstants observations, |
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:
727
diff
changeset
|
171 returns either the average of these maximum values |
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:
727
diff
changeset
|
172 or if centile is not None the n% centile from the most severe value |
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:
727
diff
changeset
|
173 |
1042
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
174 eg for TTC, centile = 15 returns the 15th centile (value such that 15% of observations are lower)''' |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
175 values = list(self.values.values()) |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
176 if centile is not None: |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
177 if self.mostSevereIsMax: |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
178 c = 100-centile |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
179 else: |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
180 c = centile |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
181 return percentile(values, c) |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
182 elif minNInstants is not None and minNInstants <= self.__len__(): |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
183 values = sorted(values, reverse = self.mostSevereIsMax) # inverted if most severe is max -> take the first values |
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
184 return mean(values[:minNInstants]) |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
185 else: |
1042
b1ba6d44fcb9
corrected bug in severity indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1029
diff
changeset
|
186 return None |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
187 |
1265
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
188 def getInstantOfMostSevereValue(self, minSevereValue = None): |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
189 '''Returns the instant at which the indicator reaches its most severe value |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
190 or the instants when value is above minSevereValue (it not None)''' |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
191 if minSevereValue is None: |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
192 if self.mostSevereIsMax: |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
193 return max(self.values, key=self.values.get) |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
194 else: |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
195 return min(self.values, key=self.values.get) |
692
9a258687af4c
corrected some errors for ttc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
691
diff
changeset
|
196 else: |
1265
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
197 if self.mostSevereIsMax: |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
198 return [t for t in self.values if self.values[t] >= minSevereValue] |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
199 else: |
0f5bebd62a55
minor modifications
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
1256
diff
changeset
|
200 return [t for t in self.values if self.values[t] <= minSevereValue] |
692
9a258687af4c
corrected some errors for ttc
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
691
diff
changeset
|
201 |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
202 # functions to aggregate discretized maps of indicators |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
203 # TODO add values in the cells between the positions (similar to discretizing vector graphics to bitmap) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
204 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
205 def indicatorMap(indicatorValues, trajectory, squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
206 '''Returns a dictionary |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
207 with keys for the indices of the cells (squares) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
208 in which the trajectory positions are located |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
209 at which the indicator values are attached |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
210 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
211 ex: speeds and trajectory''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
212 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
213 assert len(indicatorValues) == trajectory.length() |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
214 indicatorMap = {} |
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:
727
diff
changeset
|
215 for k in range(trajectory.length()): |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
216 p = trajectory[k] |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
217 i = floor(p.x/squareSize) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
218 j = floor(p.y/squareSize) |
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:
727
diff
changeset
|
219 if (i,j) in indicatorMap: |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
220 indicatorMap[(i,j)].append(indicatorValues[k]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
221 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
222 indicatorMap[(i,j)] = [indicatorValues[k]] |
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:
727
diff
changeset
|
223 for k in indicatorMap: |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
224 indicatorMap[k] = mean(indicatorMap[k]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
225 return indicatorMap |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
226 |
691
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
227 # def indicatorMapFromPolygon(value, polygon, squareSize): |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
228 # '''Fills an indicator map with the value within the polygon |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
229 # (array of Nx2 coordinates of the polygon vertices)''' |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
230 # points = [] |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
231 # for x in arange(min(polygon[:,0])+squareSize/2, max(polygon[:,0]), squareSize): |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
232 # for y in arange(min(polygon[:,1])+squareSize/2, max(polygon[:,1]), squareSize): |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
233 # points.append([x,y]) |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
234 # inside = nx.points_inside_poly(array(points), polygon) |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
235 # indicatorMap = {} |
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:
727
diff
changeset
|
236 # for i in range(len(inside)): |
691
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
237 # if inside[i]: |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
238 # indicatorMap[(floor(points[i][0]/squareSize), floor(points[i][1]/squareSize))] = 0 |
fa9aa5f08210
cleaned imports in indicators.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
665
diff
changeset
|
239 # return indicatorMap |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
240 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
241 def indicatorMapFromAxis(value, limits, squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
242 '''axis = [xmin, xmax, ymin, ymax] ''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
243 indicatorMap = {} |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
244 for x in arange(limits[0], limits[1], squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
245 for y in arange(limits[2], limits[3], squareSize): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
246 indicatorMap[(floor(x/squareSize), floor(y/squareSize))] = value |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
247 return indicatorMap |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
248 |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
249 def combineIndicatorMaps(maps, squareSize, combinationFunction): |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
250 '''Puts many indicator maps together |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
251 (averaging the values in each cell |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
252 if more than one maps has a value)''' |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
253 indicatorMap = {} |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
254 for m in maps: |
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:
727
diff
changeset
|
255 for k,v in m.items(): |
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:
727
diff
changeset
|
256 if k in indicatorMap: |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
257 indicatorMap[k].append(v) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
258 else: |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
259 indicatorMap[k] = [v] |
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:
727
diff
changeset
|
260 for k in indicatorMap: |
244
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
261 indicatorMap[k] = combinationFunction(indicatorMap[k]) |
5027c174ab90
moved indicators to new file, added ExtrapolatedTrajectory class to extrapolation file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
diff
changeset
|
262 return indicatorMap |
247
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
263 |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
264 if __name__ == "__main__": |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
265 import doctest |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
266 import unittest |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
267 suite = doctest.DocFileSuite('tests/indicators.txt') |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
268 unittest.TextTestRunner().run(suite) |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
269 # #doctest.testmod() |
8f0ed138d373
moved the tests for indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
244
diff
changeset
|
270 # #doctest.testfile("example.txt") |