wrapperutils.py 41 KB
Newer Older
1
2
#!/usr/bin/env python
#
Paul McCarthy's avatar
Paul McCarthy committed
3
4
# wrapperutils.py - Functions and decorators used by the FSL wrapper
# functions.
5
6
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
7
# Author: Martin Craig <martin.craig@eng.ox.ac.uk>
8
#
Paul McCarthy's avatar
Paul McCarthy committed
9
10
11
12
"""This module contains functions and decorators used by the FSL wrapper
functions.


Paul McCarthy's avatar
Paul McCarthy committed
13
The :func:`cmdwrapper` and :func:`fslwrapper` functions are convenience
14
15
16
17
18
19
20
21
22
23
24
25
26
27
decorators which allow you to write your wrapper function such that it simply
generates the command-line needed to respectively run a standard shell
command or a FSL command. For example::


    @fslwrapper
    def fslreorient2std(input, output):
        return ['fslreorient2std', input, output]


When this ``fslreorient2std`` function is called, the ``fslwrapper`` decorator
will take care of invoking the command in a standardised way.


Paul McCarthy's avatar
Paul McCarthy committed
28
The :func:`applyArgStyle` function can be used to automatically convert
29
30
31
32
33
34
35
36
37
38
39
40
41
keyword arguments into command-line arguments, based on a set of standard
patterns. For example::


    @fslwrapper
    def flirt(src, ref, **kwargs):
        cmd  = ['flirt', '-in', src, '-ref', ref]
        return cmd + applyArgStyle('-=', **kwargs)


The :func:`fileOrImage` and :func:`fileOrArray` functions can be used to
decorate a wrapper function such that in-memory ``nibabel`` images or Numpy
arrays can be passed in as arguments - they will be automatically saved out to
Paul McCarthy's avatar
Paul McCarthy committed
42
files, and then the file names passed into the wrapper function. For example::
43
44
45
46
47
48
49
50
51
52
53
54
55


    @fileOrImage('src', 'ref')
    @fslwrapper
    def flirt(src, ref, **kwargs):
        cmd  = ['flirt', '-in', src, '-ref', ref]
        return cmd + applyArgStyle('-=', **kwargs)


Now this ``flirt`` function can be called either with file names, or
``nibabel`` images.


56
57
58
59
60
61
62
.. note:: Because the :func:`fileOrImage` and :func:`fileOrArray` decorators
          manipulate the return value of the decorated function, they should
          be applied *after* any other decorators. Furthermore, if you need to
          apply both a ``fileOrImage`` and ``fileOrArray`` decorator to a
          function, they should be grouped together, e.g.::

              @fileOrImage('a', 'b')
Paul McCarthy's avatar
Paul McCarthy committed
63
              @fileOrArray('c', 'd')
64
65
66
67
68
              @fslwrapper
              def func(**kwargs):
                  ...


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
Command outputs can also be loaded back into memory by using the special
:data:`LOAD` value when calling a wrapper function. For example::


    @fileOrImage('src', 'ref', 'out')
    @fslwrapper
    def flirt(src, ref, **kwargs):
        cmd  = ['flirt', '-in', src, '-ref', ref]
        return cmd + applyArgStyle('-=', **kwargs)


If we set the ``out`` argument to ``LOAD``, the output image will be loaded
and returned::

    src     = nib.load('src.nii')
    ref     = nib.load('ref.nii')
    aligned = flirt(src, ref, out=LOAD)['out']
Paul McCarthy's avatar
Paul McCarthy committed
86
"""
87
88


89
90
91
92
93
94
95
96
97
import itertools       as it
import os.path         as op
import collections.abc as abc
import                    os
import                    re
import                    sys
import                    glob
import                    random
import                    string
98
import                    pathlib
99
100
101
102
103
104
105
import                    fnmatch
import                    inspect
import                    logging
import                    tempfile
import                    warnings
import                    functools

106
107
108

import            six
import nibabel as nib
Paul McCarthy's avatar
Paul McCarthy committed
109
import numpy   as np
110

111
import fsl.utils.run     as run
112
113
import fsl.utils.path    as fslpath
import fsl.utils.tempdir as tempdir
114
115
116
import fsl.data.image    as fslimage


117
118
119
log = logging.getLogger(__name__)


Paul McCarthy's avatar
Paul McCarthy committed
120
121
122
123
124
def _update_wrapper(wrapper, wrapped, *args, **kwargs):
    """Replacement for the built-in ``functools.update_wrapper``. This
    implementation ensures that the wrapper function has an attribute
    called ``__wrapped__``, which refers to the ``wrapped`` function.

125
    This custom function is only needed in Python versions < 3.4.
Paul McCarthy's avatar
Paul McCarthy committed
126
127
128
129
130
    """

    wrapper = functools.update_wrapper(wrapper, wrapped, *args, **kwargs)

    # Python >= 3.4 does things right
Paul McCarthy's avatar
Paul McCarthy committed
131
    if (sys.version_info[0] * 10 + sys.version_info[1]) < 34:
Paul McCarthy's avatar
Paul McCarthy committed
132
133
        wrapper.__wrapped__ = wrapped
    return wrapper
134
135


Paul McCarthy's avatar
Paul McCarthy committed
136
137
138
139
140
141
def _unwrap(func):
    """Replacement for the built-in ``inspect.unwrap`` function, which
    is not present in Python versions prior to 3.4.
    """

    # Python >= 3.4 has an inspect.unwrap function
Paul McCarthy's avatar
Paul McCarthy committed
142
    if (sys.version_info[0] * 10 + sys.version_info[1]) >= 34:
