diff --git a/.Rbuildignore b/.Rbuildignore index baeabfa..2d18417 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -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 diff --git a/NAMESPACE b/NAMESPACE index e309dd7..f788cc8 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -58,6 +58,7 @@ export(fsldiv) export(fsldiv.help) export(fsledge) export(fsledge.help) +export(fslentropy) export(fslerode) export(fslerode.help) export(fslexp) @@ -77,6 +78,7 @@ export(fslmask) export(fslmask.help) export(fslmaths) export(fslmaths.help) +export(fslmean) export(fslmerge) export(fslmerge.help) export(fslmul) @@ -98,6 +100,7 @@ export(fslrem) export(fslrem.help) export(fslreorient2std) export(fslreorient2std.help) +export(fslsd) export(fslsin) export(fslsin.help) export(fslsmooth) diff --git a/R/fslentropy.R b/R/fslentropy.R new file mode 100644 index 0000000..feb4acd --- /dev/null +++ b/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 +} diff --git a/R/fslhd.R b/R/fslhd.R index 1715c31..1102299 100644 --- a/R/fslhd.R +++ b/R/fslhd.R @@ -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()){ @@ -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 @@ -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 diff --git a/R/fslmean.R b/R/fslmean.R new file mode 100644 index 0000000..0177ce5 --- /dev/null +++ b/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 +} diff --git a/R/fslsd.R b/R/fslsd.R new file mode 100644 index 0000000..cd50573 --- /dev/null +++ b/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 +} diff --git a/fslr_Generic_fslstats.R b/fslr_Generic_fslstats.R new file mode 100644 index 0000000..3440ab2 --- /dev/null +++ b/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 +} diff --git a/fslr_Generic_fslstats_Maker.R b/fslr_Generic_fslstats_Maker.R new file mode 100644 index 0000000..9f378b3 --- /dev/null +++ b/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) + + + + + diff --git a/fslr_Generic_fslstats_nonzero.R b/fslr_Generic_fslstats_nonzero.R new file mode 100644 index 0000000..72999e6 --- /dev/null +++ b/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 +} diff --git a/man/fslentropy.Rd b/man/fslentropy.Rd new file mode 100644 index 0000000..0a0382d --- /dev/null +++ b/man/fslentropy.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fslentropy.R +\name{fslentropy} +\alias{fslentropy} +\title{Image Mean Entropy} +\usage{ +fslentropy(img, nonzero = FALSE, verbose = TRUE, ts = FALSE) +} +\arguments{ +\item{img}{Object of class nifti, or path of file} + +\item{nonzero}{(logical) Should the statistic be taken over non-zero voxels} + +\item{verbose}{(logical) print out command before running} + +\item{ts}{(logical) is the series a timeseries (4D), invoking \code{-t} +option} +} +\value{ +Vector of unless ts option invoked, then matrix +} +\description{ +Estimates Mean Entropy of Image from FSL +} +\note{ +This uses option -e or -E in \code{\link{fslstats}} +} + diff --git a/man/fslmean.Rd b/man/fslmean.Rd new file mode 100644 index 0000000..1a9fcd8 --- /dev/null +++ b/man/fslmean.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fslmean.R +\name{fslmean} +\alias{fslmean} +\title{Image Mean} +\usage{ +fslmean(img, nonzero = FALSE, verbose = TRUE, ts = FALSE) +} +\arguments{ +\item{img}{Object of class nifti, or path of file} + +\item{nonzero}{(logical) Should the statistic be taken over non-zero voxels} + +\item{verbose}{(logical) print out command before running} + +\item{ts}{(logical) is the series a timeseries (4D), invoking \code{-t} +option} +} +\value{ +Vector of unless ts option invoked, then matrix +} +\description{ +Estimates Mean of Image from FSL +} +\note{ +This uses option -m or -M in \code{\link{fslstats}} +} + diff --git a/man/fslrange.Rd b/man/fslrange.Rd index dec6084..d9afaec 100644 --- a/man/fslrange.Rd +++ b/man/fslrange.Rd @@ -4,11 +4,13 @@ \alias{fslrange} \title{Get range of an image} \usage{ -fslrange(file, verbose = TRUE, ts = FALSE, ...) +fslrange(file, robust = FALSE, verbose = TRUE, ts = FALSE, ...) } \arguments{ \item{file}{(character) filename of image to be checked} +\item{robust}{(logical) Should the range be robust (\code{-r})} + \item{verbose}{(logical) print out command before running} \item{ts}{(logical) is the series a timeseries (4D), invoking \code{-t} @@ -20,7 +22,8 @@ option} numeric vector of length 2 } \description{ -This function calls \code{fslstats -R} to get the range of an image +This function calls \code{fslstats -R} to get the range of an image or \code{fslstats -r} to +get the robust range } \examples{ if (have.fsl()){ diff --git a/man/fslsd.Rd b/man/fslsd.Rd new file mode 100644 index 0000000..65625de --- /dev/null +++ b/man/fslsd.Rd @@ -0,0 +1,28 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fslsd.R +\name{fslsd} +\alias{fslsd} +\title{Image Standard Deviation} +\usage{ +fslsd(img, nonzero = FALSE, verbose = TRUE, ts = FALSE) +} +\arguments{ +\item{img}{Object of class nifti, or path of file} + +\item{nonzero}{(logical) Should the statistic be taken over non-zero voxels} + +\item{verbose}{(logical) print out command before running} + +\item{ts}{(logical) is the series a timeseries (4D), invoking \code{-t} +option} +} +\value{ +Vector of unless ts option invoked, then matrix +} +\description{ +Estimates Standard Deviation of Image from FSL +} +\note{ +This uses option -s or -S in \code{\link{fslstats}} +} + diff --git a/man/fslstats.help.Rd b/man/fslstats.help.Rd index a3ec4ec..7c2e720 100644 --- a/man/fslstats.help.Rd +++ b/man/fslstats.help.Rd @@ -1,7 +1,10 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/fslhd.R \name{fslstats.help} +\alias{fslentropy.help} +\alias{fslmean.help} \alias{fslrange.help} +\alias{fslsd.help} \alias{fslstats.help} \title{FSL Stats Help} \usage{