Ejemplo n.º 1
0
    def main(self):
        with TemporaryDirectory() as t:
            t = local.path(t)
            ukf = self.ukf
            fsindwi = self.fsindwi
            if nrrd(self.fsindwi):
                fsindwi = t / 'wmparcInDwi.nii.gz'
                ConvertBetweenFileFormats(self.fsindwi, fsindwi)
            if '.gz' in self.ukf.suffix:
                ukf = t / 'ukf.vtk'
                from plumbum.cmd import gunzip
                (gunzip['-c', self.ukf] > ukf)()

            tract_querier = local['tract_querier']
            tract_math = local['tract_math']
            ukfpruned = t / 'ukfpruned.vtk'
            # tract_math(ukf, 'tract_remove_short_tracts', '2', ukfpruned)
            tract_math[ukf, 'tract_remove_short_tracts', '2', ukfpruned] & FG
            if not ukfpruned.exists():
                raise Exception(
                    "tract_math failed to make '{}'".format(ukfpruned))
            self.out.mkdir()
            tract_querier['-t', ukfpruned, '-a', fsindwi, '-q', self.query,
                          '-o', self.out / '_'] & FG

            logging.info('Convert vtk field data to tensor data')

            # use the following multi-processed loop
            pool = Pool(int(self.nproc))
            pool.map_async(_activateTensors_py, self.out.glob('*.vtk'))
            pool.close()
            pool.join()
Ejemplo n.º 2
0
    def main(self):
        with TemporaryDirectory() as t:
            t = local.path(t)
            ukf = self.ukf
            fsindwi = self.fsindwi
            if nrrd(self.fsindwi):
                fsindwi = t / 'wmparcInDwi.nii.gz'
                ConvertBetweenFileFormats(self.fsindwi, fsindwi)
            if '.gz' in self.ukf.suffix:
                ukf = t / 'ukf.vtk'
                from plumbum.cmd import gunzip
                (gunzip['-c', self.ukf] > ukf)()

            tract_querier = local['tract_querier']
            tract_math = local['tract_math']
            ukfpruned = t / 'ukfpruned.vtk'
            # tract_math(ukf, 'tract_remove_short_tracts', '2', ukfpruned)
            tract_math[ukf, 'tract_remove_short_tracts', '2', ukfpruned] & FG
            if not ukfpruned.exists():
                raise Exception(
                    "tract_math failed to make '{}'".format(ukfpruned))
            self.out.mkdir()
            tract_querier['-t', ukfpruned, '-a', fsindwi, '-q', self.query,
                          '-o', self.out / '_'] & FG

            logging.info('Convert vtk field data to tensor data')
            for vtk in self.out.glob('*.vtk'):
                vtknew = vtk.dirname / (vtk.stem[2:] + ''.join(vtk.suffixes))
                activateTensors_py(vtk, vtknew)
                vtk.delete()
Ejemplo n.º 3
0
def makeAtlases(target, trainingTable, outdir, mabs=False):
    outdir = local.path(outdir)

    from plumbum.cmd import mkdir
    mkdir('-p', outdir)

    logging.info(
        'Create atlases: compute transforms from images to target and apply')
    for idx, r in trainingTable.iterrows():
        warp = outdir / 'warp{idx}.nii.gz'.format(**locals())
        atlas = outdir / 'atlas{idx}.nii.gz'.format(**locals())
        computeWarp(r['image'], target, warp)
        applyWarp(r['image'], warp, target, atlas)
        for labelname, label in r.iloc[1:].iteritems():
            atlaslabel = outdir / '{labelname}{idx}.nii.gz'.format(**locals())
            applyWarp(label,
                      warp,
                      target,
                      atlaslabel,
                      interpolation='NearestNeighbor')

    if mabs:
        from plumbum.cmd import unu, ConvertBetweenFileFormats, AverageImages
        for labelname in list(trainingTable)[1:]:
            out = outdir / labelname + '.nrrd'
            labelmaps = outdir // labelname + '*'
            with TemporaryDirectory() as tmpdir:
                nii = tmpdir / 'mabs.nii.gz'
                AverageImages('3', nii, '0', *labelmaps)
                ConvertBetweenFileFormats(nii, out)
            unu['2op', 'gt', out, '0.5'] | \
                unu['save', '-e', 'gzip', '-f', 'nrrd', '-o', out] & FG
Ejemplo n.º 4
0
def fuseAvg(labels, out):
    from plumbum.cmd import AverageImages
    with local.tempdir() as tmpdir:
        nii = local.path(tmpdir) / 'avg.nii.gz'
        AverageImages('3', nii, '0', *labels)
        ConvertBetweenFileFormats(nii, out)
    (unu['2op', 'gt', out, '0.5'] | \
     unu['save', '-e', 'gzip', '-f', 'nrrd', '-o', out]) & FG
Ejemplo n.º 5
0
def _Register_vol(vol):

    volnii = vol.with_suffix('.nii.gz')
    ConvertBetweenFileFormats(vol, volnii, 'short')
    logging.info('Run FSL flirt affine registration')
    flirt('-interp', 'sinc', '-sincwidth', '7', '-sincwindow', 'blackman',
          '-in', volnii, '-ref', 'b0.nii.gz', '-nosearch', '-o', volnii,
          '-omat', volnii.with_suffix('.txt', depth=2), '-paddingsize', '1')

    return volnii
Ejemplo n.º 6
0
def fuseAvg(labels, out):
    from plumbum.cmd import AverageImages
    with local.tempdir() as tmpdir:
        nii = local.path(tmpdir) / 'avg.nii.gz'
        AverageImages('3', nii, '0', *labels)

        # out is {labelname}.nrrd
        ConvertBetweenFileFormats(nii, out)

    # Binary operation, if out>0.5, pipe the output and save as {labelname}.nrrd
    (unu['2op', 'gt', out, '0.5'] | \
        unu['save', '-e', 'gzip', '-f', 'nrrd', '-o', out]) & FG
Ejemplo n.º 7
0
    def main(self):
        fshome = local.path(os.getenv('FREESURFER_HOME'))

        if not fshome:
            logging.error('Set FREESURFER_HOME first.')
            sys.exit(1)

        if not self.force and self.out.exists():
            logging.error(
                'Output directory exists, use -f/--force to force an overwrite.'
            )
            sys.exit(1)

        with TemporaryDirectory() as tmpdir, local.env(SUBJECTS_DIR=tmpdir,
                                                       FSFAST_HOME='',
                                                       MNI_DIR=''):

            if self.t1mask:
                logging.info('Mask the t1')
                ImageMath('3', tmpdir / 't1masked.nii.gz', 'm', self.t1,
                          self.t1mask)
                t1 = tmpdir / 't1masked.nii.gz'
                skullstrip = '-noskullstrip'
            else:
                skullstrip = ''
                if '.nrrd' in self.t1.suffixes or '.nhdr' in self.t1.suffixes:
                    logging.info('t1 is in nrrd format, convert to nifti')
                    t1 = tmpdir / 't1.nii.gz'
                    ConvertBetweenFileFormats(self.t1, t1)

            logging.info("Run freesurfer on " + t1)
            subjid = t1.stem

            from plumbum.cmd import bash
            bash['-c',
                 'source ' + fshome + '/FreeSurferEnv.sh; recon-all -s ' +
                 subjid + ' -i ' + t1 + ' -autorecon1 ' + skullstrip] & FG
            (tmpdir / subjid / 'mri/T1.mgz').copy(tmpdir / subjid /
                                                  'mri/brainmask.mgz')
            bash['-c', 'source ' + fshome +
                 '/FreeSurferEnv.sh; recon-all -autorecon2 -subjid ' +
                 subjid] & FG
            bash['-c', 'source ' + fshome +
                 '/FreeSurferEnv.sh; recon-all -autorecon3 -subjid ' +
                 subjid] & FG
            logging.info("Freesurfer done.")

            (tmpdir / subjid).copy(self.out,
                                   override=True)  # overwrites existing
            logging.info("Made " + self.out)
Ejemplo n.º 8
0
def fuseWeightedAvg(labels, weights, out):

    # for each label, fuse warped labelmaps to compute output labelmap
    print("Apply weights to warped training {} et al., fuse, and threshold".format(labels[0]))
    init= True
    for label, w in zip(labels, weights):
        img= nib.load(str(label))
        if init:
            data=img.get_data()*w
            affine= img.affine
            init= False
        else:
            data+=img.get_data()*w

    data= (data>0.5)*1

    with TemporaryDirectory() as tmpdir:
        nii = local.path(tmpdir) / 'avg.nii.gz'
        Nifti1Image= nib.Nifti1Image(data, affine= affine, header= img.header)
        nib.save(Nifti1Image, str(nii))
        ConvertBetweenFileFormats(nii, out)

    print("Made labelmap: " + out)
