PageRenderTime 60ms CodeModel.GetById 16ms app.highlight 20ms RepoModel.GetById 21ms app.codeStats 0ms

/test_apiary/stattools.py

https://bitbucket.org/lindenlab/apiary/
Python | 134 lines | 85 code | 25 blank | 24 comment | 21 complexity | a23e82b2e64760a08743d457d47411ca MD5 | raw file
  1#
  2# $LicenseInfo:firstyear=2010&license=mit$
  3# 
  4# Copyright (c) 2010, Linden Research, Inc.
  5# 
  6# Permission is hereby granted, free of charge, to any person obtaining a copy
  7# of this software and associated documentation files (the "Software"), to deal
  8# in the Software without restriction, including without limitation the rights
  9# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10# copies of the Software, and to permit persons to whom the Software is
 11# furnished to do so, subject to the following conditions:
 12# 
 13# The above copyright notice and this permission notice shall be included in
 14# all copies or substantial portions of the Software.
 15# 
 16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22# THE SOFTWARE.
 23# $/LicenseInfo$
 24#
 25
 26import unittest
 27
 28from apiary.tools.stattools import StatValue, StatWindow, StatErrorNoSamples
 29
 30
 31class TestValue(unittest.TestCase):
 32    
 33    def buildStat(self):
 34        return StatValue()
 35        
 36    def assertStats(self, s, count=None, min=None, max=None,
 37                            average=None, stddev=None):
 38        if count is not None:
 39            self.assertEqual(s.count(), count)
 40        if min is not None:
 41            self.assertEqual(s.min(), min)
 42        if max is not None:
 43            self.assertEqual(s.max(), max)        
 44        if average is not None:
 45            self.assertEqual(s.average(), average)        
 46        if stddev is not None:
 47            self.assertEqual(s.stddev(), stddev)
 48
 49    def testEmpty(self):
 50        s = self.buildStat()
 51        self.assertEqual(s.count(), 0)
 52        self.assertRaises(StatErrorNoSamples, s.min)
 53        self.assertRaises(StatErrorNoSamples, s.max)        
 54        self.assertRaises(StatErrorNoSamples, s.average)        
 55        self.assertRaises(StatErrorNoSamples, s.stddev)        
 56
 57    def testSingle(self):
 58        s = self.buildStat()
 59        s.sample(42)
 60        self.assertStats(s, count=1, min=42.0, max=42.0,
 61            average=42.0, stddev=0.0)
 62
 63    atomic_weights = [15.9994, 83.798, 28.0855] # Carbon, Krypton, Silicon
 64    
 65    def testSequence(self):
 66        s = self.buildStat()
 67        for x in [2, 4, 4, 4, 5, 5, 7, 9]:
 68            s.sample(x)
 69        self.assertStats(s, count=8, min=2.0, max=9.0,
 70            average=5.0, stddev=2.0)
 71
 72        s = self.buildStat()
 73        for x in self.atomic_weights:
 74            s.sample(x)
 75        self.assertStats(s, count=3, min=15.9994, max=83.798,
 76            average=42.627633333333328, stddev=29.527024592208562)
 77
 78    def testFormat(self):
 79        s = self.buildStat()
 80        for x in [15.9994, 83.798, 28.0855]:
 81            s.sample(x)
 82        
 83        p = s.format("%4d", "%6.2f")
 84        self.assertEqual(p, "n=   3:  16.00,  42.63,  83.80, sd= 29.53")
 85    
 86        p = s.format("%d", "%8.4f")
 87        self.assertEqual(p, "n=3:  15.9994,  42.6276,  83.7980, sd= 29.5270")
 88
 89    def testFormatEmtpy(self):
 90        s = self.buildStat()
 91
 92        p = s.format("%4d", "%6.2f")
 93        self.assertEqual(p, "n=   0:")
 94
 95        p = s.format("%d", "%8.4f")
 96        self.assertEqual(p, "n=0:")
 97
 98
 99class TestWindow(TestValue):
100    def buildStat(self):
101        return StatWindow(10)
102
103    def testSliding(self):
104        s = StatWindow(3)
105        s.sample(15)
106        s.sample(9)
107        s.sample(6)
108        self.assertStats(s, count=3, min=6, max=15, average=10.0)
109        s.sample(12)
110        self.assertStats(s, count=3, min=6, max=12, average=9.0)
111        s.sample(12)
112        self.assertStats(s, count=3, min=6, max=12, average=10.0)
113        s.sample(9)
114        self.assertStats(s, count=3, min=9, max=12, average=11.0)
115    
116    def testResizing(self):
117        s = StatWindow(6)
118        for x in [ 2, 6, 3, 8, 5, 3 ]:
119            s.sample(x)
120        self.assertStats(s, count=6, min=2, max=8, average=4.5)
121        s.setwindow(2) # less than half
122        self.assertStats(s, count=2, min=3, max=5, average=4.0)
123        
124        s = StatWindow(6)
125        for x in [ 2, 6, 3, 8, 5, 3 ]:
126            s.sample(x)
127        self.assertStats(s, count=6, min=2, max=8, average=4.5)
128        s.setwindow(4) # more than half
129        self.assertStats(s, count=4, min=3, max=8, average=4.75)
130        
131        
132          
133if __name__ == '__main__':
134    unittest.main()