예제 #1
0
    def test_nifti2nrrd(self):

        PREFIX = pjoin(outdir, 'nifti2nhdr')

        # run nifti2nrrd conversion
        conversion.nhdr_write(REFERENCE_NIFTI, REFERENCE_BVAL, REFERENCE_BVEC,
                              PREFIX + '.nhdr')
        shutil.copy(REFERENCE_NIFTI,
                    pjoin(outdir, os.path.basename(REFERENCE_NIFTI)))

        # load converted output
        converted_nrrd_data, converted_nrrd_hdr = read(PREFIX + '.nhdr')
        converted_nrrd_bvals, converted_nrrd_bvecs = conversion.nrrd_bvals_bvecs(
            converted_nrrd_hdr)[:2]

        # load converted reference
        reference_nrrd_data, reference_nrrd_hdr = read(CONVERTED_NHDR)
        reference_nrrd_bvals, reference_nrrd_bvecs = conversion.nrrd_bvals_bvecs(
            reference_nrrd_hdr)[:2]

        # test equality
        testing.assert_equal(reference_nrrd_hdr, converted_nrrd_hdr)
        testing.assert_array_equal(reference_nrrd_data, converted_nrrd_data)
        testing.assert_array_equal(reference_nrrd_bvals, converted_nrrd_bvals)
        testing.assert_array_equal(reference_nrrd_bvecs, converted_nrrd_bvecs)
예제 #2
0
파일: ukf.py 프로젝트: yoobinkwak/pnlNipype
    def main(self):

        with TemporaryDirectory() as tmpdir:
            tmpdir = local.path(tmpdir)
            shortdwi = tmpdir / 'dwiShort.nii.gz'
            shortmask = tmpdir / 'maskShort.nii.gz'

            tmpdwi = tmpdir / 'dwi.nhdr'
            tmpdwimask = tmpdir / 'dwimask.nhdr'

            # TODO when UKFTractography supports float32, it should be removed
            # typecast to short
            short = load_nifti(self.dwi._path)
            save_nifti(shortdwi._path,
                       short.get_data().astype('int16'), short.affine,
                       short.header)

            short = load_nifti(self.dwimask._path)
            save_nifti(shortmask._path,
                       short.get_data().astype('int16'), short.affine,
                       short.header)

            # convert the dwi to NRRD
            nhdr_write(shortdwi._path, self.bvalFile._path,
                       self.bvecFile._path, tmpdwi._path)

            # convert the mask to NRRD
            nhdr_write(shortmask._path, None, None, tmpdwimask._path)

            key_val_pair = []

            if self.givenParams:
                key_val_pair = self.givenParams.split(',')

                for i in range(
                        0,
                        len(ukfdefaults) - 1, 2
                ):  # -1 to pass --recordTensors which is always a default

                    try:
                        ind = key_val_pair.index(ukfdefaults[i])
                        ukfdefaults[i + 1] = key_val_pair[ind + 1]
                        # since ukfdefault[i+1] has been already replaced by key_val_pair[ind+1]
                        # remove key_val_pair[ind] and [ind+1] to prevent double specification
                        key_val_pair[ind:ind + 2] = []
                    except ValueError:
                        pass

            params = [
                '--dwiFile', tmpdwi, '--maskFile', tmpdwimask, '--seedsFile',
                tmpdwimask, '--tracts', self.out
            ] + list(ukfdefaults) + key_val_pair

            logging.info('Peforming UKF tractography of {}'.format(tmpdwi))
            UKFTractography[params] & FG
예제 #3
0
파일: ukf.py 프로젝트: pnlbwh/pnlNipype
    def main(self):

        with TemporaryDirectory() as tmpdir:
            tmpdir = local.path(tmpdir)
            shortdwi = tmpdir / 'dwiShort.nii.gz'
            shortmask = tmpdir / 'maskShort.nii.gz'

            tmpdwi = tmpdir / 'dwi.nhdr'
            tmpdwimask = tmpdir / 'dwimask.nhdr'

            # TODO when UKFTractography supports float32, it should be removed
            # typecast to short
            short= load_nifti(self.dwi._path)
            save_nifti(shortdwi._path, short.get_data().astype('int16'), short.affine, short.header)

            short= load_nifti(self.dwimask._path)
            save_nifti(shortmask._path, short.get_data().astype('int16'), short.affine, short.header)


            if self.bhigh:
                from conversion import grad_remove

                bhigh_prefix= tmpdir / self.dwi.stem+ f'_bhigh_{self.bhigh}'
                bhigh_dwi= bhigh_prefix+'.nii.gz'
                grad_remove(shortdwi._path, bhigh_dwi, interval=[int(self.bhigh)+50,1e6], bvalFile=self.bvalFile, bvecFile=self.bvecFile)

                shortdwi= local.path(bhigh_dwi)
                self.bvalFile = local.path(bhigh_prefix + '.bval')
                self.bvecFile = local.path(bhigh_prefix + '.bvec')

                # preserve the filtered attributes in case the user wants to run UKFTractography separately in future
                shortdwi.copy(self.dwi.dirname)
                self.bvalFile.copy(self.dwi.dirname)
                self.bvecFile.copy(self.dwi.dirname)


            # convert the dwi to NRRD
            nhdr_write(shortdwi._path, self.bvalFile._path, self.bvecFile._path, tmpdwi._path)

            # convert the mask to NRRD
            nhdr_write(shortmask._path, None, None, tmpdwimask._path)

            key_val_pair=[]

            if self.givenParams:
                key_val_pair= self.givenParams.split(',')

                for i in range(0,len(ukfdefaults)-1,2): # -1 to pass --recordTensors which is always a default

                    try:
                        ind= key_val_pair.index(ukfdefaults[i])
                        ukfdefaults[i + 1] = key_val_pair[ind + 1]
                        # since ukfdefault[i+1] has been already replaced by key_val_pair[ind+1]
                        # remove key_val_pair[ind] and [ind+1] to prevent double specification
                        key_val_pair[ind:ind+2]=[]
                    except ValueError:
                        pass


            params = ['--dwiFile', tmpdwi, '--maskFile', tmpdwimask,
                      '--seedsFile', tmpdwimask, '--tracts', self.out] + list(ukfdefaults) + key_val_pair


            logging.info('Peforming UKF tractography of {}'.format(tmpdwi))
            UKFTractography[params] & FG