ANTsX / ANTsR

R interface to the ANTs biomedical image processing library
https://antsx.github.io/ANTsR
Apache License 2.0
127 stars 35 forks source link

kmeansSegmentation error #273

Closed ptsii closed 4 years ago

ptsii commented 5 years ago

One of my images gives this error when I try segmenting it:

nickT1manual_seg3<-kmeansSegmentation(nickT1manual,3) Error in antsImageRead(probsout[1], mydim) : file does not exist

This image has previously been segmented, so even though I put "3" in there 2 categories is probably better. Nevertheless, I don't understand what the error is... Is it possible that one of the categories has no membership, causing an empty image?

Here is a plot of the image itself:

nickT1manual_R_plot_slices

muschellij2 commented 5 years ago

Need to provide https://stackoverflow.com/help/minimal-reproducible-example John

On Tue, Jun 11, 2019 at 11:50 AM ptsii notifications@github.com wrote:

One of my images gives this error when I try segmenting it:

nickT1manual_seg3<-kmeansSegmentation(nickT1manual,3) Error in antsImageRead(probsout[1], mydim) : file does not exist

This image has previously been segmented, so even though I put "3" in there 2 categories is probably better. Nevertheless, I don't understand what the error is... Is it possible that one of the categories has no membership, causing an empty image?

Here is a plot of the image itself:

[image: nickT1manual_R_plot_slices] https://user-images.githubusercontent.com/5157597/59287151-15ea0f80-8c3f-11e9-9128-cc7e50cf10d5.jpg

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/273?email_source=notifications&email_token=AAIGPLTV4OFOYEOZIUK4F53PZ7CUVA5CNFSM4HW7YFPKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4GY3H2DQ, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIGPLXEZOYVCHH7POJYW7TPZ7CUVANCNFSM4HW7YFPA .

ptsii commented 5 years ago

Try the following code with the attached image:

nickT1manual.nii.gz

nickT1manual<-antsImageRead("nickT1manual.nii.gz") nickT1manual_seg3<-kmeansSegmentation(nickT1manual,3)

When I do this, I get: Error in antsImageRead(probsout[1], mydim) : file does not exist

ptsii commented 5 years ago

Also: nickT1manual_seg2<-kmeansSegmentation(nickT1manual,2) ...with the same image does not give an error, and produces an output that seems reasonable

muschellij2 commented 5 years ago

If you put verbose = TRUE, I get:

itk::ExceptionObject (0x7fb3efe4beb0) Location: "unknown" File: /Users/johnmuschelli/Dropbox/Packages/ANTsRCore/src/ants/ImageSegmentation/antsGaussianListSampleFunction.hxx Line: 115 Description: itk::ERROR: GaussianListSampleFunction(0x7fb402958460): Covariance is singular (determinant = 0 < 1.0e-6)

John

On Tue, Jun 11, 2019 at 1:06 PM ptsii notifications@github.com wrote:

Also: nickT1manual_seg2<-kmeansSegmentation(nickT1manual,2) ...with the same image does not give an error, and produces an output that seems reasonable

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/273?email_source=notifications&email_token=AAIGPLVFFNQL4V7ADF5OFFDPZ7LR3A5CNFSM4HW7YFPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXN2Z3Y#issuecomment-500935919, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIGPLQWTM7TRHYIW33KAWLPZ7LR3ANCNFSM4HW7YFPA .

muschellij2 commented 5 years ago

The data only has values for WM and GM - there are not 3 things in there as CSF has been removed. John

On Tue, Jun 11, 2019 at 1:12 PM John Muschelli muschellij2@gmail.com wrote:

If you put verbose = TRUE, I get:

itk::ExceptionObject (0x7fb3efe4beb0) Location: "unknown" File: /Users/johnmuschelli/Dropbox/Packages/ANTsRCore/src/ants/ImageSegmentation/antsGaussianListSampleFunction.hxx Line: 115 Description: itk::ERROR: GaussianListSampleFunction(0x7fb402958460): Covariance is singular (determinant = 0 < 1.0e-6)

John

On Tue, Jun 11, 2019 at 1:06 PM ptsii notifications@github.com wrote:

Also: nickT1manual_seg2<-kmeansSegmentation(nickT1manual,2) ...with the same image does not give an error, and produces an output that seems reasonable

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/273?email_source=notifications&email_token=AAIGPLVFFNQL4V7ADF5OFFDPZ7LR3A5CNFSM4HW7YFPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXN2Z3Y#issuecomment-500935919, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIGPLQWTM7TRHYIW33KAWLPZ7LR3ANCNFSM4HW7YFPA .

