def qap_mask_workflow(workflow, resource_pool, config):

    import os
    import sys

    import nipype.interfaces.io as nio
    import nipype.pipeline.engine as pe

    import nipype.interfaces.utility as niu
    import nipype.interfaces.fsl.maths as fsl
    from nipype.interfaces.fsl.base import Info

    from qap_workflows_utils import select_thresh, slice_head_mask

    from workflow_utils import check_input_resources, check_config_settings

    # check_input_resources(resource_pool, 'anatomical_reorient')
    # check_input_resources(resource_pool, 'ants_affine_xfm')
    if "template_skull_for_anat" not in config:
        config["template_skull_for_anat"] = Info.standard_image("MNI152_T1_2mm.nii.gz")

    check_config_settings(config, "template_skull_for_anat")

    if "flirt_affine_xfm" not in resource_pool.keys():

        from anatomical_preproc import flirt_anatomical_linear_registration

        workflow, resource_pool = flirt_anatomical_linear_registration(workflow, resource_pool, config)

    if "anatomical_reorient" not in resource_pool.keys():

        from anatomical_preproc import anatomical_reorient_workflow

        workflow, resource_pool = anatomical_reorient_workflow(workflow, resource_pool, config)

    select_thresh = pe.Node(
        niu.Function(input_names=["input_skull"], output_names=["thresh_out"], function=select_thresh),
        name="qap_headmask_select_thresh",
        iterfield=["input_skull"],
    )

    mask_skull = pe.Node(fsl.Threshold(args="-bin"), name="qap_headmask_thresh")

    dilate_node = pe.Node(fsl.MathsCommand(args="-dilM -dilM -dilM -dilM -dilM -dilM"), name="qap_headmask_dilate")

    erode_node = pe.Node(fsl.MathsCommand(args="-eroF -eroF -eroF -eroF -eroF -eroF"), name="qap_headmask_erode")

    slice_head_mask = pe.Node(
        niu.Function(
            input_names=["infile", "transform", "standard"], output_names=["outfile_path"], function=slice_head_mask
        ),
        name="qap_headmask_slice_head_mask",
    )

    combine_masks = pe.Node(fsl.BinaryMaths(operation="add", args="-bin"), name="qap_headmask_combine_masks")

    if len(resource_pool["anatomical_reorient"]) == 2:
        node, out_file = resource_pool["anatomical_reorient"]
        workflow.connect(
            [
                (node, select_thresh, [(out_file, "input_skull")]),
                (node, mask_skull, [(out_file, "in_file")]),
                (node, slice_head_mask, [(out_file, "infile")]),
            ]
        )
    else:
        select_thresh.inputs.input_skull = resource_pool["anatomical_reorient"]
        mask_skull.inputs.in_file = resource_pool["anatomical_reorient"]
        # convert_fsl_xfm.inputs.infile =
        #    resource_pool['anatomical_reorient']
        slice_head_mask.inputs.infile = resource_pool["anatomical_reorient"]

    if len(resource_pool["flirt_affine_xfm"]) == 2:
        node, out_file = resource_pool["flirt_affine_xfm"]
        workflow.connect(node, out_file, slice_head_mask, "transform")
    else:
        slice_head_mask.inputs.transform = resource_pool["flirt_affine_xfm"]

    # convert_fsl_xfm.inputs.standard = config['template_skull_for_anat']
    slice_head_mask.inputs.standard = config["template_skull_for_anat"]

    workflow.connect(
        [
            (select_thresh, mask_skull, [("thresh_out", "thresh")]),
            # (convert_fsl_xfm, slice_head_mask, [('converted_xfm', 'transform')])
            (mask_skull, dilate_node, [("out_file", "in_file")]),
            (dilate_node, erode_node, [("out_file", "in_file")]),
            (erode_node, combine_masks, [("out_file", "in_file")]),
            (slice_head_mask, combine_masks, [("outfile_path", "operand_file")]),
        ]
    )

    resource_pool["qap_head_mask"] = (combine_masks, "out_file")
    return workflow, resource_pool
