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): sum_view = data[:, start + left_len:end - right_len] sums = np.sum(sum_view, axis=1) succs = int(sums[pos_map.succ_count]) fails = int(sums[pos_map.fail_count]) commits = int(sums[pos_map.commit_count]) succ_periods = int(sums[pos_map.succ_periods]) fail_periods = int(sums[pos_map.fail_periods]) view = data[:, start:end] succ = view[pos_map.succ_count, :] fail = view[pos_map.fail_count, :] 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, int(succ_periods / (succs + 1)), int(fail_periods / (fails + 1)) def calc_count_cdf(data, pos_map, start, end, left_len, right_len=0): view = data[:, start + left_len:end - right_len] view = np.cumsum(view, axis=1) succs = view[pos_map.succ_count, :] commits = view[pos_map.commit_count, :] return succs.ravel(), commits.ravel() 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_chprice(data, pos_map, start, end): commits = data[pos_map.commit_count, :] pos_commit = np.where(commits > 0) price = None profit = None pratio = None if len(pos_commit[0]) > 0: view = data[:, pos_commit] view = view.reshape(pos_map.dim(), -1) sums = np.sum(view, axis=1) count = int(sums[pos_map.commit_count]) amounts = sums[pos_map.commit_amounts] price = round(amounts / count, 4) else: return price, profit, pratio succs = data[pos_map.succ_count, :] pos_succ = np.where(succs > 0) if len(pos_succ[0]) > 0: view = data[:, pos_succ] view = view.reshape(pos_map.dim(), -1) sums = np.sum(view, axis=1) mch_amounts = sums[pos_map.succ_mch_amounts] ch_amounts = sums[pos_map.succ_amounts] profit = mch_amounts - ch_amounts pratio = round(profit / ch_amounts, 4) return price, profit, pratio 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.0000001) back_time = (succ_periods + fail_periods) / (succs + fails + 0.0000001) 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, 6), round(profit, 4) 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)