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