Update DerivativeCommand to use flexible column names.
authorW. Trevor King <wking@drexel.edu>
Sat, 7 Aug 2010 22:28:36 +0000 (18:28 -0400)
committerW. Trevor King <wking@drexel.edu>
Sat, 7 Aug 2010 22:28:36 +0000 (18:28 -0400)
hooke/plugin/curve.py

index 5ba69d7c1352f22b52fb98e2b74f731d51988898..5c13ba741a4e325720bbaa9641c555b7a64cf325 100644 (file)
@@ -24,6 +24,8 @@ associated :class:`hooke.command.Command`\s for handling
 :mod:`hooke.curve` classes.
 """
 
+import copy
+
 import numpy
 
 from ..command import Command, Argument, Failure
@@ -271,11 +273,11 @@ class DerivativeCommand (Command):
 Data block to differentiate.  For an approach/retract force curve, `0`
 selects the approaching curve and `1` selects the retracting curve.
 """.strip()),
-                Argument(name='x column', type='int', default=0,
+                Argument(name='x column', type='string',
                          help="""
 Column of data block to differentiate with respect to.
 """.strip()),
-                Argument(name='f column', type='int', default=1,
+                Argument(name='f column', type='string',
                          help="""
 Column of data block to differentiate.
 """.strip()),
@@ -283,15 +285,43 @@ Column of data block to differentiate.
                          help="""
 Weighting scheme dictionary for finite differencing.  Defaults to
 central differencing.
+""".strip()),
+                Argument(name='output column name', type='string',
+                         help="""
+Name of the new column for storing the derivative (without units, defaults to `derivative of <f column name> with respect to <x column name>`).
 """.strip()),
                 ],
             help=self.__doc__, plugin=plugin)
 
     def _run(self, hooke, inqueue, outqueue, params):
         data = params['curve'].data[params['block']]
-        outqueue.put(derivative(
-                block, x_col=params['x column'], f_col=params['f column'],
-                weights=params['weights']))
+        # 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
+
+        x_col = data.info['columns'].index(params['x column'])
+        f_col = data.info['columns'].index(params['f column'])
+        d = derivative(
+            block, x_col=x_col, f_col=f_col, weights=params['weights'])
+
+        x_name,x_units = split_data_label(params['x column'])
+        f_name,f_units = split_data_label(params['f column'])
+        if params['output column name'] == None:
+            params['output column name'] = (
+                'derivative of %s with respect to %s' % (
+                    params['f column'], params['x column']))
+
+        new.info['columns'].append(
+            join_data_label(params['output distance column'],
+                            '%s/%s' % (f_units/x_units)))
+        new[:,-1] = d[:,1]
+        params['curve'].data[params['block']] = new
+
 
 class PowerSpectrumCommand (Command):
     """Calculate the power spectrum of a data block.
@@ -332,9 +362,8 @@ If `True`, each chunk overlaps the previous chunk by half its length.
 Otherwise, the chunks are end-to-end, and not overlapping.
 """.strip()),
                 Argument(name='output block name', type='string',
-                         default='power spectrum',
                          help="""
-Name of the new data block (without `of <source block name> <source column name>`) for storing the power spectrum.
+Name of the new data block (defaults to `power spectrum of <source block name> <source column name>`) for storing the power spectrum.
 """.strip()),
                 ],
             help=self.__doc__, plugin=plugin)
@@ -349,10 +378,13 @@ Name of the new data block (without `of <source block name> <source column name>
             d, freq=params['freq'],
             chunk_size=params['chunk size'],
             overlap=params['overlap'])
+
         name,data_units = split_data_label(params['column'])
         b = Data((len(freq_axis),2), dtype=data.dtype)
-        b.info['name'] = '%s of %s %s' % (
-            params['output block name'], data.info['name'], params['column'])
+        if params['output block name'] == None:
+            params['output block name'] = 'power spectrum of %s %s' % (
+              params['output block name'], data.info['name'], params['column'])
+        b.info['name'] = params['output block name']
         b.info['columns'] = [
             join_data_label('frequency axis', params['freq units']),
             join_data_label('power density',