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