From 3f2ae51729aacf6e9c47b7396f06bc12ff3384ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20K=C3=BCgler?= Date: Mon, 4 Dec 2023 14:26:54 +0100 Subject: [PATCH] Fix typos in realistic_deformations.sh Add apply_warp.py --- CerebNet/apply_warp.py | 85 +++++++++++++++++++++ CerebNet/datasets/realistic_deformations.sh | 8 +- 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 CerebNet/apply_warp.py diff --git a/CerebNet/apply_warp.py b/CerebNet/apply_warp.py new file mode 100644 index 00000000..5518b363 --- /dev/null +++ b/CerebNet/apply_warp.py @@ -0,0 +1,85 @@ + +# Copyright 2022 Image Analysis Lab, German Center for Neurodegenerative Diseases (DZNE), Bonn +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# IMPORTS +from os.path import join +import numpy as np +import nibabel as nib + +from CerebNet.datasets import utils + + +def save_nii_image(img_data, save_path, header, affine): + img_out = nib.Nifti1Image(img_data, header=header, affine=affine) + print(f"Saving {save_path}") + nib.save(img_out, save_path) + + +def store_warped_data(img_path, lbl_path, warp_path, result_path, patch_size): + + img, img_file = utils.load_reorient_rescale_image(img_path) + + lbl_file = nib.load(lbl_path) + label = np.asarray(lbl_file.get_fdata(), dtype=np.int16) + + warp_field = np.asarray(nib.load(warp_path).get_fdata()) + img = utils.map_size(img, base_shape=warp_field.shape[:3]) + label = utils.map_size(label, base_shape=warp_field.shape[:3]) + warped_img = utils.apply_warp_field(warp_field, img, interpol_order=3) + warped_lbl = utils.apply_warp_field(warp_field, label, interpol_order=0) + utils.map_subseg2label(warped_lbl, label_type='cereb_subseg') + roi = utils.bounding_volume(label, patch_size) + + img = utils.map_size(warped_img[roi], patch_size) + label = utils.map_size(warped_lbl[roi], patch_size) + + img_file.header['dim'][1:4] = patch_size + img_file.set_data_dtype(img.dtype) + lbl_file.header['dim'][1:4] = patch_size + save_nii_image(img, + join(result_path, "T1_warped_cropped.nii.gz"), + header=img_file.header, + affine=img_file.affine) + save_nii_image(label, + join(result_path, "label_warped_cropped.nii.gz"), + header=lbl_file.header, + affine=lbl_file.affine) + + +if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() + parser.add_argument("--img_path", + help="path to T1 image", + type=str) + parser.add_argument("--lbl_path", + help="path to label image", + type=str) + parser.add_argument("--result_path", + help="folder to store the results", + type=str) + + parser.add_argument("--warp_filename", + help="Warp field file", + default='1Warp.nii.gz', + type=str) + + args = parser.parse_args() + warp_path = join(args.result_path, args.warp_filename) + store_warped_data(args.img_path, + args.lbl_path, + warp_path=warp_path, + result_path=args.result_path, + patch_size=(128, 128, 128)) diff --git a/CerebNet/datasets/realistic_deformations.sh b/CerebNet/datasets/realistic_deformations.sh index efc5297a..0f0f4214 100644 --- a/CerebNet/datasets/realistic_deformations.sh +++ b/CerebNet/datasets/realistic_deformations.sh @@ -15,7 +15,7 @@ # IMPORTS -if ["$ANTSPATH" = "" ] || [ -f "$ANTSPATH/antsRegistrationSyNQuick.sh" ] +if [ "$ANTSPATH" = "" ] || [ -f "$ANTSPATH/antsRegistrationSyNQuick.sh" ] then exit "environment \$ANTSPATH not defined or invalid. \$ANTSPATH must contain antsRegistrationSyNQuick.sh." fi @@ -114,14 +114,16 @@ output_dir=$moving_dataroot/subj2subj_reg mkdir -p $output_dir if [ "$unlabeled_subject" != "UNDEFINED" ] +then IFS=',' read -r -a unlabeled_subject_array <<< "$unlabeled_subject" fi if [ "$labeled_subject" != "UNDEFINED" ] +then IFS=',' read -r -a labeled_subject_array <<< "$labeled_subject" fi -if [ ${#labeled_subject_array} != 1 ] && [ ${#unlabeled_subject_array} != 1 ] && [ ${#labeled_subject_array} != ${#unlabeled_subject_array}] +if [ ${#labeled_subject_array} != 1 ] && [ ${#unlabeled_subject_array} != 1 ] && [ ${#labeled_subject_array} != ${#unlabeled_subject_array} ] then exit "invalid parameters for labeled_subject and unlabeled_subject" fi @@ -179,7 +181,7 @@ do else echo "$result_path$output_warp already exists, skipping $sub1 -> $sub2." fi - elif + else echo "WARNING: $lab_img or $unlab_img does not exist, skipping $sub1 -> $sub2." fi