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