from .DataStream import EMchPosmap import numpy as np import logging logger = logging.getLogger('calcer') def calc_chratios(data, pos_map, start, end): 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 = succ + fail + 0.0000001 y = succ / commit y = y.ravel() return int(view[0, -1]), int(view[0, -1] + view[1, -1]), y def calc_cov_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], :] 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_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] sums = np.sum(view, 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, round(back_time, 5),round(succ_time, 5) 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): view = data[[pos_map.succ_count, pos_map.fail_count, pos_map.submit_count], :] view = view[:, start:end] all = np.cumsum(view, axis=1) succ = all[0, :] commit = all[0, :] + all[1, :] commit += 0.0000001 y = succ / commit y = y.ravel() return int(all[0, -1]), int(all[0, -1] + all[1, -1]), 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, left_len,right_len): 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') succ = succ[left_len:end - start - right_len] fail = fail[left_len:end - start - right_len] fail = fail / (fail + succ + 0.0000001) return fail, fails, (succs + fails)