diff --git a/scripts/label_segmentation.py b/scripts/label_segmentation.py new file mode 100644 index 0000000..21a580c --- /dev/null +++ b/scripts/label_segmentation.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# This script labels the spinal cord segmentation using the disc labels. +# +# For more context, see: https://github.com/spinalcordtoolbox/PAM50/pull/21 +# +# How to run: +# cd where this script is located and run: +# python label_segmentation.py +# +# Author: Julien Cohen-Adad + +import numpy as np +import nibabel as nib + + +# Open PAM50 spinal cord segmentation +nii_seg = nib.load("../template/PAM50_cord.nii.gz") + +# Open disc labels +nii_discs = nib.load("../template/PAM50_label_disc.nii.gz") +data_discs = nii_discs.get_fdata() + +# Iterate across z-slices and assign proper vertebral level value +data_seg_labeled = nii_seg.get_fdata() +nx, ny, nz = data_seg_labeled.shape +disc_value = 0 +# Note: iteration is from superior (highest slice) to inferior (slice=0) +for iz in range(nz-1, 0, -1): + data_seg_labeled[..., iz] *= disc_value + # Check if there is a disc at this slice + if np.any(data_discs[..., iz]): + # If so, update disc_value + disc_value += 1 + +# Use proper dtype +data_seg_labeled = np.uint8(data_seg_labeled) +# Note: here we assume that PAM50_cord is also UINT8, hence there is no need to modify the header + +# Save file +nii_seg_new = nib.Nifti1Image(data_seg_labeled, nii_seg.affine) +fname_out = "PAM50_levels_new.nii.gz" +nib.save(nii_seg_new, fname_out) + +print(f"Done! 🎉 \nFile created: {fname_out}") diff --git a/scripts/symmetrize_cord_segmentation.py b/scripts/symmetrize_cord_segmentation.py new file mode 100644 index 0000000..29f8fef --- /dev/null +++ b/scripts/symmetrize_cord_segmentation.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# This script creates a symmetrical image by copying the information from the right side of the image +# to the left side. +# +# It is particularly useful when manually correcting a spinal cord segmentation, because only the right +# part needs to be corrected, and then this script is run to correct the left part. +# +# For more context, see: https://github.com/spinalcordtoolbox/PAM50/issues/19 +# +# How to run: +# cd where this script is located and run: +# python symmetrize_cord_segmentation.py +# +# Author: Julien Cohen-Adad + +import numpy as np +import nibabel as nib + + +# Open PAM50 spinal cord segmentation +nii_seg = nib.load("../template/PAM50_cord.nii.gz") +data_seg = nii_seg.get_fdata() + +# Symmetrize image by copying the right to the left +data_seg[71:, ...] = np.flip(data_seg[:70, ...], axis=0) + +# Use proper dtype +data_seg = np.uint8(data_seg) +header_seg = nii_seg.header.copy() +header_seg.set_data_dtype(np.uint8) + +# Save file +# nii_seg_new = copy.deepcopy(nii_seg) +nii_seg_new = nib.Nifti1Image(data_seg, nii_seg.affine, header_seg) +fname_out = "PAM50_cord_new.nii.gz" +nib.save(nii_seg_new, fname_out) + +print(f"Done! 🎉 \nFile created: {fname_out}") diff --git a/template/PAM50_cord.nii.gz b/template/PAM50_cord.nii.gz index df6c647..ca45801 100644 Binary files a/template/PAM50_cord.nii.gz and b/template/PAM50_cord.nii.gz differ diff --git a/template/PAM50_levels.nii.gz b/template/PAM50_levels.nii.gz index 40b051b..c866778 100644 Binary files a/template/PAM50_levels.nii.gz and b/template/PAM50_levels.nii.gz differ