Create array of brain mask files via function MapNode

Hey everyone,

I want to create an array consisting of differently created brain masks to check their
effect on the subsequent first level analysis. Basically, I thought to do that via a small function
and function MapNode. Something in the line of:

def create_mask_array(brain_mask_files):
    
    brain_masks=[]

    for mask in brain_mask_files:
        brain_masks.append(mask)
        
    return brain_masks

create_mask_array = MapNode(Function(input_names=['brain_mask_files'],
                                     iterfield=['brain_mask_files'],
                                     output_names=['brain_masks'],
                                     function=create_mask_array),
                    name='create_mask_array')

maskflow.connect([(meanfuncmask, create_mask_array, [('mask_file'), ('brain_mask_files')]),
                  (binarize, create_mask_array, [('binary_file'), ('brain_mask_files')]),
                  (binarize_aparc, create_mask_array, [('binary_file'), ('brain_mask_files')]),
                 ])

With that, I get the error:

name='create_mask_array')

TypeError: __init__() takes at least 4 arguments (3 given)

I’m kinda sure, I’m missing something obvious, maybe something MapNode related, as I use some Function Nodes (which are working properly) within my pipeline which are defined like the one above, except for being a MapNode!? I’ve no idea why it should be at least 4 arguments, as I don’t define that number of necessary inputs.

Does anyone have an idea and/or pointers?

Have a good one, best, Peer

Hey,

as I though, I missed something obvious: the iterfield argument should’ve been the last one when defining the node:

def create_mask_array(brain_mask_files):
    
    brain_masks=[]

    for mask in brain_mask_files:
        brain_masks.append(mask)
        
    return brain_masks

create_mask_array = MapNode(Function(input_names=['brain_mask_files'],  
                                     output_names=['brain_masks'],
                                     function=create_mask_array),
                                     name='create_mask_array',
                                     **iterfield=['brain_mask_files']**)

maskflow.connect([(meanfuncmask, create_mask_array, [('mask_file'), ('brain_mask_files')]),
                  (binarize, create_mask_array, [('binary_file'), ('brain_mask_files')]),
                  (binarize_aparc, create_mask_array, [('binary_file'), ('brain_mask_files')]),
                 ])

However, now I get a new error:

line 234, in <module>
    (binarize_aparc, create_mask_array, [('binary_file'), ('brain_mask_files')]),

  File "*envs/py36/lib/python3.6/site-packages/nipype/pipeline/engine/workflows.py", line 193, in connect
    for source, dest in connects:

ValueError: too many values to unpack (expected 2)

Now it seems that the function node is getting to many inputs, but I don’t know why, as the there’s only one output called “binary_file” from the connecting node!?

Best, Peer

Hey again,

never mind, I found the error:
there was a typo connecting the nodes, e.g.

(binarize, create_mask_array, [('binary_file'), ('brain_mask_files')]),

should’ve been:

(binarize, create_mask_array, [('binary_file', 'brain_mask_files')]),

Furthermore, the script won’t work like that, because I assigned the output from multiple nodes to the input of one.
Hence, I changed the number of inputs within my function and it seems to work now.

Sorry for multiple posts!