Paul McCarthy's avatar
Paul McCarthy committed
143
144
145
146
147
148
149
150
151
        return inspect.unwrap(func)

    # Otherwise we follow the __wrapped__ chain ourselves
    if hasattr(func, '__wrapped__'):
        return _unwrap(func.__wrapped__)

    return func


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def genxwrapper(func, runner):
    """This function is used by :func:`cmdwrapper` and :func:`fslwrapper`.
    It is not intended to be used in any other circumstances.

    This function generates a wrapper function which calls ``func`` to
    generate a command-line call, and then uses ``runner`` to invoke that
    command.

    ``func`` is assumed to be a wrapper function which generates a command-
    line. ``runner`` is assumed to be Either :func:`.run.run` or
    :func:`.run.runfsl`.

    The generated wrapper function will pass all of its arguments to ``func``,
    and will then pass the generated command-line to ``runner``, returning
    whatever is returned.

    The following keyword arguments will be intercepted by the wrapper
    function, and will *not* be passed to ``func``:
      - ``stdout``:   Passed to ``runner``. Defaults to ``True``.
      - ``stderr``:   Passed to ``runner``. Defaults to ``True``.
      - ``exitcode``: Passed to ``runner``. Defaults to ``False``.
      - ``submit``:   Passed to ``runner``. Defaults to ``None``.
      - ``log``:      Passed to ``runner``. Defaults to ``{'tee':True}``.
175
      - ``cmdonly``:  Passed to ``runner``. Defaults to ``False``.
176
177
178

    :arg func:   A function which generates a command line.
    :arg runner: Either :func:`.run.run` or :func:`.run.runfsl`.
179
    """
180

181
    def wrapper(*args, **kwargs):
182
183
        stdout   = kwargs.pop('stdout',   True)
        stderr   = kwargs.pop('stderr',   True)
184
        exitcode = kwargs.pop('exitcode', False)
185
        submit   = kwargs.pop('submit',   None)
186
        cmdonly  = kwargs.pop('cmdonly',  False)
187
188
        log      = kwargs.pop('log',      {'tee' : True})
        cmd      = func(*args, **kwargs)
189

190
191
192
193
194
195
196
197
        return runner(cmd,
                      stderr=stderr,
                      log=log,
                      submit=submit,
                      cmdonly=cmdonly,
                      stdout=stdout,
                      exitcode=exitcode)

198
199
200
    return _update_wrapper(wrapper, func)


201
202
203
204
205
206
207
208
209
210
def cmdwrapper(func):
    """This decorator can be used on functions which generate a command line.
    It will pass the return value of the function to the
    :func:`fsl.utils.run.run` function in a standardised manner.

    See the :func:`genxwrapper` function for details.
    """
    return genxwrapper(func, run.run)


211
212
213
214
def fslwrapper(func):
    """This decorator can be used on functions which generate a FSL command
    line. It will pass the return value of the function to the
    :func:`fsl.utils.run.runfsl` function in a standardised manner.
215
216

    See the :func:`genxwrapper` function for details.
217
    """
218
    return genxwrapper(func, run.runfsl)
219
220


Paul McCarthy's avatar
Paul McCarthy committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
SHOW_IF_TRUE = object()
"""Constant to be used in the ``valmap`` passed to the :func:`applyArgStyle`
function.

When a ``SHOW_IF_TRUE`` argument is ``True``, it is added to the generated
command line arguments.
"""


HIDE_IF_TRUE = object()
"""Constant to be used in the ``valmap`` passed to the :func:`applyArgStyle`
function.

When a ``HIDE_IF_TRUE`` argument is ``True``, it is suppressed from the
generated command line arguments.
"""
237
238


239
240
241
242
243
244
def applyArgStyle(style,
                  valsep=None,
                  argmap=None,
                  valmap=None,
                  singlechar_args=False,
                  **kwargs):
Paul McCarthy's avatar
Paul McCarthy committed
245
246
247
248
    """Turns the given ``kwargs`` into command line options. This function
    is intended to be used to automatically generate command line options
    from arguments passed into a Python function.

249
250
251
252
253
254
255
    The ``style`` and ``valsep`` arguments control how key-value pairs
    are converted into command-line options:


    =========  ==========  ===========================
    ``style``  ``valsep``  Result
    =========  ==========  ===========================
Paul McCarthy's avatar
Paul McCarthy committed
256
257
258
259
260
261
262
263
264
265
266
267
    ``'-'``    ``' '``     ``-name val1 val2 val3``
    ``'-'``    ``'"'``     ``-name "val1 val2 val3"``
    ``'-'``    ``','``     ``-name val1,val2,val3``
    ``'--'``   ``' '``     ``--name val1 val2 val3``
    ``'--'``   ``'"'``     ``--name "val1 val2 val3"``
    ``'--'``   ``','``     ``--name val1,val2,val3``
    ``'-='``   ``' '``     Not supported
    ``'-='``   ``'"'``     ``-name="val1 val2 val3"``
    ``'-='``   ``','``     ``-name=val1,val2,val3``
    ``'--='``  ``' '``     Not supported
    ``'--='``  ``'"'``     ``--name="val1 val2 val3"``
    ``'--='``  ``','``     ``--name=val1,val2,val3``
268
269
270
    =========  ==========  ===========================


Paul McCarthy's avatar
Paul McCarthy committed
271
    :arg style:  Controls how the ``kwargs`` are converted into command-line
272
273
274
275
276
                 options - must be one of ``'-'``, ``'--'``, ``'-='``, or
                 ``'--='``.

    :arg valsep: Controls how the values passed to command-line options
                 which expect multiple arguments are delimited - must be
277
278
                 one of ``' '``, ``','`` or ``'"'``. Defaults to ``' '``
                 if ``'=' not in style``, ``','`` otherwise.
Paul McCarthy's avatar
Paul McCarthy committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

    :arg argmap: Dictionary of ``{kwarg-name : cli-name}`` mappings. This can
                 be used if you want to use different argument names in your
                 Python function for the command-line options.

    :arg valmap: Dictionary of ``{cli-name : value}`` mappings. This can be
                 used to define specific semantics for some command-line
                 options. Acceptable values for ``value`` are as follows

                  - :data:`SHOW_IF_TRUE` - if the argument is present, and
                    ``True`` in ``kwargs``, the command line option
                    will be added (without any arguments).

                  - :data:`HIDE_IF_TRUE` - if the argument is present, and
                    ``False`` in ``kwargs``, the command line option
                    will be added (without any arguments).

                  - Any other constant value. If the argument is present
                    in ``kwargs``, its command-line option will be
                    added, with the constant value as its argument.

                 The argument for any options not specified in the ``valmap``
                 will be converted into strings.
302

303
304
305
    :arg singlechar_args: If True, single character arguments always take a
                          single hyphen prefix (e.g. -h) regardless of the
                          style.
306

Paul McCarthy's avatar
Paul McCarthy committed
307
308
309
310
311
    :arg kwargs: Arguments to be converted into command-line options.

    :returns:    A list containing the generated command-line options.
    """

