test_that("regular grid", { expect_snapshot( error = TRUE, grid_regular(mixture(), trees(), levels = 1:4) ) expect_equal( nrow(grid_regular(mixture(), trees(), levels = 2)), 4 ) expect_equal( nrow(grid_regular(mixture(), trees(), levels = 2, filter = trees == 1)), 2 ) expect_equal( nrow(grid_regular(mixture(), trees(), levels = 2:3)), prod(2:3) ) expect_equal( dplyr::n_distinct( select( grid_regular(mixture(), trees(), levels = c(trees = 2, mixture = 3)), trees ) ), 2 ) expect_equal( dplyr::n_distinct( select( grid_regular(mixture(), trees(), levels = c(mixture = 3, trees = 2)), trees ) ), 2 ) expect_snapshot( error = TRUE, grid_regular(mixture(), trees(), size = 3) ) expect_equal( grid_regular(list(mixture(), trees()), levels = 3), grid_regular(mixture(), trees(), levels = 3) ) expect_snapshot( error = TRUE, grid_regular(mixture(), trees(), levels = c(2, trees = 4)) ) expect_snapshot( error = TRUE, grid_regular(mixture(), trees(), levels = c(wrong = 2, names = 4)) ) }) test_that("random grid", { expect_equal( nrow(grid_random(mixture(), trees(), size = 2)), 2 ) set.seed(1) expect_lte( # There are 6x2 possible combinations nrow(grid_random(prod_degree(), prune_method(), size = 50)), 12 ) expect_equal( nrow(grid_random(list(mixture(), trees()), size = 2)), 2 ) }) test_that("wrong argument name", { skip_if_below_r_version("3.6") p <- parameters(penalty(), mixture()) set.seed(1) expect_snapshot(error = TRUE, { grid_space_filling(p, levels = 5, type = "latin_hypercube") }) expect_snapshot(error = TRUE, { grid_space_filling(p, levels = 5, type = "max_entropy") }) expect_snapshot(error = TRUE, grid_random(p, levels = 5)) expect_snapshot(error = TRUE, grid_regular(p, size = 5)) }) test_that("filter arg yields same results", { p <- parameters(penalty(), mixture()) expect_equal( filter(with_seed(36L, grid_random(p)), penalty < 0.01), with_seed(36L, grid_random(p, filter = penalty < 0.01)) ) expect_equal( filter(with_seed(36L, grid_random(p)), penalty > 0.001), with_seed(36L, grid_random(p, filter = penalty > 0.001)) ) expect_equal( filter(with_seed(36L, grid_random(p)), mixture == 0.01), with_seed(36L, grid_random(p, filter = mixture == 0.01)) ) }) test_that("grid_random validates inputs", { expect_snapshot(error = TRUE, grid_random(penalty(), size = "five")) expect_snapshot(error = TRUE, grid_random(penalty(), size = -1)) expect_snapshot(error = TRUE, grid_random(penalty(), original = "yes")) }) test_that("grid_random() errors with non-param inputs", { # default method expect_snapshot(error = TRUE, grid_random()) expect_snapshot(error = TRUE, grid_random("not a param")) # param method expect_snapshot(error = TRUE, grid_random(penalty(), "min_n")) expect_snapshot(error = TRUE, grid_random(mtry(), "min_n")) # list method expect_snapshot(error = TRUE, grid_random(list())) expect_snapshot(error = TRUE, grid_random(list(penalty(), "min_n"))) expect_snapshot(error = TRUE, grid_random(list(mtry(), "min_n"))) }) test_that("grid_random.parameters() checks for NA", { p <- parameters(penalty()) p <- update(p, penalty = NA) expect_snapshot(error = TRUE, grid_random(p)) }) test_that("grid_random() errors with params containing unknowns", { # parameters method expect_snapshot(error = TRUE, grid_random(parameters(mtry()))) # param method expect_snapshot(error = TRUE, grid_random(mtry())) expect_snapshot(error = TRUE, grid_random(mtry(), sample_size())) # list method expect_snapshot(error = TRUE, grid_random(list(mtry()))) expect_snapshot(error = TRUE, grid_random(list(mtry_custom_name = mtry()))) expect_snapshot(error = TRUE, grid_random(list(mtry(), sample_size()))) }) test_that("grid_random() errors with duplicate parameter ids", { # param method expect_snapshot(error = TRUE, grid_random(penalty(), penalty())) # list method expect_snapshot(error = TRUE, grid_random(list(a = penalty(), a = penalty()))) }) test_that("grid_regular validates inputs", { expect_snapshot(error = TRUE, grid_regular(penalty(), levels = "three")) expect_snapshot(error = TRUE, grid_regular(penalty(), levels = -1)) expect_snapshot(error = TRUE, grid_regular(penalty(), original = "yes")) }) test_that("grid_regular() errors with non-param inputs", { # default method expect_snapshot(error = TRUE, grid_regular()) expect_snapshot(error = TRUE, grid_regular("not a param")) # param method expect_snapshot(error = TRUE, grid_regular(penalty(), "min_n")) expect_snapshot(error = TRUE, grid_regular(mtry(), "min_n")) # list method expect_snapshot(error = TRUE, grid_regular(list())) expect_snapshot(error = TRUE, grid_regular(list(penalty(), "min_n"))) expect_snapshot(error = TRUE, grid_regular(list(mtry(), "min_n"))) }) test_that("grid_regular.parameters() checks for NA", { p <- parameters(penalty()) p <- update(p, penalty = NA) expect_snapshot(error = TRUE, grid_regular(p)) }) test_that("grid_regular() errors with params containing unknowns", { # parameters method expect_snapshot(error = TRUE, grid_regular(parameters(mtry()))) # param method expect_snapshot(error = TRUE, grid_regular(mtry())) expect_snapshot(error = TRUE, grid_regular(penalty(), sample_size())) # list method expect_snapshot(error = TRUE, grid_regular(list(mtry()))) expect_snapshot(error = TRUE, grid_regular(list(mtry_custom_name = mtry()))) expect_snapshot(error = TRUE, grid_regular(list(mtry(), sample_size()))) }) test_that("grid_regular() errors with duplicate parameter ids", { # param method expect_snapshot(error = TRUE, grid_regular(penalty(), penalty())) # list method expect_snapshot( error = TRUE, grid_regular(list(a = penalty(), a = penalty())) ) }) test_that("new param grid from conventional data frame", { x <- data.frame(num_comp = 1:3) expect_no_condition(y <- dials:::new_param_grid(x)) expect_true(tibble::is_tibble(y)) # or from a matrix? expect_snapshot( error = TRUE, new_param_grid(as.matrix(x)) ) })