Skip to content

Commit 733fc23

Browse files
committed
fix code and test compliance
1 parent dba34e6 commit 733fc23

File tree

5 files changed

+36
-130
lines changed

5 files changed

+36
-130
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Encoding: UTF-8
4141
VignetteBuilder: knitr
4242
Imports: assertthat, caret, deldir, digest, distances, dplyr, filelock,
4343
furrr (>= 0.2.0), ggplot2, methods, purrr, ranger, readr, ridge, rlang,
44-
rlist, R.utils, stats, stringr, tibble, tidyr, utils, withr
44+
rlist, R.utils, stats, stringr, tibble, tidyr, tidyselect, utils, withr
4545
Suggests: BiocStyle, covr, earth, future, igraph (>= 1.2.7), iml, kernlab,
4646
knitr, MASS, rmarkdown, RSNNS, testthat (>= 3.0.0), xgboost
4747
RoxygenNote: 7.2.0

NEWS.md

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# mistyR 1.5.x
22
- Added different modeling functions. Might not be completely backwards compatible!
33

4-
54
# mistyR 1.4.0
65

76
- Release version for Bioconductor 3.15. See changes for 1.3.x.

R/misty.R

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ run_misty <- function(views, results.folder = "results", seed = 42,
134134
model.function = random_forest_model, ...) {
135135

136136
model.name <- as.character(rlang::enexpr(model.function))
137+
138+
if(!exists(model.name, envir = globalenv()))
139+
model.function <- utils::getFromNamespace(model.name, "mistyR")
137140

138141
normalized.results.folder <- R.utils::getAbsolutePath(results.folder)
139142

R/model-functions.R

+13-13
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ gradient_boosting_model <- function(view_data, target, seed, k = 10, ...) {
114114
dplyr::select(-tidyselect::all_of(target)) %>%
115115
as.matrix()
116116

117-
label.hat <- predict(model, pred.test)
117+
label.hat <- stats::predict(model, pred.test)
118118

119119
tibble::tibble(index = holdout, prediction = label.hat)
120-
}) %>% dplyr::arrange(index)
120+
}) %>% dplyr::arrange(.data$index)
121121

122122
predictors <- view_data %>%
123123
dplyr::select(-tidyselect::all_of(target)) %>%
@@ -214,7 +214,7 @@ bagged_mars_model <- function(view_data, target, seed,
214214

215215
oob <- seq.int(1, nrow(view_data))[!(seq.int(1, nrow(view_data)) %in% bag)]
216216

217-
pred <- predict(model, view_data[oob, ])
217+
pred <- stats::predict(model, view_data[oob, ])
218218
list(
219219
model = model,
220220
prediction = tibble::tibble(index = oob, prediction = as.vector(pred))
@@ -225,9 +225,9 @@ bagged_mars_model <- function(view_data, target, seed,
225225
predictions <- purrr::map_dfr(models, function(model) {
226226
tibble::tibble(model$prediction)
227227
}) %>%
228-
dplyr::group_by(index) %>%
229-
dplyr::summarise(prediction = mean(prediction)) %>%
230-
dplyr::arrange(index)
228+
dplyr::group_by(.data$index) %>%
229+
dplyr::summarise(prediction = mean(.data$prediction)) %>%
230+
dplyr::arrange(.data$index)
231231

232232
assertthat::assert_that(nrow(predictions) == nrow(view_data),
233233
msg = "There are too few bags to get OOB predictions for all observations.
@@ -305,10 +305,10 @@ mars_model <- function(view_data, target, seed, approx = 1.0, k = 10, ...) {
305305

306306
model <- do.call(earth::earth, algo.arguments)
307307

308-
label.hat <- predict(model, test)
308+
label.hat <- stats::predict(model, test)
309309

310310
tibble::tibble(index = holdout, prediction = label.hat[,1])
311-
}) %>% dplyr::arrange(index)
311+
}) %>% dplyr::arrange(.data$index)
312312

313313
algo.arguments.wm <- list(
314314
formula = stats::as.formula(paste0(target, " ~ .")),
@@ -365,10 +365,10 @@ linear_model <- function(view_data, target, seed, k = 10, ...) {
365365

366366
model <- do.call(stats::lm, algo.arguments)
367367

368-
pred <- predict.lm(model, view_data[holdout, ])
368+
pred <- stats::predict(model, view_data[holdout, ])
369369

370370
tibble::tibble(index = holdout, prediction = as.vector(pred))
371-
}) %>% dplyr::arrange(index)
371+
}) %>% dplyr::arrange(.data$index)
372372