312
313
314
315
    if valsep is None:
        if '=' in style: valsep = ','
        else:            valsep = ' '

Paul McCarthy's avatar
Paul McCarthy committed
316
317
    if style not in ('-', '--', '-=', '--='):
        raise ValueError('Invalid style: {}'.format(style))
318
319
320
321
322
323
324
325
326
    if valsep not in (' ', ',', '"'):
        raise ValueError('Invalid valsep: {}'.format(valsep))

    # we don't handle the case where '=' in
    # style, and valsep == ' ', because no
    # sane CLI app would do this. Right?
    if '=' in style and valsep == ' ':
        raise ValueError('Incompatible style and valsep: s={} v={}'.format(
            style, valsep))
Paul McCarthy's avatar
Paul McCarthy committed
327
328
329
330
331

    if argmap is None: argmap = {}
    if valmap is None: valmap = {}

    def fmtarg(arg):
332
333
334
335
        if   style in ('-',  '-=') or (singlechar_args and len(arg) == 1):
            arg =  '-{}'.format(arg)
        elif style in ('--', '--='):
            arg = '--{}'.format(arg)
336
337
        return arg

338
    # always returns a sequence
Paul McCarthy's avatar
Paul McCarthy committed
339
    def fmtval(val):
340
        if     isinstance(val, abc.Sequence) and \
341
           not isinstance(val, six.string_types):
342
343
344
345
346

            val = [str(v) for v in val]
            if   valsep == ' ': return val
            elif valsep == '"': return [' '   .join(val)]
            else:               return [valsep.join(val)]
347
        else:
348
349
350
351
352
353
354
355
            return [str(val)]

    # val is assumed to be a sequence
    def fmtargval(arg, val):
        # if '=' in style, val will
        # always be a single string
        if '=' in style: return ['{}={}'.format(arg, val[0])]
        else:            return [arg] + val
356
357
358
359
360

    args = []

    for k, v in kwargs.items():

361
362
        if v is None: continue

363
        k    = argmap.get(k, k)
Paul McCarthy's avatar
Paul McCarthy committed
364
365
366
        mapv = valmap.get(k, fmtval(v))
        k    = fmtarg(k)

367
368
369
370
        if mapv in (SHOW_IF_TRUE, HIDE_IF_TRUE):
            if (mapv is SHOW_IF_TRUE and     v) or \
               (mapv is HIDE_IF_TRUE and not v):
                args.append(k)
371
        else:
372
            args.extend(fmtargval(k, mapv))
373
374
375
376

    return args


377
def namedPositionals(func, args):
378
    """Given a function, and a sequence of positional arguments destined
379
    for that function, identifies the name for each positional argument.
380
    Variable positional arguments are given an automatic name.
381

382
383
    :arg func: Function which will accept ``args`` as positionals.
    :arg args: Tuple of positional arguments to be passed to ``func``.
384
    """
Paul McCarthy's avatar
Paul McCarthy committed
385

386
387
388
389
390
391
392
393
394
395
    # Current implementation will
    # result in naming collisions
    # for something like this:
    #
    # def func(args0, *args):
    #     ...
    # because of automatic vararg
    # naming. But who would write
    # a function like that anyway?

396
397
    # Remove any decorators
    # from the function
Paul McCarthy's avatar
Paul McCarthy committed
398
399
    func = _unwrap(func)

400
401
402
    # getargspec is the only way to
    # get the names of positional
    # arguments in Python 2.x.
Paul McCarthy's avatar
Paul McCarthy committed
403
    if sys.version_info[0] < 3:
404
405
406
        spec     = inspect.getargspec(func)
        argnames = spec.args
        varargs  = spec.varargs
Paul McCarthy's avatar
Paul McCarthy committed
407

408
409
    # But getargspec is deprecated
    # in python 3.x
Paul McCarthy's avatar
Paul McCarthy committed
410
411
412
413
414
415
    else:

        # getfullargspec is deprecated in
        # python 3.5, but not in python 3.6.
        with warnings.catch_warnings():
            warnings.filterwarnings('ignore', category=DeprecationWarning)
416
417
418
            spec     = inspect.getfullargspec(func)
            argnames = spec.args
            varargs  = spec.varargs
419

420
421
422
    # we only care about the arguments
    # that are being passed in
    argnames = argnames[:len(args)]
423

424
425
426
427
    # make up names for varargs
    nvarargs = len(args) - len(argnames)
    if varargs is not None and nvarargs > 0:
        argnames += ['{}{}'.format(varargs, i) for i in range(nvarargs)]
428

429
    return argnames
430
431


Paul McCarthy's avatar
Paul McCarthy committed
432
LOAD = object()
433
"""Constant used by the :class:`FileOrThing` class to indicate that an output
Paul McCarthy's avatar
Paul McCarthy committed
434
file should be loaded into memory and returned as a Python object.
435
436
437
"""


