-
Notifications
You must be signed in to change notification settings - Fork 5
/
batch-dl+direct.sh
executable file
·133 lines (106 loc) · 3.03 KB
/
batch-dl+direct.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
usage() {
cat << EOF
Usage: batch-dl+direct [-h] [-c n_cpu] [-g n_GPU] [-b] [-m model_file] SRC_DIR DST_DIR
Batch processing using DL+DiReCT (with --bet), with N parallel jobs on CPU and GPU.
SRC_DIR is a directory with data to process, each subject should be in a separate subdirectory (with a T1.nii.gz inside).
Results are written to DST_DIR.
optional arguments:
-h|--help show this usage
-t|--t1 name of T1 nifti (default: T1.nii.gz)
-c|--cpu number of parallel CPU jobs (default 8)
-g|--gpu number of parallel GPU jobs (default 1)
-b|--bet Skull-stripping using hd-bet
-m|--model Use given trained model (default v0, and v6 for _ce images)
EOF
exit 0
}
invalid() {
echo "ERROR: Invalid argument $1"
usage 1
}
die() {
echo "ERROR: $1"
exit 1
}
# defaults
T1_FILE=T1.nii.gz
N_PARALLEL_GPU=1
N_PARALLEL_CPU=8
MODEL_ARGS=""
BET_ARGS=""
# Parse arguments
POSITIONAL=()
while [[ $# -gt 0 ]]; do
case "${1}" in
-h|--help) usage 0;;
-t|--t1) shift; T1_FILE=$1 ;;
-g|--gpu) shift; N_PARALLEL_GPU=$1 ;;
-c|--cpu) shift; N_PARALLEL_CPU=$1 ;;
-b|--bet) BET_ARGS=" --bet" ;;
-m|--model) shift; MODEL_ARGS=" --model $1" ;;
-*) invalid "$1" ;;
*) POSITIONAL+=("$1") ;;
esac
shift
done
# Restore positional parameters
set -- "${POSITIONAL[@]}"
if [ $# -lt 2 ] ; then
usage 1
fi
export SRC=$1
export DST=$2
export T1_FILE
export BET_ARGS
export MODEL_ARGS
[[ "`which parallel`X" != "X" ]] || die "'parallel' not found. Install it with 'sudo apt install parallel'"
[[ -d ${SRC} ]] || die "Source directory ${SRC} not found"
export JOB_QUEUE=job_queue.txt
[[ -f ${JOB_QUEUE} ]] && rm ${JOB_QUEUE}
TAIL=tail
[[ "`uname -s`" == "Darwin" ]] && TAIL=gtail
run_dl() {
SUBJ=$1
START=`date +%s`
DIR=${DST}/${SUBJ}
mkdir -p ${DIR}
if [ ! -f ${DIR}/T1w_norm_seg.nii.gz ] ; then
if [ -f ${SRC}/${SUBJ}/T1_INV2.nii.gz ] ; then
# MP2Rage
BET_ARGS=" ${BET_ARGS} --mp2rage-inv2 ${SRC}/${SUBJ}/T1_INV2.nii.gz"
fi
if [[ "${SUBJ}" =~ .*"_ce"$ ]]; then
# if the subject name ends with '_ce', treat as contrast-enhanced (ce) image
MODEL_ARGS=" --model v6"
fi
dl+direct --subject ${SUBJ} ${BET_ARGS} --no-cth --keep ${MODEL_ARGS} ${SRC}/${SUBJ}/${T1_FILE} ${DIR} 2>&1 >> ${DIR}/dl.log
fi
STOP=`date +%s`
DT=$((${STOP}-${START}))
echo "GPU done (dt=${DT}s): ${SUBJ}"
echo ${SUBJ} >> ${JOB_QUEUE}
}
export -f run_dl
run_direct() {
SUBJ=$1
if [ ${SUBJ} == "dummy" ] ; then
# ignore 'dummy' entry
return
fi
START=`date +%s`
DIR=${DST}/${SUBJ}
if [ ! -f ${DIR}/T1w_norm_thickmap.nii.gz ] ; then
direct ${SUBJ} ${DIR} 2>&1 >> ${DIR}/direct.log
fi
STOP=`date +%s`
DT=$((${STOP}-${START}))
echo "CPU done (dt=${DT}s): ${SUBJ}"
}
export -f run_direct
ls ${SRC} | parallel -j ${N_PARALLEL_GPU} run_dl {} &
PID_DL=$!
true > ${JOB_QUEUE}
# create first N_PARALLEL_CPU dummy entries. Otherwise jobs will only start once N_PARALLEL_CPU jobs are queued
for i in `seq 1 ${N_PARALLEL_CPU}` ; do echo dummy >> ${JOB_QUEUE} ; done
${TAIL} -n+0 -f ${JOB_QUEUE} --pid ${PID_DL} | parallel -j ${N_PARALLEL_CPU} run_direct {}