Peano
csv_to_plot.py
Go to the documentation of this file.
1 import matplotlib.pyplot as plt
2 import csv
3 import argparse
4 
5 "Written by Marc Marot-Lassauzaie, I use this file to convert data from the .csv files generated by the parsers to somewhat legible plots"
6 
7 
8 """
9 parse arguments to get details about which data should be extracted
10 """
11 
12 parser = argparse.ArgumentParser(description='Script to convert CSV generated by ExaHyPE2 executions into plots')
13 parser.add_argument("--f", dest="file_name", type=str, required=True, help="Name of the file to be checked, does not need to contain '.csv'" )
14 parser.add_argument("--v", dest="v", type=int, required=True, help="Index of the variable to be plotted" )
15 parser.add_argument("--name", dest="variable_name", type=str, default=None, help="Name of the variable to be plotted" )
16 parser.add_argument("--tmax", dest="t_max", type=float, default=None, help="Minimal time for which data should be plotted" )
17 parser.add_argument("--tmin", dest="t_min", type=float, default=None, help="Maximal time for which data should be plotted" )
18 parser.add_argument("--x", dest="x", type=float, default=None, help="x position of tracer which should be considered" )
19 parser.add_argument("--y", dest="y", type=float, default=None, help="y position of tracer which should be considered" )
20 parser.add_argument("--z", dest="z", type=float, default=None, help="z position of tracer which should be considered" )
21 args = parser.parse_args()
22 
23 var_name = "Value" if args.variable_name==None else args.variable_name
24 
25 
26 """
27 Extract some metadata about the positions of the various information in the csv file.
28 These should always be the same but it's safer to check.
29 """
30 try:
31  csvData = list(csv.reader(open(args.file_name,'r'), delimiter=','))
32 except FileNotFoundError:
33  try:
34  csvData = list(csv.reader(open(args.file_name + '.csv','r'), delimiter=','))
35  except:
36  print("the file you have specified does not exist, please make sure that it does in the specified location")
37  exit()
38 
39 
40 timeIndex = csvData[0].index(' t')
41 dataIndex = csvData[0].index(" data ") + args.v
42 firstNumberIndex = csvData[0].index('number(0)')
43 secondNumberIndex = csvData[0].index(' number(1)')
44 if ' x(2)' in csvData[0]:
45  positionIndexes = [csvData[0].index(' x(0)'), csvData[0].index(' x(1)'), csvData[0].index(' x(2)')]
46 else:
47  positionIndexes = [csvData[0].index(' x(0)'), csvData[0].index(' x(1)')]
48 
49 """
50 remove first row which should only contain metadata
51 """
52 csvData = csvData[1:-1]
53 
54 
55 """
56 Filter out only the data from tracers at the specified positions
57 """
58 
60  value = True
61  if args.t_min!=None:
62  value = value and float(a[timeIndex])>=args.t_min
63  if args.t_max!=None:
64  value = value and float(a[timeIndex])<=args.t_max
65  if args.x!=None:
66  value = value and float(a[positionIndexes[0]])==args.x
67  if args.y!=None:
68  value = value and float(a[positionIndexes[1]])==args.y
69  if args.z!=None:
70  value = value and float(a[positionIndexes[2]])==args.z
71  return value
72 
73 csvData = [data for data in csvData if position_check(data)]
74 
75 
76 """
77 Check how many tracers fit the filter by extracting the total numbers of number(0) and
78 number(1) tuples that exist with the filter.
79 The {number(0), number(1)} tuples should be unique for each tracer, therefore if there
80 exists more than one value for either of these after filtering, there are multiple
81 tracers that correspond to the filter.
82 In the same way if there are none, none of the tracers correspond to the filter.
83 """
84 checkFirstIndexLength = set([data[firstNumberIndex] for data in csvData])
85 checksecondIndexLength = set([data[secondNumberIndex] for data in csvData])
86 
87 if len(checkFirstIndexLength)==0:
88  raise Exception("Specification was valid for none of the tracers, please check that a tracer exists at the specified position")
89 elif len(checkFirstIndexLength)>1 or len(checksecondIndexLength) >1:
90  raise Exception("Specification was valid for more than one tracer, please clarify which you want through use of positions x, y and z")
91 
92 
93 """
94 Extract only the data from time and from the required value
95 """
96 
97 dt = []
98 y = []
99 
100 for row in csvData:
101  dt.append(float(row[timeIndex]))
102  y.append(float(row[dataIndex]))
103 
104 """
105 Sort this data in the direction of increasing time
106 """
107 
108 dt, y = zip(*sorted(zip(dt,y)))
109 
110 
111 """
112 Plot the data
113 """
114 
115 plt.plot(dt, y, color = 'b', linestyle = 'dashed',
116  marker = 'o',label = var_name + " as a function of time")
117 
118 plt.xticks(rotation = 25)
119 plt.xlabel('t')
120 plt.ylabel(var_name)
121 plt.title(var_name + ' as a function of time', fontsize = 20)
122 plt.grid()
123 plt.legend()
124 plt.show()
def position_check(a)
Definition: csv_to_plot.py:59