From 43b290a7855b10caa2361e51e3cbadc113d13c8d Mon Sep 17 00:00:00 2001 From: Michiel Cottaar Date: Thu, 17 Oct 2019 15:31:09 +0100 Subject: [PATCH] REF: compute fov_mask in eddy_postproc rather than Diffusion2Structural --- .../scripts/DiffusionToStructural.sh | 10 ++++---- .../scripts/eddy_postproc.sh | 24 +++++++++++++++++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/DiffusionPreprocessing/scripts/DiffusionToStructural.sh b/DiffusionPreprocessing/scripts/DiffusionToStructural.sh index bba8c924d..a7b1b3114 100755 --- a/DiffusionPreprocessing/scripts/DiffusionToStructural.sh +++ b/DiffusionPreprocessing/scripts/DiffusionToStructural.sh @@ -107,21 +107,19 @@ if [ ${GdcorrectionFlag} -eq 1 ]; then ${CARET7DIR}/wb_command -volume-dilate $DataDirectory/warped/data_warped.nii.gz $DilateDistance NEAREST $DataDirectory/warped/data_warped_dilated.nii.gz ${FSLDIR}/bin/applywarp --rel -i "$DataDirectory"/warped/data_warped_dilated -r "$T1wRestoreImage"_${DiffRes} -w "$WorkingDirectory"/grad_unwarp_diff2str --interp=spline -o "$T1wOutputDirectory"/data - #Create a mask representing voxels within the field of view for all volumes prior to dilation - ${FSLDIR}/bin/fslmaths "$DataDirectory"/warped/data_warped -abs -Tmin -bin -fillh "$DataDirectory"/warped/fov_mask + # Transforms field of view mask to T1-weighted space ${FSLDIR}/bin/applywarp --rel -i "$DataDirectory"/warped/fov_mask -r "$T1wRestoreImage"_${DiffRes} -w "$WorkingDirectory"/grad_unwarp_diff2str --interp=trilinear -o "$T1wOutputDirectory"/fov_mask - #Now register the grad_dev tensor + # Now register the grad_dev tensor ${FSLDIR}/bin/vecreg -i "$DataDirectory"/grad_dev -o "$T1wOutputDirectory"/grad_dev -r "$T1wRestoreImage"_${DiffRes} -t "$WorkingDirectory"/diff2str.mat --interp=spline ${FSLDIR}/bin/fslmaths "$T1wOutputDirectory"/grad_dev -mas "$T1wOutputDirectory"/nodif_brain_mask_temp "$T1wOutputDirectory"/grad_dev #Mask-out values outside the brain else # Dilation outside of the field of view to minimise the effect of the hard field of view edge on the interpolation ${CARET7DIR}/wb_command -volume-dilate $DataDirectory/data.nii.gz $DilateDistance NEAREST $DataDirectory/data_dilated.nii.gz - #Register diffusion data to T1w space without considering gradient nonlinearities + # Register diffusion data to T1w space without considering gradient nonlinearities ${FSLDIR}/bin/flirt -in "$DataDirectory"/data_dilated -ref "$T1wRestoreImage"_${DiffRes} -applyxfm -init "$WorkingDirectory"/diff2str.mat -interp spline -out "$T1wOutputDirectory"/data - #Create a mask representing voxels within the field of view for all volumes prior to dilation - ${FSLDIR}/bin/fslmaths "$DataDirectory"/data -abs -Tmin -bin -fillh "$DataDirectory"/fov_mask + # Transforms field of view mask to T1-weighted space ${FSLDIR}/bin/flirt -in "$DataDirectory"/fov_mask -ref "$T1wRestoreImage"_${DiffRes} -applyxfm -init "$WorkingDirectory"/diff2str.mat -interp trilinear -out "$T1wOutputDirectory"/fov_mask fi diff --git a/DiffusionPreprocessing/scripts/eddy_postproc.sh b/DiffusionPreprocessing/scripts/eddy_postproc.sh index a553d529a..158df46df 100755 --- a/DiffusionPreprocessing/scripts/eddy_postproc.sh +++ b/DiffusionPreprocessing/scripts/eddy_postproc.sh @@ -87,13 +87,31 @@ else fi #fi + +#Create a mask representing voxels within the field of view for all volumes prior to dilation +${FSLDIR}/bin/fslmaths ${datadir}/data -abs -Tmin -bin -fillh ${datadir}/fov_mask + if [ ! $GdCoeffs = "NONE" ] ; then echo "Correcting for gradient nonlinearities" # Note: "data_warped" is eddy-current and suspectibility distortion corrected (via 'eddy'), but prior to gradient distortion correction # i.e., "data_posteddy_preGDC" would be another way to think of it ${FSLDIR}/bin/immv ${datadir}/data ${datadir}/data_warped - ${globalscriptsdir}/GradientDistortionUnwarp.sh --workingdir="${datadir}" --coeffs="${GdCoeffs}" --in="${datadir}/data_warped" --out="${datadir}/data" --owarp="${datadir}/fullWarp" + + # Dilation outside of the field of view to minimise the effect of the hard field of view edge on the interpolation + DiffRes=`${FSLDIR}/bin/fslval ${datadir}/data pixdim1` + DilateDistance=`echo "$DiffRes * 4" | bc` # Extrapolates the diffusion data up to 4 voxels outside of the FOV + ${CARET7DIR}/wb_command -volume-dilate ${datadir}/warped/data_warped.nii.gz $DilateDistance NEAREST ${datadir}/warped/data_warped_dilated.nii.gz + + # apply gradient distortion correction + ${globalscriptsdir}/GradientDistortionUnwarp.sh --workingdir="${datadir}" --coeffs="${GdCoeffs}" --in="${datadir}/data_warped_dilated" --out="${datadir}/data" --owarp="${datadir}/fullWarp" + + # Transform field of view mask (using conservative trilinear interolation with high threshold) + ${FSLDIR}/bin/immv ${datadir}/fov_mask ${datadir}/fov_mask_warped + ${FSLDIR}/bin/applywarp --rel --interp=trilinear -i ${datadir}/fov_mask_warped -r ${datadir}/fov_mask_warped -w ${datadir}/fullWarp -o ${datadir}/fov_mask + ${FSLDIR}/bin/fslmaths ${datadir}/fov_mask -thr 0.999 -bin ${datadir}/fov_mask + # mask out any data outside the field of view + ${FSLDIR}/bin/fslmaths ${datadir}/data -mas ${datadir}/fov_mask ${datadir}/data echo "Computing gradient coil tensor to correct for gradient nonlinearities" ${FSLDIR}/bin/calc_grad_perc_dev --fullwarp=${datadir}/fullWarp -o ${datadir}/grad_dev @@ -102,10 +120,12 @@ if [ ! $GdCoeffs = "NONE" ] ; then ${FSLDIR}/bin/imrm ${datadir}/grad_dev_? ${FSLDIR}/bin/imrm ${datadir}/trilinear ${FSLDIR}/bin/imrm ${datadir}/data_warped_vol1 - + ${FSLDIR}/bin/imrm ${datadir}/data_warped_dilated + #Keep the original warped data and warp fields mkdir -p ${datadir}/warped ${FSLDIR}/bin/immv ${datadir}/data_warped ${datadir}/warped + ${FSLDIR}/bin/immv ${datadir}/fov_mask_warped ${datadir}/warped ${FSLDIR}/bin/immv ${datadir}/fullWarp ${datadir}/warped ${FSLDIR}/bin/immv ${datadir}/fullWarp_abs ${datadir}/warped fi