def qap_mask_workflow(workflow, resource_pool, config):

    import os
    import sys

    import nipype.interfaces.io as nio
    import nipype.pipeline.engine as pe

    import nipype.interfaces.utility as util
    import nipype.interfaces.fsl.maths as fsl

    from qap_workflows_utils import select_thresh, slice_head_mask

    from workflow_utils import check_input_resources, check_config_settings

    # check_input_resources(resource_pool, "anatomical_reorient")
    # check_input_resources(resource_pool, "ants_affine_xfm")
    check_config_settings(config, "template_skull_for_anat")

    if "flirt_affine_xfm" not in resource_pool.keys():

        from anatomical_preproc import flirt_anatomical_linear_registration

        workflow, resource_pool = flirt_anatomical_linear_registration(workflow, resource_pool, config)

    if "anatomical_reorient" not in resource_pool.keys():

        from anatomical_preproc import anatomical_reorient_workflow

        workflow, resource_pool = anatomical_reorient_workflow(workflow, resource_pool, config)

    select_thresh = pe.Node(
        util.Function(input_names=["input_skull"], output_names=["thresh_out"], function=select_thresh),
        name="qap_headmask_select_thresh",
        iterfield=["input_skull"],
    )

    mask_skull = pe.Node(interface=fsl.Threshold(), name="qap_headmask_thresh")

    mask_skull.inputs.args = "-bin"

    dilate_node = pe.Node(interface=fsl.MathsCommand(), name="qap_headmask_dilate")

    dilate_node.inputs.args = "-dilM -dilM -dilM -dilM -dilM -dilM"

    erode_node = pe.Node(interface=fsl.MathsCommand(), name="qap_headmask_erode")

    erode_node.inputs.args = "-eroF -eroF -eroF -eroF -eroF -eroF"

    slice_head_mask = pe.Node(
        util.Function(
            input_names=["infile", "transform", "standard"], output_names=["outfile_path"], function=slice_head_mask
        ),
        name="qap_headmask_slice_head_mask",
    )

    combine_masks = pe.Node(interface=fsl.BinaryMaths(), name="qap_headmask_combine_masks")

    combine_masks.inputs.operation = "add"
    combine_masks.inputs.args = "-bin"

    if len(resource_pool["anatomical_reorient"]) == 2:
        node, out_file = resource_pool["anatomical_reorient"]
        workflow.connect(node, out_file, select_thresh, "input_skull")
        workflow.connect(node, out_file, mask_skull, "in_file")
        # workflow.connect(node, out_file, convert_fsl_xfm, 'infile')
        workflow.connect(node, out_file, slice_head_mask, "infile")
    else:
        select_thresh.inputs.input_skull = resource_pool["anatomical_reorient"]
        mask_skull.inputs.in_file = resource_pool["anatomical_reorient"]
        # convert_fsl_xfm.inputs.infile = \
        #    resource_pool["anatomical_reorient"]
        slice_head_mask.inputs.infile = resource_pool["anatomical_reorient"]

    if len(resource_pool["flirt_affine_xfm"]) == 2:
        node, out_file = resource_pool["flirt_affine_xfm"]
        workflow.connect(node, out_file, slice_head_mask, "transform")
    else:
        slice_head_mask.inputs.transform = resource_pool["flirt_affine_xfm"]

    # convert_fsl_xfm.inputs.standard = config["template_skull_for_anat"]
    slice_head_mask.inputs.standard = config["template_skull_for_anat"]

    workflow.connect(select_thresh, "thresh_out", mask_skull, "thresh")

    # workflow.connect(convert_fsl_xfm, 'converted_xfm', \
    #                     slice_head_mask, 'transform')

    workflow.connect(mask_skull, "out_file", dilate_node, "in_file")

    workflow.connect(dilate_node, "out_file", erode_node, "in_file")

    workflow.connect(erode_node, "out_file", combine_masks, "in_file")

    workflow.connect(slice_head_mask, "outfile_path", combine_masks, "operand_file")

    resource_pool["qap_head_mask"] = (combine_masks, "out_file")

    return workflow, resource_pool
Example #3
0
def qap_mask_workflow(workflow, resource_pool, config):

    import os
    import sys

    import nipype.interfaces.io as nio
    import nipype.pipeline.engine as pe

    import nipype.interfaces.utility as niu
    import nipype.interfaces.fsl.maths as fsl
    from nipype.interfaces.fsl.base import Info

    from qap_workflows_utils import select_thresh, \
        slice_head_mask

    from workflow_utils import check_input_resources, \
        check_config_settings

    # check_input_resources(resource_pool, 'anatomical_reorient')
    # check_input_resources(resource_pool, 'ants_affine_xfm')
    if 'template_skull_for_anat' not in config:
        config['template_skull_for_anat'] = Info.standard_image(
            'MNI152_T1_2mm.nii.gz')

    check_config_settings(config, 'template_skull_for_anat')

    if 'flirt_affine_xfm' not in resource_pool.keys():

        from anatomical_preproc import flirt_anatomical_linear_registration

        workflow, resource_pool = \
            flirt_anatomical_linear_registration(workflow, resource_pool,
                                                 config)

    if 'anatomical_reorient' not in resource_pool.keys():

        from anatomical_preproc import anatomical_reorient_workflow

        workflow, resource_pool = \
            anatomical_reorient_workflow(workflow, resource_pool, config)

    select_thresh = pe.Node(niu.Function(input_names=['input_skull'],
                                         output_names=['thresh_out'],
                                         function=select_thresh),
                            name='qap_headmask_select_thresh',
                            iterfield=['input_skull'])

    mask_skull = pe.Node(fsl.Threshold(args='-bin'),
                         name='qap_headmask_thresh')

    dilate_node = pe.Node(
        fsl.MathsCommand(args='-dilM -dilM -dilM -dilM -dilM -dilM'),
        name='qap_headmask_dilate')

    erode_node = pe.Node(
        fsl.MathsCommand(args='-eroF -eroF -eroF -eroF -eroF -eroF'),
        name='qap_headmask_erode')

    slice_head_mask = pe.Node(niu.Function(
        input_names=['infile', 'transform', 'standard'],
        output_names=['outfile_path'],
        function=slice_head_mask),
                              name='qap_headmask_slice_head_mask')

    combine_masks = pe.Node(fsl.BinaryMaths(operation='add', args='-bin'),
                            name='qap_headmask_combine_masks')

    if len(resource_pool['anatomical_reorient']) == 2:
        node, out_file = resource_pool['anatomical_reorient']
        workflow.connect([(node, select_thresh, [(out_file, 'input_skull')]),
                          (node, mask_skull, [(out_file, 'in_file')]),
                          (node, slice_head_mask, [(out_file, 'infile')])])
    else:
        select_thresh.inputs.input_skull = resource_pool['anatomical_reorient']
        mask_skull.inputs.in_file = resource_pool['anatomical_reorient']
        # convert_fsl_xfm.inputs.infile =
        #    resource_pool['anatomical_reorient']
        slice_head_mask.inputs.infile = resource_pool['anatomical_reorient']

    if len(resource_pool['flirt_affine_xfm']) == 2:
        node, out_file = resource_pool['flirt_affine_xfm']
        workflow.connect(node, out_file, slice_head_mask, 'transform')
    else:
        slice_head_mask.inputs.transform = resource_pool['flirt_affine_xfm']

    # convert_fsl_xfm.inputs.standard = config['template_skull_for_anat']
    slice_head_mask.inputs.standard = config['template_skull_for_anat']

    workflow.connect([
        (select_thresh, mask_skull, [('thresh_out', 'thresh')]),
        # (convert_fsl_xfm, slice_head_mask, [('converted_xfm', 'transform')])
        (mask_skull, dilate_node, [('out_file', 'in_file')]),
        (dilate_node, erode_node, [('out_file', 'in_file')]),
        (erode_node, combine_masks, [('out_file', 'in_file')]),
        (slice_head_mask, combine_masks, [('outfile_path', 'operand_file')])
    ])

    resource_pool['qap_head_mask'] = (combine_masks, 'out_file')
    return workflow, resource_pool