Skip to content

Commit

Permalink
Added all the functions from fslstats for simple wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
muschellij2 committed Nov 10, 2015
1 parent 808b422 commit ed59b85
Show file tree
Hide file tree
Showing 14 changed files with 287 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Expand Up @@ -9,6 +9,9 @@ Generic_Function.R
Generic_Function2.R
fslr_Generic_Maker.R
fslr_Generic_Function.R
fslr_Generic_fslstats.R
fslr_Generic_fslstats_nonzero.R
fslr_Generic_fslstats_Maker.R
fslr_Generic_fslmaths.R
fslr_Generic_fslmaths_onefile.R
fslr_Generic_fslmaths_Maker.R
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Expand Up @@ -58,6 +58,7 @@ export(fsldiv)
export(fsldiv.help)
export(fsledge)
export(fsledge.help)
export(fslentropy)
export(fslerode)
export(fslerode.help)
export(fslexp)
Expand All @@ -77,6 +78,7 @@ export(fslmask)
export(fslmask.help)
export(fslmaths)
export(fslmaths.help)
export(fslmean)
export(fslmerge)
export(fslmerge.help)
export(fslmul)
Expand All @@ -98,6 +100,7 @@ export(fslrem)
export(fslrem.help)
export(fslreorient2std)
export(fslreorient2std.help)
export(fslsd)
export(fslsin)
export(fslsin.help)
export(fslsmooth)
Expand Down
23 changes: 23 additions & 0 deletions R/fslentropy.R
@@ -0,0 +1,23 @@
#' @title Image Mean Entropy
#' @description Estimates Mean Entropy of Image from FSL
#' @param img Object of class nifti, or path of file
#' @param nonzero (logical) Should the statistic be taken over non-zero voxels
#' @param verbose (logical) print out command before running
#' @param ts (logical) is the series a timeseries (4D), invoking \code{-t}
#' option
#' @note This uses option -e or -E in \code{\link{fslstats}}
#' @return Vector of unless ts option invoked, then matrix
#' @export
fslentropy = function(img, nonzero = FALSE, verbose = TRUE, ts = FALSE){
opts = "-e"
opts = ifelse(nonzero, toupper(opts), opts)

val = fslstats(img, opts = opts, verbose = verbose, ts = ts)
val = strsplit(val, " ")
if (length(val) == 1) {
val = as.numeric(val[[1]])
} else {
val = sapply(val, as.numeric)
}
val
}
30 changes: 14 additions & 16 deletions R/fslhd.R
Expand Up @@ -118,7 +118,7 @@ fslmaths.help = function(){
#' @title FSL Stats Help
#' @description This function calls \code{fslstats}'s help
#' @return Prints help output and returns output as character vector
#' @aliases fslrange.help
#' @aliases fslrange.help fslmean.help fslentropy.help fslsd.help
#' @export
#' @examples
#' if (have.fsl()){
Expand Down Expand Up @@ -659,8 +659,10 @@ check_sform_file <- function(file, value=0, ...){


#' @title Get range of an image
#' @description This function calls \code{fslstats -R} to get the range of an image
#' @description This function calls \code{fslstats -R} to get the range of an image or \code{fslstats -r} to
#' get the robust range
#' @param file (character) filename of image to be checked
#' @param robust (logical) Should the range be robust (\code{-r})
#' @param verbose (logical) print out command before running
#' @param ts (logical) is the series a timeseries (4D), invoking \code{-t}
#' option
Expand All @@ -674,22 +676,18 @@ check_sform_file <- function(file, value=0, ...){
#' "MNI152_T1_2mm.nii.gz")
#' fslrange(mnifile)
#' }
fslrange <- function(file, verbose =TRUE, ts = FALSE, ...){
cmd <- get.fsl()
file = checkimg(file, ...)
cmd <- paste0(cmd,
sprintf('fslstats %s "%s" -R', ifelse(ts, "-t", ""), file))
if (verbose){
cat(cmd, "\n")
}
x = str_trim(system(cmd, intern = TRUE))
x = strsplit(x, " ")
if (length(x) == 1) {
x = as.numeric(x[[1]])
fslrange <- function(file, robust = FALSE, verbose = TRUE, ts = FALSE, ...){
opts = "-R"
opts = ifelse(robust, tolower(opts), opts)

val = fslstats(file, opts = opts, verbose = verbose, ts = ts)
val = strsplit(val, " ")
if (length(val) == 1) {
val = as.numeric(val[[1]])
} else {
x = t(sapply(x, as.numeric))
val = t(sapply(val, as.numeric))
}
x
val
}

#' @title Fill image holes
Expand Down
23 changes: 23 additions & 0 deletions R/fslmean.R
@@ -0,0 +1,23 @@
#' @title Image Mean
#' @description Estimates Mean of Image from FSL
#' @param img Object of class nifti, or path of file
#' @param nonzero (logical) Should the statistic be taken over non-zero voxels
#' @param verbose (logical) print out command before running
#' @param ts (logical) is the series a timeseries (4D), invoking \code{-t}
#' option
#' @note This uses option -m or -M in \code{\link{fslstats}}
#' @return Vector of unless ts option invoked, then matrix
#' @export
fslmean = function(img, nonzero = FALSE, verbose = TRUE, ts = FALSE){
opts = "-m"
opts = ifelse(nonzero, toupper(opts), opts)

val = fslstats(img, opts = opts, verbose = verbose, ts = ts)
val = strsplit(val, " ")
if (length(val) == 1) {
val = as.numeric(val[[1]])
} else {
val = sapply(val, as.numeric)
}
val
}
23 changes: 23 additions & 0 deletions R/fslsd.R
@@ -0,0 +1,23 @@
#' @title Image Standard Deviation
#' @description Estimates Standard Deviation of Image from FSL
#' @param img Object of class nifti, or path of file
#' @param nonzero (logical) Should the statistic be taken over non-zero voxels
#' @param verbose (logical) print out command before running
#' @param ts (logical) is the series a timeseries (4D), invoking \code{-t}
#' option
#' @note This uses option -s or -S in \code{\link{fslstats}}
#' @return Vector of unless ts option invoked, then matrix
#' @export
fslsd = function(img, nonzero = FALSE, verbose = TRUE, ts = FALSE){
opts = "-s"
opts = ifelse(nonzero, toupper(opts), opts)

val = fslstats(img, opts = opts, verbose = verbose, ts = ts)
val = strsplit(val, " ")
if (length(val) == 1) {
val = as.numeric(val[[1]])
} else {
val = sapply(val, as.numeric)
}
val
}
21 changes: 21 additions & 0 deletions fslr_Generic_fslstats.R
@@ -0,0 +1,21 @@
#' @title Image %FUNC%
#' @description Estimates %FUNC% of Image from FSL
#' @param img Object of class nifti, or path of file
#' @param verbose (logical) print out command before running
#' @param ts (logical) is the series a timeseries (4D), invoking \code{-t}
#' option
#' @note This uses option -%opt% in \code{\link{fslstats}}
#' @return Vector of length 1 unless ts option invoked, then vector of with length the number of timepoints.
#' @export
fsl%func% = function(img, verbose = TRUE, ts = FALSE){
opts = "-%opt%"

val = fslstats(img, opts = opts, verbose = verbose, ts = ts)
val = strsplit(val, " ")
if (length(val) == 1) {
val = as.numeric(val[[1]])
} else {
val = t(sapply(val, as.numeric))
}
val
}
62 changes: 62 additions & 0 deletions fslr_Generic_fslstats_Maker.R
@@ -0,0 +1,62 @@

proper = function(x){
paste0(toupper(substr(x, 1,1)), substr(x, 2, nchar(x)))
}

#############################################
# Function maker that involves 1 file
#############################################
makefunc = function(opt, FUNC, func,
remove = TRUE){

x = readLines('fslr_Generic_fslstats.R')
x = gsub("%opt%", opt, x)
x = gsub("%FUNC%", FUNC, x)
x = gsub("%func%", func, x)
funcname = paste0("fsl", opt)

writeLines(text = x, con = paste0("R/", funcname, ".R"))
if (remove) file.remove(paste0("R/", funcname, ".R"))
return(TRUE)
}

#############################################
# Function maker that involves non-zero entries
#############################################
makefunc = function(opt,
FUNC,
func,
remove = TRUE){

x = readLines('fslr_Generic_fslstats_nonzero.R')
x = gsub("%opt%", opt, x)
x = gsub("%OPT%", toupper(opt), x)
x = gsub("%FUNC%", FUNC, x)
x = gsub("%func%", func, x)
funcname = paste0("fsl", func)

writeLines(text = x, con = paste0("R/", funcname, ".R"))
if (remove) file.remove(paste0("R/", funcname, ".R"))
return(TRUE)
}


remove = FALSE
################################################################
# Make Functions that take in 1 file
################################################################
# makefunc("r", FUNC = "Robust Range", func = "rrange",
# remove = TRUE)


makefunc("m", FUNC = "Mean", func = "mean",
remove = remove)
makefunc("s", FUNC = "Standard Deviation", func = "sd",
remove = remove)
makefunc("e", FUNC = "Mean Entropy", func = "entropy",
remove = remove)





23 changes: 23 additions & 0 deletions fslr_Generic_fslstats_nonzero.R
@@ -0,0 +1,23 @@
#' @title Image %FUNC%
#' @description Estimates %FUNC% of Image from FSL
#' @param img Object of class nifti, or path of file
#' @param nonzero (logical) Should the statistic be taken over non-zero voxels
#' @param verbose (logical) print out command before running
#' @param ts (logical) is the series a timeseries (4D), invoking \code{-t}
#' option
#' @note This uses option -%opt% or -%OPT% in \code{\link{fslstats}}
#' @return Vector of unless ts option invoked, then matrix
#' @export
fsl%func% = function(img, nonzero = FALSE, verbose = TRUE, ts = FALSE){
opts = "-%opt%"
opts = ifelse(nonzero, toupper(opts), opts)

val = fslstats(img, opts = opts, verbose = verbose, ts = ts)
val = strsplit(val, " ")
if (length(val) == 1) {
val = as.numeric(val[[1]])
} else {
val = sapply(val, as.numeric)
}
val
}
28 changes: 28 additions & 0 deletions man/fslentropy.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/fslmean.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/fslrange.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions man/fslsd.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fslstats.help.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed59b85

Please sign in to comment.