From f8469825e93cd553efa4da11f43a3ee933f949df Mon Sep 17 00:00:00 2001 From: andreanne-lemay Date: Thu, 2 Jul 2020 15:08:49 -0400 Subject: [PATCH] files --- register_data.sh | 71 ++++++++++++++++++++++++++++------------- verifyDim.py | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 verifyDim.py diff --git a/register_data.sh b/register_data.sh index 9efd353..430ffdf 100755 --- a/register_data.sh +++ b/register_data.sh @@ -22,42 +22,69 @@ trap "echo Caught Keyboard Interrupt within script. Exiting now.; exit" INT # Retrieve input params SUBJECT=$1 -FILEPARAM=$2 +#FILEPARAM=$2 # SCRIPT STARTS HERE # ============================================================================== # shellcheck disable=SC1090 -source $FILEPARAM +#source $FILEPARAM # Go to results folder, where most of the outputs will be located -cd $PATH_RESULTS +cd $PATH_RESULTS; # Copy source images and segmentations -mkdir -p data/derivatives/labels -cd data +mkdir -p data/derivatives/labels; +cd data; -cp -r $PATH_DATA/$SUBJECT . -cp -r $PATH_DATA/derivatives/labels/$SUBJECT $PATH_RESULTS/data/derivatives/labels +cp -r $PATH_DATA/$SUBJECT .; +cp -r $PATH_DATA/derivatives/labels/$SUBJECT $PATH_RESULTS/data/derivatives/labels; # Go to data folder -cd $PATH_RESULTS/data/$SUBJECT/anat/ +cd $PATH_RESULTS/data/$SUBJECT/anat/; ## Setup file names -file_t2w=${SUBJECT}_T2w -file_t1w=${SUBJECT}_T1w -path_t1w=`pwd`/${file_t1w}.nii.gz +file_t2w=${SUBJECT}_T2w; +file_t1w=${SUBJECT}_T1w; +path_t1w=`pwd`/${file_t1w}.nii.gz; if [ -f ${path_t1w} ]; then - sct_register_multimodal -i ${file_t1w}.nii.gz -d ${file_t2w}.nii.gz -x spline - mv ${file_t2w}_reg.nii.gz ${file_t2w}.nii.gz - mv ${file_t1w}_reg.nii.gz ${file_t1w}.nii.gz - warp_T2w2T1w=`pwd`/warp_${file_t2w}2${file_t1w}.nii.gz - warp_T1w2T2w=`pwd`/warp_${file_t1w}2${file_t2w}.nii.gz +# sct_resample -i ${file_t1w}.nii.gz -mm 0.2x0.2x0.5 -o ${file_t1w}.nii.gz; +# sct_resample -i ${file_t2w}.nii.gz -mm 0.2x0.2x0.5 -o ${file_t2w}.nii.gz; +# sct_crop_image -i ${file_t1w}.nii.gz -ref ${file_t2w}.nii.gz -o ${file_t1w}.nii.gz; +# sct_resample -i ${file_t1w}.nii.gz -mm 0.5x0.5x2 -o ${file_t1w}.nii.gz; +# sct_resample -i ${file_t2w}.nii.gz -mm 0.5x0.5x2 -o ${file_t2w}.nii.gz; +# sct_resample -i ${path_mask}${file_t2w}_centerline.nii.gz -mm 0.5x0.5x2 -o ${path_mask}${file_t2w}_centerline.nii.gz; + # step below to improve with dedicated optic model + sct_get_centerline -i ${file_t1w}.nii.gz -c t1 -method optic; + sct_create_mask -i ${file_t1w}.nii.gz -size 30mm -f cylinder -p centerline,${file_t1w}_centerline.nii.gz -o mask.nii.gz; +# sct_crop_image -i ${file_t1w}.nii.gz -m mask.nii.gz -o ${file_t1w}.nii.gz; +# sct_crop_image -i ${file_t2w}.nii.gz -m mask.nii.gz -o ${file_t2w}.nii.gz; + sct_register_multimodal -i ${file_t1w}.nii.gz -d ${file_t2w}.nii.gz -m mask.nii.gz -param step=1,type=im,algo=slicereg,metric=CC; +# + mv ${file_t2w}_reg.nii.gz ${file_t2w}.nii.gz; + mv ${file_t1w}_reg.nii.gz ${file_t1w}.nii.gz; + warp_T2w2T1w=`pwd`/warp_${file_t2w}2${file_t1w}.nii.gz; + warp_T1w2T2w=`pwd`/warp_${file_t1w}2${file_t2w}.nii.gz; fi -cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/ +cd $PATH_RESULTS/data/derivatives/labels/$SUBJECT/anat/; -file_t2w_seg=${SUBJECT}_T2w_seg-tumor -file_t1w_seg=${SUBJECT}_T1w_seg-tumor +file_t2w_seg=${SUBJECT}_T2w_seg-tumor; +file_t1w_seg=${SUBJECT}_T1w_seg-tumor; +file_t2w_edema=${SUBJECT}_T2w_edema; +file_t1w_edema=${SUBJECT}_T1w_edema; +path_edema=`pwd`/${file_t2w_edema}.nii.gz; if [ -f ${path_t1w} ]; then - sct_apply_transfo -i ${file_t1w_seg}.nii.gz -d ${file_t2w_seg}.nii.gz -w ${warp_T1w2T2w} -o ${file_t1w_seg}.nii.gz -x linear - sct_apply_transfo -i ${file_t2w_seg}.nii.gz -d ${file_t1w_seg}.nii.gz -w ${warp_T2w2T1w} -o ${file_t2w_seg}.nii.gz -x linear -fi \ No newline at end of file +# sct_resample -i ${file_t1w_seg}.nii.gz -mm 0.2x0.2x0.5 -o ${file_t1w_seg}.nii.gz; +# sct_crop_image -i ${file_t1w_seg}.nii.gz -ref $PATH_RESULTS/data/$SUBJECT/anat/${file_t2w}.nii.gz -o ${file_t1w_seg}.nii.gz; +# sct_resample -i ${file_t2w_seg}.nii.gz -mm 0.2x0.2x0.5 -o ${file_t2w_seg}.nii.gz; +# sct_crop_image -i ${file_t2w_seg}.nii.gz -ref $PATH_RESULTS/data/$SUBJECT/anat/${file_t2w}.nii.gz -o ${file_t2w_seg}.nii.gz; +# sct_resample -i ${file_t2w_seg}.nii.gz -mm 0.5x0.5x2 -o ${file_t2w_seg}.nii.gz; +# sct_resample -i ${file_t1w_seg}.nii.gz -mm 0.5x0.5x2 -o ${file_t1w_seg}.nii.gz; + sct_apply_transfo -i ${file_t1w_seg}.nii.gz -d ${file_t2w_seg}.nii.gz -w ${warp_T1w2T2w} -o ${file_t1w_seg}.nii.gz -x linear; + sct_apply_transfo -i ${file_t2w_seg}.nii.gz -d ${file_t1w_seg}.nii.gz -w ${warp_T2w2T1w} -o ${file_t2w_seg}.nii.gz -x linear; + if [ -f ${path_edema} ]; then + sct_apply_transfo -i ${file_t1w_edema}.nii.gz -d ${file_t2w_edema}.nii.gz -w ${warp_T1w2T2w} -o ${file_t1w_edema}.nii.gz -x linear; + sct_apply_transfo -i ${file_t2w_edema}.nii.gz -d ${file_t1w_edema}.nii.gz -w ${warp_T2w2T1w} -o ${file_t2w_edema}.nii.gz -x linear; +# sct_resample -i ${file_t2w_edema}.nii.gz -mm 0.5x0.5x2 -o ${file_t2w_edema}.nii.gz; +# sct_resample -i ${file_t1w_edema}.nii.gz -mm 0.5x0.5x2 -o ${file_t1w_edema}.nii.gz; + fi +fi diff --git a/verifyDim.py b/verifyDim.py new file mode 100644 index 0000000..1afe649 --- /dev/null +++ b/verifyDim.py @@ -0,0 +1,83 @@ +from __future__ import print_function + +import nibabel as nib +import numpy as np +from torch import nn +from bids_neuropoly import bids +from spinalcordtoolbox.image import Image +from shutil import copyfile +import os +from ivadomed.transforms import CenterCrop + +class DiceLoss(nn.Module): + """DiceLoss. + + .. seealso:: + Milletari, Fausto, Nassir Navab, and Seyed-Ahmad Ahmadi. "V-net: Fully convolutional neural networks for + volumetric medical image segmentation." 2016 fourth international conference on 3D vision (3DV). IEEE, 2016. + + Args: + smooth (float): Value to avoid division by zero when images and predictions are empty. + + Attributes: + smooth (float): Value to avoid division by zero when images and predictions are empty. + """ + def __init__(self, smooth=1.0): + super(DiceLoss, self).__init__() + self.smooth = smooth + + def forward(self, prediction, target): + iflat = prediction.reshape(-1) + tflat = target.reshape(-1) + intersection = (iflat * tflat).sum() + + return - (2.0 * intersection + self.smooth) / (iflat.sum() + tflat.sum() + self.smooth) + +# rootDataPath = "/home/andreanne/Documents/dataset/tumor_segmentation_masks/results/data" +rootDataPath = "/home/andreanne/Documents/dataset/tumor_segmentation_new_edema/results/data" +bids_ds = bids.BIDS(rootDataPath) +df = bids.BIDS(rootDataPath).participants.content +subject_lst = df['participant_id'].tolist() +contrast_lst = ["T2w"] +bids_subjects = [s for s in bids_ds.get_subjects() if s.record["subject_id"] in subject_lst] +max_dim = 0 +filename = 0 +dice_sum = 0 +counter = 0 +dirty = 0 +h_ = 0 +w_ = 0 +d_ = 0 + +for s in subject_lst: + img_shape = None + for subject in bids_subjects: + files = [] + if subject.record["subject_id"] == s: + if subject.record["modality"] in ["T2w"]: + input_file = subject.record.absolute_path + files.append(input_file) + files.append(input_file.replace("T2w", "T1w")) + derivatives = subject.get_derivatives("labels") + for deriv in derivatives: + if deriv.endswith(subject.record["modality"] + "_edema.nii.gz") or deriv.endswith(subject.record["modality"] + "_seg-tumor.nii.gz"): + files.append(deriv) + files.append(deriv.replace("T2w", "T1w")) + data = [] + for file in files: + if os.path.exists(file): + input_nii = nib.load(file) + data.append(input_nii.get_fdata().shape) + + if len(set(data)) != 1: + # print(files[0]) + transform = CenterCrop(size=data[0]) + for i in files: + if os.path.exists(i): + img_nii = nib.load(i) + img = img_nii.get_fdata() + cropped, _ = transform(img, {'crop_params': {}}) + print(cropped.shape) + nib_image = nib.Nifti1Image(cropped, img_nii.affine) + nib.save(nib_image, i) +