Merged hooke.plugin.plotmanip into hooke.plugin.curve.
[hooke.git] / hooke / plugin / curve.py
index 13ee496cc8a3d8d6bd890fc1f0de37a5297194c1..bf219180721bc8e88755c2a5d9f70d51a025ba30 100644 (file)
@@ -1,5 +1,7 @@
-# Copyright (C) 2010 Fibrin's Benedetti
-#                    W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2008-2010 Alberto Gomez-Casado
+#                         Fabrizio Benedetti
+#                         Massimo Sandal <devicerandom@gmail.com>
+#                         W. Trevor King <wking@drexel.edu>
 #
 # This file is part of Hooke.
 #
@@ -23,8 +25,10 @@ associated :class:`hooke.command.Command`\s for handling
 """
 
 from ..command import Command, Argument, Failure
+from ..curve import Data
 from ..plugin import Builtin
 from ..plugin.playlist import current_playlist_callback
+from ..util.calculus import derivative
 
 
 class CurvePlugin (Builtin):
@@ -32,7 +36,7 @@ class CurvePlugin (Builtin):
         super(CurvePlugin, self).__init__(name='curve')
 
     def commands(self):
-        return [InfoCommand(), ]
+        return [InfoCommand(), ExportCommand()]
 
 
 # Define common or complicated arguments
@@ -114,18 +118,17 @@ class InfoCommand (Command):
     def _get_block_sizes(self, curve):
         return [block.shape for block in curve.data]
 
-
 class ExportCommand (Command):
     """Export a :class:`hooke.curve.Curve` data block as TAB-delimeted
     ASCII text.
     """
     def __init__(self):
-        super(InfoCommand, self).__init__(
-            name='curve info',
+        super(ExportCommand, self).__init__(
+            name='export block',
             arguments=[
                 CurveArgument,
                 Argument(name='block', aliases=['set'], type='int', default=0,
-                    help="""
+                         help="""
 Data block to save.  For an approach/retract force curve, `0` selects
 the approacing curve and `1` selects the retracting curve.
 """.strip()),
@@ -137,7 +140,83 @@ File name for the output data.  Defaults to 'curve.dat'
             help=self.__doc__)
 
     def _run(self, hooke, inqueue, outqueue, params):
-        data = params['curve'].data[params['index']]
+        data = params['curve'].data[params['block']]
         f = open(params['output'], 'w')
         data.tofile(f, sep='\t')
         f.close()
+
+class DifferenceCommand (Command):
+    """Calculate the derivative (actually, the discrete differentiation)
+    of a curve data block.
+
+    See :func:`hooke.util.calculus.derivative` for implementation
+    details.
+    """
+    def __init__(self):
+        super(DifferenceCommand, self).__init__(
+            name='block difference',
+            arguments=[
+                CurveArgument,
+                Argument(name='block one', aliases=['set one'], type='int',
+                         default=1,
+                         help="""
+Block A in A-B.  For an approach/retract force curve, `0` selects the
+approacing curve and `1` selects the retracting curve.
+""".strip()),
+                Argument(name='block two', aliases=['set two'], type='int',
+                         default=0,
+                         help='Block B in A-B.'),
+                Argument(name='x column', type='int', default=0,
+                         help="""
+Column of data block to differentiate with respect to.
+""".strip()),
+                Argument(name='y column', type='int', default=1,
+                         help="""
+Column of data block to differentiate.
+""".strip()),
+                ],
+            help=self.__doc__)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+        a = params['curve'].data[params['block one']]
+        b = params['curve'].data[params['block two']]
+        assert a[:,params['x column']] == b[:,params['x column']]:
+        out = Data((a.shape[0],2), dtype=a.dtype)
+        out[:,0] = a[:,params['x column']]
+        out[:,1] = a[:,params['y column']] - b[:,params['y column']]:
+        outqueue.put(out)
+
+class DerivativeCommand (Command):
+    """Calculate the difference between two blocks of data.
+    """
+    def __init__(self):
+        super(DerivativeCommand, self).__init__(
+            name='block derivative',
+            arguments=[
+                CurveArgument,
+                Argument(name='block', aliases=['set'], type='int', default=0,
+                         help="""
+Data block to differentiate.  For an approach/retract force curve, `0`
+selects the approacing curve and `1` selects the retracting curve.
+""".strip()),
+                Argument(name='x column', type='int', default=0,
+                         help="""
+Column of data block to differentiate with respect to.
+""".strip()),
+                Argument(name='y column', type='int', default=1,
+                         help="""
+Column of data block to differentiate.
+""".strip()),
+                Argument(name='weights', type='dict', default={-1:-0.5, 1:0.5},
+                         help="""
+Weighting scheme dictionary for finite differencing.  Defaults to
+central differencing.
+""".strip()),
+                ],
+            help=self.__doc__)
+
+    def _run(self, hooke, inqueue, outqueue, params):
+        data = params['curve'].data[params['block']]
+        outqueue.put(derivative(
+                block, x_col=params['x column'], y_col=params['y column'],
+                weights=params['weights']))