Beispiel #1
0
def dicom_convert_ppl(name='t1_dicom_cvt', crop_t1=True):
    inputnode = pe.Node(utility.IdentityInterface(fields=[
        'dicom_files', 'out_file_format', 'voxel_order', 'meta_force_add'
    ]),
                        run_without_submitting=True,
                        name='inputspec')

    outputnode = pe.Node(utility.IdentityInterface(fields=['t1_nifti']),
                         run_without_submitting=True,
                         name='outputspec')

    n_convert_t1_dicom = pe.Node(np_dcmstack.DCMStackAnatomical(),
                                 name='convert_t1_dicom')

    n_autobox_t1 = pe.Node(afni.Autobox(), name='autobox_t1')

    n_crop_t1 = pe.Node(nipy.Crop(out_file='%s_crop.nii', outputtype='NIFTI'),
                        name='crop_t1')

    w = pe.Workflow(name=name)

    def zmax2keep(z):
        return max(0, z - 174)

    wmax = wrap(max)

    w.connect([
        (inputnode,
         n_convert_t1_dicom, [('dicom_files', 'dicom_files'),
                              ('out_file_format', 'out_file_format'),
                              ('voxel_order', 'voxel_order'),
                              ('meta_force_add', 'meta_force_add')]),
    ])
    if crop_t1:
        w.connect([
            (n_convert_t1_dicom, n_autobox_t1, [('nifti_file', 'in_file')]),
            (n_convert_t1_dicom, n_crop_t1, [('nifti_file', 'in_file')]),
            (n_autobox_t1, n_crop_t1, [(('x_min', wmax, [0]), 'x_min'),
                                       ('x_max', 'x_max'),
                                       (('y_min', wmax, [0]), 'y_min'),
                                       ('y_max', 'y_max'), ('z_max', 'z_max'),
                                       (('z_max', zmax2keep), 'z_min')]),
            (n_autobox_t1, outputnode, [('out_file', 't1_nifti')]),
        ])
    else:
        w.connect(n_convert_t1_dicom, 'nifti_file', outputnode, 't1_nifti'),

    return w
Beispiel #2
0
def run(options):
    # fix!
    out_dir = os.path.join('option', '1')
    err_dir = os.path.join('option', '2')
    data_dir = os.path.join('option', '3')
    work_dir = os.path.join('something', 'else')

    # Workflow
    merica_wf = pe.Workflow('merica_wf')
    merica_wf.base_dir = work_dir

    inputspec = pe.Node(util.IdentityInterface(fields=options.keys()),
                        name='inputspec')

    # Node: subject_iterable
    run_iterable = pe.Node(util.IdentityInterface(fields=['run'],
                                                  mandatory_inputs=True),
                           name='run_iterable')
    run_iterable.iterables = ('run', runs)

    info = dict(mri_files=[['run']])

    # Create a datasource node to get the mri files
    datasource = pe.Node(nio.DataGrabber(infields=['run'],
                                         outfields=info.keys()),
                         name='datasource')
    datasource.inputs.template = '*'
    datasource.inputs.base_directory = abspath(data_dir)
    datasource.inputs.field_template = dict(mri_files='%s/func/*.nii.gz')
    datasource.inputs.template_args = info
    datasource.inputs.sort_filelist = True
    datasource.inputs.ignore_exception = False
    datasource.inputs.raise_on_empty = True
    meica_wf.connect(run_iterable, 'run', datasource, 'run')

    # Create a Function node to rename output files
    getsubs = pe.Node(util.Function(input_names=['run', 'mri_files'],
                                    output_names=['subs'],
                                    function=get_subs),
                      name='getsubs')
    getsubs.inputs.ignore_exception = False
    meica_wf.connect(run_iterable, 'run', getsubs, 'run')
    meica_wf.connect(datasource, 'mri_files', getsubs, 'mri_files')



    get_cm = pe.Node(util.Function(input_names=['fname'],
                                   output_names=['x', 'y', 'z'],
                                   function=find_CM),
                     name='get_cm')
    get_obliquity = pe.Node(util.Function(input_names=['fname'],
                                          output_names=['angmerit'],
                                          function=check_obliquity),
                            name='get_cm')
    if get_obliquity.is_oblique == True:
        deoblique = pe.Node(afni.Warp(deoblique=True)
                            name='deoblique')
        merica_wf.connect(upstream, 't1', deoblique, 'in_file')
        warpspeed = pe.Node(afni.Warp(args='-card2oblique -newgrid 1.0'))
    if skull-stripped == False:
        unifeyes = pe.Node(afni.Unifize()
                            name='unifeyes')
        if get_obliquity.is_oblique == True:
            merica_wf.connect(deoblique, 'out_file', unifeyes, 'in_file')
        else:
            merica_wf.connect(upstream, 't1', unifeyes, 'in_file')
        skullstrip = pe.Node(afni.SkullStrip(args='-shrink_fac_bot_lim 0.3 -orig_vol')
                                name='skullstrip')
        autobots = pe.Node(afni.Autobox()
                            name='autobots')
        merica_wf.connect(skullstrip, 'out_file', autobots, 'in_file')

    # Moving on to functional preprocessing, be back later!
    if despike == True:
        despike = pe.Node(afni.Despike()
                            name='despike')
        if skull-stripped == False:
            merica_wf.connect(autobots, 'out_file', despike, 'in_file')
        else:
            merica_wf.connect(upstream, 't1', despike, 'in_file')


    meica_wf.connect(run_iterable, 'run', get_cm, 'fname')
    meica_wf.connect(run_iterable, 'run', get_cm, 'fname')
