We don't seem to need to guess the scale in SurfacePositionModel
[hooke.git] / hooke / plugin / vclamp.py
index fbd6335aec334c78950ad04a19361c1f32aad65b..5ffba75b5585c02e96d31260326782aeabfb25d8 100644 (file)
@@ -26,6 +26,7 @@ common velocity clamp analysis tasks.
 """
 
 import copy
+import logging
 
 import numpy
 import scipy
@@ -75,6 +76,7 @@ def scale(hooke, curve, block=None):
             cant_adjust.run(hooke, inqueue, outqueue, **params)
     return curve
 
+
 class SurfacePositionModel (ModelFitter):
     """Bilinear surface position model.
 
@@ -96,7 +98,9 @@ class SurfacePositionModel (ModelFitter):
     In order for this model to produce a satisfactory fit, there
     should be enough data in the off-surface region that interactions
     due to proteins, etc. will not seriously skew the fit in the
-    off-surface region.
+    off-surface region.  If you don't have much of a tail, you can set
+    the `info` dict's `ignore non-contact before index` parameter to
+    the index of the last surface- or protein-related feature.
     """
     def model(self, params):
         """A continuous, bilinear model.
@@ -115,7 +119,8 @@ class SurfacePositionModel (ModelFitter):
         :math:`p_3` is the slope of the second region.
         """
         p = params  # convenient alias
-        if self.info.get('force zero non-contact slope', None) == True:
+        rNC_ignore = self.info['ignore non-contact before index']
+        if self.info['force zero non-contact slope'] == True:
             p = list(p)
             p.append(0.)  # restore the non-contact slope parameter
         r2 = numpy.round(abs(p[2]))
@@ -124,19 +129,32 @@ class SurfacePositionModel (ModelFitter):
         if r2 < len(self._data)-1:
             self._model_data[r2:] = \
                 p[0] + p[1]*p[2] + p[3] * numpy.arange(len(self._data)-r2)
+            if r2 < rNC_ignore:
+                self._model_data[r2:rNC_ignore] = self._data[r2:rNC_ignore]
         return self._model_data
 
-    def set_data(self, data, info=None):
-        super(SurfacePositionModel, self).set_data(data, info)
-        if info == None:
-            info = {}
-        self.info = info
-        self.info['min position'] = 0
-        self.info['max position'] = len(data)
-        self.info['max deflection'] = data.max()
-        self.info['min deflection'] = data.min()
-        self.info['position range'] = self.info['max position'] - self.info['min position']
-        self.info['deflection range'] = self.info['max deflection'] - self.info['min deflection']
+    def set_data(self, data, info=None, *args, **kwargs):
+        super(SurfacePositionModel, self).set_data(data, info, *args, **kwargs)
+        if self.info == None:
+            self.info = {}
+        for key,value in [
+            ('force zero non-contact slope', False),
+            ('ignore non-contact before index', 6158),
+            ('min position', 0),  # Store postions etc. to avoid recalculating.
+            ('max position', len(data)),
+            ('max deflection', data.max()),
+            ('min deflection', data.min()),
+            ]:
+            if key not in self.info:
+                self.info[key] = value
+        for key,value in [
+            ('position range',
+             self.info['max position'] - self.info['min position']),
+            ('deflection range',
+             self.info['max deflection'] - self.info['min deflection']),
+            ]:
+            if key not in self.info:
+                self.info[key] = value
 
     def guess_initial_params(self, outqueue=None):
         """Guess the initial parameters.
@@ -157,36 +175,25 @@ class SurfacePositionModel (ModelFitter):
         right_slope = 0
         self.info['guessed contact slope'] = left_slope
         params = [left_offset, left_slope, kink_position, right_slope]
-        if self.info.get('force zero non-contact slope', None) == True:
+        if self.info['force zero non-contact slope'] == True:
             params = params[:-1]
         return params
 
