Skip to content

Commit

Permalink
feat: keep_results can be character vector of IDs
Browse files Browse the repository at this point in the history
This can be useful when wanting access to an object that is
not an output node of the graph, i.e. we don't have to add
a `PipeOpNOP` (or keep all results) to achieve this.
  • Loading branch information
sebffischer committed Oct 11, 2023
1 parent dae03af commit 961c3b2
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# mlr3pipelines 0.5.0-9000

* Feature: The `Graph`'s `keep_results` can now also be a character vector
containing the IDs of the `PipeOp`s whose results are being stored.

# mlr3pipelines 0.5.0-1

* Bugfix: `PipeOpTuneThreshold` was not overloading the correct `.train` and `.predict` functions.
Expand Down
5 changes: 3 additions & 2 deletions R/Graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@
#' * `phash` :: `character(1)` \cr
#' Stores a checksum calculated on the [`Graph`] configuration, which includes all [`PipeOp`] hashes
#' *except* their `$param_set$values`, and a hash of `$edges`.
#' * `keep_results` :: `logical(1)` \cr
#' * `keep_results` :: `logical(1)` or `character()` \cr
#' Whether to store intermediate results in the [`PipeOp`]'s `$.result` slot, mostly for debugging purposes. Default `FALSE`.
#' Can also be a character vector of IDs, in which case only the results of the selected `PipeOp`s are stored.
#' * `man` :: `character(1)`\cr
#' Identifying string of the help page that shows with `help()`.
#'
Expand Down Expand Up @@ -642,7 +643,7 @@ graph_reduce = function(self, input, fun, single_input) {
lg$debug("Running PipeOp '%s$%s()'", id, fun, pipeop = op, input = input)

output = op[[fun]](input)
if (self$keep_results) {
if (isTRUE(self$keep_results) || op$id %in% self$keep_results) {
op$.result = output
}

Expand Down
2 changes: 1 addition & 1 deletion R/PipeOp.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
#' [`PipeOp`]'s functionality may change depending on more than these values, it should inherit the `$hash` active
#' binding and calculate the hash as `digest(list(super$hash, <OTHER THINGS>), algo = "xxhash64")`.
#' * `.result` :: `list` \cr
#' If the [`Graph`]'s `$keep_results` flag is set to `TRUE`, then the intermediate Results of `$train()` and `$predict()`
#' If the [`Graph`]'s `$keep_results` flag is set to `TRUE` or contains the ID of this `PipeOp`, then the intermediate Results of `$train()` and `$predict()`
#' are saved to this slot, exactly as they are returned by these functions. This is mainly for debugging purposes
#' and done, if requested, by the [`Graph`] backend itself; it should *not* be done explicitly by `private$.train()` or `private$.predict()`.
#' * `man` :: `character(1)`\cr
Expand Down
3 changes: 2 additions & 1 deletion man/Graph.Rd

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

2 changes: 1 addition & 1 deletion man/PipeOp.Rd

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

2 changes: 1 addition & 1 deletion man/mlr_pipeops_nmf.Rd

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

11 changes: 11 additions & 0 deletions tests/testthat/test_Graph.R
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,14 @@ test_that("Same output into multiple channels does not cause a bug", {
expect_true(res$po3.output1 == 2)
expect_true(res$po4.output1 == 2)
})

test_that("keep_results can be a character vector", {
graph = po("pca") %>>% po("ica")

graph$keep_results = "pca"

graph$train(tsk("iris"))

expect_true(is.null(graph$pipeops$ica$.result))
expect_class(graph$pipeops$pca$.result[[1L]], "Task")
})

0 comments on commit 961c3b2

Please sign in to comment.