Ejemplo n.º 9
0
def convertImage(i, o, bthash):
    if i.suffixes == o.suffixes:
        i.copy(o)
    with BRAINSTools.env(bthash):
        from plumbum.cmd import ConvertBetweenFileFormats
        ConvertBetweenFileFormats(i, o)
Ejemplo n.º 10
0
    def main(self):
        self.out = local.path(self.out)
        if self.out.exists():
            if self.overwrite:
                self.out.delete()
            else:
                logging.error(
                    "{} exists, use '--force' to overwrite it".format(
                        self.out))
                sys.exit(1)
        outxfms = self.out.dirname / self.out.stem + '-xfms.tgz'
        with TemporaryDirectory() as tmpdir, local.cwd(tmpdir):
            tmpdir = local.path(tmpdir)

            # fileinput() caused trouble reading data file in python 3, so switching to nrrd
            # if the hdr has 'nan' in space origin, the following will take care of that
            img = nrrd.read(self.dwi)
            dwi = img[0]
            hdr = img[1]

            hdr_out = hdr.copy()
            hdr_out['space origin'] = hdr_out['space origin'][0:3]

            nrrd.write('dwijoined.nhdr',
                       dwi,
                       header=hdr_out,
                       compression_level=1)

            # we want to use this hdr to write a new .nhdr file with corresponding data file
            # so delete old data file from the hdr
            if 'data file' in hdr_out.keys():
                del hdr_out['data file']
            elif 'datafile' in hdr_out.keys():
                del hdr_out['datafile']

            if 'content' in hdr_out.keys():
                del hdr_out['content']

            logging.info('Dice the DWI')

            # Since fslmerge works along the 3rd axis only, dicing also has to be along that axis
            # So, use `unu permute` to reorient the volumes to be stacked along 3rd axis only
            # Include this issue in the tutorial
            (unu['convert', '-t', 'int16', '-i', 'dwijoined.nhdr']
             | unu['dice', '-a', '3', '-o', 'Diffusion-G'])()
            vols = tmpdir.glob('Diffusion-G*.nrrd')
            vols.sort()

            logging.info('Extract the B0')
            bse_py('-i', 'dwijoined.nhdr', '-o', 'b0.nrrd')
            ConvertBetweenFileFormats('b0.nrrd', 'b0.nii.gz', 'short')

            logging.info('Register each volume to the B0')

            # use the following multi-processed loop
            pool = Pool(int(self.nproc))
            res = pool.map_async(_Register_vol, vols)
            volsRegistered = res.get()
            pool.close()
            pool.join()

            # or use the following for loop
            # volsRegistered = []
            # for vol in vols:
            #     volnii = vol.with_suffix('.nii.gz')
            #     ConvertBetweenFileFormats(vol, volnii, 'short')
            #     logging.info('Run FSL flirt affine registration')
            #     flirt('-interp' ,'sinc'
            #           ,'-sincwidth' ,'7'
            #           ,'-sincwindow' ,'blackman'
            #           ,'-in', volnii
            #           ,'-ref', 'b0.nii.gz'
            #           ,'-nosearch'
            #           ,'-o', volnii
            #           ,'-omat', volnii.with_suffix('.txt', depth=2)
            #           ,'-paddingsize', '1')
            #     volsRegistered.append(volnii)

            fslmerge('-t', 'EddyCorrect-DWI', volsRegistered)
            transforms = tmpdir.glob('Diffusion-G*.txt')
            transforms.sort()

            # nibabel loading can be avoided by setting 'data file' = EddyCorrect-DWI.nii.gz
            # and 'byteskip' = -1
            # Tashrif updated Pynrrd package to properly handle that
            new_dwi = nib.load('EddyCorrect-DWI.nii.gz').get_data()

            logging.info('Extract the rotations and realign the gradients')

            space = hdr_out['space'].lower()
            if (space == 'left'):
                spctoras = np.matrix([[-1, 0, 0], [0, -1, 0], [0, 0, 1]])
            else:
                spctoras = np.matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
            mf = np.matrix(hdr['measurement frame'])

            # Transforms are in RAS so need to do inv(MF)*inv(SPC2RAS)*ROTATION*SPC2RAS*MF*GRADIENT
            mfras = mf.I * spctoras.I
            rasmf = spctoras * mf
            for (i, t) in enumerate(transforms):

                gDir = [
                    float(num) for num in hdr_out['DWMRI_gradient_' +
                                                  '{:04}'.format(i)].split(' ')
                    if num
                ]

                logging.info('Apply ' + t)
                tra = np.loadtxt(t)
                # removes the translation
                aff = np.matrix(tra[0:3, 0:3])
                # computes the finite strain of aff to get the rotation
                rot = aff * aff.T
                # compute the square root of rot
                [el, ev] = np.linalg.eig(rot)
                eL = np.identity(3) * np.sqrt(el)
                sq = ev * eL * ev.I
                # finally the rotation is defined as
                rot = sq.I * aff
                newdir = np.dot(mfras * rot * rasmf, gDir)

                hdr_out['DWMRI_gradient_' + '{:04}'.format(i)] = ('   ').join(
                    str(x) for x in newdir.tolist()[0])

            tar('cvzf', outxfms, transforms)

            nrrd.write(self.out, new_dwi, header=hdr_out, compression_level=1)

            if self.debug:
                tmpdir.copy(
                    join(dirname(self.out), "eddy-debug-" + str(getpid())))
