Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reproducible kmeans segmentation #51

Closed
muschellij2 opened this issue Jul 16, 2018 · 3 comments
Closed

Reproducible kmeans segmentation #51

muschellij2 opened this issue Jul 16, 2018 · 3 comments

Comments

@muschellij2
Copy link
Collaborator

After the discussion we had about registration randomness (ANTsX/ANTsR#226 (comment)), which includes ANTsX/ANTsR#210 and ANTsX/ANTs#444, I wanted to look at other randomness and reproducibility weaknesses.

I understand that Atropos relies on a k-means segmentation and like all other k-means segs, it requires random initializations. I think we should have the ability to have this implementation of Atropos to have reproducible output. As you can see below, it is not reproducible, even when ANTS_RANDOM_SEED is set.

library(ANTsRCore)
set.seed(2)
Sys.setenv(ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS = 1,
           ANTS_RANDOM_SEED = 20180716)
Sys.getenv("ANTS_RANDOM_SEED")
[1] "20180716"
Sys.getenv("ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS")
[1] "1"
orig <-antsImageRead( getANTsRData("r16") ,2)
seg<-kmeansSegmentation( orig, 3 )
seg2<-kmeansSegmentation( orig, 3 )
arr1 = as.array(seg$segmentation)
arr2 = as.array(seg2$segmentation)
tab = table(arr1, arr2)
tab
    arr2
arr1     0     1     2     3
   0 47118     0     0     0
   1     0  2383     2     0
   2     0     0  7352     3
   3     0     0     6  8672
identical(arr1, arr2)
[1] FALSE

Atropos documentation

In the documentation for Atropos, there is the option for using a random seed:

     -r, --use-random-seed 0/(1)
          Initialize internal random number generator with a random seed. Otherwise, 
          initialize with a constant seed number. 

But that is not clear in the atropos documentation or a standard option. I would like to add in random_seed to be a boolean that we pass into the r argument, but looking at this line https://github.com/ANTsX/ANTs/blob/6a428f6fabda42aaa2af595dbc97a9d74668a702/Examples/Atropos.cxx#L762 it seems as though it will use some arbitrary fixed random seed (that may not be right). Is it possible to set this seed using Sys.setenv or is -r 0 the best we can do at the moment?

@muschellij2
Copy link
Collaborator Author

We should likely use the recommending RNGState variables: https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Random-number-generation, but Rcpp could offer something a bit more elegant: http://gallery.rcpp.org/articles/random-number-generation/

@stnava
Copy link
Member

stnava commented Jul 16, 2018 via email

muschellij2 added a commit that referenced this issue Jul 16, 2018
@muschellij2
Copy link
Collaborator Author

muschellij2 commented Jul 16, 2018

There is different default behavior (aka be reproducible by default). I mean different from Atropos in ANTs default. If that's not OK, please set the default in https://github.com/ANTsX/ANTsRCore/blob/master/R/atropos.R#L57 to TRUE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants