123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- from .DataStream import EMchPosmap
- import numpy as np
- import logging
- logger = logging.getLogger('algorithm')
- def calc_chratios(data, pos_map, start, end, window, left_len, right_len):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
- all = view[:, start:end]
- view = np.cumsum(all, axis=1)
- succ = view[0, :]
- fail = view[1, :]
- commit = view[2, :] + 0.0000001
- y = succ / commit
- y = y.ravel()
- if window is not None:
- y = np.convolve(y, window, 'same')
- y = y[left_len:end - start - right_len]
- cur = all[:, left_len:end - start - right_len]
- sums = np.sum(cur, axis=1)
- succs = int(sums[0])
- fails = int(sums[1])
- commits = int(sums[1])
- return succs, commits, y
- def calc_cov_chratios(data, pos_map, start, end, window, left_len,right_len = 0):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
- sum_view = view[:, start + left_len:end - right_len]
- sums = np.sum(sum_view, axis=1)
- succs = int(sums[0])
- fails = int(sums[1])
- commits = int(sums[2])
- view = view[:, start:end]
- succ = view[0, :]
- fail = view[1, :]
- if window is not None:
- succ = np.convolve(succ, window, 'same')
- fail = np.convolve(fail, window, 'same')
- commit = succ + fail + 0.0000001
- y = succ / commit
- y = y[left_len:end - start - right_len]
- return succs, commits, y
- def calc_chspeed_ratio(data, pos_map, start, end, period):
- dim = pos_map.dim()
- view = data[:, start:end]
- sum_view = view.reshape((dim, -1, period))
- sums = np.sum(sum_view, axis=2)
- succs = sums[pos_map.succ_count]
- commits = sums[pos_map.commit_count]
- ratios = succs / (commits + 0.000001)
- return commits, succs, ratios
- def calc_cov_chsuccs(data, pos_map, start, end, window, left_len,right_len):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.commit_count], :]
- view = view[:, start:end]
- cur = view[:, left_len:end - start - right_len]
- sums = np.sum(cur, axis=1)
- succs = int(sums[0])
- fails = int(sums[1])
- succ = view[0, :]
- commit = view[2, :]
- succ = np.convolve(succ, window, 'same')
- commit = np.convolve(commit, window, 'same')
- succ = succ[left_len:end - start - right_len]
- commit = commit[left_len:end - start - right_len]
- return succ, commit, succs, (succs + fails)
- def calc_chspeed(data, pos_map, start, end):
- view = data[[pos_map.commit_count], :]
- view = view[:, start:end]
- speed = np.sum(view, axis=1)
- return int(speed[0])
- def calc_chratio(data, pos_map, start, end):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.succ_periods, pos_map.fail_periods, pos_map.commit_count], :]
- view = view[:, start:end]
- sums = np.sum(view, axis=1)
- succs = sums[0]
- fails = sums[1]
- succ_periods = sums[2]
- fail_periods = sums[3]
- commit_count = int(sums[4])
- all = int(succs + fails)
- ratio = succs / (commit_count + 0.00001)
- back_time = (succ_periods + fail_periods) / (succs + fails + 0.00001)
- succ_time = (succ_periods) / (succs + 0.00001)
- return round(ratio, 5), commit_count, int(back_time),int(succ_time)
- def calc_commit(data, pos_map, start, end):
- view = data[[pos_map.commit_count], :]
- view = view[:, start:end]
- sums = np.sum(view, axis=1)
- commit_count = int(sums[0])
- return commit_count
- def calc_mchratios(data, pos_map, start, end, window, left_len, right_len):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
- sum_view = view[:, start + left_len:end - right_len]
- sums = np.sum(sum_view, axis=1)
- succs = int(sums[0])
- fails = int(sums[1])
- commits = int(sums[2])
- view = view[:, start:end]
- all = np.cumsum(view, axis=1)
- succ = all[0, :]
- commit = all[2, :] + 0.0000001
- y = succ / commit
- y = y.ravel()
- if window is not None:
- y = np.convolve(y, window, 'same')
- y = y[left_len:end - start - right_len]
- return succs, commits, y
- def calc_mchratios_val(data, pos_map, start, end):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
- view = view[:, start:end]
- sums = np.sum(view, axis=1)
- succs = sums[0]
- fails = sums[1]
- ratio = succs / (succs + fails + 0.0000001)
- return int(succs), int(succs + fails), round(ratio,5)
- def calc_cov_mchratios(data, pos_map, start, end, window, left_len, right_len):
- view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :]
- sum_view = view[:, start + left_len:end - right_len]
- sums = np.sum(sum_view, axis=1)
- succs = sums[0]
- fails = sums[1]
- view = view[:, start:end]
- succ = view[0, :]
- fail = view[1, :]
- succ = np.convolve(succ, window, 'same')
- fail = np.convolve(fail, window, 'same')
- commit = succ + fail + 0.0000001
- y = succ / commit
- y = y[left_len:end - start - right_len]
- return int(succs), int(succs + fails), y
- def calc_morder_send(data, pos_map: type(EMchPosmap), start: int, end: int):
- view = data[:, start:end]
- sums = np.sum(view, axis=1)
- all_return = sums[pos_map.succ_mch_amounts] + sums[pos_map.fail_mch_amounts] + 0.0000001
- ratio = sums[pos_map.succ_mch_amounts] / all_return
- send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
- send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
- lack_amounts = send_amounts * ratio
- return send_count, sums[pos_map.submit_count], sums[pos_map.succ_count], sums[pos_map.fail_count], sums[pos_map.submit_amounts], \
- sums[pos_map.succ_mch_amounts], sums[pos_map.fail_mch_amounts], send_amounts, lack_amounts
- def calc_morder_lack(data, pos_map: type(EMchPosmap), start: int, end: int):
- view = data[:, start:end]
- sums = np.sum(view, axis=1)
- all_return = sums[pos_map.succ_count] + sums[pos_map.fail_count] + 0.0000001
- ratio = sums[pos_map.succ_count] / all_return
- send_count = sums[pos_map.submit_count] - sums[pos_map.succ_count] - sums[pos_map.fail_count]
- send_amounts = sums[pos_map.submit_amounts] - sums[pos_map.succ_mch_amounts] - sums[pos_map.fail_mch_amounts]
- lack_amounts = send_amounts * ratio
- logger.info("send_count=%d send_amounts=%.4f ratio=%.4f lack_amounts=%.4f", send_count, send_amounts, ratio, lack_amounts)
- return send_amounts, lack_amounts
- # 用于计算成功率及利润率
- # succ_count, fail_count, succ_ratio, profit,profit_ratio
- def calc_mch_profit(data, pos_map: type(EMchPosmap), start: int, end: int):
- view = data[:, start:end]
- sums = np.sum(view, axis=1)
- submit_count = sums[pos_map.submit_count]
- succ_count = sums[pos_map.succ_count]
- fail_count = sums[pos_map.fail_count]
- succ_ratio = succ_count / (succ_count + fail_count + 0.0000001)
- ch_amounts = sums[pos_map.succ_ch_amounts]
- mch_amounts = sums[pos_map.succ_mch_amounts]
- profit = mch_amounts - ch_amounts
- return int(submit_count), int(succ_count), int(fail_count), round(succ_ratio, 5), round(profit, 3)
- def calc_cov_netfail(data, pos_map, start, end, window):
- view = data[[pos_map.succ_count, pos_map.fail_count], :]
- view = view[:, start:end]
- sums = np.sum(view, axis=1)
- succs = int(sums[0])
- fails = int(sums[1])
- succ = view[0, :]
- fail = view[1, :]
- succ = np.convolve(succ, window, 'same')
- fail = np.convolve(fail, window, 'same')
- fail = fail / (fail + succ + 0.0000001)
- return fail, fails, (succs + fails)
|