Mercurial > hg > nsaunier > traffic-intelligence
annotate python/utils.py @ 462:af2222c0c9c0
TTC tested and updatet!
| author | Sohail Zangenehpour <sohail.zangenehpour@mail.mcgill.ca> |
|---|---|
| date | Mon, 03 Feb 2014 15:41:57 -0500 |
| parents | 9a714f32fc9f |
| children | abe0b2347d4c |
| rev | line source |
|---|---|
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
1 #! /usr/bin/env python |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
2 ''' Generic utilities.''' |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
3 |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
4 #from numpy import * |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
5 #from pylab import * |
|
397
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
6 from datetime import time, datetime |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
7 |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
8 __metaclass__ = type |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
9 |
|
45
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
10 commentChar = '#' |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
11 |
|
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
12 delimiterChar = '%'; |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
13 |
|
421
4fce27946c60
first example of video metadata using sqlalchemy
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
405
diff
changeset
|
14 datetimeFormat = "%Y-%m-%d %H:%M:%S" |
|
4fce27946c60
first example of video metadata using sqlalchemy
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
405
diff
changeset
|
15 |
|
185
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
16 ######################### |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
17 # Enumerations |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
18 ######################### |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
19 |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
20 def inverseEnumeration(l): |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
21 'Returns the dictionary that provides for each element in the input list its index in the input list' |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
22 result = {} |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
23 for i,x in enumerate(l): |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
24 result[x] = i |
|
c06379f25ab8
utilities for user types
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
181
diff
changeset
|
25 return result |
|
155
f03fe3d6d0c8
added functions to parse options
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
26 |
|
f03fe3d6d0c8
added functions to parse options
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
152
diff
changeset
|
27 ######################### |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
28 # simple statistics |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
29 ######################### |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
30 |
|
423
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
31 def sampleSize(stdev, tolerance, percentConfidence, printLatex = False): |
|
301
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
32 from scipy.stats.distributions import norm |
|
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
33 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. |
|
423
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
34 if printLatex: |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
35 print('${0}^2\\frac{{{1}^2}}{{{2}^2}}$'.format(k, stdev, tolerance)) |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
36 return (k*stdev/tolerance)**2 |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
37 |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
38 def confidenceInterval(mean, stdev, nSamples, percentConfidence, trueStd = True, printLatex = False): |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
39 'if trueStd, use normal distribution, otherwise, Student' |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
40 from math import sqrt |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
41 from scipy.stats.distributions import norm, t |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
42 if trueStd: |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
43 k = round(norm.ppf(0.5+percentConfidence/200., 0, 1)*100)/100. # 1.-(100-percentConfidence)/200. |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
44 else: # use Student |
|
f738fa1b69f0
added sample size and Student distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
421
diff
changeset
|
45 k = round(t.ppf(0.5+percentConfidence/200., nSamples-1)*100)/100. |
|
301
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
46 e = k*stdev/sqrt(nSamples) |
|
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
47 if printLatex: |
|
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
48 print('${0} \pm {1}\\frac{{{2}}}{{\sqrt{{{3}}}}}$'.format(mean, k, stdev, nSamples)) |
|
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
49 return mean-e, mean+e |
|
27f06d28036d
added simple helper for confidence intervals
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
297
diff
changeset
|
50 |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
51 def computeChi2(expected, observed): |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
52 '''Returns the Chi2 statistics''' |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
53 result = 0. |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
54 for e, o in zip(expected, observed): |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
55 result += ((e-o)*(e-o))/e |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
56 return result |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
57 |
|
85
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
58 class EmpiricalDistribution: |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
59 def nSamples(self): |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
60 return sum(self.counts) |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
61 |
|
197
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
62 def cumulativeDensityFunction(sample): |
| 276 | 63 '''Returns the cumulative density function of the sample of a random variable''' |
|
197
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
64 from numpy.core.multiarray import array |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
65 from numpy.lib.function_base import unique |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
66 from numpy.core.fromnumeric import sum |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
67 a = array(sample) |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
68 a.sort() |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
69 xaxis = unique(a) |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
70 counts = [sum(a <= x) for x in xaxis] |
|
2788b2827670
simple cumulatie function distribution computation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
185
diff
changeset
|
71 return xaxis, counts |
|
85
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
72 |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
73 class EmpiricalDiscreteDistribution(EmpiricalDistribution): |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
74 '''Class to represent a sample of a distribution for a discrete random variable |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
75 ''' |
|
86
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
76 from numpy.core.fromnumeric import sum |
|
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
77 |
|
85
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
78 def __init__(self, categories, counts): |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
79 self.categories = categories |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
80 self.counts = counts |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
81 |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
82 def mean(self): |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
83 result = [float(x*y) for x,y in zip(self.categories, self.counts)] |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
84 return sum(result)/self.nSamples() |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
85 |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
86 def var(self, mean = None): |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
87 if not mean: |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
88 m = self.mean() |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
89 else: |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
90 m = mean |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
91 result = 0. |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
92 squares = [float((x-m)*(x-m)*y) for x,y in zip(self.categories, self.counts)] |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
93 return sum(squares)/(self.nSamples()-1) |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
94 |
|
86
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
95 def referenceCounts(self, probability): |
|
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
96 '''probability is a function that returns the probability of the random variable for the category values''' |
|
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
97 refProba = [probability(c) for c in self.categories] |
|
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
98 refProba[-1] = 1-sum(refProba[:-1]) |
|
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
99 refCounts = [r*self.nSamples() for r in refProba] |
|
f03ec4697a09
corrected discrete distribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
85
diff
changeset
|
100 return refCounts, refProba |
|
85
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
101 |
|
7f1e54234f96
added empirical discrete distribution, modified class organization and names
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
77
diff
changeset
|
102 class EmpiricalContinuousDistribution(EmpiricalDistribution): |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
103 '''Class to represent a sample of a distribution for a continuous random variable |
|
76
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
104 with the number of observations for each interval |
|
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
105 intervals (categories variable) are defined by their left limits, the last one being the right limit |
|
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
106 categories contain therefore one more element than the counts''' |
|
35
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
107 def __init__(self, categories, counts): |
| 276 | 108 # todo add samples for initialization and everything to None? (or setSamples?) |
|
35
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
109 self.categories = categories |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
110 self.counts = counts |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
111 |
|
35
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
112 def mean(self): |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
113 result = 0. |
|
76
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
114 for i in range(len(self.counts)-1): |
|
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
115 result += self.counts[i]*(self.categories[i]+self.categories[i+1])/2 |
|
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
116 return result/self.nSamples() |
|
35
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
117 |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
118 def var(self, mean = None): |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
119 if not mean: |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
120 m = self.mean() |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
121 else: |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
122 m = mean |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
123 result = 0. |
|
76
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
124 for i in range(len(self.counts)-1): |
|
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
125 mid = (self.categories[i]+self.categories[i+1])/2 |
|
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
126 result += self.counts[i]*(mid - m)*(mid - m) |
|
35
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
127 return result/(self.nSamples()-1) |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
128 |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
129 def referenceCounts(self, cdf): |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
130 '''cdf is a cumulative distribution function |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
131 returning the probability of the variable being less that x''' |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
132 # refCumulativeCounts = [0]#[cdf(self.categories[0][0])] |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
133 # for inter in self.categories: |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
134 # refCumulativeCounts.append(cdf(inter[1])) |
|
76
64fde2b1f96d
simplified intervales in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
75
diff
changeset
|
135 refCumulativeCounts = [cdf(x) for x in self.categories[1:-1]] |
|
35
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
136 |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
137 refProba = [refCumulativeCounts[0]] |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
138 for i in xrange(1,len(refCumulativeCounts)): |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
139 refProba.append(refCumulativeCounts[i]-refCumulativeCounts[i-1]) |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
140 refProba.append(1-refCumulativeCounts[-1]) |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
141 refCounts = [p*self.nSamples() for p in refProba] |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
142 |
|
8cafee54466f
forgotten update of histogram class
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
32
diff
changeset
|
143 return refCounts, refProba |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
144 |
|
77
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
145 def printReferenceCounts(self, refCounts=None): |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
146 if refCounts: |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
147 ref = refCounts |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
148 else: |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
149 ref = self.referenceCounts |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
150 for i in xrange(len(ref[0])): |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
151 print('{0}-{1} & {2:0.3} & {3:0.3} \\\\'.format(self.categories[i],self.categories[i+1],ref[1][i], ref[0][i])) |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
152 |
|
5e6cd36a991c
added pretty print in empiricalDistribution
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
76
diff
changeset
|
153 |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
154 ######################### |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
155 # maths section |
|
27
44689029a86f
updated segmentIntersection and other
Nicolas Saunier <nico@confins.net>
parents:
24
diff
changeset
|
156 ######################### |
|
24
6fb59cfb201e
first version of segmentIntersection
Nicolas Saunier <nico@confins.net>
parents:
19
diff
changeset
|
157 |
|
433
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
158 # def kernelSmoothing(sampleX, X, Y, weightFunc, halfwidth): |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
159 # '''Returns a smoothed weighted version of Y at the predefined values of sampleX |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
160 # Sum_x weight(sample_x,x) * y(x)''' |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
161 # from numpy import zeros, array |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
162 # smoothed = zeros(len(sampleX)) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
163 # for i,x in enumerate(sampleX): |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
164 # weights = array([weightFunc(x,xx, halfwidth) for xx in X]) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
165 # if sum(weights)>0: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
166 # smoothed[i] = sum(weights*Y)/sum(weights) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
167 # else: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
168 # smoothed[i] = 0 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
169 # return smoothed |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
170 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
171 def kernelSmoothing(x, X, Y, weightFunc, halfwidth): |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
172 '''Returns the smoothed estimate of (X,Y) at x |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
173 Sum_x weight(sample_x,x) * y(x)''' |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
174 from numpy import zeros, array |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
175 weights = array([weightFunc(x,observedx, halfwidth) for observedx in X]) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
176 if sum(weights)>0: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
177 return sum(weights*Y)/sum(weights) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
178 else: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
179 return 0 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
180 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
181 def uniform(center, x, halfwidth): |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
182 if abs(center-x)<halfwidth: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
183 return 1. |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
184 else: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
185 return 0. |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
186 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
187 def gaussian(center, x, halfwidth): |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
188 from numpy import exp |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
189 return exp(-((center-x)/halfwidth)**2/2) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
190 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
191 def epanechnikov(center, x, halfwidth): |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
192 diff = abs(center-x) |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
193 if diff<halfwidth: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
194 return 1.-(diff/halfwidth)**2 |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
195 else: |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
196 return 0. |
|
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
197 |
| 434 | 198 def triangular(center, x, halfwidth): |
| 199 diff = abs(center-x) | |
| 200 if diff<halfwidth: | |
| 201 return 1.-abs(diff/halfwidth) | |
| 202 else: | |
| 203 return 0. | |
|
433
d40ad901b272
added kernel smoothing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
423
diff
changeset
|
204 |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
205 def argMaxDict(d): |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
206 return max(d.iterkeys(), key=(lambda key: d[key])) |
|
279
3af4c267a7bf
generic simple LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
276
diff
changeset
|
207 |
|
395
6fba1ab040f1
minor modification to framestotime
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
391
diff
changeset
|
208 def framesToTime(nFrames, frameRate, initialTime = time()): |
|
6fba1ab040f1
minor modification to framestotime
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
391
diff
changeset
|
209 '''returns a datetime.time for the time in hour, minutes and seconds |
|
6fba1ab040f1
minor modification to framestotime
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
391
diff
changeset
|
210 initialTime is a datetime.time''' |
|
248
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
241
diff
changeset
|
211 from math import floor |
|
395
6fba1ab040f1
minor modification to framestotime
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
391
diff
changeset
|
212 seconds = int(floor(float(nFrames)/float(frameRate))+initialTime.hour*3600+initialTime.minute*60+initialTime.second) |
|
261
4aa792cb0fa9
changing framesToTime to return a datetime.time
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
248
diff
changeset
|
213 h = int(floor(seconds/3600.)) |
|
248
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
241
diff
changeset
|
214 seconds = seconds - h*3600 |
|
261
4aa792cb0fa9
changing framesToTime to return a datetime.time
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
248
diff
changeset
|
215 m = int(floor(seconds/60)) |
|
248
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
241
diff
changeset
|
216 seconds = seconds - m*60 |
|
262
a048066bd20f
correcting bug in framesToTime
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
261
diff
changeset
|
217 return time(h, m, seconds) |
|
248
571ba5ed22e2
added utils for bus processing
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
241
diff
changeset
|
218 |
|
381
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
376
diff
changeset
|
219 def timeToFrames(t, frameRate): |
|
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
376
diff
changeset
|
220 return frameRate*(t.hour*3600+t.minute*60+t.second) |
|
387cc0142211
script to replay event annotations
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
376
diff
changeset
|
221 |
|
241
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
222 def sortXY(X,Y): |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
223 'returns the sorted (x, Y(x)) sorted on X' |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
224 D = {} |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
225 for x, y in zip(X,Y): |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
226 D[x]=y |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
227 xsorted = sorted(D.keys()) |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
228 return xsorted, [D[x] for x in xsorted] |
|
ee1caff48b03
added function to sort to list of paired data X,Y
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
235
diff
changeset
|
229 |
| 32 | 230 def ceilDecimals(v, nDecimals): |
| 231 '''Rounds the number at the nth decimal | |
| 232 eg 1.23 at 0 decimal is 2, at 1 decimal is 1.3''' | |
| 233 from math import ceil,pow | |
| 234 tens = pow(10,nDecimals) | |
| 235 return ceil(v*tens)/tens | |
| 236 | |
|
152
74b1fc68d4df
re-organized code to avoid cyclic python module dependencies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
237 def inBetween(bound1, bound2, x): |
|
74b1fc68d4df
re-organized code to avoid cyclic python module dependencies
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
116
diff
changeset
|
238 return bound1 <= x <= bound2 or bound2 <= x<= bound1 |
|
24
6fb59cfb201e
first version of segmentIntersection
Nicolas Saunier <nico@confins.net>
parents:
19
diff
changeset
|
239 |
|
6fb59cfb201e
first version of segmentIntersection
Nicolas Saunier <nico@confins.net>
parents:
19
diff
changeset
|
240 def crossProduct(l1, l2): |
|
6fb59cfb201e
first version of segmentIntersection
Nicolas Saunier <nico@confins.net>
parents:
19
diff
changeset
|
241 return l1[0]*l2[1]-l1[1]*l2[0] |
|
6fb59cfb201e
first version of segmentIntersection
Nicolas Saunier <nico@confins.net>
parents:
19
diff
changeset
|
242 |
|
29
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
243 def filterMovingWindow(input, halfWidth): |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
244 '''Returns an array obtained after the smoothing of the input by a moving average |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
245 The first and last points are copied from the original.''' |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
246 width = float(halfWidth*2+1) |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
247 win = ones(width,'d') |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
248 result = convolve(win/width,array(inputSignal),'same') |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
249 result[:halfWidth] = inputSignal[:halfWidth] |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
250 result[-halfWidth:] = inputSignal[-halfWidth:] |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
251 return result |
|
ca8e716cc231
added moving average filter
Nicolas Saunier <nico@confins.net>
parents:
27
diff
changeset
|
252 |
|
199
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
253 def linearRegression(x, y, deg = 1, plotData = False): |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
254 '''returns the least square estimation of the linear regression of y = ax+b |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
255 as well as the plot''' |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
256 from numpy.lib.polynomial import polyfit |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
257 from matplotlib.pyplot import plot |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
258 from numpy.core.multiarray import arange |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
259 coef = polyfit(x, y, deg) |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
260 if plotData: |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
261 def poly(x): |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
262 result = 0 |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
263 for i in range(len(coef)): |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
264 result += coef[i]*x**(len(coef)-i-1) |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
265 return result |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
266 plot(x, y, 'x') |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
267 xx = arange(min(x), max(x),(max(x)-min(x))/1000) |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
268 plot(xx, [poly(z) for z in xx]) |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
269 return coef |
|
ca9d9104afba
added utility to calibrate polynoms and plot
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
197
diff
changeset
|
270 |
|
27
44689029a86f
updated segmentIntersection and other
Nicolas Saunier <nico@confins.net>
parents:
24
diff
changeset
|
271 ######################### |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
272 # sequence section |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
273 ######################### |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
274 |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
275 class LCSS: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
276 '''Class that keeps the LCSS parameters |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
277 and puts together the various computations''' |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
278 def __init__(self, similarityFunc, delta = float('inf'), aligned = False, lengthFunc = min): |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
279 self.similarityFunc = similarityFunc |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
280 self.aligned = aligned |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
281 self.delta = delta |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
282 self.lengthFunc = lengthFunc |
|
389
6d26dcc7bba0
modifications to compute alignment for None indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
283 self.subSequenceIndices = [(0,0)] |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
284 |
|
373
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
285 def similarities(self, l1, l2, jshift=0): |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
286 from numpy import zeros, int as npint |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
287 n1 = len(l1) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
288 n2 = len(l2) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
289 self.similarityTable = zeros((n1+1,n2+1), dtype = npint) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
290 for i in xrange(1,n1+1): |
|
374
a7af3519687e
finished implementation of aligned LCSS with matching sequence decoded
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
373
diff
changeset
|
291 for j in xrange(max(1,i-jshift-self.delta),min(n2,i-jshift+self.delta)+1): |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
292 if self.similarityFunc(l1[i-1], l2[j-1]): |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
293 self.similarityTable[i,j] = self.similarityTable[i-1,j-1]+1 |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
294 else: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
295 self.similarityTable[i,j] = max(self.similarityTable[i-1,j], self.similarityTable[i,j-1]) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
296 |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
297 def subSequence(self, i, j): |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
298 '''Returns the subsequence of two sequences |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
299 http://en.wikipedia.org/wiki/Longest_common_subsequence_problem''' |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
300 if i == 0 or j == 0: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
301 return [] |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
302 elif self.similarityTable[i][j] == self.similarityTable[i][j-1]: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
303 return self.subSequence(i, j-1) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
304 elif self.similarityTable[i][j] == self.similarityTable[i-1][j]: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
305 return self.subSequence(i-1, j) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
306 else: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
307 return self.subSequence(i-1, j-1) + [(i-1,j-1)] |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
308 |
|
373
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
309 def _compute(self, _l1, _l2, computeSubSequence = False): |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
310 '''returns the longest common subsequence similarity |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
311 based on the threshold on distance between two elements of lists l1, l2 |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
312 similarityFunc returns True or False whether the two points are considered similar |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
313 |
|
373
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
314 if aligned, returns the best matching if using a finite delta by shiftinig the series alignments |
|
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
315 |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
316 eg distance(p1, p2) < epsilon |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
317 ''' |
|
372
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
318 if len(_l2) < len(_l1): # l1 is the shortest |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
319 l1 = _l2 |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
320 l2 = _l1 |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
321 revertIndices = True |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
322 else: |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
323 l1 = _l1 |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
324 l2 = _l2 |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
325 revertIndices = False |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
326 n1 = len(l1) |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
327 n2 = len(l2) |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
328 |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
329 if self.aligned: |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
330 lcssValues = {} |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
331 similarityTables = {} |
|
374
a7af3519687e
finished implementation of aligned LCSS with matching sequence decoded
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
373
diff
changeset
|
332 for i in xrange(-n2-self.delta+1, n1+self.delta): # interval such that [i-shift-delta, i-shift+delta] is never empty, which happens when i-shift+delta < 1 or when i-shift-delta > n2 |
|
373
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
333 self.similarities(l1, l2, i) |
|
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
334 lcssValues[i] = self.similarityTable.max() |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
335 similarityTables[i] = self.similarityTable |
|
374
a7af3519687e
finished implementation of aligned LCSS with matching sequence decoded
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
373
diff
changeset
|
336 #print self.similarityTable |
|
389
6d26dcc7bba0
modifications to compute alignment for None indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
337 alignmentShift = argMaxDict(lcssValues) # ideally get the medium alignment shift, the one that minimizes distance |
|
6d26dcc7bba0
modifications to compute alignment for None indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
338 self.similarityTable = similarityTables[alignmentShift] |
|
372
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
339 else: |
|
389
6d26dcc7bba0
modifications to compute alignment for None indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
340 alignmentShift = 0 |
|
372
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
341 self.similarities(l1, l2) |
|
373
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
342 |
|
374
a7af3519687e
finished implementation of aligned LCSS with matching sequence decoded
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
373
diff
changeset
|
343 # threshold values for the useful part of the similarity table are n2-n1-delta and n1-n2-delta |
|
389
6d26dcc7bba0
modifications to compute alignment for None indicators
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
381
diff
changeset
|
344 self.similarityTable = self.similarityTable[:min(n1, n2+alignmentShift+self.delta)+1, :min(n2, n1-alignmentShift+self.delta)+1] |
|
373
d0b86ed50f32
work in progress on LCSS
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
372
diff
changeset
|
345 |
|
372
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
346 if computeSubSequence: |
|
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
347 self.subSequenceIndices = self.subSequence(self.similarityTable.shape[0]-1, self.similarityTable.shape[1]-1) |
|
371
924e38c9f70e
work in progress
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
370
diff
changeset
|
348 if revertIndices: |
|
374
a7af3519687e
finished implementation of aligned LCSS with matching sequence decoded
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
373
diff
changeset
|
349 self.subSequenceIndices = [(j,i) for i,j in self.subSequenceIndices] |
|
372
349eb1e09f45
Cleaned the methods/functions indicating if a point is in a polygon
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
371
diff
changeset
|
350 return self.similarityTable[-1,-1] |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
351 |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
352 def compute(self, l1, l2, computeSubSequence = False): |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
353 '''get methods are to be shadowed in child classes ''' |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
354 return self._compute(l1, l2, computeSubSequence) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
355 |
|
375
2ea8584aa80a
making indicator LCSS work
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
374
diff
changeset
|
356 def computeAlignment(self): |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
357 from numpy import mean |
|
374
a7af3519687e
finished implementation of aligned LCSS with matching sequence decoded
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
373
diff
changeset
|
358 return mean([j-i for i,j in self.subSequenceIndices]) |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
359 |
|
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
360 def _computeNormalized(self, l1, l2, computeSubSequence = False): |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
361 ''' compute the normalized LCSS |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
362 ie, the LCSS divided by the min or mean of the indicator lengths (using lengthFunc) |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
363 lengthFunc = lambda x,y:float(x,y)/2''' |
|
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
364 return float(self._compute(l1, l2, computeSubSequence))/self.lengthFunc(len(l1), len(l2)) |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
365 |
|
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
366 def computeNormalized(self, l1, l2, computeSubSequence = False): |
|
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
367 return self._computeNormalized(l1, l2, computeSubSequence) |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
368 |
|
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
369 def _computeDistance(self, l1, l2, computeSubSequence = False): |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
370 ''' compute the LCSS distance''' |
|
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
371 return 1-self._computeNormalized(l1, l2, computeSubSequence) |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
372 |
|
376
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
373 def computeDistance(self, l1, l2, computeSubSequence = False): |
|
2e6b8610bcaa
work on indicator similarity
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
375
diff
changeset
|
374 return self._computeDistance(l1, l2, computeSubSequence) |
|
370
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
375 |
|
97e8fa0ee9a1
work in progress for complete alignment
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
369
diff
changeset
|
376 ######################### |
|
45
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
377 # plotting section |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
378 ######################### |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
379 |
|
332
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
380 def plotPolygon(poly, options = ''): |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
381 'Plots shapely polygon poly' |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
382 from numpy.core.multiarray import array |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
383 from matplotlib.pyplot import plot |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
384 from shapely.geometry import Polygon |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
385 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
386 tmp = array(poly.exterior) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
387 plot(tmp[:,0], tmp[:,1], options) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
388 |
| 324 | 389 def stepPlot(X, firstX, lastX, initialCount = 0, increment = 1): |
| 390 '''for each value in X, increment by increment the initial count | |
|
297
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
391 returns the lists that can be plotted |
| 324 | 392 to obtain a step plot increasing by one for each value in x, from first to last value |
| 393 firstX and lastX should be respectively smaller and larger than all elements in X''' | |
|
297
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
394 |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
395 sortedX = [] |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
396 counts = [initialCount] |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
397 for x in sorted(X): |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
398 sortedX += [x,x] |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
399 counts.append(counts[-1]) |
| 324 | 400 counts.append(counts[-1]+increment) |
|
297
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
401 counts.append(counts[-1]) |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
402 return [firstX]+sortedX+[lastX], counts |
|
f6f423e25c7f
adding function to generate step plots (for cumulative number of vehicles)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
286
diff
changeset
|
403 |
|
45
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
404 class PlottingPropertyValues: |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
405 def __init__(self, values): |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
406 self.values = values |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
407 |
|
116
2bf5b76320c0
moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
115
diff
changeset
|
408 def __getitem__(self, i): |
|
45
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
409 return self.values[i%len(self.values)] |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
410 |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
411 markers = PlottingPropertyValues(['+', '*', ',', '.', 'x', 'D', 's', 'o']) |
|
116
2bf5b76320c0
moved intersection plotting and added markers for scatter plots
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
115
diff
changeset
|
412 scatterMarkers = PlottingPropertyValues(['s','o','^','>','v','<','d','p','h','8','+','x']) |
|
45
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
413 |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
414 linestyles = PlottingPropertyValues(['-', '--', '-.', ':']) |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
415 |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
416 colors = PlottingPropertyValues('brgmyck') # 'w' |
|
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
417 |
|
115
550556378466
added functionalities to indicator maps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
86
diff
changeset
|
418 def plotIndicatorMap(indicatorMap, squareSize, masked = True, defaultValue=-1): |
|
65
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
419 from numpy import array, arange, ones, ma |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
420 from matplotlib.pyplot import pcolor |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
421 coords = array(indicatorMap.keys()) |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
422 minX = min(coords[:,0]) |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
423 minY = min(coords[:,1]) |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
424 X = arange(minX, max(coords[:,0])+1.1)*squareSize |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
425 Y = arange(minY, max(coords[:,1])+1.1)*squareSize |
|
115
550556378466
added functionalities to indicator maps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
86
diff
changeset
|
426 C = defaultValue*ones((len(Y), len(X))) |
|
65
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
427 for k,v in indicatorMap.iteritems(): |
|
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
428 C[k[1]-minY,k[0]-minX] = v |
|
115
550556378466
added functionalities to indicator maps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
86
diff
changeset
|
429 if masked: |
|
550556378466
added functionalities to indicator maps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
86
diff
changeset
|
430 pcolor(X, Y, ma.masked_where(C==defaultValue,C)) |
|
550556378466
added functionalities to indicator maps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
86
diff
changeset
|
431 else: |
|
550556378466
added functionalities to indicator maps
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
86
diff
changeset
|
432 pcolor(X, Y, C) |
|
65
75cf537b8d88
moved and generalized map making functions to the library
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
48
diff
changeset
|
433 |
|
45
74d2de078baf
added colors, linestyles and markers to pick from
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
42
diff
changeset
|
434 ######################### |
|
27
44689029a86f
updated segmentIntersection and other
Nicolas Saunier <nico@confins.net>
parents:
24
diff
changeset
|
435 # file I/O section |
|
44689029a86f
updated segmentIntersection and other
Nicolas Saunier <nico@confins.net>
parents:
24
diff
changeset
|
436 ######################### |
|
24
6fb59cfb201e
first version of segmentIntersection
Nicolas Saunier <nico@confins.net>
parents:
19
diff
changeset
|
437 |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
438 def openCheck(filename, option = 'r', quit = False): |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
439 '''Open file filename in read mode by default |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
440 and checks it is open''' |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
441 try: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
442 return open(filename, option) |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
443 except IOError: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
444 print 'File %s could not be opened.' % filename |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
445 if quit: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
446 from sys import exit |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
447 exit() |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
448 return None |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
449 |
|
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
450 def readline(f, commentCharacter = commentChar): |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
451 '''Modified readline function to skip comments.''' |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
452 s = f.readline() |
|
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
453 while (len(s) > 0) and s.startswith(commentCharacter): |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
454 s = f.readline() |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
455 return s.strip() |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
456 |
|
48
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
457 def getLines(f, delimiterCharacter = delimiterChar): |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
458 '''Gets a complete entry (all the lines) in between delimiterChar.''' |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
459 dataStrings = [] |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
460 s = readline(f) |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
461 while (len(s) > 0) and (not s.startswith(delimiterCharacter)): |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
462 dataStrings += [s.strip()] |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
463 s = readline(f) |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
464 return dataStrings |
|
8aed225f71d8
rearranged code for function readline and getlines (and characters), moved invertHomography to cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
46
diff
changeset
|
465 |
|
304
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
466 class FakeSecHead(object): |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
467 '''Add fake section header [asection] |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
468 |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
469 from http://stackoverflow.com/questions/2819696/parsing-properties-file-in-python/2819788#2819788 |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
470 use read_file in Python 3.2+ |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
471 ''' |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
472 def __init__(self, fp): |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
473 self.fp = fp |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
474 self.sechead = '[main]\n' |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
475 |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
476 def readline(self): |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
477 if self.sechead: |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
478 try: return self.sechead |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
479 finally: self.sechead = None |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
480 else: return self.fp.readline() |
|
20f9cd972dde
added capability to parse cfg file for display-trajectories.py
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
301
diff
changeset
|
481 |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
482 def removeExtension(filename, delimiter = '.'): |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
483 '''Returns the filename minus the extension (all characters after last .)''' |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
484 i = filename.rfind(delimiter) |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
485 if i>0: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
486 return filename[:i] |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
487 else: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
488 return filename |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
489 |
|
46
b5d007612e16
added filename util
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
45
diff
changeset
|
490 def cleanFilename(s): |
|
b5d007612e16
added filename util
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
45
diff
changeset
|
491 'cleans filenames obtained when contatenating figure characteristics' |
|
266
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
492 return s.replace(' ','-').replace('.','').replace('/','-') |
|
46
b5d007612e16
added filename util
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
45
diff
changeset
|
493 |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
494 def listfiles(dirname, extension, remove = False): |
|
14
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
495 '''Returns the list of files with the extension in the directory dirname |
|
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
496 If remove is True, the filenames are stripped from the extension''' |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
497 from os import listdir |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
498 tmp = [f for f in listdir(dirname) if f.endswith(extension)] |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
499 tmp.sort() |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
500 if remove: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
501 return [removeExtension(f, extension) for f in tmp] |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
502 else: |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
503 return tmp |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
504 |
|
266
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
505 def mkdir(dirname): |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
506 'Creates a directory if it does not exist' |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
507 import os |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
508 if not os.path.exists(dirname): |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
509 os.mkdir(dirname) |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
510 else: |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
511 print(dirname+' already exists') |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
512 |
|
14
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
513 def removeFile(filename): |
|
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
514 '''Deletes the file while avoiding raising an error |
|
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
515 if the file does not exist''' |
|
266
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
516 import os |
|
14
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
517 if (os.path.exists(filename)): |
|
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
518 os.remove(filename) |
|
266
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
519 else: |
|
aba9711b3149
small modificatons and reorganization
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
262
diff
changeset
|
520 print(filename+' does not exist') |
|
14
e7bbe8465591
homography and other utils
Nicolas Saunier <nico@confins.net>
parents:
7
diff
changeset
|
521 |
|
42
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
522 def line2Floats(l, separator=' '): |
|
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
523 '''Returns the list of floats corresponding to the string''' |
|
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
524 return [float(x) for x in l.split(separator)] |
|
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
525 |
|
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
526 def line2Ints(l, separator=' '): |
|
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
527 '''Returns the list of ints corresponding to the string''' |
|
1a2ac2d4f53a
added loading of the rest of the data for objects
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
35
diff
changeset
|
528 return [int(x) for x in l.split(separator)] |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
529 |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
530 ######################### |
|
332
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
531 # CLI utils |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
532 ######################### |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
533 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
534 def parseCLIOptions(helpMessage, options, cliArgs, optionalOptions=[]): |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
535 ''' Simple function to handle similar argument parsing |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
536 Returns the dictionary of options and their values |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
537 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
538 * cliArgs are most likely directly sys.argv |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
539 (only the elements after the first one are considered) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
540 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
541 * options should be a list of strings for getopt options, |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
542 eg ['frame=','correspondences=','video='] |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
543 A value must be provided for each option, or the program quits''' |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
544 import sys, getopt |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
545 from numpy.core.fromnumeric import all |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
546 optionValues, args = getopt.getopt(cliArgs[1:], 'h', ['help']+options+optionalOptions) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
547 optionValues = dict(optionValues) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
548 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
549 if '--help' in optionValues.keys() or '-h' in optionValues.keys(): |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
550 print(helpMessage+ |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
551 '\n - Compulsory options: '+' '.join([opt.replace('=','') for opt in options])+ |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
552 '\n - Non-compulsory options: '+' '.join([opt.replace('=','') for opt in optionalOptions])) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
553 sys.exit() |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
554 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
555 missingArgument = [('--'+opt.replace('=','') in optionValues.keys()) for opt in options] |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
556 if not all(missingArgument): |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
557 print('Missing argument') |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
558 print(optionValues) |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
559 sys.exit() |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
560 |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
561 return optionValues |
|
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
562 |
|
397
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
563 |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
564 ######################### |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
565 # Utils to read .ini type text files for configuration, meta data... |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
566 ######################### |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
567 |
|
333
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
568 class TrackingParameters: |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
569 def loadConfigFile(self, filename): |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
570 from ConfigParser import ConfigParser |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
571 from numpy import loadtxt |
|
364
a50a69e04c2a
script modification so that command line arguments take precedence over config file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
352
diff
changeset
|
572 from os import path |
|
a50a69e04c2a
script modification so that command line arguments take precedence over config file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
352
diff
changeset
|
573 |
|
333
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
574 config = ConfigParser() |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
575 config.readfp(FakeSecHead(openCheck(filename))) |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
576 self.sectionHeader = config.sections()[0] |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
577 self.videoFilename = config.get(self.sectionHeader, 'video-filename') |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
578 self.databaseFilename = config.get(self.sectionHeader, 'database-filename') |
|
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
579 self.homographyFilename = config.get(self.sectionHeader, 'homography-filename') |
|
364
a50a69e04c2a
script modification so that command line arguments take precedence over config file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
352
diff
changeset
|
580 if (path.exists(self.homographyFilename)): |
|
a50a69e04c2a
script modification so that command line arguments take precedence over config file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
352
diff
changeset
|
581 self.homography = loadtxt(self.homographyFilename) |
|
a50a69e04c2a
script modification so that command line arguments take precedence over config file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
352
diff
changeset
|
582 else: |
|
a50a69e04c2a
script modification so that command line arguments take precedence over config file
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
352
diff
changeset
|
583 self.homography = None |
|
333
c9201f6b143a
moved the config parser to utils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
332
diff
changeset
|
584 self.firstFrameNum = config.getint(self.sectionHeader, 'frame1') |
|
348
c64a4f889b97
added safety analysis options to feature tracking (with default values for backward compatibility)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
344
diff
changeset
|
585 self.videoFrameRate = config.getfloat(self.sectionHeader, 'video-fps') |
|
332
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
586 |
|
350
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
587 self.maxPredictedSpeed = config.getfloat(self.sectionHeader, 'max-predicted-speed')/3.6/self.videoFrameRate |
|
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
588 self.predictionTimeHorizon = config.getfloat(self.sectionHeader, 'prediction-time-horizon')*self.videoFrameRate |
|
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
589 self.collisionDistance = config.getfloat(self.sectionHeader, 'collision-distance') |
|
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
590 self.crossingZones = config.getboolean(self.sectionHeader, 'crossing-zones') |
|
352
72aa44072093
safety analysis script with option for prediction method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
351
diff
changeset
|
591 self.predictionMethod = config.get(self.sectionHeader, 'prediction-method') |
|
350
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
592 self.nPredictedTrajectories = config.getint(self.sectionHeader, 'npredicted-trajectories') |
|
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
593 self.minAcceleration = config.getfloat(self.sectionHeader, 'min-acceleration')/self.videoFrameRate**2 |
|
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
594 self.maxAcceleration = config.getfloat(self.sectionHeader, 'max-acceleration')/self.videoFrameRate**2 |
|
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
595 self.maxSteering = config.getfloat(self.sectionHeader, 'max-steering')/self.videoFrameRate |
|
351
891858351bcb
added new parameter (use features) for safety analysis
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
350
diff
changeset
|
596 self.useFeaturesForPrediction = config.getboolean(self.sectionHeader, 'use-features-prediction') |
|
350
7e9ad2d9d79c
added new parameters in safety analysis script
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
348
diff
changeset
|
597 |
|
397
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
598 class SceneParameters: |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
599 def __init__(self, config, sectionName): |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
600 from ConfigParser import NoOptionError |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
601 from ast import literal_eval |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
602 try: |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
603 self.sitename = config.get(sectionName, 'sitename') |
|
405
a9e275b4ecb7
modification of the scene.cfg format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
604 self.databaseFilename = config.get(sectionName, 'data-filename') |
|
a9e275b4ecb7
modification of the scene.cfg format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
605 self.homographyFilename = config.get(sectionName, 'homography-filename') |
|
a9e275b4ecb7
modification of the scene.cfg format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
606 self.calibrationFilename = config.get(sectionName, 'calibration-filename') |
|
a9e275b4ecb7
modification of the scene.cfg format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
607 self.videoFilename = config.get(sectionName, 'video-filename') |
|
a9e275b4ecb7
modification of the scene.cfg format
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
398
diff
changeset
|
608 self.frameRate = config.getfloat(sectionName, 'framerate') |
|
421
4fce27946c60
first example of video metadata using sqlalchemy
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
405
diff
changeset
|
609 self.date = datetime.strptime(config.get(sectionName, 'date'), datetimeFormat) # 2011-06-22 11:00:39 |
|
397
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
610 self.translation = literal_eval(config.get(sectionName, 'translation')) # = [0.0, 0.0] |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
611 self.rotation = config.getfloat(sectionName, 'rotation') |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
612 self.duration = config.getint(sectionName, 'duration') |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
613 except NoOptionError as e: |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
614 print(e) |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
615 print('Not a section for scene meta-data') |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
616 |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
617 @staticmethod |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
618 def loadConfigFile(filename): |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
619 from ConfigParser import ConfigParser |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
620 config = ConfigParser() |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
621 config.readfp(openCheck(filename)) |
|
398
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
397
diff
changeset
|
622 configDict = dict() |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
397
diff
changeset
|
623 for sectionName in config.sections(): |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
397
diff
changeset
|
624 configDict[sectionName] = SceneParameters(config, sectionName) |
|
3399bd48cb40
Ajout d'une méthode pour obtenir le nombre de FPS
Jean-Philippe Jodoin <jpjodoin@gmail.com>
parents:
397
diff
changeset
|
625 return configDict |
|
397
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
626 |
|
b36b00dd27c3
added function to read scene metadata
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
395
diff
changeset
|
627 |
|
332
a6ca86107f27
reorganized utils module
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
324
diff
changeset
|
628 ######################### |
|
233
ab1a11176d7b
initial sqlite code and bug corrected in cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
199
diff
changeset
|
629 # sqlite |
|
ab1a11176d7b
initial sqlite code and bug corrected in cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
199
diff
changeset
|
630 ######################### |
|
ab1a11176d7b
initial sqlite code and bug corrected in cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
199
diff
changeset
|
631 |
|
344
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
632 def printDBError(error): |
|
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
633 print('DB Error: {}'.format(error)) |
|
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
634 |
|
235
584613399513
added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
233
diff
changeset
|
635 def dropTables(connection, tableNames): |
|
584613399513
added script and functions to remove object tables
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
233
diff
changeset
|
636 'deletes the table with names in tableNames' |
|
352
72aa44072093
safety analysis script with option for prediction method
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
351
diff
changeset
|
637 import sqlite3 |
|
344
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
638 try: |
|
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
639 cursor = connection.cursor() |
|
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
640 for tableName in tableNames: |
|
391
03dbecd3a887
modified feature grouping to return vectors of pointers to feature trajectories
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
389
diff
changeset
|
641 cursor.execute('DROP TABLE IF EXISTS '+tableName) |
|
344
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
642 except sqlite3.OperationalError as error: |
|
14a2405f54f8
slight modification to safety analysis and generalized script to delete computed data (objects and interactions)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
333
diff
changeset
|
643 printDBError(error) |
|
233
ab1a11176d7b
initial sqlite code and bug corrected in cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
199
diff
changeset
|
644 |
|
ab1a11176d7b
initial sqlite code and bug corrected in cvutils
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
199
diff
changeset
|
645 ######################### |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
646 # running tests |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
647 ######################### |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
648 |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
649 if __name__ == "__main__": |
|
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
650 import doctest |
|
2
de5642925615
started implementation of TimeInterval and Spatio-temporal object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
0
diff
changeset
|
651 import unittest |
|
31
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
652 suite = doctest.DocFileSuite('tests/utils.txt') |
|
c000f37c316d
moved tests to independent file, added chi2 computation
Nicolas Saunier <nico@confins.net>
parents:
29
diff
changeset
|
653 #suite = doctest.DocTestSuite() |
|
2
de5642925615
started implementation of TimeInterval and Spatio-temporal object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
0
diff
changeset
|
654 unittest.TextTestRunner().run(suite) |
|
de5642925615
started implementation of TimeInterval and Spatio-temporal object
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
0
diff
changeset
|
655 #doctest.testmod() |
|
0
aed8eb63cdde
initial commit with non-functional python code for NGSIM
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
656 #doctest.testfile("example.txt") |
