c79488dd72a53cef199d2904af4bbcd59e8c14b0
[hooke.git] / hooke / plugin / multidistance.py
1 # Copyright (C) 2009-2012 Fabrizio Benedetti <fabrizio.benedetti.82@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 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 hooke.libhooke import WX_GOOD, ClickedPoint
20
21 import wxversion
22 wxversion.select(WX_GOOD)
23 from wx import PostEvent
24 import numpy as np
25 import scipy as sp
26 import copy
27 import os.path
28 import time
29
30 import warnings
31 warnings.simplefilter('ignore',np.RankWarning)
32
33
34 class multidistanceCommands(object):
35
36     def do_multidistance(self,args):
37      '''
38      MULTIDISTANCE
39      multidistance.py
40      Based on the convolution recognition automatically give the distances between the peaks found.
41      The command allow also to remove the unwanted peaks that can be due to interference.
42      When you first issue the command, it will ask for the filename. If you are giving the filename
43      of an existing file, autopeak will resume it and append measurements to it. If you are giving
44      a new filename, it will create the file and append to it until you close Hooke.
45      You can also define a minimun deviation of the peaks.
46
47      Syntax:
48      multidistance [deviation]
49      deviation = number of times the convolution signal is above the noise absolute deviation.
50      '''
51
52       
53      noflatten=False
54      peaks_location, peak_size=self.find_current_peaks(noflatten)
55      
56      #if no peaks, we have nothing to plot. exit.
57      if len(peaks_location)==0:
58             return
59
60      #otherwise, we plot the peak locations.
61      xplotted_ret=self.plots[0].vectors[1][0]
62      yplotted_ret=self.plots[0].vectors[1][1]
63      xgood=[xplotted_ret[index] for index in peaks_location]
64      ygood=[yplotted_ret[index] for index in peaks_location]
65
66      recplot=self._get_displayed_plot()
67      recplot.vectors.append([xgood,ygood])
68      if recplot.styles==[]:
69          recplot.styles=[None,None,'scatter']
70          recplot.colors=[None,None,None]
71      else:
72          recplot.styles+=['scatter']
73          recplot.colors+=[None]
74
75      self._send_plot([recplot])
76
77      print 'Peaks to ignore (0,1...n from contact point,return to take all)'
78      print 'N to discard measurement'
79      exclude_raw=raw_input('Input:')
80      if exclude_raw=='N':
81         print 'Discarded.'
82         return
83
84      if not exclude_raw=='':
85         exclude=exclude_raw.split(',')
86         #we convert in numbers the input
87         try:
88             exclude=[int(item) for item in exclude]
89         except:
90             print 'Bad input, taking nothing.'
91             return
92
93 #    we remove the peaks that we don't want from the list, we need a counter beacause if we remove
94 #    a peaks the other peaks in the list are shifted by one at each step
95         count=0
96         for a in exclude:
97           if (a==0):
98              peaks_location=peaks_location[1:]
99           else:
100              new_a=a-count
101              peaks_location=  peaks_location[0:new_a]+peaks_location[new_a+1:]
102              peak_size=            peak_size[0:new_a]+peak_size[new_a+1:]
103           count+=1
104
105      #we calculate the distance vector
106      dist=[]
107      for i in range(len(peaks_location)-1):
108          dist.append(xplotted_ret[peaks_location[i]]-xplotted_ret[peaks_location[i+1]])
109
110
111
112
113
114         #Save file info
115      if self.autofile=='':
116             self.autofile=raw_input('Multidistance filename? (return to ignore) ')
117             if self.autofile=='':
118                 print 'Not saved.'
119                 return
120
121      if not os.path.exists(self.autofile):
122             f=open(self.autofile,'w+')
123             f.write('Analysis started '+time.asctime()+'\n')
124             f.write('----------------------------------------\n')
125             f.write('; Delta Distance length (m)\n')
126             f.write(self.current.path+'\n')
127             for o in dist:
128                f.write(";")
129                f.write(str(o))
130             f.write("\n")
131             f.close()
132
133      print 'Saving...'
134      f=open(self.autofile,'a+')
135
136      f.write(self.current.path+'\n')
137      for i in dist:
138           f.write(";")
139           f.write(str(i))
140
141      f.write("\n")
142      f.close()