Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.3
Collate:
'bound_prediction.R'
'butcher.R'
'cal-apply-binary.R'
'cal-apply-impl.R'
'cal-apply-multi.R'
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Add `required_pkgs()` methods to `int_conformal_cv()`, `int_conformal_full()`, `int_conformal_quantile()`, and `int_conformal_split()`. (#190)

* Add butcher methods to `int_conformal_cv()`, `int_conformal_full()`, `int_conformal_quantile()`, and `int_conformal_split()`. (#194)

# probably 1.1.1

* Updated unit tests for new ggplot2 release (#180).
Expand Down
173 changes: 173 additions & 0 deletions R/butcher.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
#' Butcher methods for conformal inteference intervals
#'
#' These methods allow you to use the butcher package to reduce the size of a
#' conformal inference interval object. After calling `butcher::butcher()` on a
#' conformal inference interval object, the only guarantee is that you will
#' still be able to `predict()` from that conformal inference interval object.
#' Other functions may not work as expected.
#'
#' @param x A conformal inference interval object.
#' @param verbose Should information be printed about how much memory is freed
#' from butchering?
#' @param ... Extra arguments possibly used by underlying methods.
#'
#' @keywords internal
#' @name inf_conformal-butcher

# int_conformal_full

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_call.int_conformal_full <- function(x, verbose = FALSE, ...) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that there is a good amount of repetition. However the functions are also really short, so I think that this is the more readable form

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind but maybe they should all share a int_conformal class?

x$wflow <- butcher::axe_call(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for not directly exporting the method? The help page for vctrs::s3_register() says it's for registering an S3 method for a generic from a package you dont' import, just suggest. probably is importing butcher so I'm wondering if we need this extra layer here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm doing it the same way that it was done in {workflows}

https://github.com/tidymodels/workflows/blob/db117b59d8c545a55fd57eeb339ad232616e38a8/R/zzz.R#L6-L10

https://github.com/tidymodels/workflows/blob/db117b59d8c545a55fd57eeb339ad232616e38a8/R/butcher.R#L15

It didn't work when I did it the normal way. So it might be a little out of my depth to understand why.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly sure that the reason for this in workflows is because butcher is only in Suggests and not in Imports.

Maybe that affects things here because we make use of the workflows methods, which don't seem to be exported from workflows? 🤔 I'm okay moving on and getting this merged though!

#' @rdname inf_conformal-butcher
axe_ctrl.int_conformal_full <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_ctrl(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_data.int_conformal_full <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_data(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_env.int_conformal_full <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_env(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_fitted.int_conformal_full <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_fitted(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# int_conformal_split

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_call.int_conformal_split <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_call(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_ctrl.int_conformal_split <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_ctrl(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_data.int_conformal_split <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_data(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_env.int_conformal_split <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_env(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_fitted.int_conformal_split <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_fitted(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# int_conformal_quantile

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_call.int_conformal_quantile <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_call(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_ctrl.int_conformal_quantile <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_ctrl(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_data.int_conformal_quantile <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_data(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_env.int_conformal_quantile <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_env(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_fitted.int_conformal_quantile <- function(x, verbose = FALSE, ...) {
x$wflow <- butcher::axe_fitted(x$wflow, verbose = verbose, ...)
add_butcher_class(x)
}

# int_conformal_cv

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_call.int_conformal_cv <- function(x, verbose = FALSE, ...) {
x$models <- purrr::map(x$models, butcher::axe_call, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_ctrl.int_conformal_cv <- function(x, verbose = FALSE, ...) {
x$models <- purrr::map(x$models, butcher::axe_ctrl, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_data.int_conformal_cv <- function(x, verbose = FALSE, ...) {
x$models <- purrr::map(x$models, butcher::axe_data, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_env.int_conformal_cv <- function(x, verbose = FALSE, ...) {
x$models <- purrr::map(x$models, butcher::axe_env, verbose = verbose, ...)
add_butcher_class(x)
}

# @export - onLoad
#' @rdname inf_conformal-butcher
axe_fitted.int_conformal_cv <- function(x, verbose = FALSE, ...) {
x$models <- purrr::map(x$models, butcher::axe_fitted, verbose = verbose, ...)
add_butcher_class(x)
}

# ------------------------------------------------------------------------------

# butcher:::add_butcher_class
add_butcher_class <- function(x) {
if (!any(grepl("butcher", class(x)))) {
class(x) <- append(paste0("butchered_", rev(class(x))[1]), class(x))
}
x
}
24 changes: 24 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
.onLoad <- function(libname, pkgname) {
vctrs::s3_register("tune::collect_metrics", "cal_rset")
vctrs::s3_register("tune::collect_predictions", "cal_rset")

vctrs::s3_register("butcher::axe_call", "int_conformal_cv")
vctrs::s3_register("butcher::axe_ctrl", "int_conformal_cv")
vctrs::s3_register("butcher::axe_data", "int_conformal_cv")
vctrs::s3_register("butcher::axe_env", "int_conformal_cv")
vctrs::s3_register("butcher::axe_fitted", "int_conformal_cv")

vctrs::s3_register("butcher::axe_call", "int_conformal_full")
vctrs::s3_register("butcher::axe_ctrl", "int_conformal_full")
vctrs::s3_register("butcher::axe_data", "int_conformal_full")
vctrs::s3_register("butcher::axe_env", "int_conformal_full")
vctrs::s3_register("butcher::axe_fitted", "int_conformal_full")

vctrs::s3_register("butcher::axe_call", "int_conformal_split")
vctrs::s3_register("butcher::axe_ctrl", "int_conformal_split")
vctrs::s3_register("butcher::axe_data", "int_conformal_split")
vctrs::s3_register("butcher::axe_env", "int_conformal_split")
vctrs::s3_register("butcher::axe_fitted", "int_conformal_split")

vctrs::s3_register("butcher::axe_call", "int_conformal_quantile")
vctrs::s3_register("butcher::axe_ctrl", "int_conformal_quantile")
vctrs::s3_register("butcher::axe_data", "int_conformal_quantile")
vctrs::s3_register("butcher::axe_env", "int_conformal_quantile")
vctrs::s3_register("butcher::axe_fitted", "int_conformal_quantile")
}
82 changes: 82 additions & 0 deletions man/inf_conformal-butcher.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading