@@ -179,3 +179,62 @@ add_methods <- function(x, engine) {
179179 x $ method <- get_model_spec(specific_model(x ), x $ mode , x $ engine )
180180 x
181181}
182+
183+
184+ # ' Translate names of model tuning parameters
185+ # '
186+ # ' This function creates a key that connects the identifiers users make for
187+ # ' tuning parameter names, the standardized parsnip parameter names, and the
188+ # ' argument names to the underlying fit function for the engine.
189+ # '
190+ # ' @param object A workflow or parsnip model specification.
191+ # ' @param as_tibble A logical. Should the results be in a tibble (the default)
192+ # ' or in a list that can facilitate renaming grid objects?
193+ # ' @return A tibble with columns `user`, `parsnip`, and `engine`, or a list
194+ # ' with named character vectors `user_to_parsnip` and `parsnip_to_engine`.
195+ # ' @examples
196+ # ' mod <-
197+ # ' linear_reg(penalty = tune("regularization"), mixture = tune()) %>%
198+ # ' set_engine("glmnet")
199+ # '
200+ # ' mod %>% .model_param_name_key()
201+ # '
202+ # ' rn <- mod %>% .model_param_name_key(as_tibble = FALSE)
203+ # ' rn
204+ # '
205+ # ' grid <- tidyr::crossing(regularization = c(0, 1), mixture = (0:3) / 3)
206+ # '
207+ # ' grid %>%
208+ # ' dplyr::rename(!!!rn$user_to_parsnip)
209+ # '
210+ # ' grid %>%
211+ # ' dplyr::rename(!!!rn$user_to_parsnip) %>%
212+ # ' dplyr::rename(!!!rn$parsnip_to_engine)
213+ # ' @export
214+ .model_param_name_key <- function (object , as_tibble = TRUE ) {
215+ if (! inherits(object , c(" model_spec" , " workflow" ))) {
216+ rlang :: abort(" 'object' should be a model specification or workflow." )
217+ }
218+ if (inherits(object , " workflow" )) {
219+ object <- hardhat :: extract_spec_parsnip(object )
220+ }
221+
222+ # To translate from given names/ids in grid to parsnip names:
223+ params <- object %> % hardhat :: extract_parameter_set_dials()
224+ params <- tibble :: as_tibble(params ) %> %
225+ dplyr :: select(user = id , parsnip = name )
226+ # Go from parsnip names to engine names
227+ arg_key <- get_from_env(paste0(class(object )[1 ], " _args" )) %> %
228+ dplyr :: filter(engine == object $ engine ) %> %
229+ dplyr :: select(engine = original , parsnip )
230+
231+ res <- dplyr :: left_join(params , arg_key , by = " parsnip" )
232+ if (! as_tibble ) {
233+ res0 <- list (user_to_parsnip = res $ user , parsnip_to_engine = res $ parsnip )
234+ names(res0 $ user_to_parsnip ) <- res $ parsnip
235+ names(res0 $ parsnip_to_engine ) <- res $ engine
236+ res <- res0
237+ }
238+ res
239+ }
240+
0 commit comments