Mercurial Hosting > traffic-intelligence
diff 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 |
line wrap: on
line diff
--- a/python/utils.py Sat Feb 13 18:51:22 2010 -0500 +++ b/python/utils.py Sat Feb 13 19:46:33 2010 -0500 @@ -4,25 +4,35 @@ #from numpy import * #from pylab import * -from moving import Point,Interval +import moving __metaclass__ = type commentChar = '#'; ######################### +# simple statistics +######################### + +def computeChi2(expected, observed): + '''Returns the Chi2 statistics''' + result = 0. + for e, o in zip(expected, observed): + result += ((e-o)*(e-o))/e + return result + +class histogram: + '''Class to represent a sample of a distribution for a continuous random variable + with the number of observations for each interval''' + + + +######################### # maths section ######################### def segmentIntersection(p1, p2, p3, p4): - '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise - - >>> segmentIntersection(Point(0,0),Point(1,1), Point(0,1), Point(1,2)) - >>> segmentIntersection(Point(0,1),Point(1,0), Point(0,2), Point(2,1)) - >>> segmentIntersection(Point(0,0),Point(2,0), Point(1,-1),Point(1,1)) - (1.000000,0.000000) - >>> segmentIntersection(Point(0,1),Point(2,0),Point(1,1),Point(1,2)) - ''' + '''Returns the intersecting point of the segments [p1, p2] and [p3, p4], None otherwise''' from numpy import matrix from numpy.linalg import linalg, det @@ -38,11 +48,11 @@ return None else: intersection = linalg.solve(A,B) - if (Interval(p1.x, p2.x, True).contains(intersection[0,0]) - and Interval(p3.x, p4.x, True).contains(intersection[0,0]) - and Interval(p1.y, p2.y, True).contains(intersection[1,0]) - and Interval(p3.y, p4.y, True).contains(intersection[1,0])): - return Point(intersection[0,0], intersection[1,0]) + if (moving.Interval(p1.x, p2.x, True).contains(intersection[0,0]) + and moving.Interval(p3.x, p4.x, True).contains(intersection[0,0]) + and moving.Interval(p1.y, p2.y, True).contains(intersection[1,0]) + and moving.Interval(p3.y, p4.y, True).contains(intersection[1,0])): + return moving.Point(intersection[0,0], intersection[1,0]) else: return None @@ -65,11 +75,7 @@ def openCheck(filename, option = 'r', quit = False): '''Open file filename in read mode by default - and checks it is open - - >>> f = openCheck('non_existant_file.txt') - File non_existant_file.txt could not be opened. - ''' + and checks it is open''' try: return open(filename, option) except IOError: @@ -87,12 +93,7 @@ return s.strip() def removeExtension(filename, delimiter = '.'): - '''Returns the filename minus the extension (all characters after last .) - >>> removeExtension('test-adfasdf.asdfa.txt') - 'test-adfasdf.asdfa' - >>> removeExtension('test-adfasdf') - 'test-adfasdf' - ''' + '''Returns the filename minus the extension (all characters after last .)''' i = filename.rfind(delimiter) if i>0: return filename[:i] @@ -131,11 +132,16 @@ tmp = array(poly.exterior) plot(tmp[:,0], tmp[:,1], options) + +######################### +# running tests +######################### + if __name__ == "__main__": import doctest import unittest - #suite = doctest.DocFileSuite('tests/ubc_utils.txt') - suite = doctest.DocTestSuite() + suite = doctest.DocFileSuite('tests/utils.txt') + #suite = doctest.DocTestSuite() unittest.TextTestRunner().run(suite) #doctest.testmod() #doctest.testfile("example.txt")