373373
algo.arguments.wm <- list(
374374
formula = stats::as.formula(paste0(target, " ~ .")),
@@ -443,7 +443,7 @@ svm_model <- function(view_data, target, seed, approx = 0.4, k = 10, ...) {
443443
pred <- kernlab::predict(model, view_data[holdout, ])
444444

445445
tibble::tibble(index = holdout, prediction = as.vector(pred))
446-
}) %>% dplyr::arrange(index)
446+
}) %>% dplyr::arrange(.data$index)
447447

448448
algo.arguments.wm <- list(
449449
x = stats::as.formula(paste0(target, " ~ .")),
@@ -533,10 +533,10 @@ mlp_model <- function(view_data, target, seed, approx = 0.6, k = 10, ...) {
533533

534534
model <- do.call(RSNNS::mlp, algo.arguments)
535535

536-
label.hat <- predict(model, X_test)
536+
label.hat <- stats::predict(model, X_test)
537537

538538
tibble::tibble(index = holdout, prediction = label.hat[,1])
539-
}) %>% dplyr::arrange(index)
539+
}) %>% dplyr::arrange(.data$index)
540540

541541
algo.arguments.wm <- list(
542542
x = X,

tests/testthat/test-misty.R

+19-115
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,20 @@ test_that("warning raised if variance of variable is 0", {
9999
})
100100

101101
test_that("all models work and produce the correct output", {
102-
functions <- list("rf" = random_forest_model,
103-
"bag_mars" = bagged_mars_model,
104-
"mars" = mars_model,
105-
"linear" = linear_model,
106-
"svm" = svm_model,
107-
"boosting" = gradient_boosting_model,
108-
"mpl" = mlp_model)
102+
functions <- list("rf" = rlang::expr(random_forest_model),
103+
"bag_mars" = rlang::expr(bagged_mars_model),
104+
"mars" = rlang::expr(mars_model),
105+
"linear" = rlang::expr(linear_model),
106+
"svm" = rlang::expr(svm_model),
107+
"boosting" = rlang::expr(gradient_boosting_model),
108+
"mpl" = rlang::expr(mlp_model))
109109

110110
ncols <- 5
111111
expr <- generate_random_tibble(100, ncols)
112112
misty.views <- create_initial_view(expr)
113113

114-
misty.test <- purrr::map(functions, function(fun) {
115-
suppressWarnings(misty.results <- run_misty(misty.views, model.function = fun) %>%
114+
misty.test <- purrr::walk(functions, function(fun) {
115+
suppressWarnings(misty.results <- run_misty(misty.views, model.function = !!fun) %>%
116116
collect_results()
117117
)
118118
expect_true(dir.exists("results"))
@@ -135,114 +135,18 @@ test_that("ellipsis arguments can be passed to the provided ML models", {
135135
misty.views <- create_initial_view(expr) %>%
136136
add_paraview(positions = pos, l = 10)
137137

138-
# random forest
139-
start <- Sys.time()
140-
suppressWarnings(
141-
misty.test <- run_misty(misty.views, model.function = random_forest_model)
142-
)
143-
end <- Sys.time()
144-
first.run = end - start
145-
146-
start <- Sys.time()
147-
suppressWarnings(
148-
misty.test <- run_misty(misty.views, model.function = random_forest_model,
149-
num.trees = 2000)
150-
)
151-
end <- Sys.time()
152-
second.run = end - start
153-
testthat::expect_true(first.run < second.run)
154-
155-
# bagged mars
156-
start <- Sys.time()
157-
suppressWarnings(
158-
misty.test <- run_misty(misty.views, model.function = bagged_mars_model,
159-
degree = 1)
160-
)
161-
end <- Sys.time()
162-
first.run = end - start
163-
164-
start <- Sys.time()
165-
suppressWarnings(
166-
misty.test <- run_misty(misty.views, model.function = bagged_mars_model,
167-
n.bags = 50)
138+
suppressWarnings(misty.test <- run_misty(misty.views, model.function = mars_model,
139+
degree = 3, nk = 30, cached = TRUE)
168140
)
169-
end <- Sys.time()
170-
second.run = end - start
171-
testthat::expect_true(first.run < second.run)
172141

173-
# mars
174-
start <- Sys.time()
175-
suppressWarnings(
176-
misty.test <- run_misty(misty.views, model.function = mars_model,
177-
degree = 3, nk = 30)
178-
)
179-
end <- Sys.time()
180-
first.run = end - start
181-
182-
start <- Sys.time()
183-
suppressWarnings(
184-
misty.test <- run_misty(misty.views, model.function = mars_model,
185-
degree = 3, nk = 30)
186-
)
187-
end <- Sys.time()
188-
second.run = end - start
189-
testthat::expect_true(first.run < second.run)
190-
191-
# svm
192-
start <- Sys.time()
193-
suppressWarnings(
194-
misty.test <- run_misty(misty.views, model.function = svm_model,
195-
C = 1)
196-
)
197-
end <- Sys.time()
198-
first.run = end - start
199-
200-
start <- Sys.time()
201-
suppressWarnings(
202-
misty.test <- run_misty(misty.views, model.function = svm_model,
203-
C = 100)
204-
)
205-
end <- Sys.time()
206-
second.run = end - start
207-
testthat::expect_true(first.run < second.run)
208-
209-
# gradient boosting
210-
start <- Sys.time()
211-
suppressWarnings(
212-
misty.test <- run_misty(misty.views, model.function = gradient_boosting_model,
213-
booster = "gbtree", nrounds = 10)
214-
)
215-
end <- Sys.time()
216-
first.run = end - start
217-
218-
start <- Sys.time()
219-
suppressWarnings(
220-
misty.test <- run_misty(misty.views, model.function = gradient_boosting_model,
221-
booster = "gbtree", nrounds = 20)
222-
)
223-
end <- Sys.time()
224-
second.run = end - start
225-
testthat::expect_true(first.run < second.run)
226-
227-
# multi-layer perceptron
228-
start <- Sys.time()
229-
suppressWarnings(
230-
misty.test <- run_misty(misty.views, model.function = mlp_model,
231-
size = c(1), maxit = 1)
232-
)
233-
end <- Sys.time()
234-
first.run = end - start
235-
236-
start <- Sys.time()
237-
suppressWarnings(
238-
misty.test <- run_misty(misty.views, model.function = mlp_model,
239-
size = c(10), maxit = 100)
240-
)
241-
end <- Sys.time()
242-
second.run = end - start
243-
testthat::expect_true(first.run < second.run)
244-
245-
unlink("results", recursive = TRUE)
142+
cache.folder <- paste0(".misty.temp/", misty.views[["misty.uniqueid"]])
143+
cached.files <- list.files(cache.folder)
144+
145+
expect_true(all(stringr::str_detect(cached.files, "mars_model")) &
146+
all(stringr::str_detect(cached.files, "degree.3.nk.30")))
147+
148+
clear_cache()
149+
unlink("results", recursive = TRUE)
246150
})
247151

248152
test_that("k for cv , n.bags for bagging can be changed and approx works", {

0 commit comments

Comments
 (0)