Sct_label_vertebrae fails v 4.0.0

I ran the sct_label_vertebrae on a t2w file using a seg file created using deepseg -

things appear to be processing routinely (output attached) - but after a minute or so - I get the following error:

(sct-env) david@ubuntu-work-vm:/mnt/workspace/sct_june2019/sct_dev/sub-ID03/anat$ sct_label_vertebrae -i sub-ID03_T2w.nii.gz -s deepseg/sub-ID03_T2w_seg.nii.gz -c t2

--
Spinal Cord Toolbox (dev)


Create temporary folder (/tmp/sct-20190911102136.956829-label_vertebrae-epldo_nk)...

Copying input data to tmp folder...

Straighten spinal cord...

Create temporary folder (/tmp/sct-20190911102137.936778-straighten_spinalcord-vv7u8gdj)...

Copy files to tmp folder...
Window length needs to be >= 3. Returning input signal.
Window length needs to be >= 3. Returning input signal.
Fitting centerline using B-spline approximation
Error on approximation = 9.64 mm
Error on approximation = 5.07 mm
Error on approximation = 2.56 mm
Error on approximation = 0.72 mm
Error on approximation = 0.53 mm
Error on approximation = 0.38 mm
Error on approximation = 0.25 mm
Error on approximation = 0.18 mm
Error on approximation = 0.13 mm
Error on approximation = 0.12 mm
Error on approximation = 0.12 mm
Error on approximation = 0.12 mm
Error on approximation = 0.13 mm
Error on approximation = 0.15 mm
Error on approximation = 0.16 mm
Error on approximation = 0.18 mm
Error on approximation = 0.18 mm
Error on approximation = 0.2 mm
Error on approximation = 0.19 mm
Error on approximation = 0.2 mm
Error on approximation = 0.17 mm
Error on approximation = 0.18 mm
Error on approximation = 0.16 mm
Error on approximation = 0.17 mm
Error on approximation = 0.15 mm
Error on approximation = 0.17 mm
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Singular Matrix in NURBS algorithm -> wrong reconstruction
Create the straight space and the safe zone
Length of spinal cord: 168.1520834395413
Size of spinal cord in z direction: 160.20716225354036
Ratio length/size: 1.0495915480571802
Safe zone boundaries (curved space): [-69.61428680259067, 90.59287545094969]
Safe zone boundaries (straight space): [-73.58674739559113, 94.56533604395015]
Pad input volume to account for spinal cord length...
Time to generate centerline: 59.0 ms
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 216/216 [00:12<00:00, 15.66it/s]
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 272/272 [00:54<00:00,  6.46it/s]
Warping field generated: tmp.curve2straight.nii.gz
Warping field generated: tmp.straight2curve.nii.gz
Apply transformation to input image...
/mnt/workspace/sct_dev/bin/isct_antsApplyTransforms -d 3 -r tmp.centerline_pad_crop.nii.gz -i data.nii -o tmp.anat_rigid_warp.nii.gz -t tmp.curve2straight.nii.gz -n 'BSpline[3]' # in /tmp/sct-20190911102137.936778-straighten_spinalcord-vv7u8gdj
Generate output files...
  File created: ./warp_curve2straight.nii.gz
  File created: ./warp_straight2curve.nii.gz
cp /tmp/sct-20190911102137.936778-straighten_spinalcord-vv7u8gdj/tmp.anat_rigid_warp.nii.gz ./straight_ref.nii.gz
sct_convert -i /tmp/sct-20190911102137.936778-straighten_spinalcord-vv7u8gdj/tmp.anat_rigid_warp.nii.gz -o ./data_straight.nii
  File created: ./data_straight.nii
Remove temporary files...
rm -rf /tmp/sct-20190911102137.936778-straighten_spinalcord-vv7u8gdj

Finished! Elapsed time: 128 s

Resample to 0.5mm isotropic...
sct_resample -i data_straight.nii -mm 0.5x0.5x0.5 -x linear -o data_straightr.nii # in /tmp/sct-20190911102136.956829-label_vertebrae-epldo_nk

