Example #1
0
def register_harmonized(imgPath, warp2mni, trans2mni, templatePath, siteName):

    print(f'Warping {imgPath} diffusion measures to standard space')
    directory = dirname(imgPath)
    inPrefix = imgPath.split('.nii')[0]
    prefix = psplit(inPrefix)[-1]

    dmImg = pjoin(directory, 'dti', prefix + f'_FA.nii.gz')
    dmTmp = pjoin(templatePath, f'Mean_{siteName}_FA.nii.gz')
    maskTmp = pjoin(templatePath, f'{siteName}_Mask.nii.gz')
    outPrefix = pjoin(templatePath, prefix + '_FA_ToMNI')
    warp2tmp = outPrefix + '1Warp.nii.gz'
    trans2tmp = outPrefix + '0GenericAffine.mat'
    # signal reconstruction might change with zero padding size, median filtering kernel size, and harmonized mask
    # so in case multiple debug is needed, redo the registration
    antsReg(dmTmp, maskTmp, dmImg, outPrefix)

    for dm in diffusionMeasures:
        output = pjoin(templatePath, prefix + f'_{dm}_ToTmpWarped.nii.gz')

        moving = pjoin(directory, 'dti', prefix + f'_{dm}.nii.gz')
        # warp diffusion measures to template space first, then to MNI space
        antsApplyTransforms['-d', '3', '-i', moving, '-o', output, '-r', dmTmp,
                            '-t', warp2tmp, trans2tmp] & FG

        output = pjoin(templatePath, prefix + f'_InMNI_{dm}.nii.gz')

        moving = pjoin(templatePath, prefix + f'_{dm}_ToTmpWarped.nii.gz')

        antsApplyTransforms['-d', '3', '-i', moving, '-o', output, '-r',
                            mniTmp, '-t', warp2mni, trans2mni] & FG
def register_harmonized(imgPath, warp2mni, trans2mni, templatePath, siteName):

    print(f'Warping {imgPath} diffusion measures to standard space')
    directory = dirname(imgPath)
    inPrefix = imgPath.split('.nii')[0]
    prefix = basename(inPrefix)

    dmImg = pjoin(directory, 'dti', prefix + f'_FA.nii.gz')
    dmTmp = pjoin(templatePath, f'Mean_{siteName}_FA_b{bshell_b}.nii.gz')
    maskTmp = pjoin(templatePath, f'{siteName}_Mask.nii.gz')
    outPrefix = pjoin(templatePath,
                      prefix.replace(f'_b{bshell_b}', '') + '_FA')
    warp2tmp = outPrefix + '1Warp.nii.gz'
    trans2tmp = outPrefix + '0GenericAffine.mat'

    # check existence of transforms created with _b{bmax}
    if not exists(warp2tmp):
        antsReg(dmTmp, maskTmp, dmImg, outPrefix)

    for dm in diffusionMeasures:
        output = pjoin(templatePath, prefix + f'_{dm}_ToTmpWarped.nii.gz')

        moving = pjoin(directory, 'dti', prefix + f'_{dm}.nii.gz')
        # warp diffusion measures to template space first, then to MNI space
        antsApplyTransforms['-d', '3', '-i', moving, '-o', output, '-r', dmTmp,
                            '-t', warp2tmp, trans2tmp] & FG

        output = pjoin(templatePath, prefix + f'_InMNI_{dm}.nii.gz')

        moving = pjoin(templatePath, prefix + f'_{dm}_ToTmpWarped.nii.gz')

        antsApplyTransforms['-d', '3', '-i', moving, '-o', output, '-r',
                            mniTmp, '-t', warp2mni, trans2mni] & FG
def register_target(imgPath, templatePath):

    print(f'Warping {imgPath} diffusion measures to standard space')
    directory = dirname(imgPath)
    inPrefix = imgPath.split('.nii')[0]
    prefix = psplit(inPrefix)[-1]

    dmImg = pjoin(directory, 'dti', prefix + f'_FA.nii.gz')

    outPrefix = pjoin(templatePath, prefix + '_FA_ToMNI_')
    warp2mni = outPrefix + '1Warp.nii.gz'
    trans2mni = outPrefix + '0GenericAffine.mat'
    # unprocessed target data is given, so in case multiple debug is needed, pass the registration
    if not exists(warp2mni):
        antsReg(mniTmp, None, dmImg, outPrefix)

    for dm in diffusionMeasures:
        output = pjoin(templatePath, prefix + f'_InMNI_{dm}.nii.gz')

        moving = pjoin(directory, 'dti', prefix + f'_{dm}.nii.gz')
        # warp diffusion measure to template space first, then to MNI space
        antsApplyTransforms[
            '-d', '3',
            '-i', moving,
            '-o', output,
            '-r', mniTmp,
            '-t', warp2mni, trans2mni,
        ] & FG
def sub2tmp2mni(templatePath, siteName, caselist, ref= False, tar_unproc= False, tar_harm= False):

    # obtain the transform
    moving = pjoin(templatePath, f'Mean_{siteName}_FA.nii.gz')

    outPrefix= pjoin(templatePath, f'TemplateToMNI_{siteName}')
    warp2mni= outPrefix+'1Warp.nii.gz'
    trans2mni= outPrefix+'0GenericAffine.mat'
    # template is created once, it is expected that the user wants to keep the template same during debugging
    # so in case multiple debug is needed, pass the registration
    if not exists(warp2mni):
        antsReg(mniTmp, None, moving, outPrefix)

    imgs, _= read_caselist(caselist)

    pool= multiprocessing.Pool(N_proc)
    for imgPath in imgs:

        if ref:
            pool.apply_async(func= register_reference, args= (imgPath, warp2mni, trans2mni, templatePath, ))
        elif tar_unproc:
            pool.apply_async(func= register_target, args= (imgPath, templatePath, ))
        elif tar_harm:
            pool.apply_async(func= register_harmonized, args= (imgPath, warp2mni, trans2mni, templatePath, siteName, ))

    pool.close()
    pool.join()
def sub2tmp2mni(templatePath,
                siteName,
                caselist,
                ref=False,
                tar_unproc=False,
                tar_harm=False):

    # obtain the transform
    moving = pjoin(templatePath, f'Mean_{siteName}_FA_b{bshell_b}.nii.gz')

    outPrefix = pjoin(templatePath, f'TemplateToMNI_{siteName}')
    warp2mni = outPrefix + '1Warp.nii.gz'
    trans2mni = outPrefix + '0GenericAffine.mat'

    # check existence of transforms created with _b{bmax}
    if not exists(warp2mni):
        antsReg(mniTmp, None, moving, outPrefix)

    imgs, _ = read_caselist(caselist)

    pool = multiprocessing.Pool(N_proc)
    for imgPath in imgs:

        if ref:
            pool.apply_async(func=register_reference,
                             args=(
                                 imgPath,
                                 warp2mni,
                                 trans2mni,
                                 templatePath,
                             ))
        elif tar_unproc:
            pool.apply_async(func=register_target,
                             args=(
                                 imgPath,
                                 templatePath,
                             ))
        elif tar_harm:
            pool.apply_async(func=register_harmonized,
                             args=(
                                 imgPath,
                                 warp2mni,
                                 trans2mni,
                                 templatePath,
                                 siteName,
                             ))

    pool.close()
    pool.join()