-    def guess_scale(self, params, outqueue=None):
-        """Guess the parameter scales.
+    def fit(self, *args, **kwargs):
+        """Fit the model to the data.
 
         Notes
         -----
-        We guess offset scale (:math:`p_0`) as one tenth of the total
-        deflection range, the kink scale (:math:`p_2`) as one tenth of
-        the total index range, the initial (contact) slope scale
-        (:math:`p_1`) as one tenth of the contact slope estimation,
-        and the final (non-contact) slope scale (:math:`p_3`) is as
-        one tenth of the initial slope scale.
+        We change the `epsfcn` default from :func:`scipy.optimize.leastsq`'s
+        `0` to `1e-3`, so the initial Jacobian estimate takes larger steps,
+        which helps avoid being trapped in noise-generated local minima.
         """
-        offset_scale = self.info['deflection range']/10.
-        left_slope_scale = abs(params[1])/10.
-        kink_scale = self.info['position range']/10.
-        right_slope_scale = left_slope_scale/10.
-        scale = [offset_scale, left_slope_scale, kink_scale, right_slope_scale]
-        if self.info.get('force zero non-contact slope', None) == True:
-            scale = scale[:-1]
-        return scale
-
-    def fit(self, *args, **kwargs):
         self.info['guessed contact slope'] = None
+        if 'epsfcn' not in kwargs:
+            kwargs['epsfcn'] = 1e-3  # take big steps to estimate the Jacobian
         params = super(SurfacePositionModel, self).fit(*args, **kwargs)
         params[2] = abs(params[2])
-        if self.info.get('force zero non-contact slope', None) == True:
+        if self.info['force zero non-contact slope'] == True:
             params = list(params)
             params.append(0.)  # restore the non-contact slope parameter
 
@@ -210,12 +217,13 @@ class SurfacePositionModel (ModelFitter):
                           % (params[3], self.info['guessed contact slope']))
         return params
 
+
 class VelocityClampPlugin (Plugin):
     def __init__(self):
         super(VelocityClampPlugin, self).__init__(name='vclamp')
         self._commands = [
             SurfaceContactCommand(self), ForceCommand(self),
-            CantileverAdjustedExtensionCommand(self),
+            CantileverAdjustedExtensionCommand(self), FlattenCommand(self),
             ]
 
     def default_settings(self):
@@ -253,7 +261,7 @@ selects the retracting curve.
                 Argument(name='input distance column', type='string',
                          default='z piezo (m)',
                          help="""
-Name of the column to use as the surface positioning input.
+Name of the column to use as the surface position input.
 """.strip()),
                 Argument(name='input deflection column', type='string',
                          default='deflection (m)',
@@ -263,7 +271,7 @@ Name of the column to use as the deflection input.
                 Argument(name='output distance column', type='string',
                          default='surface distance',
                          help="""
-Name of the column (without units) to use as the surface positioning output.
+Name of the column (without units) to use as the surface position output.
 """.strip()),
                 Argument(name='output deflection column', type='string',
                          default='surface deflection',
@@ -283,7 +291,7 @@ Name (without units) for storing the deflection offset in the `.info` dictionary
                 Argument(name='fit parameters info name', type='string',
                          default='surface deflection offset',
                          help="""
-Name (without units) for storing the deflection offset in the `.info` dictionary.
+Name (without units) for storing fit parameters in the `.info` dictionary.
 """.strip()),
                 ],
             help=self.__doc__, plugin=plugin)
@@ -441,13 +449,14 @@ Name (without units) for storing the deflection offset in the `.info` dictionary
 
         Notes
         -----
-        Uses :func:`analyze_surf_pos_data_wtk` internally.
+        Uses :class:`SurfacePositionModel` internally.
         """
         reverse = z_data[0] > z_data[-1]
         if reverse == True:    # approaching, contact region on the right
             d_data = d_data[::-1]
