Python wrapper for running instances of trisurf-ng
Samo Penic
2017-11-28 824b3e562b5e0e7f9450b06a45deabda6ebe20df
commit | author | age
d2f7b0 1 from . import trisurf
SP 2
3
4 def demo(run, **kwargs):
5     host=kwargs.get('host', None)
6     print("Demo analysis")
7     print("Analysis on host "+host['name']+" for run "+run.Dir.fullpath()+" completed")
8     print("here comes info on the run variable:")
9     print(run)
10     print("here comes info on the host variable:")
11     print(host)
12     print("here comes info on the args variable:")
13     print(kwargs.get('args',None))
14
15
d55834 16 # can be wrapped to specify scalar_field)
SP 17 def plotrunningavginteractive(run, scalar_field='vertices_idx', **kwargs):
d2f7b0 18     import matplotlib.pyplot as plt
SP 19     from trisurf import VTKRendering as vtk
20     import math
21     from multiprocessing import Process
22     table=trisurf.Statistics(run.Dir.fullpath(),filename='data_tspoststat.csv').getTable()
23     def running_avg(col):
24         import numpy as np
25         avg=[]    
26         for i in range(0,len(col)):
27             avg.append(np.average(col[:-i]))
28         return avg
29     def spawned_viewer(n):
d55834 30         vtk.Renderer(kwargs.get('args', None),kwargs.get('host',None),run, timestep=n,scalar_field=scalar_field)
d2f7b0 31
SP 32     fig=plt.figure(1)
33     ra=running_avg(table['hbar'])
34     l=len(table['hbar'])
35     plt.plot(ra)
36     plt.title('Running average')
37     plt.ylabel('1/n sum_i=niter^n(hbar_i)')
38     plt.xlabel('n')
39     def onclick(event):
40         #print('button=%d, x=%d, y=%d, xdata=%f, ydata=%f' % (event.button, event.x, event.y, event.xdata, event.ydata))
41         p=Process(target=spawned_viewer, args=(l-math.floor(event.xdata)-1,))
42         p.start()
43     cid = fig.canvas.mpl_connect('button_press_event', onclick)
44     plt.show()
45     plt.close(1)
46
47
48 # -------------------------------
49 # these functions should be wrapped
50 # -------------------------------
51
52 def plotColumnFromPostProcess(run, filename='data_tspoststat.csv', column='hbar', **kwargs):
53     import matplotlib.pyplot as plt
54
55     def smooth(y, box_pts):
56         import numpy as np
57         box = np.ones(box_pts)/box_pts
58         y_smooth = np.convolve(y, box, mode='same')
59         return y_smooth
60     table=trisurf.Statistics(run.Dir.fullpath(),filename=filename).getTable()
61     plt.plot(table[column], '.')
62     plt.title(run.Dir.fullpath())
63     plt.xlabel('Iteration')
64     plt.ylabel(column)
65     smooth_window=10
66     smoothed=smooth(table[column],smooth_window)
67     plt.plot(tuple(range(int(smooth_window/2),len(smoothed)-int(smooth_window/2))),smoothed[int(smooth_window/2):-int(smooth_window/2)])
68     plt.show()
69     print
70     #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.
71     return False
72
73