def find_best_k(func, method, td_lag): # Find embedding dimension which maximizes AP k_best, ap_best, auc_best = 0, 0.0, 0.0 regions_best = [] for k in range(3, 21): detections = maxdiv.maxdiv(func['ts'], method=method, mode='I_OMEGA', extint_min_len=20, extint_max_len=100, num_intervals=None, td_dim=k, td_lag=td_lag) cur_ap = eval.average_precision([func['gt']], [detections]) cur_auc = eval.auc(func['gt'], detections, func['ts'].shape[1]) if (k_best == 0) or (cur_ap > ap_best) or ((cur_ap == ap_best) and (cur_auc > auc_best)): k_best, ap_best, auc_best, regions_best = k, cur_ap, cur_auc, detections return regions_best, k_best
ratios = [] for l in range(20, 201, 15): print(l) funcs = [sample_gp_with_meanshift(n, l) for i in range(m)] for method in methods: auc = [] regions = [] for i in range(m): gp, ygt = funcs[i] regions.append(maxdiv.maxdiv(gp, method = method, num_intervals = 5, extint_min_len = 20, extint_max_len = 220, kernelparameters={'kernel_sigma_sq': args.kernel_sigma_sq}, **parameters)) auc.append(eval.auc(ygt, regions[-1], n)) aucs[method].append(np.mean(auc)) aps[method].append(eval.average_precision([ygt for gp, ygt in funcs], regions)) ratios.append(float(l) / n) # Plot results fig_auc = plt.figure() sp_auc = fig_auc.add_subplot(111, xlabel = 'Length of anomaly / Length of time series', ylabel = 'AUC') fig_ap = plt.figure() sp_ap = fig_ap.add_subplot(111, xlabel = 'Length of anomaly / Length of time series', ylabel = 'Average Precision') for method in methods: sp_auc.plot(ratios, aucs[method], marker = 'x', label = method) sp_ap.plot(ratios, aps[method], marker = 'x', label = method) sp_auc.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3, ncol=len(methods), mode="expand", borderaxespad=0.)
aucs[ftype] = [] best_k[ftype] = Counter() for i, func in enumerate(data[ftype]): func_ids.append('{}_{:03d}'.format(ftype, i)) ygts.append(func['gt']) det, k_best = optimizers[args.optimizer](func, args.method, args.td_lag) # Divide scores by maximum score since their range differs widely depending on the dimensionality if args.method not in ('gaussian_cov_ts', 'gaussian_ts'): for r in range(len(det) - 1, -1, -1): det[r] = (det[r][0], det[r][1], det[r][2] / det[0][2]) regions.append(det) aucs[ftype].append(eval.auc(func['gt'], det, func['ts'].shape[1])) best_k[ftype][k_best] += 1 print("Best k: {}".format(k_best)) aps[ftype] = eval.average_precision(ygts, regions) print("AP: {}".format(aps[ftype])) if args.plot: plt.bar( np.array(list(best_k[ftype].keys())) - 0.5, list(best_k[ftype].values()), 1) plt.title(ftype) plt.show() all_ids += func_ids all_regions += regions
maxdiv.maxdiv( func['ts'], useLibMaxDiv=True, method=method, preproc=preproc, mode=mode, td_dim=args.td_dim if preproc is not None else 1, kernelparameters={ 'kernel_sigma_sq': args.kernel_sigma_sq }, **parameters)) time_stop = time.time() ygts.append(func['gt']) aucs.append( eval.auc(func['gt'], regions[-1], func['ts'].shape[1])) if preproc is None: if func['ts'].shape[1] not in times: times[func['ts'].shape[1]] = { m: [] for m in METHODS } times[func['ts'].shape[1]][method].append(time_stop - time_start) auc[id][ftype] = np.mean(aucs) auc_sd[id][ftype] = np.std(aucs) aps[id][ftype] = eval.average_precision(ygts, regions) all_gt[id] += ygts all_regions[id] += regions