Skip to content

Commit e332d2b

Browse files
committed
update sparse_hotel_rates with tibble argument
1 parent 9bf1d2e commit e332d2b

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

tests/testthat/helper-objects.R

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ is_tf_ok <- function() {
2828
# ------------------------------------------------------------------------------
2929
# For sparse tibble testing
3030

31-
sparse_hotel_rates <- function() {
31+
sparse_hotel_rates <- function(tibble = FALSE) {
3232
# 99.2 sparsity
3333
hotel_rates <- modeldata::hotel_rates
3434

@@ -49,5 +49,15 @@ sparse_hotel_rates <- function() {
4949
)
5050

5151
res <- as.matrix(res)
52-
Matrix::Matrix(res, sparse = TRUE)
52+
res <- Matrix::Matrix(res, sparse = TRUE)
53+
54+
if (tibble) {
55+
res <- sparsevctrs::coerce_to_sparse_tibble(res)
56+
57+
# materialize outcome
58+
withr::local_options("sparsevctrs.verbose_materialize" = NULL)
59+
res$avg_price_per_room <- res$avg_price_per_room[]
60+
}
61+
62+
res
5363
}

tests/testthat/test-sparsevctrs.R

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ test_that("sparse tibble can be passed to `fit()", {
22
skip_if_not_installed("xgboost")
33
withr::local_options("sparsevctrs.verbose_materialize" = 3)
44

5-
hotel_data <- sparse_hotel_rates()
6-
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
5+
hotel_data <- sparse_hotel_rates(tibble = TRUE)
76

87
spec <- boost_tree() %>%
98
set_mode("regression") %>%
@@ -54,11 +53,7 @@ test_that("sparse matrix can be passed to `fit()", {
5453
test_that("sparse tibble can be passed to `fit_xy()", {
5554
skip_if_not_installed("xgboost")
5655

57-
hotel_data <- sparse_hotel_rates()
58-
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
59-
60-
# materialize outcome
61-
hotel_data$avg_price_per_room <- hotel_data$avg_price_per_room[]
56+
hotel_data <- sparse_hotel_rates(tibble = TRUE)
6257

6358
withr::local_options("sparsevctrs.verbose_materialize" = 3)
6459

@@ -108,11 +103,7 @@ test_that("sparse matrices can be passed to `fit_xy()", {
108103
test_that("sparse tibble can be passed to `predict()", {
109104
skip_if_not_installed("ranger")
110105

111-
hotel_data <- sparse_hotel_rates()
112-
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
113-
114-
# materialize outcome
115-
hotel_data$avg_price_per_room <- hotel_data$avg_price_per_room[]
106+
hotel_data <- sparse_hotel_rates(tibble = TRUE)
116107

117108
withr::local_options("sparsevctrs.verbose_materialize" = 3)
118109

@@ -191,8 +182,7 @@ test_that("sparse data work with xgboost engine", {
191182
predict(tree_fit, hotel_data)
192183
)
193184

194-
hotel_data <- sparsevctrs::coerce_to_sparse_tibble(hotel_data)
195-
185+
hotel_data <- sparse_hotel_rates(tibble = TRUE)
196186

197187
expect_snapshot(
198188
error = TRUE,
@@ -203,11 +193,6 @@ test_that("sparse data work with xgboost engine", {
203193
predict(tree_fit, hotel_data)
204194
)
205195

206-
# materialize outcome
207-
withr::local_options("sparsevctrs.verbose_materialize" = NULL)
208-
hotel_data$avg_price_per_room <- hotel_data$avg_price_per_room[]
209-
withr::local_options("sparsevctrs.verbose_materialize" = 3)
210-
211196
expect_no_error(
212197
tree_fit <- fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1])
213198
)

0 commit comments

Comments
 (0)