-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
280 additions
and
133 deletions.
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 was deleted.
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,93 @@ | ||
#' @title Create Workhorse Callback | ||
#' | ||
#' @description | ||
#' Callbacks allow to customize the behavior of processes in mlr3. | ||
#' | ||
#' @export | ||
CallbackWorkhorse= R6Class("CallbackWorkhorse", | ||
inherit = Callback, | ||
public = list( | ||
|
||
on_workhorse_before_train = NULL, | ||
|
||
on_workhorse_before_predict = NULL, | ||
|
||
on_workhorse_before_result = NULL | ||
) | ||
) | ||
|
||
#' @title Create Workhorse Callback | ||
#' | ||
#' @description | ||
#' Function to create a [CallbackWorkhorse]. | ||
#' | ||
#' ``` | ||
#' Start Workhorse | ||
#' - on_workhorse_before_train | ||
#' - on_workhorse_before_predict | ||
#' - on_workhorse_before_result | ||
#' End Tuning | ||
#' ``` | ||
# | ||
#' @details | ||
#' When implementing a callback, each function must have two arguments named `callback` and `context`. | ||
#' A callback can write data to the state (`$state`), e.g. settings that affect the callback itself. | ||
#' Workhorse callbacks access [ContextWorkhorse]. | ||
#' | ||
#' @param id (`character(1)`)\cr | ||
#' Identifier for the new instance. | ||
#' @param label (`character(1)`)\cr | ||
#' Label for the new instance. | ||
#' @param man (`character(1)`)\cr | ||
#' String in the format `[pkg]::[topic]` pointing to a manual page for this object. | ||
#' The referenced help package can be opened via method `$help()`. | ||
#' | ||
#' @export | ||
#' @inherit CallbackWorkhorse examples | ||
callback_workhorse = function( | ||
id, | ||
label = NA_character_, | ||
man = NA_character_, | ||
on_workhorse_before_train = NULL, | ||
on_workhorse_before_predict = NULL, | ||
on_workhorse_before_result = NULL | ||
) { | ||
stages = discard(set_names(list( | ||
on_workhorse_before_train, | ||
on_workhorse_before_predict, | ||
on_workhorse_before_result), | ||
c( | ||
"on_workhorse_before_train", | ||
"on_workhorse_before_predict", | ||
"on_workhorse_before_result" | ||
)), is.null) | ||
|
||
walk(stages, function(stage) assert_function(stage, args = c("callback", "context"))) | ||
callback = CallbackWorkhorse$new(id, label, man) | ||
iwalk(stages, function(stage, name) callback[[name]] = stage) | ||
callback | ||
} | ||
|
||
#' @title Assertions for Callbacks | ||
#' | ||
#' @description | ||
#' Assertions for [CallbackWorkhorse] class. | ||
#' | ||
#' @param callback ([CallbackWorkhorse]). | ||
#' @param null_ok (`logical(1)`)\cr | ||
#' If `TRUE`, `NULL` is allowed. | ||
#' | ||
#' @return [CallbackWorkhorse | List of [CallbackWorkhorse]s. | ||
#' @export | ||
assert_workhorse_callback = function(callback, null_ok = FALSE) { | ||
if (null_ok && is.null(callback)) return(invisible(NULL)) | ||
assert_class(callback, "CallbackWorkhorse") | ||
invisible(callback) | ||
} | ||
|
||
#' @export | ||
#' @param callbacks (list of [CallbackWorkhorse]). | ||
#' @rdname assert_workhorse_callback | ||
assert_workhorse_callbacks = function(callbacks) { | ||
invisible(lapply(callbacks, assert_workhorse_callback)) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.