How to add custom tracts when using `sct_analyze_lesion -f`

Dear SCT Team
I would like to use sct_analyze_lesiont to compute statistics on segmented lesions, the calculation gives the proportions of each ROI occupied by lesions.But the only ROIs obtained are some pre-set metrics, including PAM50_0-35,WM,GM,DC,LF,VF, What should I do if I want to add custom tracts, such as ascending tracts,the right and left hemi-cord,etc.? I’ve tried adding custom tracts to the info_lable, as shown in the picture,but that doesn’t work.



In addition, if I understand correctly, this function is calculated for volume, Is it possible to compute the proportions of each ROI (e.g. GM, WM) occupied by lesions on per axial slice?
Thanks!

Hi @willimon,

Thank you very much for your questions!

What should I do if I want to add custom tracts, such as ascending tracts,the right and left hemi-cord,etc.? I’ve tried adding custom tracts to the info_lable, as shown in the picture,but that doesn’t work.

Unfortunately, only the “main” ROI files (labels 0:35) are used for sct_analyze_lesion distribution computation. In fact, even the predefined ROIs in “CombinedLabels” (50-55) are not currently considered for sct_analyze_lesion.

I have opened an issue for your feature request:

That being said, I think it would be fairly simple to create a small utility script in Python that “postprocesses” the lesion_analysis.xls file and adds new columns that sum the percentage values corresponding to those within the CombinedLabels. That way, you can calculate the values right now, and don’t have to wait until this feature is added to SCT, nor do you have to fuss with spreadsheet formulas and etc.

If this would be useful for you, I would be happy to write this utility script + provide instructions on how to use it. :slight_smile:


In addition, if I understand correctly, this function is calculated for volume, Is it possible to compute the proportions of each ROI (e.g. GM, WM) occupied by lesions on per axial slice?

If you look at the lesion_analysis.xls file once more, there is actually a tab called “ROI_occupied_by_lesion” that is very close to what you are requesting.

Unfortunately, rather than being “per axial slice” as you’ve requested, this table is aggregated by vertebral level. So, in order to meet your request, we would need to change the rows to be “slicewise” instead.

As it happens, there is already an open issue related to this feature request:

Given that this previous feature request was also tied to a user’s post on the forum, I will increase the priority of the issue. :slight_smile:

Kind regards,
Joshua

Grateful for that! This is exactly what I needed!

Happy to help!

Here is the script I’ve written: sum_combined_labels.py (3.5 KB)

To use this script, you just need to do the following:

# make the script executable (only needs to be done once)
chmod +x sum_combined_labels.py

# install 1 additional dependency in SCT (only needs to be done once)
"$SCT_DIR/python/envs/venv_sct/bin/pip" install openpyxl

# run the script, pointed at your XLS file and info_label.txt file
"$SCT_DIR/python/envs/venv_sct/bin/python" sum_combined_labels.py -x lesion_analysis.xls -i info_label.txt -o output.xls

Instructions are also included in the text of the script itself, so feel free to skim the script to get a sense of what it’s doing.

I have tried this on a sample lesion_analysis.xls file and the output looked good to me. But, please let me know if you run into any issues.

If this works for you, then it should be straightforward to add this exact functionality into sct_analyze_lesion -f, too. :slight_smile:

Kind regards,
Joshua

1 Like

Hi Joshua,

Thanks for the script. I am new to SCT and python.

Firstly,I downloaded the script, should I move the script to the SCT folder?

Secondly about the openpyxl installation, when I run the command “$SCT_DIR/python/envs/venv_sct/bin/pip” install openpyxl with git bash, it shows:bash: D:\newsct/python/envs/venv_sct/bin/pip: No such file or directory

I didn’t actually find the bin folder in the venv_sct folder. I’m not sure what’s wrong, The version I’m using is version 6.2, running on windows

Firstly,I downloaded the script, should I move the script to the SCT folder?

There is no need, thankfully! As long as you run the script using the “SCT_DIR” command I shared, the script can be placed anywhere. :slight_smile:

it shows:bash: D:\newsct/python/envs/venv_sct/bin/pip: No such file or directory

Ah, my apologies! I made the assumption that you were on macOS or Linux, but it looks like you are on Windows. Terribly sorry about that!

In that case, as you correctly identified, there is no bin/ folder. Instead, on Windows, the folder is called “Scripts” (this is beyond our control, sadly). So, you will need to use these commands instead:

# install 1 additional dependency in SCT (only needs to be done once)
"$SCT_DIR\python\envs\venv_sct\Scripts\pip.exe" install openpyxl

# run the script, pointed at your XLS file and info_label.txt file
"$SCT_DIR\python\envs\venv_sct\Scripts\python.exe" sum_combined_labels.py -x lesion_analysis.xls -i info_label.txt -o output.xls

Please let me know if you run into any other issues and I will be happy to help. :slight_smile:

Kind regards,
Joshua

Thank you very much! I got what I wanted to add in lesion#1_distribution, but I was actually more concerned about ROI_occupied_by_lesion, and still didn’t get what I wanted here,is there a good solution please?


Also, in some patients with spinal cord injuries, the SCT divides the lesion into multiple parts counted separately, is there a way to get these combined?
1

Hi @willimon,

I was actually more concerned about ROI_occupied_by_lesion, and still didn’t get what I wanted here,is there a good solution please?

My apologies, but adding CombinedLabels to the ROI page is not as straightforward.

  • For the lesion_distribution page, since we are dealing with “percentage of lesion”, the sub-percentages can just be summed.
  • For the ROI_occupied_by_lesion page, since we are dealing with “percentage of ROI”, the percentages are dependent on the ROI volume, which would change for the “CombinedLabels” ROIs. Since the sheet doesn’t contain volume values, the processing required is bit more complex (reading the ROI image files, measuring volume, recalculating percentages, etc.)

At that point, it may be easier to implement the functionality directly within sct_analyze_lesion (rather than as a “postprocessing script” like I’ve written). Thankfully, SCT is preparing to release a new version (6.4) shortly. Would you be able to wait ~2wk for a new release containing this feature?

Also, in some patients with spinal cord injuries, the SCT divides the lesion into multiple parts counted separately, is there a way to get these combined?

SCT uses “connected components labeling” to label the segmentation. If the lesion is labeled with multiple values, then it means the parts of the lesion are not connected.

While it would be possible for SCT to treat these unconnected regions as a single region, I would be cautious about this, since “multiple labels” could be due to many reasons:

  • Poor/missing segmentation of a single lesion
  • False-positive segmentation of non-lesion areas
  • Multiple true-positive lesions

Merging the areas in this case might be inaccurate. So, I think that if you expect a single lesion, but get multiple labels, you might want to perform some manual QC + correction of the segmentation if necessary (either by filling in the mis-segmented areas, removing false positives, etc.).

That said, it would be fairly easy to add a new flag to sct_analyze_lesion called e.g. “-single-lesion” which would skip the “labeling” step and treat the entire segmentation as one lesion. (This too could be added for SCT v6.4.)

Kind regards,
Joshua

Thank you very much for your help! Look forward to seeing these new features in version 6.4!

Dear @willimon,

SCT v6.4 has been released, with support for custom CombinedLabels entries in info_label.txt, as well as a -perslice option for slicewise metrics (rather than per-vertebral level).

Kind regards,
Joshua

Thank you very much for the timely update! I will try the new version now