Example #1
0
def test_CreateTiledMosaic_outputs():
    output_map = dict(output_image=dict(), )
    outputs = CreateTiledMosaic.output_spec()

    for key, metadata in output_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(outputs.traits()[key], metakey), value
def test_CreateTiledMosaic_outputs():
    output_map = dict(output_image=dict(),
    )
    outputs = CreateTiledMosaic.output_spec()

    for key, metadata in output_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(outputs.traits()[key], metakey), value
def test_CreateTiledMosaic_inputs():
    input_map = dict(alpha_value=dict(argstr='-a %.2f',
    ),
    args=dict(argstr='%s',
    ),
    direction=dict(argstr='-d %d',
    ),
    environ=dict(nohash=True,
    usedefault=True,
    ),
    flip_slice=dict(argstr='-f %s',
    ),
    ignore_exception=dict(nohash=True,
    usedefault=True,
    ),
    input_image=dict(argstr='-i %s',
    mandatory=True,
    ),
    mask_image=dict(argstr='-x %s',
    ),
    num_threads=dict(nohash=True,
    usedefault=True,
    ),
    output_image=dict(argstr='-o %s',
    usedefault=True,
    ),
    pad_or_crop=dict(argstr='-p %s',
    ),
    permute_axes=dict(argstr='-g',
    ),
    rgb_image=dict(argstr='-r %s',
    mandatory=True,
    ),
    slices=dict(argstr='-s %s',
    ),
    terminal_output=dict(mandatory=True,
    nohash=True,
    ),
    tile_geometry=dict(argstr='-t %s',
    ),
    )
    inputs = CreateTiledMosaic.input_spec()

    for key, metadata in input_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(inputs.traits()[key], metakey), value
Example #4
0
def test_CreateTiledMosaic_inputs():
    input_map = dict(
        alpha_value=dict(argstr='-a %.2f', ),
        args=dict(argstr='%s', ),
        direction=dict(argstr='-d %d', ),
        environ=dict(
            nohash=True,
            usedefault=True,
        ),
        flip_slice=dict(argstr='-f %s', ),
        ignore_exception=dict(
            nohash=True,
            usedefault=True,
        ),
        input_image=dict(
            argstr='-i %s',
            mandatory=True,
        ),
        mask_image=dict(argstr='-x %s', ),
        num_threads=dict(
            nohash=True,
            usedefault=True,
        ),
        output_image=dict(
            argstr='-o %s',
            usedefault=True,
        ),
        pad_or_crop=dict(argstr='-p %s', ),
        permute_axes=dict(argstr='-g', ),
        rgb_image=dict(
            argstr='-r %s',
            mandatory=True,
        ),
        slices=dict(argstr='-s %s', ),
        terminal_output=dict(nohash=True, ),
        tile_geometry=dict(argstr='-t %s', ),
    )
    inputs = CreateTiledMosaic.input_spec()

    for key, metadata in input_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(inputs.traits()[key], metakey), value
