Skip to content

Commit

Permalink
ENH: Trap exit and cleanup tmp dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
cookpa committed Apr 20, 2022
1 parent cb7c441 commit 2220305
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
49 changes: 37 additions & 12 deletions run_ashs.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash -e

# cleanup tmp dir
cleanupTmp=1

module load c3d/20191022
module load ashs

Expand Down Expand Up @@ -35,9 +38,6 @@ submitToQueue=0
# Leave at 1 if submitting jobs, otherwise set to num procs
greedyThreads=1

# cleanup tmp dir
cleanup=1

# trim neck
trimNeck=1

Expand Down Expand Up @@ -85,7 +85,7 @@ function help()
Options:
-c : Cleanup working directory (default = $cleanup).
-c : Cleanup working directory (default = $cleanupTmp).
-f : T2w MTL image. This is a specialized image, described in the ASHS documentation as: \"oblique coronal,
oriented along the main axis of the hippocampus. It should have high in-plane resolution (0.4mmx0.4mm)
Expand Down Expand Up @@ -157,7 +157,7 @@ function options()
F) mtlT2wAtlas=$(readlink -m "$OPTARG");;
G) mtlT1wAtlas=$(readlink -m "$OPTARG");;
I) icvAtlas=$(readlink -m "$OPTARG");;
c) cleanup=$OPTARG;;
c) cleanupTmp=$OPTARG;;
f) inputT2w=$(readlink -m "$OPTARG");;
g) inputT1w=$(readlink -m "$OPTARG");;
l) submitToQueue=$OPTARG;;
Expand Down Expand Up @@ -331,6 +331,38 @@ if [[ ! -d "$jobTmpDir" ]]; then
exit 1
fi

# function to clean up tmp and report errors at exit
function cleanup {
EXIT_CODE=$?
LAST_CMD=${BASH_COMMAND}
set +e # disable termination on error

if [[ $cleanupTmp -gt 0 ]]; then
rm -rf ${jobTmpDir}/MTLSeg ${jobTmpDir}/ICVSeg ${jobTmpDir}/neckTrim
rmdir ${jobTmpDir}
else
echo "Leaving working directory ${jobTmpDir}"
fi

if [[ ${EXIT_CODE} -gt 0 ]]; then
echo "
$0 EXITED ON ERROR - PROCESSING MAY BE INCOMPLETE"
echo "
The command \"${LAST_CMD}\" exited with code ${EXIT_CODE}
"
fi

exit $EXIT_CODE
}

trap cleanup EXIT

function sigintCleanup {
exit $?
}

trap sigintCleanup SIGINT

# This is what gets used in the segmentation calls
segT1w=${outputDir}/${inputT1wBasename}

Expand Down Expand Up @@ -364,10 +396,3 @@ echo "Step 2/3: Done!"
echo "Step 3/3: Reorganize output and summarize the result"
Summarize
echo "Step 3/3: Done!"

if [[ $cleanup -gt 0 ]]; then
rm -rf ${jobTmpDir}/MTLSeg ${jobTmpDir}/ICVSeg ${jobTmpDir}/neckTrim
rmdir ${jobTmpDir}
else
echo "Not cleaning up working directory ${jobTmpDir}"
fi
47 changes: 36 additions & 11 deletions run_ashs_icv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ submitToQueue=0
greedyThreads=1

# cleanup tmp dir
cleanup=1
cleanupTmp=1

# trim neck
trimNeck=1
Expand Down Expand Up @@ -61,7 +61,7 @@ function help()
Options:
-c : Cleanup working directory (default = $cleanup).
-c : Cleanup working directory (default = $cleanupTmp).
-l : Parallel execution with LSF (default = $submitToQueue), for faster segmentation of a single session. If
processing many sessions, it's more efficient to run without this option.
Expand Down Expand Up @@ -101,7 +101,7 @@ function options()
while getopts "I:c:g:l:o:t:h" opt; do
case $opt in
I) icvAtlas=$(readlink -m "$OPTARG");;
c) cleanup=$OPTARG;;
c) cleanupTmp=$OPTARG;;
g) inputT1w=$(readlink -m "$OPTARG");;
l) submitToQueue=$OPTARG;;
o) outputDir=$(readlink -m "$OPTARG");;
Expand Down Expand Up @@ -193,6 +193,38 @@ if [[ ! -d "$jobTmpDir" ]]; then
exit 1
fi

# function to clean up tmp and report errors
function cleanup {
EXIT_CODE=$?
LAST_CMD=${BASH_COMMAND}
set +e # disable termination on error

if [[ $cleanupTmp -gt 0 ]]; then
rm -rf ${jobTmpDir}/MTLSeg ${jobTmpDir}/ICVSeg ${jobTmpDir}/neckTrim
rmdir ${jobTmpDir}
else
echo "Leaving working directory ${jobTmpDir}"
fi

if [[ ${EXIT_CODE} -gt 0 ]]; then
echo "
$0 EXITED ON ERROR - PROCESSING MAY BE INCOMPLETE"
echo "
The command \"${LAST_CMD}\" exited with code ${EXIT_CODE}
"
fi

exit $EXIT_CODE
}

trap cleanup EXIT

function sigintCleanup {
exit $?
}

trap sigintCleanup SIGINT

# This is what gets used in the segmentation call
segT1w=${outputDir}/${inputT1wBasename}

Expand All @@ -216,11 +248,4 @@ echo "Step 1/2: Done!"
# reorganize and summarize the result
echo "Step 2/2: Reorganize output and summarize the result"
Summarize
echo "Step 2/2: Done!"

if [[ $cleanup -gt 0 ]]; then
rm -rf ${jobTmpDir}/ICVSeg ${jobTmpDir}/neckTrim
rmdir ${jobTmpDir}
else
echo "Not cleaning up working directory ${jobTmpDir}"
fi
echo "Step 2/2: Done!"

0 comments on commit 2220305

Please sign in to comment.