Skip to content

Commit

Permalink
Merge pull request #415 from dkuegler/fix/cerebnet/typos
Browse files Browse the repository at this point in the history
"Small" changes to the CerebNet traning pipeline (missing apply_warp.py and shell errors in realistic_deformations)
  • Loading branch information
dkuegler authored Dec 14, 2023
2 parents f87b429 + 3f2ae51 commit 2c24b04
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 3 deletions.
85 changes: 85 additions & 0 deletions CerebNet/apply_warp.py
Original file line number Diff line number Diff line change
@@ -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))
8 changes: 5 additions & 3 deletions CerebNet/datasets/realistic_deformations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 2c24b04

Please sign in to comment.