1 # Copyright (C) 2010 Fibrin's Benedetti
2 # W. Trevor King <wking@drexel.edu>
4 # This file is part of Hooke.
6 # Hooke is free software: you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation, either
9 # version 3 of the License, or (at your option) any later version.
11 # Hooke is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with Hooke. If not, see
18 # <http://www.gnu.org/licenses/>.
20 """The ``curve`` module provides :class:`CurvePlugin` and several
21 associated :class:`hooke.command.Command`\s for handling
22 :mod:`hooke.curve` classes.
25 from ..command import Command, Argument, Failure
26 from ..plugin import Builtin
27 from ..plugin.playlist import current_playlist_callback
30 class CurvePlugin (Builtin):
32 super(CurvePlugin, self).__init__(name='curve')
35 return [InfoCommand(), ]
38 # Define common or complicated arguments
40 def current_curve_callback(hooke, command, argument, value):
43 playlist = current_playlist_callback(hooke, command, argument, value)
44 curve = playlist.current()
46 raise Failure('No curves in %s' % playlist)
49 CurveArgument = Argument(
50 name='curve', type='curve', callback=current_curve_callback,
52 :class:`hooke.curve.Curve` to act on. Defaults to the current curve
53 of the current playlist.
59 class InfoCommand (Command):
60 """Get selected information about a :class:`hooke.curve.Curve`.
65 Argument(name='all', type='bool', default=False, count=1,
66 help='Get all curve information.'),
68 self.fields = ['name', 'path', 'experiment', 'driver', 'filetype', 'note',
69 'blocks', 'block sizes']
70 for field in self.fields:
72 name=field, type='bool', default=False, count=1,
73 help='Get curve %s' % field))
74 super(InfoCommand, self).__init__(
75 name='curve info', arguments=args, help=self.__doc__)
77 def _run(self, hooke, inqueue, outqueue, params):
79 for key in self.fields:
80 fields[key] = params[key]
81 if reduce(lambda x,y: x and y, fields.values()) == False:
82 params['all'] = True # No specific fields set, default to 'all'
83 if params['all'] == True:
84 for key in self.fields:
87 for key in self.fields:
88 if fields[key] == True:
89 get = getattr(self, '_get_%s' % key.replace(' ', '_'))
90 lines.append('%s: %s' % (key, get(params['curve'])))
91 outqueue.put('\n'.join(lines))
93 def _get_name(self, curve):
96 def _get_path(self, curve):
99 def _get_experiment(self, curve):
100 return curve.info.get('experiment', None)
102 def _get_driver(self, curve):
105 def _get_filetype(self, curve):
106 return curve.info.get('filetype', None)
108 def _get_note(self, curve):
109 return curve.info.get('note', None)
111 def _get_blocks(self, curve):
112 return len(curve.data)
114 def _get_block_sizes(self, curve):
115 return [block.shape for block in curve.data]
118 class ExportCommand (Command):
119 """Export a :class:`hooke.curve.Curve` data block as TAB-delimeted
123 super(InfoCommand, self).__init__(
127 Argument(name='block', aliases=['set'], type='int', default=0,
129 Data block to save. For an approach/retract force curve, `0` selects
130 the approacing curve and `1` selects the retracting curve.
132 Argument(name='output', type='file', default='curve.dat',
134 File name for the output data. Defaults to 'curve.dat'
139 def _run(self, hooke, inqueue, outqueue, params):
140 data = params['curve'].data[params['index']]
141 f = open(params['output'], 'w')
142 data.tofile(f, sep='\t')