]> Creatis software - clitk.git/blob - scripts/midp_common.sh
a5d0bafecedab8873c7a1615729fa2ed9cac92be
[clitk.git] / scripts / midp_common.sh
1 #! /bin/sh -x
2
3 ###############################################################################
4 #
5 # FILE: midp_common.sh
6 # AUTHOR: RĂ´mulo Pinho 05/08/2011
7 #
8 # Helper file with many functions used in the midP scripts.
9 #
10 ###############################################################################
11
12 #
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.
16 #
17 abort_on_error()
18 {
19   if [ $2 != 0 ]; then
20     echo Aborted at $1 with code $2
21     #if [ $# = 3 ]; then
22     #  eval $3
23     #fi
24
25     exit $2
26   fi
27 }
28
29 #
30 # wait for all processes in the list and return their exit codes
31 # in the ret_codes variable.
32 #
33 # OBS: this function must always be called in the same shell
34 # that launched the processes.
35 #
36 wait_pids()
37 {
38   local pids=$*
39   local ret=
40   local rets=
41 #   echo PIDS: $pids
42   for p in $pids; do
43 #     echo waiting $p
44     wait $p > /dev/null 2> /dev/null
45     ret=$?
46     if [ ret != 127 ]; then
47       rets=$rets" "$ret
48     else
49       rets=$rets" "0
50     fi
51       
52   done
53
54   ret_codes=$rets
55 }
56
57 #
58 # clean-up functions for maks, registration, and midp
59 #
60 clean_up_masks()
61 {
62   rm -fr $mask_dir_tmp
63 }
64
65 clean_up_midp()
66 {
67   rm -fr $midp_dir
68 }
69
70 clean_up_registration()
71 {
72   rm -fr $vf_dir
73   rm -fr $output_dir
74 }
75
76
77 #
78 # block execution untill the number of threads (jobs) launched by the
79 # current process is below the given number of threads. 
80 MAX_THREADS=2
81 check_threads()
82 {
83   nbth=$1
84   while [[ $(jobs -p | wc -l) -ge $nbth ]]; do
85       jobs
86       sleep 10
87   done
88 }
89
90 #
91 # receive a 4D file and and extract the corresponding phase numbers
92 # export the variables containing each of the extracted data
93 #
94 extract_4d_phase_numbers()
95 {
96   mhd4d=$1
97
98   nb_phases=${#phase_files[@]}
99
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
102
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]\+"` )
107   else
108     for i in $patterns; do 
109
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]\+"` ) 
115         break
116       fi
117     
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]\+"` ) 
123         break
124       fi
125
126     done
127   fi
128
129   echo "Phase numbers are ${phase_nbs[@]}"
130   rm /tmp/patterns.txt
131 }
132
133 #
134 # receive a 4D file and extract the corresponding phase files, 
135 # and phase numbers.
136 # export the variables containing each of the extracted data
137 #
138 extract_4d_phases()
139 {
140   mhd4d=$1
141
142   echo "4D file is $mhd4d"
143
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[@]}"
147
148   extract_4d_phase_numbers $mhd4d 
149 }
150
151
152 #
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. 
156 #
157 # export the variables containing each of the extracted data
158 #
159 extract_4d_phases_ref()
160 {
161   extract_4d_phases $1
162
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"
166
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"
172       break
173     fi
174   done
175 }
176
177 #
178 # replacement for clitkCombineImage
179 combine_image()
180 {
181 #  eg: -i $result_in -j $result_out -o $out_result -m $motion_mask
182   local tmp1=$RANDOM
183   local tmp2=$RANDOM
184
185   clitkSetBackground -i $1 -o $tmp1.mhd -m $4
186   clitkSetBackground -i $2 -o $tmp2.mhd -m $4 --fg
187
188   clitkImageArithm -i $tmp1.mhd -j $tmp2.mhd -o $3
189   rm $tmp1.* $tmp2.*
190 }
191
192
193 # replacement for clitkAverageTemporalDimension
194 average_temporal_dimension()
195 {
196   # eg: -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd
197   local tmp=$RANDOM
198   local tot=$tmp.mhd
199
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
203
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
207   done
208
209   clitkImageArithm -i $tot -o $2 -t 11 -s $nbphases
210   rm $tmp.*
211 }
212
213 select_contour_gui()
214 {
215   local roi_list=$@
216   roi=`zenity --list --title="Available Contours" --column="Please choose a contour:" $roi_list`
217   case $? in
218     0)
219     if [ -z $roi ]
220     then
221       zenity --warning --text="You must choose one contour."
222       select_contour $roi_list
223     else
224       rtstruct_roi_name=$roi
225     fi;;
226     1)
227     if zenity --question --text="Do you really wish to quit?"
228     then
229       exit
230     else
231       select_contour $roi_list
232     fi;;
233     -1)
234       zenity --error --text="Unexpected error. Please relaunch the application."
235       exit;;
236   esac
237 }
238
239 select_contour()
240 {
241   local roi_list=$@
242   echo "Available Contours:" 
243   for r in $roi_list; do
244     echo $r
245   done
246
247   echo "Please choose a contour number:"
248   read rtstruct_roi_index
249   let i=0
250   for r in $roi_list; do
251     if [ $rtstruct_roi_index = `echo $r | cut -d ':' -f 1` ]; then
252       rtstruct_roi_index=$i
253       break;
254     fi
255     let i=i+1
256   done
257   
258 }
259
260 select_roi()
261 {
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]}
267   done
268
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`
273   else
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 ))]}
277   fi
278 }