]> Creatis software - clitk.git/blobdiff - cluster_tools/mergeDoseByRegions.sh
Debug RTStruct conversion with empty struc
[clitk.git] / cluster_tools / mergeDoseByRegions.sh
index 0548db6372d87354637939d608e867513e3842cf..b9303aac008b3b3a55a77bf4808531ad1c9e56b6 100755 (executable)
@@ -7,9 +7,9 @@ function usage {
 }
 
 function addToPartialResult {
-  IN1=$2
-  IN2=$4
-  RESULT=$6
+  IN1=$2 #merged file for previous jobs
+  IN2=$4 # current job
+  RESULT=$6 #output merged file
 
   test -f ${IN1} && test -f ${IN2} || usage
 
@@ -25,6 +25,44 @@ function addToPartialResult {
     exit 1
   fi
 
+  # Find the number of primaries for this job (the same for all line and different of 0)
+  # Get the number of primaries from edep and square_edep to determine the std.
+  nbPrimary=0
+  for i in $(seq 2 $nblines); do
+    #Get the line
+    file2=`sed -n "${i}p" < ${IN2}`
+
+    edep2=$(echo ${file2} | cut -f3 -d' ')
+    stdEdep2=$(echo ${file2} | cut -f4 -d' ')
+    sqEdep2=$(echo ${file2} | cut -f5 -d' ')
+    nbPrimary=$(python <<EOP
+if ($edep2 == 0 or $sqEdep2 == 0):
+  print(0)
+else:
+  temp=pow($edep2,2)*(pow($stdEdep2,2)-1)/(pow($stdEdep2*$edep2,2)-$sqEdep2)
+  print(int(round(temp)))
+EOP
+    )
+    if [ $nbPrimary -ne 0 ]; then
+      break
+    fi
+
+    dose2=$(echo ${file2} | cut -f6 -d' ')
+    stdDose2=$(echo ${file2} | cut -f7 -d' ')
+    sqDose2=$(echo ${file2} | cut -f8 -d' ')
+    nbPrimary=$(python <<EOP
+if ($dose2 == 0 or $sqDose2 == 0):
+  print(0)
+else:
+  temp=pow($dose2,2)*(pow($stdDose2,2)-1)/(pow($stdDose2*$dose2,2)-$sqDose2)
+  print(int(round(temp)))
+EOP
+    )
+    if [ $nbPrimary -ne 0 ]; then
+      break
+    fi
+  done
+
   #Copy the 1st line in output
   line1=`sed -n "1p" < ${RESULT}`
   echo "${line1}" >> ${TMP}
@@ -44,18 +82,6 @@ function addToPartialResult {
     edep3=${edep3/#./0.}
     file3=$(echo $file3 |awk -v r=${edep3} '{$3=r}1')
 
-    # sqrt sum square std_edep*edep: get the 2 values, sum the square, take the sqrt and replace it in the file3
-    stdEdep1=$(echo ${file1} | cut -f4 -d' ')
-    stdEdep2=$(echo ${file2} | cut -f4 -d' ')
-    stdEdep3=$(python <<EOP
-import math;
-temp=$edep2*$stdEdep2;
-print(math.sqrt($stdEdep1*$stdEdep1+temp*temp))
-EOP
-    )
-    stdEdep3=${stdEdep3/#./0.}
-    file3=$(echo $file3 |awk -v r=${stdEdep3} '{$4=r}1')
-
     # sum square_edep: get the 2 values, sum them and replace it in the file3
     sqEdep1=$(echo ${file1} | cut -f5 -d' ')
     sqEdep2=$(echo ${file2} | cut -f5 -d' ')
@@ -63,6 +89,16 @@ EOP
     sqEdep3=${sqEdep3/#./0.}
     file3=$(echo $file3 |awk -v r=${sqEdep3} '{$5=r}1')
 
+    # Get the number of primaries from edep and square_edep to determine the std.
+    # Sum the number of primaries with the latter ones
+    nbPrimaryEdep1=$(echo ${file1} | cut -f4 -d' ')
+    nbPrimaryEdep3=$(python <<EOP
+print($nbPrimary+$nbPrimaryEdep1)
+EOP
+    )
+    nbPrimaryEdep3=${nbPrimaryEdep3/#./0.}
+    file3=$(echo $file3 |awk -v r=${nbPrimaryEdep3} '{$4=r}1')
+
     # sum dose: get the 2 values, sum them and replace it in the file3
     dose1=$(echo ${file1} | cut -f6 -d' ')
     dose2=$(echo ${file2} | cut -f6 -d' ')
@@ -70,18 +106,6 @@ EOP
     dose3=${dose3/#./0.}
     file3=$(echo $file3 |awk -v r=${dose3} '{$6=r}1')
 
-    # sqrt sum square std_dose*dose: get the 2 values, sum the square, take the sqrt and replace it in the file3
-    stdDose1=$(echo ${file1} | cut -f7 -d' ')
-    stdDose2=$(echo ${file2} | cut -f7 -d' ')
-    stdDose3=$(python <<EOP
-import math;
-temp=$dose2*$stdDose2;
-print(math.sqrt($stdDose1*$stdDose1+temp*temp))
-EOP
-    )
-    stdDose3=${stdDose3/#./0.}
-    file3=$(echo $file3 |awk -v r=${stdDose3} '{$7=r}1')
-
     # sum square_dose: get the 2 values, sum them and replace it in the file3
     sqDose1=$(echo ${file1} | cut -f8 -d' ')
     sqDose2=$(echo ${file2} | cut -f8 -d' ')
@@ -89,6 +113,17 @@ EOP
     sqDose3=${sqDose3/#./0.}
     file3=$(echo $file3 |awk -v r=${sqDose3} '{$8=r}1')
 
+    # Get the number of primaries from dose and square_dose to determine the std.
+    # Sum the number of primaries with the latter ones
+    nbPrimaryDose1=$(echo ${file1} | cut -f7 -d' ')
+    stdDose2=$(echo ${file2} | cut -f7 -d' ')
+    nbPrimaryDose3=$(python <<EOP
+print($nbPrimary+$nbPrimaryDose1)
+EOP
+    )
+    nbPrimaryDose3=${nbPrimaryDose3/#./0.}
+    file3=$(echo $file3 |awk -v r=${nbPrimaryDose3} '{$7=r}1')
+
     # sum n_hits: get the 2 values, sum them and replace it in the file3
     hit1=$(echo ${file1} | cut -f9 -d' ')
     hit2=$(echo ${file2} | cut -f9 -d' ')
@@ -107,86 +142,69 @@ EOP
   mv -f ${TMP} ${RESULT}
 }
 
-function addWithoutPartialResult {
+function copyFirstPartialResult {
   IN1=$2
   RESULT=$4
 
-  test -f ${IN1} || usage
-
   TMP="$(mktemp)"
 
+  # check if all 2 text files have the same number of lines
   nblines=`cat ${IN1} | wc -l`
+  nb3=`cat ${RESULT} | wc -l`
 
-  #Copy the 1st line in output
-  line1=`sed -n "1p" < ${IN1}`
-  echo "${line1}" >> ${TMP}
-  # for all lines (except the 1st), split according tab
-  # sum the some elements (dose, edep) ...
+  if [ $nblines -ne $nb3 ]; then
+    echo "Files does not have the same size"
+    exit 1
+  fi
+  # Find the number of primaries for this job (the same for all line and different of 0)
+  # Get the number of primaries from edep and square_edep to determine the std.
+  nbPrimary=0
   for i in $(seq 2 $nblines); do
-    #Get the lines
-    file1=`sed -n "${i}p" < ${IN1}`
-
-    # copy id
-    id1=$(echo ${file1} | cut -f1 -d' ')
-    id1=${id1/#./0.}
-    file3=$(echo $id1)
-
-    # copy volume
-    vol1=$(echo ${file1} | cut -f2 -d' ')
-    vol1=${vol1/#./0.}
-    file3=$(echo "$file3 $vol1")
-
-    # copy edep
-    edep1=$(echo ${file1} | cut -f3 -d' ')
-    edep1=${edep1/#./0.}
-    file3=$(echo "$file3 $edep1")
+    #Get the line
+    file2=`sed -n "${i}p" < ${IN1}`
 
-    # sqrt sum square std_edep*edep
-    stdEdep1=$(echo ${file1} | cut -f4 -d' ')
-    stdEdep3=$(python <<EOP
-import math;
-temp=$edep1*$stdEdep1;
-print(math.sqrt(temp*temp))
+    edep2=$(echo ${file2} | cut -f3 -d' ')
+    stdEdep2=$(echo ${file2} | cut -f4 -d' ')
+    sqEdep2=$(echo ${file2} | cut -f5 -d' ')
+    nbPrimary=$(python <<EOP
+if ($edep2 == 0 or $sqEdep2 == 0):
+  print(0)
+else:
+  temp=pow($edep2,2)*(pow($stdEdep2,2)-1)/(pow($stdEdep2*$edep2,2)-$sqEdep2)
+  print(int(round(temp)))
 EOP
     )
-    stdEdep3=${stdEdep3/#./0.}
-    file3=$(echo "$file3 $stdEdep3")
+    if [ $nbPrimary -ne 0 ]; then
+      break
+    fi
 
-    # copy square_edep
-    sqEdep1=$(echo ${file1} | cut -f5 -d' ')
-    sqEdep1=${sqEdep1/#./0.}
-    file3=$(echo "$file3 $sqEdep1")
-
-    # copy dose
-    dose1=$(echo ${file1} | cut -f6 -d' ')
-    dose1=${dose1/#./0.}
-    file3=$(echo "$file3 $dose1")
-
-    # sqrt sum square std_dose*dose
-    stdDose1=$(echo ${file1} | cut -f7 -d' ')
-    stdDose3=$(python <<EOP
-import math;
-temp=$dose1*$stdDose1;
-print(math.sqrt(temp*temp))
+    dose2=$(echo ${file2} | cut -f6 -d' ')
+    stdDose2=$(echo ${file2} | cut -f7 -d' ')
+    sqDose2=$(echo ${file2} | cut -f8 -d' ')
+    nbPrimary=$(python <<EOP
+if ($dose2 == 0 or $sqDose2 == 0):
+  print(0)
+else:
+  temp=pow($dose2,2)*(pow($stdDose2,2)-1)/(pow($stdDose2*$dose2,2)-$sqDose2)
+  print(int(round(temp)))
 EOP
     )
-    stdDose3=${stdDose3/#./0.}
-    file3=$(echo "$file3 $stdDose3")
-
-    # copy square_dose
-    sqDose1=$(echo ${file1} | cut -f8 -d' ')
-    sqDose1=${sqDose1/#./0.}
-    file3=$(echo "$file3 $sqDose1")
+    if [ $nbPrimary -ne 0 ]; then
+      break
+    fi
+  done
 
-    # copy n_hits
-    hit1=$(echo ${file1} | cut -f9 -d' ')
-    hit1=${hit1/#./0.}
-    file3=$(echo "$file3 $hit1")
+  #Copy the 1st line in output
+  line1=`sed -n "1p" < ${RESULT}`
+  echo "${line1}" >> ${TMP}
+  # for all lines (except the 1st), split according tab
+  # write the number of primaries
+  for i in $(seq 2 $nblines); do
+    #Get the lines
+    file3=`sed -n "${i}p" < ${RESULT}`
 
-    # copy n_event_hits
-    event1=$(echo ${file1} | cut -f10 -d' ')
-    event1=${event1/#./0.}
-    file3=$(echo "$file3 $event1")
+    file3=$(echo $file3 |awk -v r=${nbPrimary} '{$4=r}1')
+    file3=$(echo $file3 |awk -v r=${nbPrimary} '{$7=r}1')
 
     #Write the output
     echo "${file3}" >> ${TMP}
@@ -194,15 +212,13 @@ EOP
   mv -f ${TMP} ${RESULT}
 }
 
-
 function divideUncertaintyResult {
   IN1=$2
-  value=$4
-  RESULT=$6
+  RESULT=$4
 
   TMP="$(mktemp)"
 
-  # check if all 3 text files have the same number of lines
+  # check if all 2 text files have the same number of lines
   nblines=`cat ${IN1} | wc -l`
   nb3=`cat ${RESULT} | wc -l`
 
@@ -223,14 +239,15 @@ function divideUncertaintyResult {
 
     # Divide uncertainty and replace it in the file3
     edep1=$(echo ${file1} | cut -f3 -d' ')
-    stdEdep1=$(echo ${file1} | cut -f4 -d' ')
+    nbPrimaryEdep1=$(echo ${file1} | cut -f4 -d' ')
+    sqEdep1=$(echo ${file1} | cut -f5 -d' ')
     stdEdep3=$(python <<EOP
-temp=$stdEdep1/$value
-if $edep1!=0:
-  temp/=$edep1
-if temp==0:
-  temp=1
-print(temp)
+import math
+if ($edep1 == 0 or $sqEdep1 == 0):
+  print(1.0)
+else:
+  temp=math.sqrt(($sqEdep1/$nbPrimaryEdep1-pow($edep1/$nbPrimaryEdep1, 2))/($nbPrimaryEdep1-1))/($edep1/$nbPrimaryEdep1)
+  print(temp)
 EOP
     )
     stdEdep3=${stdEdep3/#./0.}
@@ -238,14 +255,15 @@ EOP
 
     # Divide uncertainty and replace it in the file3
     dose1=$(echo ${file1} | cut -f6 -d' ')
-    stdDose1=$(echo ${file1} | cut -f7 -d' ')
+    nbPrimaryDose1=$(echo ${file1} | cut -f7 -d' ')
+    sqDose1=$(echo ${file1} | cut -f8 -d' ')
     stdDose3=$(python <<EOP
-temp=$stdDose1/$value
-if $dose1!=0:
-  temp/=$dose1
-if temp==0:
-  temp=1
-print(temp)
+import math
+if ($edep1 == 0 or $sqEdep1 == 0):
+  print(1.0)
+else:
+  temp=math.sqrt(($sqDose1/$nbPrimaryDose1-pow($dose1/$nbPrimaryDose1, 2))/($nbPrimaryDose1-1))/($dose1/$nbPrimaryDose1)
+  print(temp)
 EOP
     )
     stdDose3=${stdDose3/#./0.}