Skip to content

format_<prediction type>() functions only format when necessary #922

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

Merged
merged 2 commits into from
Mar 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 23 additions & 44 deletions R/predict.R
Original file line number Diff line number Diff line change
Expand Up @@ -222,28 +222,19 @@ check_pred_type <- function(object, type, ...) {
#' @export

format_num <- function(x) {
if (inherits(x, "tbl_spark"))
if (inherits(x, "tbl_spark")) {
return(x)

if (isTRUE(ncol(x) > 1) | is.data.frame(x)) {
x <- as_tibble(x, .name_repair = "minimal")
if (!any(grepl("^\\.pred", names(x)))) {
names(x) <- paste0(".pred_", names(x))
}
} else {
x <- tibble(.pred = unname(x))
}

x
ensure_parsnip_format(x, ".pred", overwrite = FALSE)
}

#' @rdname format-internals
#' @export
format_class <- function(x) {
if (inherits(x, "tbl_spark"))
if (inherits(x, "tbl_spark")) {
return(x)

tibble(.pred_class = unname(x))
}
ensure_parsnip_format(x, ".pred_class")
}

#' @rdname format-internals
Expand All @@ -260,57 +251,45 @@ format_classprobs <- function(x) {
#' @rdname format-internals
#' @export
format_time <- function(x) {
if (isTRUE(ncol(x) > 1) | is.data.frame(x)) {
x <- as_tibble(x, .name_repair = "minimal")
if (!any(grepl("^\\.pred_time", names(x)))) {
names(x) <- paste0(".pred_time_", names(x))
}
} else {
x <- tibble(.pred_time = unname(x))
}

x
ensure_parsnip_format(x, ".pred_time", overwrite = FALSE)
}

#' @rdname format-internals
#' @export
format_survival <- function(x) {
if (isTRUE(ncol(x) > 1) | is.data.frame(x)) {
x <- as_tibble(x, .name_repair = "minimal")
names(x) <- ".pred"
} else {
x <- tibble(.pred = unname(x))
}

x
ensure_parsnip_format(x, ".pred")
}

#' @rdname format-internals
#' @export
format_linear_pred <- function(x) {
if (inherits(x, "tbl_spark"))
if (inherits(x, "tbl_spark")){
return(x)

if (isTRUE(ncol(x) > 1) | is.data.frame(x)) {
x <- as_tibble(x, .name_repair = "minimal")
names(x) <- ".pred_linear_pred"
} else {
x <- tibble(.pred_linear_pred = unname(x))
}

x
ensure_parsnip_format(x, ".pred_linear_pred")
}

#' @rdname format-internals
#' @export
format_hazard <- function(x) {
ensure_parsnip_format(x, ".pred")
}

ensure_parsnip_format <- function(x, col_name, overwrite = TRUE) {
if (isTRUE(ncol(x) > 1) | is.data.frame(x)) {
x <- as_tibble(x, .name_repair = "minimal")
names(x) <- ".pred"
if (!any(grepl(paste0("^\\", col_name), names(x)))) {
if (overwrite) {
names(x) <- col_name
} else {
names(x) <- paste(col_name, names(x), sep = "_")
}
}
} else {
x <- tibble(.pred_hazard = unname(x))
x <- tibble(unname(x))
names(x) <- col_name
x
}

x
}

Expand Down