Apply straightening to segmentation...
/mnt/workspace/sct_dev/bin/isct_antsApplyTransforms -d 3 -i segmentation.nii -r data_straightr.nii -t warp_curve2straight.nii.gz -o segmentation_straight.nii -n Linear # in /tmp/sct-20190911102136.956829-label_vertebrae-epldo_nk
sct_maths -i segmentation_straight.nii -thr 0.5 -o segmentation_straight.nii # in /tmp/sct-20190911102136.956829-label_vertebrae-epldo_nk

Create label to identify disc...
Creating temporary folder...
Run C2-C3 detector...
C2-C3 detected...
Remove temporary files...
rm -rf /tmp/sct-20190911102355.980419-62txwmhp
WARNING: File /tmp/sct-20190911102136.956829-label_vertebrae-epldo_nk/labelz.nii.gz already exists. Will overwrite it.

And apply straightening to label...
/mnt/workspace/sct_dev/bin/isct_antsApplyTransforms -d 3 -i labelz.nii.gz -r data_straightr.nii -t warp_curve2straight.nii.gz -o labelz_straight.nii.gz -n NearestNeighbor # in /tmp/sct-20190911102136.956829-label_vertebrae-epldo_nk

Get z and disc values from straight label...
.. [257, 0]

Look for template...
Path template: /mnt/workspace/sct_dev/data/PAM50

Open template and vertebral levels...

Disc values from template: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Z-values for each disc: [962, 939, 908, 871, 834, 801, 770, 736, 693, 647, 601, 552, 501, 450, 397, 343, 290, 232, 169, 106, 56]
Distances between discs (in voxel): [23.0, 31.0, 37.0, 37.0, 33.0, 31.0, 34.0, 43.0, 46.0, 46.0, 49.0, 51.0, 51.0, 53.0, 54.0, 53.0, 58.0, 63.0, 63.0, 50.0]

Detect intervertebral discs...
Current disc: 0 (z=257). Direction: superior
.. Peak found: z=9 (correlation = 0.2887059228787826)
Traceback (most recent call last):
  File "/mnt/workspace/sct_dev/spinalcordtoolbox/vertebrae/core.py", line 200, in vertebral_detection
    approx_distance_to_next_disc = list_distance[list_disc_value_template.index(current_disc - 1)]
ValueError: -1 is not in list

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/mnt/workspace/sct_dev/scripts/sct_label_vertebrae.py", line 427, in <module>
    main()
  File "/mnt/workspace/sct_dev/scripts/sct_label_vertebrae.py", line 372, in main
    verbose=verbose, path_template=path_template, path_output=path_output, scale_dist=scale_dist)
  File "/mnt/workspace/sct_dev/spinalcordtoolbox/vertebrae/core.py", line 202, in vertebral_detection
    sct.printv('WARNING: Disc value not included in template. Using previously-calculated distance: ' + str(approx_distance_to_next_disc))
UnboundLocalError: local variable 'approx_distance_to_next_disc' referenced before assignment

advice?

thanks!

Hi @davidcadotte,

For some reasons, the C2-C3 disc detected assigned the value “0”. Would you mind sending me the T2 image + segmentation (could be in private email) so I can reproduce your issue and debug from there?

Cheers,
Julien

Thank you for sharing your images @davidcadotte. I confirm that I am able to reproduce the bug. A ticket has been opened here and we are working towards a solution.

Best,
Julien

wonderful! thanks very much-

@davidcadotte,

For your information, the problem comes from the segmentation, which is only partial at the top segmented slice (#205), causing instabilities in the straightening, and all sorts of problems in the chain of events (it was fun to debug this one :wink: ):

You can follow the current discussion about how we are planning to handle these edge cases (pun not intended), but for now you can simply edit your segmentation by removing those isolated voxels on slice #205 and re-run the labeling.

Sorry for the trouble, and thank you for reporting this bug!

Julien