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