438
class FileOrThing(object):
Paul McCarthy's avatar
Paul McCarthy committed
439
440
441
442
443
    """Decorator which ensures that certain arguments which are passed into the
    decorated function are always passed as file names. Both positional and
    keyword arguments can be specified.


444
    The ``FileOrThing`` class is not intended to be used directly - see the
Paul McCarthy's avatar
Paul McCarthy committed
445
446
447
    :func:`fileOrImage` and :func:`fileOrArray` decorator functions for more
    details.

448

Paul McCarthy's avatar
Paul McCarthy committed
449
450
451
452
453
454
455
456
457
458
459
460
    These decorators are intended for functions which wrap a command-line tool,
    i.e. where some inputs/outputs need to be specified as file names.


    **Inputs**


    Any arguments which are not of type ``Thing`` are passed through to the
    decorated function unmodified.  Arguments which are of type ``Thing`` are
    saved to a temporary file, and the name of that file is passed to the
    function.

461

Paul McCarthy's avatar
Paul McCarthy committed
462
    **Outputs**
463
464


Paul McCarthy's avatar
Paul McCarthy committed
465
    If an argument is given the special :data:`LOAD` value, it is assumed
Paul McCarthy's avatar
Paul McCarthy committed
466
467
468
    to be an output argument. In this case, it is replaced with a temporary
    file name then, after the function has completed, that file is loaded
    into memory, and the value returned (along with the function's output,
Paul McCarthy's avatar
Paul McCarthy committed
469
    and any other arguments with a value of ``LOAD``).
Paul McCarthy's avatar
Paul McCarthy committed
470
471
472
473
474


    **Return value**


475
    Functions decorated with a ``FileOrThing`` decorator will always return a
476
    ``dict``-like object, where the function's actual return value is
477
    accessible via an attribute called ``stdout``. All output arguments with a
478
    value of ``LOAD`` will be present as dictionary entries, with the keyword
479
480
481
482
    argument names used as keys; these values will also be accessible as
    attributes of the results dict, when possible. Any ``LOAD`` output
    arguments which were not generated by the function will not be present in
    the dictionary.
Paul McCarthy's avatar
Paul McCarthy committed
483
484


485
    **Exceptions**
486
487


488
489
490
491
492
    The above description holds in all situations, except when arguments called
    ``submit`` and/or ``cmdonly`` are passed, and are set to values which
    evaluate to ``True``. In this case, the ``FileOrThing`` decorator will pass
    all arguments straight through to the decorated function, and will return
    its return value unchanged.
493
494
495

    This is because most functions that are decorated with the
    :func:`fileOrImage` or :func:`fileOrArray` decorators will invoke a call
496
    to :func:`.run.run` or :func:`.runfsl`, where:
497

498
499
500
501
      - a value of ``submit=True`` will cause the command to be executed
        asynchronously on a cluster platform.
      - a value of ``cmdonly=True`` will cause the command to *not* be executed,
        but instead the command that would have been executed is returned.
502
503

    A :exc:`ValueError` will be raised if the decorated function is called
504
505
    with ``submit=True`` and/or ``cmdonly=True``, and with any in-memory
    objects or ``LOAD`` symbols.
506
507


Paul McCarthy's avatar
Paul McCarthy committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
    **Example**


    As an example of using the ``fileOrArray`` decorator on a function
    which concatenates two files containing affine transformations, and
    saves the output to a file::

        # if atob, btoc, or output are passed
        # in as arrays, they are converted to
        # file names.
        @fileOrArray('atob', 'btoc', 'output')
        def concat(atob, btoc, output=None):

            # inputs are guaranteed to be files
            atob = np.loadtxt(atob)
            btoc = np.loadtxt(atoc)

            atoc = np.dot(btoc, atob)

            if output is not None:
                np.savetxt(output, atoc)

530
531
            return 'Done'

Paul McCarthy's avatar
Paul McCarthy committed
532
533
534
535

    Because we have decorated the ``concat`` function with :func:`fileToArray`,
    it can be called with either file names, or Numpy arrays::

536

Paul McCarthy's avatar
Paul McCarthy committed
537
538
        # All arguments are passed through
        # unmodified - the output will be
539
        # saved to a file called atoc.mat.
Paul McCarthy's avatar
Paul McCarthy committed
540
541
        concat('atob.txt', 'btoc.txt', 'atoc.mat')

542
543
        # The function's return value
        # is accessed via an attribute called
544
545
        # "stdout" on the dict
        assert concat('atob.txt', 'btoc.txt', 'atoc.mat').stdout == 'Done'
546
547
548

        # Outputs to be loaded into memory
        # are returned in a dictionary,
549
550
551
        # with argument names as keys. Values
        # can be accessed as dict items, or
        # as attributes.
552
        atoc = concat('atob.txt', 'btoc.txt', LOAD)['atoc']
553
        atoc = concat('atob.txt', 'btoc.txt', LOAD).atoc
Paul McCarthy's avatar
Paul McCarthy committed
554

555
556
557
558
559
        # In-memory inputs are saved to
        # temporary files, and those file
        # names are passed to the concat
        # function.
        atoc = concat(np.diag([2, 2, 2, 0]),
560
                      np.diag([3, 3, 3, 3]), LOAD).atoc
561
562
563


    **Using with other decorators**
Paul McCarthy's avatar
Paul McCarthy committed
564
565


566
567
    ``FileOrThing`` decorators can be chained with other ``FileOrThing``
    decorators, and other decorators.  When multiple ``FileOrThing``
Paul McCarthy's avatar
Paul McCarthy committed
568
569
570
571
    decorators are used on a single function, the outputs from each decorator
    are merged together into a single dict-like object.


572
    ``FileOrThing`` decorators can be used with any other decorators
573
    **as long as** they do not manipulate the return value, and as long as
574
    the ``FileOrThing`` decorators are adjacent to each other.
575
576
577
    """


