Skip to content

Commit

Permalink
Fixes ranger test failures on CRAN
Browse files Browse the repository at this point in the history
  • Loading branch information
edgararuiz-zz committed Jul 15, 2019
1 parent 0c9514e commit 510b71e
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: tidypredict
Version: 0.4.1
Version: 0.4.2
Title: Run Predictions Inside the Database
Description: It parses a fitted 'R' model object, and returns a formula
in 'Tidy Eval' code that calculates the predictions.
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ S3method(tidypredict_fit,model_fit)
S3method(tidypredict_fit,party)
S3method(tidypredict_fit,pm_regression)
S3method(tidypredict_fit,pm_tree)
S3method(tidypredict_fit,pm_xgb)
S3method(tidypredict_fit,randomForest)
S3method(tidypredict_fit,ranger)
S3method(tidypredict_fit,xgb.Booster)
Expand Down
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# tidypredict 0.4.1
# tidypredict 0.4.2

- Simplifies tests that verify `ranger`

- Adds fit method for parsed `xgboost` models

- Sets conditional requirement for `xgboost`, for test and vignette

Expand Down
2 changes: 1 addition & 1 deletion R/model-xgboost.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ parse_model.xgb.Booster <- function(model) {

pm <- list()
pm$general$model <- "xgb.Booster"
pm$general$type <- "tree"
pm$general$type <- "xgb"
pm$general$niter <- model$niter
pm$general$params <- wosilent
pm$general$feature_names <- model$feature_names
Expand Down
5 changes: 5 additions & 0 deletions R/predict-fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@ tidypredict_fit.pm_regression <- function(model) {
tidypredict_fit.pm_tree <- function(model) {
build_fit_formula_rf(model)
}

#' @export
tidypredict_fit.pm_xgb <- function(model) {
build_fit_formula_xgb(model)
}
2 changes: 1 addition & 1 deletion cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Release summary

* Switches `xgboost` to conditional requirement for tests and vignette
* Addresses `ranger` test failures

## Test environments
* Local windows 10 install, R 3.6.0
Expand Down
9 changes: 0 additions & 9 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
library(testthat)
library(tidypredict)
library(purrr)
library(dplyr)
library(Cubist)
library(mlbench)
library(rlang)
library(partykit)
library(yaml)

if (requireNamespace("xgboost", quietly = TRUE)) library(xgboost)

test_check("tidypredict")
15 changes: 6 additions & 9 deletions tests/testthat/test-ranger.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
num_trees <- 20
num_trees <- 10

run_test <- function(model, test_formula = TRUE) {
tf <- tidypredict_fit(model)
Expand All @@ -12,23 +12,20 @@ run_test <- function(model, test_formula = TRUE) {
})
if(test_formula) {
test_that("Returns expected case_when() dplyr formula", {
expect_equal(
rlang::expr_text(tf[[1]]),
"case_when(Petal.Length < 2.45 ~ \"setosa\", Petal.Width < 1.45 & \n Petal.Width < 1.65 & Petal.Length >= 2.45 ~ \"versicolor\", \n Sepal.Length < 6.55 & Petal.Width >= 1.65 & Petal.Length >= \n 2.45 ~ \"virginica\", Petal.Length < 4.95 & Petal.Width >= \n 1.45 & Petal.Width < 1.65 & Petal.Length >= 2.45 ~ \"versicolor\", \n Petal.Width < 1.75 & Sepal.Length >= 6.55 & Petal.Width >= \n 1.65 & Petal.Length >= 2.45 ~ \"versicolor\", Petal.Width >= \n 1.75 & Sepal.Length >= 6.55 & Petal.Width >= 1.65 & Petal.Length >= \n 2.45 ~ \"virginica\", Sepal.Length >= 6.15 & Petal.Length >= \n 4.95 & Petal.Width >= 1.45 & Petal.Width < 1.65 & Petal.Length >= \n 2.45 ~ \"virginica\", Sepal.Width < 2.45 & Sepal.Length < \n 6.15 & Petal.Length >= 4.95 & Petal.Width >= 1.45 & Petal.Width < \n 1.65 & Petal.Length >= 2.45 ~ \"virginica\", Sepal.Width >= \n 2.45 & Sepal.Length < 6.15 & Petal.Length >= 4.95 & Petal.Width >= \n 1.45 & Petal.Width < 1.65 & Petal.Length >= 2.45 ~ \"versicolor\")"
)
expect_is(tidypredict_fit(pm), "list")
})
}
}

context("ranger")
run_test(
ranger::ranger(Species ~ ., data = iris, num.trees = num_trees, seed = 100)
ranger::ranger(Species ~ ., data = iris, num.trees = num_trees, seed = 100, num.threads = 2)
)

context("ranger-classification")
run_test(
parsnip::fit(
parsnip::set_engine(parsnip::rand_forest(trees = num_trees, mode = "classification"), "ranger", seed = 100),
parsnip::set_engine(parsnip::rand_forest(trees = num_trees, mode = "classification"), "ranger", seed = 100, num.threads = 2),
Species ~ .,
data = iris
), test_formula = FALSE
Expand All @@ -37,15 +34,15 @@ run_test(
context("ranger-parsnip")
run_test(
parsnip::fit(
parsnip::set_engine(parsnip::rand_forest(trees = num_trees), "ranger", seed = 100),
parsnip::set_engine(parsnip::rand_forest(trees = num_trees), "ranger", seed = 100, num.threads = 2),
Species ~ .,
data = iris
)
)

context("ranger-saved")
test_that("Model can be saved and re-loaded", {
model <- ranger::ranger(Species ~ ., data = iris, num.trees = num_trees, seed = 100)
model <- ranger::ranger(Species ~ ., data = iris, num.trees = num_trees, seed = 100, num.threads = 2)
mp <- tempfile(fileext = ".yml")
yaml::write_yaml(parse_model(model), mp)
l <- yaml::read_yaml(mp)
Expand Down

0 comments on commit 510b71e

Please sign in to comment.