(mcs.py) lower- and upper- case both working
[hooke.git] / autopeak.py
index da96fcf81d4ff87b7c2d9b1b9582d0d09199be9c..1eabb18ab9c990ab04cb583e262da4444c6b891c 100644 (file)
@@ -66,9 +66,10 @@ class autopeakCommands:
         auto_fit_nm = number of nm to fit before the peak maximum, for WLC (if usepoints false)
         auto_fit_points = number of points to fit before the peak maximum, for WLC (if usepoints true)
         
-        baseline_clicks = 0: automatic baseline
-                          1: decide baseline with a single click and length defined in auto_left_baseline
-                          2: let user click points of baseline
+        baseline_clicks = -1: no baseline, f=0 at the contact point (whether hand-picked or automatically found)\r
+                           0: automatic baseline
+                           1: decide baseline with a single click and length defined in auto_left_baseline
+                           2: let user click points of baseline
         auto_left_baseline = length in nm to use as baseline from the right point (if baseline_clicks=0 , 1)
         auto_right_baseline = distance in nm of peak-most baseline point from last peak (if baseline_clicks = 0)
         '''
@@ -82,6 +83,7 @@ class autopeakCommands:
             plot: plot to use
             backwards: if true, finds a point backwards.
             '''
+            whatset=1 #FIXME: should be decidable
             x_vect=plot.vectors[1][0]
             
             c=0
@@ -105,7 +107,7 @@ class autopeakCommands:
             contact_point_index=contact_point.index
             self.wlccontact_point=contact_point
             self.wlccontact_index=contact_point.index
-            self.wlccurrent=self.current.path
+            self.wlccurrent=self.current.path\r
             return contact_point, contact_point_index
         
         def find_current_peaks():
@@ -127,6 +129,8 @@ class autopeakCommands:
         #initialize output data vectors
         c_lengths=[]
         p_lengths=[]
+        sigma_c_lengths=[]
+        sigma_p_lengths=[]
         forces=[]
         slopes=[]
         
@@ -175,11 +179,15 @@ class autopeakCommands:
         
         peak_location, peak_size = find_current_peaks()
         
+        if len(peak_location) == 0:
+            print 'No peaks to fit.'
+            return
+        
         fitplot=copy.deepcopy(displayed_plot)
         
         #Pick up force baseline
         if rebase:
-            clicks=self.config['baseline_clicks']
+            clicks=self.config['baseline_clicks']\r
             if clicks==0:
                 self.basepoints=[]
                 base_index_0=peak_location[-1]+fit_interval_nm(peak_location[-1], displayed_plot, self.config['auto_right_baseline'],False)
@@ -200,8 +208,14 @@ class autopeakCommands:
         boundaries=[self.basepoints[0].index, self.basepoints[1].index]
         boundaries.sort()
         to_average=displayed_plot.vectors[1][1][boundaries[0]:boundaries[1]] #y points to average
-        avg=np.mean(to_average)
-        
+        avg=np.mean(to_average)\r
+        \r
+        clicks=self.config['baseline_clicks']\r
+        if clicks==-1:\r
+            try:\r
+                avg=displayed_plot.vectors[1][1][contact_point_index]
+            except:\r
+                avg=displayed_plot.vectors[1][1][cindex]
         
         for peak in peak_location:
             #WLC FITTING
@@ -217,7 +231,7 @@ class autopeakCommands:
             if abs(peak_point.index-other_fit_point.index) < 2:
                 continue
             
-            params, yfit, xfit, fit_errors = self.wlc_fit(points, displayed_plot.vectors[1][0], displayed_plot.vectors[1][1], pl_value, T)
+            params, yfit, xfit, fit_errors = self.wlc_fit(points, displayed_plot.vectors[1][0], displayed_plot.vectors[1][1], pl_value, T, return_errors=True)
             
                 
             #Measure forces
@@ -233,20 +247,26 @@ class autopeakCommands:
             if len(params)==1: #if we did choose 1-value fit
                 p_lengths.append(pl_value)
                 c_lengths.append(params[0]*(1.0e+9))
+                sigma_p_lengths.append(0)
+                sigma_c_lengths.append(fit_errors[0]*(1.0e+9))
                 forces.append(abs(y-avg)*(1.0e+12))
                 slopes.append(slope)     
                 #Add WLC fit lines to plot
                 fitplot.add_set(xfit,yfit)
                 if len(fitplot.styles)==0:
                     fitplot.styles=[]
