Exemplo n.º 1
0
 def main(self):
     with TemporaryDirectory() as tmpdir:
         tmpdir = local.path(tmpdir)
         pre = tmpdir / 'ants'
         rigidxfm = pre + '0GenericAffine.mat'
         antsRegistrationSyN_sh['-t', 'r', '-m', self.infile, '-f',
                                self.target, '-o', pre, '-n', 32] & FG
         antsApplyTransforms('-d', '3', '-i', self.labelmap, '-t', rigidxfm,
                             '-r', self.target, '-o', self.out,
                             '--interpolation', 'NearestNeighbor')
Exemplo n.º 2
0
    def main(self):
        fshome = local.path(os.getenv('FREESURFER_HOME'))
        if not fshome:
            logging.error('Set FREESURFER_HOME first.')
            sys.exit(1)

        with TemporaryDirectory() as tmpdir:
            tmpdir = local.path(tmpdir)
            b0masked = tmpdir / "b0masked.nrrd"
            b0masked1mm = tmpdir / "b0masked1mm.nrrd"
            brain = tmpdir / "brain.nii.gz"
            wmparc = tmpdir / "wmparc.nii.gz"
            brainmgz = self.parent.fsdir / 'mri/brain.mgz'
            wmparcmgz = self.parent.fsdir / 'mri/wmparc.mgz'
            wmparcindwi1mm = tmpdir / 'wmparcInDwi1mm.nii.gz'

            logging.info(
                "Make brain.nii.gz and wmparc.nii.gz from their mgz versions")
            vol2vol = local[fshome / 'bin/mri_vol2vol']
            label2vol = local[fshome / 'bin/mri_label2vol']
            with local.env(SUBJECTS_DIR=''):
                vol2vol('--mov', brainmgz, '--targ', brainmgz, '--regheader',
                        '--o', brain)
                label2vol('--seg', wmparcmgz, '--temp', brainmgz,
                          '--regheader', wmparcmgz, '--o', wmparc)

            logging.info('Extract B0 from DWI and mask')
            bse_py('-i', self.parent.dwi, '-m', self.parent.dwimask, '-o',
                   b0masked)
            logging.info('Made masked B0')

            logging.info('Upsample masked baseline to 1x1x1')
            ResampleImageBySpacing('3', b0masked, b0masked1mm, '1', '1', '1')
            logging.info('Made 1x1x1 baseline')

            logging.info('Register wmparc to B0')
            pre = tmpdir / 'fsbrain_to_b0'
            affine = pre + '0GenericAffine.mat'
            warp = pre + '1Warp.nii.gz'
            antsRegistrationSyNMI_sh['-m', brain, '-f', b0masked1mm, '-o', pre,
                                     '-n', 32] & FG
            antsApplyTransforms('-d', '3', '-i', wmparc, '-t', warp, affine,
                                '-r', b0masked1mm, '-o', wmparcindwi1mm,
                                '--interpolation', 'NearestNeighbor')
            logging.info('Made ' + wmparcindwi1mm)

            logging.info('Make output directory')
            self.parent.out.mkdir()
            b0masked.copy(self.parent.out)
            b0masked1mm.copy(self.parent.out)
            wmparcindwi1mm.copy(self.parent.out)
Exemplo n.º 3
0
    def main(self):
        if not self.force and self.out.exists():
            logging.error("'{}' already exists, use '--force' to force overwrite.".format(self.out))
            sys.exit(1)
        with TemporaryDirectory() as tmpdir:
            tmpdir = local.path(tmpdir)
            bse = tmpdir / "maskedbse.nrrd"
            t2masked = tmpdir / "maskedt2.nrrd"
            t2inbse = tmpdir / "t2inbse.nrrd"
            epiwarp = tmpdir / "epiwarp.nii.gz"
            t2tobse_rigid = tmpdir / "t2tobse_rigid"

            logging.info('1. Extract and mask the DWI b0')
            bse_py('-m', self.dwimask, '-i', self.dwi, '-o', bse)

            logging.info("2. Mask the T2")
            unu("3op", "ifelse", self.t2mask, self.t2, "0", "-o", t2masked)

            logging.info(
                "3. Compute a rigid registration from the T2 to the DWI baseline")
            antsRegistrationSyN_sh("-d", "3", "-f", bse, "-m", t2masked, "-t",
                                   "r", "-o", tmpdir / "t2tobse_rigid")
            antsApplyTransforms("-d", "3", "-i", t2masked, "-o", t2inbse, "-r",
                                bse, "-t",
                                tmpdir / "t2tobse_rigid0GenericAffine.mat")

            logging.info(
                "4. Compute 1d nonlinear registration from the DWI to the T2 along the phase direction")
            moving = bse
            fixed = t2inbse
            pre = tmpdir / "epi"
            dwiepi = tmpdir / ("dwiepi" + ''.join(self.out.suffixes))
            antsRegistration("-d", "3", "-m",
                             "cc[" + str(fixed) + "," + str(moving) + ",1,2]", "-t",
                             "SyN[0.25,3,0]", "-c", "50x50x10", "-f", "4x2x1",
                             "-s", "2x1x0", "--restrict-deformation", "0x1x0",
                             "-v", "1", "-o", pre)

            local.path(str(pre) + "0Warp.nii.gz").move(epiwarp)

            logging.info("5. Apply warp to the DWI")
            antsApplyTransformsDWI_py('-i', self.dwi, '-m', self.dwimask, '-t', epiwarp, '-o', dwiepi)

            if '.nhdr' in dwiepi.suffixes:
                unu("save", "-e", "gzip", "-f", "nrrd", "-i", dwiepi, self.out)
            else:
                dwiepi.move(self.out)

            if self.debug:
                tmpdir.copy(self.out.dirname / ("epidebug-" + str(getpid())))
Exemplo n.º 4
0
def applyWarp(moving, warp, reference, out, interpolation='Linear'):
    '''Interpolation options:
    Linear
    NearestNeighbor
    MultiLabel[<sigma=imageSpacing>,<alpha=4.0>]
    Gaussian[<sigma=imageSpacing>,<alpha=1.0>]
    BSpline[<order=3>]
    CosineWindowedSinc
    WelchWindowedSinc
    HammingWindowedSinc
    LanczosWindowedSinc
    GenericLabel[<interpolator=Linear>]
    '''
    from util.antspath import antsApplyTransforms
    antsApplyTransforms('-d', '3', '-i', moving, '-t', warp, '-r', reference,
                        '-o', out, '--interpolation', interpolation)
Exemplo n.º 5
0
def registerFs2Dwi(tmpdir, namePrefix, b0masked, brain, wmparc, wmparc_out):

    logging.info('Registering wmparc to B0')
    pre = tmpdir / namePrefix
    affine = pre + '0GenericAffine.mat'
    warp = pre + '1Warp.nii.gz'

    logging.info('Computing warp from brain.nii.gz to (resampled) baseline')
    antsRegistrationSyNMI_sh['-m', brain, '-f', b0masked, '-o', pre, '-n',
                             N_CPU] & FG

    logging.info(
        'Applying warp to wmparc.nii.gz to create (resampled) wmparcindwi.nii.gz'
    )
    antsApplyTransforms('-d', '3', '-i', wmparc, '-t', warp, affine, '-r',
                        b0masked, '-o', wmparc_out, '--interpolation',
                        'NearestNeighbor')

    logging.info('Made ' + wmparc_out)
Exemplo n.º 6
0
    def main(self):
        if not self.force and self.out.exists():
            logging.error(
                "'{}' already exists, use '--force' to force overwrite.".
                format(self.out))
            sys.exit(1)
        with TemporaryDirectory() as tmpdir:
            tmpdir = local.path(tmpdir)
            bse = tmpdir / "maskedbse.nrrd"
            t2masked = tmpdir / "maskedt2.nrrd"
            t2inbse = tmpdir / "t2inbse.nrrd"
            epiwarp = tmpdir / "epiwarp.nii.gz"

            t2tobse_rigid = tmpdir / "t2tobse_rigid"
            affine = tmpdir / "t2tobse_rigid0GenericAffine.mat"

            logging.info('1. Extract and mask the DWI b0')
            bse_py('-m', self.dwimask, '-i', self.dwi, '-o', bse)

            logging.info("2. Mask the T2")
            unu("3op", "ifelse", self.t2mask, self.t2, "0", "-o", t2masked)

            logging.info(
                "3. Compute a rigid registration from the T2 to the DWI baseline"
            )
            antsRegistrationSyN_sh("-d", "3", "-f", bse, "-m", t2masked, "-t",
                                   "r", "-o", tmpdir / "t2tobse_rigid")

            antsApplyTransforms("-d", "3", "-i", t2masked, "-o", t2inbse, "-r",
                                bse, "-t", affine)

            logging.info(
                "4. Compute 1d nonlinear registration from the DWI to the T2 along the phase direction"
            )
            moving = bse
            fixed = t2inbse
            pre = tmpdir / "epi"
            dwiepi = tmpdir / ("dwiepi" + ''.join(self.out.suffixes))
            antsRegistration("-d", "3", "-m",
                             "cc[" + str(fixed) + "," + str(moving) + ",1,2]",
                             "-t", "SyN[0.25,3,0]", "-c", "50x50x10", "-f",
                             "4x2x1", "-s", "2x1x0", "--restrict-deformation",
                             "0x1x0", "-v", "1", "-o", pre)

            local.path(str(pre) + "0Warp.nii.gz").move(epiwarp)

            logging.info("5. Apply warp to the DWI")
            antsApplyTransformsDWI_py['-i', self.dwi, '-m', self.dwimask, '-t',
                                      epiwarp, '-o', dwiepi, '-n',
                                      str(self.nproc)] & FG

            logging.info('6. Apply warp to the DWI mask')
            epimask = self.out.dirname / self.out.basename.split(
                '.')[0] + '-mask.nrrd'
            antsApplyTransforms('-d', '3', '-i', self.dwimask, '-o', epimask,
                                '-n', 'NearestNeighbor', '-r', bse, '-t',
                                epiwarp)
            unu('convert', '-t', 'uchar', '-i', epimask, '-o', epimask)

            if '.nhdr' in dwiepi.suffixes:
                unu("save", "-e", "gzip", "-f", "nrrd", "-i", dwiepi, "-o",
                    self.out)
            else:
                dwiepi.move(self.out)

            # FIXME: the following conversion is only for UKFTractography, should be removed in future
            if self.typeCast:
                unu('convert', '-t', 'int16', '-i', self.out, '-o', self.out)

            if self.debug:
                tmpdir.copy(self.out.dirname / ("epidebug-" + str(getpid())))