-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from spinalcordtoolbox/jca/19-update-cord-seg
Update `PAM50_cord.nii.gz` spinal cord segmentation
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.