source: examples/stats/ipdist-genplot.py @ d9a4dc2

develop
Last change on this file since d9a4dc2 was d9a4dc2, checked in by Jacob Van Walraven <jcv9@…>, 4 years ago

Move ipdist to stats directory, Move the plotting of data into python script

  • Property mode set to 100644
File size: 5.2 KB
Line 
1#!/usr/bin/python
2
3import sys
4import subprocess
5import os
6
7# Ensure a data directory was supplied
8if len(sys.argv) != 2:
9        print("Usage: python data-directory")
10        sys.exit()
11
12dir = sys.argv[1]
13dataFiles = []
14statsFiles = []
15
16# Get all data/stats files contained within the directory
17for file in os.listdir(dir):
18        if file.endswith(".data"):
19                dataFiles.append(file)
20        if file.endswith(".stats"):
21                statsFiles.append(file)
22
23# Every datafile should have a corresponding stats file
24# Iterate over every dataFile
25for i in range(len(dataFiles)):
26        dataFile = dataFiles[i]
27        filename,extension = dataFiles[i].split(".")
28        statsFile = filename + ".stats"
29        tick = filename.split("-")
30        tick = tick[1]
31
32        for x in range(4):
33                plot = subprocess.Popen(['gnuplot -persistent','-p'],
34                                        shell=True,
35                                        stdin=subprocess.PIPE,)
36
37                plot.stdin.write("set term pngcairo enhanced size 1280,960\n")
38                plot.stdin.write("set output '" + dir + "/" + filename + "-octet" + str(x+1) + ".png'\n")
39                plot.stdin.write("set multiplot layout 2,1\n")
40                plot.stdin.write("set title 'IP Distribution - " + tick + "'\n")
41                plot.stdin.write("set xrange[0:255]\n")
42                plot.stdin.write("set y2range[-1:1]\n")
43                plot.stdin.write("set y2tics\n")
44                plot.stdin.write("set xlabel 'Prefix'\n")
45                plot.stdin.write("set ylabel 'Hits'\n")
46                plot.stdin.write("set y2label 'Skewness'\n")
47                plot.stdin.write("set xtics 0,10,255\n")
48                plot.stdin.write("stats '" + dir + "/" + statsFile + "' index " + str(x) + " every ::0::0 using 2 name 'SOURCEMEAN' nooutput\n")
49                plot.stdin.write("stats '" + dir + "/" + statsFile + "' index " + str(x) + " every ::1::1 using 2 name 'DESTMEAN' nooutput\n")
50                plot.stdin.write("stats '" + dir + "/" + statsFile + "' index " + str(x) + " every ::0::0 using 7 name 'SOURCESKEW' nooutput\n")
51                plot.stdin.write("stats '" + dir + "/" + statsFile + "' index " + str(x) + " every ::1::1 using 7 name 'DESTSKEW' nooutput\n")
52                plot.stdin.write("set arrow from SOURCEMEAN_min, graph 0 to SOURCEMEAN_min, graph 1 nohead lt 1\n")
53                plot.stdin.write("set arrow from DESTMEAN_min, graph 0 to DESTMEAN_min, graph 1 nohead lt 2\n")
54                plot.stdin.write("plot '" + dir + "/" + dataFile + "' using " + str((x*4)+3) + ":" + str((x*4)+4) + " index 0 title 'Source octet " + str(x+1) + "' smooth unique with boxes,")
55                plot.stdin.write("'' using " + str((x*4)+5) + ":" + str((x*4)+6) + " index 0 title 'Destination octet " + str(x+1) + "' smooth unique with boxes,")
56                plot.stdin.write("1/0 t 'Source mean' lt 1,")
57                plot.stdin.write("1/0 t 'Destination mean' lt 2,")
58                plot.stdin.write("SOURCESKEW_min title 'Source Skewness' axes x1y2,")
59                plot.stdin.write("DESTSKEW_min title 'Destination Skewness' axes x1y2\n")
60                plot.stdin.write("unset y2tics\n")
61                plot.stdin.write("unset y2label\n")
62                plot.stdin.write("unset arrow\n")
63                plot.stdin.write("unset label 1\nunset label 2\nunset label 3\nunset label 4\nunset label 5\nunset label 6\n")
64                plot.stdin.write("set title 'Zipf Distribution'\n")
65                plot.stdin.write("set xlabel 'Rank'\n")
66                plot.stdin.write("set ylabel 'Frequency'\n")
67                plot.stdin.write("set logscale xy 10\n")
68                plot.stdin.write("set xrange[1:255]\n")
69                plot.stdin.write("set xtics 0,10,255\n")
70                plot.stdin.write("plot '" + dir + "/" + dataFile + "' using 2:" + str((x*4)+4) + " index 0 title 'Source octet " + str(x+1) + "',")
71                plot.stdin.write("'' using 2:" + str((x*4)+6) + " index 0 title 'Destination octet " + str(x+1) + "'\n")
72                plot.stdin.flush()
73                plot.communicate()
74
75
76# Generate plots for the timeseries data captured over the entire trace
77for i in range(4):
78        for x in range(2):
79                plot = subprocess.Popen(['gnuplot -persistent','-p'],
80                                        shell=True,
81                                        stdin=subprocess.PIPE,)
82                plot.stdin.write("set term pngcairo size 1280,960\n")
83                if x == 0:
84                        plot.stdin.write("set output '" + dir + "/ipdist-timeseries-src-octet" + str(i+1) + ".png'\n")
85                        plot.stdin.write("set title 'Timeseries src octet " + str(i+1) + "'\n")
86                else:
87                        plot.stdin.write("set output '" + dir + "/ipdist-timeseries-dst-octet" + str(i+1) + ".png'\n")
88                        plot.stdin.write("set title 'Timeseries dst octet " + str(i+1) + "'\n")
89                plot.stdin.write("set multiplot layout 2,1\n")
90                plot.stdin.write("set xtics rotate\n")
91                plot.stdin.write("set ytics\n")
92                plot.stdin.write("set xlabel 'Timestamp'\n")
93                plot.stdin.write("set key off\n")
94                plot.stdin.write("set autoscale xy\n")
95                if x == 0:
96                        plot.stdin.write("plot '" + dir + "/ipdist-src-octet" + str(i+1) + ".timeseries' using 2:xtic(1) with lines title columnheader(2) at end smooth cumulative, for[i=3:257] '' using i with lines title columnheader(i) at end smooth cumulative\n")
97                else:
98                        plot.stdin.write("plot '" + dir + "/ipdist-dst-octet" + str(i+1) + ".timeseries' using 2:xtic(1) with lines title columnheader(2) at end smooth cumulative, for[i=3:257] '' using i with lines title columnheader(i) at end smooth cumulative\n")
99                plot.stdin.write("set title 'Timeseries mean skewness'\n")
100                plot.stdin.write("set yrange[-1:1]\n")
101                plot.stdin.write("set xlabel 'Timestamp'\n")
102                plot.stdin.write("set ylabel 'Skewness'\n")
103                plot.stdin.write("plot '" + dir + "/ipdist-timeseries-skewness.stats' using " + str((i*2)+2+x) + ":xtic(1) with lines\n")
104                plot.stdin.write("unset multiplot\n")
105                plot.stdin.flush()
106                plot.communicate()
107
Note: See TracBrowser for help on using the repository browser.