/
list_learners.R
58 lines (49 loc) 路 1.72 KB
/
list_learners.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
get_all_learners <- function() {
# search for objects named like sl3 learners
learner_names <- apropos("^Lrnr_")
learners <- mget(learner_names, inherits = TRUE)
# verify that learner inherits from Lrnr_base (and is therefore an actual
# sl3 Learner)
is_learner_real <- sapply(learners, `[[`, "inherit") == "Lrnr_base"
return(learners[which(is_learner_real)])
}
################################################################################
get_learner_class_properties <- function(learner_class) {
return(learner_class$private_fields$.properties)
}
################################################################################
#' @rdname list_learners
#'
#' @export
#
sl3_list_properties <- function() {
learners <- get_all_learners()
properties <- lapply(learners, get_learner_class_properties)
return(sort(unique(unlist(properties))))
}
################################################################################
#' List sl3 Learners
#'
#' Lists learners in \code{sl3} (defined as objects that start with \code{Lrnr_}
#' and inherit from \code{Lrnr_base})
#'
#' @rdname list_learners
#'
#' @param properties a vector of properties that learners must match to be returned
#'
#' @return a vector of learner names that match the property list
#'
#' @importFrom utils apropos
#'
#' @export
#
sl3_list_learners <- function(properties = c()) {
learners <- get_all_learners()
# filter by properties list
learner_has_properties <- function(learner_class, properties) {
return(all(properties %in% get_learner_class_properties(learner_class)))
}
match_properties <- sapply(learners, learner_has_properties, properties)
matched_learners <- names(learners)[which(match_properties)]
return(matched_learners)
}