Moved contact point detection from plugin.fit -> plugin.vclamp.
[hooke.git] / hooke / plugin / flatfilt.py
index efca147af2e964fbf2137987930c6753cdfc378c..1637ce3f6ed12f139e41d2316921f56b05b3dca1 100644 (file)
@@ -54,7 +54,7 @@ class FlatFiltPlugin (Plugin):
     def __init__(self):
         super(FlatFiltPlugin, self).__init__(name='flatfilt')
         self._arguments = [ # For Command initialization
-            Argument('median filter', type='int', default=7, help="""
+            Argument('median window', type='int', default=7, help="""
 Median window filter size (in points).
 """.strip()),
             Argument('blind window', type='float', default=20e-9, help="""
@@ -121,9 +121,10 @@ class FlatPeaksCommand (Command):
             help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
-        z_data,d_data,params = self._setup(params)
+        z_data,d_data,params = self._setup(hooke, params)
         start_index = 0
-        while z_data[start_index] < params['blind window']:
+        while (start_index < len(z_data)
+               and z_data[start_index] < params['blind window']):
             start_index += 1
         median = medfilt(d_data[start_index:], params['median window'])
         deriv = diff(median)
@@ -134,7 +135,7 @@ class FlatPeaksCommand (Command):
             peak.index += start_index
         outqueue.put(peaks)
 
-    def _setup(self, params):
+    def _setup(self, hooke, params):
         """Setup `params` from config and return the z piezo and
         deflection arrays.
         """
@@ -142,17 +143,17 @@ class FlatPeaksCommand (Command):
         if curve.info['experiment'] != VelocityClamp:
             raise Failure('%s operates on VelocityClamp experiments, not %s'
                           % (self.name, curve.info['experiment']))
-        for col in ['surface z piezo (m)', 'deflection (N)']:
+        for col in ['surface distance (m)', 'deflection (N)']:
             if col not in curve.data[0].info['columns']:
-                scale(curve)
+                scale(hooke, curve)
         data = None
-        for block in curve.data:
+        for i,block in enumerate(curve.data):
             if block.info['name'].startswith('retract'):
                 data = block
                 break
         if data == None:
             raise Failure('No retraction blocks in %s.' % curve)
-        z_data = data[:,data.info['columns'].index('surface z piezo (m)')]
+        z_data = data[:,data.info['columns'].index('surface distance (m)')]
         if 'flattened deflection (N)' in data.info['columns']:
             params['deflection column name'] = 'flattened deflection (N)'
         else:
@@ -163,7 +164,7 @@ class FlatPeaksCommand (Command):
             if value == None: # Use configured default value.
                 params[key] = self.plugin.config[key]
         # TODO: better option parser to do this automatically by Argument.type
-        for key in ['max cut', 'min deviations', 'min points', 'see double', 'stable']:
+        for key in ['blind window', 'median window', 'max cut', 'min deviations', 'min points', 'see double', 'stable']:
             params[key] = float(params[key])
         # TODO: convert 'see double' from nm to points
         return z_data,d_data,params
@@ -196,11 +197,12 @@ class FlatFilterCommand (FilterCommand):
         self.arguments.extend(plugin._arguments)
 
     def filter(self, curve, hooke, inqueue, outqueue, params):
+        params['curve'] = curve
         inq = Queue()
         outq = Queue()
-        conv_command = [c for c in hooke.commands
+        filt_command = [c for c in hooke.commands
                         if c.name=='flat filter peaks'][0]
-        conv_command.run(hooke, inq, outq, **params)
+        filt_command.run(hooke, inq, outq, **params)
         peaks = outq.get()
         if not (isinstance(peaks, list) and (len(peaks) == 0
                                              or isinstance(peaks[0], Peak))):
@@ -208,4 +210,6 @@ class FlatFilterCommand (FilterCommand):
         ret = outq.get()
         if not isinstance(ret, Success):
             raise ret
-        return len(peaks) >= params['min peaks']
+        if params['min peaks'] == None: # Use configured default value.
+            params['min peaks'] = self.plugin.config['min peaks']
+        return len(peaks) >= int(params['min peaks'])