From fc36396770fa18bb3010fc95a6e9386c06e2efb0 Mon Sep 17 00:00:00 2001 From: Rohan Banerjee Date: Tue, 9 Jul 2024 00:03:02 -0400 Subject: [PATCH] added scripts --- scripts/symmetrize_cord_segmentation.py | 41 +++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scripts/symmetrize_cord_segmentation.py diff --git a/scripts/symmetrize_cord_segmentation.py b/scripts/symmetrize_cord_segmentation.py new file mode 100644 index 0000000..852b03b --- /dev/null +++ b/scripts/symmetrize_cord_segmentation.py @@ -0,0 +1,41 @@ +#!/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("/Users/rohanbanerjee/Downloads/avg014_template_updated/template/templatedog_cord.nii.gz") +data_seg = nii_seg.get_fdata() +print(data_seg.shape) + +# Symmetrize image by copying the right to the left +data_seg[51:, ...] = np.flip(data_seg[:50, ...], 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}")