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