Diff of /R/radiomics_slice.R [000000] .. [3b2327]

Switch to unified view

a b/R/radiomics_slice.R
1
#' Radiomic Calculation on CT Slices
2
#'
3
#' Calculate radiomic features on the each 2D slice of the whole 3D lung, right and left lungs separately
4
#'
5
#' @param img CT scan in ANTs image file format
6
#' @param mask Mask of CT scan in ANTs image file format
7
#' @param sides Choose to calculate radiomic features on the right and/or left lungs. Note: Right lung = 1, left lung = 2, non-lung = 0
8
#' @param plane One of: axial, coronal, sagittal
9
#' @param featuresFirst First level radiomic features to calculate
10
#' @param featuresSpatial Spatial radiomic features to calculate
11
#' @param tidy Logical. If true, outputs a tidy dataframe with results. If false, outputs nested loop.
12
#' @param reduce Logical. If true, reduces the dimensions of the scan based on extent of mask using reduce_scan.
13
#'
14
#' @return Radiomic values from every slice in both the right and left lungs
15
#' @export
16
#'
17
radiomics_slice <- function(img,
18
                           mask,
19
                           sides = c("right", "left"),
20
                           plane = 'axial',
21
                           featuresFirst = c('mean', 'sd', 'skew', 'kurtosis', 'min', 'q1', 'median', 'q3', 'max','energy', 'rms', 'uniformity', 'entropy'),
22
                           featuresSpatial = c('mi', 'gc', 'fd'),
23
                           tidy = TRUE,
24
                           reduce = TRUE){
25
26
27
  featuresMask <- lapply(sides, function(side){
28
29
    if(side == "right"){mv = 1}
30
    if(side == "left"){mv = 2}
31
32
    mask2 <- mask == mv
33
34
    # Reduce scan (optional)
35
    if(reduce == TRUE){
36
      red <- reduce_scan(img, mask2)
37
      img2 <- red$img
38
      mask2 <- red$mask
39
      rm(red)
40
      gc()
41
    }else{img2 <- img}
42
43
    # Put image in array format and remove non-mask values
44
    img2 <- as.array(img2)
45
    mask2 <- as.array(mask2)
46
    img2[mask2 != 1] <- NA
47
48
49
    # Which plane?
50
    if(plane == 'axial'){p = 3}
51
    if(plane == 'coronal'){p = 2}
52
    if(plane == 'sagittal'){p = 1}
53
54
55
    # Calculate features
56
    ndim <- dim(img2)[p]
57
    features <- apply(img2, p, function(x){
58
      npixels <- length(x[!is.na(x)])
59
      if(length(featuresFirst)>0){
60
        features1 <- radiomics_first(x, featuresFirst)
61
      }else(features1 <- NULL)
62
      if(length(featuresSpatial)>0){
63
        features2 <- radiomics_spatial(x, featuresSpatial)
64
      }else(features2 <- NULL)
65
      features <- c(features1, features2)
66
      features <- features[c(featuresFirst, featuresSpatial)]
67
      features <- c(npixels = npixels, features)
68
      return(features)
69
    })
70
    names(features) <- paste0('slic_num_', 1:ndim)
71
72
    return(features)
73
  })
74
  names(featuresMask) <- sides
75
76
77
  if(tidy == TRUE){
78
    # Make a nice little data frame to output
79
    test2 = NULL
80
    for(i in 1:length(featuresMask)){
81
      test <- do.call('rbind', featuresMask[[i]])
82
      test <- cbind.data.frame(lung = names(featuresMask)[i],
83
                               slice_number = names(featuresMask[[i]]),
84
                               test)
85
      test$slice_number <- gsub("slic_num_", "", test$slice_number)
86
      test <- as.data.frame(sapply(test, as.numeric))
87
      nslic <- dim(test)[1]
88
      test$slice_percent <- test$slice_number/nslic * 100
89
      test2 <- rbind(test2, test)
90
    }
91
    featuresMask <- test2
92
    rownames(featuresMask) <- c()
93
  }
94
95
  return(featuresMask)
96
}