578
    class Results(dict):
579
        """A custom ``dict`` type used to return outputs from a function
580
        decorated with ``FileOrThing``. All outputs are stored as dictionary
581
582
583
        items, with the argument name as key, and the output object (the
        "thing") as value.

584
585
        Where possible (i.e. for outputs named with a valid Python
        identifier), the outputs are also made accessible as attributes of
586
        the ``Results`` object.
587

588
        The decorated function's actual return value is accessible via the
589
        :meth:`stdout` property.
590
        """
591
592


593
        def __init__(self, stdout):
594
            """Create a ``Results`` dict.
595

Paul McCarthy's avatar
Paul McCarthy committed
596
            :arg stdout: Return value of the decorated function (typically a
597
598
                         tuple containing the standard output and error of the
                         underlying command).
599
            """
600
            super().__init__()
601
602
603
604
            self.__stdout = stdout


        def __setitem__(self, key, val):
605
            """Add an item to the dict. The item is also added as an attribute.
606
607
            """
            super().__setitem__(key, val)
608
            setattr(self, key, val)
609

610

611
        @property
612
        def stdout(self):
613
            """Access the return value of the decorated function. """
614
            return self.__stdout
615
616


617
618
619
620
621
622
    def __init__(self,
                 func,
                 prepIn,
                 prepOut,
                 load,
                 removeExt,
Paul McCarthy's avatar
Paul McCarthy committed
623
624
                 *args,
                 **kwargs):
625
        """Initialise a ``FileOrThing`` decorator.
Paul McCarthy's avatar
Paul McCarthy committed
626

627
628
629
630
        :arg func:      The function to be decorated.

        :arg prepIn:    Function which returns a file name to be used in
                        place of an input argument.
631

632
633
        :arg prepOut:   Function which generates a file name to use for
                        arguments that were set to :data:`LOAD`.
Paul McCarthy's avatar
Paul McCarthy committed
634

635
        :arg load:      Function which is called to load items for arguments
636
637
                        that were set to :data:`LOAD`. Must accept the
                        following arguments:
Paul McCarthy's avatar
Paul McCarthy committed
638

639
640
                         - the name of the argument
                         - path to the file to be loaded
641

642
643
644
        :arg removeExt: Function which can remove a file extension from a file
                        path.

Paul McCarthy's avatar
Paul McCarthy committed
645
646
647
648
649
650
651
652
        :arg outprefix: Must be passed as a keyword argument. The name of a
                        positional or keyword argument to the function, which
                        specifies an output file name prefix.  All other
                        arguments with names that begin with this prefix may
                        be interpreted as things to ``LOAD``.

        All other positional arguments are interpreted as the names of the
        arguments to the function which will be handled by this
653
        ``FileOrThing`` decorator. If not provided, *all* arguments passed to
Paul McCarthy's avatar
Paul McCarthy committed
654
        the function will be handled.
655

656
657
658
659
660
661

        The ``prepIn`` and ``prepOut`` functions must accept the following
        positional arguments:

          - A directory in which all temporary input/output files should be
            stored
Paul McCarthy's avatar
Paul McCarthy committed
662

663
664
665
666
          - The name of the keyword argument to be processed

          - The argument value that was passed in
        """
667
668
669
670
        self.__func      = func
        self.__prepIn    = prepIn
        self.__prepOut   = prepOut
        self.__load      = load
671
        self.__removeExt = removeExt
Paul McCarthy's avatar
Paul McCarthy committed
672
673
        self.__things    = args
        self.__outprefix = kwargs.get('outprefix', None)
Paul McCarthy's avatar
Paul McCarthy committed
674
675


676
    def __call__(self, *args, **kwargs):
677
        """Function which calls ``func``, ensuring that any arguments of
Paul McCarthy's avatar
Paul McCarthy committed
678
        type ``Thing`` are saved to temporary files, and any arguments
Paul McCarthy's avatar
Paul McCarthy committed
679
        with the value :data:`LOAD` are loaded and returned.
680

681
        All other arguments are passed through to ``func``.
682
        """
Paul McCarthy's avatar
Paul McCarthy committed
683

684
685
        func     = self.__func
        argnames = namedPositionals(func, args)
686

687
688
        # Special case - if fsl.utils.run[fsl] is
        # being decorated (e.g. via cmdwrapper/
689
690
691
692
693
        # fslwrapper), and submit=True or
        # cmdonly=True, this call will ultimately
        # submit the job to the cluster, or will
        # return the command that would have been
        # executed, and will return immediately.
694
695
696
697
698
699
700
        #
        # We error if we are given any in-memory
        # things, or LOAD symbols.
        #
        # n.b. testing values to be strings could
        # interfere with the fileOrText decorator.
        # Possible solution is to use pathlib?
701
702
        if kwargs.get('submit',  False) or \
           kwargs.get('cmdonly', False):
703
704
705
706
707
708
709
710
            allargs = {**dict(zip(argnames, args)), **kwargs}
            for name, val in allargs.items():
                if (name in self.__things) and \
                   (not isinstance(val, six.string_types)):
                    raise ValueError('Cannot use in-memory objects '
                                     'or LOAD with submit=True!')
            return func(*args, **kwargs)

711
712
        # If this FileOrThing is being called
        # by another FileOrThing don't create
713
714
715
716
717
718
719
720
        # another working directory. We do this
        # sneakily, by setting an attribute on
        # the wrapped function which stores the
        # current working directory.
        wrapped     = _unwrap(func)
        fot_workdir = getattr(wrapped, '_fot_workdir', None)
        parent      = fot_workdir is None

721
        # Create a tempdir to store any temporary
Paul McCarthy's avatar
Paul McCarthy committed
722
        # input/output things, but don't change
723
724
        # into it, as file paths passed to the
        # function may be relative.
725
        with tempdir.tempdir(changeto=False, override=fot_workdir) as td:
726

727
728
            log.debug('Redirecting LOADed outputs to %s', td)

729
730
            # Replace any things with file names.
            # Also get a list of LOAD outputs
731
            args = self.__prepareArgs(parent, td, argnames, args, kwargs)
732
            args, kwargs, outprefix, outfiles, prefixes = args
733

734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
            # The prefix/patterns may be
            # overridden by a parent FoT
            outprefix = getattr(wrapped, '_fot_outprefix', outprefix)
            prefixes  = getattr(wrapped, '_fot_prefixes',  prefixes)

            # if there are any other FileOrThings
            # in the decorator chain, get them to
            # use our working directory, and
            # prefixes, instead of creating their
            # own.
            if parent:
                setattr(wrapped, '_fot_workdir',   td)
                setattr(wrapped, '_fot_outprefix', outprefix)
                setattr(wrapped, '_fot_prefixes',  prefixes)

749
            # Call the function
750
751
752
753
754
755
756
757
758
759
760
            try:
                result = func(*args, **kwargs)

            finally:
                # if we're the top-level FileOrThing
                # decorator, remove the attributes we
                # added above.
                if parent:
                    delattr(wrapped, '_fot_workdir')
                    delattr(wrapped, '_fot_outprefix')
                    delattr(wrapped, '_fot_prefixes')
761

762
763
            return self.__generateResult(
                td, result, outprefix, outfiles, prefixes)
764
765


766
    def __prepareArgs(self, parent, workdir, argnames, args, kwargs):
767
768
769
770
771
        """Prepares all input and output arguments to be passed to the
        decorated function. Any arguments with a value of :data:`LOAD` are
        passed to the ``prepOut`` function specified at :meth:`__init__`.
        All other arguments are passed through the ``prepIn`` function.

772
773
        :arg parent:  ``True`` if this ``FileOrThing`` is the first in a
                      chain of ``FileOrThing`` decorators.
774

775
776
        :arg workdir: Directory in which all temporary files should be stored.

777
778
779
        :arg args:    Positional arguments to be passed to the decorated
                      function.

780
781
782
783
        :arg kwargs:  Keyword arguments to be passed to the decorated function.

        :returns:     A tuple containing:

784
785
786
                        - An updated copy of ``args``.

                        - An updated copy of ``kwargs``.
787

788
789
790
791
792
793
794
                        - The output file prefix that was actually passed in
                          (it is subsequently modified so that prefixed outputs
                          are redirected to a temporary location). All prefixed
                          outputs that are not ``LOAD``ed should be moved into
                          this directory. ``None`` if there is no output
                          prefix.

795
796
                        - A dictionary of ``{ name : filename }`` mappings,
                          for all arguments with a value of ``LOAD``.
797

798
                        - A dictionary of ``{ filepat : replstr }`` paths, for
799
                          all output-prefix arguments with a value of ``LOAD``.
800
801
        """

802
803
804
805
        # These containers keep track
        # of output files which are to
        # be loaded into memory
        outfiles      = dict()
806
        prefixedFiles = dict()
807

808
809
810
        allargs  = {k : v for k, v in zip(argnames, args)}
        allargs.update(kwargs)

811
812
813
814
        # Has an output prefix been specified?
        prefix     = allargs.get(self.__outprefix, None)
        realPrefix = None

815
816
        # Prefixed outputs are only
        # managed by the parent
817
        # FileOrthing in a chain of
818
819
820
821
        # FoT decorators.
        if not parent:
            prefix = None

822
823
824
825
826
827
828
829
830
831
        # If so, replace it with a new output
        # prefix which will redirect all output
        # to the temp dir.
        #
        # Importantly, here we assume that the
        # underlying function (and hence the
        # underlying command-line tool) will
        # accept an output prefix which contains
        # a directory path.
        if prefix is not None:
832
833
834
835
836
837
838
839
840
841

            # If prefix is set to LOAD,
            # all generated output files
            # should be loaded - we use a
            # randomly generated prefix,
            # and add it to prefixedFiles,
            # so that every file which
            # starts with it will be
            # loaded.
            if prefix is LOAD:
842
843
844
                prefix                = random.sample(string.ascii_letters, 10)
                prefix                = ''.join(prefix)
                prefixedFiles[prefix] = self.__outprefix
845

846
            realPrefix                = prefix
847
848
849
            fakePrefix                = op.join(workdir, prefix)
            allargs[self.__outprefix] = fakePrefix

850
851
            log.debug('Replacing output prefix: %s -> %s',
                      realPrefix, fakePrefix)
852

853
854
855
856
            # If the prefix specifies a
            # directory, make sure it
            # exists (remember that we're
            # in a temporary directory)
857
858
859
            pdir = op.dirname(fakePrefix)
            if pdir != '' and not op.exists(pdir):
                os.makedirs(pdir)
860

861
862
        if len(self.__things) > 0: things = self.__things
        else:                      things = allargs.keys()
863

864
        for name, val in list(allargs.items()):
865

866
867
868
869
870
            # don't process the
            # outprefix argument
            if name == self.__outprefix:
                continue

871
            # is this argument referring
872
            # to a prefixed output?
873
874
            isprefixed = (prefix is not None and
                          name.startswith(prefix))
875

876
            if not (isprefixed or name in things):
877
878
                continue

879
880
881
882
            # Prefixed output files may only
            # be given a value of LOAD
            if isprefixed and val is not LOAD:
                raise ValueError('Cannot specify name of prefixed file - the '
883
884
                                 'name is defined by the output prefix: '
                                 '{}'.format(name))
885

886
            if val is LOAD:
887

888
889
890
891
892
893
894
                # this argument refers to an output
                # that is generated from the output
                # prefix argument, and doesn't map
                # directly to an argument of the
                # function. So we don't pass it
                # through.
                if isprefixed:
895
                    prefixedFiles[name] = name
896
897
898
899
900
                    allargs.pop(name)

                # regular output-file argument
                else:
                    outfile = self.__prepOut(workdir, name, val)
901
                    outfiles[name] = outfile
902
                    allargs[ name] = outfile
Paul McCarthy's avatar
Paul McCarthy committed
903

904
            # Assumed to be an input file
905
            else:
906
907
                # sequences may be
                # accepted for inputs
908
909
910
911
912
913
914
915
916
                if isinstance(val, (list, tuple)):
                    infile = list(val)
                    for i, v in enumerate(val):
                        v = self.__prepIn(workdir, name, v)
                        if v is not None:
                            infile[i] = v

                else:
                    infile = self.__prepIn(workdir, name, val)
Paul McCarthy's avatar
Paul McCarthy committed
917

918
                if infile is not None:
919
920
                    allargs[name] = infile

921
922
923
        if realPrefix is not None and len(prefixedFiles) == 0:
            allargs[self.__outprefix] = realPrefix

924
925
        args   = [allargs.pop(k) for k in argnames]
        kwargs = allargs
926

927
        return args, kwargs, realPrefix, outfiles, prefixedFiles
928
929


930
931
    def __generateResult(
            self, workdir, result, outprefix, outfiles, prefixes):
932
        """Loads function outputs and returns a :class:`Results` object.
933
934
935

        Called by :meth:`__call__` after the decorated function has been
        called. Figures out what files should be loaded, and loads them into
936
        a ``Results`` object.
937
938
939
940
941
942
943
944
945
946

        :arg workdir:   Directory which contains the function outputs.
        :arg result:    Function return value.
        :arg outprefix: Original output prefix that was passed into the
                        function (or ``None`` if one wasn't passed)
        :arg outfiles:  Dictionary containing output files to be loaded (see
                        :meth:`__prepareArgs`).
        :arg prefixes:  Dictionary containing output-prefix patterns to be
                        loaded (see :meth:`__prepareArgs`).

947
        :returns:       A ``Results`` object containing all loaded outputs.
948
949
        """

950
        # make a Results object to store
951
        # the output. If we are decorating
952
        # another FileOrThing, the
953
        # results will get merged together
954
955
956
        # into a single Results dict.
        if not isinstance(result, FileOrThing.Results):
            result = FileOrThing.Results(result)
957
958
959
960
961
962

        # Load the LOADed outputs
        for oname, ofile in outfiles.items():

            log.debug('Loading output %s: %s', oname, ofile)

963
            if op.exists(ofile): oval = self.__load(oname, ofile)
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
            else:                oval = None

            result[oname] = oval

        # No output prefix - we're done
        if outprefix is None or len(prefixes) == 0:
            return result

        # Load or move output-prefixed files.
        # Find all files with a name that
        # matches the prefix that was passed
        # in (recursing into matching sub-
        # directories too).
        allPrefixed = glob.glob(op.join(workdir, '{}*'.format(outprefix)))
        allPrefixed = [fslpath.allFiles(f) if op.isdir(f) else [f]
                       for f in allPrefixed]

        for prefixed in it.chain(*allPrefixed):
            fullpath = prefixed
            prefixed = op.relpath(prefixed, workdir)
            for prefPat, prefName in prefixes.items():
                if not fnmatch.fnmatch(prefixed, '{}*'.format(prefPat)):
                    continue

                log.debug('Loading prefixed output %s [%s]: %s',
                          prefPat, prefName, prefixed)

991
992
993
994
995
                noext   = self.__removeExt(prefixed)
                prefPat = prefPat.replace('\\', '\\\\')
                noext   = re.sub('^' + prefPat, prefName, noext)
                withext = re.sub('^' + prefPat, prefName, prefixed)

996
997
998
                # if the load function returns
                # None, this file is probably
                # not of the correct type.
999
                fval = self.__load(noext, fullpath)
1000
                if fval is not None:
1001

1002
1003
1004
                    # If there is already an item in result with the
                    # name (stripped of prefix), then instead store
                    # the result with the full prefixed name
1005
1006
1007
1008
                    if noext not in result:
                        result[noext] = fval
                    else:
                        result[withext] = fval
1009
1010
1011
1012
1013
                    break

        return result


1014
def fileOrImage(*args, **kwargs):
Paul McCarthy's avatar
Paul McCarthy committed
1015
1016
    """Decorator which can be used to ensure that any NIfTI images are saved
    to file, and output images can be loaded and returned as ``nibabel``
1017
    image objects or :class:`.Image` objects.
Paul McCarthy's avatar
Paul McCarthy committed
1018
1019
    """

1020
1021
1022
1023
1024
1025
    # keep track of the input argument
    # types on each call, so we know
    # whether to return a fsl.Image or
    # a nibabel image
    intypes = []

1026
    def prepIn(workdir, name, val):
Paul McCarthy's avatar
Paul McCarthy committed
1027

1028
        infile = None
Paul McCarthy's avatar
Paul McCarthy committed
1029

1030
1031
1032
1033
1034
        if isinstance(val, fslimage.Image):
            intypes.append(fslimage.Image)

        elif isinstance(val, nib.nifti1.Nifti1Image):
            intypes.append(nib.nifti1.Nifti1Image)
1035
1036
1037
1038

        if isinstance(val, fslimage.Image):
            val = val.nibImage

Paul McCarthy's avatar
Paul McCarthy committed
1039
        if isinstance(val, nib.nifti1.Nifti1Image):
1040
            infile = val.get_filename()
Paul McCarthy's avatar
Paul McCarthy committed
1041
1042
1043

            # in-memory image - we have
            # to save it out to a file
1044
1045
            if infile is None:
                hd, infile = tempfile.mkstemp(fslimage.defaultExt())