Ejemplo n.º 11
0
    def main(self):
        self.out = local.path(self.out)
        if self.out.exists():
            if self.overwrite:
                self.out.delete()
            else:
                logging.error(
                    "{} exists, use '--force' to overwrite it".format(
                        self.out))
                sys.exit(1)
        outxfms = self.out.dirname / self.out.stem + '-xfms.tgz'
        with TemporaryDirectory() as tmpdir, local.cwd(tmpdir):
            tmpdir = local.path(tmpdir)

            unu('save', '-f', 'nrrd', '-e', 'gzip', '-i', self.dwi, '-o',
                'dwijoined.nhdr')

            logging.info('Dice the DWI')
            (unu['convert', '-t', 'int16', '-i', 'dwijoined.nhdr']
             | unu['dice', '-a', '3', '-o', 'Diffusion-G'])()
            vols = tmpdir.glob('Diffusion-G*.nrrd')
            vols.sort()

            logging.info('Extract the B0')
            bse_py('-i', 'dwijoined.nhdr', '-o', 'b0.nrrd')
            ConvertBetweenFileFormats('b0.nrrd', 'b0.nii.gz', 'short')

            logging.info('Register each volume to the B0')
            volsRegistered = []
            for vol in vols:
                volnii = vol.with_suffix('.nii.gz')
                ConvertBetweenFileFormats(vol, volnii, 'short')
                logging.info('Run FSL flirt affine registration')
                flirt('-interp', 'sinc', '-sincwidth', '7', '-sincwindow',
                      'blackman', '-in', volnii, '-ref', 'b0.nii.gz',
                      '-nosearch', '-o', volnii, '-omat',
                      volnii.with_suffix('.txt', depth=2), '-paddingsize', '1')
                volsRegistered.append(volnii)
            fslmerge('-t', 'EddyCorrect-DWI', volsRegistered)
            transforms = tmpdir.glob('Diffusion-G*.txt')
            transforms.sort()

            logging.info('Extract the rotations and realign the gradients')
            gDir = []
            header = ''
            gNum = []
            gframe = []
            with open('dwijoined.nhdr') as f:
                for line in f:
                    if line.find('DWMRI_gradient_') != -1:
                        gNum.append(line[15:19])
                        gDir.append(map(float, line[21:-1].split()))
                    elif line.find('data file:') != -1:
                        header = header + 'data file: EddyCorrect-DWI.nii.gz\n'
                    elif line.find('encoding:') != -1:
                        header = header + line + 'byteskip: -1\n'
                    elif line.find('measurement frame:') != -1:
                        header = header + line
                        mf = np.matrix([
                            map(float,
                                line.split()[2][1:-1].split(',')),
                            map(float,
                                line.split()[3][1:-1].split(',')),
                            map(float,
                                line.split()[4][1:-1].split(','))
                        ])
                    elif line.find('space:') != -1:
                        header = header + line
                        # Here I assume either lps or ras so only need to check the first letter
                        space = line.split()[1][0]
                        if (space == 'l') | (space == 'L'):
                            spctoras = np.matrix([[-1, 0, 0], [0, -1, 0],
                                                  [0, 0, 1]])
                        else:
                            spctoras = np.matrix([[1, 0, 0], [0, 1, 0],
                                                  [0, 0, 1]])
                    else:
                        header = header + line

            with open('EddyCorrect-DWI.nhdr', 'w') as f:
                f.write(header)
                i = 0
                # Transforms are in RAS so need to do inv(MF)*inv(SPC2RAS)*ROTATION*SPC2RAS*MF*GRADIENT
                mfras = mf.I * spctoras.I
                rasmf = spctoras * mf
                for t in transforms:
                    logging.info('Apply ' + t)
                    tra = np.loadtxt(t)
                    #removes the translation
                    aff = np.matrix(tra[0:3, 0:3])
                    # computes the finite strain of aff to get the rotation
                    rot = aff * aff.T
                    # Computer the square root of rot
                    [el, ev] = np.linalg.eig(rot)
                    eL = np.identity(3) * np.sqrt(el)
                    sq = ev * eL * ev.I
                    # finally the rotation is defined as
                    rot = sq.I * aff
                    newdir = np.dot(mfras * rot * rasmf, gDir[i])
                    f.write('DWMRI_gradient_' + gNum[i] + ':= ' +
                            str(newdir[0, 0]) + ' ' + str(newdir[0, 1]) + ' ' +
                            str(newdir[0, 2]) + '\n')
                    i = i + 1

            tar('cvzf', outxfms, transforms)
            unu('save', '-f', 'nrrd', '-e', 'gzip', '-i',
                'EddyCorrect-DWI.nhdr', '-o', self.out)

            if self.debug:
                tmpdir.move("eddy-debug-" + str(getpid()))
Ejemplo n.º 12
0
    def main(self):

        with 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'
            logging.info('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'

            logging.info(
                "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)

            logging.info('Extracting B0 from DWI and masking it')
            bse_py['-i', self.parent.dwi, '-m', self.parent.dwimask, '-o',
                   tmpdir / 'b0mask.nrrd'] & FG
            ConvertBetweenFileFormats(tmpdir / 'b0mask.nrrd', b0masked)
            logging.info('Made masked B0')

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

            logging.info(
                'Computing rigid registration from brain.nii.gz to t2')
            # antsRegistrationSyNMI_sh['-d', '3', '-t', 'r', '-m', brain, '-f', t2masked, '-o', pre,
            #                 '-n', N_CPU] & FG
            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 = nib.load(str(b0masked)).header['pixdim'][1:4].round()
            brain_res = nib.load(str(brain)).header['pixdim'][1:4].round()
            logging.info(f'DWI resolution: {dwi_res}')
            logging.info(f'FreeSurfer brain resolution: {brain_res}')

            if dwi_res.ptp() or brain_res.ptp():
                logging.info('Resolution is not uniform among all the axes')

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

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

                logging.info('Resampling B0 to brain resolution')

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

                logging.info('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()))

        logging.info('See output files in ' + self.parent.out._path)
Ejemplo n.º 13
0
    def main(self):

        with 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'

            logging.info(
                "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)

            logging.info('Extracting B0 from DWI and masking it')
            bse_py['-i', self.parent.dwi, '-m', self.parent.dwimask, '-o',
                   tmpdir / 'b0mask.nrrd'] & FG
            ConvertBetweenFileFormats(tmpdir / 'b0mask.nrrd', b0masked)
            logging.info('Made masked B0')

            dwi_res = nib.load(str(b0masked)).header['pixdim'][1:4].round()
            brain_res = nib.load(str(brain)).header['pixdim'][1:4].round()
            logging.info(f'DWI resolution: {dwi_res}')
            logging.info(f'FreeSurfer brain resolution: {brain_res}')

            if dwi_res.ptp() or brain_res.ptp():
                logging.info('Resolution is not uniform among all the axes')

            logging.info('Registering wmparc to B0')
            registerFs2Dwi(tmpdir, 'fsbrainToB0', b0masked, brain, wmparc,
                           wmparcindwi)

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

                logging.info('Resampling B0 to brain resolution')

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

                logging.info('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()))

        logging.info('See output files in ' + self.parent.out._path)