WLC and FJC output are now with 2 decimal precision. Added a comment on autopeak...
[hooke.git] / flatfilts.py
index 27e526f61427d8b45bc7280b09a7a5ee828ab237..0843481310c67c2bc3d546e5306d7ee5b2917fe7 100755 (executable)
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -*- coding: utf-8 -*-
 
 '''
 FLATFILTS
@@ -147,19 +148,22 @@ class flatfiltsCommands:
     #-----Convolution-based peak recognition and filtering.
     #Requires the libpeakspot.py library
     
-    def has_peaks(self, plot, abs_devs):
+    def has_peaks(self, plot, abs_devs=None, maxpeak=True, window=10):
         '''
         Finds peak position in a force curve.
         FIXME: should be moved in libpeakspot.py
         '''
+        if abs_devs==None:
+            abs_devs=self.convfilt_config['mindeviation']
+        
+        
         xret=plot.vectors[1][0]
         yret=plot.vectors[1][1]
         #Calculate convolution.
         convoluted=lps.conv_dx(yret, self.convfilt_config['convolution'])
         
         #surely cut everything before the contact point
-        cut_index=self.find_contact_point()
-        
+        cut_index=self.find_contact_point(plot)
         #cut even more, before the blind window
         start_x=xret[cut_index]
         blind_index=0
@@ -168,18 +172,29 @@ class flatfiltsCommands:
                 break
             blind_index+=1
         cut_index+=blind_index
-        
         #do the dirty convolution-peak finding stuff
         noise_level=lps.noise_absdev(convoluted[cut_index:], self.convfilt_config['positive'], self.convfilt_config['maxcut'], self.convfilt_config['stable'])               
         above=lps.abovenoise(convoluted,noise_level,cut_index,abs_devs)     
-        peak_location,peak_size=lps.find_peaks(above)
+        peak_location,peak_size=lps.find_peaks(above,seedouble=self.convfilt_config['seedouble'])
                 
-        #take the maximum
+        #take the minimum or the maximum of a peak
         for i in range(len(peak_location)):
             peak=peak_location[i]
-            maxpk=min(yret[peak-10:peak+10])
-            index_maxpk=yret[peak-10:peak+10].index(maxpk)+(peak-10)
-            peak_location[i]=index_maxpk
+            valpk=min(yret[peak-window:peak+window])  #maximum in force (near the unfolding point)
+            index_pk=yret[peak-window:peak+window].index(valpk)+(peak-window)            
+
+            if maxpeak==False:
+               valpk=max(yret[peak:peak+window]) #minimum in force, near the baseline
+               index_pk=yret[peak:peak+window].index(valpk)+(peak)
+
+#  Let's explain that for the minimum.  Immaging that we know that there is a peak at position/region 100 and you have found its y-value,
+#  Now you look in the array, from 100-10 to 100+10  (if the window is 10).
+#  This "100-10 to 100+10" is substancially a new array with its index. In this array you have 20
+#  elements, so the index of your y-value will be 10.
+#  Now to find the index in the TOTAL array you have to add the "position" of the "region" (that in this case
+#  correspond to 100) and also substract the window size ---> (+100-10)
+
+            peak_location[i]=index_pk
             
         return peak_location,peak_size
     
@@ -222,12 +237,11 @@ class flatfiltsCommands:
         if len(args)==0:
             args=self.convfilt_config['mindeviation']
         
-           
-        
         try:
             abs_devs=float(args)
         except:
-            pass
+            print 'Wrong argument, using config value'
+            abs_devs=float(self.convfilt_config['mindeviation'])
                         
         defplots=self.current.curve.default_plots()[0] #we need the raw, uncorrected plots
         
@@ -239,6 +253,8 @@ class flatfiltsCommands:
         
         peak_location,peak_size=self.has_peaks(defplots,abs_devs)
         print 'Found '+str(len(peak_location))+' peaks.'
+        to_dump='peaks '+self.current.path+' '+str(len(peak_location))
+        self.outlet.push(to_dump)
         #print peak_location
         
         #if no peaks, we have nothing to plot. exit.
@@ -255,8 +271,10 @@ class flatfiltsCommands:
         recplot.vectors.append([xgood,ygood])
         if recplot.styles==[]:
             recplot.styles=[None,None,'scatter']
+            recplot.colors=[None,None,None]
         else:
             recplot.styles+=['scatter']
+            recplot.colors+=[None]
         
         self._send_plot([recplot])
         
@@ -272,10 +290,10 @@ class flatfiltsCommands:
         convfilt [min_npks min_deviation]
 
         min_npks = minmum number of peaks
-        (default=4)
+        (to set the default, see convfilt.conf file; CONVCONF and SETCONF commands)
 
         min_deviation = minimum signal/noise ratio *in the convolution*
-        (default=4)
+        (to set the default, see convfilt.conf file; CONVCONF and SETCONF commands)
 
         If called without arguments, it uses default values.
         '''
@@ -294,7 +312,6 @@ class flatfiltsCommands:
         print '(Please wait)'
         notflat_list=[]
         
-        
         c=0
         for item in self.current_list:
             c+=1
@@ -315,7 +332,10 @@ class flatfiltsCommands:
                 item.peak_size=peak_size
                 item.curve=None #empty the item object, to further avoid memory leak
                 notflat_list.append(item)
-        
+
+            for i in range(1000):
+                k=0
+
         #Warn that no flattening had been done.
         if not ('flatten' in self.config['plotmanips']):
             print 'Flatten manipulator was not found. Processing was done without flattening.'
@@ -331,16 +351,7 @@ class flatfiltsCommands:
             self.current_list=notflat_list
             self.current=self.current_list[self.pointer]
             self.do_plot(0)
-
-    def do_convconf(self,args):
-        '''
-        CONVCONFIG
-        (flatfilts.py)
-        Prints the current convfilt configuration variables.
-        ------
-        Syntax: convconfig
-        '''
-        print self.convfilt_config
+        
         
     def do_setconv(self,args):
         '''
@@ -351,10 +362,22 @@ class flatfiltsCommands:
         Syntax: setconv variable value
         '''
         args=args.split()
-        try:
-            self.convfilt_config[args[0]]=eval(args[1])
-        except NameError:
-            self.convfilt_config[args[0]]=args[1]
+        #FIXME: a general "set dictionary" function has to be built
+        if len(args)==0:
+            print self.convfilt_config
+        else:
+            if not (args[0] in self.convfilt_config.keys()):
+                print 'This is not an internal convfilt variable!'
+                print 'Run "setconv" without arguments to see a list of defined variables.'
+                return
+            
+            if len(args)==1:
+                print self.convfilt_config[args[0]]
+            elif len(args)>1:
+                try:
+                    self.convfilt_config[args[0]]=eval(args[1])
+                except NameError: #we have a string argument
+                    self.convfilt_config[args[0]]=args[1]
 
 
 #########################