Skip to content
Snippets Groups Projects
Commit f7d0f0d8 authored by Tobias Schmidt's avatar Tobias Schmidt
Browse files

corrected some errors in stutil.py and added unit test

parent fb0c2084
Branches
Tags
No related merge requests found
......@@ -45,7 +45,7 @@ def Mean(xs):
"""
if len(xs)==0:
raise RuntimeError("Can't calculate mean of empty sequence")
return sum(xs)/len(xs)
return float(sum(xs))/len(xs)
@FloatValueExtract
def Median(xs):
......@@ -86,7 +86,7 @@ def Correl(xs, ys):
sum[(xi-<x>)*(yi-<y>)]
r=----------------------
(n-1)*sx*sy
sx*sy
where <x>, <y> are the mean of dataset xs and ys, and, sx and sy are the
standard deviations.
......@@ -103,9 +103,9 @@ def Correl(xs, ys):
cross_term+=(x-mean_x)*(y-mean_y)
sigma_x+=(x-mean_x)**2
sigma_y+=(y-mean_y)**2
sigma_x=math.sqrt(sigma_x/len(xs))
sigma_y=math.sqrt(sigma_y/len(ys))
return cross_term/((len(xs)-1)*sigma_x*sigma_y)
sigma_x=math.sqrt(sigma_x)
sigma_y=math.sqrt(sigma_y)
return cross_term/(sigma_x*sigma_y)
def Histogram(xs, bounds, num_bins):
bins=[0 for i in range(num_bins)]
......
......@@ -2,6 +2,7 @@ set(OST_BASE_UNIT_TESTS
test_generic_property.cc
test_string_ref.cc
test_pod_vector.cc
test_stutil.py
tests.cc
)
......
import unittest
from ost import stutil
class TestStUtils(unittest.TestCase):
def setUp(self):
self.data1 = [0,1,2,3,4,5,6,-5,-4,-3,-2,-1,1]
self.data2 = [1,2,3,4,5,6,7,8,9,10,11,12,13]
self.data3 = [0.1,0.5,0.7,2.4,0.5,4.1,0.9,-1.1,-0.5,0.7,-1.4,-7.5,8.5]
self.mean = 0.5384615385
self.mean2 = 0.6076923077
self.median = 1
self.median2 = 0.5
self.stddev = 3.3192998478
self.stddev2 = 3.4192805223
self.minimum = -5
self.minimum2 = -7.5
self.maximum = 6
self.maximum2 = 8.5
self.correl = -0.39639313
self.correl2 = -0.0619291504
def isSimilar(self, value, reference, accuracy):
if abs(value - reference) <= accuracy:
return True
return False
def testMean(self):
assert self.isSimilar(stutil.Mean(self.data1), self.mean, 0.001), \
"Mean (%f) does not correspond to precalculated mean (%f)" % \
(stutil.Mean(self.data1), self.mean)
assert self.isSimilar(stutil.Mean(self.data3), self.mean2, 0.001), \
"Mean (%f) does not correspond to precalculated mean (%f)" % \
(stutil.Mean(self.data3), self.mean2)
def testMedian(self):
assert self.isSimilar(stutil.Median(self.data1), self.median, 0.001), \
"Median (%f) does not correspond to precalculated median (%f)" % \
(stutil.Median(self.data1), self.median)
assert self.isSimilar(stutil.Median(self.data3), self.median2, 0.001), \
"Median (%f) does not correspond to precalculated median (%f)" % \
(stutil.Median(self.data3), self.median2)
def testStddev(self):
assert self.isSimilar(stutil.StdDev(self.data1), self.stddev, 0.001), \
"StdDev (%f) does not correspond to precalculated StdDev (%f)" % \
(stutil.StdDev(self.data1), self.stddev)
assert self.isSimilar(stutil.StdDev(self.data3), self.stddev2, 0.001), \
"StdDev (%f) does not correspond to precalculated StdDev (%f)" % \
(stutil.StdDev(self.data3), self.stddev2)
def testMinimum(self):
assert self.isSimilar(stutil.Min(self.data1), self.minimum, 0.001), \
"Minimum (%f) does not correspond to precalculated minimum (%f)" % \
(stutil.Min(self.data1), self.minimum)
assert self.isSimilar(stutil.Min(self.data3), self.minimum2, 0.001), \
"Minimum (%f) does not correspond to precalculated minimum (%f)" % \
(stutil.Min(self.data3), self.minimum2)
def testMaximum(self):
assert self.isSimilar(stutil.Max(self.data1), self.maximum, 0.001), \
"Maximum (%f) does not correspond to precalculated maximum (%f)" % \
(stutil.Max(self.data1), self.maximum)
assert self.isSimilar(stutil.Max(self.data3), self.maximum2, 0.001), \
"Maximum (%f) does not correspond to precalculated maximum (%f)" % \
(stutil.Max(self.data3), self.maximum2)
def testCorrel(self):
assert self.isSimilar(stutil.Correl(self.data1, self.data2), self.correl, 0.001), \
"Correl (%f) does not correspond to precalculated correl (%f)" % \
(stutil.Correl(self.data1, self.data2), self.correl)
assert self.isSimilar(stutil.Correl(self.data3, self.data2), self.correl2, 0.001), \
"Correl (%f) does not correspond to precalculated correl (%f)" % \
(stutil.Correl(self.data3, self.data2), self.correl2)
if __name__ == "__main__":
try:
unittest.main()
except Exception, e:
print e
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment