#!/usr/bin/env python from datetime import datetime from traffic_csv_converter import * import cv2 """ sessions_plotter.py has 3 functions to create spectogram, histogram, 2d_histogram from [(ts, size),..] session. sessions_plotter.py 具有 3 个功能,可从 [(ts, size),..] 会话创建频谱图、直方图2d_histogram。 """ import os import matplotlib import cv2 as cv matplotlib.use('agg') import matplotlib.pyplot as plt import numpy as np import datetime,time MTU = 1500 def session_spectogram(ts, sizes, name=None): plt.scatter(ts, sizes, marker='.') plt.ylim(0, MTU)#设置y轴坐标区间 #plt.ylim(MTU, MTU2) # 设置y轴坐标区间 plt.xlim(ts[0], ts[-1])#设置x轴坐标区间 # plt.yticks(np.arange(0, MTU, 10)) # plt.xticks(np.arange(int(ts[0]), int(ts[-1]), 10)) plt.title(name + " Session Spectogram")#输出表头 plt.ylabel('Size [B]') plt.xlabel('Time [sec]') plt.grid(True)#显示网格线 1=True=默认显示;0=False=不显示 plt.show() def session_atricle_spectogram(ts, sizes, fpath=None, show=True, tps=None): if tps is None: max_delta_time = ts[-1] - ts[0] else: max_delta_time = tps ts_norm = ((np.array(ts) - ts[0]) / max_delta_time) * MTU plt.figure() plt.scatter(ts_norm, sizes, marker=',', c='k', s=5) plt.ylim(0, MTU) plt.xlim(0, MTU) plt.ylabel('Packet Size [B]') plt.xlabel('Normalized Arrival Time') plt.set_cmap('binary')#如果将当前图窗比作一幅简笔画,则cmap就代表颜料盘的配色,用所提供的颜料盘自动给当前简笔画上色,就是cmap所做的事。 #plt.set_cmap('cool') plt.axes().set_aspect('equal')#设置轴缩放的方面,即y-unit与x-unit的比率 plt.grid(False)#显示网格线 1=True=默认显示;0=False=不显示 if fpath is not None: # plt.savefig(OUTPUT_DIR + fname, bbox_inches='tight', pad_inches=1) plt.savefig(fpath, bbox_inches='tight') #第一个实参指定要以什么样的文件名保存图表。 #第二个实参指定将图表多余的空白区域裁减掉。如果要保留图表周围多余的空白区域,可省略这个实参。 if show: plt.show() plt.close() def session_histogram(sizes, plot=False): hist, bin_edges = np.histogram(sizes, bins=range(0, MTU + 1, 1)) #sizes数组,bins表示的是范围,即直方图纵坐标最大值,(0,MTU + 1)表示只统计[0,MTU]不包括0 if plot: plt.bar(bin_edges[:-1], hist, width=1)#hist直方图,width单个直方图的宽度 plt.xlim(min(bin_edges), max(bin_edges)+100) plt.show() return hist.astype(np.uint16)#转换数据类型为np.uint16 def session_2d_histogram(ts, sizes, plot=False, tps=None, qwe=0):#plot=False不画出图例 if tps is None: max_delta_time = ts[-1] - ts[0] else: max_delta_time = tps # ts_norm = map(int, ((np.array(ts) - ts[0]) / max_delta_time) * MTU) ts_norm = ((np.array(ts) - ts[0]) / max_delta_time) * MTU H, xedges, yedges = np.histogram2d(sizes, ts_norm, bins=(range(0, MTU + 1, 1), range(0, MTU + 1, 1))) if plot: plt.pcolormesh(xedges, yedges, H)#pcolormeshp()函数用于创建具有非规则矩形网格的伪彩色图 #plt.colorbar()#colorbar 即主图旁一个长条状的小图,能够辅助表示主图中 colormap 的颜色组成和颜色与数值的对应关系 plt.scatter(ts_norm, sizes, marker=',', s=1) plt.xlim(0, MTU) plt.ylim(0, MTU) # 326 plt.ylabel('Packet Size [B]') plt.xlabel('Normalized Arrival Time') # plt.set_cmap('gnuplot') plt.set_cmap('BuPu') # plt.set_cmap('Greys') plt.axis('off') qwe=str(qwe) figure_save_path = "appp111" # 这里创建了一个文件夹,如果依次创建不同文件夹,可以用name_list[i] # figure_save_path2 = "appp08" # 这里创建了一个文件夹,如果依次创建不同文件夹,可以用name_list[i] if not os.path.exists(figure_save_path): os.makedirs(figure_save_path) # 如果不存在目录figure_save_path,则创建 # if not os.path.exists(figure_save_path2): # os.makedirs(figure_save_path2) # 如果不存在目录figure_save_path,则创建 plt.savefig(os.path.join(figure_save_path, qwe+ ".png"),bbox_inches='tight',pad_inches = 0) # 分别命名图片 # # cv2.imwrite(filename, img) # img = cv2.imread(s) # cv2.imwrite(img, figure_save_path2, qwe+ ".png") # # # #plt.savefig("filename" +qwe+ ".png") plt.close() plt.show() # plt.axis('off') # # plt.show() # plt.savefig(os.path.join(figure_save_path2, qwe + ".png")) # 分别命名图片 return H.astype(np.uint16)#转换数据类型为np.uint16 # def session_2d_histogram1(ts, sizes, plot=False, tps=None, qwe=0):#plot=False不画出图例 # if tps is None: # max_delta_time = ts[-1] - ts[0] # else: # max_delta_time = tps # # # ts_norm = map(int, ((np.array(ts) - ts[0]) / max_delta_time) * MTU) # ts_norm = ((np.array(ts) - ts[0]) / max_delta_time) * MTU # H, xedges, yedges = np.histogram2d(sizes, ts_norm, bins=(range(0, MTU + 1, 1), range(0, MTU + 1, 1))) # if plot: # plt.pcolormesh(xedges, yedges, H)#pcolormeshp()函数用于创建具有非规则矩形网格的伪彩色图 # #plt.colorbar()#colorbar 即主图旁一个长条状的小图,能够辅助表示主图中 colormap 的颜色组成和颜色与数值的对应关系 # plt.scatter(ts_norm, sizes, marker=',', s=0.5) # plt.xlim(0, MTU) # plt.ylim(0, MTU) # plt.ylabel('Packet Size [B]') # plt.xlabel('Normalized Arrival Time') # #plt.set_cmap('gnuplot') # plt.set_cmap('BuPu') # qwe=str(qwe) # # figure_save_path = "fig_voip_vpn1" # 这里创建了一个文件夹,如果依次创建不同文件夹,可以用name_list[i] # if not os.path.exists(figure_save_path): # os.makedirs(figure_save_path) # 如果不存在目录figure_save_path,则创建 # plt.savefig(os.path.join(figure_save_path, qwe+ ".png")) # 分别命名图片 # plt.close() # #plt.savefig("filename" +qwe+ ".png") # plt.show() # return H.astype(np.uint16)#转换数据类型为np.uint16