Python wrapper for running instances of trisurf-ng
Samo Penic
2017-09-17 a4f8eb21f85326596a491b39e0330882ee8017f0
commit | author | age
d2f7b0 1 from . import trisurf
SP 2
3
29660c 4 def analysis(*args):
c1504d 5     """Decorator for adding the analysis functions to function lists"""
SP 6     def analysis_decorator(analysis_function):
7         trisurf._analysis_list[analysis_name]=analysis_function
8         def wrapper(*args, **kwargs):
9             analysis_function(*args,**kwargs)
10         return wrapper
29660c 11     if len(args) == 1 and callable(args[0]): #no arguments
SP 12         analysis_name=args[0].__name__
13         return analysis_decorator(args[0])
14     else:
15         analysis_name=args[0]
16         return analysis_decorator
c1504d 17
SP 18
29660c 19 @analysis
d2f7b0 20 def demo(run, **kwargs):
SP 21     host=kwargs.get('host', None)
22     print("Demo analysis")
23     print("Analysis on host "+host['name']+" for run "+run.Dir.fullpath()+" completed")
24     print("here comes info on the run variable:")
25     print(run)
26     print("here comes info on the host variable:")
27     print(host)
28     print("here comes info on the args variable:")
29     print(kwargs.get('args',None))
30
31
d55834 32 # can be wrapped to specify scalar_field)
c1504d 33 @analysis('plotrunningavginteractive')
d55834 34 def plotrunningavginteractive(run, scalar_field='vertices_idx', **kwargs):
d2f7b0 35     import matplotlib.pyplot as plt
SP 36     from trisurf import VTKRendering as vtk
37     import math
38     from multiprocessing import Process
a4f8eb 39     table=trisurf.Statistics(run.Dir.fullpath(),filename='poststat.csv').getTable()
d2f7b0 40     def running_avg(col):
SP 41         import numpy as np
42         avg=[]    
43         for i in range(0,len(col)):
44             avg.append(np.average(col[:-i]))
45         return avg
46     def spawned_viewer(n):
d55834 47         vtk.Renderer(kwargs.get('args', None),kwargs.get('host',None),run, timestep=n,scalar_field=scalar_field)
d2f7b0 48
SP 49     fig=plt.figure(1)
50     ra=running_avg(table['hbar'])
51     l=len(table['hbar'])
52     plt.plot(ra)
53     plt.title('Running average')
54     plt.ylabel('1/n sum_i=niter^n(hbar_i)')
55     plt.xlabel('n')
56     def onclick(event):
57         #print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata))
58         p=Process(target=spawned_viewer, args=(l-math.floor(event.xdata)-1,))
59         p.start()
60     cid = fig.canvas.mpl_connect('button_press_event', onclick)
61     plt.show()
62     plt.close(1)
63
64
65 # -------------------------------
66 # these functions should be wrapped
67 # -------------------------------
c1504d 68 @analysis('plotColumnFromPostProcess')
a4f8eb 69 def plotColumnFromPostProcess(run, filename='poststat.csv', column='hbar', **kwargs):
d2f7b0 70     import matplotlib.pyplot as plt
SP 71
72     def smooth(y, box_pts):
73         import numpy as np
74         box = np.ones(box_pts)/box_pts
75         y_smooth = np.convolve(y, box, mode='same')
76         return y_smooth
77     table=trisurf.Statistics(run.Dir.fullpath(),filename=filename).getTable()
78     plt.plot(table[column], '.')
79     plt.title(run.Dir.fullpath())
80     plt.xlabel('Iteration')
81     plt.ylabel(column)
82     smooth_window=10
83     smoothed=smooth(table[column],smooth_window)
84     plt.plot(tuple(range(int(smooth_window/2),len(smoothed)-int(smooth_window/2))),smoothed[int(smooth_window/2):-int(smooth_window/2)])
85     plt.show()
86     print
87     #if return False or no return statement, the analysis will continue with next running instance in the list. if return True, the analysis will stop after this run.
88     return False
89
90