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