Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No more slice removal for odd number of slices #218

Merged
38 changes: 32 additions & 6 deletions DiffusionPreprocessing/DiffPreprocPipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,22 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
[--b0maxbval=<b0-max-bval>]
Volumes with a bvalue smaller than this value will be
considered as b0s. Defaults to ${DEFAULT_B0_MAX_BVAL}
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
actually running them. --printcom=echo is intended to
be used for testing purposes
[--topup-config-file=<filename>]
File containing the FSL topup configuration.
Defaults to b02b0.cnf in the HCP configuration
directory (as defined by ${HCPPIPEDIR_Config}).
[--select-best-b0]
If set selects the best b0 for each phase encoding direction
to pass on to topup rather than the default behaviour of
using equally spaced b0's throughout the scan. The best b0
is identified as the least distorted (i.e., most similar to
the average b0 after registration).
[--ensure-even-slices]
If set will ensure the input images to FSL's topup and eddy
have an even number of slices by removing one slice if necessary.
This behaviour used to be the default, but is now optional,
because discarding a slice is incompatible with using slice-to-volume correction
in FSL's eddy.
[--extra-eddy-arg=<value>]
Generic single token (no whitespace) argument to pass
to the DiffPreprocPipeline_Eddy.sh script and subsequently
Expand Down Expand Up @@ -181,7 +186,6 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
option can be used to specify which eddy_cuda binary
version to use. If specified, FSLDIR/bin/eddy_cudaX.Y
will be used.

[--combine-data-flag=<value>]
Specified value is passed as the CombineDataFlag value
for the eddy_postproc.sh script.
Expand All @@ -194,6 +198,11 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
acquired
0 - As 1, but also include uncombined single volumes
Defaults to 1
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
actually running them. --printcom=echo is intended to
be used for testing purposes

Return Status Value:

Expand Down Expand Up @@ -233,6 +242,7 @@ EOF
# structural images
# ${b0maxbval} Volumes with a bvalue smaller than this value will
# be considered as b0s
# ${TopupConfig} Filename with topup configuration
# ${runcmd} Set to a user specifed command to use if user has
# requested that commands be echo'd (or printed)
# instead of actually executed. Otherwise, set to
Expand Down Expand Up @@ -269,9 +279,11 @@ get_options() {
DWIName="Diffusion"
DegreesOfFreedom=${DEFAULT_DEGREES_OF_FREEDOM}
b0maxbval=${DEFAULT_B0_MAX_BVAL}
TopupConfig=${HCPPIPEDIR_Config}/b02b0.cnf
runcmd=""
extra_eddy_args=""
SelectBestB0="false"
EnsureEvenSlices="false"
no_gpu="false"
cuda_version=""
CombineDataFlag=1
Expand Down Expand Up @@ -341,6 +353,14 @@ get_options() {
SelectBestB0="true"
index=$((index + 1))
;;
--ensure-even-slices)
EnsureEvenSlices="true"
index=$((index + 1))
;;
--topup-config-file=*)
TopupConfig=${argument#*=}
index=$((index + 1))
;;
--extra-eddy-arg=*)
extra_eddy_arg=${argument#*=}
extra_eddy_args+=" ${extra_eddy_arg} "
Expand Down Expand Up @@ -430,7 +450,9 @@ get_options() {
echo " b0maxbval: ${b0maxbval}"
echo " runcmd: ${runcmd}"
echo " CombineDataFlag: ${CombineDataFlag}"
echo " TopupConfig: ${TopupConfig}"
echo " SelectBestB0: ${SelectBestB0}"
echo " EnsureEvenSlices: ${EnsureEvenSlices}"
echo " extra_eddy_args: ${extra_eddy_args}"
echo " no_gpu: ${no_gpu}"
echo " cuda-version: ${cuda_version}"
Expand Down Expand Up @@ -511,10 +533,14 @@ main() {
pre_eddy_cmd+=" --negData=${NegInputImages} "
pre_eddy_cmd+=" --echospacing=${echospacing} "
pre_eddy_cmd+=" --b0maxbval=${b0maxbval} "
pre_eddy_cmd+=" --topup-config-file=${TopupConfig} "
pre_eddy_cmd+=" --printcom=${runcmd} "
if [ "${SelectBestB0}" == "true" ]; then
pre_eddy_cmd+=" --select-best-b0 "
fi
if [ "${EnsureEvenSlices}" == "true" ]; then
pre_eddy_cmd+=" --ensure-even-slices "
fi

log_Msg "pre_eddy_cmd: ${pre_eddy_cmd}"
${pre_eddy_cmd}
Expand Down
10 changes: 5 additions & 5 deletions DiffusionPreprocessing/DiffPreprocPipeline_Eddy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,6 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
--subject=<subject-id> subject ID
[--dwiname=<DWIName>] name to give DWI output directories
Defaults to Diffusion
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
actually running them. --printcom=echo is intended to
be used for testing purposes
[--dont_peas] pass the --dont_peas (Do NOT perform a post-eddy
alignment of shells) option to the eddy invocation
[--fwhm=<value>] --fwhm value to pass to the eddy binary
Expand All @@ -149,6 +144,11 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
option can be used to specify which eddy_cuda binary
version to use. If specified, FSLDIR/bin/eddy_cudaX.Y
will be used.
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
actually running them. --printcom=echo is intended to
be used for testing purposes

Return Status Value:

Expand Down
10 changes: 5 additions & 5 deletions DiffusionPreprocessing/DiffPreprocPipeline_PostEddy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
[--dof=<Degrees of Freedom>]
Degrees of Freedom for registration to structural
images. Defaults to ${DEFAULT_DEGREES_OF_FREEDOM}
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
actually running them. --printcom=echo is intended to
be used for testing purposes
[--select_best_b0]
Whether the --select-best-b0 flag was used in
DiffPreprocPipeline_PreEddy.
Expand All @@ -108,6 +103,11 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
acquired
0 - As 1, but also include uncombined single volumes
Defaults to 1
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
actually running them. --printcom=echo is intended to
be used for testing purposes

Return Status Value:

Expand Down
63 changes: 62 additions & 1 deletion DiffusionPreprocessing/DiffPreprocPipeline_PreEddy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
data_AP1@data_AP2@...data_APn
--echospacing=<echo-spacing>
Effective Echo Spacing in msecs
--topup-config-file=<filename>
File containing the FSL topup configuration.
[--dwiname=<DWIname>] name to give DWI output directories.
Defaults to Diffusion
[--b0maxbval=<b0-max-bval>]
Expand All @@ -103,6 +105,12 @@ PARAMETERs are: [ ] = optional; < > = user supplied value
using equally spaced b0's throughout the scan. The best b0
is identified as the least distorted (i.e., most similar to
the average b0 after registration).
[--ensure-even-slices]
If set will ensure the input images to FSL's topup and eddy
have an even number of slices by removing one slice if necessary.
This behaviour used to be the default, but is now optional,
because discarding a slice is incompatible with using slice-to-volume correction
in FSL's eddy.
[--printcom=<print-command>]
Use the specified <print-command> to echo or otherwise
output the commands that would be executed instead of
Expand Down Expand Up @@ -138,8 +146,12 @@ EOF
# encoding direction
# ${echospacing} Echo spacing in msecs
# ${DWIName} Name to give DWI output directories
# ${TopupConfig} Filename with topup configuration
# ${b0maxbval} Volumes with a bvalue smaller than this value will
# be considered as b0s
# ${SelectBestB0} If "true" will select the least distorted B0 for topup
# ${EnsureEvenSlices} If "true" will ensure input images to topup/eddy have
# even number of slices
# ${runcmd} Set to a user specified command to use if user has
# requested that commands be echo'd (or printed)
# instead of actually executed. Otherwise, set to
Expand All @@ -150,6 +162,10 @@ EOF
# Support Functions
# --------------------------------------------------------------------------------

isodd() {
echo "$(($1 % 2))"
}

get_options() {
local arguments=($@)

Expand All @@ -161,10 +177,12 @@ get_options() {
unset NegInputImages
unset echospacing
unset SelectBestB0
unset TopupConfig
DWIName="Diffusion"
b0maxbval=${DEFAULT_B0_MAX_BVAL}
runcmd=""
SelectBestB0="false"
EnsureEvenSlices="false"

# parse arguments
local index=0
Expand Down Expand Up @@ -215,6 +233,10 @@ get_options() {
b0maxbval=${argument#*=}
index=$((index + 1))
;;
--topup-config-file=*)
TopupConfig=${argument#*=}
index=$((index + 1))
;;
--printcom=*)
runcmd=${argument#*=}
index=$((index + 1))
Expand All @@ -223,6 +245,10 @@ get_options() {
SelectBestB0="true"
index=$((index + 1))
;;
--ensure-even-slices)
EnsureEvenSlices="true"
index=$((index + 1))
;;
*)
show_usage
echo "ERROR: Unrecognized Option: ${argument}"
Expand Down Expand Up @@ -266,6 +292,10 @@ get_options() {
error_msgs+="\nERROR: <DWIName> not specified"
fi

if [ -z ${TopupConfig} ]; then
error_msgs+="\nERROR: <TopupConfig> not specified"
fi

if [ ! -z "${error_msgs}" ]; then
show_usage
echo -e ${error_msgs}
Expand Down Expand Up @@ -444,6 +474,18 @@ main() {
exit 1
fi

# if the number of slices are odd, check that the user has a way to deal with that
if [ "${EnsureEvenSlices}" == "false" ] && [ "${TopupConfig}" == "${HCPPIPEDIR_Config}/b02b0.cnf" ] ; then
dimz=$(${FSLDIR}/bin/fslval ${topupdir}/Pos_b0 dim3)
if [ $(isodd $dimz) -eq 1 ]; then
log_Msg "Input images have an odd number of slices. This is incompatible with the default topup configuration file."
log_Msg "Either supply a topup configuration file that doesn't use subsampling (e.g., FSL's 'b02b0_1.cnf') using the --topup-config-file=<file> flag (recommended)"
log_Msg "or instruct the HCP pipelines to remove a slice using the --ensure-even-slices flag (legacy option)."
log_Msg "Note that the legacy option is incompatible with slice-to-volume correction in FSL's eddy"
exit 1
fi
fi

# Create two files for each phase encoding direction, that for each series contain the number of
# corresponding volumes and the number of actual volumes. The file e.g. Pos_SeriesCorrespVolNum.txt
# will contain as many rows as non-EMPTY series. The entry M in row J indicates that volumes 0-M
Expand Down Expand Up @@ -490,8 +532,27 @@ main() {
${runcmd} ${HCPPIPEDIR_dMRI}/basic_preproc_sequence.sh ${outdir} ${ro_time} ${PEdir} ${b0dist} ${b0maxbval}
fi

if [ "${EnsureEvenSlices}" == "true" ]; then
topupdir=${outdir}/topup
eddydir=${outdir}/eddy
dimz=$(${FSLDIR}/bin/fslval ${topupdir}/Pos_b0 dim3)
if [ $(isodd $dimz) -eq 1 ]; then
echo "Removing one slice from data to get even number of slices"
for filename in Pos_Neg_b0 Pos_b0 Neg_b0 ; do
${FSLDIR}/bin/fslroi ${topupdir}/${filename} ${topupdir}/${filename}_tmp 0 -1 0 -1 1 -1
${FSLDIR}/bin/imrm ${topupdir}/${filename}
${FSLDIR}/bin/immv ${topupdir}/${filename}_tmp ${topupdir}/${filename}
done
${FSLDIR}/bin/fslroi ${eddydir}/Pos_Neg ${eddydir}/Pos_Neg_tmp 0 -1 0 -1 1 -1
${FSLDIR}/bin/imrm ${eddydir}/Pos_Neg
${FSLDIR}/bin/immv ${eddydir}/Pos_Neg_tmp ${eddydir}/Pos_Neg
else
echo "Skipping slice removal, because data already has an even number of slices"
fi
fi

log_Msg "Running Topup"
${runcmd} ${HCPPIPEDIR_dMRI}/run_topup.sh ${outdir}/topup
${runcmd} ${HCPPIPEDIR_dMRI}/run_topup.sh ${outdir}/topup ${TopupConfig}

log_Msg "Completed!"
exit 0
Expand Down
23 changes: 1 addition & 22 deletions DiffusionPreprocessing/scripts/basic_preproc_best_b0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ echo "${scriptName}: Input Parameter: ro_time: ${ro_time}" # Readout time in sec
echo "${scriptName}: Input Parameter: PEdir: ${PEdir}"
echo "${scriptName}: Input Parameter: b0maxbval: ${b0maxbval}"

isodd() {
echo "$(($1 % 2))"
}

rawdir=${workingdir}/rawdata
topupdir=${workingdir}/topup
eddydir=${workingdir}/eddy
Expand Down Expand Up @@ -223,7 +219,7 @@ elif [ ${PEdir} -eq 2 ]; then #AP/PA phase encoding
fi

################################################################################################
## Merging Files and correct number of slices
## Merging Files
################################################################################################
echo "Merging Pos and Neg images"
${FSLDIR}/bin/fslmerge -t ${rawdir}/Pos $(echo ${rawdir}/${basePos}_[0-9]*.nii*)
Expand All @@ -243,23 +239,6 @@ for idx in $(seq 1 $(${FSLDIR}/bin/fslval ${rawdir}/Neg dim4)); do
echo 2 >>${rawdir}/index.txt
done

dimz=$(${FSLDIR}/bin/fslval ${rawdir}/Pos dim3)
if [ $(isodd $dimz) -eq 1 ]; then
echo "Remove one slice from data to get even number of slices"
${FSLDIR}/bin/fslroi ${rawdir}/Pos ${rawdir}/Posn 0 -1 0 -1 1 -1
${FSLDIR}/bin/fslroi ${rawdir}/Neg ${rawdir}/Negn 0 -1 0 -1 1 -1
${FSLDIR}/bin/fslroi ${rawdir}/best_Pos_b0 ${rawdir}/best_Pos_b0n 0 -1 0 -1 1 -1
${FSLDIR}/bin/fslroi ${rawdir}/best_Neg_b0 ${rawdir}/best_Neg_b0n 0 -1 0 -1 1 -1
${FSLDIR}/bin/imrm ${rawdir}/Pos
${FSLDIR}/bin/imrm ${rawdir}/Neg
${FSLDIR}/bin/imrm ${rawdir}/best_Pos_b0
${FSLDIR}/bin/imrm ${rawdir}/best_Neg_b0
${FSLDIR}/bin/immv ${rawdir}/Posn ${rawdir}/Pos
${FSLDIR}/bin/immv ${rawdir}/Negn ${rawdir}/Neg
${FSLDIR}/bin/immv ${rawdir}/best_Pos_b0n ${rawdir}/best_Pos_b0
${FSLDIR}/bin/immv ${rawdir}/best_Neg_b0n ${rawdir}/best_Neg_b0
fi

echo "Perform final merge"
${FSLDIR}/bin/fslmerge -t ${rawdir}/Pos_Neg_b0 ${rawdir}/best_Pos_b0 ${rawdir}/best_Neg_b0
# include Pos_b0 as the first volume of Pos_Neg, so that eddy will use it as reference
Expand Down
22 changes: 1 addition & 21 deletions DiffusionPreprocessing/scripts/basic_preproc_sequence.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ echo "${scriptName}: Input Parameter: ro_time: ${ro_time}" # Readout time in sec
echo "${scriptName}: Input Parameter: PEdir: ${PEdir}"
echo "${scriptName}: Input Parameter: b0dist: ${b0dist}"
echo "${scriptName}: Input Parameter: b0maxbval: ${b0maxbval}"
isodd() {
echo "$(($1 % 2))"
}

rawdir=${workingdir}/rawdata
topupdir=${workingdir}/topup
Expand Down Expand Up @@ -138,7 +135,7 @@ for entry in ${rawdir}/${baseNeg}_[0-9]*.nii*; do #For each Neg volume
done

################################################################################################
## Merging Files and correct number of slices
## Merging Files
################################################################################################
echo "Merging Pos and Neg images"
${FSLDIR}/bin/fslmerge -t ${rawdir}/Pos_b0 $(${FSLDIR}/bin/imglob ${rawdir}/Pos_b0_????.*)
Expand All @@ -153,23 +150,6 @@ paste $(echo ${rawdir}/${basePos}_[0-9]*.bvec) >${rawdir}/Pos.bvec
paste $(echo ${rawdir}/${baseNeg}_[0-9]*.bval) >${rawdir}/Neg.bval
paste $(echo ${rawdir}/${baseNeg}_[0-9]*.bvec) >${rawdir}/Neg.bvec

dimz=$(${FSLDIR}/bin/fslval ${rawdir}/Pos dim3)
if [ $(isodd $dimz) -eq 1 ]; then
echo "Remove one slice from data to get even number of slices"
${FSLDIR}/bin/fslroi ${rawdir}/Pos ${rawdir}/Posn 0 -1 0 -1 1 -1
${FSLDIR}/bin/fslroi ${rawdir}/Neg ${rawdir}/Negn 0 -1 0 -1 1 -1
${FSLDIR}/bin/fslroi ${rawdir}/Pos_b0 ${rawdir}/Pos_b0n 0 -1 0 -1 1 -1
${FSLDIR}/bin/fslroi ${rawdir}/Neg_b0 ${rawdir}/Neg_b0n 0 -1 0 -1 1 -1
${FSLDIR}/bin/imrm ${rawdir}/Pos
${FSLDIR}/bin/imrm ${rawdir}/Neg
${FSLDIR}/bin/imrm ${rawdir}/Pos_b0
${FSLDIR}/bin/imrm ${rawdir}/Neg_b0
${FSLDIR}/bin/immv ${rawdir}/Posn ${rawdir}/Pos
${FSLDIR}/bin/immv ${rawdir}/Negn ${rawdir}/Neg
${FSLDIR}/bin/immv ${rawdir}/Pos_b0n ${rawdir}/Pos_b0
${FSLDIR}/bin/immv ${rawdir}/Neg_b0n ${rawdir}/Neg_b0
fi

echo "Perform final merge"
${FSLDIR}/bin/fslmerge -t ${rawdir}/Pos_Neg_b0 ${rawdir}/Pos_b0 ${rawdir}/Neg_b0
${FSLDIR}/bin/fslmerge -t ${rawdir}/Pos_Neg ${rawdir}/Pos ${rawdir}/Neg
Expand Down
5 changes: 1 addition & 4 deletions DiffusionPreprocessing/scripts/run_topup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ source "${HCPPIPEDIR}/global/scripts/debug.shlib" "$@" # Debugging functions; al
echo -e "\n START: run_topup"

workingdir=$1

configdir=${HCPPIPEDIR_Config}
#topup_config_file=${FSLDIR}/etc/flirtsch/b02b0.cnf
topup_config_file=${configdir}/b02b0.cnf
topup_config_file=$2

${FSLDIR}/bin/topup --imain=${workingdir}/Pos_Neg_b0 --datain=${workingdir}/acqparams.txt --config=${topup_config_file} --out=${workingdir}/topup_Pos_Neg_b0 -v --fout=${workingdir}/topup_Pos_Neg_b0_field.nii.gz

Expand Down