Beispiel #3
0
def t1_pipeline(name='t1_preproc'):
    inputnode = pe.Node(utility.IdentityInterface(fields=['t1_dicom_dir']),
                        name='inputspec')
    outputnode = pe.Node(utility.IdentityInterface(
        fields=['mask', 'corrected_t1', 'corrected_t1_brain']),
                         name='outputspec')

    n_t1_dicom_files = pe.Node(nio.DataGrabber(sort_filelist=True, ),
                               name='t1_dicom_files')

    n_to3d_t1 = pe.Node(afni.To3D(filetype='anat',
                                  environ=dict(AFNI_DICOM_RESCALE='YES')),
                        name='to3d_t1')

    n_reorient_t1 = pe.Node(afni.Resample(orientation='RPI'),
                            name='reorient_t1')

    n_autobox_t1 = pe.Node(afni.Autobox(padding=5), name='autobox_t1')

    n_zcut_t1 = pe.Node(afni.ZCutUp(outputtype='NIFTI'), name='zcut_t1')

    n_newsegment_t1 = pe.Node(spm.NewSegment(
        write_deformation_fields=[True, True],
        channel_info=(0.0001, 60, (True, True))),
                              name='newsegment_t1')

    n_seg2mask = pe.Node(fsl.MultiImageMaths(
        output_type='NIFTI',
        op_string=' -add %s -add %s -thr 0.8 -bin -eroF -dilF -dilF'),
                         name='seg2mask')

    n_mask_brain = pe.Node(interface=fsl.ImageMaths(op_string='-mul',
                                                    suffix='_brain',
                                                    output_type='NIFTI'),
                           name='mask_brain')

    w = pe.Workflow(name=name)

    def zmax2keep(z):
        return '%d %d' % (max(0, z - 174), z)

    w.connect([
        (inputnode, n_t1_dicom_files, [('t1_dicom_dir', 'base_directory')]),
        (n_t1_dicom_files, n_to3d_t1,
         [(('outfiles', sort_t1_files), 'in_files'),
          (('outfiles', t1_filename, 'nii.gz'), 'out_file')]),
        (n_to3d_t1, n_reorient_t1, [('out_file', 'in_file')]),
        (n_reorient_t1, n_autobox_t1, [('out_file', 'in_file')]),
        (n_reorient_t1, n_zcut_t1, [('out_file', 'in_file')]),
        (n_autobox_t1, n_zcut_t1, [(('z_max', zmax2keep), 'keep')]),
        (n_zcut_t1, n_newsegment_t1, [('out_file', 'channel_files')]),
        (n_newsegment_t1, n_seg2mask, [(('native_class_images', getitem_rec, 0,
                                         0), 'in_file'),
                                       (('native_class_images', getitem_rec,
                                         slice(1, 3), 0), 'operand_files')]),
        (n_zcut_t1, n_seg2mask, [(('out_file', fname_presuffix_basename, '',
                                   '_mask', '.'), 'out_file')]),
        (n_newsegment_t1, n_mask_brain, [('bias_corrected_images', 'in_file')
                                         ]),
        (n_seg2mask, n_mask_brain, [('out_file', 'in_file2')]),
        (n_seg2mask, outputnode, [('out_file', 'mask')]),
        (n_newsegment_t1, outputnode, [('bias_corrected_images',
                                        'corrected_t1')])
    ])
    return w
Beispiel #4
0
def t1_pipeline(name='t1_preproc'):
    inputnode = pe.Node(utility.IdentityInterface(fields=['t1_file']),
                        run_without_submitting=True,
                        name='inputspec')
    outputnode = pe.Node(utility.IdentityInterface(
        fields=['mask', 'corrected_t1', 'corrected_t1_brain']),
                         run_without_submitting=True,
                         name='outputspec')

    spm_path = spm.Info.version()['path']

    tpm = os.path.join(spm_path, 'toolbox', 'Seg', 'TPM.nii')
    n_newsegment_t1 = pe.Node(spm.NewSegment(
        write_deformation_fields=[False, True],
        channel_info=(0.0001, 60, (True, True)),
        tissues=[
            ((tpm, 1), 2, (True, True), (True, True)),
            ((tpm, 2), 2, (True, True), (True, True)),
            ((tpm, 3), 2, (True, False), (True, True)),
            ((tpm, 4), 3, (True, False), (False, False)),
            ((tpm, 5), 4, (True, False), (False, False)),
            ((tpm, 6), 2, (False, False), (False, False)),
        ]),
                              name='newsegment_t1')

    n_seg2mask = pe.Node(fsl.MultiImageMaths(
        output_type='NIFTI',
        output_datatype='char',
        op_string=' -add %s -add %s -thr 0.8 -bin -eroF -dilF -dilF'),
                         name='seg2mask')

    n_autobox_mask = pe.Node(afni.Autobox(padding=3, out_name='%s_crop'),
                             name='autobox_mask')

    n_merge_crop = pe.Node(utility.Merge(3), name='merge_crop')

    n_crop_all = pe.MapNode(nipy.Crop(out_file='%s_crop.nii.gz',
                                      outputtype='NIFTI_GZ'),
                            iterfield=['in_file'],
                            name='crop_all')

    w = pe.Workflow(name=name)

    w.connect([
        (inputnode, n_newsegment_t1, [('t1_file', 'channel_files')]),
        (n_newsegment_t1, n_seg2mask, [(('native_class_images', getitem_rec, 0,
                                         0), 'in_file'),
                                       (('native_class_images', getitem_rec,
                                         slice(1, 3), 0), 'operand_files')]),
        (inputnode, n_seg2mask, [(('t1_file', fname_presuffix_basename, '',
                                   '_mask', '.'), 'out_file')]),
        (n_seg2mask, n_autobox_mask, [('out_file', 'in_file')]),
        (n_newsegment_t1, n_merge_crop,
         [('bias_corrected_images', 'in1'),
          (('native_class_images', utility.select, [0, 1, 2]), 'in2')]),
        (n_seg2mask, n_merge_crop, [('out_file', 'in3')]),
        (n_merge_crop, n_crop_all, [(('out', utility.flatten), 'in_file')]),
        (n_autobox_mask, n_crop_all, [
            ('x_min', 'x_min'),
            ('x_max', 'x_max'),
            ('y_min', 'y_min'),
            ('y_max', 'y_max'),
            ('z_min', 'z_min'),
            ('z_max', 'z_max'),
        ]), (n_seg2mask, outputnode, [('out_file', 'mask')]),
        (n_newsegment_t1, outputnode, [('bias_corrected_images',
                                        'corrected_t1')])
    ])
    return w
Beispiel #5
0
def t1_freesurfer_pipeline(name='t1_preproc', use_T2=False):
    inputnode = pe.Node(utility.IdentityInterface(
        fields=['t1_files', 't2_file', 'subject_id']),
                        run_without_submitting=True,
                        name='inputspec')

    n_freesurfer = pe.Node(
        interface=freesurfer.ReconAll(directive='all', use_T2=use_T2),
        name='freesurfer',
    )

    n_fs_seg2mask = pe.Node(utility.Function(
        input_names=['parc_file', 'out_file'],
        output_names=['out_file'],
        function=fs_seg2mask),
                            name='fs_seg2mask')
    n_fs_seg2mask.inputs.out_file = 'mask.nii'

    n_autobox_mask_fs = pe.Node(afni.Autobox(padding=15,
                                             out_file='%s_crop.nii'),
                                name='autobox_mask_fs')

    n_compute_pvmaps = pe.Node(freesurfer.utils.ComputeVolumeFractions(
        gm_file='pve.nii.gz',
        niigz=True,
    ),
                               name='compute_pvmaps')

    n_crop_t1 = pe.Node(nipy.Crop(out_file='%s_crop.nii.gz',
                                  outputtype='NIFTI_GZ'),
                        name='crop_t1')

    n_crop_brain = pe.Node(nipy.Crop(out_file='%s_crop.nii.gz',
                                     outputtype='NIFTI_GZ'),
                           name='crop_brain')

    n_t1_nii = pe.Node(freesurfer.MRIConvert(out_type='niigz'), name='t1_nii')

    n_dilate_mask = pe.Node(fsl.DilateImage(operation='mean',
                                            kernel_shape='3D'),
                            name='dilate_mask')

    n_reg_crop = pe.Node(freesurfer.Tkregister(reg_file='reg_crop.dat',
                                               freeview='freeview.mat',
                                               fsl_reg_out='fsl_reg_out.mat',
                                               lta_out='lta.mat',
                                               xfm_out='xfm.mat',
                                               reg_header=True),
                         name='reg_crop')

    w = pe.Workflow(name=name)

    if (use_T2):
        w.connect([(inputnode, n_freesurfer, [
            ('t2_file', 'T2_file'),
        ])])

    w.connect([
        (inputnode, n_freesurfer, [('t1_files', 'T1_files'),
                                   ('subject_id', 'subject_id')]),
        (n_freesurfer, n_fs_seg2mask, [(('aparc_aseg', utility.select, 1),
                                        'parc_file')]),
        (n_fs_seg2mask, n_autobox_mask_fs, [('out_file', 'in_file')]),
        (n_autobox_mask_fs, n_dilate_mask, [('out_file', 'in_file')]),
        (n_autobox_mask_fs, n_crop_t1, [('%s_min' % c, ) * 2 for c in 'xyz'] +
         [(('%s_max' % c, wrap(operator.__add__), [1]), '%s_max' % c)
          for c in 'xyz']),  # add 1 for boundary inclusive
        (n_freesurfer, n_crop_t1, [('T1', 'in_file')]),
        (n_autobox_mask_fs, n_crop_brain, [('%s_min' % c, ) * 2
                                           for c in 'xyz'] +
         [(('%s_max' % c, wrap(operator.__add__), [1]), '%s_max' % c)
          for c in 'xyz']),  # add 1 for boundary inclusive
        (n_freesurfer, n_crop_brain, [('norm', 'in_file')]),
        (n_freesurfer, n_t1_nii, [('T1', 'in_file')]),
        (n_freesurfer, n_reg_crop, [('norm', 'target'), ('subject_id', ) * 2,
                                    ('subjects_dir', ) * 2]),
        (n_autobox_mask_fs, n_reg_crop, [('out_file', 'mov')]),
        (n_reg_crop, n_compute_pvmaps, [('reg_file', ) * 2]),
        (n_autobox_mask_fs, n_compute_pvmaps, [('out_file', 'in_file')]),
        (n_freesurfer, n_compute_pvmaps, [('subjects_dir', ) * 2])
    ])
    return w
