From 957d9814401f49f1b497566fe7d4e457d4273b76 Mon Sep 17 00:00:00 2001 From: Santiago Casanova Date: Fri, 17 Nov 2023 14:47:24 -0600 Subject: [PATCH] Test for multi-driver implementation --- R/multi_driver_test.R | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 R/multi_driver_test.R diff --git a/R/multi_driver_test.R b/R/multi_driver_test.R new file mode 100644 index 00000000..0c93448b --- /dev/null +++ b/R/multi_driver_test.R @@ -0,0 +1,89 @@ +library(tidyverse) +library(f1dataR) + +# multi_driver <- function(season, round, drivers, laps = 'fastest'){ +# data <- tibble( +# driver = drivers, +# round = round, +# season = season +# ) +# load_race_session(obj_name = "session", season = season, round = round, session = 'R') +# reticulate::py_run_string("session.load()") +# +# get_driver <- function(season, round, driver, laps = 'fastest'){ +# reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_fastest().get_telemetry().add_distance(){opt}", +# driver = driver, opt = '' +# )) +# py_env <- reticulate::py_run_string(paste("tel.SessionTime = tel.SessionTime.dt.total_seconds()", +# "tel.Time = tel.Time.dt.total_seconds()", +# sep = "\n" +# )) +# +# tel <- reticulate::py_to_r(reticulate::py_get_item(py_env, "tel")) +# +# tel %>% +# dplyr::mutate(driverCode = driver) %>% +# tibble::tibble() %>% +# janitor::clean_names() +# } +# +# mapply(get_driver, data$season, data$round, data$driver)%>% +# t() %>% +# data.frame() %>% +# unnest(cols = c(date, session_time, driver_ahead, +# distance_to_driver_ahead, time, rpm, speed, n_gear, throttle, brake, drs, +# source, relative_distance, status, x, y, z, distance, driver_code)) +# } +# +# personalfix::time_check({ +# multi_driver(2023,1,c('LEC', 'VER')) +# }) +# +# personalfix::time_check({ +# multi_driver(2023,1,c('LEC', 'VER', 'BOT', 'PER', 'SAI', 'SAR', 'ALB')) +# }) + + +multi_driver_parallel <- function(season, round, drivers, laps = 'fastest'){ + data <- tibble( + driver = drivers, + round = round, + season = season + ) + load_race_session(obj_name = "session", season = season, round = round, session = 'R') + reticulate::py_run_string("session.load()") + + get_driver <- function(season, round, driver, laps = 'fastest'){ + reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_fastest().get_telemetry().add_distance(){opt}", + driver = driver, opt = '' + )) + py_env <- reticulate::py_run_string(paste("tel.SessionTime = tel.SessionTime.dt.total_seconds()", + "tel.Time = tel.Time.dt.total_seconds()", + sep = "\n" + )) + + tel <- reticulate::py_to_r(reticulate::py_get_item(py_env, "tel")) + + tel %>% + dplyr::mutate(driverCode = driver) %>% + tibble::tibble() %>% + janitor::clean_names() + } + + parallel::mcmapply(get_driver, data$season, data$round, data$driver, mc.cores=parallel::detectCores()-1)%>% + t() %>% + data.frame() %>% + unnest(cols = c(date, session_time, driver_ahead, + distance_to_driver_ahead, time, rpm, speed, n_gear, throttle, brake, drs, + source, relative_distance, status, x, y, z, distance, driver_code)) +} + +personalfix::time_check({ + multi_driver_parallel(2023,1,c('LEC', 'VER')) +}) + +personalfix::time_check({ + multi_driver_parallel(2023,1,c('LEC', 'VER', 'BOT', 'PER', 'SAI', 'SAR', 'ALB')) +}) + +