# STAPLE Example

## STAPLE Example

Simultaneous truth and performance level estimation (STAPLE) is a method for estimating a image segmentation. Commonly an image is segmented from a set of raters or readers. Here, we have an image that has been segmented from 3 different readers. Each is a binary segmentation, which includes areas of lesions from one person’s image.

## Running STAPLE

Here we will use STAPLE to fuse the data:

library(stapler)
images = staple_example_data()
res = staple(x = images, set_orient = TRUE)
#> Reshaping images
#> All images are niftiImage ojects
#> Warning in orientation<-(*tmp*, value = ori): Image qform and sform
#> codes are both zero, so it cannot be reoriented

#> Warning in orientation<-(*tmp*, value = ori): Image qform and sform
#> codes are both zero, so it cannot be reoriented

#> Warning in orientation<-(*tmp*, value = ori): Image qform and sform
#> codes are both zero, so it cannot be reoriented
#> There are 2 levels present
#> Removing elements where all raters agree
#> Making multiple, matrices. Hot-one encode
#> Convergence!
#> Creating output probability images/arrays
#> Creating output label image/array
print(names(res))
#> [1] "sensitivity"           "specificity"           "probability"
#> [4] "label"                 "prior"                 "number_iterations"
#> [7] "convergence_threshold" "convergence_value"     "converged"

We use the set_orient = TRUE argument so that if the images headers are different, then they will be set to the same orientation when run. The staple function will read in the images, reshape the data into a matrix, compute a prior for each element (voxel, in this case), run STAPLE. The output probability image, labeled image (if probability $$\geq 0.5$$), and prior image are given in the output res.

## Multi-class STAPLE

STAPLE works with multi-class data as well. The data do not need to be binary, but they need to be consistently labeled for each image/segmentation. Here we will

x = matrix(rbinom(5000, size = 5, prob = 0.5), ncol = 1000)
table(x)
#> x
#>    0    1    2    3    4    5
#>  144  769 1565 1605  766  151
res_mult = staple_multi_mat(x)
#> There are 6 levels present
#> Removing elements where all raters agree
#> Making multiple, matrices. Hot-one encode
#> iter: 25, diff: 3.36496935143815e-06
#> iter: 50, diff: 8.45837011542727e-10
#> iter: 75, diff: 2.07500683302442e-13
#> Convergence!
ncol(res_mult$probability) #> [1] 6 colnames(res_mult$probability)
#> [1] "0" "1" "2" "3" "4" "5"