X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=scripts%2Fcreate_midP_masks-2.0.sh;h=292ff3c4d27abeb8ce70043f663af607f0b5e90a;hb=f54492f7ac7d30e35ad953fe9f8c485bbb98c36d;hp=bbf44f0fa9a857da69581133681ddb046f715905;hpb=d8f6cf956310ff7b123df4ad82e20b61831d09ff;p=clitk.git diff --git a/scripts/create_midP_masks-2.0.sh b/scripts/create_midP_masks-2.0.sh index bbf44f0..292ff3c 100755 --- a/scripts/create_midP_masks-2.0.sh +++ b/scripts/create_midP_masks-2.0.sh @@ -43,7 +43,7 @@ extract_bones() extract_lungs() { echo "$phase_file -> Extracting lungs..." - clitkExtractLung -i $phase_file -o $mask_dir_tmp/lungs_$phase_nb.mhd -a $afdb_file --noAutoCrop --doNotSeparateLungs + clitkExtractLung -i $phase_file -o $mask_dir_tmp/lungs_$phase_nb.mhd -a $afdb_file --noAutoCrop --doNotSeparateLungs --type 1 } @@ -52,6 +52,7 @@ resample() { echo "$phase_file -> Resampling..." clitkResampleImage -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/patient_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo + clitkResampleImage -i $mask_dir_tmp/patient_mask_$phase_nb.mhd -o $mask_dir_tmp/patient_mask_$phase_nb.mhd --spacing $resample_spacing --interp $resample_algo clitkResampleImage -i $mask_dir_tmp/lungs_$phase_nb.mhd -o $mask_dir_tmp/lungs_$phase_nb.mhd --like $mask_dir_tmp/patient_$phase_nb.mhd } @@ -118,10 +119,12 @@ create_registration_masks() # echo "$phase_file -> Creating registration masks..." # inside - create_banded_mask $mask_dir_tmp/inside_$phase_nb.mhd $mask_dir_tmp/mm_$phase_nb.mhd $mask_dir_tmp/banded_inside_$phase_nb.mhd $mask_dir_tmp/mask_inside_$phase_nb.mhd 4 + clitkMorphoMath -i $mask_dir_tmp/mm_$phase_nb.mhd -o $mask_dir_tmp/mask_inside_$phase_nb.mhd --type 1 --radius 8 + create_banded_mask $mask_dir_tmp/inside_$phase_nb.mhd $mask_dir_tmp/mm_$phase_nb.mhd $mask_dir_tmp/banded_inside_$phase_nb.mhd $mask_dir_tmp/banded_mask_inside_$phase_nb.mhd 4 # outside clitkExtractPatient -i $mask_dir_tmp/outside_$phase_nb.mhd -o $mask_dir_tmp/mm_outside_$phase_nb.mhd --noAutoCrop - create_banded_mask $mask_dir_tmp/outside_$phase_nb.mhd $mask_dir_tmp/mm_outside_$phase_nb.mhd $mask_dir_tmp/banded_outside_$phase_nb.mhd $mask_dir_tmp/mask_outside_$phase_nb.mhd 4 + clitkMorphoMath -i $mask_dir_tmp/mm_outside_$phase_nb.mhd -o $mask_dir_tmp/mask_outside_$phase_nb.mhd --type 1 --radius 8 + create_banded_mask $mask_dir_tmp/outside_$phase_nb.mhd $mask_dir_tmp/mm_outside_$phase_nb.mhd $mask_dir_tmp/banded_outside_$phase_nb.mhd $mask_dir_tmp/banded_mask_outside_$phase_nb.mhd 4 } mm_preprocessing() @@ -130,7 +133,9 @@ mm_preprocessing() # extract_bones extract_lungs # remove_tmp_masks 1 - resample + if [ $resample_spacing -ne 0 ] ; then + resample + fi } mm_postprocessing() @@ -140,12 +145,79 @@ mm_postprocessing() create_registration_masks } +wait_mm_creation() +{ + # the motion masks are first created in a tmp directory. this directory is + # later going to be renamed to the final motion mask directory. concurrent + # executions trying to create the same set of masks will be blocked until + # the first execution finishes. naturally, these other executions will not + # recreate the masks. so first we try to create the tmp directory. + # if the creation fails, it means that another execution is + # already creating the masks, so this execution will be blocked. the + # execution is unblocked only when the creation of masks is finished and + # the mask directory is renamed. + # + do_mm=1 + if [ -e $mask_dir ]; then + # check that the final mask dir exists and that it contains all files it needs. + # the check assumes that the inside and outside masks are the key files to exist. + do_mm=0 + nb_phases=${#phase_nbs[@]} + nb_in_masks=`ls $mask_dir/mask_in*.mhd | wc -l` + nb_out_masks=`ls $mask_dir/mask_out*.mhd | wc -l` + if [ $nb_in_masks != $nb_phases -o $nb_out_masks != $nb_phases ]; then + # if the mask dir is invalid, remove it and recreate all masks, just in case. + rm -fr $mask_dir 2> /dev/null + do_mm=1 + fi + fi + + if [ $do_mm = 1 ]; then + if ! mkdir $mask_dir_tmp 2> /dev/null; then + if [ ! -e $mask_dir_tmp ]; then + # if the temp dir couldn't be created, but it doesn't exist, abort + abort_on_error wait_mm_creation $? clean_up_masks + else + # assumes another process is creating the maks in the temp dir. + # now we need to wait until the masks are complete or until the + # time limit is reached. + interval=10 + sleeping=0 + max_wait=3600 # one hour + nb_files0=`ls $mask_dir_tmp/* | wc -l` + while [ ! -e $mask_dir -a $sleeping -le $max_wait ]; do + echo "waiting creation of motion masks..." + sleep $interval + sleeping=$(( $sleeping + $interval )) + nb_files1=`ls $mask_dir_tmp/* | wc -l` + if [ $nb_files1 != $nb_files0 ]; then + nb_files0=$nb_files1 + sleeping=0 + fi + done + + if [ $sleeping -gt $max_wait ]; then + abort_on_error wait_mm_creation -1 clean_up_masks + else + echo "finished waiting" + do_mm=0 + fi + fi + fi + fi +} + motion_mask() { #set cmd line variables - mhd4d=$1 - resample_spacing=$2 - resample_algo=$3 + mhd4d=`basename $1` + if [ $# -eq 3 ] ; then + resample_spacing=$2 + resample_algo=$3 + else + resample_spacing=0 + resample_algo=0 + fi dir=`dirname $1` cd $dir @@ -156,7 +228,11 @@ motion_mask() fi #set other global variables - mask_dir="MASK-${resample_spacing}mm-$resample_algo" + if [ $resample_spacing -ne 0 ] ; then + mask_dir="MASK-${resample_spacing}mm-$resample_algo" + else + mask_dir="MASK" + fi mask_dir_tmp="tmp.$mask_dir" extract_4d_phases $mhd4d @@ -166,35 +242,8 @@ motion_mask() echo "start: $start" echo - # the motion masks are first created in a tmp directory. this directory is - # later going to be renamed to the final motion mask directory. concurrent - # executions trying to create the same set of masks will be blocked until - # the first execution finishes. naturally, these other executions will not - # recreate the masks. so first we try to create the tmp directory. - # if the creation fails, it means that another execution is - # already creating the masks, so this execution will be blocked. the - # execution is unblocked only when the creation of masks is finished and - # the mask directory is renamed. - # - # ATTENTION: RP - 08/02/2011 - # robustness issue: tmp directory may exist but may be empty or - # incomplete. the solution is to check per file, but I'll leave it like - # this for the moment. - do_mm=0 - if [ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]; then - mkdir $mask_dir_tmp 2> /dev/null - return_mkdir=$? - if [ $return_mkdir == 0 ]; then - do_mm=1 - else - while [[ $(ls -d $mask_dir 2> /dev/null | wc -l) -eq 0 ]]; do - echo "waiting creation of motion masks..." - sleep 2 - done - echo "finished waiting" - fi - fi - + wait_mm_creation + # do_mm=1 # mask_dir_tmp=$mask_dir if [ $do_mm == 1 ]; then @@ -264,8 +313,8 @@ motion_mask() # main # ################# -if [ $# != 3 ]; then - echo "Usage: $0 CT_4D RESAMPLE_SPACING RESAMPLE_ALGORITHM" +if [ $# -ne 3 -a $# -ne 1 ]; then + echo "Usage: $0 CT_4D [RESAMPLE_SPACING RESAMPLE_ALGORITHM]" exit -1 fi @@ -276,5 +325,9 @@ fi # if [ $1 != "using-as-lib" ]; then - motion_mask $1 $2 $3 + if [ $# -eq 3 ] ; then + motion_mask $1 $2 $3 + else + motion_mask $1 + fi fi