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

fMRIVolume Related Pull Request — Use of a specific BOLD mask #141

Closed
42 changes: 41 additions & 1 deletion fMRIVolume/GenericfMRIVolumeProcessingPipeline.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fi

source $HCPPIPEDIR/global/scripts/log.shlib # Logging related functions
source $HCPPIPEDIR/global/scripts/opts.shlib # Command line option functions
source ${HCPPIPEDIR}/global/scripts/processingmodecheck.shlib # Check processing mode requirements


################################################ SUPPORT FUNCTIONS ##################################################

Expand Down Expand Up @@ -220,6 +222,43 @@ then
log_Err_Abort "the --usejacobian option must be 'true' or 'false'"
fi


# ------------------------------------------------------------------------------
# Legacy Style Data Options
# ------------------------------------------------------------------------------

BOLDMask=`opts_GetOpt1 "--boldmask" $@` # Specifies what mask to use for the final bold:
# T1_fMRI_FOV: combined T1w brain mask and fMRI FOV masks (the default),
# T1_DILATED_fMRI_FOV: a once dilated T1w brain based mask combined with fMRI FOV
# T1_DILATED2x_fMRI_FOV: a twice dilated T1w brain based mask combined with fMRI FOV,
# fMRI_FOV: a fMRI FOV mask

# Defaults
BOLDMask=`opts_DefaultOpt $BOLDMask "T1_fMRI_FOV"`

# ------------------------------------------------------------------------------
# Compliance check
# ------------------------------------------------------------------------------

ProcessingMode=`opts_GetOpt1 "--processing-mode" $@`
ProcessingMode=`opts_DefaultOpt $ProcessingMode "HCPStyleData"`
Compliance="HCPStyleData"
ComplianceMsg=""

if [ "${BOLDMask}" != 'T1_fMRI_FOV' ]; then
if [ "${BOLDMask}" != "T1_DILATED_fMRI_FOV" ] && [ "${BOLDMask}" != "T1_DILATED2x_fMRI_FOV" ] && [ "${BOLDMask}" != "fMRI_FOV" ] ; then
log_Err_Abort "--boldmask=${BOLDMask} is invalid! Valid options are: T1_fMRI_FOV (default), T1_DILATED_fMRI_FOV, T1_DILATED2x_fMRI_FOV, fMRI_FOV."
fi
ComplianceMsg+=" --boldmask=${BOLDMask}"
Compliance="LegacyStyleData"
fi

check_mode_compliance "${ProcessingMode}" "${Compliance}" "${ComplianceMsg}"

# -- End compliance check



# Setup PATHS
PipelineScripts=${HCPPIPEDIR_fMRIVol}
GlobalScripts=${HCPPIPEDIR_Global}
Expand Down Expand Up @@ -461,7 +500,8 @@ ${RUN} ${PipelineScripts}/IntensityNormalization.sh \
--ofmri=${fMRIFolder}/${NameOffMRI}_nonlin_norm \
--inscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin \
--oscout=${fMRIFolder}/${NameOffMRI}_SBRef_nonlin_norm \
--usejacobian=${UseJacobian}
--usejacobian=${UseJacobian} \
--boldmask=${BOLDMask}

#Copy selected files to ResultsFolder
${RUN} cp ${fMRIFolder}/${NameOffMRI}_nonlin_norm.nii.gz ${ResultsFolder}/${NameOffMRI}.nii.gz
Expand Down
92 changes: 72 additions & 20 deletions fMRIVolume/scripts/IntensityNormalization.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
#!/bin/bash
set -e
source ${HCPPIPEDIR}/global/scripts/debug.shlib # Debugging functions

# Intensity normalisation, and bias field correction, and optional Jacobian modulation, applied to fMRI images (all inputs must be in fMRI space)

Expand Down Expand Up @@ -35,18 +36,23 @@ Usage() {
echo " [--inscout=<input name for scout image (pre-sat EPI)>]"
echo " [--oscout=<output name for normalized scout image>]"
echo " [--workingdir=<working dir>]"
echo " [--boldmask=<what mask to use for generating final BOLD timeseries:"
echo " T1_fMRI_FOV (default): mask based on T1 and voxels available at all timepoints (i.e., the fMRI FOV)"
echo " T1_DILATED_fMRI_FOV: a once dilated T1w brain based mask combined with fMRI FOV"
echo " T1_DILATED2x_fMRI_FOV: a twice dilated T1w brain based mask combined with fMRI FOV,"
echo " fMRI_FOV: a fMRI FOV mask"
}

# function for parsing options
getopt1() {
sopt="$1"
shift 1
for fn in $@ ; do
if [ `echo $fn | grep -- "^${sopt}=" | wc -w` -gt 0 ] ; then
echo $fn | sed "s/^${sopt}=//"
# if [ ] ; then Usage ; echo " " ; echo "Error:: option ${sopt} requires an argument"; exit 1 ; end
return 0
fi
if [ `echo $fn | grep -- "^${sopt}=" | wc -w` -gt 0 ] ; then
echo $fn | sed "s/^${sopt}=//"
# if [ ] ; then Usage ; echo " " ; echo "Error:: option ${sopt} requires an argument"; exit 1 ; end
return 0
fi
done
}

Expand Down Expand Up @@ -75,10 +81,27 @@ OutputfMRI=`getopt1 "--ofmri" $@` # "$5"
ScoutInput=`getopt1 "--inscout" $@` # "$6"
ScoutOutput=`getopt1 "--oscout" $@` # "$7"
UseJacobian=`getopt1 "--usejacobian" $@` #
BOLDMask=`getopt1 "--boldmask" $@`

verbose_red_echo "---> Intensity normalization"

verbose_echo " "
verbose_echo " Using parameters ..."
verbose_echo " --infmri: ${InputfMRI}"
verbose_echo " --biasfield: ${BiasField}"
verbose_echo " --jacobian: ${Jacobian}"
verbose_echo " --brainmask: ${BrainMask}"
verbose_echo " --ofmri: ${OutputfMRI}"
verbose_echo " --inscout: ${ScoutInput}"
verbose_echo " --oscout: ${ScoutOutput}"
verbose_echo " --usejacobian: ${UseJacobian}"
verbose_echo " --boldmask: ${BOLDMask}"
verbose_echo " "

# default parameters
OutputfMRI=`$FSLDIR/bin/remove_ext $OutputfMRI`
WD=`defaultopt $WD ${OutputfMRI}.wdir`
BOLDMask=`defaultopt $BOLDMask "T1_fMRI_FOV"`

#sanity check the jacobian option
if [[ "$UseJacobian" != "true" && "$UseJacobian" != "false" ]]
Expand All @@ -89,7 +112,7 @@ fi

jacobiancom=""
if [[ $UseJacobian == "true" ]] ; then
jacobiancom="-mul $Jacobian"
jacobiancom="-mul $Jacobian"
fi

biascom=""
Expand All @@ -99,10 +122,10 @@ then
fi

# sanity checking
if [ X${ScoutInput} != X ] ; then
if [ X${ScoutInput} != X ] ; then
if [ X${ScoutOutput} = X ] ; then
echo "Error: Must supply an output name for the normalised scout image"
exit 1
echo "Error: Must supply an output name for the normalised scout image"
exit 1
fi
fi

Expand All @@ -117,20 +140,49 @@ echo "PWD = `pwd`" >> $WD/log.txt
echo "date: `date`" >> $WD/log.txt
echo " " >> $WD/log.txt

########################################## DO WORK ##########################################

########################################## DO WORK ##########################################

# FinalMask is a combination of the FS-derived brainmask, and the spatial coverage mask that captures the
# voxels that have data available at *ALL* time points (derived in OneStepResampling)
FinalMask=`${FSLDIR}/bin/remove_ext ${InputfMRI}`_finalmask
${FSLDIR}/bin/fslmaths ${BrainMask} -bin -mas ${InputfMRI}_mask ${FinalMask}
T1FOVMask=`${FSLDIR}/bin/remove_ext ${InputfMRI}`_T1FOVmask
${FSLDIR}/bin/fslmaths ${BrainMask} -bin -mas ${InputfMRI}_mask ${T1FOVMask}

