From c13d98e9e6918ff210ff76c23ec646437ffc96a2 Mon Sep 17 00:00:00 2001 From: Emil Hvitfeldt Date: Fri, 17 Jan 2025 09:42:29 -0800 Subject: [PATCH] only do things in toggle_sparsity if there is a recipe --- R/sparsevctrs.R | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/R/sparsevctrs.R b/R/sparsevctrs.R index ce0ec1e..e091a4e 100644 --- a/R/sparsevctrs.R +++ b/R/sparsevctrs.R @@ -3,31 +3,32 @@ is_sparse_matrix <- function(x) { } toggle_sparsity <- function(object, data) { - toggle_sparse <- "no" - - if (allow_sparse(expect_spec_parsnip(object))) { - if (has_preprocessor_recipe(object)) { - est_sparsity <- recipes::.recipes_estimate_sparsity( - extract_preprocessor(object) - ) - } else { - est_sparsity <- sparsevctrs::sparsity(data, sample = 1000) - } + if ( + allow_sparse(object$fit$actions$model$spec) && + has_preprocessor_recipe(object) + ) { + est_sparsity <- recipes::.recipes_estimate_sparsity( + extract_preprocessor(object) + ) pred_log_fold <- pred_log_fold( est_sparsity, extract_spec_parsnip(object)$engine, nrow(data) ) + + toggle_sparse <- "no" + if (pred_log_fold > 0) { toggle_sparse <- "yes" } + + object$pre$actions$recipe$recipe <- recipes::.recipes_toggle_sparse_args( + object$pre$actions$recipe$recipe, + choice = toggle_sparse + ) } - object$pre$actions$recipe$recipe <- recipes::.recipes_toggle_sparse_args( - object$pre$actions$recipe$recipe, - choice = toggle_sparse - ) object }