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):
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/sct_register_to_template.pyβ, line 841, in
main(sys.argv[1:])
File β/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/scripts/sct_register_to_template.pyβ, line 569, in main
sc_straight.straighten()
File β/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/straightening.pyβ, line 387, in straighten
idx_closest = centerline_straight.get_closest_index(disc_label, relative_position,
File β/data/u_uhorn_software/sct_6.1/spinalcordtoolbox/types.pyβ, 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/types.pyβ, line 505, in get_closest_to_absolute_position
backup_index_reference = backup_centerline.index_disc[reference_level]
KeyError: βC1β
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:
Using more than 2 disc labels
Missing the disc label 1 (disc above C1 vertebrae)
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:
using 2 disc labels
keeping label 1 (assuming it exists in your image)
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 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.
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.