Skip to content

Commit

Permalink
only do things in toggle_sparsity if there is a recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Jan 17, 2025
1 parent 7ffc1f9 commit c13d98e
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions R/sparsevctrs.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit c13d98e

Please sign in to comment.