]> Creatis software - clitk.git/blobdiff - scripts/create_midP_masks-2.0.sh
more robust motion mask script
[clitk.git] / scripts / create_midP_masks-2.0.sh
index 9f64694d8bcf96117de8e1ba0dd8d339a7b85877..292ff3c4d27abeb8ce70043f663af607f0b5e90a 100755 (executable)
@@ -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()
@@ -142,6 +145,68 @@ 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
@@ -177,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