PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/test_apiary/stattools.py

https://bitbucket.org/lindenlab/apiary/
Python | 134 lines | 85 code | 25 blank | 24 comment | 11 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. import unittest
  26. from apiary.tools.stattools import StatValue, StatWindow, StatErrorNoSamples
  27. class TestValue(unittest.TestCase):
  28. def buildStat(self):
  29. return StatValue()
  30. def assertStats(self, s, count=None, min=None, max=None,
  31. average=None, stddev=None):
  32. if count is not None:
  33. self.assertEqual(s.count(), count)
  34. if min is not None:
  35. self.assertEqual(s.min(), min)
  36. if max is not None:
  37. self.assertEqual(s.max(), max)
  38. if average is not None:
  39. self.assertEqual(s.average(), average)
  40. if stddev is not None:
  41. self.assertEqual(s.stddev(), stddev)
  42. def testEmpty(self):
  43. s = self.buildStat()
  44. self.assertEqual(s.count(), 0)
  45. self.assertRaises(StatErrorNoSamples, s.min)
  46. self.assertRaises(StatErrorNoSamples, s.max)
  47. self.assertRaises(StatErrorNoSamples, s.average)
  48. self.assertRaises(StatErrorNoSamples, s.stddev)
  49. def testSingle(self):
  50. s = self.buildStat()
  51. s.sample(42)
  52. self.assertStats(s, count=1, min=42.0, max=42.0,
  53. average=42.0, stddev=0.0)
  54. atomic_weights = [15.9994, 83.798, 28.0855] # Carbon, Krypton, Silicon
  55. def testSequence(self):
  56. s = self.buildStat()
  57. for x in [2, 4, 4, 4, 5, 5, 7, 9]:
  58. s.sample(x)
  59. self.assertStats(s, count=8, min=2.0, max=9.0,
  60. average=5.0, stddev=2.0)
  61. s = self.buildStat()
  62. for x in self.atomic_weights:
  63. s.sample(x)
  64. self.assertStats(s, count=3, min=15.9994, max=83.798,
  65. average=42.627633333333328, stddev=29.527024592208562)
  66. def testFormat(self):
  67. s = self.buildStat()
  68. for x in [15.9994, 83.798, 28.0855]:
  69. s.sample(x)
  70. p = s.format("%4d", "%6.2f")
  71. self.assertEqual(p, "n= 3: 16.00, 42.63, 83.80, sd= 29.53")
  72. p = s.format("%d", "%8.4f")
  73. self.assertEqual(p, "n=3: 15.9994, 42.6276, 83.7980, sd= 29.5270")
  74. def testFormatEmtpy(self):
  75. s = self.buildStat()
  76. p = s.format("%4d", "%6.2f")
  77. self.assertEqual(p, "n= 0:")
  78. p = s.format("%d", "%8.4f")
  79. self.assertEqual(p, "n=0:")
  80. class TestWindow(TestValue):
  81. def buildStat(self):
  82. return StatWindow(10)
  83. def testSliding(self):
  84. s = StatWindow(3)
  85. s.sample(15)
  86. s.sample(9)
  87. s.sample(6)
  88. self.assertStats(s, count=3, min=6, max=15, average=10.0)
  89. s.sample(12)
  90. self.assertStats(s, count=3, min=6, max=12, average=9.0)
  91. s.sample(12)
  92. self.assertStats(s, count=3, min=6, max=12, average=10.0)
  93. s.sample(9)
  94. self.assertStats(s, count=3, min=9, max=12, average=11.0)
  95. def testResizing(self):
  96. s = StatWindow(6)
  97. for x in [ 2, 6, 3, 8, 5, 3 ]:
  98. s.sample(x)
  99. self.assertStats(s, count=6, min=2, max=8, average=4.5)
  100. s.setwindow(2) # less than half
  101. self.assertStats(s, count=2, min=3, max=5, average=4.0)
  102. s = StatWindow(6)
  103. for x in [ 2, 6, 3, 8, 5, 3 ]:
  104. s.sample(x)
  105. self.assertStats(s, count=6, min=2, max=8, average=4.5)
  106. s.setwindow(4) # more than half
  107. self.assertStats(s, count=4, min=3, max=8, average=4.75)
  108. if __name__ == '__main__':
  109. unittest.main()