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
-#!/bin/sh -x
+#!/bin/sh
###############################################################################
#
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...
# 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}_
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
# 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_*
-#! /bin/bash -x
-
+#! /bin/bash
+
###############################################################################
#
# FILE: create_midP-2.0.sh
{
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()
{
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
}
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
}
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]}
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
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
-#! /bin/sh +x
+#! /bin/sh -x
###############################################################################
#
#
###############################################################################
+#
+# 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
#
###############################################################################
+source `dirname $0`/midp_common.sh
+
################# BLUTDIR #####################
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 #####################
# 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
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
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
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
//-----------------------------------------------------------
// Constructor
//-----------------------------------------------------------
- ExtractPatientGenericFilter::ExtractPatientGenericFilter()
+ ExtractPatientGenericFilter:::ExtractPatientGenericFilter()
{
m_Verbose=false;
m_InputFileName="";
// 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
clitk::MotionMaskGenericFilter::Pointer genericFilter=clitk::MotionMaskGenericFilter::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
FilterType::Pointer genericFilter = FilterType::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
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
clitk::CombineImageGenericFilter::Pointer genericFilter=clitk::CombineImageGenericFilter::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
//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;
}
// Go !
filter->SetArgsInfo(args_info);
- filter->Update();
+ CLITK_TRY_CATCH_EXIT(filter->Update());
// this is the end my friend
return EXIT_SUCCESS;
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;
FilterType::Pointer genericFilter = FilterType::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
FilterType::Pointer genericFilter = FilterType::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
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;
clitk::SetBackgroundGenericFilter::Pointer genericFilter=clitk::SetBackgroundGenericFilter::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
clitk::WarpImageGenericFilter::Pointer genericFilter=clitk::WarpImageGenericFilter::New();
genericFilter->SetArgsInfo(args_info);
- genericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(genericFilter->Update());
return EXIT_SUCCESS;
}// end main
zeroVFGenericFilter->SetVerbose(args_info.verbose_flag);
//update
- zeroVFGenericFilter->Update();
+ CLITK_TRY_CATCH_EXIT(zeroVFGenericFilter->Update());
return EXIT_SUCCESS;
}
#endif