Skip to content

Commit 9562a47

Browse files
committed
reimplemented prep_fun; something had gone wrong when reverting a branch
1 parent ef502e3 commit 9562a47

File tree

3 files changed

+29
-115
lines changed

3 files changed

+29
-115
lines changed

R/qdecr.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ qdecr <- function(id,
104104
md <- imp2list(data)
105105

106106
# Assemble and check input arguments
107-
vw$input <- qdecr_check(id, md, margs, hemi, vertex, measure, model, target, project, dir_out_tree, clobber, fwhm, n_cores)
107+
vw$input <- qdecr_check(id, md, margs, hemi, vertex, measure, model, target, project, dir_out_tree, clobber, fwhm, n_cores, prep_fun)
108108
vw$mask <- qdecr_check_mask(mask, mask_path)
109109
vw$paths <- check_paths(vw, dir_tmp, dir_subj, dir_out, dir_fshome, mask_path)
110110

111111
# Check model
112-
vw$model <- qdecr_model(vw$input$model, vw$input$md, vw$input$id, vw$input$vertex, vw$input$margs, vw$paths$dir_tmp2)
112+
vw$model <- qdecr_model(vw$input$model, vw$input$prep_fun, vw$input$md, vw$input$id, vw$input$vertex, vw$input$margs, vw$paths$dir_tmp2)
113113

114114
# Check backing
115115
qdecr_check_backing(c(vw$model$backing, vw$model$backing_to_remove, vw$paths$backing_mgh), clobber)

R/qdecr_check.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ qdecr_check_backing <- function(backing, clobber){
1414
}
1515

1616
qdecr_check <- function(id, md, margs, hemi, vertex, measure, model, target,
17-
project, dir_out_tree, clobber, fwhm, n_cores){
17+
project, dir_out_tree, clobber, fwhm, n_cores, prep_fun){
1818

1919
# verify that id is correct and exists within data
2020
check_id(id, md)
@@ -27,6 +27,9 @@ qdecr_check <- function(id, md, margs, hemi, vertex, measure, model, target,
2727

2828
# Check whether the number of cores is correct
2929
check_cores(n_cores)
30+
31+
# Check whether prep_fun exists
32+
check_prep_fun(prep_fun)
3033

3134
# Return all output arguments assembled
3235
input <- list()
@@ -45,6 +48,7 @@ qdecr_check <- function(id, md, margs, hemi, vertex, measure, model, target,
4548
input[["fwhm"]] <- fwhm
4649
input[["fwhmc"]] <- paste0("fwhm", fwhm)
4750
input[["n_cores"]] <- n_cores
51+
input[["prep_fun"]] <- prep_fun
4852
input
4953
}
5054

@@ -167,6 +171,11 @@ check_paths <- function(vw, dir_tmp, dir_subj, dir_out, dir_fshome, mask_path){
167171
paths
168172
}
169173

174+
check_prep_fun <- function(prep_fun){
175+
tryCatch(get2(prep_fun), error = function(e) stop("Provided `prep_fun` cannot be found."))
176+
invisible(NULL)
177+
}
178+
170179
check_vertex <- function(vertex, id, md){
171180
stxt <- c("Please provide a unique, unused name to `vertex`. \n",
172181
"We only use it as an identifier in the model output.")

R/qdecr_model.R

Lines changed: 17 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,22 @@
1-
2-
qdecr_model <- function(model, md, id, vertex, margs, dir_tmp2){
3-
1+
qdecr_model <- function(model, prep_fun, md, id, vertex, margs, dir_tmp2) {
42
prepvw <- list(model = model, data = md, id = id,
5-
vertex = vertex, margs = margs)
3+
vertex = vertex, margs = margs, path = dir_tmp2)
64
class(prepvw) <- "prepvw"
7-
8-
if (prepvw$model == "QDECR::megha") {
9-
model_qdecr_megha(prepvw)
10-
} else if (prepvw$model == "RcppEigen::fastLm") {
11-
prepvw$so <- c("coef", "se", "t", "p", "resid")
12-
prepvw$backing <- paste0(dir_tmp2, "_", prepvw$so[1:4], "_backend")
13-
prepvw$backing_to_remove <- paste0(dir_tmp2, "_", prepvw$so[5], "_backend")
14-
model_RcppEigen_fastLm(prepvw)
15-
} else if (prepvw$model == "stats::glm") {
16-
model_stats_glm(prepvw)
17-
} else if (prepvw$model == "stats::lm") {
18-
model_stats_lm(prepvw)
19-
} else if (prepvw$model == "survival::coxph") {
20-
model_survival_coxph(prepvw)
21-
} else if (prepvw$model == "default") {
22-
prepvw$so <- c("coef", "se", "t", "p", "resid")
23-
prepvw$backing <- paste0(dir_tmp2, "_", prepvw$so[1:4], "_backend")
24-
prepvw$backing_to_remove <- paste0(dir_tmp2, "_", prepvw$so[5], "_backend")
25-
model_default(prepvw)
26-
}
5+
do.call2(prep_fun, list(prepvw = prepvw))
276
}
287

29-
model_default <- function(prepvw) {
30-
prepvw$ff <- "vw_default"
31-
prepvw$formula <- prepvw$margs$formula
32-
vw <- prepvw
33-
class(vw) <- c(vw$ff, "vw")
34-
vw
8+
qdecr_backing_path <- function(prepvw, to_keep, to_remove) {
9+
prepvw$so <- c(to_keep, to_remove)
10+
prepvw$backing <- paste0(prepvw$path, "_", prepvw$so[1:4], "_backend")
11+
prepvw$backing_to_remove <- paste0(prepvw$path, "_", prepvw$so[5], "_backend")
12+
return(prepvw)
3513
}
3614

37-
model_qdecr_megha <- function(prepvw){
38-
stop("`QDECR::megha` has not been implemented yet.")
39-
}
40-
41-
model_RcppEigen_fastLm <- function(prepvw){
15+
prep_fastlm <- function(prepvw){
16+
to_keep <- c("coef", "se", "t", "p")
17+
to_remove <- "resid"
18+
prepvw <- qdecr_backing_path(prepvw, to_keep, to_remove)
19+
4220
mf <- prepvw$margs
4321
if (is.null(mf$formula))
4422
stop("No `formula` set in margs.")
@@ -63,15 +41,15 @@ model_RcppEigen_fastLm <- function(prepvw){
6341
mx_test[, prepvw$vertex] <- 999
6442

6543
if (!identical(mx[[nn]], mx_test)) stop ("Somewhere in your formula you specified a special term related to your vertex measure",
66-
" (interaction, polynomial, AsIs, etc); `qdecr_fastlm` currently does not support this.")
67-
44+
" (interaction, polynomial, AsIs, etc); `qdecr_fastlm` currently does not support this.")
45+
6846
y <- model.response(mx[[nn]], "numeric")
6947

7048
if (nrow(mx[[nn]]) != nr) stop("The data that you are putting into the regression has missings! \n",
7149
"QDECR can't handle that yet; we will fix this soon!")
7250

7351
ys <- if(identical(unname(y), rep(999, nrow(mx[[nn]])))) "LHS" else "RHS"
74-
52+
7553
if (ys == "LHS") {
7654
mx_test2 <- mx_test
7755
mx_test2b <- model.matrix(mx_test2, object = mt, contrasts)
@@ -92,78 +70,5 @@ model_RcppEigen_fastLm <- function(prepvw){
9270
}
9371
class(vw) <- c(vw$ff, "vw")
9472
vw
73+
9574
}
96-
97-
model_stats_glm <- function(prepvw){
98-
stop("`stats::glm` has not been implemented yet.")
99-
}
100-
101-
model_stats_lm <- function(prepvw){
102-
mf <- prepvw$margs
103-
ret.x <- mf$x
104-
ret.y <- mf$y
105-
if (is.null(mf$drop.unused.levels))
106-
mf$drop.unused.levels <- TRUE
107-
if (is.null(mf$formula))
108-
stop("No `formula` set in margs.")
109-
iii <- c("formula", "data", "subset", "weights", "na.action", "offset")
110-
mf[iii] <- mf[!sapply(mf, is.symbol)][iii]
111-
mfz <- mf[match(c("formula", "data", "subset", "weights", "na.action", "offset"), names(mf), 0L)]
112-
nr <- nrow(prepvw$data[[1]])
113-
mx <- lapply(prepvw$data, function(x) {
114-
mfz$data <- x
115-
mfz$data[, prepvw$vertex] <- 999
116-
do.call2("stats::model.frame", mfz)
117-
})
118-
nn <- length(prepvw$data)
119-
if (mf$method == "model.frame")
120-
stop("`margs$method` is equal to `model.frame` (for the lm call). Try `qr`.") else
121-
if (mf$method != "qr")
122-
stop(gettextf("margs$method = '%s' is not supported in lm. Try 'qr'",
123-
method), domain = NA)
124-
mt <- attr(mx[[nn]], "terms")
125-
y <- model.response(mx[[nn]], "numeric")
126-
ys <- if(identical(unname(y), rep(999, nr))) "LHS" else "RHS"
127-
w <- as.vector(model.weights(mx[[nn]]))
128-
if (!is.null(w) && !is.numeric(w))
129-
stop("'margs$weights' must be a numeric vector (for the lm call)")
130-
offset <- as.vector(model.offset(mf))
131-
if (!is.null(offset)) {
132-
if (length(offset) != NROW(y))
133-
stop(gettextf("number of offsets is %d, should equal %d (number of observations) (in lm call)",
134-
length(offset), NROW(y)), domain = NA)
135-
}
136-
if (is.empty.model(mt)) stop("The provided model (to lm) is empty. Check your data + formula.")
137-
test <- model.matrix(mt, mx[[1]], contrasts)
138-
test2 <- model.matrix(mt, mx[[nn]], contrasts)
139-
mm <- NULL
140-
prepvw$ff <- "vw_lm_fit_slow"
141-
if (prepvw$vertex %in% colnames(attr(mt, "factors")) || ys == "LHS"){
142-
# mfz2 <- mfz
143-
# mfz2$data <- prepvw$data[[1]]
144-
# mfz2$data[, prepvw$vertex] <- mfz$data[, prepvw$vertex]
145-
# mm2 <- do.call2("stats::model.frame", mfz2)
146-
if(identical(test, test2)){
147-
mm <- lapply(mx, model.matrix, object = mt, contrasts)
148-
ff <- if (is.null(w)) "vw_lm_fit" else "vw.lm_wfit"
149-
vw <- list(mm = mm, ff = ff, vertex = prepvw$vertex, ys = ys,
150-
y = y, w = w, offset = offset,
151-
singular.ok = prepvw$margs$singular.ok)
152-
} else {
153-
warning("Your formula for `lm` contains computed terms. \n",
154-
"We will rely on the slower implementation of our lm.")
155-
vw <- prepvw
156-
}
157-
} else {
158-
warning("Your formula for `lm` contains complicated terms. \n",
159-
"We will rely on the slower implementation of our lm.")
160-
vw <- prepvw
161-
}
162-
class(vw) <- c(vw$ff, "vw")
163-
vw
164-
}
165-
166-
model_survival_coxph <- function(prepvw){
167-
stop("`survival::coxph` has not been implemented yet.")
168-
}
169-

0 commit comments

Comments
 (0)