1 # Copyright (C) 2008-2010 Alberto Gomez-Casado
3 # Massimo Sandal <devicerandom@gmail.com>
4 # W. Trevor King <wking@drexel.edu>
6 # This file is part of Hooke.
8 # Hooke is free software: you can redistribute it and/or modify it
9 # under the terms of the GNU Lesser General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # Hooke is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
16 # Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with Hooke. If not, see
20 # <http://www.gnu.org/licenses/>.
22 """The ``curve`` module provides :class:`CurvePlugin` and several
23 associated :class:`hooke.command.Command`\s for handling
24 :mod:`hooke.curve` classes.
31 from ..command import Command, Argument, Failure
32 from ..curve import Data
33 from ..plugin import Builtin
34 from ..plugin.playlist import current_playlist_callback
35 from ..util.calculus import derivative
36 from ..util.fft import unitary_avg_power_spectrum
37 from ..util.si import ppSI, join_data_label, split_data_label
41 class CurvePlugin (Builtin):
43 super(CurvePlugin, self).__init__(name='curve')
45 GetCommand(self), InfoCommand(self), DeltaCommand(self),
46 ExportCommand(self), DifferenceCommand(self),
47 DerivativeCommand(self), PowerSpectrumCommand(self)]
50 # Define common or complicated arguments
52 def current_curve_callback(hooke, command, argument, value):
55 playlist = current_playlist_callback(hooke, command, argument, value)
56 curve = playlist.current()
58 raise Failure('No curves in %s' % playlist)
61 CurveArgument = Argument(
62 name='curve', type='curve', callback=current_curve_callback,
64 :class:`hooke.curve.Curve` to act on. Defaults to the current curve
65 of the current playlist.
71 class GetCommand (Command):
72 """Return a :class:`hooke.curve.Curve`.
74 def __init__(self, plugin):
75 super(GetCommand, self).__init__(
76 name='get curve', arguments=[CurveArgument],
77 help=self.__doc__, plugin=plugin)
79 def _run(self, hooke, inqueue, outqueue, params):
80 outqueue.put(params['curve'])
82 class InfoCommand (Command):
83 """Get selected information about a :class:`hooke.curve.Curve`.
85 def __init__(self, plugin):
88 Argument(name='all', type='bool', default=False, count=1,
89 help='Get all curve information.'),
91 self.fields = ['name', 'path', 'experiment', 'driver', 'filetype', 'note',
92 'blocks', 'block sizes']
93 for field in self.fields:
95 name=field, type='bool', default=False, count=1,
96 help='Get curve %s' % field))
97 super(InfoCommand, self).__init__(
98 name='curve info', arguments=args,
99 help=self.__doc__, plugin=plugin)
101 def _run(self, hooke, inqueue, outqueue, params):
103 for key in self.fields:
104 fields[key] = params[key]
105 if reduce(lambda x,y: x and y, fields.values()) == False:
106 params['all'] = True # No specific fields set, default to 'all'
107 if params['all'] == True:
108 for key in self.fields:
111 for key in self.fields:
112 if fields[key] == True:
113 get = getattr(self, '_get_%s' % key.replace(' ', '_'))
114 lines.append('%s: %s' % (key, get(params['curve'])))
115 outqueue.put('\n'.join(lines))
117 def _get_name(self, curve):
120 def _get_path(self, curve):
123 def _get_experiment(self, curve):
124 return curve.info.get('experiment', None)
126 def _get_driver(self, curve):
129 def _get_filetype(self, curve):
130 return curve.info.get('filetype', None)
132 def _get_note(self, curve):
133 return curve.info.get('note', None)
135 def _get_blocks(self, curve):
136 return len(curve.data)
138 def _get_block_sizes(self, curve):
139 return [block.shape for block in curve.data]
142 class DeltaCommand (Command):
143 """Get distance information between two points.
145 With two points A and B, the returned distances are A-B.
147 def __init__(self, plugin):
148 super(DeltaCommand, self).__init__(
152 Argument(name='block', type='int', default=0,
154 Data block that points are selected from. For an approach/retract
155 force curve, `0` selects the approaching curve and `1` selects the
158 Argument(name='point', type='point', optional=False, count=2,
160 Indicies of points bounding the selected data.
162 Argument(name='SI', type='bool', default=False,
164 Return distances in SI notation.
167 help=self.__doc__, plugin=plugin)
169 def _run(self, hooke, inqueue, outqueue, params):
170 data = params['curve'].data[params['block']]
171 As = data[params['point'][0],:]
172 Bs = data[params['point'][1],:]
173 ds = [A-B for A,B in zip(As, Bs)]
174 if params['SI'] == False:
175 out = [(name, d) for name,d in zip(data.info['columns'], ds)]
178 for name,d in zip(data.info['columns'], ds):
179 n,units = split_data_label(name)
181 (n, ppSI(value=d, unit=units, decimals=2)))
185 class ExportCommand (Command):
186 """Export a :class:`hooke.curve.Curve` data block as TAB-delimeted
189 A "#" prefixed header will optionally appear at the beginning of
190 the file naming the columns.
192 def __init__(self, plugin):
193 super(ExportCommand, self).__init__(
197 Argument(name='block', aliases=['set'], type='int', default=0,
199 Data block to save. For an approach/retract force curve, `0` selects
200 the approaching curve and `1` selects the retracting curve.
202 Argument(name='output', type='file', default='curve.dat',
204 File name for the output data. Defaults to 'curve.dat'
206 Argument(name='header', type='bool', default=True,
208 True if you want the column-naming header line.
211 help=self.__doc__, plugin=plugin)
213 def _run(self, hooke, inqueue, outqueue, params):
214 data = params['curve'].data[params['block']]
216 f = open(params['output'], 'w')
217 if params['header'] == True:
218 f.write('# %s \n' % ('\t'.join(data.info['columns'])))
219 numpy.savetxt(f, data, delimiter='\t')
222 class DifferenceCommand (Command):
223 """Calculate the difference between two blocks of data.
225 def __init__(self, plugin):
226 super(DifferenceCommand, self).__init__(
227 name='block difference',
230 Argument(name='block one', aliases=['set one'], type='int',
233 Block A in A-B. For an approach/retract force curve, `0` selects the
234 approaching curve and `1` selects the retracting curve.
236 Argument(name='block two', aliases=['set two'], type='int',
238 help='Block B in A-B.'),
239 Argument(name='x column', type='int', default=0,
241 Column of data to return as x values.
243 Argument(name='y column', type='int', default=1,
245 Column of data block to difference.
248 help=self.__doc__, plugin=plugin)
250 def _run(self, hooke, inqueue, outqueue, params):
251 a = params['curve'].data[params['block one']]
252 b = params['curve'].data[params['block two']]
253 assert a[:,params['x column']] == b[:,params['x column']]
254 out = Data((a.shape[0],2), dtype=a.dtype)
255 out[:,0] = a[:,params['x column']]
256 out[:,1] = a[:,params['y column']] - b[:,params['y column']]
259 class DerivativeCommand (Command):
260 """Calculate the derivative (actually, the discrete differentiation)
261 of a curve data block.
263 See :func:`hooke.util.calculus.derivative` for implementation
266 def __init__(self, plugin):
267 super(DerivativeCommand, self).__init__(
268 name='block derivative',
271 Argument(name='block', aliases=['set'], type='int', default=0,
273 Data block to differentiate. For an approach/retract force curve, `0`
274 selects the approaching curve and `1` selects the retracting curve.
276 Argument(name='x column', type='string',
278 Column of data block to differentiate with respect to.
280 Argument(name='f column', type='string',
282 Column of data block to differentiate.
284 Argument(name='weights', type='dict', default={-1:-0.5, 1:0.5},
286 Weighting scheme dictionary for finite differencing. Defaults to
287 central differencing.
289 Argument(name='output column name', type='string',
291 Name of the new column for storing the derivative (without units, defaults to `derivative of <f column name> with respect to <x column name>`).
294 help=self.__doc__, plugin=plugin)
296 def _run(self, hooke, inqueue, outqueue, params):
297 data = params['curve'].data[params['block']]
298 # HACK? rely on params['curve'] being bound to the local hooke
299 # playlist (i.e. not a copy, as you would get by passing a
300 # curve through the queue). Ugh. Stupid queues. As an
301 # alternative, we could pass lookup information through the
303 new = Data((data.shape[0], data.shape[1]+1), dtype=data.dtype)
304 new.info = copy.deepcopy(data.info)
307 x_col = data.info['columns'].index(params['x column'])
308 f_col = data.info['columns'].index(params['f column'])
310 block, x_col=x_col, f_col=f_col, weights=params['weights'])
312 x_name,x_units = split_data_label(params['x column'])
313 f_name,f_units = split_data_label(params['f column'])
314 if params['output column name'] == None:
315 params['output column name'] = (
316 'derivative of %s with respect to %s' % (
317 params['f column'], params['x column']))
319 new.info['columns'].append(
320 join_data_label(params['output distance column'],
321 '%s/%s' % (f_units/x_units)))
323 params['curve'].data[params['block']] = new
326 class PowerSpectrumCommand (Command):
327 """Calculate the power spectrum of a data block.
329 def __init__(self, plugin):
330 super(PowerSpectrumCommand, self).__init__(
331 name='block power spectrum',
334 Argument(name='block', aliases=['set'], type='int', default=0,
336 Data block to act on. For an approach/retract force curve, `0`
337 selects the approaching curve and `1` selects the retracting curve.
339 Argument(name='column', type='string', optional=False,
341 Name of the data block column containing to-be-transformed data.
343 Argument(name='bounds', type='point', optional=True, count=2,
345 Indicies of points bounding the selected data.
347 Argument(name='freq', type='float', default=1.0,
351 Argument(name='freq units', type='string', default='Hz',
353 Units for the sampling frequency.
355 Argument(name='chunk size', type='int', default=2048,
357 Number of samples per chunk. Use a power of two.
359 Argument(name='overlap', type='bool', default=False,
361 If `True`, each chunk overlaps the previous chunk by half its length.
362 Otherwise, the chunks are end-to-end, and not overlapping.
364 Argument(name='output block name', type='string',
366 Name of the new data block (defaults to `power spectrum of <source block name> <source column name>`) for storing the power spectrum.
369 help=self.__doc__, plugin=plugin)
371 def _run(self, hooke, inqueue, outqueue, params):
372 data = params['curve'].data[params['block']]
373 col = data.info['columns'].index(params['column'])
376 d = d[params['bounds'][0]:params['bounds'][1]]
377 freq_axis,power = unitary_avg_power_spectrum(
378 d, freq=params['freq'],
379 chunk_size=params['chunk size'],
380 overlap=params['overlap'])
382 name,data_units = split_data_label(params['column'])
383 b = Data((len(freq_axis),2), dtype=data.dtype)
384 if params['output block name'] == None:
385 params['output block name'] = 'power spectrum of %s %s' % (
386 params['output block name'], data.info['name'], params['column'])
387 b.info['name'] = params['output block name']
388 b.info['columns'] = [
389 join_data_label('frequency axis', params['freq units']),
390 join_data_label('power density',
391 '%s^2/%s' % (data_units, params['freq units'])),
395 params['curve'].data.append(b)