Exemplo n.º 1
0
    def main(self):

        with tempfile.TemporaryDirectory() as tmpdir:

            tmpdir = local.path(tmpdir)

            b0masked = tmpdir / "b0masked.nii.gz"  # Sylvain wants both
            b0maskedbrain = tmpdir / "b0maskedbrain.nii.gz"

            t2masked = tmpdir / 't2masked.nii.gz'
            print('Masking the T2')
            ImageMath(3, t2masked, 'm', self.t2, self.t2mask)

            brain = tmpdir / "brain.nii.gz"
            wmparc = tmpdir / "wmparc.nii.gz"

            brainmgz = self.parent.fsdir / 'mri/brain.mgz'
            wmparcmgz = self.parent.fsdir / 'mri/wmparc.mgz'

            wmparcindwi = tmpdir / 'wmparcInDwi.nii.gz'  # Sylvain wants both
            wmparcinbrain = tmpdir / 'wmparcInBrain.nii.gz'

            print(
                "Making brain.nii.gz and wmparc.nii.gz from their mgz versions"
            )

            vol2vol = local[self.parent.fshome / 'bin/mri_vol2vol']
            label2vol = local[self.parent.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)

            print('Extracting B0 from DWI and masking it')
            check_call((' ').join([
                pjoin(FILEDIR, 'bse.py'), '-i', self.parent.dwi, '-m',
                self.parent.dwimask, '-o', b0masked
            ]),
                       shell=True)
            print('Made masked B0')

            # rigid registration from t2 to brain.nii.gz
            pre = tmpdir / 'BrainToT2'
            BrainToT2Affine = pre + '0GenericAffine.mat'

            print('Computing rigid registration from brain.nii.gz to t2')
            rigid_registration(3, brain, t2masked, pre)
            # generates three files for rigid registration:
            # pre0GenericAffine.mat  preInverseWarped.nii.gz  preWarped.nii.gz

            # generates five files for default(rigid+affine+deformable syn) registration:
            # pre0GenericAffine.mat  pre1Warp.nii.gz  preWarped.nii.gz   pre1InverseWarp.nii.gz  preInverseWarped.nii.gz

            dwi_res = load_nifti(
                str(b0masked)).header['pixdim'][1:4].round(decimals=2)
            brain_res = load_nifti(
                str(brain)).header['pixdim'][1:4].round(decimals=2)
            print(f'DWI resolution: {dwi_res}')
            print(f'FreeSurfer brain resolution: {brain_res}')

            print('Registering wmparc to B0 through T2')
            registerFs2Dwi_T2(tmpdir, 'fsbrainToT2ToB0', b0masked, t2masked,
                              BrainToT2Affine, wmparc, wmparcindwi)

            if (dwi_res != brain_res).any():
                print(
                    'DWI resolution is different from FreeSurfer brain resolution'
                )
                print(
                    'wmparc wil be registered to both DWI and brain resolution'
                )
                print(
                    'Check output files wmparcInDwi.nii.gz and wmparcInBrain.nii.gz'
                )

                print('Resampling B0 to brain resolution')

                ResampleImageBySpacing('3', b0masked, b0maskedbrain,
                                       brain_res.tolist())

                print('Registering wmparc to resampled B0')
                registerFs2Dwi_T2(tmpdir, 'fsbrainToT2ToResampledB0',
                                  b0maskedbrain, t2masked, BrainToT2Affine,
                                  wmparc, wmparcinbrain)

            # copying images to outDir
            b0masked.copy(self.parent.out)
            wmparcindwi.copy(self.parent.out)

            if b0maskedbrain.exists():
                b0maskedbrain.copy(self.parent.out)
                wmparcinbrain.copy(self.parent.out)

            if self.parent.debug:
                tmpdir.copy(self.parent.out,
                            'fs2dwi-debug-' + str(os.getpid()))

        print('See output files in ', self.parent.out._path)
Exemplo n.º 2
0
    def main(self):

        with tempfile.TemporaryDirectory() as tmpdir:

            tmpdir = local.path(tmpdir)

            b0masked = tmpdir / "b0masked.nii.gz"  # Sylvain wants both
            b0maskedbrain = tmpdir / "b0maskedbrain.nii.gz"

            brain = tmpdir / "brain.nii.gz"
            wmparc = tmpdir / "wmparc.nii.gz"

            brainmgz = self.parent.fsdir / 'mri/brain.mgz'
            wmparcmgz = self.parent.fsdir / 'mri/wmparc.mgz'

            wmparcindwi = tmpdir / 'wmparcInDwi.nii.gz'  # Sylvain wants both
            wmparcinbrain = tmpdir / 'wmparcInBrain.nii.gz'

            print(
                "Making brain.nii.gz and wmparc.nii.gz from their mgz versions"
            )

            vol2vol = local[self.parent.fshome / 'bin/mri_vol2vol']
            label2vol = local[self.parent.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)

            print('Extracting B0 from DWI and masking it')
            check_call((' ').join([
                pjoin(FILEDIR, 'bse.py'), '-i', self.parent.dwi, '-m',
                self.parent.dwimask, '-o', b0masked
            ]),
                       shell=True)
            print('Made masked B0')

            dwi_res = load_nifti(
                str(b0masked)).header['pixdim'][1:4].round(decimals=2)
            brain_res = load_nifti(
                str(brain)).header['pixdim'][1:4].round(decimals=2)
            print(f'DWI resolution: {dwi_res}')
            print(f'FreeSurfer brain resolution: {brain_res}')

            print('Registering wmparc to B0')
            registerFs2Dwi(tmpdir, 'fsbrainToB0', b0masked, brain, wmparc,
                           wmparcindwi)

            if (dwi_res != brain_res).any():
                print(
                    'DWI resolution is different from FreeSurfer brain resolution'
                )
                print(
                    'wmparc wil be registered to both DWI and brain resolution'
                )
                print(
                    'Check output files wmparcInDwi.nii.gz and wmparcInBrain.nii.gz'
                )

                print('Resampling B0 to brain resolution')

                ResampleImageBySpacing('3', b0masked, b0maskedbrain,
                                       brain_res.tolist())

                print('Registering wmparc to resampled B0')
                registerFs2Dwi(tmpdir, 'fsbrainToResampledB0', b0maskedbrain,
                               brain, wmparc, wmparcinbrain)

            # copying images to outDir
            b0masked.copy(self.parent.out)
            wmparcindwi.copy(self.parent.out)

            if b0maskedbrain.exists():
                b0maskedbrain.copy(self.parent.out)
                wmparcinbrain.copy(self.parent.out)

            if self.parent.debug:
                tmpdir.copy(self.parent.out,
                            'fs2dwi-debug-' + str(os.getpid()))

        print('See output files in ', self.parent.out._path)