-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TESTS: Add support for marshalling {parsnip} objects [#5]
- Loading branch information
1 parent
5f952cd
commit 544af57
Showing
6 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#' Marshal and Unmarshal a 'parsnip' object | ||
#' | ||
#' @param x | ||
#' A `parnsip:model_fit` object. | ||
#' | ||
#' @param \dots Not used. | ||
#' | ||
#' @return | ||
#' A `marshalled` object as described in [marshal()]. | ||
#' | ||
#' @details | ||
#' The `fit` element of the `model_fit` object is marshalled. | ||
#' | ||
#' @example incl/marshal.parsnip.R | ||
#' | ||
#' @rdname marshal.parsnip | ||
#' @aliases marshal.model_fit | ||
#' @export | ||
marshal.model_fit <- function(x, ...) { | ||
x[["fit"]] <- marshal(x[["fit"]]) | ||
res <- list( | ||
marshalled = x | ||
) | ||
class(res) <- marshal_class(x) | ||
|
||
## IMPORTANT: We don't want any of the input arguments | ||
## to be part of the unmarshal() environment | ||
rm(list = c("x", names(list(...)))) | ||
|
||
res[["unmarshal"]] <- unmarshal_model_fit | ||
assert_no_references(res) | ||
res | ||
} | ||
|
||
unmarshal_model_fit <- function(x, ...) { | ||
object <- x[["marshalled"]] | ||
object[["fit"]] <- unmarshal(object[["fit"]]) | ||
res <- object | ||
stopifnot(all.equal(class(res), marshal_unclass(x), check.attributes = FALSE)) | ||
res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
if (requireNamespace("parsnip", quietly = TRUE) && requireNamespace("xgboost", quietly = TRUE)) { | ||
library(parsnip) | ||
|
||
## Adopted from example("boost_tree", package = "parsnip") | ||
model <- boost_tree(mode = "classification", trees = 20L, engine = "xgboost") | ||
model <- set_mode(model, "regression") | ||
fit <- fit(model, mpg ~ ., data = datasets::mtcars) | ||
|
||
## Marshal | ||
fit_ <- marshal(fit) | ||
|
||
## Unmarshal | ||
fit2 <- unmarshal(fit_) | ||
|
||
## Marshal again | ||
fit2_ <- marshal(fit2) | ||
|
||
## Assert identity | ||
stopifnot( | ||
all.equal(fit2_, fit_) | ||
) | ||
|
||
fit3 <- unmarshal(fit2_) | ||
|
||
## Assert identity | ||
stopifnot( | ||
all.equal(fit3, fit2) | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
library(marshal) | ||
|
||
if (requireNamespace("parsnip", quietly = TRUE) && requireNamespace("xgboost", quietly = TRUE)) { | ||
library(parsnip) | ||
|
||
## Adopted from example("boost_tree", package = "parsnip") | ||
model <- boost_tree(mode = "classification", trees = 20L, engine = "xgboost") | ||
model <- set_mode(model, "regression") | ||
fit <- fit(model, mpg ~ ., data = datasets::mtcars) | ||
|
||
## Marshal | ||
fit_ <- marshal(fit) | ||
|
||
## Unmarshal | ||
fit2 <- unmarshal(fit_) | ||
|
||
## Marshal again | ||
fit2_ <- marshal(fit2) | ||
|
||
## Assert identity | ||
stopifnot( | ||
all.equal(fit2_, fit_) | ||
) | ||
|
||
fit3 <- unmarshal(fit2_) | ||
|
||
## Assert identity | ||
stopifnot( | ||
all.equal(fit3, fit2) | ||
) | ||
} |