41083f68651f0f9a03d7fd16b856d19e6c950759
[hooke.git] / hooke / plugin / superimpose.py
1 from libhooke import WX_GOOD
2 import wxversion
3 wxversion.select(WX_GOOD)
4 from wx import PostEvent
5
6 import libhookecurve as lhc
7 from numpy import arange, mean
8
9 class superimposeCommands(object):
10
11     def _plug_init(self):
12         self.imposed=[]
13
14     def do_selimpose(self,args):
15         '''
16         SELIMPOSE (superimpose.py plugin)
17         Hand-selects the curve portion to superimpose
18         '''
19         #fixme: set to superimpose should be in args
20
21         if args=='clear':
22             self.imposed=[]
23             return
24
25         current_set=1
26
27         points=self._measure_two_points()
28         boundaries=[points[0].index, points[1].index]
29         boundaries.sort()
30
31         theplot=self.plots[0]
32         #append the selected section
33         self.imposed.append([])
34         self.imposed[-1].append(theplot.vectors[1][0][boundaries[0]:boundaries[1]]) #x
35         self.imposed[-1].append(theplot.vectors[1][1][boundaries[0]:boundaries[1]]) #y
36
37         #align X first point
38         self.imposed[-1][0] = [item-self.imposed[-1][0][0] for item in self.imposed[-1][0]]
39         #align Y first point
40         self.imposed[-1][1] = [item-self.imposed[-1][1][0] for item in self.imposed[-1][1]]
41
42     def do_plotimpose(self,args):
43         '''
44         PLOTIMPOSE (sumperimpose.py plugin)
45         plots superimposed curves
46         '''
47         imposed_object=lhc.PlotObject()
48         imposed_object.vectors=self.imposed
49         print 'Plotting',len(imposed_object.vectors),'imposed curves'
50
51         imposed_object.normalize_vectors()
52
53         imposed_object.units=self.plots[0].units
54         imposed_object.title='Imposed curves'
55         imposed_object.destination=1
56
57         plot_graph=self.list_of_events['plot_graph']
58         PostEvent(self.frame,plot_graph(plots=[imposed_object]))
59
60     def do_plotavgimpose(self,args):
61         '''
62         PLOTAVGIMPOSE (superimpose.py plugin)
63         Plots the average of superimposed curves using a running window
64         '''
65         step=(-5*(10**-10))
66         #find extension of each superimposed curve
67         min_x=[]
68         for curve in self.imposed:
69             min_x.append(min(curve[0]))
70
71         #find minimum extension
72         min_ext_limit=max(min_x)
73
74         x_avg=arange(step,min_ext_limit,step)
75         y_avg=[]
76         for value in x_avg:
77             to_avg=[]
78             for curve in self.imposed:
79                 for xvalue, yvalue in zip(curve[0],curve[1]):
80                     if xvalue >= (value+step) and xvalue <= (value-step):
81                         to_avg.append(yvalue)
82             y_avg.append(mean(to_avg))
83
84         print 'len x len y'
85         print len(x_avg), len(y_avg)
86         print y_avg
87
88         avg_object=lhc.PlotObject()
89         avg_object.vectors=[[x_avg, y_avg]]
90         avg_object.normalize_vectors()
91         avg_object.units=self.plots[0].units
92         avg_object.title="Average curve"
93         avg_object.destination=1
94
95         plot_graph=self.list_of_events['plot_graph']
96         PostEvent(self.frame,plot_graph(plots=[avg_object]))