From: Romulo Pinho Date: Thu, 3 Nov 2011 15:56:52 +0000 (+0100) Subject: more refined abort-on-exception mechanism X-Git-Tag: v1.3.0~174^2^2~3 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=0451c66d4a069d04db99c9d719e053c6019923d8;p=clitk.git more refined abort-on-exception mechanism - new macro defined in clitkCommon.h - abort execution with a proper return code - only used in tools related to mid-P calculation, for now - mid-P scripts interpret return codes --- diff --git a/common/clitkCommon.h b/common/clitkCommon.h index 31f28dc..51258e6 100644 --- a/common/clitkCommon.h +++ b/common/clitkCommon.h @@ -48,6 +48,24 @@ namespace clitk { typedef unsigned char uchar; typedef unsigned short ushort; typedef unsigned int uint; + +#define CLITK_TRY_CATCH_EXIT(func) \ + try { \ + func; \ + } \ + catch (const itk::ExceptionObject& e) { \ + e.Print(std::cout); \ + exit(-1);\ + } \ + catch (const std::exception& e) { \ + std::cout << e.what() << std::endl; \ + exit(-2);\ + } \ + catch (...) { \ + std::cout << "Unknown excpetion" << std::endl; \ + exit(-3); \ + } + //-------------------------------------------------------------------- // when everything goes wrong diff --git a/scripts/create_midP-2.0.sh b/scripts/create_midP-2.0.sh index 8ea7e2c..0b3947b 100755 --- a/scripts/create_midP-2.0.sh +++ b/scripts/create_midP-2.0.sh @@ -1,4 +1,4 @@ -#!/bin/sh -x +#!/bin/sh ############################################################################### # @@ -80,8 +80,14 @@ registration() motion_mask=$mask_dir/mm_$phase_nb.mhd vf_result=$vf_dir/vf_${ref_phase_nb}_$phase_nb.mhd clitkCombineImage -i $vf_in -j $vf_out -m $motion_mask -o $vf_result + abort_on_error registration $? clean_up_registration + clitkZeroVF -i $vf_in -o vf_zero.mhd + abort_on_error registration $? clean_up_registration + clitkCombineImage -i $vf_result -j vf_zero.mhd -m $patient_mask -o $vf_result + abort_on_error registration $? clean_up_registration + rm vf_zero.* # save for later... @@ -91,6 +97,7 @@ registration() # create (zero) vf from reference to reference clitkZeroVF -i $vf_ref -o $vf_dir/vf_${ref_phase_nb}_${ref_phase_nb}.mhd + abort_on_error registration $? clean_up_registration # create 4D vf create_mhd_4D_pattern.sh $vf_dir/vf_${ref_phase_nb}_ @@ -120,14 +127,20 @@ midp() midp=$midp_dir/midp_$phase_nb.mhd # average the vf's from reference phase to phase clitkAverageTemporalDimension -i $vf_dir/vf_${ref_phase_nb}_4D.mhd -o $vf_midp + abort_on_error midp $? clean_up_midp + # invert the vf (why?) clitkInvertVF -i $vf_midp -o $vf_midp + abort_on_error midp $? clean_up_midp + # create the midp by warping the reference phase with the reference vf clitkWarpImage -i $ref_phase_file -o $midp --vf=$vf_midp -s 1 + abort_on_error midp $? clean_up_midp ref_vf_midp=$vf_midp ref_midp=$midp clitkImageConvert -i $ref_midp -o $ref_midp -t float + abort_on_error midp $? clean_up_midp ########### calculate the midp wrt the other phases for i in $( seq 0 $((${#phase_files[@]} - 1))); do @@ -141,16 +154,25 @@ midp() # calculate vf from phase to midp, using the vf from reference phase to midp (-i) # and the vf from reference phase to phase (-j) clitkComposeVF -i $ref_vf_midp -j $vf_dir/vf_$ref_phase_nb\_$phase_nb.mhd -o $vf_midp + abort_on_error midp $? clean_up_midp + clitkWarpImage -i $phase_file -o $midp --vf=$vf_midp -s 1 + abort_on_error midp $? clean_up_midp + clitkImageConvert -i $midp -o $midp -t float + abort_on_error midp $? clean_up_midp fi done create_mhd_4D_pattern.sh $midp_dir/midp_ + echo "Calculating midp_avg.mhd..." clitkAverageTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_avg.mhd + abort_on_error midp $? clean_up_midp + echo "Calculating midp_med.mhd..." clitkMedianTemporalDimension -i $midp_dir/midp_4D.mhd -o $midp_dir/midp_med.mhd + abort_on_error midp $? clean_up_midp # clean-up rm $midp_dir/vf_* diff --git a/scripts/create_midP_masks-2.0.sh b/scripts/create_midP_masks-2.0.sh index f5738e0..6fadeec 100755 --- a/scripts/create_midP_masks-2.0.sh +++ b/scripts/create_midP_masks-2.0.sh @@ -1,5 +1,5 @@ -#! /bin/bash -x - +#! /bin/bash + ############################################################################### # # FILE: create_midP-2.0.sh @@ -21,7 +21,10 @@ extract_patient() { echo "$phase_file -> Extracting patient..." clitkExtractPatient -i $phase_file -o $mask_dir_tmp/patient_mask_$phase_nb.mhd --noAutoCrop -a $afdb_file $ExtractPatientExtra +# abort_on_error clitkExtractPatient $? + clitkSetBackground -i $phase_file -o $mask_dir_tmp/patient_$phase_nb.mhd --mask $mask_dir_tmp/patient_mask_$phase_nb.mhd --outsideValue -1000 +# abort_on_error clitkSetBackground $? } extract_bones() @@ -47,7 +50,6 @@ 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/lungs_$phase_nb.mhd -o $mask_dir_tmp/lungs_$phase_nb.mhd --like $mask_dir_tmp/patient_$phase_nb.mhd } @@ -60,7 +62,7 @@ compute_motion_mask() FillingLevel=94 fi - clitkMotionMask -i $mask_dir_tmp/patient_$phase_nb.mhd -o $mask_dir_tmp/mm_$phase_nb.mhd --featureLungs $mask_dir_tmp/lungs_$phase_nb.mhd --upperThresholdLungs -400 --fillingLevel $FillingLevel --offsetDetect $MotionMaskOffsetDetect --pad --writeFeature=$mask_dir_tmp/feature_$phase_nb.mhd $MotionMaskExtra + clitkMotionMask -i $mask_dir_tmp/patient_$phase_nb.mh -o $mask_dir_tmp/mm_$phase_nb.mhd --featureLungs $mask_dir_tmp/lungs_$phase_nb.mhd --upperThresholdLungs -400 --fillingLevel $FillingLevel --offsetDetect $MotionMaskOffsetDetect --pad --writeFeature=$mask_dir_tmp/feature_$phase_nb.mhd $MotionMaskExtra #--monitor=$mask_dir_tmp/monitor_$phase_nb.mhd } @@ -198,6 +200,7 @@ motion_mask() mkdir -p $mask_log_dir # multi-threaded pre-processing for motion mask calcs + pids=( ) for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do phase_nb=${phase_nbs[$i]} phase_file=${phase_files[$i]} @@ -205,6 +208,12 @@ motion_mask() check_threads $MAX_THREADS mm_preprocessing & + pids=( "${pids[@]}" "$!" ) + done + + wait_pids ${pids[@]} + for ret in $ret_codes; do + abort_on_error mm_preprocessing $ret clean_up_masks done # single-threaded motion mask calc @@ -215,17 +224,26 @@ motion_mask() check_threads 1 echo "$phase_file -> Computing motion mask..." compute_motion_mask > $mask_log_dir/motion_mask_$phase_file.log + abort_on_error compute_motion_mask $? clean_up_masks done # multi-threaded post-processing of motion mask calcs + pids=( ) for i in $( seq 0 $((${#phase_nbs[@]} - 1))); do phase_nb=${phase_nbs[$i]} phase_file=${phase_files[$i]} check_threads $MAX_THREADS mm_postprocessing & + pids=( "${pids[@]}" "$!" ) + done + + wait_pids ${pids[@]} + for ret in $ret_codes; do + abort_on_error mm_postprocessing $ret clean_up_masks done + # rename tmp mask directory after mask creation check_threads 1 mv -f $mask_dir_tmp $mask_dir diff --git a/scripts/midp_common.sh b/scripts/midp_common.sh index 0f76707..aa500ae 100755 --- a/scripts/midp_common.sh +++ b/scripts/midp_common.sh @@ -1,4 +1,4 @@ -#! /bin/sh +x +#! /bin/sh -x ############################################################################### # @@ -9,7 +9,72 @@ # ############################################################################### +# +# check return value passed and abort if it represents an error (ie, ret != 0) +# optionally, a function can be passed as a 3rd parameter, to be called just +# before exiting. this is useful for cleaning up, for example. +# +abort_on_error() +{ + if [ $2 != 0 ]; then + echo Aborted at $1 with code $2 + if [ $# = 3 ]; then + eval $3 + fi + exit $2 + fi +} + +# +# wait for all processes in the list and return their exit codes +# in the ret_codes variable. +# +# OBS: this function must always be called in the same shell +# that launched the processes. +# +wait_pids() +{ + local pids=$* + local ret= + local rets= +# echo PIDS: $pids + for p in $pids; do +# echo waiting $p + wait $p > /dev/null 2> /dev/null + ret=$? + if [ ret != 127 ]; then + rets=$rets" "$ret + else + rets=$rets" "0 + fi + + done + + ret_codes=$rets +} + +# +# clean-up functions for maks, registration, and midp +# +clean_up_masks() +{ + rm -fr $mask_dir_tmp +} + +clean_up_midp() +{ + rm -fr $midp_dir +} + +clean_up_registration() +{ + rm -fr $vf_dir + rm -fr $output_dir +} + + +# # block execution untill the number of threads (jobs) launched by the # current process is below the given number of threads. MAX_THREADS=2 diff --git a/scripts/registration.sh b/scripts/registration.sh index 40d5fbe..724e3ac 100755 --- a/scripts/registration.sh +++ b/scripts/registration.sh @@ -31,6 +31,8 @@ # ############################################################################### +source `dirname $0`/midp_common.sh + ################# BLUTDIR ##################### registration_blutdir() @@ -56,6 +58,8 @@ registration_blutdir() blutdir_params="--levels $nb_levels --metric $metric --optimizer $optimizer --samples $nb_samples --spacing $spacing,$spacing,$spacing --bins $hist_bins --maxIt $nb_iter --interp $interpolator --verbose" cmd="clitkBLUTDIR -r $reference -t $target -m $mask_ref --targetMask $mask_targ --vf $vf -o $result $blutdir_params" $cmd > $log + + abort_on_error registration_blutdir $? clean_up_registration } ################# ELASTIX ##################### @@ -105,10 +109,12 @@ registration_elastix() # image registration cmd="elastix -f $reference -m $target -fMask $mask_ref -mMask $mask_targ -out $result_dir -p params_BSpline_${suffix}.txt" $cmd > /dev/null + abort_on_error registration_elastix $? clean_up_registration # generate vector field cmd="transformix -tp $result_dir/TransformParameters.0.txt -out $vf_dir -def all" $cmd > /dev/null + abort_on_error registration_elastix $? clean_up_registration # post-processing mv $vf_dir/deformationField.mhd $vf diff --git a/segmentation/clitkExtractBones.cxx b/segmentation/clitkExtractBones.cxx index 3e27657..4d3302f 100644 --- a/segmentation/clitkExtractBones.cxx +++ b/segmentation/clitkExtractBones.cxx @@ -34,11 +34,7 @@ int main(int argc, char * argv[]) filter->SetArgsInfo(args_info); - try { - filter->Update(); - } catch(std::runtime_error e) { - std::cout << e.what() << std::endl; - } + CLITK_TRY_CATCH_EXIT(filter->Update()); return EXIT_SUCCESS; } // This is the end, my friend diff --git a/segmentation/clitkExtractLung.cxx b/segmentation/clitkExtractLung.cxx index 36fa4e8..1b5fda9 100644 --- a/segmentation/clitkExtractLung.cxx +++ b/segmentation/clitkExtractLung.cxx @@ -33,11 +33,7 @@ int main(int argc, char * argv[]) filter->SetArgsInfo(args_info); - try { - filter->Update(); - } catch(std::runtime_error e) { - std::cerr << e.what() << std::endl; - } + CLITK_TRY_CATCH_EXIT(filter->Update()); return EXIT_SUCCESS; } // This is the end, my friend diff --git a/segmentation/clitkExtractPatient.cxx b/segmentation/clitkExtractPatient.cxx index 43a0863..ebbd3ad 100644 --- a/segmentation/clitkExtractPatient.cxx +++ b/segmentation/clitkExtractPatient.cxx @@ -33,11 +33,7 @@ int main(int argc, char * argv[]) { filter->SetArgsInfo(args_info); - try { - filter->Update(); - } catch(std::runtime_error e) { - std::cout << e.what() << std::endl; - } + CLITK_TRY_CATCH_EXIT(filter->Update()); return EXIT_SUCCESS; } // This is the end, my friend diff --git a/segmentation/clitkExtractPatientGenericFilter.cxx b/segmentation/clitkExtractPatientGenericFilter.cxx index 3dfde0d..88c9cb8 100644 --- a/segmentation/clitkExtractPatientGenericFilter.cxx +++ b/segmentation/clitkExtractPatientGenericFilter.cxx @@ -37,7 +37,7 @@ namespace clitk //----------------------------------------------------------- // Constructor //----------------------------------------------------------- - ExtractPatientGenericFilter::ExtractPatientGenericFilter() + ExtractPatientGenericFilter:::ExtractPatientGenericFilter() { m_Verbose=false; m_InputFileName=""; diff --git a/segmentation/clitkMorphoMath.cxx b/segmentation/clitkMorphoMath.cxx index 46e3489..25c40df 100644 --- a/segmentation/clitkMorphoMath.cxx +++ b/segmentation/clitkMorphoMath.cxx @@ -36,11 +36,7 @@ int main(int argc, char * argv[]) { // Passing the arguments to the generic filter filter->SetArgsInfo(args_info); - try { - filter->Update(); - } catch(std::runtime_error e) { - std::cout << e.what() << std::endl; - } + CLITK_TRY_CATCH_EXIT(filter->Update()); return EXIT_SUCCESS; } // This is the end, my friend diff --git a/segmentation/clitkMotionMask.cxx b/segmentation/clitkMotionMask.cxx index fdc31c7..fd65c64 100644 --- a/segmentation/clitkMotionMask.cxx +++ b/segmentation/clitkMotionMask.cxx @@ -43,7 +43,7 @@ int main(int argc, char * argv[]) { clitk::MotionMaskGenericFilter::Pointer genericFilter=clitk::MotionMaskGenericFilter::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkAverageTemporalDimension.cxx b/tools/clitkAverageTemporalDimension.cxx index d643454..6304aed 100644 --- a/tools/clitkAverageTemporalDimension.cxx +++ b/tools/clitkAverageTemporalDimension.cxx @@ -45,7 +45,7 @@ int main(int argc, char * argv[]) FilterType::Pointer genericFilter = FilterType::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkBinarizeImage.cxx b/tools/clitkBinarizeImage.cxx index 1bfc838..70d9992 100644 --- a/tools/clitkBinarizeImage.cxx +++ b/tools/clitkBinarizeImage.cxx @@ -34,11 +34,7 @@ int main(int argc, char * argv[]) filter->SetArgsInfo(args_info); - try { - filter->Update(); - } catch(std::runtime_error e) { - std::cout << e.what() << std::endl; - } + CLITK_TRY_CATCH_EXIT(filter->Update()); return EXIT_SUCCESS; } // This is the end, my friend diff --git a/tools/clitkCombineImage.cxx b/tools/clitkCombineImage.cxx index a78b131..ee8de0d 100644 --- a/tools/clitkCombineImage.cxx +++ b/tools/clitkCombineImage.cxx @@ -43,7 +43,7 @@ int main(int argc, char * argv[]) { clitk::CombineImageGenericFilter::Pointer genericFilter=clitk::CombineImageGenericFilter::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkComposeVF.cxx b/tools/clitkComposeVF.cxx index 08cff65..72b95cf 100644 --- a/tools/clitkComposeVF.cxx +++ b/tools/clitkComposeVF.cxx @@ -39,7 +39,7 @@ int main( int argc, char *argv[] ) //JV how to pass for different dims? //ComposeVFGenericFilter->SetEdgePaddingValue(args_info.pad_arg); ComposeVFGenericFilter->SetVerbose(args_info.verbose_flag); - ComposeVFGenericFilter->Update(); + CLITK_TRY_CATCH_EXIT(ComposeVFGenericFilter->Update()); return EXIT_SUCCESS; } diff --git a/tools/clitkImageArithm.cxx b/tools/clitkImageArithm.cxx index 61dbcb1..4cb22ca 100644 --- a/tools/clitkImageArithm.cxx +++ b/tools/clitkImageArithm.cxx @@ -43,7 +43,7 @@ int main(int argc, char * argv[]) // Go ! filter->SetArgsInfo(args_info); - filter->Update(); + CLITK_TRY_CATCH_EXIT(filter->Update()); // this is the end my friend return EXIT_SUCCESS; diff --git a/tools/clitkImageConvert.cxx b/tools/clitkImageConvert.cxx index 297b637..64bcfe1 100644 --- a/tools/clitkImageConvert.cxx +++ b/tools/clitkImageConvert.cxx @@ -60,7 +60,7 @@ int main(int argc, char * argv[]) if (args_info.type_given) filter->SetOutputPixelType(args_info.type_arg); // Go ! - filter->Update(); + CLITK_TRY_CATCH_EXIT(filter->Update()); // this is the end my friend return 0; diff --git a/tools/clitkInvertVF.cxx b/tools/clitkInvertVF.cxx index 0aec225..145863a 100644 --- a/tools/clitkInvertVF.cxx +++ b/tools/clitkInvertVF.cxx @@ -45,7 +45,7 @@ int main(int argc, char * argv[]) FilterType::Pointer genericFilter = FilterType::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkMedianTemporalDimension.cxx b/tools/clitkMedianTemporalDimension.cxx index 5af8a7f..cf50f68 100644 --- a/tools/clitkMedianTemporalDimension.cxx +++ b/tools/clitkMedianTemporalDimension.cxx @@ -45,7 +45,7 @@ int main(int argc, char * argv[]) FilterType::Pointer genericFilter = FilterType::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkResampleImage.cxx b/tools/clitkResampleImage.cxx index 661e183..bd2f374 100644 --- a/tools/clitkResampleImage.cxx +++ b/tools/clitkResampleImage.cxx @@ -33,7 +33,7 @@ int main(int argc, char * argv[]) FilterType::Pointer filter = FilterType::New(); filter->SetArgsInfo(args_info); - filter->Update(); + CLITK_TRY_CATCH_EXIT(filter->Update()); // this is the end my friend return EXIT_SUCCESS; diff --git a/tools/clitkSetBackground.cxx b/tools/clitkSetBackground.cxx index a44b4eb..b707999 100644 --- a/tools/clitkSetBackground.cxx +++ b/tools/clitkSetBackground.cxx @@ -34,7 +34,7 @@ int main(int argc, char * argv[]) clitk::SetBackgroundGenericFilter::Pointer genericFilter=clitk::SetBackgroundGenericFilter::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkWarpImage.cxx b/tools/clitkWarpImage.cxx index cf1dec0..3360661 100644 --- a/tools/clitkWarpImage.cxx +++ b/tools/clitkWarpImage.cxx @@ -44,7 +44,7 @@ int main(int argc, char * argv[]) clitk::WarpImageGenericFilter::Pointer genericFilter=clitk::WarpImageGenericFilter::New(); genericFilter->SetArgsInfo(args_info); - genericFilter->Update(); + CLITK_TRY_CATCH_EXIT(genericFilter->Update()); return EXIT_SUCCESS; }// end main diff --git a/tools/clitkZeroVF.cxx b/tools/clitkZeroVF.cxx index f19e567..52b3a2a 100644 --- a/tools/clitkZeroVF.cxx +++ b/tools/clitkZeroVF.cxx @@ -50,7 +50,7 @@ int main( int argc, char *argv[] ) zeroVFGenericFilter->SetVerbose(args_info.verbose_flag); //update - zeroVFGenericFilter->Update(); + CLITK_TRY_CATCH_EXIT(zeroVFGenericFilter->Update()); return EXIT_SUCCESS; } #endif