3 ###############################################################################
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
8 # Helper file with many functions used in the midP scripts.
10 ###############################################################################
13 # check return value passed and abort if it represents an error (ie, ret != 0)
14 # optionally, a function can be passed as a 3rd parameter, to be called just
15 # before exiting. this is useful for cleaning up, for example.
20 echo Aborted at $1 with code $2
30 # wait for all processes in the list and return their exit codes
31 # in the ret_codes variable.
33 # OBS: this function must always be called in the same shell
34 # that launched the processes.
44 wait $p > /dev/null 2> /dev/null
46 if [ ret != 127 ]; then
58 # clean-up functions for maks, registration, and midp
70 clean_up_registration()
78 # block execution untill the number of threads (jobs) launched by the
79 # current process is below the given number of threads.
84 while [[ $(jobs -p | wc -l) -ge $nbth ]]; do
91 # receive a 4D file and and extract the corresponding phase numbers
92 # export the variables containing each of the extracted data
94 extract_4d_phase_numbers()
98 nb_phases=${#phase_files[@]}
100 # get everything except numbers and punctuation
101 cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[^0-9[:punct:]]*" | sort -u > /tmp/patterns.txt
103 # find which patterns have the phases connected to it
104 patterns=`cat /tmp/patterns.txt`
105 if [ -z "$patterns" ]; then
106 phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep "[0-9]\+"` )
108 for i in $patterns; do
110 # check if the pattern appears before the phase number
111 nb_phases_found=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | sort -u | wc -l`
112 if [ $nb_phases_found == $nb_phases ]; then
113 # keep only what identifies the phase number
114 phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "$i[0-9[:punct:]]\+" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` )
118 # check if the pattern appears after the phase number
119 nb_phases_found=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | sort -u | wc -l`
120 if [ $nb_phases_found == $nb_phases ]; then
121 # keep only what identifies the phase number
122 phase_nbs=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | sed 's/.mhd//' | grep -o "[0-9[:punct:]]\+$i" | grep -o "[^${i}]\+" | grep -o "[0-9]\+[[:punct:]]*[0-9]*" | grep -o "[0-9]*[[:punct:]]*[0-9]\+"` )
129 echo "Phase numbers are ${phase_nbs[@]}"
134 # receive a 4D file and extract the corresponding phase files,
136 # export the variables containing each of the extracted data
142 echo "4D file is $mhd4d"
144 # array of phase files
145 phase_files=( `cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:'` )
146 echo "Phase files are ${phase_files[@]}"
148 extract_4d_phase_numbers $mhd4d
153 # receive a 4D file and the reference phase number as input
154 # and extract the corresponding phase files, phase numbers,
155 # and reference phase file.
157 # export the variables containing each of the extracted data
159 extract_4d_phases_ref()
163 # reference phase file
164 ref_phase_file=`cat $mhd4d | grep ".z*raw" | sed 's:.z*raw:.mhd:' | grep $2`
165 echo "Reference phase is $ref_phase_file"
167 # reference phase number
168 for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do
169 ref_phase_nb=`echo ${phase_nbs[$i]} | grep $2`
170 if [ -n "$ref_phase_nb" ]; then
171 echo "Reference phase number is $ref_phase_nb"
178 # replacement for clitkCombineImage
181 # eg: -i $result_in -j $result_out -o $out_result -m $motion_mask
185 clitkSetBackground -i $1 -o $tmp1.mhd -m $4
186 clitkSetBackground -i $2 -o $tmp2.mhd -m $4 --fg
188 clitkImageArithm -i $tmp1.mhd -j $tmp2.mhd -o $3
193 # replacement for clitkAverageTemporalDimension
194 average_temporal_dimension()
196 # eg: -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd
200 local dir=`dirname $1`
201 local first=`grep z*raw $1 | sed 's/z*raw/mhd/g' | head -n 1`
202 clitkImageArithm -i $dir/$first -o $tot -t 1 -s 0
204 local nbphases=`grep z*raw $1 | sed 's/z*raw/mhd/g' | wc -l`
205 for i in $(grep z*raw $1 | sed 's/z*raw/mhd/g'); do
206 clitkImageArithm -i $dir/$i -j $tot -o $tot
209 clitkImageArithm -i $tot -o $2 -t 11 -s $nbphases
216 roi=`zenity --list --title="Available Contours" --column="Please choose a contour:" $roi_list`
221 zenity --warning --text="You must choose one contour."
222 select_contour $roi_list
224 rtstruct_roi_name=$roi
227 if zenity --question --text="Do you really wish to quit?"
231 select_contour $roi_list
234 zenity --error --text="Unexpected error. Please relaunch the application."
242 echo "Available Contours:"
243 for r in $roi_list; do
247 echo "Please choose a contour number:"
248 read rtstruct_roi_index
250 for r in $roi_list; do
251 if [ $rtstruct_roi_index = `echo $r | cut -d ':' -f 1` ]; then
252 rtstruct_roi_index=$i
262 rtstruct_roi_name_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0026" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Name] \|]//'` )
263 rtstruct_roi_number_list=( `clitkDicomInfo ${rtstruct_file} | grep "3006|0022" | cut -d '[' -f 4 | sed 's/| V 3006|0026[LO] [ROI Number] \|]//'` )
264 rtstruct_roi_list=( )
265 for i in $(seq 0 1 $(( ${#rtstruct_roi_name_list[@]} - 1 ))); do
266 rtstruct_roi_list[$i]=${rtstruct_roi_number_list[$i]}:${rtstruct_roi_name_list[$i]}
269 if [ $gui_mode = 1 ]; then
270 select_contour_gui ${rtstruct_roi_list[@]}
271 rtstruct_roi=`echo ${rtstruct_roi_name} | cut -d ':' -f 1`
272 rtstruct_roi_name=`echo ${rtstruct_roi_name} | cut -d ':' -f 2`
274 select_contour ${rtstruct_roi_list[@]}
275 rtstruct_roi=${rtstruct_roi_number_list[$(( $rtstruct_roi_index ))]}
276 rtstruct_roi_name=${rtstruct_roi_name_list[$(( $rtstruct_roi_index ))]}