Skip to content

Commit 4132fa9

Browse files
clevelamCleveland
and
Cleveland
authored
Add a pre and post parser hook to the interactive parser (#46)
* add an optional pre and post parser hook and and add_parser_arg hook * update arrivial plotting * fix font size and add pre/post/add_parser_args tests * fix type conversion error * cleanup testing error * more scale factor cleanup * fix more numpy min max type errors * fix min max type casting * revert changes * alternative min max calls for dumps * prevent agressive numpy data type casting * more numpy1-numpy2 min/max fixes * add a diagnostic print * fix ambiguous 1d/2d/3d dump parsing --------- Co-authored-by: Cleveland <[email protected]>
1 parent 86a066c commit 4132fa9

18 files changed

+291
-77
lines changed

opppy/dump_utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def thread_all(file_name, key_words, result_d):
515515
result_d[file_name.split('/')[-1]] = opppy_parser.build_data_dictionary(file_name,key_words)
516516
print("Number of threads used for processing: ",nthreads)
517517
for stride in range(math.ceil(float(total)/float(nthreads))):
518-
files = dump_files[nthreads*stride:min(nthreads*(stride+1),len(dump_files))]
518+
files = dump_files[nthreads*stride:array([nthreads*(stride+1),len(dump_files)]).min()]
519519
with Manager() as manager:
520520
result_d = manager.dict()
521521
threads = []

opppy/interactive_utils.py

+102-30
Large diffs are not rendered by default.

opppy/plot_dictionary.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def plot_dict(self, args, dictionaries, data_names):
146146

147147
print('# ', header_xlabel, header_ylabel, file=outputfile)
148148
for x_value, y_value in zip(x, y):
149-
outstring = "%.9e"%(x_value*scale_x)+" %.9e"%(y_value*scale_y)+"\n"
149+
outstring = "%.9e"%(x_value)+" %.9e"%(y_value)+"\n"
150150
outputfile.write(outstring)
151151
data_name = ''
152152
if(args.data_file_name is not None):
@@ -199,12 +199,13 @@ def plot_dict(self, args, dictionaries, data_names):
199199
if(len(x)>0):
200200
last_x.append(x[-1])
201201
last_y.append(y[-1])
202+
203+
if(args.font_size is not None):
204+
PyPloter.rcParams.update({'font_size':args.font_size})
202205

203206
if(args.plot_max):
204207
last_y.append(sum(sorted(y,reverse=True)[0:2])/3.0)
205208
last_x.append(x[-1])
206-
elif(args.plot_arrival):
207-
continue
208209
elif(data_line_color != '' and data_line_type != ''):
209210
PyPloter.plot(x,y,label = data_name, linestyle = data_line_type, color = data_line_color)
210211
elif(data_line_color != '' ):
@@ -214,17 +215,22 @@ def plot_dict(self, args, dictionaries, data_names):
214215
else:
215216
PyPloter.plot(x,y,label = data_name)
216217

218+
interp_x = x[0]
217219
if(args.plot_arrival):
220+
args.plot_arrival = False
218221
last_x = x[0]
219222
last_y = y[0]
220223
for x_value, y_value in zip(x,y):
221224
if(y_value>args.y_exceeds_value):
225+
args.plot_arrival = True
222226
dy = (args.y_exceeds_value - last_y)/(y_value - last_y)
223227
interp_x = last_x + dy*(x_value-last_x)
224-
print(data_name, "first exceeds ", y_exceeds_value, " at ", interp_x)
228+
print(data_name, "first exceeds ", args.y_exceeds_value, " at ", interp_x)
225229
break
226230
last_x = x_value
227231
last_y = y_value
232+
if(not args.plot_arrival):
233+
print("WARNING: y never exceeds y_exceeds_value="+str(args.y_exceeds_value));
228234

229235
if(args.find_max_y):
230236
print(data_name, "max y value ", x[y.index(max(y))], max(y))
@@ -233,9 +239,13 @@ def plot_dict(self, args, dictionaries, data_names):
233239

234240
if(args.last_point_only):
235241
PyPloter.plot(last_x, last_y, label = data_name)
236-
elif(args.plot_arrival or args.plot_max):
237-
print(last_x, last_y)
238-
PyPloter.plot(last_x, last_y, label = data_name)
242+
elif(args.plot_arrival):
243+
color=PyPloter.gca().lines[-1].get_color()
244+
PyPloter.plot(interp_x, args.y_exceeds_value, linestyle=None, color=color, marker='o', label = data_name + " exceeds y="+str(args.y_exceeds_value)+" @ x="+str(interp_x))
245+
elif(args.plot_max):
246+
print("max y= "+str(last_y)+" @ x="+str(last_x))
247+
color=PyPloter.gca().lines[-1].get_color()
248+
PyPloter.plot(last_x, last_y, linestyle=None, color=color, marker='x', label = data_name + " max y="+str(last_y)+" @ x="+str(last_x))
239249

240250
if(args.x_label is not None):
241251
PyPloter.xlabel(args.x_label)

opppy/plot_dump_dictionary.py

+43-27
Original file line numberDiff line numberDiff line change
@@ -773,17 +773,23 @@ def init_lines():
773773
if(args.data_file_name is not None):
774774
outputfile = open(args.data_file_name+'_'+filename.split('/')[-1]+'.'+re.sub(r'[^\w]','',yname)+'.dat', 'w')
775775

776-
xmin=(np.array(series_data[0][xname])*scale_x).min()
777-
xmax=(np.array(series_data[0][xname])*scale_x).max()
778-
ymin=(np.array(series_data[0][yname])*scale_y).min()
779-
ymax=(np.array(series_data[0][yname])*scale_y).max()
776+
xmin=(np.array(series_data[0][xname])).min()
777+
xmax=(np.array(series_data[0][xname])).max()
778+
ymin=(np.array(series_data[0][yname])).min()
779+
ymax=(np.array(series_data[0][yname])).max()
780780
for data, index_value in zip(series_data, series_pair.index[index_key]):
781-
x = np.array(data[xname])*scale_x
782-
y = np.array(data[yname])*scale_y
783-
xmin = min(x.min(),xmin)
784-
xmax = max(x.max(),xmax)
785-
ymin = min(y.min(),ymin)
786-
ymax = max(y.max(),ymax)
781+
x = np.array(data[xname])
782+
y = np.array(data[yname])
783+
xmin = np.array([x.min(),xmin]).min()
784+
xmax = np.array([x.max(),xmax]).max()
785+
ymin = np.array([y.min(),ymin]).min()
786+
ymax = np.array([y.max(),ymax]).max()
787+
x = x*scale_x
788+
y = y*scale_y
789+
xmin = xmin*scale_x
790+
xmax = xmax*scale_x
791+
ymin = ymin*scale_y
792+
ymax = ymax*scale_y
787793
if(args.data_file_name is not None):
788794
if(args.x_label is not None):
789795
header_xlabel = args.x_label
@@ -797,7 +803,7 @@ def init_lines():
797803

798804
print('# ', index_key, header_xlabel, header_ylabel, file=outputfile)
799805
for x_value, y_value in zip(x, y):
800-
outstring = "%.9e"%(index_value)+" %.9e"%(x_value*scale_x)+" %.9e"%(y_value*scale_y)+"\n"
806+
outstring = "%.9e"%(index_value)+" %.9e"%(x_value)+" %.9e"%(y_value)+"\n"
801807
outputfile.write(outstring)
802808
if(args.data_file_name is not None):
803809
print("data saved as -- "+args.data_file_name+'_'+filename.split('/')[-1]+'.'+re.sub(r'[^\w]','',yname)+'.dat')
@@ -1003,28 +1009,38 @@ def init_contour():
10031009
if(args.data_file_name is not None):
10041010
outputfile = open(args.data_file_name+'_'+filename.split('/')[-1]+'.'+re.sub(r'[^\w]','',yname)+'.dat', 'w')
10051011

1006-
vmin=(np.array(series_data[0][dname])*args.scale_value).min()
1007-
vmax=(np.array(series_data[0][dname])*args.scale_value).max()
1008-
xmin=(np.array(series_data[0][xname])*args.scale_x).min()
1009-
xmax=(np.array(series_data[0][xname])*args.scale_x).max()
1010-
ymin=(np.array(series_data[0][yname])*args.scale_y).min()
1011-
ymax=(np.array(series_data[0][yname])*args.scale_y).max()
1012+
vmin=(np.array(series_data[0][dname])).min()
1013+
vmax=(np.array(series_data[0][dname])).max()
1014+
xmin=(np.array(series_data[0][xname])).min()
1015+
xmax=(np.array(series_data[0][xname])).max()
1016+
ymin=(np.array(series_data[0][yname])).min()
1017+
ymax=(np.array(series_data[0][yname])).max()
10121018
bias = 0.0
10131019
for data, index_value in zip(series_data, series_pair.index[index_key]):
1014-
v = np.array(data[dname])*args.scale_value
1020+
v = np.array(data[dname])
10151021
if(args.log_scale):
10161022
bias = v.min()
10171023
bias = 0.0 if bias>0.0 else abs(bias)
10181024
v = np.array([ [log10(val+bias) if (val+bias)>0.0 else 0.0
10191025
for val in vals] for vals in v])
1020-
x = np.array(data[xname])*args.scale_x
1021-
y = np.array(data[yname])*args.scale_y
1022-
vmin = min(v.min(),vmin)
1023-
vmax = max(v.max(),vmax)
1024-
xmin = min(x.min(),xmin)
1025-
xmax = max(x.max(),xmax)
1026-
ymin = min(y.min(),ymin)
1027-
ymax = max(y.max(),ymax)
1026+
x = np.array(data[xname])
1027+
y = np.array(data[yname])
1028+
vmin = np.array([v.min(),vmin]).min()
1029+
vmax = np.array([v.max(),vmax]).max()
1030+
xmin = np.array([x.min(),xmin]).min()
1031+
xmax = np.array([x.max(),xmax]).max()
1032+
ymin = np.array([y.min(),ymin]).min()
1033+
ymax = np.array([y.max(),ymax]).max()
1034+
# apply scaling
1035+
v = v*args.scale_value
1036+
x = x*args.scale_x
1037+
y = y*args.scale_y
1038+
vmin = vmin*args.scale_value
1039+
vmax = vmax*args.scale_value
1040+
xmin = xmin*args.scale_x
1041+
xmax = xmax*args.scale_x
1042+
ymin = ymin*args.scale_y
1043+
ymax = ymax*args.scale_y
10281044
if(args.data_file_name is not None):
10291045
if(args.x_label is not None):
10301046
header_xlabel = args.x_label
@@ -1038,7 +1054,7 @@ def init_contour():
10381054

10391055
print('# ', index_key, header_xlabel, header_ylabel, dname, file=outputfile)
10401056
for x_value, y_value, v_value in zip(x, y, v):
1041-
outstring = "%.9e"%(index_value)+" %.9e"%(x_value*args.scale_x)+" %.9e"%(y_value*args.scale_y)+" %.9e"%(v_value*args.scale_value)+"\n"
1057+
outstring = "%.9e"%(index_value)+" %.9e"%(x_value)+" %.9e"%(y_value)+" %.9e"%(v_value)+"\n"
10421058
outputfile.write(outstring)
10431059
if(args.data_file_name is not None):
10441060
print("data saved as -- "+args.data_file_name+'_'+filename.split('/')[-1]+'.'+re.sub(r'[^\w]','',dname)+'.dat')

opppy/plotting_help.py

+2
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def add_plot_options(parser):
276276
parser.add_argument('-lpo','--last_point_only', dest='last_point_only', help='only plot the last data point', nargs='?', type=bool, default=False, const=True )
277277
parser.add_argument('-ltv','--last_time_value', dest='last_time_value', help='only plot the last time value', nargs=1, type=float)
278278
parser.add_argument('-fr','--figure_resolution', dest='figure_resolution', help='figure resolution in dpi', nargs='?', type=float, default=300.0)
279+
parser.add_argument('-fs','--font_size', dest='font_size', help='set the plot font size', type=float, default=None, nargs="?")
279280
parser.add_argument('-yev','--y_exceeds_value', dest='y_exceeds_value', help='y arrival value', nargs=1, type=float, default=0.0)
280281
parser.add_argument('-sy','--scale_y', dest='scale_y', help='scale y values', type=float, default=[], action="append")
281282
parser.add_argument('-sx','--scale_x', dest='scale_x', help='scale x values', type=float, default=[], action="append")
@@ -301,6 +302,7 @@ def add_2d_plot_options(parser):
301302
parser.add_argument('-xlab','--xlab', dest='x_label', help='x axis label')
302303
parser.add_argument('-ylab','--ylabl', dest='y_label', help='y axis label')
303304
parser.add_argument('-fr','--figure_resolution', dest='figure_resolution', help='figure resolution in dpi', nargs='?', type=float, default=300.0)
305+
parser.add_argument('-fs','--font_size', dest='font_size', help='set the plot font size', type=float, default=None, nargs="?")
304306
parser.add_argument('-sv','--scale_value', dest='scale_value', help='scale values', type=float, default=1.0)
305307
parser.add_argument('-sx','--scale_x', dest='scale_x', help='scale x values', type=float, default=1.0)
306308
parser.add_argument('-sy','--scale_y', dest='scale_y', help='scale y values', type=float, default=1.0)

tests/example_1d_dump.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
keys: time cell_id x y z density temperature pressure
2+
3+
time: 1.0
4+
5+
cell_id: 1 2 3 4
6+
x: 1 2 3 4 5
7+
y: 1 1 1 1 1
8+
z: 1 1 1 1 1
9+
density: 1.1 1.1 1.1 1.1 1.1
10+
temperature: 3.1 3.1 3.1 3.1 3.1
11+
pressure: 5.1 5.1 5.1 5.1 5.1
12+

tests/example_1d_dump_2.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
keys: time cell_id x y z density temperature pressure
2+
3+
time: 2.0
4+
5+
cell_id: 1 2 3 4 5
6+
x: 1 2 3 4 5
7+
density: 1.2 1.2 1.2 1.2 1.2
8+
temperature: 3.2 3.2 3.2 3.2 3.2
9+
pressure: 5.2 5.2 5.2 5.2 5.2
10+

tests/example_1d_dump_3.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
keys: time cell_id x y z density temperature pressure
2+
3+
time: 3.0
4+
5+
cell_id: 1 2 3 4 5
6+
x: 1 2 3 4 5
7+
density: 1.3 1.3 1.3 1.3 1.3
8+
temperature: 3.3 3.3 3.3 3.3 3.3
9+
pressure: 5.3 5.3 5.3 5.3 5.3
10+

tests/example_2d_dump.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
keys: time cell_id x y z density temperature pressure
2+
3+
time: 1.0
4+
5+
cell_id: 1 2 3 4 5 6 7 8 9 10
6+
x: 1 2 3 4 5 1 2 3 4 5
7+
y: 1 1 1 1 1 2 2 2 2 2
8+
z: 1 1 1 1 1 1 1 1 1 1
9+
density: 1.1 1.1 1.1 1.1 1.1 2.1 2.1 2.1 2.1 2.1
10+
temperature: 3.1 3.1 3.1 3.1 3.1 4.1 4.1 4.1 4.1 4.1
11+
pressure: 5.1 5.1 5.1 5.1 5.1 6.1 6.1 6.1 6.1 6.1
12+

tests/example_2d_dump_2.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
keys: time cell_id x y z density temperature pressure
2+
3+
time: 2.0
4+
5+
cell_id: 1 2 3 4 5 6 7 8 9 10
6+
x: 1 2 3 4 5 1 2 3 4 5
7+
y: 1 1 1 1 1 2 2 2 2 2
8+
density: 1.2 1.2 1.2 1.2 1.2 2.2 2.2 2.2 2.2 2.2
9+
temperature: 3.2 3.2 3.2 3.2 3.2 4.2 4.2 4.2 4.2 4.2
10+
pressure: 5.2 5.2 5.2 5.2 5.2 6.2 6.2 6.2 6.2 6.2
11+

tests/example_2d_dump_3.txt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
keys: time cell_id x y z density temperature pressure
2+
3+
time: 3.0
4+
5+
cell_id: 1 2 3 4 5 6 7 8 9 10
6+
x: 1 2 3 4 5 1 2 3 4 5
7+
y: 1 1 1 1 1 2 2 2 2 2
8+
z: 1 1 1 1 1 1 1 1 1 1
9+
density: 1.3 1.3 1.3 1.3 1.3 2.3 2.3 2.3 2.3 2.3
10+
temperature: 3.3 3.3 3.3 3.3 3.3 4.3 4.3 4.3 4.3 4.3
11+
pressure: 5.3 5.3 5.3 5.3 5.3 6.3 6.3 6.3 6.3 6.3
12+

tests/gold_dumps.p

-161 Bytes
Binary file not shown.

tests/gold_line_data.p

0 Bytes
Binary file not shown.

tests/gold_sub_dumps.p

-93 Bytes
Binary file not shown.

tests/my_test_opppy_dump_parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def build_data_dictionary(self, filename, dump_keys=None):
3232
for line in lines:
3333
for key in keys:
3434
if key in line and len(key) is len(line.split(':')[0]):
35-
data[key] = array(str_vector_to_float_vector(line.strip('\n').split(' ')[1:]))
35+
data[key] = array(str_vector_to_float_vector(line.strip('\n').split(' ')[1:])).astype(float)
3636

3737

3838
# build xy_verts for 2d mesh plotting example
@@ -45,3 +45,4 @@ def build_data_dictionary(self, filename, dump_keys=None):
4545
return data
4646

4747

48+

tests/my_test_opppy_parser.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def __init__(self):
1414
self.file_end_string = None
1515
print("Initializing my_test_opppy_parser")
1616

17+
def add_parser_args(self, parser):
18+
parser.add_argument('-ppt', '--pre_parser_test', dest="pre_parser_test", nargs="?", default=None)
19+
20+
def pre_parse(self, args):
21+
if(args.pre_parser_test is not None):
22+
print("pre_parse hook works: args.pre_parser_test")
23+
else:
24+
print("pre_parse hook works: None")
25+
1726
def parse_cycle_string(self,cycle_string):
1827
# return dictionary of dictionaries
1928
data_dict = {}
@@ -53,8 +62,13 @@ def parse_cycle_string(self,cycle_string):
5362
# append dictionary with multiple entries
5463
data_dict['density'] = density_data
5564

65+
return data_dict
5666

67+
def post_parse(self, args, data):
68+
if(args.pre_parser_test is not None):
69+
data["post_parse_test_"+args.pre_parser_test]=data['test_data1']
70+
else:
71+
data['post_parser_test']=data['test_data1']
5772

5873

59-
return data_dict
60-
74+

tests/my_test_opppy_tally_parser.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ def __init__(self):
2020
self.file_end_string = None
2121
print("Initializing my_test_opppy_tally_parser")
2222

23+
def add_parser_args(self, parser):
24+
parser.add_argument('-ppt', '--pre_parser_test', dest="pre_parser_test", nargs="?", default=None)
25+
26+
def pre_parse(self, args):
27+
if(args.pre_parser_test is not None):
28+
print("pre_parse hook works: args.pre_parser_test")
29+
else:
30+
print("pre_parse hook works: None")
31+
32+
2333
def parse_cycle_string(self,cycle_string):
2434
cycle_data_keys = ['bins','odd_counts','even_counts']
2535
cycle_info_keys = ['time', 'cycle']
@@ -58,4 +68,8 @@ def parse_cycle_string(self,cycle_string):
5868
data_dict['cool_counts'] = counts
5969

6070
return data_dict
61-
71+
72+
def post_parse(self, args, data):
73+
print("Post Parse Test Data keys: ",data.keys())
74+
75+

0 commit comments

Comments
 (0)