+                    fitplot.colors=[]
                 else:
                     fitplot.styles.append(None)
-            else:
+                    fitplot.colors.append(None)
+            else: #2-value fit
                 p_leng=params[1]*(1.0e+9)
                 #check if persistent length makes sense. otherwise, discard peak.
                 if p_leng>self.config['auto_min_p'] and p_leng<self.config['auto_max_p']:
                     p_lengths.append(p_leng)       
                     c_lengths.append(params[0]*(1.0e+9))
+                    sigma_c_lengths.append(fit_errors[0]*(1.0e+9))
+                    sigma_p_lengths.append(fit_errors[1]*(1.0e+9))
                     forces.append(abs(y-avg)*(1.0e+12))
                     slopes.append(slope)     
                     
@@ -254,8 +274,10 @@ class autopeakCommands:
                     fitplot.add_set(xfit,yfit)
                     if len(fitplot.styles)==0:
                         fitplot.styles=[]
+                        fitplot.colors=[]
                     else:
                         fitplot.styles.append(None)
+                        fitplot.colors.append(None)
                 else:
                     pass
  
@@ -263,12 +285,19 @@ class autopeakCommands:
         #add basepoints to fitplot
         fitplot.add_set([self.basepoints[0].graph_coords[0],self.basepoints[1].graph_coords[0]],[self.basepoints[0].graph_coords[1],self.basepoints[1].graph_coords[1]]) 
         fitplot.styles.append('scatter')
-        
+        fitplot.colors.append(None)
         
         #Show wlc fits and peak locations
         self._send_plot([fitplot])
         #self.do_peaks('')
         
+        print 'Measurements for all peaks detected:'
+        print 'contour (nm)', c_lengths
+        print 'sigma contour (nm)',sigma_c_lengths
+        print 'p (nm)',p_lengths
+        print 'sigma p (nm)',sigma_p_lengths
+        print 'forces (pN)',forces
+        print 'slopes (N/m)',slopes
         
         #Ask the user what peaks to ignore from analysis.
         print 'Peaks to ignore (0,1...n from contact point,return to take all)'
@@ -286,15 +315,24 @@ class autopeakCommands:
                     p_lengths[i]=None
                     forces[i]=None
                     slopes[i]=None
+                    sigma_c_lengths[i]=None
+                    sigma_p_lengths[i]=None
             except:
                  print 'Bad input, taking all...'
         #Clean data vectors from ignored peaks        
+        #FIXME:code duplication
         c_lengths=[item for item in c_lengths if item != None]
         p_lengths=[item for item in p_lengths if item != None]
         forces=[item for item in forces if item != None]
         slopes=[item for item in slopes if item != None]    
+        sigma_c_lengths=[item for item in sigma_c_lengths if item != None]    
+        sigma_p_lengths=[item for item in sigma_p_lengths if item != None]    
+        
+        print 'Measurements for chosen peaks:'
         print 'contour (nm)',c_lengths
+        print 'sigma contour (nm)',sigma_c_lengths
         print 'p (nm)',p_lengths
+        print 'sigma p (nm)',sigma_p_lengths
         print 'forces (pN)',forces
         print 'slopes (N/m)',slopes
         
@@ -309,7 +347,7 @@ class autopeakCommands:
             f=open(self.autofile,'w+')
             f.write('Analysis started '+time.asctime()+'\n')
             f.write('----------------------------------------\n')
-            f.write('; Contour length (nm)  ;  Persistence length (nm) ;  Max.Force (pN)  ;  Slope (N/m) \n')
+            f.write('; Contour length (nm)  ;  Persistence length (nm) ;  Max.Force (pN)  ;  Slope (N/m) ;  Sigma contour (nm) ; Sigma persistence (nm)\n')
             f.close()
             
         print 'Saving...'
@@ -317,7 +355,7 @@ class autopeakCommands:
         
         f.write(self.current.path+'\n')
         for i in range(len(c_lengths)):
-            f.write(' ; '+str(c_lengths[i])+' ; '+str(p_lengths[i])+' ; '+str(forces[i])+' ; '+str(slopes[i])+'\n')
+            f.write(' ; '+str(c_lengths[i])+' ; '+str(p_lengths[i])+' ; '+str(forces[i])+' ; '+str(slopes[i])+' ; '+str(sigma_c_lengths[i])+' ; '+str(sigma_p_lengths[i])+'\n')
             
         f.close()
         self.do_note('autopeak')