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