Lambda functions in nipype workflow connection

I often make use of the inline processing function feature available for nipype workflows:

testsave.connect(bet2, ('mask_file', func), maths, "in_file2")

as - for instance - exemplified here:

It would be even more convenient, if I could perform simple operator processig inline, e.g. instead of

def divideby_10(a):
    return a/10.
testsave.connect(somenode, ('out_number', divideby_10), someothernode, "in_number")


testsave.connect(somenode, ('out_number', /10.), someothernode, "in_number")

Currently this fails with SyntaxError: invalid syntax and

testsave.connect(somenode, ('out_number', +10.), someothernode, "in_number")

fails with TypeError: 10.0 is not a module, class, method, function, traceback, frame, or code object.
The obvious solution at this point, a lambda function:

testsave.connect(somenode, ('out_number', lambda x: x/10.), someothernode, "in_number")

fails at runtime with:

Error executing function:
 (f_percentile, f_threshold, [(('out_stat', lambda x: x/10.), 'thresh')]),

Functions in connection strings have to be standalone.
They cannot be declared either interactively or inside
another function or inline in the connect string. Any
imports should be done inside the function

Is there any way I can perform division inline with the connection specification, or do I have to wrap the operation in a defined function?

What about

import operator as op
testsave.connect(somenode, ('out_number', op.truediv, 10.),
                 someothernode, "in_number")

I get:

TypeError: <built-in function truediv> is not a module, class, method, function, traceback, frame, or code object

The aforementioned awkward explicit function definition works, however. It seems Python is really picky here about what it considers a function.

Ah, that’s what I get for not testing. Well, you could try numpy.divide (which may fail if traits fails to interpret numpy.float64 as a float), or just build a little hacky library of common functions like:


def div(a, b):
    return a / b


Or, if you’re feeling up to it, automatically generate functions by iterating over dir(operator), but that might fall afoul of the requirements, as well.