ANTsX / ANTs

Advanced Normalization Tools (ANTs)
Apache License 2.0
1.15k stars 374 forks source link

Atropos - Error when using 3 class tissue segmentation #1765

Open grahamlittlephd opened 4 days ago

grahamlittlephd commented 4 days ago

Operating system and version

Ubuntu 22.04

CPU architecture

x86_64 (PC, Intel Mac, other Intel/AMD)

ANTs code version

2.5.2

ANTs installation type

Downloaded Github release binaries

Summary of the problem

Getting the following error when running a 3 class tissue segmentation of the hippocampus/amygdala.

Progress: WARNING: In /home/runner/work/ANTs/ANTs/ImageSegmentation/antsHistogramParzenWindowsListSampleFunction.hxx, line 57 HistogramParzenWindowsListSampleFunction (0x55a3c60883d0): The input list sample has <= 1 element.Function evaluations will be equal to 0.

Iteration 0 (of 5): posterior probability = 0 (annealing temperature = 1)

itk::ExceptionObject (0x55a3c60b8cf0) Location: "unknown" File: /home/runner/work/_temp/build/staging/include/ITK-5.4/itkLabelMap.hxx Line: 180 Description: ITK ERROR: LabelMap(0x55a3c6f42450): Can't access label object at position 2. The label map has only 2 label objects registered

Seems to work with v2.3.5.post76-g28dd25c

Note if I change the number of classes to 2, without changing any other input the command works

Commands to reproduce the problem.

Atropos -a 122317_b0b1000_MD_plusFA.nii.gz -x 122317_initialization_LEFT_HIPPOAMYG.nii.gz -i PriorProbabilityImages[3,122317_initialization_INITAMYGHIPPOLH%02d.nii.gz,0.3,0.0001] -m [0.3, 2x2x2] --use-partial-volume-likelihoods false -s 1x3 -s 1x2 -o [122317_initialization_INITAMYGHIPPO_LH_LABELS.nii.gz, 122317_initialization_INITAMYGHIPPO_LHPROBS%02d.nii.gz] -k HistogramParzenWindows[1.0,32] -v 1

Output of the command with verbose output.

"""Atropos -a 122317_b0b1000_MD_plusFA.nii.gz -x 122317_initialization_LEFT_HIPPOAMYG.nii.gz -i PriorProbabilityImages[3,122317_initialization_INITAMYGHIPPOLH%02d.nii.gz,0.3,0.0001] -m [0.3, 2x2x2] --use-partial-volume-likelihoods false -s 1x3 -s 1x2 -o [122317_initialization_INITAMYGHIPPO_LH_LABELS.nii.gz, 122317_initialization_INITAMYGHIPPO_LHPROBS%02d.nii.gz] -k HistogramParzenWindows[1.0,32] -v 1

Running Atropos for 3-dimensional images.

Progress: WARNING: In /home/runner/work/ANTs/ANTs/ImageSegmentation/antsHistogramParzenWindowsListSampleFunction.hxx, line 57 HistogramParzenWindowsListSampleFunction (0x55a3c60883d0): The input list sample has <= 1 element.Function evaluations will be equal to 0.

Iteration 0 (of 5): posterior probability = 0 (annealing temperature = 1)

itk::ExceptionObject (0x55a3c60b8cf0) Location: "unknown" File: /home/runner/work/_temp/build/staging/include/ITK-5.4/itkLabelMap.hxx Line: 180 Description: ITK ERROR: LabelMap(0x55a3c6f42450): Can't access label object at position 2. The label map has only 2 label objects registered."""

Data to reproduce the problem

atropos_hippoamyg_seg.zip

You should be able to run the command from the inside the directory in the zip

cookpa commented 4 days ago

Thanks for the command and data. I think the problem is that there are zero voxels with label 3 in the segmentation. If I dilate the mask, it works for me.

grahamlittlephd commented 4 days ago

Thanks so much for the quick reply, I am trying to get some scripts updated to work with the newest version of ants. So am I correct that this was acceptable in a previous version (2.3.5) but now in 2.5.2, if a label doesn't exist in the output for all classes Atropos will error.

Maybe this should addressed as a bug for fixing or detailed somewhere in the documentation

cookpa commented 4 days ago

There was a bug in the parzen window code where it would fail if multi-threaded, so if you run 2.3.5 to check, use one thread.

I'll build 2.3.5 and check in a bit

grahamlittlephd commented 4 days ago

Thanks, something I forgot to mention, I can reproduce the error in 2.5.2 when I remove the parzen window option and just use defaults. Just an FYI incase you are testing.

cookpa commented 4 days ago

Thanks, yes I switched to Gaussian to avoid that other bug. I couldn't build 2.3.5 but I was able to run the binaries for 2.4.1, and it works. Will track down the release where this occurs.

cookpa commented 4 days ago

OK, it works with 2.5.1 but not 2.5.2.

@ntustison, would you mind taking a look when you have time? The simplified command I was running with the example data above:

Atropos -a 122317_b0b1000_MD_plusFA.nii.gz -x 122317_initialization_LEFT_HIPPOAMYG.nii.gz -i PriorProbabilityImages[3,122317_initialization_INITAMYGHIPPO_LH_%02d.nii.gz,0.3,0.0001] -m [0.3, 2x2x2] -o [122317_initialization_INITAMYGHIPPO_LH_LABELS.nii.gz, 122317_initialization_INITAMYGHIPPO_LH_PROBS_%02d.nii.gz] -k Gaussian -v 1

It works on 2.5.1 but with 2.5.2, it gives this error

Running Atropos for 3-dimensional images.

Progress: 
WARNING: In /Users/runner/work/ANTs/ANTs/ImageSegmentation/antsGaussianListSampleFunction.hxx, line 106
GaussianListSampleFunction (0x600003704300): The input list sample has <= 1 element.  Function evaluations will be equal to 0.

  Iteration 0 (of 5): posterior probability = 0 (annealing temperature = 1)

itk::ExceptionObject (0x6000028083a0)
Location: "unknown" 
File: /Users/runner/work/_temp/build/staging/include/ITK-5.4/itkLabelMap.hxx
Line: 181
Description: ITK ERROR: LabelMap(0x7f7da5709190): Can't access label object at position 2. The label map has only 2 label objects registered.
gdevenyi commented 4 days ago

Could try a git bisect to find the exact commit which did it.

ntustison commented 4 days ago

You're setting up your initial partitioning of the masked region with the three probability images (I'm guessing 1: amygdala, 2: hippocampus, and 3: cortex. Just probing the values in the mask, it would appear to me that no voxels are being assigned to the third label because one or both of the probabilities of the first two labels for all the voxels in the mask are greater than the corresponding probabilities of the third. Did you check that this isn't the case?

cookpa commented 4 days ago

it would appear to me that no voxels are being assigned to the third label because one or both of the probabilities of the first two labels for all the voxels in the mask are greater than the corresponding probabilities of the third.

That is what's happening, yes. But this did not cause an error in ANTs 2.5.1 and earlier. Earlier versions did produce the warning

WARNING: In /Users/runner/work/ANTs/ANTs/ImageSegmentation/antsGaussianListSampleFunction.hxx, line 106
GaussianListSampleFunction (0x600003704300): The input list sample has <= 1 element.  Function evaluations will be equal to 0.

but then ran successfully. The output posteriors for class 3 were very small, and there were no class 3 labels in the output, but it didn't exit with the error.

ntustison commented 4 days ago

If this is that degenerate case, it shouldn't go past initialization because the distribution for the third label is unknown without any representative samples. Perhaps a better use of time would be a check and a better explanation of the error, rather than tracking down why it "worked" before. Although if i had to guess, it is probably due to keeping the sample list around and some check that I'm doing.

ntustison commented 3 days ago

Okay @cookpa , see if the above proposed solution works for you. I did a brief look to figure out why it worked before but I didn't see anything obvious.

cookpa commented 3 days ago

Thanks @ntustison

@grahamlittlephd I think @ntustison is correct that it's better to error in this situation because the algorithm updates based on voxels labeled as a class, not just voxels with a non-zero prior probability.

Maybe it would make sense to extend the ROI or maybe even have a separate ROI representing cortex, so that Atropos has some sense of what cortex looks like

ntustison commented 3 days ago

Related a bit---@grahamlittlephd, are you interested in segmenting the amygdala and hippocampus? If so, I can point you to functionality in our deep learning library which does this.

grahamlittlephd commented 3 days ago

Thanks so much for looking into this! I agree that raising an error at least prevents the user from getting into this situation.

However, this still doesn't explain why the behavior of Atropos changed from 2.5.1 to 2.5.2. In 2.5.1 I always would get 3-classes in my output even if there are only a few voxels in label 3 (cortex). I have ran this command on at least 500 subjects. Now in version 2.5.2 there are only 2-classes in my output and the error was thrown.

I realize this is very much an edge case but you may want to consider finding the commit that did it like @gdevenyi mentioned, otherwise are you sure that this version update didn't effect other ants tools?

grahamlittlephd commented 3 days ago

Related a bit---@grahamlittlephd, are you interested in segmenting the amygdala and hippocampus? If so, I can point you to functionality in our deep learning library which does this.

Very cool didn't know this existed. Not for this project but will add for something to try out at some point :+1:

ntustison commented 3 days ago

However, this still doesn't explain why the behavior of Atropos changed from 2.5.1 to 2.5.2.

It shouldn't have worked. I don't know exactly why it did. I have my hunches but, as I mentioned above, it's not worth my time to go back and investigate.

otherwise are you sure that this version update didn't effect other ants tools?

I can't guarantee that it didn't but I highly doubt that it did.