# Use the requested BOLD mask

if [ "${BOLDMask}" = "T1_fMRI_FOV" ] ; then
# FinalMask is a combination of the FS-derived brainmask, and the spatial coverage mask that captures the
# voxels that have data available at *ALL* time points (derived in OneStepResampling)
${FSLDIR}/bin/imcp ${T1FOVMask} ${FinalMask}

elif [ "${BOLDMask}" = "T1_DILATED_fMRI_FOV" ] ; then
# FinalMask is a combination of once dilated FS-derived brainmask, and the spatial coverage mask that captures the
# voxels that have data available at *ALL* time points (derived in OneStepResampling)
${FSLDIR}/bin/fslmaths ${BrainMask} -bin -dilF -mas ${InputfMRI}_mask ${FinalMask}

elif [ "${BOLDMask}" = "T1_DILATED2x_fMRI_FOV" ] ; then
# FinalMask is a combination of twice dilated FS-derived brainmask, and the spatial coverage mask that captures the
# voxels that have data available at *ALL* time points (derived in OneStepResampling)
${FSLDIR}/bin/fslmaths ${BrainMask} -bin -dilF -dilF -mas ${InputfMRI}_mask ${FinalMask}

elif [ "${BOLDMask}" = "fMRI_FOV" ] ; then
# FinalMask is the spatial coverage mask that captures the
# voxels that have data available at *ALL* time points (derived in OneStepResampling)
${FSLDIR}/bin/imcp ${InputfMRI}_mask ${FinalMask}

elif
# No valid BOLDMask option was specified
log_Err_Abort "Invalid entry for specified BOLD mask (--boldmask=${BOLDMask})"
fi

# Create a simple summary text file of the percentage of spatial coverage of the fMRI data inside the FS-derived brain mask

# Create a simple summary text file of the percentage of spatial coverage of the fMRI data inside the FS-derived brain mask and the actual mask used
NvoxBrainMask=`fslstats ${BrainMask} -V | awk '{print $1}'`
NvoxT1FOVMask=`fslstats ${T1FOVMask} -V | awk '{print $1}'`
NvoxFinalMask=`fslstats ${FinalMask} -V | awk '{print $1}'`
PctCoverage=`echo "scale=4; 100 * ${NvoxFinalMask} / ${NvoxBrainMask}" | bc -l`
echo "PctCoverage, NvoxFinalMask, NvoxBrainMask" >| ${FinalMask}.stats.txt
echo "${PctCoverage}, ${NvoxFinalMask}, ${NvoxBrainMask}" >> ${FinalMask}.stats.txt
PctBrainCoverage=`echo "scale=4; 100 * ${NvoxT1FOVMask} / ${NvoxBrainMask}" | bc -l`
PctMaskCoverage=`echo "scale=4; 100 * ${NvoxFinalMask} / ${NvoxT1FOVMask}" | bc -l`
mharms marked this conversation as resolved.
Show resolved Hide resolved
echo "BOLDMask, PctBrainCoverage, PctMaskCoverage, NvoxT1FOVMask, NvoxBrainMask, NvoxFinalMask" >| ${FinalMask}.stats.txt
echo "${BOLDMask}, ${PctBrainCoverage}, ${PctMaskCoverage}, ${NvoxT1FOVMask}, ${NvoxBrainMask}, ${NvoxFinalMask}" >> ${FinalMask}.stats.txt


# Run intensity normalisation, with bias field correction and optional jacobian modulation,
# for the main fmri timeseries and the scout images (pre-saturation images)
Expand All @@ -146,7 +198,7 @@ echo " "
echo "END: IntensityNormalization"
echo " END: `date`" >> $WD/log.txt

########################################## QA STUFF ##########################################
########################################## QA STUFF ##########################################

if [ -e $WD/qa.txt ] ; then rm -f $WD/qa.txt ; fi
echo "cd `pwd`" >> $WD/qa.txt
Expand Down