Paul McCarthy's avatar
Paul McCarthy committed
1046
                os.close(hd)
1047
                val.to_filename(infile)
Paul McCarthy's avatar
Paul McCarthy committed
1048

1049
        return infile
Paul McCarthy's avatar
Paul McCarthy committed
1050

1051
1052
    def prepOut(workdir, name, val):
        return op.join(workdir, '{}.nii.gz'.format(name))
Paul McCarthy's avatar
Paul McCarthy committed
1053

1054
    def load(name, path):
1055
1056
1057
1058

        if not fslimage.looksLikeImage(path):
            return None

Paul McCarthy's avatar
Paul McCarthy committed
1059
1060
        # create an independent in-memory
        # copy of the image file
Paul McCarthy's avatar
Paul McCarthy committed
1061
1062
        img  = nib.load(path, mmap=False)
        data = np.asanyarray(img.dataobj)
1063
1064
1065
1066

        # if any arguments were fsl images,
        # that takes precedence.
        if fslimage.Image in intypes:
1067
1068
            return fslimage.Image(data, header=img.header, name=name)

1069
1070
1071
        # but if all inputs were file names,
        # nibabel takes precedence
        elif nib.nifti1.Nifti1Image in intypes or len(intypes) == 0:
Paul McCarthy's avatar
Paul McCarthy committed
1072
            return nib.nifti1.Nifti1Image(data, None, img.header)
1073
1074
1075
1076
1077

        # this function should not be called
        # under any other circumstances
        else:
            raise RuntimeError('Cannot handle type: {}'.format(intypes))
Paul McCarthy's avatar
Paul McCarthy committed
1078

1079
    def decorator(func):
1080
1081
1082
1083
1084
1085
1086
        fot = FileOrThing(func,
                          prepIn,
                          prepOut,
                          load,
                          fslimage.removeExt,
                          *args,
                          **kwargs)
1087
1088

        def wrapper(*args, **kwargs):
1089
1090
1091
            result = fot(*args, **kwargs)
            intypes[:] = []
            return result
1092
1093
1094
1095

        return _update_wrapper(wrapper, func)

    return decorator
Paul McCarthy's avatar
Paul McCarthy committed
1096
1097


1098
def fileOrArray(*args, **kwargs):
Paul McCarthy's avatar
Paul McCarthy committed
1099
1100
1101
1102
    """Decorator which can be used to ensure that any Numpy arrays are saved
    to text files, and output files can be loaded and returned as Numpy arrays.
    """

1103
    def prepIn(workdir, name, val):
Paul McCarthy's avatar
Paul McCarthy committed
1104

1105
        infile = None
Paul McCarthy's avatar
Paul McCarthy committed
1106
1107

        if isinstance(val, np.ndarray):
1108
            hd, infile = tempfile.mkstemp('.txt')
Paul McCarthy's avatar
Paul McCarthy committed
1109
            os.close(hd)
1110
            np.savetxt(infile, val, fmt='%0.18f')
Paul McCarthy's avatar
Paul McCarthy committed
1111

1112
        return infile
Paul McCarthy's avatar
Paul McCarthy committed
1113

1114
1115
    def prepOut(workdir, name, val):
        return op.join(workdir, '{}.txt'.format(name))
Paul McCarthy's avatar
Paul McCarthy committed
1116

1117
    def load(_, path):
1118
1119
        try:              return np.loadtxt(path)
        except Exception: return None
Paul McCarthy's avatar
Paul McCarthy committed
1120

1121
    def decorator(func):
1122
1123
1124
1125
1126
1127
1128
        fot = FileOrThing(func,
                          prepIn,
                          prepOut,
                          load,
                          fslpath.removeExt,
                          *args,
                          **kwargs)
1129
1130
1131
1132
1133
1134
1135

        def wrapper(*args, **kwargs):
            return fot(*args, **kwargs)

        return _update_wrapper(wrapper, func)

    return decorator
1136

1137

1138
def fileOrText(*args, **kwargs):
1139
1140
1141
    """Decorator which can be used to ensure that any text output (e.g. log
    file) are saved to text files, and output files can be loaded and returned
    as strings.
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164

    To be able to distinguish between input values and input file paths, the
    ``fileOrText`` decorator requires that input and output file paths are
    passed in as ``pathlib.Path`` objects. For example, given a function
    like this::

        @fileOrText()
        def myfunc(infile, outfile):
            ...

    if we want to pass file paths for both ``infile`` and ``outfile``, we would
    do this::

        from pathlib import Path
        myfunc(Path('input.txt'), Path('output.txt'))

    Input values may be passed in as normal strings, e.g.::

        myfunc('input data', Path('output.txt'))

    Output values can be loaded as normal via the :attr:`LOAD` symbol, e.g.::

        myfunc(Path('input.txt'), LOAD)
1165
1166
1167
1168
1169
1170
    """

    def prepIn(workdir, name, val):

        infile = None

1171
1172
1173
1174
        if not isinstance(val, pathlib.Path):
            with tempfile.NamedTemporaryFile(mode='w',
                                             suffix='.txt',
                                             delete=False) as f:
1175
1176
1177
1178
1179
1180
1181
                f.write(val)
                infile = f.name
        return infile

    def prepOut(workdir, name, val):
        return op.join(workdir, '{}.txt'.format(name))

1182
    def load(_, path):
1183
1184
1185
1186
1187
1188
        try:
            with open(path, "r") as f:
                return f.read()
        except Exception: return None

    def decorator(func):
1189
1190
1191
1192
1193
1194
1195
        fot = FileOrThing(func,
                          prepIn,
                          prepOut,
                          load,
                          fslpath.removeExt,
                          *args,
                          **kwargs)
1196
1197
1198
1199
1200
1201
1202

        def wrapper(*args, **kwargs):
            return fot(*args, **kwargs)

        return _update_wrapper(wrapper, func)

    return decorator