Skip to content

Commit

Permalink
Merge pull request #21 from spinalcordtoolbox/jca/19-update-cord-seg
Browse files Browse the repository at this point in the history
Update `PAM50_cord.nii.gz` spinal cord segmentation
  • Loading branch information
jcohenadad authored Sep 22, 2023
2 parents 79de707 + 0b32914 commit cfe8ff2
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
46 changes: 46 additions & 0 deletions scripts/label_segmentation.py
Original file line number Diff line number Diff line change
@@ -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}")
40 changes: 40 additions & 0 deletions scripts/symmetrize_cord_segmentation.py
Original file line number Diff line number Diff line change
@@ -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}")
Binary file modified template/PAM50_cord.nii.gz
Binary file not shown.
Binary file modified template/PAM50_levels.nii.gz
Binary file not shown.

0 comments on commit cfe8ff2

Please sign in to comment.