algorithm.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from .DataStream import EMchPosmap
  2. import numpy as np
  3. import logging
  4. logger = logging.getLogger('calcer')
  5. def calc_chratios(data, pos_map, start, end, window, left_len, right_len):
  6. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  7. all = view[:, start:end]
  8. view = np.cumsum(all, axis=1)
  9. succ = view[0, :]
  10. fail = view[1, :]
  11. commit = view[2, :] + 0.0000001
  12. y = succ / commit
  13. y = y.ravel()
  14. if window is not None:
  15. y = np.convolve(y, window, 'same')
  16. y = y[left_len:end - start - right_len]
  17. cur = all[:, left_len:end - start - right_len]
  18. sums = np.sum(cur, axis=1)
  19. succs = int(sums[0])
  20. fails = int(sums[1])
  21. commits = int(sums[1])
  22. return succs, commits, y
  23. def calc_cov_chratios(data, pos_map, start, end, window, left_len,right_len = 0):
  24. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  25. sum_view = view[:, start + left_len:end - right_len]
  26. sums = np.sum(sum_view, axis=1)
  27. succs = int(sums[0])
  28. fails = int(sums[1])
  29. commits = int(sums[2])
  30. view = view[:, start:end]
  31. succ = view[0, :]
  32. fail = view[1, :]
  33. if window is not None:
  34. succ = np.convolve(succ, window, 'same')
  35. fail = np.convolve(fail, window, 'same')
  36. commit = succ + fail + 0.0000001
  37. y = succ / commit
  38. y = y[left_len:end - start - right_len]
  39. return succs, commits, y
  40. def calc_cov_chsuccs(data, pos_map, start, end, window, left_len,right_len):
  41. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  42. view = view[:, start:end]
  43. cur = view[:, left_len:end - start - right_len]
  44. sums = np.sum(cur, axis=1)
  45. succs = int(sums[0])
  46. fails = int(sums[1])
  47. succ = view[0, :]
  48. commit = view[2, :]
  49. succ = np.convolve(succ, window, 'same')
  50. commit = np.convolve(commit, window, 'same')
  51. succ = succ[left_len:end - start - right_len]
  52. commit = commit[left_len:end - start - right_len]
  53. return succ, commit, succs, (succs + fails)
  54. def calc_chspeed(data, pos_map, start, end):
  55. view = data[[pos_map.commit_count], :]
  56. view = view[:, start:end]
  57. speed = np.sum(view, axis=1)
  58. return int(speed[0])
  59. def calc_chratio(data, pos_map, start, end):
  60. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.succ_periods, pos_map.fail_periods, pos_map.commit_count], :]
  61. view = view[:, start:end]
  62. sums = np.sum(view, axis=1)
  63. succs = sums[0]
  64. fails = sums[1]
  65. succ_periods = sums[2]
  66. fail_periods = sums[3]
  67. commit_count = int(sums[4])
  68. all = int(succs + fails)
  69. ratio = succs / (commit_count + 0.00001)
  70. back_time = (succ_periods + fail_periods) / (succs + fails + 0.00001)
  71. succ_time = (succ_periods) / (succs + 0.00001)
  72. return round(ratio, 5), commit_count, round(back_time, 5),round(succ_time, 5)
  73. def calc_commit(data, pos_map, start, end):
  74. view = data[[pos_map.commit_count], :]
  75. view = view[:, start:end]
  76. sums = np.sum(view, axis=1)
  77. commit_count = int(sums[0])
  78. return commit_count
  79. def calc_mchratios(data, pos_map, start, end, window, left_len, right_len):
  80. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  81. sum_view = view[:, start + left_len:end - right_len]
  82. sums = np.sum(sum_view, axis=1)
  83. succs = int(sums[0])
  84. fails = int(sums[1])
  85. commits = int(sums[2])
  86. view = view[:, start:end]
  87. all = np.cumsum(view, axis=1)
  88. succ = all[0, :]
  89. commit = all[2, :] + 0.0000001
  90. y = succ / commit
  91. y = y.ravel()
  92. if window is not None:
  93. y = np.convolve(y, window, 'same')
  94. y = y[left_len:end - start - right_len]
  95. return succs, commits, y
  96. def calc_mchratios_val(data, pos_map, start, end):
  97. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  98. view = view[:, start:end]
  99. sums = np.sum(view, axis=1)
  100. succs = sums[0]
  101. fails = sums[1]
  102. ratio = succs / (succs + fails + 0.0000001)
  103. return int(succs), int(succs + fails), round(ratio,5)
  104. def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
  105. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  106. sum_view = view[:, start + left_len:end - right_len]
  107. sums = np.sum(sum_view, axis=1)
  108. succs = sums[0]
  109. fails = sums[1]
  110. view = view[:, start:end]
  111. succ = view[0, :]
  112. fail = view[1, :]
  113. succ = np.convolve(succ, window, 'same')
  114. fail = np.convolve(fail, window, 'same')
  115. commit = succ + fail + 0.0000001
  116. y = succ / commit
  117. y = y[left_len:end - start - right_len]
  118. return int(succs), int(succs + fails), y
  119. def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
  120. view = data[:, start:end]
  121. sums = np.sum(view, axis=1)
  122. all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
  123. ratio = sums[pos_map.succ_mch_amounts] / all_return
  124. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  125. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  126. lack_amounts = send_amounts * ratio
  127. return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
  128. sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
  129. def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
  130. view = data[:, start:end]
  131. sums = np.sum(view, axis=1)
  132. all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
  133. ratio = sums[pos_map.succ_count] / all_return
  134. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  135. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  136. lack_amounts = send_amounts * ratio
  137. logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
  138. return send_amounts, lack_amounts
  139. # 用于计算成功率及利润率
  140. # succ_count, fail_count, succ_ratio, profit,profit_ratio
  141. def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
  142. view = data[:, start:end]
  143. sums = np.sum(view, axis=1)
  144. submit_count = sums[pos_map.submit_count]
  145. succ_count = sums[pos_map.succ_count]
  146. fail_count = sums[pos_map.fail_count]
  147. succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
  148. ch_amounts = sums[pos_map.succ_ch_amounts]
  149. mch_amounts = sums[pos_map.succ_mch_amounts]
  150. profit = mch_amounts - ch_amounts
  151. return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 5), round(profit, 3)
  152. def calc_cov_netfail(data, pos_map, start, end, window):
  153. view = data[[pos_map.succ_count, pos_map.fail_count], :]
  154. view = view[:, start:end]
  155. sums = np.sum(view, axis=1)
  156. succs = int(sums[0])
  157. fails = int(sums[1])
  158. succ = view[0, :]
  159. fail = view[1, :]
  160. succ = np.convolve(succ, window, 'same')
  161. fail = np.convolve(fail, window, 'same')
  162. fail = fail / (fail + succ + 0.0000001)
  163. return fail, fails, (succs + fails)