Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new CLI script to predict compression probability #4760

Merged
merged 55 commits into from
Jan 22, 2025

Conversation

valosekj
Copy link
Member

@valosekj valosekj commented Dec 28, 2024

Checklist

GitHub

PR contents

Description

This PR adds a script to predict compression probability in a spinal cord MRI image using spinal cord shape metrics. The script requires spinal cord segmentation (used to compute the shape metrics) and disc labels.

Briefly, the script computes shape metrics for 3 axial slices at intervertebral disc levels C3/C4 to C6/7 (3 slices are used to compensate for potential disc label shift in the superior-inferior (S-I) axis; details: #4760 (comment) and 90cbfa6). Then, it predicts compression probability for each of these axial slices using a logistic regression model. The compression probability is printed to CLI and saved to a CSV file; see the next section for examples. More details in: https://pubmed.ncbi.nlm.nih.gov/35371944/. Particularly, equation 4.

How to test this PR

# Download the script
cd $SCT_DIR
git fetch
git checkout jv/add_sct_detect_compression

# Activate SCT python
cd $SCT_DIR
source ./python/etc/profile.d/conda.sh
conda activate venv_sct

# Run the script
python $SCT_DIR/spinalcordtoolbox/scripts/sct_detect_compression.py -s T2w_seg.nii.gz -discfile T2w_discs.nii.gz

Testing on a few spine-generic subjects with mild compression

sub-tokyoIngenia02 image
cd ~/data/data.neuro.polymtl.ca/data-multi-subject/derivatives/labels/sub-tokyoIngenia02/anat
python $SCT_DIR/spinalcordtoolbox/scripts/sct_detect_compression.py -s sub-tokyoIngenia02_space-other_T2w_label-SC_seg.nii.gz -discfile sub-tokyoIngenia02_T2w_label-discs_dlabel.nii.gz
...
Results saved to: ~/data/data.neuro.polymtl.ca/data-multi-subject/derivatives/labels/sub-tokyoIngenia02/anat/sub-tokyoIngenia02_space-other_T2w_label-SC_seg_compression_results.csv
Disc C3/C4, compression probability: no
Disc C4/C5, compression probability: yes
Disc C5/C6, compression probability: yes
Disc C6/C7, compression probability: yes

Output CSV:
image

sub-ucl05 image
cd ~/data/data.neuro.polymtl.ca/data-multi-subject/derivatives/labels/sub-ucl05/anat
python $SCT_DIR/spinalcordtoolbox/scripts/sct_detect_compression.py -s sub-ucl05_space-other_T2w_label-SC_seg.nii.gz -discfile sub-ucl05_T2w_label-discs_dlabel.nii.gz
...
Results saved to: ~/data/data.neuro.polymtl.ca/data-multi-subject/derivatives/labels/sub-ucl05/anat/sub-ucl05_space-other_T2w_label-SC_seg_compression_results.csv
Disc C3/C4, compression probability: no
Disc C4/C5, compression probability: no
Disc C5/C6, compression probability: yes
Disc C6/C7, compression probability: possible

Output CSV:
image

sub-vuiisAchieva01 image
cd  ~/data/data.neuro.polymtl.ca/data-multi-subject/derivatives/labels/sub-vuiisAchieva01/anat
python $SCT_DIR/spinalcordtoolbox/scripts/sct_detect_compression.py -s sub-vuiisAchieva01_space-other_T2w_label-SC_seg.nii.gz -discfile sub-vuiisAchieva01_T2w_label-discs_dlabel.nii.gz
...
Results saved to: ~/data/data.neuro.polymtl.ca/data-multi-subject/derivatives/labels/sub-vuiisAchieva01/anat/sub-vuiisAchieva01_space-other_T2w_label-SC_seg_compression_results.csv
Disc C3/C4, compression probability: yes
Disc C4/C5, compression probability: no
Disc C5/C6, compression probability: no
Disc C6/C7, compression probability: no

Output CSV:
image

Note

Note that the predictive model was trained using axial T2star images from a private dataset. The example spine-generic images shown above are T2w 0.8mm iso. I used these images because they are open-access to verify that the script works properly.
(I did not choose spine-generic T2star images as they cover only C2-C5, while the model detects compression also at discs C5/6 and C6/7).

Linked issues

Follow-up of: 32153ad

@valosekj valosekj added the feature category: new functionality label Dec 28, 2024
As torsion requires 3 slices above and below to be computed, it can be None at the FOV top and bottom.
Use these cut-offs to determine compression probability category and include the category into the output CSV as a new column.
Because we are talking about the compression probability. "Mild" might imply compression severity.
@sandrinebedard
Copy link
Member

I tested on 2 subjects from stanford:
Works well I think!

sub-NSDCM001

image

csv result:
image

C3/C4:
image

C4/C5:
image

C5/C6:
image

C6/C7
image

sub-NSDCM002

image

CSV file:
image

C3/C4
image

C4/C5:
image

C5/C6:
image

C6/C7:
image

@sandrinebedard sandrinebedard self-requested a review January 10, 2025 20:17
@valosekj
Copy link
Member Author

Just writing it here not to forget: currently, the function outputs a separate CSV file for each subject. However, we might consider the append option, similarly as done by sct_process_segmetnation. In other words, compression probabilities across subjects would be aggregated into a single CSV.
If we decide on that, we would need to add Timestamp, SCT Version, Filename columns to this script to distinguish files across subjects (as done by sct_process_segmetnation):

image

Copy link
Member

@joshuacwnewton joshuacwnewton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR is really close to being merged! Great work again, thank you. ♥️

spinalcordtoolbox/scripts/sct_detect_compression.py Outdated Show resolved Hide resolved
spinalcordtoolbox/scripts/sct_detect_compression.py Outdated Show resolved Hide resolved
spinalcordtoolbox/scripts/sct_detect_compression.py Outdated Show resolved Hide resolved
testing/cli/test_cli_sct_detect_compression.py Outdated Show resolved Hide resolved
setup.py Show resolved Hide resolved
Copy link
Member

@joshuacwnewton joshuacwnewton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once the reference formatting nitpick is complete, I think this is good to go!

@valosekj valosekj merged commit 8a6b609 into master Jan 22, 2025
21 checks passed
@valosekj valosekj deleted the jv/add_sct_detect_compression branch January 22, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature category: new functionality sct_detect_compression context:
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants