Peano
Loading...
Searching...
No Matches
plot-statistics.py
Go to the documentation of this file.
1# This file is part of the ExaHyPE2 project. For conditions of distribution and
2# use, please see the copyright notice at www.peano-framework.org
3import argparse
4import os
5
6import matplotlib.pyplot as plt
7
8
9Colours = [ "#ff0000",
10 "#00ff00",
11 "#0000ff",
12 "#ff00ff",
13 "#ffff00",
14 "#00ffff",
15 ]
16
17if __name__ == "__main__":
18 parser = argparse.ArgumentParser(description="""
19Peano 4 statistics postprocessing
20
21Plotter for statistics over time. The plotter works only for numerical
22data dumped as four tuple over time. Use inspect-statistics.py first
23to see what the indices of the individual column are and what semantics
24they have.
25
26""")
27 parser.add_argument("file", help="filename of examine")
28 parser.add_argument("-c",
29 "--columns",
30 dest="columns",
31 help="comma-separated list of columns to read (0 equals time stamps and is read always)",
32 required=True)
33 parser.add_argument("-o",
34 "--output",
35 dest="output",
36 help="Pick plot variant",
37 choices=[ "pdf", "png", "csv" ],
38 required=True)
39 parser.add_argument("-pt",
40 "--plot-type",
41 dest="plot_type",
42 help="Pick plot variant (ignored for csv)",
43 choices=[ "scatter", "step" ],
44 required=True)
45 parser.add_argument("-minmax",
46 "--min-max",
47 dest="minmax",
48 action="store_true",
49 default=False,
50 help="Write min-max bars" )
51 args = parser.parse_args()
52
53 columns = [ int(x) for x in args.columns.split(",") ]
54 print( "Extract columns {}".format(columns) )
55
56 if args.output=="pdf" or args.output=="png":
57 plt.clf()
58
59 #
60 # Every entry of interest is characterised through a time stamp and a
61 # measurement output from Peano. Measureemnt outputs always tell you
62 # the (average) value, the min value, the max value and the number of
63 # samples.
64 #
65 timestamps = []
66 values = []
67 min_values = []
68 max_values = []
69 samples = []
70 for column in columns:
71 #
72 # Read input file
73 #
74 input_file = open( args.file, "r" )
75 metric = None
76 for line in input_file:
77 if metric==None:
78 metric = line.split( "," )[column]
79 print( "parse metric {}".format(metric) )
80 elif line.strip()!="":
81 time_stamp = float(line.split( "," )[0])
82 token = line.split( "," )[column].strip()
83 if token!="":
84 timestamps.append(time_stamp )
85 value = float( token.split("(")[1].split("/")[0])
86 values.append( value )
87 min_values.append( float( token.split("/")[1]) )
88 max_values.append( float( token.split("/")[2]) )
89 samples.append( float( token.split("/#")[1].split(")")[0]) )
90 elif args.plot_type=="step" and len(timestamps)>0:
91 timestamps.append(time_stamp )
92 values.append( values[-1] )
93 min_values.append( min_values[-1] )
94 max_values.append( max_values[-1] )
95 samples.append( samples[-1] )
96
97 #
98 # Dump csv
99 #
100 if args.output=="csv":
101 output_file = open( args.file + "-column-" + str(column) + ".csv", "w" )
102 output_file.write( "time, value, min, max, count\n" )
103 for data in zip(timestamps,values,min_values,max_values,samples):
104 entries_per_row = 5
105 for i in range(0,entries_per_row-1):
106 output_file.write( str(data[i]) )
107 output_file.write( "," )
108 output_file.write( str(data[entries_per_row-1]) )
109 output_file.write( "\n" )
110 if (args.output=="pdf" or args.output=="png") and args.plot_type=="step":
111 assert len(timestamps)==len(values)
112 colour = columns.index(column) % len(Colours)
113 if args.minmax:
114 for i in zip(timestamps,min_values,max_values):
115 plt.plot( [i[0],i[0]], [i[1],i[2]], color=Colours[colour] )
116 plt.plot( timestamps, values, color=Colours[colour], alpha=0.5, label=metric )
117 if (args.output=="pdf" or args.output=="png") and args.plot_type=="scatter":
118 assert len(timestamps)==len(values)
119 colour = columns.index(column) % len(Colours)
120 if args.minmax:
121 for i in zip(timestamps,min_values,max_values):
122 plt.plot( [i[0],i[0]], [i[1],i[2]], color=Colours[colour] )
123 plt.scatter( timestamps, values, color=Colours[colour], alpha=0.5, label=metric )
124
125 if args.output=="pdf":
126 plt.legend()
127 plt.savefig( args.file + ".pdf" )
128 if args.output=="png":
129 plt.legend()
130 plt.savefig( args.file + ".png" )