Beispiel #6
0
def t1_vbm_pipeline(name='t1_preproc'):
    inputnode = pe.Node(utility.IdentityInterface(fields=['t1_file']),
                        name='inputspec')
    outputnode = pe.Node(utility.IdentityInterface(
        fields=['mask', 'corrected_t1', 'corrected_t1_brain']),
                         name='outputspec')

    spm_path = spm.Info.version()['path']

    tpm = os.path.join(spm_path, 'toolbox', 'Seg', 'TPM.nii')
    dartel_tpl = os.path.join(spm_path, 'toolbox', 'vbm8',
                              'Template_1_IXI550_MNI152.nii')
    n_vbm_segment = pe.Node(spm.VBMSegment(tissues=tpm,
                                           dartel_template=dartel_tpl,
                                           bias_corrected_native=True,
                                           gm_native=True,
                                           gm_dartel=1,
                                           wm_native=True,
                                           wm_dartel=1,
                                           csf_native=True,
                                           pve_label_native=True,
                                           deformation_field=(True, False)),
                            name='vbm_segment')

    n_seg2mask = pe.Node(fsl.MultiImageMaths(
        output_type='NIFTI',
        output_datatype='char',
        op_string=' -add %s -add %s -thr 0.8 -bin -eroF -dilF -dilF'),
                         name='seg2mask')

    n_seg2mask = pe.Node(utility.Function(
        input_names=['tissues_files', 'out_file'],
        output_names=['out_file'],
        function=seg2mask),
                         name='seg2mask')

    n_autobox_mask = pe.Node(afni.Autobox(padding=3, out_file='%s_crop'),
                             name='autobox_mask')

    n_merge_crop = pe.Node(utility.Merge(3), name='merge_crop')

    n_crop_all = pe.MapNode(nipy.Crop(out_file='%s_crop.nii.gz',
                                      outputtype='NIFTI_GZ'),
                            iterfield=['in_file'],
                            name='crop_all')

    w = pe.Workflow(name=name)

    w.connect([
        (inputnode, n_vbm_segment, [('t1_file', 'in_files')]),
        (n_vbm_segment, n_seg2mask, [(('native_class_images', utility.flatten),
                                      'tissues_files')]),
        #        (n_vbm_segment, n_seg2mask, [
        #            (('native_class_images',getitem_rec,0,0), 'in_file'),
        #            (('native_class_images',getitem_rec,slice(1,3),0),'operand_files')]),
        (inputnode, n_seg2mask, [(('t1_file', fname_presuffix_basename, '',
                                   '_mask', '.'), 'out_file')]),
        (n_seg2mask, n_autobox_mask, [('out_file', 'in_file')]),
        (n_vbm_segment, n_merge_crop, [('bias_corrected_images', 'in1'),
                                       (('native_class_images', utility.select,
                                         [0, 1, 2]), 'in2')]),
        (n_seg2mask, n_merge_crop, [('out_file', 'in3')]),
        (n_merge_crop, n_crop_all, [(('out', utility.flatten), 'in_file')]),
        (n_autobox_mask, n_crop_all, [
            ('x_min', 'x_min'),
            ('x_max', 'x_max'),
            ('y_min', 'y_min'),
            ('y_max', 'y_max'),
            ('z_min', 'z_min'),
            ('z_max', 'z_max'),
        ]),
        (n_seg2mask, outputnode, [('out_file', 'mask')]),
        (n_vbm_segment, outputnode, [('bias_corrected_images', 'corrected_t1')
                                     ])
    ])
    return w