-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathtune_args.R
182 lines (149 loc) · 4.57 KB
/
tune_args.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#' @method tune_args model_spec
#' @export
tune_args.model_spec <- function(object, full = FALSE, ...) {
# use the model_spec top level class as the id
model_type <- class(object)[1]
if (length(object$args) == 0L & length(object$eng_args) == 0L) {
return(tune_tbl())
}
# Locate tunable args in spec args and engine specific args
object$args <- purrr::map(object$args, convert_args)
object$eng_args <- purrr::map(object$eng_args, convert_args)
arg_id <- purrr::map_chr(object$args, find_tune_id)
eng_arg_id <- purrr::map_chr(object$eng_args, find_tune_id)
res <- c(arg_id, eng_arg_id)
res <- ifelse(res == "", names(res), res)
len_res <- length(res)
tune_tbl(
name = names(res),
tunable = unname(!is.na(res)),
id = res,
source = rep("model_spec", len_res),
component = rep(model_type, len_res),
component_id = rep(NA_character_, len_res),
full = full
)
}
# helpers for tune_args() methods -----------------------------------------
# they also exist in recipes for the `tune_args()` methods there
# If we map over a list or arguments and some are quosures, we get the message
# that "Subsetting quosures with `[[` is deprecated as of rlang 0.4.0"
convert_args <- function(x) {
if (rlang::is_quosure(x)) {
x <- rlang::quo_get_expr(x)
}
x
}
# useful for standardization and for creating a 0 row tunable tbl
# (i.e. for when there are no steps in a recipe)
tune_tbl <- function(name = character(),
tunable = logical(),
id = character(),
source = character(),
component = character(),
component_id = character(),
full = FALSE) {
complete_id <- id[!is.na(id)]
dups <- duplicated(complete_id)
if (any(dups)) {
stop("There are duplicate `id` values listed in [tune()]: ",
paste0("'", unique(complete_id[dups]), "'", collapse = ", "),
".", sep = "", call. = FALSE)
}
vry_tbl <- tibble::new_tibble(
list(
name = as.character(name),
tunable = as.logical(tunable),
id = as.character(id),
source = as.character(source),
component = as.character(component),
component_id = as.character(component_id)
),
nrow = length(as.character(name))
)
if (!full) {
vry_tbl <- vry_tbl[vry_tbl$tunable,]
}
vry_tbl
}
# Return the `id` arg in tune(); if not specified, then returns "" or if not
# a tunable arg then returns NA_character_
tune_id <- function(x) {
if (is.null(x)) {
return(NA_character_)
} else {
if (rlang::is_quosures(x)) {
# Try to evaluate to catch things in the global envir.
.x <- try(purrr::map(x, rlang::eval_tidy), silent = TRUE)
if (inherits(.x, "try-error")) {
x <- purrr::map(x, rlang::quo_get_expr)
} else {
x <- .x
}
if (is.null(x)) {
return(NA_character_)
}
}
# [tune()] will always return a call object
if (is.call(x)) {
if (rlang::is_call_simple(x) && rlang::call_name(x) == "tune") {
# If an id was specified:
if (length(x) > 1) {
return(x[[2]])
} else {
# no id
return("")
}
return(x$id)
} else {
return(NA_character_)
}
}
}
NA_character_
}
find_tune_id <- function(x) {
# STEP 1 - Early exits
# Early exit for empty elements (like list())
if (length(x) == 0L) {
return(NA_character_)
}
# turn quosures into expressions before continuing
if (rlang::is_quosures(x)) {
# Try to evaluate to catch things in the global envir. If it is a dplyr
# selector, it will fail to evaluate.
.x <- try(purrr::map(x, rlang::eval_tidy), silent = TRUE)
if (inherits(.x, "try-error")) {
x <- purrr::map(x, rlang::quo_get_expr)
} else {
x <- .x
}
}
id <- tune_id(x)
if (!is.na(id)) {
return(id)
}
if (is.atomic(x) | is.name(x) | length(x) == 1) {
return(NA_character_)
}
# STEP 2 - Recursion
# tunable_elems <- purrr::map_lgl(x, find_tune)
tunable_elems <- vector("character", length = length(x))
# use purrr::map_lgl
for (i in seq_along(x)) {
tunable_elems[i] <- find_tune_id(x[[i]])
}
tunable_elems <- tunable_elems[!is.na(tunable_elems)]
if (length(tunable_elems) == 0) {
tunable_elems <- NA_character_
}
if (sum(tunable_elems == "", na.rm = TRUE) > 1) {
stop(
"Only one tunable value is currently allowed per argument. ",
"The current argument has: `",
paste0(deparse(x), collapse = ""),
"`.",
call. = FALSE)
}
return(tunable_elems)
}