algorithm.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from .DataStream import EMchPosmap
  2. import numpy as np
  3. import logging
  4. logger = logging.getLogger('algorithm')
  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_chspeed_ratio(data, pos_map, start, end, period):
  41. dim = pos_map.dim()
  42. view = data[:, start:end]
  43. sum_view = view.reshape((dim, -1, period))
  44. sums = np.sum(sum_view, axis=2)
  45. succs = sums[pos_map.succ_count]
  46. commits = sums[pos_map.commit_count]
  47. ratios = succs / (commits + 0.000001)
  48. return commits, succs, ratios
  49. def calc_cov_chsuccs(data, pos_map, start, end, window, left_len,right_len):
  50. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
  51. view = view[:, start:end]
  52. cur = view[:, left_len:end - start - right_len]
  53. sums = np.sum(cur, axis=1)
  54. succs = int(sums[0])
  55. fails = int(sums[1])
  56. succ = view[0, :]
  57. commit = view[2, :]
  58. succ = np.convolve(succ, window, 'same')
  59. commit = np.convolve(commit, window, 'same')
  60. succ = succ[left_len:end - start - right_len]
  61. commit = commit[left_len:end - start - right_len]
  62. return succ, commit, succs, (succs + fails)
  63. def calc_chspeed(data, pos_map, start, end):
  64. view = data[[pos_map.commit_count], :]
  65. view = view[:, start:end]
  66. speed = np.sum(view, axis=1)
  67. return int(speed[0])
  68. def calc_chratio(data, pos_map, start, end):
  69. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.succ_periods, pos_map.fail_periods, pos_map.commit_count], :]
  70. view = view[:, start:end]
  71. sums = np.sum(view, axis=1)
  72. succs = sums[0]
  73. fails = sums[1]
  74. succ_periods = sums[2]
  75. fail_periods = sums[3]
  76. commit_count = int(sums[4])
  77. all = int(succs + fails)
  78. ratio = succs / (commit_count + 0.00001)
  79. back_time = (succ_periods + fail_periods) / (succs + fails + 0.00001)
  80. succ_time = (succ_periods) / (succs + 0.00001)
  81. return round(ratio, 5), commit_count, int(back_time),int(succ_time)
  82. def calc_commit(data, pos_map, start, end):
  83. view = data[[pos_map.commit_count], :]
  84. view = view[:, start:end]
  85. sums = np.sum(view, axis=1)
  86. commit_count = int(sums[0])
  87. return commit_count
  88. def calc_mchratios(data, pos_map, start, end, window, left_len, right_len):
  89. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  90. sum_view = view[:, start + left_len:end - right_len]
  91. sums = np.sum(sum_view, axis=1)
  92. succs = int(sums[0])
  93. fails = int(sums[1])
  94. commits = int(sums[2])
  95. view = view[:, start:end]
  96. all = np.cumsum(view, axis=1)
  97. succ = all[0, :]
  98. commit = all[2, :] + 0.0000001
  99. y = succ / commit
  100. y = y.ravel()
  101. if window is not None:
  102. y = np.convolve(y, window, 'same')
  103. y = y[left_len:end - start - right_len]
  104. return succs, commits, y
  105. def calc_mchratios_val(data, pos_map, start, end):
  106. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  107. view = view[:, start:end]
  108. sums = np.sum(view, axis=1)
  109. succs = sums[0]
  110. fails = sums[1]
  111. ratio = succs / (succs + fails + 0.0000001)
  112. return int(succs), int(succs + fails), round(ratio,5)
  113. def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
  114. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  115. sum_view = view[:, start + left_len:end - right_len]
  116. sums = np.sum(sum_view, axis=1)
  117. succs = sums[0]
  118. fails = sums[1]
  119. view = view[:, start:end]
  120. succ = view[0, :]
  121. fail = view[1, :]
  122. succ = np.convolve(succ, window, 'same')
  123. fail = np.convolve(fail, window, 'same')
  124. commit = succ + fail + 0.0000001
  125. y = succ / commit
  126. y = y[left_len:end - start - right_len]
  127. return int(succs), int(succs + fails), y
  128. def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
  129. view = data[:, start:end]
  130. sums = np.sum(view, axis=1)
  131. all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
  132. ratio = sums[pos_map.succ_mch_amounts] / all_return
  133. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  134. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  135. lack_amounts = send_amounts * ratio
  136. return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
  137. sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
  138. def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
  139. view = data[:, start:end]
  140. sums = np.sum(view, axis=1)
  141. all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
  142. ratio = sums[pos_map.succ_count] / all_return
  143. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  144. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  145. lack_amounts = send_amounts * ratio
  146. logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
  147. return send_amounts, lack_amounts
  148. # 用于计算成功率及利润率
  149. # succ_count, fail_count, succ_ratio, profit,profit_ratio
  150. def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
  151. view = data[:, start:end]
  152. sums = np.sum(view, axis=1)
  153. submit_count = sums[pos_map.submit_count]
  154. succ_count = sums[pos_map.succ_count]
  155. fail_count = sums[pos_map.fail_count]
  156. succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
  157. ch_amounts = sums[pos_map.succ_ch_amounts]
  158. mch_amounts = sums[pos_map.succ_mch_amounts]
  159. profit = mch_amounts - ch_amounts
  160. return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 5), round(profit, 3)
  161. def calc_cov_netfail(data, pos_map, start, end, window):
  162. view = data[[pos_map.succ_count, pos_map.fail_count], :]
  163. view = view[:, start:end]
  164. sums = np.sum(view, axis=1)
  165. succs = int(sums[0])
  166. fails = int(sums[1])
  167. succ = view[0, :]
  168. fail = view[1, :]
  169. succ = np.convolve(succ, window, 'same')
  170. fail = np.convolve(fail, window, 'same')
  171. fail = fail / (fail + succ + 0.0000001)
  172. return fail, fails, (succs + fails)