algorithm.py 6.6 KB

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