|
a |
|
b/R/calculate_DSC.R |
|
|
1 |
DSC <- function(x,y){ |
|
|
2 |
x <- as.array(x) |
|
|
3 |
y <- as.array(y) |
|
|
4 |
tt <- sum( x & y) |
|
|
5 |
tf <- sum( x & !y) |
|
|
6 |
ft <- sum(!x & y) |
|
|
7 |
ff <- sum(!x & !y) |
|
|
8 |
tab = matrix(c(ff, tf, ft, tt), ncol=2) |
|
|
9 |
n = list(c("FALSE", "TRUE"), c("FALSE", "TRUE")) |
|
|
10 |
dimnames(tab) = n |
|
|
11 |
tab = as.table(tab) |
|
|
12 |
|
|
|
13 |
DSC = 2*tab[2,2] / (2*tab[2,2] + tab[1,2] + tab[2,1]) |
|
|
14 |
return(DSC) |
|
|
15 |
} |
|
|
16 |
#' Dice Similarity Coefficient Calculation |
|
|
17 |
#' |
|
|
18 |
#' Calculate the Dice similarity coefficient (DSC) for two iterations of lung templates |
|
|
19 |
#' |
|
|
20 |
#' @param template0 Initial template mask. Right lung = 1, left lung = 2, non-lung = 0 |
|
|
21 |
#' @param template1 New template mask. Right lung = 1, left lung = 2, non-lung = 0 |
|
|
22 |
#' @param sides Calculate DSC on right and/or left lungs. Overall DSC is given by default. |
|
|
23 |
#' |
|
|
24 |
#' @return DSC on whole lung, right lung and left lung. |
|
|
25 |
#' @export |
|
|
26 |
calculate_DSC <- function(template0, template1, sides = c("right","left")){ |
|
|
27 |
|
|
|
28 |
# Overall DSC |
|
|
29 |
x <- template0 > 0 |
|
|
30 |
y <- template1 > 0 |
|
|
31 |
DSC_overall <- DSC(x,y) |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
if ("right" %in% sides){ |
|
|
35 |
x <- template0 == 1 |
|
|
36 |
y <- template1 == 1 |
|
|
37 |
DSC_right <- DSC(x,y) |
|
|
38 |
}else{DSC_right = NULL} |
|
|
39 |
|
|
|
40 |
|
|
|
41 |
if ("left" %in% sides){ |
|
|
42 |
x <- template0 == 2 |
|
|
43 |
y <- template1 == 2 |
|
|
44 |
DSC_left <- DSC(x,y) |
|
|
45 |
}else{DSC_left = NULL} |
|
|
46 |
|
|
|
47 |
return(list(DSC_overall = DSC_overall, |
|
|
48 |
DSC_right = DSC_right, |
|
|
49 |
DSC_left = DSC_left)) |
|
|
50 |
} |