예제 #1
0
def test_to3d():
    input_map = dict(
        args=dict(argstr='%s', ),
        assumemosaic=dict(argstr='-assume_dicom_mosaic', ),
        datatype=dict(argstr='-datum %s', ),
        environ=dict(usedefault=True, ),
        filetype=dict(argstr='-%s', ),
        funcparams=dict(argstr='-time:zt %s alt+z2', ),
        ignore_exception=dict(usedefault=True, ),
        infolder=dict(
            argstr='%s/*.dcm',
            mandatory=True,
        ),
        out_file=dict(
            argstr='-prefix %s',
            mandatory=True,
        ),
        outputtype=dict(),
        skipoutliers=dict(argstr='-skip_outliers', ),
    )
    instance = afni.To3D()
    for key, metadata in input_map.items():
        for metakey, value in metadata.items():
            yield assert_equal, getattr(instance.inputs.traits()[key],
                                        metakey), value
예제 #2
0
def genrDcm2afni(hq, dcm, oniiDir, series_desc, series_id, quality, scanType):
    if not os.path.exists(oniiDir):
        os.makedirs(oniiDir)
    #set the output name based on the seq_dictionary
    if hq:
        afniOutName = seq_dict[series_desc] + '_' + str(series_id) + '_best'
    else:
        afniOutName = seq_dict[series_desc] + '_' + str(series_id)
    if os.path.exists(os.path.join(oniiDir, afniOutName)) and not overwriteExisting:
        return
    #gather some fun facts about the scan
    dcmInfo = pd.read_file(dcm, stop_before_pixels=True)
    repetitionTime = int(dcmInfo.RepetitionTime)
    numRepetitions = int(dcmInfo.NumberOfTemporalPositions)
    numImages = int(dcmInfo.ImagesInAcquisition)
    #this tag is not easy to get, so you need to first get a pydicom friendly tag
    nSlices = int(dcmInfo[pd.tag.BaseTag('2166863L')].value)
    if not nSlices == (numImages/numRepetitions):
        print('something is not right...num slices not matching num dicom images')
    #run the to3d conversion
    optStr = '-session ' + oniiDir + ' -time:zt ' + str(nSlices) + ' ' + str(numRepetitions) + ' ' + str(repetitionTime)
    to3d = afni.To3D(in_folder=os.path.dirname(dcm), out_file=afniOutName, outputtype='AFNI', args=optStr, terminal_output='stream', ignore_exception=True)
    print(to3d.cmdline)
    to3d.run()
    addcmd = True
    if convertIdc:
        #remove any instance of R-number...
        addcmd = False
    logFile = os.path.join(oniiDir, afniOutName + '.log')
    write_log(to3d.cmdline, logFile, addcmd, [series_id, series_desc, quality, scanType])    
예제 #3
0
파일: t1.py 프로젝트: bpinsard/misc
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