algorithm.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 = sums[0]
  35. fails = 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 / (all + 0.00001)
  59. back_time = (succ_periods + fail_periods) / (succs + fails + 0.00001)
  60. return round(ratio, 5), commit_count, round(back_time, 5)
  61. def calc_commit(data, pos_map, start, end):
  62. view = data[[pos_map.commit_count], :]
  63. view = view[:, start:end]
  64. sums = np.sum(view, axis=1)
  65. commit_count = int(sums[0])
  66. return commit_count
  67. def calc_mchratios(data, pos_map, start, end):
  68. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  69. view = view[:, start:end]
  70. all = np.cumsum(view, axis=1)
  71. succ = all[0, :]
  72. commit = all[0, :] + all[1, :]
  73. commit += 0.0000001
  74. y = succ / commit
  75. y = y.ravel()
  76. return int(all[0, -1]), int(all[0, -1] + all[1, -1]), y
  77. def calc_mchratios_val(data, pos_map, start, end):
  78. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  79. view = view[:, start:end]
  80. sums = np.sum(view, axis=1)
  81. succs = sums[0]
  82. fails = sums[1]
  83. ratio = succs / (succs + fails + 0.0000001)
  84. return int(succs), int(succs + fails), round(ratio,5)
  85. def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
  86. view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
  87. sum_view = view[:, start + left_len:end - right_len]
  88. sums = np.sum(sum_view, axis=1)
  89. succs = sums[0]
  90. fails = sums[1]
  91. view = view[:, start:end]
  92. succ = view[0, :]
  93. fail = view[1, :]
  94. succ = np.convolve(succ, window, 'same')
  95. fail = np.convolve(fail, window, 'same')
  96. commit = succ + fail + 0.0000001
  97. y = succ / commit
  98. y = y[left_len:end - start - right_len]
  99. return int(succs), int(succs + fails), y
  100. def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
  101. view = data[:, start:end]
  102. sums = np.sum(view, axis=1)
  103. all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
  104. ratio = sums[pos_map.succ_mch_amounts] / all_return
  105. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  106. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  107. lack_amounts = send_amounts * ratio
  108. return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
  109. sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
  110. def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
  111. view = data[:, start:end]
  112. sums = np.sum(view, axis=1)
  113. all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
  114. ratio = sums[pos_map.succ_count] / all_return
  115. send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
  116. send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
  117. lack_amounts = send_amounts * ratio
  118. logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
  119. return send_amounts, lack_amounts
  120. # 用于计算成功率及利润率
  121. # succ_count, fail_count, succ_ratio, profit,profit_ratio
  122. def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
  123. view = data[:, start:end]
  124. sums = np.sum(view, axis=1)
  125. submit_count = sums[pos_map.submit_count]
  126. succ_count = sums[pos_map.succ_count]
  127. fail_count = sums[pos_map.fail_count]
  128. succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
  129. ch_amounts = sums[pos_map.succ_ch_amounts]
  130. mch_amounts = sums[pos_map.succ_mch_amounts]
  131. profit = mch_amounts - ch_amounts
  132. return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 5), round(profit, 3)