Skip to content

Negative value(s) in WM segmentation causes fail of snr_single computation in compute_cnr.py #77

@valosekj

Description

@valosekj

When processing some of my data, I encountered an issue with the computation of the snr_single. The issue occurs when the WM segmentation contains negative values.

Description

If the GM segmentation exceeds the SC segmentation:

image

The WM segmentation created by the sct_maths command:

# Generate white matter segmentation
sct_maths -i ${file_1_seg}${ext} -sub ${file_1_gmseg}${ext} -o ${file_1}_wmseg${ext}

includes -1 value(s):

image

and also all voxels which were zero now have small negative values close to zero:

image

This results in a negative variance in the compute_cnr.py script:

variance = np.average((values - average) ** 2, weights=weights)
return np.sqrt(variance)

for example:

Screenshot 2022-07-12 at 13 39 32

which in turn causes that return np.sqrt(variance) returns NaN and computation of the noise_single_slice and snr_single_slice results also in NaN:

noise_single_slice = \
[weighted_std(data1[..., iz], weights=mask[..., iz]) for iz in range(nz) if np.any(mask[..., iz])]
snr_single_slice = [m / s for m, s in zip(mean_in_roi, noise_single_slice)]

Workaround

The possible workaround which fixes the issue for me is the thresholding of the WM segmentation, for instance, like this:

sct_maths -i ${file_1}_wmseg${ext} -thr 0 -o ${file_1}_wmseg${ext} 

SCT version

$ sct_check_dependencies -short                                                     

--
Spinal Cord Toolbox (git-HEAD-45b1c1759f991332697b4120160303e963f7a3c7)

sct_check_dependencies -short
--

SCT info:
- version: git-HEAD-45b1c1759f991332697b4120160303e963f7a3c7
- path: /usr/local/lib/sct_latest
OS: linux (Linux-5.10.0-0.bpo.9-amd64-x86_64-with-debian-10.12)
CPU cores: Available: 16, Used by ITK functions: 16
RAM: Total: 128744MB, Used: 15793MB, Available: 111367MB 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions