def registerFs2Dwi_T2(tmpdir, namePrefix, b0masked, t2masked, T2toBrainAffine, wmparc, wmparc_out): print('Registering wmparc to B0') pre = tmpdir / namePrefix affine = pre + '0GenericAffine.mat' warp = pre + '1Warp.nii.gz' print('Computing warp from t2 to (resampled) baseline') check_call((' ').join(['antsRegistrationSyNMI.sh', '-d', '3', '-m', t2masked, '-f', b0masked, '-o', pre, '-n', N_CPU]), shell=True) print('Applying warp to wmparc.nii.gz to create (resampled) wmparcindwi.nii.gz') antsApplyTransforms('-d', '3', '-i', wmparc, '-t', warp, affine, T2toBrainAffine, '-r', b0masked, '-o', wmparc_out, '--interpolation', 'NearestNeighbor') print('Made ' + wmparc_out)
def registerFs2Dwi(tmpdir, namePrefix, b0masked, brain, wmparc, wmparc_out): pre = tmpdir / namePrefix affine = pre + '0GenericAffine.mat' warp = pre + '1Warp.nii.gz' print('Computing warp from brain.nii.gz to (resampled) baseline') check_call((' ').join([ pjoin(FILEDIR, 'antsRegistrationSyNMI.sh'), '-d', '3', '-m', brain, '-f', b0masked, '-o', pre, '-n', ANTSREG_THREADS ]), shell=True) print( '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') print('Made ' + wmparc_out)
def main(self): self.out = local.path(self.out) 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.nii.gz' t2masked = tmpdir / 'maskedt2.nii.gz' t2inbse = tmpdir / 't2inbse.nii.gz' epiwarp = tmpdir / 'epiwarp.nii.gz' t2tobse_rigid = tmpdir / 't2tobse_rigid' affine = tmpdir / 't2tobse_rigid0GenericAffine.mat' logging.info('1. Extract B0 and and mask it') check_call((' ').join([ pjoin(FILEDIR, 'bse.py'), '-m', self.dwimask, '-i', self.dwi, '-o', bse ]), shell=True) logging.info('2. Mask the T2') fslmaths(self.t2mask, '-mul', self.t2, t2masked) logging.info( '3. Compute a rigid registration from the T2 to the DWI baseline' ) rigid_registration(3, t2masked, bse, t2tobse_rigid) antsApplyTransforms('-d', '3', '-i', t2masked, '-o', t2inbse, '-r', bse, '-t', affine) logging.info( '4. Compute 1d nonlinear registration from the DWI to T2-in-bse along the phase direction' ) moving = bse fixed = t2inbse pre = tmpdir / 'epi' dwiepi = tmpdir / 'dwiepi.nii.gz' 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') check_call((' ').join([ pjoin(FILEDIR, 'antsApplyTransformsDWI.py'), '-i', self.dwi, '-m', self.dwimask, '-t', epiwarp, '-o', dwiepi, '-n', self.nproc ]), shell=True) # WarpTimeSeriesImageMultiTransform can also be used # dwimasked = tmpdir / 'masked_dwi.nii.gz' # fslmaths(self.dwi, '-mul', self.dwimask, dwimasked) # WarpTimeSeriesImageMultiTransform('4', dwimasked, dwiepi, '-R', dwimasked, '-i', epiwarp) logging.info('6. Apply warp to the DWI mask') epimask = self.out._path + '-mask.nii.gz' antsApplyTransforms('-d', '3', '-i', self.dwimask, '-o', epimask, '-n', 'NearestNeighbor', '-r', bse, '-t', epiwarp) fslmaths(epimask, '-mul', '1', epimask, '-odt', 'char') dwiepi.move(self.out._path + '.nii.gz') self.bvals_file.copy(self.out._path + '.bval') self.bvecs_file.copy(self.out._path + '.bvec') if self.debug: tmpdir.copy(self.out.dirname / ('epidebug-' + str(getpid())))