__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import math
  2. from math import ceil
  3. import scipy.stats
  4. from db_setup.create_triggers import create_triggers
  5. from db_setup.indices import create_indices
  6. from db_setup.seeds import seed
  7. from db_setup.tables import tables
  8. from game import DB_NAME
  9. class StDevFunc:
  10. """
  11. source: https://stackoverflow.com/a/24423341
  12. """
  13. def __init__(self):
  14. self.M = 0.0
  15. self.S = 0.0
  16. self.k = 1
  17. def step(self, value):
  18. if value is None:
  19. return
  20. value = float(value)
  21. tM = self.M
  22. self.M += (value - tM) / self.k
  23. self.S += (value - tM) * (value - self.M)
  24. self.k += 1
  25. def finalize(self):
  26. if self.k == 1:
  27. return None
  28. if self.k == 2:
  29. return 0.0
  30. return math.sqrt(self.S / (self.k - 1))
  31. def __call__(self, items):
  32. for i in items:
  33. self.step(i)
  34. return self.finalize()
  35. class MeanFunc:
  36. """
  37. source: https://stackoverflow.com/a/24423341
  38. """
  39. def __init__(self):
  40. self.sum = 0
  41. self.count = 0
  42. def step(self, value):
  43. if value is None:
  44. return
  45. value = float(value)
  46. self.sum += value
  47. self.count += 1
  48. def finalize(self):
  49. return self.sum / self.count
  50. def __call__(self, items):
  51. for i in items:
  52. self.step(i)
  53. return self.finalize()
  54. class MeanConfidenceIntervalSizeFunc:
  55. def __init__(self):
  56. self.std = StDevFunc()
  57. self.mean = MeanFunc()
  58. self.count = 0
  59. def step(self, value):
  60. self.std.step(value)
  61. self.mean.step(value)
  62. self.count += 1
  63. def finalize(self):
  64. if self.count == 0:
  65. return None # same as nan for sqlite3
  66. if self.count == 1:
  67. return math.inf
  68. std = self.std.finalize()
  69. if std == 0:
  70. return 0
  71. return self.mean.finalize() - scipy.stats.t.interval(0.95, self.count - 1,
  72. loc=self.mean.finalize(),
  73. scale=std / math.sqrt(self.count))[0]
  74. def __call__(self, items):
  75. for i in items:
  76. self.step(i)
  77. return self.finalize()
  78. def str_to_float(s):
  79. s = s.replace('.','').replace(',','.')
  80. assert not '+' in s or not '-' in s
  81. v = float(s.replace('%', ''))
  82. if '%' in s:
  83. v /= 100
  84. return v
  85. def create_functions(connection):
  86. connection.create_function('CEIL', 1, ceil)
  87. connection.create_function('POWER', 2, lambda b, e: b ** e)
  88. connection.create_aggregate('CONF', 1, MeanConfidenceIntervalSizeFunc)
  89. connection.create_aggregate('TOFLOAT', 1, str_to_float)
  90. def set_pragmas(cursor):
  91. cursor.execute('PRAGMA foreign_keys=1')
  92. cursor.execute('PRAGMA journal_mode = WAL')
  93. cursor.execute('PRAGMA synchronous = NORMAL')
  94. def setup(cursor):
  95. print('Database setup...')
  96. tables(cursor)
  97. create_triggers(cursor)
  98. create_indices(cursor)
  99. seed(cursor)
  100. if __name__ == '__main__':
  101. import model
  102. model.connect(DB_NAME)
  103. setup(model.current_cursor)
  104. model.current_connection.commit()