Ran update-copyright.py
[hooke.git] / hooke / plugin / superimpose.py
1 # Copyright (C) 2008-2012 Massimo Sandal <devicerandom@gmail.com>
2 #                         W. Trevor King <wking@tremily.us>
3 #
4 # This file is part of Hooke.
5 #
6 # Hooke is free software: you can redistribute it and/or modify it under the
7 # terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option) any
9 # later version.
10 #
11 # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY
12 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13 # A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
14 # details.
15 #
16 # You should have received a copy of the GNU Lesser General Public License
17 # along with Hooke.  If not, see <http://www.gnu.org/licenses/>.
18
19 from ..libhooke import WX_GOOD
20 import wxversion
21 wxversion.select(WX_GOOD)
22 from wx import PostEvent
23 from numpy import arange, mean
24
25 from .. import curve as lhc
26
27 class superimposeCommands(object):
28
29     def _plug_init(self):
30         self.imposed=[]
31
32     def do_selimpose(self,args):
33         '''
34         SELIMPOSE (superimpose.py plugin)
35         Hand-selects the curve portion to superimpose
36         '''
37         #fixme: set to superimpose should be in args
38
39         if args=='clear':
40             self.imposed=[]
41             return
42
43         current_set=1
44
45         points=self._measure_two_points()
46         boundaries=[points[0].index, points[1].index]
47         boundaries.sort()
48
49         theplot=self.plots[0]
50         #append the selected section
51         self.imposed.append([])
52         self.imposed[-1].append(theplot.vectors[1][0][boundaries[0]:boundaries[1]]) #x
53         self.imposed[-1].append(theplot.vectors[1][1][boundaries[0]:boundaries[1]]) #y
54
55         #align X first point
56         self.imposed[-1][0] = [item-self.imposed[-1][0][0] for item in self.imposed[-1][0]]
57         #align Y first point
58         self.imposed[-1][1] = [item-self.imposed[-1][1][0] for item in self.imposed[-1][1]]
59
60     def do_plotimpose(self,args):
61         '''
62         PLOTIMPOSE (sumperimpose.py plugin)
63         plots superimposed curves
64         '''
65         imposed_object=lhc.PlotObject()
66         imposed_object.vectors=self.imposed
67         print 'Plotting',len(imposed_object.vectors),'imposed curves'
68
69         imposed_object.normalize_vectors()
70
71         imposed_object.units=self.plots[0].units
72         imposed_object.title='Imposed curves'
73         imposed_object.destination=1
74
75         plot_graph=self.list_of_events['plot_graph']
76         PostEvent(self.frame,plot_graph(plots=[imposed_object]))
77
78     def do_plotavgimpose(self,args):
79         '''
80         PLOTAVGIMPOSE (superimpose.py plugin)
81         Plots the average of superimposed curves using a running window
82         '''
83         step=(-5*(10**-10))
84         #find extension of each superimposed curve
85         min_x=[]
86         for curve in self.imposed:
87             min_x.append(min(curve[0]))
88
89         #find minimum extension
90         min_ext_limit=max(min_x)
91
92         x_avg=arange(step,min_ext_limit,step)
93         y_avg=[]
94         for value in x_avg:
95             to_avg=[]
96             for curve in self.imposed:
97                 for xvalue, yvalue in zip(curve[0],curve[1]):
98                     if xvalue >= (value+step) and xvalue <= (value-step):
99                         to_avg.append(yvalue)
100             y_avg.append(mean(to_avg))
101
102         print 'len x len y'
103         print len(x_avg), len(y_avg)
104         print y_avg
105
106         avg_object=lhc.PlotObject()
107         avg_object.vectors=[[x_avg, y_avg]]
108         avg_object.normalize_vectors()
109         avg_object.units=self.plots[0].units
110         avg_object.title="Average curve"
111         avg_object.destination=1
112
113         plot_graph=self.list_of_events['plot_graph']
114         PostEvent(self.frame,plot_graph(plots=[avg_object]))