xfm_generation.sh 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env bash
  2. # Generates template space transformation assets via ANTs
  3. #
  4. # Synchon Mandal 2024, Forschungszentrum Juelich GmbH
  5. # Exit when a command fails
  6. set -o errexit
  7. # Fail when accessing unset variables
  8. set -o nounset
  9. # Fail when even one command in pipeline fails
  10. set -o pipefail
  11. # Allow debug by using `TRACE=1 ./xfm_generation.sh`
  12. [[ "${TRACE-0}" == "1" ]] && set -o xtrace
  13. # Help command
  14. if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
  15. cat <<USAGE
  16. Generates human template space transformation assets via ANTs. Template space
  17. names are according to `templateflow <https://www.templateflow.org/browse/>`
  18. without the `tpl-`.
  19. Usage:
  20. ./xfm_generation.sh -f <src-template-space-t1w> -t <target-template-space-t1w>
  21. -f <src-template-space-t1w> Source template space (e.g., MNI152NLin2009cAsym)
  22. -t <target-template-space-t1w> Target template space (e.g., MNI152NLin6Asym)
  23. USAGE
  24. exit
  25. fi
  26. # Logging helper function
  27. logit() {
  28. echo "$(date -u) $1";
  29. }
  30. # Assert datalad exists
  31. assert_datalad_exists() {
  32. command -v datalad &> /dev/null || { logit "ERROR Datalad could not be found!"; exit 1; }
  33. logit "INFO Datalad found, proceeding to get data...";
  34. }
  35. # Assert antsRegistration exists
  36. assert_antsRegistration_exists() {
  37. command -v antsRegistration &> /dev/null || { logit "ERROR antsRegistration could not be found!"; exit 1; }
  38. logit "INFO antsRegistration found, proceeding...";
  39. }
  40. main() {
  41. while getopts ":f:t:" opt; do
  42. case $opt in
  43. f)
  44. src_path=$OPTARG
  45. logit "DEBUG Source template path: ${src_path}"
  46. ;;
  47. t)
  48. target_path=$OPTARG
  49. logit "DEBUG Target template path: ${target_path}"
  50. ;;
  51. \?)
  52. logit "ERROR Invalid option: -$OPTARG"
  53. exit 1
  54. ;;
  55. :)
  56. logit "ERROR Option -$OPTARG requires an argument"
  57. exit 1
  58. ;;
  59. esac
  60. done
  61. assert_datalad_exists;
  62. assert_antsRegistration_exists;
  63. src_name=$(basename $(dirname $src_path));
  64. IFS="-"; arr_src_name=($src_name); unset IFS;
  65. target_name=$(basename $(dirname $target_path));
  66. IFS="-"; arr_target_name=($target_name); unset IFS;
  67. output_dir_prefix="${arr_src_name[1]}_to_${arr_target_name[1]}";
  68. logit "DEBUG Output directory prefix: ${output_dir_prefix}";
  69. # Resolve symlinks
  70. full_src_path=$(realpath "$src_path");
  71. logit "DEBUG Resolved source template path: ${full_src_path}";
  72. full_target_path=$(realpath "$target_path");
  73. logit "DEBUG Resolved target template_path: ${full_target_path}";
  74. # Create output directory if not found and change directory
  75. mkdir -p "xfms/${output_dir_prefix}";
  76. cd "${PWD}/xfms/${output_dir_prefix}";
  77. antsRegistration \
  78. --verbose 1 \
  79. --dimensionality 3 \
  80. --float 0 \
  81. --collapse-output-transforms 1 \
  82. --output $output_dir_prefix \
  83. --interpolation LanczosWindowedSinc \
  84. --use-histogram-matching 0 \
  85. --winsorize-image-intensities [ 0.005,0.995 ] \
  86. --initial-moving-transform [ $full_src_path,$full_target_path,1 ] \
  87. --transform Rigid[ 0.1 ] \
  88. --metric MI[ $full_src_path,$full_target_path,1,32,Regular,0.25 ] \
  89. --convergence [ 1000x500x250x0,1e-6,10 ] \
  90. --shrink-factors 8x4x2x1 \
  91. --smoothing-sigmas 3x2x1x0vox \
  92. --transform Affine[ 0.1 ] \
  93. --metric MI[ $full_src_path,$full_target_path,1,32,Regular,0.25 ] \
  94. --convergence [ 1000x500x250x0,1e-6,10 ] \
  95. --shrink-factors 8x4x2x1 \
  96. --smoothing-sigmas 3x2x1x0vox \
  97. --transform SyN[ 0.1,3,0 ] \
  98. --metric MI[ $full_src_path,$full_target_path,1,32] \
  99. --convergence [ 100x70x50x0,1e-6,10 ] \
  100. --shrink-factors 8x4x2x1 \
  101. --smoothing-sigmas 3x2x1x0vox \
  102. --write-composite-transform 1
  103. # Change to root
  104. cd ../../;
  105. logit "INFO Done. Data in ${PWD}/xfms";
  106. }
  107. main "$@"