Skip to content

Commit

Permalink
telemetry with lap selection only
Browse files Browse the repository at this point in the history
Partial SCasanova#78
  • Loading branch information
pbulsink committed Jun 26, 2023
1 parent 12429f4 commit dbdd428
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 36 deletions.
28 changes: 8 additions & 20 deletions R/load_driver_telemetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@
#' @param round number from 1 to 23 (depending on season selected). Also accepts race name.
#' @param session the code for the session to load Options are `'FP1'`, `'FP2'`, `'FP3'`,
#' `'Q'`, `'S'`, `'SS'`, and `'R'`. Default is `'R'`, which refers to Race.
#' @param drivers three letter driver code (see `load_drivers()` for a list)
#' @param driver three letter driver code (see `load_drivers()` for a list)
#' @param laps which lap's telemetry to return. One of an integer lap number (<= total laps in the race), `fastest`,
#' or `all`.
#' @param log_level Detail of logging from fastf1 to be displayed. Choice of:
#' `'DEBUG'`, `'INFO'`, `'WARNING'`, `'ERROR'` and `'CRITICAL'`. See \href{https://theoehrly.github.io/Fast-F1/fastf1.html#configure-logging-verbosity}{fastf1 documentation}.
#' @param race `r lifecycle::badge("deprecated")` `race` is no longer supported, use `round`.
#' @param fastest_only `r lifecycle::badge("deprecated")` `fastest_only` is no longer supported, indicated preferred
#' laps in `laps`.
#' @param driver `r lifecycle::badge("deprecated")` `driver` is no longer supported, indicated preferred
#' driver in `drivers`.
#' @importFrom magrittr "%>%"
#' @return A tibble with telemetry data for selected driver/session.
#' @import reticulate
#' @export
#' @examples
#' \dontrun{
#' telem <- load_driver_telemetry(season = 2023,
#' round = 'bahrain',
#' round = 'Bahrain',
#' session = 'Q',
#' drivers = 'HAM',
#' driver = 'HAM',
#' laps = 'fastest')
#' }
#'
load_driver_telemetry <- function(season = get_current_season(), round = 1, session = 'R', drivers, laps = 'fastest', log_level = "WARNING", race = lifecycle::deprecated(), fastest_only = lifecycle::deprecated(), driver = lifecycle::deprecated()){
load_driver_telemetry <- function(season = get_current_season(), round = 1, session = 'R', driver, laps = 'fastest', log_level = "WARNING", race = lifecycle::deprecated(), fastest_only = lifecycle::deprecated()){

#Lifecycles
if (lifecycle::is_present(race)) {
Expand All @@ -43,10 +41,6 @@ load_driver_telemetry <- function(season = get_current_season(), round = 1, sess
lap = 'fastest'
}
}
if (lifecycle::is_present(driver)) {
lifecycle::deprecate_warn("1.1.0", "load_driver_telemetry(driver)", "load_driver_telemetry(drivers)")
drivers <- driver
}

# Param checks
if(!(laps %in% c('fastest', 'all'))){
Expand All @@ -68,20 +62,14 @@ load_driver_telemetry <- function(season = get_current_season(), round = 1, sess
add_v3_option <- ''
}

if(length(drivers) > 1){
driver <- glue::glue("pick_drivers(['{drivers}'])", drivers = paste(drivers, collapse = "', '"))
} else {
driver <- glue::glue("pick_driver('{drivers}')", drivers = drivers)
}

if(laps == 'fastest'){
reticulate::py_run_string(glue::glue("tel = session.laps.{driver}.pick_fastest().get_telemetry().add_distance(){opt}",
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_fastest().get_telemetry().add_distance(){opt}",
driver = driver, opt = add_v3_option))
} else if (laps != 'all'){
reticulate::py_run_string(glue::glue("tel = session.laps.{driver}.pick_lap({laps}).get_telemetry().add_distance(){opt}",
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_lap({laps}).get_telemetry().add_distance(){opt}",
driver = driver, laps = laps, opt = add_v3_option))
} else {
reticulate::py_run_string(glue::glue("tel = session.laps.{driver}.get_telemetry().add_distance(){opt}",
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').get_telemetry().add_distance(){opt}",
driver = driver, opt = add_v3_option))

}
Expand All @@ -92,7 +80,7 @@ load_driver_telemetry <- function(season = get_current_season(), round = 1, sess
tel <- reticulate::py_to_r(reticulate::py_get_item(py_env, 'tel'))

tel %>%
#dplyr::mutate(driverCode = driver) %>%
dplyr::mutate(driverCode = driver) %>%
tibble::tibble() %>%
janitor::clean_names()
}
Expand Down
2 changes: 1 addition & 1 deletion R/plot_fastest.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ plot_fastest <- function(season = get_current_season(), round = 1, session = 'R'
round <- race
}

driver_data <- load_driver_telemetry(season, round, session, driver, T)
driver_data <- load_driver_telemetry(season, round, session, driver, 'fastest')

selected_driver_id <- load_drivers(season) %>%
dplyr::filter(.data$code == driver) %>%
Expand Down
3 changes: 1 addition & 2 deletions man/get_driver_telemetry.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions man/load_driver_telemetry.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions tests/testthat/test-load_driver_telemetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ test_that("driver telemetry", {
withr::local_options(f1dataR.cache = file.path(getwd(), "tst_telem"))

# Tests
telem <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", drivers = "HAM", laps = 'all')
telem_fast <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", drivers = "HAM", laps = 'fastest')
telem_multidrive <- load_driver_telemetry(season = 2022, round = 'Brazil', session = "S", drivers = c("HAM", "VER"), laps = 'fastest')
telem_lap <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", drivers = "HAM", laps = 1)
telem <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = 'all')
telem_fast <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = 'fastest')
telem_lap <- load_driver_telemetry(season = 2022, round = "Brazil", session = "S", driver = "HAM", laps = 1)

expect_true(nrow(telem) > nrow(telem_fast))
expect_true(ncol(telem) == ncol(telem_fast))
Expand All @@ -34,6 +33,8 @@ test_that("driver telemetry", {
expect_equal(telem_fast$session_time[[1]], 3518.595)
expect_equal(telem_fast$time[[2]], 0.044)
}
expect_equal(telem_lap$time[[1]], 0)
expect_equal(telem_lap$speed[[1]], 0)


})

0 comments on commit dbdd428

Please sign in to comment.