-# 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.
#
-# Hooke is free software: you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation, either
-# version 3 of the License, or (at your option) any later version.
+# Hooke is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
#
-# Hooke is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Lesser General Public License for more details.
+# Hooke is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+# Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Hooke. If not, see
:mod:`hooke.curve` classes.
"""
+import numpy
+
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
+from ..util.fft import unitary_avg_power_spectrum
class CurvePlugin (Builtin):
def __init__(self):
super(CurvePlugin, self).__init__(name='curve')
-
- def commands(self):
- return [InfoCommand(), ]
+ self._commands = [
+ GetCommand(self), InfoCommand(self), ExportCommand(self),
+ DifferenceCommand(self), DerivativeCommand(self),
+ PowerSpectrumCommand(self)]
# Define common or complicated arguments
# Define commands
+class GetCommand (Command):
+ """Return a :class:`hooke.curve.Curve`.
+ """
+ def __init__(self, plugin):
+ super(GetCommand, self).__init__(
+ name='get curve', arguments=[CurveArgument],
+ help=self.__doc__, plugin=plugin)
+
+ def _run(self, hooke, inqueue, outqueue, params):
+ outqueue.put(params['curve'])
+
class InfoCommand (Command):
- """Print selected information about a :class:`hooke.curve.Curve`.
+ """Get selected information about a :class:`hooke.curve.Curve`.
"""
- def __init__(self):
+ def __init__(self, plugin):
args = [
CurveArgument,
Argument(name='all', type='bool', default=False, count=1,
- help='Print all curve information.'),
+ help='Get all curve information.'),
]
self.fields = ['name', 'path', 'experiment', 'driver', 'filetype', 'note',
'blocks', 'block sizes']
for field in self.fields:
args.append(Argument(
name=field, type='bool', default=False, count=1,
- help='Print curve %s' % field))
+ help='Get curve %s' % field))
super(InfoCommand, self).__init__(
- name='curve info', arguments=args, help=self.__doc__)
+ name='curve info', arguments=args,
+ help=self.__doc__, plugin=plugin)
def _run(self, hooke, inqueue, outqueue, params):
fields = {}
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.
+
+ A "#" prefixed header will optionally appear at the beginning of
+ the file naming the columns.
"""
- def __init__(self):
- self.fields = ['name', 'path', 'experiment', 'driver', 'filetype', 'note',
- 'blocks', 'block sizes']
- for field in self.fields:
- args.append(Argument(
- name=field, type='bool', default=False, count=1,
- help='Print curve %s' % field))
- super(InfoCommand, self).__init__(
- name='curve info',
+ def __init__(self, plugin):
+ 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.
+the approaching curve and `1` selects the retracting curve.
""".strip()),
Argument(name='output', type='file', default='curve.dat',
help="""
File name for the output data. Defaults to 'curve.dat'
+""".strip()),
+ Argument(name='header', type='bool', default=True,
+ help="""
+True if you want the column-naming header line.
""".strip()),
],
- help=self.__doc__)
+ help=self.__doc__, plugin=plugin)
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')
+ if params['header'] == True:
+ f.write('# %s \n' % ('\t'.join(data.info['columns'])))
+ numpy.savetxt(f, data, delimiter='\t')
f.close()
+
+class DifferenceCommand (Command):
+ """Calculate the difference between two blocks of data.
+ """
+ def __init__(self, plugin):
+ 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
+approaching 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 to return as x values.
+""".strip()),
+ Argument(name='y column', type='int', default=1,
+ help="""
+Column of data block to difference.
+""".strip()),
+ ],
+ help=self.__doc__, plugin=plugin)
+
+ 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 derivative (actually, the discrete differentiation)
+ of a curve data block.
+
+ See :func:`hooke.util.calculus.derivative` for implementation
+ details.
+ """
+ def __init__(self, plugin):
+ 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 approaching 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='f 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__, 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']))
+
+class PowerSpectrumCommand (Command):
+ """Calculate the power spectrum of a data block.
+ """
+ def __init__(self, plugin):
+ super(PowerSpectrumCommand, self).__init__(
+ name='block power spectrum',
+ arguments=[
+ CurveArgument,
+ Argument(name='block', aliases=['set'], type='int', default=0,
+ help="""
+Data block to act on. For an approach/retract force curve, `0`
+selects the approaching curve and `1` selects the retracting curve.
+""".strip()),
+ Argument(name='column', type='int', default=1,
+ help="""
+Column of data block containing time-series data.
+""".strip()),
+ Argument(name='freq', type='float', default=1.0,
+ help="""
+Sampling frequency.
+""".strip()),
+ Argument(name='chunk size', type='int', default=2048,
+ help="""
+Number of samples per chunk. Use a power of two.
+""".strip()),
+ Argument(name='overlap', type='bool', default=False,
+ help="""
+If `True`, each chunk overlaps the previous chunk by half its length.
+Otherwise, the chunks are end-to-end, and not overlapping.
+""".strip()),
+ ],
+ help=self.__doc__, plugin=plugin)
+
+ def _run(self, hooke, inqueue, outqueue, params):
+ data = params['curve'].data[params['block']]
+ outqueue.put(unitary_avg_power_spectrum(
+ data[:,params['column']], freq=params['freq'],
+ chunk_size=params['chunk size'],
+ overlap=params['overlap']))