def __produceEncodingFiles(self, bEncs, bVecs, bVals, dwi): self.info("Produce .b .bvals and .bvecs gradient file if not existing") if not bEncs: bEncs = self.buildName(dwi, None, "b") self.info(mriutil.fslToMrtrixEncoding(dwi, bVecs, bVals, bEncs)) else: self.info("Linking {} to {}".format( bEncs, util.symlink(bEncs, self.workingDir))) if not bVecs or not bVals: self.info( mriutil.mrtrixToFslEncoding(dwi, bEncs, self.buildName(dwi, None, "bvecs"), self.buildName(dwi, None, "bvals"))) else: self.info("Linking {} to {}".format( bVecs, util.symlink(bVecs, self.workingDir))) self.info("Linking {} to {}".format( bVals, util.symlink(bVals, self.workingDir))) return (self.getImage('grad', None, 'b'), self.getImage('grad', None, 'bvecs'), self.getImage('grad', None, 'bvals'))
def __produceEncodingFiles(self, bEncs, bVecs, bVals, dwi): self.info("Produce .b .bvals and .bvecs gradient file if not existing") if not bEncs: bEncs = self.buildName(dwi, None, "b") self.info(mriutil.fslToMrtrixEncoding(dwi, bVecs, bVals, bEncs)) else: self.info("Linking {} to {}".format(bEncs, util.symlink(bEncs, self.workingDir))) if not bVecs or not bVals: self.info(mriutil.mrtrixToFslEncoding(dwi, bEncs, self.buildName(dwi, None, "bvecs"), self.buildName(dwi, None, "bvals"))) else: self.info("Linking {} to {}".format(bVecs, util.symlink(bVecs, self.workingDir))) self.info("Linking {} to {}".format(bVals, util.symlink(bVals, self.workingDir))) return (self.getImage('grad', None, 'b'), self.getImage('grad', None, 'bvecs'), self.getImage('grad', None, 'bvals'))
def implement(self): dwi = self.getDenoisingImage('dwi', 'denoise') if not dwi: dwi = self.getPreparationImage('dwi') b0AP = self.getPreparationImage('b0_ap') b0PA = self.getPreparationImage('b0_pa') bEnc = self.getPreparationImage('grad', None, 'b') bVals = self.getPreparationImage('grad', None, 'bvals') bVecs = self.getPreparationImage('grad', None, 'bvecs') norm = self.getParcellationImage('norm') parcellationMask = self.getParcellationImage('mask') # Fieldmap only mag = self.getPreparationImage("mag") phase = self.getPreparationImage("phase") freesurferAnat = self.getParcellationImage('anat', 'freesurfer') self.info("extract b0 image from the dwi") b0 = os.path.join( self.workingDir, os.path.basename(dwi).replace(self.get("prefix", 'dwi'), self.get("prefix", 'b0'))) self.info(mriutil.extractFirstB0FromDwi(dwi, b0, bVals)) self.info( "look if all images have the same voxel size and dimension scale") self.__validateSizeAndDimension(dwi, b0, b0AP, b0PA) # Generate a missing b0 image if we could. --> 0 = P>>A, 1 = A>>P if self.get("phase_enc_dir") == "0" and b0AP and b0PA is False: b0PA = b0 if self.get("phase_enc_dir") == "1" and b0PA and b0AP is False: b0AP = b0 [dwi, b0, b0AP, b0PA] = self.__oddEvenNumberOfSlices(dwi, b0, b0AP, b0PA) self.set('method', None) if b0AP is False or b0PA is False: topupBaseName = None b0Image = b0 self.set('method', 'fieldmap') else: # Concatenate B0 image together if self.get("phase_enc_dir") == "0": concatenateB0Image = self.__concatenateB0( b0PA, b0AP, self.buildName("b0pa_b0ap", None, "nii.gz")) elif self.get("phase_enc_dir") == "1": concatenateB0Image = self.__concatenateB0( b0AP, b0PA, self.buildName("b0ap_b0pa", None, "nii.gz")) # Create the acquisition parameter file acqpTopup = self.__createAcquisitionParameterFile('topup') # Run topup on concatenate B0 image [topupBaseName, topupImage] = self.__topup(concatenateB0Image, acqpTopup, self.get('b02b0_filename')) b0Image = self.__fslmathsTmean( os.path.join(self.workingDir, topupImage)) self.set('method', 'topup') self.info("create a suitable mask for the dwi") extraArgs = " -dof 6 " # same subject if self.get("methodology", "intrasession"): extraArgs += " -usesqform " mask = mriutil.computeDwiMaskFromFreesurfer( b0Image, norm, parcellationMask, self.buildName(parcellationMask, 'temporary'), extraArgs) # Create the acquisition parameter file for eddy acqpEddy = self.__createAcquisitionParameterFile('eddy') # Create an index file indexFile = self.__createIndexFile(mriutil.getNbDirectionsFromDWI(dwi)) outputImage = self.__correctionEddy(dwi, mask, topupBaseName, indexFile, acqpEddy, bVecs, bVals) eddyParameterFiles = self.getImage('dwi', None, 'eddy_parameters') if eddyParameterFiles: self.info( "Apply eddy movement correction to gradient encodings directions" ) bEnc = mriutil.applyGradientCorrection( bEnc, eddyParameterFiles, self.buildName(outputImage, None, 'b')) self.info( mriutil.mrtrixToFslEncoding( outputImage, bEnc, self.buildName(outputImage, None, 'bvecs'), self.buildName(outputImage, None, 'bvals'))) # Proceed with fieldmap if provided if mag and phase and not self.__topupCorrection: # OutputImage is now used for fieldmap correction outputImage = self.__computeFieldmap(outputImage, bVals, mag, phase, norm, parcellationMask, freesurferAnat) self.set('method', 'fieldmap') # Produce a valid b0 and mask for QA b0Corrected = self.buildName(b0, 'corrected') self.info( mriutil.extractFirstB0FromDwi(outputImage, b0Corrected, bVals)) maskCorrected = mriutil.computeDwiMaskFromFreesurfer( b0Corrected, norm, parcellationMask, self.buildName(parcellationMask, 'corrected'), extraArgs) self.rename(outputImage, self.buildName(outputImage, 'corrected'))
def implement(self): dwi = self.getDenoisingImage('dwi', 'denoise') if not dwi: dwi = self.getPreparationImage('dwi') b0AP = self.getPreparationImage('b0_ap') b0PA = self.getPreparationImage('b0_pa') bEnc = self.getPreparationImage('grad', None, 'b') bVals = self.getPreparationImage('grad', None, 'bvals') bVecs = self.getPreparationImage('grad', None, 'bvecs') norm = self.getParcellationImage('norm') parcellationMask = self.getParcellationImage('mask') # Fieldmap only mag = self.getPreparationImage("mag") phase = self.getPreparationImage("phase") freesurferAnat = self.getParcellationImage('anat', 'freesurfer') self.info("extract b0 image from the dwi") b0 = os.path.join(self.workingDir, os.path.basename(dwi).replace(self.get("prefix", 'dwi'), self.get("prefix", 'b0'))) self.info(mriutil.extractFirstB0FromDwi(dwi, b0, bVals)) self.info("look if all images have the same voxel size and dimension scale") self.__validateSizeAndDimension(dwi, b0, b0AP, b0PA) # Generate a missing b0 image if we could. --> 0 = P>>A, 1 = A>>P if self.get("phase_enc_dir") == "0" and b0AP and b0PA is False: b0PA = b0 if self.get("phase_enc_dir") == "1" and b0PA and b0AP is False: b0AP = b0 [dwi, b0, b0AP, b0PA] = self.__oddEvenNumberOfSlices(dwi, b0, b0AP, b0PA) self.set('method', None) if b0AP is False or b0PA is False: topupBaseName = None b0Image = b0 self.set('method', 'fieldmap') else: # Concatenate B0 image together if self.get("phase_enc_dir") == "0": concatenateB0Image = self.__concatenateB0(b0PA, b0AP, self.buildName("b0pa_b0ap", None, "nii.gz")) elif self.get("phase_enc_dir") == "1": concatenateB0Image = self.__concatenateB0(b0AP, b0PA, self.buildName("b0ap_b0pa", None, "nii.gz")) # Create the acquisition parameter file acqpTopup = self.__createAcquisitionParameterFile('topup') # Run topup on concatenate B0 image [topupBaseName, topupImage] = self.__topup(concatenateB0Image, acqpTopup, self.get('b02b0_filename')) b0Image = self.__fslmathsTmean(os.path.join(self.workingDir, topupImage)) self.set('method', 'topup') self.info("create a suitable mask for the dwi") extraArgs = " -dof 6 " # same subject if self.get("methodology", "intrasession"): extraArgs += " -usesqform " mask = mriutil.computeDwiMaskFromFreesurfer(b0Image, norm, parcellationMask, self.buildName(parcellationMask, 'temporary'), extraArgs) # Create the acquisition parameter file for eddy acqpEddy = self.__createAcquisitionParameterFile('eddy') # Create an index file indexFile = self.__createIndexFile(mriutil.getNbDirectionsFromDWI(dwi)) outputImage = self.__correctionEddy(dwi, mask, topupBaseName, indexFile, acqpEddy, bVecs, bVals) eddyParameterFiles = self.getImage('dwi', None, 'eddy_parameters') if eddyParameterFiles: self.info("Apply eddy movement correction to gradient encodings directions") bEnc = mriutil.applyGradientCorrection(bEnc, eddyParameterFiles, self.buildName(outputImage, None, 'b')) self.info(mriutil.mrtrixToFslEncoding(outputImage, bEnc, self.buildName(outputImage, None, 'bvecs'), self.buildName(outputImage, None, 'bvals'))) # Proceed with fieldmap if provided if mag and phase and not self.__topupCorrection: # OutputImage is now used for fieldmap correction outputImage = self.__computeFieldmap(outputImage, bVals, mag, phase, norm, parcellationMask, freesurferAnat) self.set('method', 'fieldmap') # Produce a valid b0 and mask for QA b0Corrected = self.buildName(b0, 'corrected') self.info(mriutil.extractFirstB0FromDwi(outputImage, b0Corrected, bVals)) maskCorrected = mriutil.computeDwiMaskFromFreesurfer(b0Corrected, norm, parcellationMask, self.buildName(parcellationMask, 'corrected'), extraArgs) self.rename(outputImage, self.buildName(outputImage, 'corrected'))