def view_logfile(filename, show=True): """Plots the data from a logfile""" fp = open(filename, 'r') data = json.load(fp) original_data = data['original_data'] processed_data = data['processed_data'] results = data['results'] plot_data(filename, original_data, processed_data, results, show) fp.close()
def view_logfile(filename, show=True): """Plots the data from a logfile""" fp = open(filename, "r") data = json.load(fp) original_data = data["original_data"] processed_data = data["processed_data"] results = data["results"] plot_data(filename, original_data, processed_data, results, show) fp.close()
def main(filename): fp = open(filename, 'r') data = json.load(fp) fp.close() detector_data = data[0]['datapoints'] timeseries = [t[0] for t in detector_data] SpikeDetector.convert_null_values(timeseries) timestamps = [t[1] for t in detector_data] orig_series = timeseries[:] orig_stamps = timestamps[:] res = DETECTOR.detect_anomalies(timeseries, timestamps) plot_data(filename, (orig_stamps, orig_series), (timestamps, timeseries), res)
def test(self, X_test=None, y_test=None, split=True): """Online testing instance one by one Parameters ---------- X_test: y_test: split: True split each instance into subflows Returns ------- """ # set model in the "eval: mode. self.model.eval() test_loader = zip(X_test, y_test) # Calculate Accuracy correct = 0 total = 0 y_score = [] outputs = [] # Iterate through test dataset one by one for i, (X, y) in enumerate(test_loader): if split: new_Xs, new_ys = split_instance(X, y) res = [] # testing subflow with adding more packets for i, (_x, _y) in enumerate(zip(new_Xs, new_ys)): # print(i, len(X)) _x = Tensor(_x) _y = Tensor([_y]) if torch.cuda.is_available(): _x = Variable( _x.view(1, _x.shape[0], self.in_dim).cuda()) else: _x = Variable(_x.view(1, _x.shape[0], self.in_dim)) # Forward pass only to get logits/output if torch.cuda.is_available(): v = torch.sum( (self.model(_x).cpu() - self.centers.cpu())**2, dim=1).data.numpy() else: v = torch.sum((self.model(_x) - self.centers)**2, dim=1).data.numpy() res.append(v.item( )) # [subflow1_res, subflow2_res,...] (for each flow) outputs.append(res) results = [] R = np.percentile(self.normal_dists, q=90) succeeded_flows = [] failed_flows = [] print("len of outputs:", len(outputs)) with open(r'D:\研一\异常流量检测\result.txt', 'a') as f: for i in outputs: f.write(str(i) + '\n') #print("outputs:",outputs) for vs in outputs: find_flg = False _scores = [] k = 1 v_b = 0 for j, v in enumerate( vs ): # res := [subflow1_res, subflow2_res,...] (for each flow) _score, _confidence = self.score_function(v) _scores.append([_score, _confidence]) ###########这里粗暴break有问题 # if v > R: # find_flg = True # break v_b = v_b + v * k #print(v) #print(torch.sigmoid(torch.Tensor([v_b]))) print(torch.Tensor([v_b])) if torch.sigmoid(torch.Tensor([v_b])) <= 0.1: find_flg = True break k += 1 #print(k) res = { 'needed_ptks': j + 1, 'R': R, 'prob': v, 'find_flg': find_flg, 'time': 0, '_scores': _score } results.append(res) if res['find_flg']: succeeded_flows.append(res['needed_ptks']) else: failed_flows.append(res['needed_ptks']) if len(succeeded_flows) > 0: print( f'average_needed_pkts: {np.mean(succeeded_flows)} +/- {np.std(succeeded_flows)} ' f'when R: {R}, succeeded_flows: {len(succeeded_flows)}, all_flows: {len(y_test)}' ) if len(failed_flows) > 0: print( f'failed_flows: {np.mean(failed_flows)} +/- {np.std(failed_flows)} when R: {R}, ' f'failed_flows: {len(failed_flows)}, all_flows: {len(y_test)}') # plot the figure of num of packets and aucs min_flow_len = min([len(vs) for vs in outputs]) print(f'***min_flow_len: {min_flow_len}') aucs = [] accs = [] part_results = np.asarray([v[:min_flow_len] for v in outputs]) for i in range(min_flow_len): fpr, tpr, _ = roc_curve(y_test, part_results[:, i], pos_label=1) auc = metrics.auc(fpr, tpr) aucs.append(auc) y_pred = [1 if v < self.R else 0 for v in part_results[:, i]] acc = metrics.accuracy_score(y_test, y_pred) print(acc) accs.append(acc) print(accs) plot_data(range(1, min_flow_len + 1), aucs, xlabel='num of packets in each flow', ylabel='auc', title='') plot_data(range(1, min_flow_len + 1), accs, xlabel='num of packets in each flow', ylabel='accuracy', title='') print(f'aucs: {aucs}') print(f'accs: {accs}')