Example #5
0
def main(args):
    parser = parsefn()
    subj_dir, img, seg, gap, tile, alpha, ax, roi, flip, min_sl, out = parse_inputs(
        parser, args)

    # pred preprocess dir
    pred_dir = '%s/pred_process' % os.path.abspath(subj_dir)
    if not os.path.exists(pred_dir):
        os.mkdir(pred_dir)

    # trim seg to focus
    c3 = C3d()

    mosaic_slicer = CreateTiledMosaic()

    if seg:
        c3.inputs.in_file = seg
        c3.inputs.args = "-trim %sx%sx%svox" % (roi, roi, roi)
        seg_trim_file = "%s/%s_trim_mosaic.nii.gz" % (
            pred_dir, os.path.basename(seg).split('.')[0])
        # seg_trim_file = "seg_trim.nii.gz"
        c3.inputs.out_file = seg_trim_file
        c3.run()

        # trim struct like seg
        c3.inputs.in_file = seg_trim_file
        c3.inputs.args = "%s -reslice-identity" % img
        struct_trim_file = "%s/%s_trim_mosaic.nii.gz" % (
            pred_dir, os.path.basename(img).split('.')[0])
        # struct_trim_file = "struct_trim.nii.gz"
        c3.inputs.out_file = struct_trim_file
        c3.run()

        # create rgb image from seg
        converter = ConvertScalarImageToRGB()

        converter.inputs.dimension = 3
        converter.inputs.input_image = seg_trim_file
        converter.inputs.colormap = 'jet'
        converter.inputs.minimum_input = 0
        converter.inputs.maximum_input = 10
        out_rgb = "%s/%s_trim_rgb.nii.gz" % (
            pred_dir, os.path.basename(seg).split('.')[0])
        converter.inputs.output_image = out_rgb
        converter.run()

        mosaic_slicer.inputs.rgb_image = out_rgb
        mosaic_slicer.inputs.mask_image = seg_trim_file
        mosaic_slicer.inputs.alpha_value = alpha

    else:
        struct_trim_file = img

        mosaic_slicer.inputs.rgb_image = struct_trim_file

    # stretch and clip intensities
    c3.inputs.in_file = struct_trim_file
    c3.inputs.args = "-stretch 2% 98% 0 255 -clip 0 255"
    c3.inputs.out_file = struct_trim_file
    c3.run()

    # slices to show
    if gap == 1:
        max_sl = 100
    elif gap == 2:
        max_sl = 220
    elif gap == 5:
        max_sl = 275
    else:
        max_sl = 300

    slices = '[%s,%s,%s]' % (gap, min_sl, max_sl)

    mosaic_slicer.inputs.input_image = struct_trim_file

    mosaic_slicer.inputs.output_image = out
    mosaic_slicer.inputs.direction = ax
    # mosaic_slicer.inputs.pad_or_crop = '[ -15x -50 , -15x -30 ,0]'
    mosaic_slicer.inputs.tile_geometry = tile
    mosaic_slicer.inputs.slices = slices
    mosaic_slicer.inputs.flip_slice = flip
    mosaic_slicer.terminal_output = "none"
    mosaic_slicer.run()
