From 41e17601d6204e6036fa27a3db4c8d1e6bd175a7 Mon Sep 17 00:00:00 2001 From: tbaudier Date: Fri, 13 Jul 2018 10:33:17 +0200 Subject: [PATCH] Add merger for DoseByRegions --- cluster_tools/CMakeLists.txt | 1 + cluster_tools/gate_power_merge.sh | 40 +++++++++++ cluster_tools/mergeDoseByRegions.sh | 103 ++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100755 cluster_tools/mergeDoseByRegions.sh diff --git a/cluster_tools/CMakeLists.txt b/cluster_tools/CMakeLists.txt index f0167f6..e208df4 100644 --- a/cluster_tools/CMakeLists.txt +++ b/cluster_tools/CMakeLists.txt @@ -10,6 +10,7 @@ if(CLITK_BUILD_TOOLS) gate_run_submit_cluster.sh gate_upload_release.sh mergeDosePerEnergyFile.sh + mergeDoseByRegions.sh mergeStatFile.py mergeStatFile.sh gate_run_submit_cluster_nomove.sh diff --git a/cluster_tools/gate_power_merge.sh b/cluster_tools/gate_power_merge.sh index a0c3e18..7717c57 100755 --- a/cluster_tools/gate_power_merge.sh +++ b/cluster_tools/gate_power_merge.sh @@ -196,6 +196,37 @@ function merge_dose { echo " ${indent}merged ${count} files" } +doseMerger="mergeDoseByRegions.sh" +test -x "./mergeDoseByRegions.sh" && doseMerger="./mergeDoseByRegions.sh" + +function merge_doseByRegions { + local merged="$1" + shift + echo " ${indent}entering dose merger" + echo " ${indent}merger is ${doseMerger}" + echo " ${indent}creating ${merged}" + local count=0 + start_bar $# + while test $# -gt 0 + do + local partial="$1" + shift + let count++ + + if test ! -f "${merged}" + then + update_bar ${count} "copying first partial result ${partial}" + cp "${partial}" "${merged}" + continue + fi + + update_bar ${count} "adding ${partial}" + ${doseMerger} -i "${merged}" -j "${partial}" -o "${merged}" 2> /dev/null > /dev/null || warning "error while calling ${doseMerger}" + done + end_bar + echo " ${indent}merged ${count} files" +} + txtImageMerger="clitkMergeAsciiDoseActor" test -f "./clitkMergeAsciiDoseActor" && txtImageMerger="./clitkMergeAsciiDoseActor" @@ -409,6 +440,15 @@ function merge_dispatcher { fi fi + if test "${firstpartialoutputextension}" == "txt" && grep -qs 'vol(mm3)' "${firstpartialoutputfile}" + then + echo "${indent}this is a DoseByRegions file" + local mergedfile="${outputdir}/$(basename "${firstpartialoutputfile}")" + merge_doseByRegions "${mergedfile}" ${partialoutputfiles} || error "error while merging" + return + fi + + if test "${firstpartialoutputextension}" == "txt" then echo "${indent}this is a non specific txt output" diff --git a/cluster_tools/mergeDoseByRegions.sh b/cluster_tools/mergeDoseByRegions.sh new file mode 100755 index 0000000..d7a390a --- /dev/null +++ b/cluster_tools/mergeDoseByRegions.sh @@ -0,0 +1,103 @@ +#!/bin/bash +set -u + +function usage { + echo "$0 -i -j -o " + exit 1 +} + +if [ $# != 6 ] +then + usage +fi + +IN1=$2 +IN2=$4 +RESULT=$6 + +test -f ${IN1} && test -f ${IN2} || usage + +TMP="$(mktemp)" +echo "merging dose by regions file" +# check if all 3 text files have the same number of lines +nblines=`cat ${IN1} | wc -l` +nb2=`cat ${IN2} | wc -l` +nb3=`cat ${RESULT} | wc -l` + +if [ $nblines -ne $nb2 ] || [ $nblines -ne $nb3 ]; then + echo "Files does not have the same size" + exit 1 +fi + +#Copy the 1st line in output +line1=`sed -n "1p" < ${RESULT}` +echo "${line1}" >> ${TMP} + +# for all lines (except the 1st), split according tab +# sum the some elements (dose, edep) ... +for i in $(seq 2 $nblines); do + #Get the 3 lines + line1=`sed -n "${i}p" < ${IN1}` + line2=`sed -n "${i}p" < ${IN2}` + line3=`sed -n "${i}p" < ${RESULT}` + + # sum edep: get the 2 values, sum them and replace it in the line3 + # The /#./0. is important to add 0 for decimal value between 0 and 1 + edep1=$(echo ${line1} | cut -f3 -d' ') + edep2=$(echo ${line2} | cut -f3 -d' ') + edep3=$(echo "scale=30;$edep1+$edep2" | bc) + edep3=${edep3/#./0.} + line3=$(echo $line3 |awk -v r=${edep3} '{$3=r}1') + + # sqrt sum square std_edep: get the 2 values, sum the square, take the sqrt and replace it in the line3 + edep1=$(echo ${line1} | cut -f4 -d' ') + edep2=$(echo ${line2} | cut -f4 -d' ') + edep3=$(echo "scale=30;sqrt($edep1*$edep1+$edep2*$edep2)" | bc) + edep3=${edep3/#./0.} + line3=$(echo $line3 |awk -v r=${edep3} '{$4=r}1') + + # sum square_edep: get the 2 values, sum them and replace it in the line3 + edep1=$(echo ${line1} | cut -f5 -d' ') + edep2=$(echo ${line2} | cut -f5 -d' ') + edep3=$(echo "scale=30;$edep1+$edep2" | bc) + edep3=${edep3/#./0.} + line3=$(echo $line3 |awk -v r=${edep3} '{$5=r}1') + + # sum dose: get the 2 values, sum them and replace it in the line3 + edep1=$(echo ${line1} | cut -f6 -d' ') + edep2=$(echo ${line2} | cut -f6 -d' ') + edep3=$(echo "scale=30;$edep1+$edep2" | bc) + edep3=${edep3/#./0.} + line3=$(echo $line3 |awk -v r=${edep3} '{$6=r}1') + + # sqrt sum square std_dose: get the 2 values, sum the square, take the sqrt and replace it in the line3 + edep1=$(echo ${line1} | cut -f7 -d' ') + edep2=$(echo ${line2} | cut -f7 -d' ') + edep3=$(echo "scale=30;sqrt($edep1*$edep1+$edep2*$edep2)" | bc) + edep3=${edep3/#./0.} + line3=$(echo $line3 |awk -v r=${edep3} '{$7=r}1') + + # sum square_dose: get the 2 values, sum them and replace it in the line3 + edep1=$(echo ${line1} | cut -f8 -d' ') + edep2=$(echo ${line2} | cut -f8 -d' ') + edep3=$(echo "scale=30;$edep1+$edep2" | bc) + edep3=${edep3/#./0.} + line3=$(echo $line3 |awk -v r=${edep3} '{$8=r}1') + + # sum n_hits: get the 2 values, sum them and replace it in the line3 + edep1=$(echo ${line1} | cut -f9 -d' ') + edep2=$(echo ${line2} | cut -f9 -d' ') + edep3=$(echo "scale=30;$edep1+$edep2" | bc) + line3=$(echo $line3 |awk -v r=${edep3} '{$9=r}1') + + # sum n_event_hits: get the 2 values, sum them and replace it in the line3 + edep1=$(echo ${line1} | cut -f10 -d' ') + edep2=$(echo ${line2} | cut -f10 -d' ') + edep3=$(echo "scale=30;$edep1+$edep2" | bc) + line3=$(echo $line3 |awk -v r=${edep3} '{$10=r}1') + + #Write the output + echo "${line3}" >> ${TMP} +done +mv -f ${TMP} ${RESULT} + -- 2.47.1