muschellij2 commented 5 years ago

This seems to work (passing in a mask):

library(ANTsRCore)
img = antsImageRead("nickT1manual.nii.gz")
mask = img > 0
nickT1manual_seg3<-kmeansSegmentation(img, 2, kmask = mask, verbose = TRUE)
ptsii commented 5 years ago

I clearly don't understand what the kmeansSegmentation algorithm is doing. How could it know that the GM and WM were removed? Its got a range of values to work with, e.g.,:

hist(nickT1manual[nickT1manual>0]) nickT1manual_hist

Any prior segmentation - whether manual or not (but certainly since it was manual) - couldn't possibly have exactly and perfectly removed all the CSF... There must necessarily be some partial volume voxels in the data. I would have assumed that the algorithm would have tried to put SOME of the after-manual-segmentation voxels into a 3rd category. If it really isn't doing this (i.e., there isn't some bug or oddity in the underlying code), this would seem to suggest that there is in fact a perfect and exact segmentation evident in the data, which does not seem to me to be valid or likely. It's certainly not valid for actual brains.

Also: kmeansSegmentation(img, 2, kmask = mask) seems to output almost (but not quite) the same thing as: kmeansSegmentation(img, 2)

Since your mask only removed voxels with 0 values (well, only included voxels greater than 0), why are these not exactly the same?

Thanks for helping me understand this better,

-Tom

muschellij2 commented 5 years ago

So you can do kmeansSegmentation(img, 3, kmask = mask)for what you want

John

On Tue, Jun 11, 2019 at 3:24 PM ptsii notifications@github.com wrote:

I clearly don't understand what the kmeansSegmentation algorithm is doing. How could it know that the GM and WM were removed? Its got a range of values to work with, e.g.,:

hist(nickT1manual[nickT1manual>0]) [image: nickT1manual_hist] https://user-images.githubusercontent.com/5157597/59299278-3d020a80-8c5a-11e9-9f8b-cb5c8e472261.jpg

Any prior segmentation - whether manual or not (but certainly since it was manual) - couldn't possibly have exactly and perfectly removed all the CSF... There must necessarily be some partial volume voxels in the data. I would have assumed that the algorithm would have tried to put SOME of the after-manual-segmentation voxels into a 3rd category. If it really isn't doing this (i.e., there isn't some bug or oddity in the underlying code), this would seem to suggest that there is in fact a perfect and exact segmentation evident in the data, which does not seem to me to be valid or likely. It's certainly not valid for actual brains.

Also: kmeansSegmentation(img, 2, kmask = mask) seems to output almost (but not quite) the same thing as: kmeansSegmentation(img, 2)

Since your mask only removed voxels with 0 values (well, only included voxels greater than 0), why are these not exactly the same?

Thanks for helping me understand this better,

-Tom

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ANTsX/ANTsR/issues/273?email_source=notifications&email_token=AAIGPLR43SKVRQCEAWY5GW3PZ73VLA5CNFSM4HW7YFPKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXOHHWQ#issuecomment-500986842, or mute the thread https://github.com/notifications/unsubscribe-auth/AAIGPLRVBB5PFCFDKHKANJLPZ73VLANCNFSM4HW7YFPA .

ptsii commented 5 years ago

Your code above says:

nickT1manual_seg3<-kmeansSegmentation(img, 2, kmask = mask, verbose = TRUE) (i.e., the call explicitly asks for 2 categories, not 3)

When I try it with 3 instead, I get the same error:

nickT1manual_seg3<-kmeansSegmentation(nickT1manual, 3, kmask = mask, verbose = TRUE) Error in antsImageRead(probsout[1], mydim) : file does not exist

Running Atropos for 3-dimensional images.

Progress:

itk::ExceptionObject (0x600002399310) Location: "unknown" File: /private/var/folders/mk/9_clkwx513df94kdv85c25m80000gn/T/RtmpYDg6sJ/devtools2fcb72edd385/ANTsX-ANTsRCore-8ac2911/src/ants/ImageSegmentation/antsGaussianListSampleFunction.hxx Line: 115 Description: itk::ERROR: GaussianListSampleFunction(0x60000bee0400): Covariance is singular (determinant = 0 < 1.0e-6)

I'm still curious why: 1) it somehow magically knows that the CSF has been removed and won't work if you ask it to divide the data into 3 categories vs. 2, and 2) why the mask option leads to different values than the non-mask, for the examples above?

stnava commented 4 years ago
  1. the third class has zero standard deviation
  2. an automatic mask is generated if you don't pass one - thus different results