def ANTs_cortical_thickness(subject_list, directory):

    #==============================================================
    # Loading required packages
    import nipype.interfaces.io as nio
    import nipype.pipeline.engine as pe
    import nipype.interfaces.utility as util
    import own_nipype
    from nipype.interfaces.ants.segmentation import antsCorticalThickness
    from nipype.interfaces.ants import ApplyTransforms
    from nipype.interfaces.ants import MultiplyImages
    from nipype.interfaces.utility import Function
    from nipype.interfaces.ants.visualization import ConvertScalarImageToRGB
    from nipype.interfaces.ants.visualization import CreateTiledMosaic
    from nipype.interfaces.utility import Select
    from own_nipype import GM_DENSITY
    from nipype import SelectFiles
    import os

    #====================================
    # Defining the nodes for the workflow

    # Getting the subject ID
    infosource = pe.Node(
        interface=util.IdentityInterface(fields=['subject_id']),
        name='infosource')
    infosource.iterables = ('subject_id', subject_list)

    # Getting the relevant diffusion-weighted data
    templates = dict(
        T1=
        '/imaging/jb07/CALM/CALM_BIDS/{subject_id}/anat/{subject_id}_T1w.nii.gz'
    )

    selectfiles = pe.Node(SelectFiles(templates), name="selectfiles")
    selectfiles.inputs.base_directory = os.path.abspath(directory)

    # Rigid alignment with the template space
    T1_rigid_quickSyN = pe.Node(interface=own_nipype.ants_QuickSyN(
        image_dimensions=3, transform_type='r'),
                                name='T1_rigid_quickSyN')
    T1_rigid_quickSyN.inputs.fixed_image = '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/T_template0.nii.gz'

    # Cortical thickness calculation
    corticalthickness = pe.Node(interface=antsCorticalThickness(),
                                name='corticalthickness')
    corticalthickness.inputs.brain_probability_mask = '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/T_template0_BrainCerebellumProbabilityMask.nii.gz'
    corticalthickness.inputs.brain_template = '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/T_template0.nii.gz'
    corticalthickness.inputs.segmentation_priors = [
        '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/Priors2/priors1.nii.gz',
        '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/Priors2/priors2.nii.gz',
        '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/Priors2/priors3.nii.gz',
        '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/Priors2/priors4.nii.gz',
        '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/Priors2/priors5.nii.gz',
        '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/Priors2/priors6.nii.gz'
    ]
    corticalthickness.inputs.extraction_registration_mask = '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/T_template0_BrainCerebellumExtractionMask.nii.gz'
    corticalthickness.inputs.t1_registration_template = '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/T_template0_BrainCerebellum.nii.gz'

    # Creating visualisations for quality control
    converter = pe.Node(interface=ConvertScalarImageToRGB(), name='converter')
    converter.inputs.dimension = 3
    converter.inputs.colormap = 'cool'
    converter.inputs.minimum_input = 0
    converter.inputs.maximum_input = 5

    mosaic_slicer = pe.Node(interface=CreateTiledMosaic(),
                            name='mosaic_slicer')
    mosaic_slicer.inputs.pad_or_crop = 'mask'
    mosaic_slicer.inputs.slices = '[4 ,mask , mask]'
    mosaic_slicer.inputs.direction = 1
    mosaic_slicer.inputs.alpha_value = 0.5

    # Getting GM density images
    gm_density = pe.Node(interface=GM_DENSITY(), name='gm_density')
    sl = pe.Node(interface=Select(index=1), name='sl')

    # Applying transformation
    at = pe.Node(interface=ApplyTransforms(), name='at')
    at.inputs.dimension = 3
    at.inputs.reference_image = '/imaging/jb07/Atlases/OASIS/OASIS-30_Atropos_template/T_template0_BrainCerebellum.nii.gz'
    at.inputs.interpolation = 'Linear'
    at.inputs.default_value = 0
    at.inputs.invert_transform_flags = False

    # Multiplying the normalized image with Jacobian
    multiply_images = pe.Node(interface=MultiplyImages(dimension=3),
                              name='multiply_images')

    # Naming the output of multiply_image
    def generate_filename(subject_id):
        return subject_id + '_multiplied.nii.gz'

    generate_filename = pe.Node(interface=Function(
        input_names=["subject_id"],
        output_names=["out_filename"],
        function=generate_filename),
                                name='generate_filename')

    #====================================
    # Setting up the workflow
    antsthickness = pe.Workflow(name='antsthickness')

    antsthickness.connect(infosource, 'subject_id', selectfiles, 'subject_id')
    antsthickness.connect(selectfiles, 'T1', T1_rigid_quickSyN, 'moving_image')
    antsthickness.connect(infosource, 'subject_id', T1_rigid_quickSyN,
                          'output_prefix')
    antsthickness.connect(T1_rigid_quickSyN, 'warped_image', corticalthickness,
                          'anatomical_image')
    antsthickness.connect(infosource, 'subject_id', corticalthickness,
                          'out_prefix')
    antsthickness.connect(corticalthickness, 'CorticalThickness', converter,
                          'input_image')
    antsthickness.connect(converter, 'output_image', mosaic_slicer,
                          'rgb_image')
    antsthickness.connect(corticalthickness, 'BrainSegmentationN4',
                          mosaic_slicer, 'input_image')
    antsthickness.connect(corticalthickness, 'BrainExtractionMask',
                          mosaic_slicer, 'mask_image')

    antsthickness.connect(corticalthickness, 'BrainSegmentationN4', gm_density,
                          'in_file')
    antsthickness.connect(corticalthickness, 'BrainSegmentationPosteriors', sl,
                          'inlist')
    antsthickness.connect(sl, 'out', gm_density, 'mask_file')
    antsthickness.connect(corticalthickness, 'SubjectToTemplate1Warp', at,
                          'transforms')
    antsthickness.connect(gm_density, 'out_file', at, 'input_image')
    antsthickness.connect(corticalthickness, 'SubjectToTemplateLogJacobian',
                          multiply_images, 'second_input')
    antsthickness.connect(corticalthickness,
                          'CorticalThicknessNormedToTemplate', multiply_images,
                          'first_input')
    antsthickness.connect(infosource, 'subject_id', generate_filename,
                          'subject_id')
    antsthickness.connect(generate_filename, 'out_filename', multiply_images,
                          'output_product_image')

    #====================================
    # Running the workflow
    antsthickness.base_dir = os.path.abspath(directory)
    antsthickness.write_graph()
    antsthickness.run('PBSGraph')