Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
cfhammill committed Mar 19, 2019
2 parents c9838d2 + 75bc2ef commit a02a04a
Show file tree
Hide file tree
Showing 33 changed files with 891 additions and 215 deletions.
18 changes: 8 additions & 10 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,21 @@ sudo: required
before_install: |
if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
(cd ../ ;
wget http://packages.bic.mni.mcgill.ca/minc-toolkit/Debian/minc-toolkit-1.9.11-20160202-Ubuntu_14.04-x86_64.deb)
sudo dpkg -i ../minc-toolkit-1.9.11-20160202-Ubuntu_14.04-x86_64.deb
source /opt/minc-itk4/minc-toolkit-config.sh
wget http://packages.bic.mni.mcgill.ca/minc-toolkit/Debian/minc-toolkit-1.9.16-20180117-Ubuntu_14.04-x86_64.deb )
sudo dpkg -i ../minc-toolkit-1.9.16-20180117-Ubuntu_14.04-x86_64.deb
source /opt/minc/1.9.16/minc-toolkit-config.sh
fi
if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
## brew install gcc
## mkdir $HOME/.R/; touch $HOME/.R/Makevars
## echo 'FLIBS=-L/usr/local/Cellar/gcc/5.3.0/lib/gcc/5' > $HOME/.R/Makevars
(cd ../ ;
wget http://packages.bic.mni.mcgill.ca/minc-toolkit/MacOSX/minc-toolkit-1.9.11-20160202-Darwin-10.11-x86_64.dmg)
sudo hdiutil attach ../minc-toolkit-1.9.11-20160202-Darwin-10.11-x86_64.dmg
sudo installer -package /Volumes/minc-toolkit-1.9.11-20160202-Darwin-x86_64/minc-toolkit-1.9.11-20160202-Darwin-x86_64.pkg -target /
source /opt/minc-itk4/minc-toolkit-config.sh
wget http://packages.bic.mni.mcgill.ca/minc-toolkit/MacOSX/minc-toolkit-1.9.16-20180117-Darwin-10.8-x86_64.dmg)
sudo hdiutil attach ../minc-toolkit-1.9.16-20180117-Darwin-10.8-x86_64.dmg
sudo installer -package /Volumes/minc-toolkit-1.9.16-20180117-Darwin-x86_64/minc-toolkit-1.9.16-20180117-Darwin-x86_64.pkg -target /
source /opt/minc/1.9.16/minc-toolkit-config.sh
fi
repos:
bioCsoft: http://bioconductor.org/packages/3.5/bioc


bioCsoft: http://bioconductor.org/packages/3.8/bioc
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RMINC
Version: 1.5.2.1
Version: 1.5.2.2
Date: 2017-05-31
Title: Statistical Tools for Medical Imaging NetCDF (MINC) Files
Authors@R: c(person("Jason", "Lerch", role = "aut", email = "[email protected]"),
Expand All @@ -20,6 +20,7 @@ Imports:
batchtools,
dplyr (>= 0.7.6),
tidyr (>= 0.8.1),
readr ,
lme4 (>= 1.1-13),
purrr (>= 0.2.5),
shiny (>= 1.1.0),
Expand Down Expand Up @@ -61,4 +62,4 @@ OS_type: unix
BugReports: https://github.com/Mouse-Imaging-Centre/RMINC/issues
URL: https://github.com/Mouse-Imaging-Centre/RMINC,
https://wiki.mouseimaging.ca/display/MICePub/RMINC
RoxygenNote: 6.0.1
RoxygenNote: 6.1.0
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export(anatAnova)
export(anatApply)
export(anatCombineStructures)
export(anatCreateVolume)
export(anatEffectSize)
export(anatFDR)
export(anatGetAll)
export(anatGetAll_old)
Expand Down Expand Up @@ -151,6 +152,7 @@ export(mincConvertVoxelToWorld)
export(mincConvertWorldMatrix)
export(mincConvertWorldToVoxel)
export(mincCorrelation)
export(mincEffectSize)
export(mincFDR)
export(mincFDRMask)
export(mincFindPeaks)
Expand Down Expand Up @@ -202,6 +204,8 @@ export(thresholds)
export(verboseRun)
export(vertexAnova)
export(vertexApply)
export(vertexAtlasApply)
export(vertexEffectSize)
export(vertexFDR)
export(vertexFindPeaks)
export(vertexLm)
Expand Down
8 changes: 8 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
New in Version 1.5.*.*
=======================
2.2
--
- Improvements to vertex code (thanks @vfonov), now can
read single columns from vertex data files
- Hedges G effect sizes (thanks @gdevenyi), currently
for (minc/vertex/anat)Lm
- Better cortical figure layouting
- vertexAtlasApply for wrapping a common `tapply` pattern.

2.1
--
Expand Down
194 changes: 194 additions & 0 deletions R/effectsize.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#' Effect Sizes
#'
#' Takes the output of a minc modelling function and computes the unbiased
#' hedges g* and variance of hedges g*
#' @param buffer The results of a vertex/anat/mincLm run
#' @param predictors A vector of factor predictor names. By default the effect size
#' be computed for all treatment-coded factor columns.
#' @details This code implements the methods from Nakagawa, S., Cuthill, I.C., 2007. Effect size, confidence interval and statistical significance: a practical guide for biologists. Biol. Rev. Camb. Philos. Soc. 82, 591-05. https://doi.org/10.1111/j.1469-185X.2007.00027.x
#' for computing effect size of group comparisons from a GLM.
#'
#' For now, interactions are explicitly excluded from being predictors. To get
#' effect size for interactions, use the interaction() function to create a new
#' treatment coded factor to use as a predictor.
#' @return A matrix with columns of hedgesg-<factorlevel> and hedgesg_var-<factorlevel> for each factor predictor in the GLM
#' or for each column supplied.
#' @examples
#' \dontrun{
#' getRMINCTestData()
#' # read the text file describing the dataset
#' gf <- read.csv("/tmp/rminctestdata/test_data_set.csv")
#' # run a linear model relating the data in all voxels to Genotype
#' vs <- mincLm(jacobians_fixed_2 ~ Sex, gf)
#' effectsize <- mincEffectSize(vs)
#' }
#' @export
vertexEffectSize <- function(buffer, predictors = NULL)
{
#Nakagawa, S., Cuthill, I.C., 2007. Effect size, confidence interval and statistical significance: a practical guide for biologists. Biol. Rev. Camb. Philos. Soc. 82, 591-605. https://doi.org/10.1111/j.1469-185X.2007.00027.x
#Original unbiased corrector from paper replaced with
#unbiased corrector function J from https://en.wikipedia.org/wiki/Effect_size#Hedges'_g
#Watch out for exploding gamma function
J <- function(a) {
out <- tryCatch({
return (gamma(a / 2) / (sqrt(a / 2) * gamma((a - 1) / 2)))
},
error = function(cond) {
return(1)
},
warning = function(cond) {
return(1)
})
return(out)
}
# g_biased = t ( n1 + n2 ) / sqrt(n1*n2)*sqrt(df)
# g_unbiased = g_biased * J(n1 + n2 - 2)
# g_variance_unbiased = (n1 + n2) / (n1*n2) + g_unbiased**2 / (2 * (n1 + n2 - 2))
# n1 study group
# n2 reference group
# N = n1+n2
# NN = n1*n2

#Scrub columns not needed
originalMincAttrs <- mincAttributes(buffer)
stattype <- originalMincAttrs$`stat-type`
# Remove coefficients from buffer
if (!is.null(stattype)) {
for (nStat in 1:length(stattype)) {
if (stattype[nStat] == 'beta' ||
stattype[nStat] == 'R-squared' ||
stattype[nStat] == "logLik" || stattype[nStat] == "F") {
if (!exists('indicesToRemove')) {
indicesToRemove = nStat
}
else {
indicesToRemove = c(indicesToRemove, nStat)
}
}
}
if (exists('indicesToRemove')) {
updatedAttrs <- originalMincAttrs
updatedAttrs$`stat-type` <-
updatedAttrs$`stat-type`[-indicesToRemove]
updatedAttrs$dimnames[[2]] <-
updatedAttrs$dimnames[[2]][-indicesToRemove]

buffer <- buffer[, -indicesToRemove]
buffer <- setMincAttributes(buffer, updatedAttrs)
}
}

#Extract model, contrasts from model call
model_call <- attr(buffer, "call")
model_call[[1]] <- quote(model.matrix)
names(model_call)[names(model_call) == "formula"] <- ""
mod_mat <- eval(model_call)
conts <- attr(mod_mat, "contrasts")
cat_vars <-
names(conts)[vapply(conts, function(x)
all(x == "contr.treatment"), logical(1))]

#Checking for assumptions regarding computing
if (is.null(conts))
stop("No factors in model, cannot compute g* statistics")

if (is.null(cat_vars))
stop("No treatment-coded factors in model, cannot compute g* statistics")

if (!is.null(predictors) &&
!all(predictors %in% colnames(updatedAttrs$model)))
stop("Supplied predictors not found in model")

if (!is.null(predictors) &&
!all(grepl(paste(cat_vars, collapse = "|"), predictors)))
stop("Supplied predictors are not treatment contrasts")

if (!is.null(predictors) && any(grepl(":", predictors)))
stop(
"Interactions in predictors are not currently supported,
generate treatment contrasts using interaction()"
)

#If predictors aren't provided, ennumerate factors from the model
if (is.null(predictors)) {
predictors <-
grep(
":",
grep(
paste(cat_vars, collapse = "|"),
updatedAttrs$dimnames[[2]],
value = TRUE
),
invert = TRUE,
value = TRUE
)
predictors <- gsub("tvalue-", "", predictors, fixed = TRUE)
}


#Compute reference group size, find number of total subjects, subtract all
#instaces of the treatment-coded factor, remaining number is reference group
referencegroupsize <-
matrix(1, nrow = 1, ncol = length(predictors))
colnames(referencegroupsize) <- predictors

for (var in cat_vars) {
referencegroupsize[, grep(var, predictors, value = TRUE)] <-
NROW(updatedAttrs$model[, grep(":",
grep(var, colnames(updatedAttrs$model), value = TRUE),
invert = TRUE,
value = TRUE)]) -
sum(updatedAttrs$model[, grep(":",
grep(var, colnames(updatedAttrs$model), value = TRUE),
invert = TRUE,
value = TRUE)])
}

n.cols <- length(predictors)
n.row <- 0
if (is.matrix(buffer)) {
n.row <- nrow(buffer)
}
else {
n.row <- length(buffer)
}
output <- matrix(1, nrow = n.row, ncol = 2 * n.cols)
colnames(output) <-
c(paste0("hedgesg-", predictors),
paste0("hedgesg_var-", predictors))
for (column in predictors) {
n1 <- sum(updatedAttrs$model[, column])
n2 <- referencegroupsize[, column]
N <- n1 + n2
NN <- n1 * n2
df <- updatedAttrs$df[[1]][2]
output[, paste0("hedgesg-", column)] <-
buffer[, paste0("tvalue-", column)] * N / (sqrt(NN) * sqrt(df)) * J(N -
2)
output[, paste0("hedgesg_var-", column)] <-
(N) / (NN) + output[, paste0("hedgesg-", column)] ** 2 / (2 * (N - 2))
}
rownames(output) <- rownames(buffer)
attr(output, "likeVolume") <- attr(buffer, "likeVolume")

# run the garbage collector...
gcout <- gc()

return(output)
}

#' @describeIn vertexEffectSize mincEffectSize
#' @export
mincEffectSize <- function(buffer, predictors = NULL) {
eff_call <- match.call()
eff_call[[1]] <- quote(vertexEffectSize)
eval(eff_call)
}

#' @describeIn vertexEffectSize anatEffectSize
#' @export
anatEffectSize <- function(buffer, predictors = NULL) {
eff_call <- match.call()
eff_call[[1]] <- quote(vertexEffectSize)
eval(eff_call)
}
3 changes: 2 additions & 1 deletion R/minc_FDR.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ mincFDR <- function(buffer, ...) {
}
#' @describeIn mincFDR mincSingleDim
#' @export
mincFDR.mincSingleDim <- function(buffer, df, mask = NULL, method = "qvalue", ...) {
mincFDR.mincSingleDim <- function(buffer, df, mask = NULL, method = "fdr", ...) {
if (is.null(df)) {
stop("Error: need to specify the degrees of freedom")
}
message('default method has changed, specify method = "qvalue" to retain old implementation')
# if (length(df) == 1) {
# df <- c(1,df)
# }
Expand Down
6 changes: 4 additions & 2 deletions R/minc_anatomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,15 @@ create_anat_results <-

extra_structures <- results %>% filter_(~ is.na(Structure)) %>% .$indices
if(length(extra_structures) != 0)
message("Extra Structures found in files but not in labels: "
message(length(extra_structures),
" extra Structures found in files but not in labels: "
, paste0(extra_structures, collapse = ", "))

missing_structures <-
with(label_frame, Structure[! label %in% results$indices])
if(length(missing_structures) != 0)
message("Missing Structures found in labels but not in any files: "
message(length(missing_structures),
" missing Structures found in labels but not in any files: "
, paste0(missing_structures, collapse = ", "))

results <-
Expand Down
26 changes: 24 additions & 2 deletions R/minc_hierarchical_anatomy.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
#' low=1, high=10, symmetric = F, begin=50, end=-50)
#' }
hanatToVolume <- function(anatTree, labelVolume, column) {
if(is.null(getElement(anatTree, column)))
stop(column, " doesn't seem to be in your anatomy tree")

out <- array(0, dim=dim(labelVolume))
labels <- c()
values <- c()
Expand Down Expand Up @@ -269,19 +272,37 @@ hanatLmerEstimateDF <- function(buffer, n=50) {
#' hanat <- addVolumesToHierarchy(hdefs, allvols)
#' }
addVolumesToHierarchy <- function(hdefs, volumes){
#If hdefs$leafCount > regions in volumes, aggregation will fail as rowSums() is called on NULL volumes
if (hdefs$leafCount > dim(volumes)[[2]]){
stop("Your hierarchical definitions contain more leaves than regions in your volumes.\n",
"Consider pruning your hierarchy of subtrees that do not exist in your volumes.")
}
if (hdefs$leafCount < dim(volumes)[[2]]){
stop("Your hierarchical definitions contain fewer leaves than regions in your volumes.\n",
"Are you using the correct atlas labels?")
}
if (any(is.na(volumes))) {
warning("At least one anatomical region has a value of NA.\n",
"That region's mean volume will be NA, and all of its parents too.")
}
hanat <- Clone(hdefs)
volLabels <- as.integer(attributes(volumes)$anatIDs)
hanat$Do(function(x) {
if (isLeaf(x)) {
whichIndex <- which(volLabels == x$label)
x$volumes <- volumes[,whichIndex]
x$volumes <- volumes[, whichIndex]
x$meanVolume <- mean(x$volumes)
}
})

hanat$Do(function(x) x$meanVolume <- Aggregate(x, "meanVolume", sum), traversal="post-order")
hanat$Do(function(x) x$volumes <- Aggregate(x, "volumes", rowSums),
if (dim(volumes)[[1]]==1){
hanat$Do(function(x) x$volumes <- Aggregate(x, "volumes", sum),
traversal="post-order", filterFun = isNotLeaf)
} else {
hanat$Do(function(x) x$volumes <- Aggregate(x, "volumes", rowSums),
traversal="post-order", filterFun = isNotLeaf)
}
return(hanat)
}

Expand Down Expand Up @@ -437,6 +458,7 @@ makeMICeDefsHierachical <- function(defs, abijson) {

# split the definitions into one per line (i.e. left and right separate)
ldefs <- lateralizeDefs(defs)
tree$Do(function(n){ n$name <- gsub("/", " and ", n$name)})
# create the first version of the anatomical hierarchy
mh <- addHierarchyToDefs(ldefs, tree)
treeMH <- as.Node(mh)
Expand Down
Loading

0 comments on commit a02a04a

Please sign in to comment.