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