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
17Markers = [
18 "v",
19 "^",
20 "x",
21 "o",
22 "<",
23 ">"
24 ]
25
26if __name__ == "__main__":
27 """!
28
29 Script to postprocess statistics outcomes
30
31 This script can produce two types of data: a CSV file which contains only
32 specific data (it then serves as a filter to a bigger CSV file), or a plot
33 of specific data. For the plot, we can either dump data as scatter plot,
34 or we can use historic data. After all, all data are time series, so we
35 end up with some kind of step function plot for the latter.
36
37
38 """
39 parser = argparse.ArgumentParser(description="""
40Peano 4 statistics postprocessing
41
42Plotter for statistics over time. The plotter works only for numerical
43data dumped as four tuple over time. Use inspect-statistics.py first
44to see what the indices of the individual column are and what semantics
45they have.
46
47""")
48 parser.add_argument("file", help="filename of examine")
49 parser.add_argument("-c",
50 "--columns",
51 dest="columns",
52 help="comma-separated list of columns to read (0 equals time stamps and is read always)",
53 required=True)
54 parser.add_argument("-o",
55 "--output",
56 dest="output",
57 help="Pick plot variant",
58 choices=[ "pdf", "png", "csv" ],
59 required=True)
60 parser.add_argument("-pt",
61 "--plot-type",
62 dest="plot_type",
63 help="Pick plot variant (ignored for csv)",
64 choices=[ "scatter", "step" ],
65 required=True)
66 parser.add_argument("-minmax",
67 "--min-max",
68 dest="minmax",
69 action="store_true",
70 default=False,
71 help="Write min-max bars" )
72 parser.add_argument("-v",
73 "--verbose",
74 dest="verbose",
75 action="store_true",
76 default=False,
77 help="Verbose" )
78 args = parser.parse_args()
79
80 columns = [ int(x) for x in args.columns.split(",") ]
81 print( "Extract columns {}".format(columns) )
82
83 if args.output=="pdf" or args.output=="png":
84 plt.clf()
85
86 for column in columns:
87 #
88 # Every entry of interest is characterised through a time stamp and a
89 # measurement output from Peano. Measureemnt outputs always tell you
90 # the (average) value, the min value, the max value and the number of
91 # samples.
92 #
93 timestamps = []
94 values = []
95 min_values = []
96 max_values = []
97 samples = []
98
99 #
100 # Read input file
101 #
102 input_file = open( args.file, "r" )
103 metric = None
104 for line in input_file:
105 if metric==None:
106 # If metric still equals None, then we are in the very
107 # first line, and we use this first line to extract the
108 # metric from the input file
109 metric = line.split( "," )[column]
110 print( "parse metric {}".format(metric) )
111 elif line.strip()!="":
112 time_stamp = float(line.split( "," )[0])
113 token = line.split( "," )[column].strip()
114 if len(timestamps)>0 and time_stamp<timestamps[-1]:
115 print( "data consistency error for data at {}, as last time stamp had been {}".format(time_stamp,timestamps[-1]) )
116 if token!="":
117 timestamps.append(time_stamp )
118 value = float( token.split("(")[1].split("/")[0])
119 values.append( value )
120 min_values.append( float( token.split("/")[1]) )
121 max_values.append( float( token.split("/")[2]) )
122 samples.append( float( token.split("/#")[1].split(")")[0]) )
123 elif args.plot_type=="step" and len(timestamps)>0:
124 timestamps.append(time_stamp )
125 values.append( values[-1] )
126 min_values.append( min_values[-1] )
127 max_values.append( max_values[-1] )
128 samples.append( samples[-1] )
129
130 #
131 # Dump csv
132 #
133 if args.verbose:
134 print( "dump {} data points for label {}".format(len(timestamps),metric) )
135 if args.output=="csv":
136 output_file = open( args.file + "-column-" + str(column) + ".csv", "w" )
137 output_file.write( "time, value, min, max, count\n" )
138 for data in zip(timestamps,values,min_values,max_values,samples):
139 entries_per_row = 5
140 for i in range(0,entries_per_row-1):
141 output_file.write( str(data[i]) )
142 output_file.write( "," )
143 output_file.write( str(data[entries_per_row-1]) )
144 output_file.write( "\n" )
145 if (args.output=="pdf" or args.output=="png") and args.plot_type=="step":
146 assert len(timestamps)==len(values)
147 colour = columns.index(column) % len(Colours)
148 if args.minmax:
149 for i in zip(timestamps,min_values,max_values):
150 plt.plot( [i[0],i[0]], [i[1],i[2]], color=Colours[colour] )
151 plt.plot( timestamps, values, color=Colours[colour], alpha=0.5, label=metric )
152 if (args.output=="pdf" or args.output=="png") and args.plot_type=="scatter":
153 assert len(timestamps)==len(values)
154 colour = columns.index(column) % len(Colours)
155 if args.minmax:
156 for i in zip(timestamps,min_values,max_values):
157 plt.plot( [i[0],i[0]], [i[1],i[2]], color=Colours[colour] )
158 plt.scatter( timestamps, values, marker=Markers[colour], color=Colours[colour], alpha=0.5, label=metric )
159
160 plt.rc('legend', fontsize = 6)
161
162 if args.output=="pdf":
163 plt.legend()
164 plt.savefig( args.file + ".pdf" )
165 if args.output=="png":
166 plt.legend()
167 plt.savefig( args.file + ".png" )