spinalcordtoolbox icon indicating copy to clipboard operation
spinalcordtoolbox copied to clipboard

Unintuitive `IndexError` during C2-C3 detection when using cropped spinal cord segmentation

Open joshuacwnewton opened this issue 1 year ago • 1 comments

While investigating #4085, I wanted to use sct_label_vertebrae to compare SCT-generated labels (vert body) with the existing manual labels (posterior tip of disc), to see how label positioning can affect registration results.

However, when running the following command:

sct_label_vertebrae -i sub-errsm37_T1w.nii.gz -s sub-errsm37_T1w_seg_cropz.nii.gz -c t1

I ran into the following error:

Run C2-C3 detector...
Traceback (most recent call last):
  File "/home/joshua/repos/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_label_vertebrae.py", line 494, in <module>
    main(sys.argv[1:])
  File "/home/joshua/repos/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_label_vertebrae.py", line 378, in main
    im_label_c2c3 = detect_c2c3(im_data, im_seg, contrast, verbose=verbose_detect_c2c3)
  File "/home/joshua/repos/spinalcordtoolbox/spinalcordtoolbox/vertebrae/detect_c2c3.py", line 109, in detect_c2c3
    pred[midSlice_mask == 0] = 0
IndexError: boolean index did not match indexed array along dimension 1; dimension is 722 but corresponding boolean dimension is 535

I have a feeling this is just due to the cropping of the bottom of the segmentation image (https://github.com/spinalcordtoolbox/spinalcordtoolbox/issues/4085#issuecomment-1501171469).

Still, this isn't the most intuitive error message, so perhaps we should do some basic input validation, e.g. ensuring that the segmentation image has the same dimensions as the anat image, assuming that requirement is mandatory.

joshuacwnewton avatar Jun 15 '23 18:06 joshuacwnewton

I have a feeling this is just due to the cropping of the bottom of the segmentation image (#4085 (comment)).

Yep, this is the problem.

If I instead crop the image using the -b 0 option, then the dimensions are preserved, and thus there is no IndexError.

joshuacwnewton avatar Jun 15 '23 18:06 joshuacwnewton