Mercurial Hosting > traffic-intelligence
comparison python/utils.py @ 31:c000f37c316d
moved tests to independent file, added chi2 computation
author | Nicolas Saunier <nico@confins.net> |
---|---|
date | Sat, 13 Feb 2010 19:46:33 -0500 |
parents | ca8e716cc231 |
children | 48e56179c39e |
comparison
equal
deleted
inserted
replaced
30:418b41056e6c | 31:c000f37c316d |
---|---|
2 ''' Generic utilities.''' | 2 ''' Generic utilities.''' |
3 | 3 |
4 #from numpy import * | 4 #from numpy import * |
5 #from pylab import * | 5 #from pylab import * |
6 | 6 |
7 from moving import Point,Interval | 7 import moving |
8 | 8 |
9 __metaclass__ = type | 9 __metaclass__ = type |
10 | 10 |
11 commentChar = '#'; | 11 commentChar = '#'; |
12 | 12 |
13 ######################### | 13 ######################### |
14 # simple statistics | |
15 ######################### | |
16 | |
17 def computeChi2(expected, observed): | |
18 '''Returns the Chi2 statistics''' | |
19 result = 0. | |
20 for e, o in zip(expected, observed): | |
21 result += ((e-o)*(e-o))/e | |
22 return result | |
23 | |
24 class histogram: | |
25 '''Class to represent a sample of a distribution for a continuous random variable | |
26 with the number of observations for each interval''' | |
27 | |
28 | |
29 | |
30 ######################### | |
14 # maths section | 31 # maths section |
15 ######################### | 32 ######################### |
16 | 33 |
17 def segmentIntersection(p1, p2, p3, p4): | 34 def segmentIntersection(p1, p2, p3, p4): |
18 '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise | 35 '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' |
19 | |
20 >>> segmentIntersection(Point(0,0),Point(1,1), Point(0,1), Point(1,2)) | |
21 >>> segmentIntersection(Point(0,1),Point(1,0), Point(0,2), Point(2,1)) | |
22 >>> segmentIntersection(Point(0,0),Point(2,0), Point(1,-1),Point(1,1)) | |
23 (1.000000,0.000000) | |
24 >>> segmentIntersection(Point(0,1),Point(2,0),Point(1,1),Point(1,2)) | |
25 ''' | |
26 from numpy import matrix | 36 from numpy import matrix |
27 from numpy.linalg import linalg, det | 37 from numpy.linalg import linalg, det |
28 | 38 |
29 dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] | 39 dp1 = p2-p1#[s1[0][1]-s1[0][0], s1[1][1]-s1[1][0]] |
30 dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] | 40 dp2 = p4-p3#[s2[0][1]-s2[0][0], s2[1][1]-s2[1][0]] |
36 | 46 |
37 if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: | 47 if linalg.det(A) == 0:#crossProduct(ds1, ds2) == 0: |
38 return None | 48 return None |
39 else: | 49 else: |
40 intersection = linalg.solve(A,B) | 50 intersection = linalg.solve(A,B) |
41 if (Interval(p1.x, p2.x, True).contains(intersection[0,0]) | 51 if (moving.Interval(p1.x, p2.x, True).contains(intersection[0,0]) |
42 and Interval(p3.x, p4.x, True).contains(intersection[0,0]) | 52 and moving.Interval(p3.x, p4.x, True).contains(intersection[0,0]) |
43 and Interval(p1.y, p2.y, True).contains(intersection[1,0]) | 53 and moving.Interval(p1.y, p2.y, True).contains(intersection[1,0]) |
44 and Interval(p3.y, p4.y, True).contains(intersection[1,0])): | 54 and moving.Interval(p3.y, p4.y, True).contains(intersection[1,0])): |
45 return Point(intersection[0,0], intersection[1,0]) | 55 return moving.Point(intersection[0,0], intersection[1,0]) |
46 else: | 56 else: |
47 return None | 57 return None |
48 | 58 |
49 def crossProduct(l1, l2): | 59 def crossProduct(l1, l2): |
50 return l1[0]*l2[1]-l1[1]*l2[0] | 60 return l1[0]*l2[1]-l1[1]*l2[0] |
63 # file I/O section | 73 # file I/O section |
64 ######################### | 74 ######################### |
65 | 75 |
66 def openCheck(filename, option = 'r', quit = False): | 76 def openCheck(filename, option = 'r', quit = False): |
67 '''Open file filename in read mode by default | 77 '''Open file filename in read mode by default |
68 and checks it is open | 78 and checks it is open''' |
69 | |
70 >>> f = openCheck('non_existant_file.txt') | |
71 File non_existant_file.txt could not be opened. | |
72 ''' | |
73 try: | 79 try: |
74 return open(filename, option) | 80 return open(filename, option) |
75 except IOError: | 81 except IOError: |
76 print 'File %s could not be opened.' % filename | 82 print 'File %s could not be opened.' % filename |
77 if quit: | 83 if quit: |
85 while (len(s) > 0) and s.startswith(commentChar): | 91 while (len(s) > 0) and s.startswith(commentChar): |
86 s = f.readline() | 92 s = f.readline() |
87 return s.strip() | 93 return s.strip() |
88 | 94 |
89 def removeExtension(filename, delimiter = '.'): | 95 def removeExtension(filename, delimiter = '.'): |
90 '''Returns the filename minus the extension (all characters after last .) | 96 '''Returns the filename minus the extension (all characters after last .)''' |
91 >>> removeExtension('test-adfasdf.asdfa.txt') | |
92 'test-adfasdf.asdfa' | |
93 >>> removeExtension('test-adfasdf') | |
94 'test-adfasdf' | |
95 ''' | |
96 i = filename.rfind(delimiter) | 97 i = filename.rfind(delimiter) |
97 if i>0: | 98 if i>0: |
98 return filename[:i] | 99 return filename[:i] |
99 else: | 100 else: |
100 return filename | 101 return filename |
129 from shapely.geometry import Polygon | 130 from shapely.geometry import Polygon |
130 | 131 |
131 tmp = array(poly.exterior) | 132 tmp = array(poly.exterior) |
132 plot(tmp[:,0], tmp[:,1], options) | 133 plot(tmp[:,0], tmp[:,1], options) |
133 | 134 |
135 | |
136 ######################### | |
137 # running tests | |
138 ######################### | |
139 | |
134 if __name__ == "__main__": | 140 if __name__ == "__main__": |
135 import doctest | 141 import doctest |
136 import unittest | 142 import unittest |
137 #suite = doctest.DocFileSuite('tests/ubc_utils.txt') | 143 suite = doctest.DocFileSuite('tests/utils.txt') |
138 suite = doctest.DocTestSuite() | 144 #suite = doctest.DocTestSuite() |
139 unittest.TextTestRunner().run(suite) | 145 unittest.TextTestRunner().run(suite) |
140 #doctest.testmod() | 146 #doctest.testmod() |
141 #doctest.testfile("example.txt") | 147 #doctest.testfile("example.txt") |