-        s = SurfacePositionModel(d_data)
-        s.info['force zero non-contact slope'] = True
+        s = SurfacePositionModel(d_data, info={
+                'force zero non-contact slope':True},
+                                 rescale=True)
         offset,contact_slope,surface_index,non_contact_slope = s.fit(
             outqueue=outqueue)
         info = {
@@ -579,83 +588,109 @@ Name of the spring constant in the `.info` dictionary.
         params['curve'].data[params['block']] = new
 
 
-class generalvclampCommands(object):
-
-    def plotmanip_flatten(self, plot, current, customvalue=False):
-        '''
-        Subtracts a polynomial fit to the non-contact part of the curve, as to flatten it.
-        the best polynomial fit is chosen among polynomials of degree 1 to n, where n is
-        given by the configuration file or by the customvalue.
-
-        customvalue= int (>0) --> starts the function even if config says no (default=False)
-        '''
+class FlattenCommand (Command):
+    """Flatten a deflection column.
 
-        #not a smfs curve...
-        if current.curve.experiment != 'smfs':
-            return plot
+    Subtracts a polynomial fit from the non-contact part of the curve
+    to flatten it.  The best polynomial fit is chosen among
+    polynomials of degree 1 to `max degree`.
 
-        #only one set is present...
-        if len(self.plots[0].vectors) != 2:
-            return plot
-
-        #config is not flatten, and customvalue flag is false too
-        if (not self.config['flatten']) and (not customvalue):
-            return plot
-
-        max_exponent=12
-        delta_contact=0
+    .. todo:  Why does flattening use a polynomial fit and not a sinusoid?
+      Isn't most of the oscillation due to laser interference?
+      See Jaschke 1995 ( 10.1063/1.1146018 )
+      and the figure 4 caption of Weisenhorn 1992 ( 10.1103/PhysRevB.45.11226 )
+    """
+    def __init__(self, plugin):
+        super(FlattenCommand, self).__init__(
+            name='add flattened extension array',
+            arguments=[
+                CurveArgument,
+                Argument(name='block', aliases=['set'], type='int', default=0,
+                         help="""
+Data block for which the adjusted extension should be calculated.  For
+an approach/retract force curve, `0` selects the approaching curve and
+`1` selects the retracting curve.
+""".strip()),
+                Argument(name='max degree', type='int',
+                         default=1,
+                         help="""
+Highest order polynomial to consider for flattening.  Using values
+greater than one usually doesn't help and can give artifacts.
+However, it could be useful too.  (TODO: Back this up with some
+theory...)
+""".strip()),
+                Argument(name='input distance column', type='string',
+                         default='surface distance (m)',
+                         help="""
+Name of the column to use as the distance input.
+""".strip()),
+                Argument(name='input deflection column', type='string',
+                         default='deflection (N)',
+                         help="""
+Name of the column to use as the deflection input.
+""".strip()),
+                Argument(name='output deflection column', type='string',
+                         default='flattened deflection',
+                         help="""
+Name of the column (without units) to use as the deflection output.
+""".strip()),
+                Argument(name='fit info name', type='string',
+                         default='flatten fit',
+                         help="""
+Name of the flattening information in the `.info` dictionary.
+""".strip()),
+                ],
+            help=self.__doc__, plugin=plugin)
 
-        if customvalue:
-            max_cycles=customvalue
-        else:
-            max_cycles=self.config['flatten'] #Using > 1 usually doesn't help and can give artefacts. However, it could be useful too.
-
-        contact_index=self.find_contact_point()
-
-        valn=[[] for item in range(max_exponent)]
-        yrn=[0.0 for item in range(max_exponent)]
-        errn=[0.0 for item in range(max_exponent)]
-        
-        #Check if we have a proper numerical value
-        try:
-            zzz=int(max_cycles)
-        except:
-            #Loudly and annoyingly complain if it's not a number, then fallback to zero
-            print '''Warning: flatten value is not a number!
-            Use "set flatten" or edit hooke.conf to set it properly
-            Using zero.'''
-            max_cycles=0
-        
-        for i in range(int(max_cycles)):
-
-            x_ext=plot.vectors[0][0][contact_index+delta_contact:]
-            y_ext=plot.vectors[0][1][contact_index+delta_contact:]
-            x_ret=plot.vectors[1][0][contact_index+delta_contact:]
-            y_ret=plot.vectors[1][1][contact_index+delta_contact:]
-            for exponent in range(max_exponent):
-                try:
-                    valn[exponent]=sp.polyfit(x_ext,y_ext,exponent)
-                    yrn[exponent]=sp.polyval(valn[exponent],x_ret)
-                    errn[exponent]=sp.sqrt(sum((yrn[exponent]-y_ext)**2)/float(len(y_ext)))
-                except Exception,e:
-                    print 'Cannot flatten!'
-                    print e
-                    return plot
-
-            best_exponent=errn.index(min(errn))
-
-            #extension
-            ycorr_ext=y_ext-yrn[best_exponent]+y_ext[0] #noncontact part
-            yjoin_ext=np.array(plot.vectors[0][1][0:contact_index+delta_contact]) #contact part
-            #retraction
-            ycorr_ret=y_ret-yrn[best_exponent]+y_ext[0] #noncontact part
-            yjoin_ret=np.array(plot.vectors[1][1][0:contact_index+delta_contact]) #contact part
-
-            ycorr_ext=np.concatenate((yjoin_ext, ycorr_ext))
-            ycorr_ret=np.concatenate((yjoin_ret, ycorr_ret))
-
-            plot.vectors[0][1]=list(ycorr_ext)
-            plot.vectors[1][1]=list(ycorr_ret)
-
-        return plot
+    def _run(self, hooke, inqueue, outqueue, params):
+        data = params['curve'].data[params['block']]
+        # HACK? rely on params['curve'] being bound to the local hooke
+        # playlist (i.e. not a copy, as you would get by passing a
+        # curve through the queue).  Ugh.  Stupid queues.  As an
+        # alternative, we could pass lookup information through the
+        # queue.
+        new = Data((data.shape[0], data.shape[1]+1), dtype=data.dtype)
+        new.info = copy.deepcopy(data.info)
+        new[:,:-1] = data
+        z_data = data[:,data.info['columns'].index(
+                params['input distance column'])]
+        d_data = data[:,data.info['columns'].index(
+                params['input deflection column'])]
 
+        d_name,d_unit = split_data_label(params['input deflection column'])
+        new.info['columns'].append(
+            join_data_label(params['output deflection column'], d_unit))
+
+        contact_index = numpy.absolute(z_data).argmin()
+        mask = z_data > 0
+        indices = numpy.argwhere(mask)
+        z_nc = z_data[indices].flatten()
+        d_nc = d_data[indices].flatten()
+
+        min_err = numpy.inf
+        degree = poly_values = None
+        log = logging.getLogger('hooke')
+        for deg in range(params['max degree']):
+            try:
+                pv = scipy.polyfit(z_nc, d_nc, deg)
+                df = scipy.polyval(pv, z_nc)
+                err = numpy.sqrt((df-d_nc)**2).sum()
+            except Exception,e:
+                log.warn('failed to flatten with a degree %d polynomial: %s'
+                         % (deg, e))
+                continue
+            if err < min_err:  # new best fit
+                min_err = err
+                degree = deg
+                poly_values = pv
+
+        if degree == None:
+            raise Failure('failed to flatten with all degrees')
+        new.info[params['fit info name']] = {
+            'error':min_err/len(z_nc),
+            'degree':degree,
+            'max degree':params['max degree'],
+            'polynomial values':poly_values,
+            }
+        new[:,-1] = d_data - mask*scipy.polyval(poly_values, z_data)
+        params['curve'].data[params['block']] = new