Error when using sct_register_to_template in SCT 6.1


I experience an issue with the latest SCT version when using sct_register_to_template.
Usually I do the following with my T1 weighted image (simplified):

sct_label_vertebrae -i anat.nii.gz -s anat_seg.nii.gz -c t1
sct_label_utils -i anat_seg_labeled_discs.nii.gz -keep 2,3,4,5,6,7,8 -o disc_labels.nii.gz

and then use these disc labels to register the anatomy to the template.

sct_register_to_template -i anat.nii.gz -s anat_seg.nii.gz -c t1 -ldisc disc_labels.nii.gz

This worked fine in my previous SCT version (I used 5.5) but crashes in 6.1 with the following traceback:

Traceback (most recent call last):
File β€œ/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/scripts/”, line 841, in
File β€œ/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/scripts/”, line 569, in main
File β€œ/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/”, line 387, in straighten
idx_closest = centerline_straight.get_closest_index(disc_label, relative_position,
File β€œ/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/”, line 477, in get_closest_index
closest_index = self.get_closest_to_absolute_position(closest_centerline_level,
File β€œ/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/”, line 505, in get_closest_to_absolute_position
backup_index_reference = backup_centerline.index_disc[reference_level]
KeyError: β€˜C1’

Thanks for your help in advance!

Hi @Ulrike,

Thank you for reporting this issue! My apologies for the inconvenience. This is a known issue (previously reported here), and we are in the process of fixing this for version 6.2 (see this fix).

The issue is caused by a combination of 3 factors:

  1. Using more than 2 disc labels
  2. Missing the disc label 1 (disc above C1 vertebrae)
  3. Spinal cord segmentation is present in slices above the topmost disc label (in your case, 2)

In the meantime, you could fix this issue by changing any one of the above factors:

  1. using 2 disc labels
  2. keeping label 1 (assuming it exists in your image)
  3. fetching the z slice of the topmost label and zeroing the segmentation above that slice

For solution 3, here is a set of commands that will perform the cropping for you:

# Fetch the labels from the image, then fetch the specific 'SI' slice
# Note: `-display` outputs labels in the form: x,y,z,value:x,y,z,value:etc.,
# so we can use the delimiters ':' and ',' to isolate 'z'
# ((This assumes image is in an orientation where SI axis == z axis (e.g. RPI)))
MAX_Z_SLICE=$(sct_label_utils -i disc_labels.nii.gz -display | tail -1 | cut -d ":" -f 1 | cut -d "," -f 3)
# Use the `-b 0` argument to zero the segmentation above the chosen z slice
sct_crop_image -i anat_seg.nii.gz -zmax $MAX_Z_SLICE -b 0 -o anat_seg_cropped.nii.gz

If none of these solutions are feasible for you, please let me know, and I will do my best to provide a more satisfactory short-term workaround (at least until v6.2 is released).

Thank you for your patience,

1 Like

Hi Joshua,

thank you for the quick and super helpful response! I see now where the problem came from.
Using just 2 labels works like a charm!
I will also try the cropping as I thought it might be nicer to give as much subject information as possible and using more labels? But then again maybe the algorithm tries to solve tiny local problems when registering and might sometimes give worse results as well?
What is the current advice regarding the number of labels?
Maybe I will try both ways on a dataset and see how it works out.

Thanks again for the lightning-fast response!

1 Like

A summary of the pros and cons can be found here: How many vertebral labels should I use for registration? - Spinal Cord Toolbox documentation. I think that there is a very good chance that using 2 labels would be sufficient and perhaps even preferable to 3+ labels. :slight_smile:

I can also respond to this forum post once v6.2 is released (which should be some time this month), so that you can try the fixed 3+ labels registration when it’s available again.

Kind regards,

1 Like

As a quick update: SCT v6.2 has been released, and it contains a fix for the registration issue mentioned in this post. :slight_smile:

Kind regards,

1 Like