-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
let fit_xy()
take dgCMatrix input
#1121
Changes from 11 commits
4584a9b
d1d4df4
bbe0732
6e2fb00
44f2f81
fb14d88
de70c14
28420f6
fddd590
2b8f56e
ae90e84
124f925
604bece
7718d67
f2faed9
f0f92e9
616b548
777f48a
a60a928
1971f05
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
to_sparse_data_frame <- function(x, object) { | ||
if (methods::is(x, "sparseMatrix")) { | ||
if (allow_sparse(object)) { | ||
x <- sparsevctrs::coerce_to_sparse_data_frame(x) | ||
} else { | ||
cli::cli_warn(c( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to update the PR to make this a failure. |
||
"!" = "{.arg x} is a sparse matrix, but model doesn't accept that.", | ||
"i" = "Converted {.arg x} to data.frame." | ||
)) | ||
x <- as.data.frame(x) | ||
} | ||
} | ||
x | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# to_sparse_data_frame() is used correctly | ||
|
||
Code | ||
fit_xy(spec, x = mtcars[, -1], y = mtcars[, 1]) | ||
Condition | ||
Error in `to_sparse_data_frame()`: | ||
! x is not sparse | ||
|
||
--- | ||
|
||
Code | ||
fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
Condition | ||
Error in `to_sparse_data_frame()`: | ||
! x is spare, and sparse is not allowed | ||
|
||
--- | ||
|
||
Code | ||
fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
Condition | ||
Error in `to_sparse_data_frame()`: | ||
! x is spare, and sparse is allowed | ||
|
||
# maybe_sparse_matrix() is used correctly | ||
|
||
Code | ||
fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
Condition | ||
Error in `maybe_sparse_matrix()`: | ||
! sparse vectors detected | ||
|
||
--- | ||
|
||
Code | ||
fit_xy(spec, x = mtcars[, -1], y = mtcars[, 1]) | ||
Condition | ||
Error in `maybe_sparse_matrix()`: | ||
! no sparse vectors detected | ||
|
||
--- | ||
|
||
Code | ||
fit_xy(spec, x = as.data.frame(mtcars)[, -1], y = as.data.frame(mtcars)[, 1]) | ||
Condition | ||
Error in `maybe_sparse_matrix()`: | ||
! no sparse vectors detected | ||
|
||
--- | ||
|
||
Code | ||
fit_xy(spec, x = tibble::as_tibble(mtcars)[, -1], y = tibble::as_tibble(mtcars)[, | ||
1]) | ||
Condition | ||
Error in `maybe_sparse_matrix()`: | ||
! no sparse vectors detected | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
test_that("sparse matrices can be passed to `fit_xy()", { | ||
skip_if_not_installed("LiblineaR") | ||
|
||
hotel_data <- sparse_hotel_rates() | ||
|
||
spec <- svm_linear() %>% | ||
set_mode("regression") %>% | ||
set_engine("LiblineaR") | ||
|
||
expect_no_error( | ||
lm_fit <- fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if this didn't work, it would take quite a lot longer to run which we would notice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we? Does "didn't work" mean a failure or just inefficient? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inefficient. it should pop up as a "this test is running a little long" from CRAN / CMD R Check |
||
) | ||
}) | ||
|
||
test_that("to_sparse_data_frame() is used correctly", { | ||
skip_if_not_installed("LiblineaR") | ||
|
||
local_mocked_bindings( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The main testing strategy follows this template:
|
||
to_sparse_data_frame = function(x, object) { | ||
if (methods::is(x, "sparseMatrix")) { | ||
if (allow_sparse(object)) { | ||
stop("x is spare, and sparse is allowed") | ||
} else { | ||
stop("x is spare, and sparse is not allowed") | ||
} | ||
} | ||
stop("x is not sparse") | ||
} | ||
) | ||
|
||
hotel_data <- sparse_hotel_rates() | ||
|
||
spec <- linear_reg() %>% | ||
set_engine("lm") | ||
|
||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = mtcars[, -1], y = mtcars[, 1]) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
) | ||
|
||
spec <- svm_linear() %>% | ||
set_mode("regression") %>% | ||
set_engine("LiblineaR") | ||
|
||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
) | ||
}) | ||
|
||
test_that("maybe_sparse_matrix() is used correctly", { | ||
skip_if_not_installed("LiblineaR") | ||
|
||
local_mocked_bindings( | ||
maybe_sparse_matrix = function(x) { | ||
if (any(vapply(x, sparsevctrs::is_sparse_vector, logical(1)))) { | ||
stop("sparse vectors detected") | ||
} else { | ||
stop("no sparse vectors detected") | ||
} | ||
} | ||
) | ||
|
||
hotel_data <- sparse_hotel_rates() | ||
|
||
spec <- svm_linear() %>% | ||
set_mode("regression") %>% | ||
set_engine("LiblineaR") | ||
|
||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = hotel_data[, -1], y = hotel_data[, 1]) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = mtcars[, -1], y = mtcars[, 1]) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = as.data.frame(mtcars)[, -1], y = as.data.frame(mtcars)[, 1]) | ||
) | ||
expect_snapshot( | ||
error = TRUE, | ||
fit_xy(spec, x = tibble::as_tibble(mtcars)[, -1], y = tibble::as_tibble(mtcars)[, 1]) | ||
) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it uses dev version because of this bug fix: r-lib/sparsevctrs@9c22ca9
sparsevctrs will of course be merged in time for parsnip release