X-Git-Url: http://git.tremily.us/?a=blobdiff_plain;f=hooke%2Fplugin%2Fcut.py;h=ad1a0679a00781f790964db28a7dbdab2a831ea1;hb=4a1eb29445b37543e5249e4a6e8bcdb77538cb69;hp=33010e4ff8cc8e9478f0d0ae73a472a63a1405ab;hpb=ba4daa53462d81c9302a91d959612d534efc6be7;p=hooke.git diff --git a/hooke/plugin/cut.py b/hooke/plugin/cut.py index 33010e4..ad1a067 100644 --- a/hooke/plugin/cut.py +++ b/hooke/plugin/cut.py @@ -1,28 +1,75 @@ -"""Defines :class:`CutPlugin` and :class:`CutCommand`. +# Copyright (C) 2009-2010 Fabrizio Benedetti +# Massimo Sandal +# W. Trevor King +# +# 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 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 +# . + +"""The `cut` module provides :class:`CutPlugin` and +:class:`CutCommand`. """ -from . import Plugin, Command, Argument +import numpy + +from ..command import Command, Argument, Failure +from ..plugin import Plugin class CutPlugin (Plugin): def __init__(self): super(CutPlugin, self).__init__(name='cut') + self._commands = [CutCommand(self)] + + +# Define common or complicated arguments + +def current_curve_callback(hooke, command, argument, value): + playlist = hooke.playlists.current() + if playlist == None: + raise Failure('No playlists loaded') + curve = playlist.current() + if curve == None: + raise Failure('No curves in playlist %s' % playlist.name) + return curve + +CurveArgument = Argument( + name='curve', type='curve', callback=current_curve_callback, + help=""" +:class:`hooke.curve.Curve` to cut from. Defaults to the current curve. +""".strip()) - def commands(self): - return [CutCommand()] + +# Define commands class CutCommand (Command): - def __init__(self): + """Cut the selected signal between two points and write it to a file. + + The data is saved in TAB-delimited ASCII text. A "#" prefixed + header will optionally appear at the beginning of the file naming + the columns. + """ + def __init__(self, plugin): super(CutCommand, self).__init__( name='cut', arguments=[ - Argument(name='curve', type='curve', optional=False, help=""" -:class:``hooke.curve.Curve`` to cut from. -""".strip()), + CurveArgument, Argument(name='block', aliases=['set'], type='int', default=0, 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='bounds', type='point', optional=False, count=2, help=""" @@ -31,16 +78,18 @@ Indicies of points bounding the selected data. Argument(name='output', type='file', default='cut.dat', help=""" File name for the output data. +""".strip()), + Argument(name='header', type='bool', default=True, + help=""" +True if you want the column-naming header line. """.strip()), ], - help=""" -Cut the selected signal between two points and write it to a file. + help=self.__doc__, plugin=plugin) -The data is saved in TAB-delimited ASCII text, where the first column -is "x" and the second is "y". There is no header row. -""".strip()) + def _run(self, hooke, inqueue, outqueue, params): + if params['curve'] == None: + params['curve'] = hooke.playlists.current().current() - def _run(inqueue, outqueue, params): i_min = min([p.index for p in params['points']]) i_max = max([p.index for p in params['points']]) @@ -48,6 +97,8 @@ is "x" and the second is "y". There is no header row. cut_data = data[i_min:i_max+1,:] # slice rows from row-major data # +1 to include data[i_max] row - f = open(params['output'], 'w') - cut_data.tofile(f, sep='\t') + f = open(params['output'], 'w') + if params['header'] == True: + f.write('# %s \n' % ('\t'.join(cut_data.info['columns']))) + numpy.savetxt(f, cut_data, delimiter='\t') f.close()