Skip to content

Commit

Permalink
Merge branch 'master' into devtest
Browse files Browse the repository at this point in the history
  • Loading branch information
SCasanova committed Mar 11, 2024
2 parents cb47803 + cbf0d0c commit 55d1e72
Show file tree
Hide file tree
Showing 74 changed files with 1,668 additions and 456 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/check-standard.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
- {os: windows-latest, r: "release", py: "3.x" }
- {os: ubuntu-latest, r: "devel", py: "3.x", http-user-agent: "release"}
- {os: ubuntu-latest, r: "oldrel-1", py: "3.x" }
- {os: ubuntu-latest, r: "release", py: "3.8" }
- {os: ubuntu-latest, r: "release", py: "3.9" }
- {os: ubuntu-latest, r: "release", py: "3.10"}
- {os: ubuntu-latest, r: "release", py: "3.11"}
- {os: ubuntu-latest, r: "release", py: "3.12"}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down Expand Up @@ -167,7 +167,7 @@ jobs:

runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})
name: HARD ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
Expand Down
63 changes: 63 additions & 0 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,69 @@ jobs:
name: coverage-test-failures
path: ${{ runner.temp }}/package

test-coverage-hard:
# NOTE: This workflow only directly installs "hard" dependencies, i.e. Depends,
# Imports, and LinkingTo dependencies. Notably, Suggests dependencies are never
# installed, with the exception of testthat, knitr, and rmarkdown. The cache is
# never used to avoid accidentally restoring a cache containing a suggested
# dependency.

runs-on: ubuntu-latest

name: Test coverage - HARD

strategy:
fail-fast: false

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
dependencies: '"hard"'
cache: false
extra-packages: |
any::rcmdcheck
any::testthat
any::knitr
any::rmarkdown
any::markdown
any::covr
needs: coverage

- name: Test coverage
run: |
covr::codecov(
quiet = FALSE,
clean = FALSE,
install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
)
shell: Rscript {0}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package

test-pkgdown-ok:
name: Test pkgdown ok
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ f1dataR.Rproj
/doc/
/Meta/
/tst_*
Rplots.pdf
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: f1dataR
Title: Access Formula 1 Data
Version: 1.5.0
Version: 1.5.0.9000
Authors@R: c(
person("Santiago", "Casanova", , "[email protected]", role = c("aut", "cre", "cph")),
person("Philip", "Bulsink", , "[email protected]", role = "aut",
Expand Down Expand Up @@ -39,7 +39,7 @@ Imports:
withr
Suggests:
ggplot2,
httptest,
httptest2,
knitr,
rmarkdown,
testthat (>= 3.0.0),
Expand Down
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# f1dataR (development version)

* Added (very soft) deprecation warning to Ergast functions in advance of the Ergast API being defunct in less than 12 months.
* Deprecated support for FastF1 v < 3.1.0. Older FastF1 versions do not support all of the functions in use and may return different values from some data retrieval or calculation functions. Forcing use of up-to-date FastF1 allows for simpler bugfixes and code updates. (#198)
* Improved messaging to users regarding updating FastF1 (#226)
* Named items in list returned by `load_circuit_details()`
* Changed the way that `correct_track_ratio()` works. Visually the results are the same, but now any labels/annotations added to the ggplot should be kept in the right spot.
* Added Vignette describing some `load_circuit_details()` usage.
* Code improvements (better handle variation in Ergast response, better handle Ergast connection failures) (#228)
* Testing improvements to validate our handling of internet failures (#228)
* Upgraded to use `httptest2` for testing no-internet scenarios (no affect on package performance)
* Code cleanup (removed old inaccessible code, centralized repeated steps to functions, etc.)


# f1dataR 1.5.0

* Added `load_circuit_details` (#210)
Expand Down
27 changes: 12 additions & 15 deletions R/circuit_details.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,16 @@
#'
#' @export
load_circuit_details <- function(season = get_current_season(), round = 1, log_level = "WARNING") {
if (get_fastf1_version()$major < 3 || (get_fastf1_version()$major == 3 && get_fastf1_version()$minor < 1)) {
cli::cli_abort("An old version of FastF1 is in use. FastF1 verison 3.1.0 or later.")
}
# Deprecation Checks
check_ff1_version()

load_race_session(obj_name = "session", season = season, round = round, session = "R", log_level = log_level)
# Function Code
status <- load_race_session(obj_name = "session", season = season, round = round, session = "R", log_level = log_level)

tryCatch(
{
# Only returns a value if session.load() has been successful
# If it hasn't, retry
reticulate::py_run_string("session.t0_date")
},
error = function(e) {
reticulate::py_run_string("session.load()")
}
)
if (is.null(status)) {
# Failure to load - escape
return(NULL)
}

py_env <- reticulate::py_run_string("circuit_info = session.get_circuit_info()")

Expand All @@ -70,5 +64,8 @@ load_circuit_details <- function(season = get_current_season(), round = 1, log_l

rotation <- circuit_info$rotation

return(list(corners, marshal_post, marshal_sectors, rotation))
return(list(
"corners" = corners, "marshal_posts" = marshal_post, "marshal_sectors" = marshal_sectors,
"rotation" = rotation
))
}
19 changes: 6 additions & 13 deletions R/clear_f1_cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,12 @@ clear_f1_cache <- function() {
if (reticulate::py_available(initialize = TRUE)) {
if ("fastf1" %in% reticulate::py_list_packages()$package) {
reticulate::py_run_string("import fastf1")
if (get_fastf1_version()$major >= 3) {
try(
reticulate::py_run_string(glue::glue("fastf1.Cache.clear_cache('{cache_dir}')",
cache_dir = normalizePath(getOption("f1dataR.cache"))
))
)
} else {
try(
reticulate::py_run_string(glue::glue("fastf1.api.Cache.clear_cache('{cache_dir}')",
cache_dir = normalizePath(getOption("f1dataR.cache"))
))
)
}

try(
reticulate::py_run_string(glue::glue("fastf1.Cache.clear_cache('{cache_dir}')",
cache_dir = normalizePath(getOption("f1dataR.cache"))
))
)
}
}

Expand Down
5 changes: 5 additions & 0 deletions R/load_circuits.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ load_circuits <- function(season = get_current_season()) {
url <- glue::glue("{season}/circuits.json?limit=40",
season = season
)

data <- get_ergast_content(url)

if (is.null(data)) {
return(NULL)
}

data$MRData$CircuitTable$Circuits %>%
tidyr::unnest(cols = c("Location")) %>%
dplyr::select(
Expand Down
4 changes: 4 additions & 0 deletions R/load_constructors.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ load_constructors <- function() {
url <- "constructors.json?limit=300"
data <- get_ergast_content(url)

if (is.null(data)) {
return(NULL)
}

return(data$MRData$ConstructorTable$Constructors %>%
dplyr::select("constructorId", "name", "nationality") %>%
janitor::clean_names())
Expand Down
58 changes: 13 additions & 45 deletions R/load_driver_telemetry.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,19 @@
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
# Deprecation Checks
if (lifecycle::is_present(race)) {
lifecycle::deprecate_stop("1.4.0", "load_driver_telemetry(race)", "load_driver_telemetry(round)")
round <- race
}
if (lifecycle::is_present(fastest_only)) {
lifecycle::deprecate_stop("1.4.0", "load_driver_telemetry(fastest_only)", "load_driver_telemetry(laps)")
if (fastest_only) {
laps <- "fastest"
} else {
laps <- "all"
}
}
check_ff1_version()

# Function Code
# Param checks
if (!(laps %in% c("fastest", "all"))) {
if (is.numeric(laps)) {
if (get_fastf1_version()$major < 3) {
cli::cli_abort("{.var laps} can only be a lap number if using fastf1 v3.0 or higher")
}
if (as.numeric(laps) != as.integer(laps)) {
cli::cli_abort("{.var laps} must be one of `fastest`, `all` or an integer value")
}
Expand All @@ -67,36 +60,24 @@ load_driver_telemetry <- function(season = get_current_season(), round = 1, sess
}
}

load_race_session(obj_name = "session", season = season, round = round, session = session, log_level = log_level)

tryCatch(
{
# Only returns a value if session.load() has been successful
# If it hasn't, retry
reticulate::py_run_string("session.t0_date")
},
error = function(e) {
reticulate::py_run_string("session.load()")
}
)
status <- load_race_session(obj_name = "session", season = season, round = round, session = session, log_level = log_level)

if (get_fastf1_version()$major < 3) {
add_v3_option <- ""
} else {
add_v3_option <- ".add_driver_ahead()"
if (is.null(status)) {
# Failure to load - escape
return(NULL)
}

if (laps == "fastest") {
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
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_fastest().get_telemetry().add_distance().add_driver_ahead()",
driver = driver
))
} else if (laps != "all") {
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
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').pick_lap({laps}).get_telemetry().add_distance().add_driver_ahead()",
driver = driver, laps = laps
))
} else {
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').get_telemetry().add_distance(){opt}",
driver = driver, opt = add_v3_option
reticulate::py_run_string(glue::glue("tel = session.laps.pick_driver('{driver}').get_telemetry().add_distance().add_driver_ahead()",
driver = driver
))
}
py_env <- reticulate::py_run_string(paste("tel.SessionTime = tel.SessionTime.dt.total_seconds()",
Expand Down Expand Up @@ -135,17 +116,4 @@ get_driver_telemetry <-
"get_driver_telemetry()",
"load_driver_telemetry()"
)

# default fastest only to TRUE
fastest_only <- ifelse(lifecycle::is_present(fastest_only), fastest_only, TRUE)

load_driver_telemetry(
season = season,
round = round,
session = session,
driver = driver,
laps = ifelse(fastest_only, "fastest", "all"),
log_level = log_level,
race = race
)
}
44 changes: 20 additions & 24 deletions R/load_drivers.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,25 @@ load_drivers <- function(season = get_current_season()) {
)
data <- get_ergast_content(url)

if (season < 2014) {
data$MRData$DriverTable$Drivers %>%
dplyr::select(
"driverId",
"givenName",
"familyName",
"nationality",
"dateOfBirth"
) %>%
tibble::as_tibble() %>%
janitor::clean_names()
} else {
data$MRData$DriverTable$Drivers %>%
dplyr::select(
"driverId",
"givenName",
"familyName",
"nationality",
"dateOfBirth",
"code",
"permanentNumber"
) %>%
tibble::as_tibble() %>%
janitor::clean_names()
if (is.null(data)) {
return(NULL)
}

data <- data$MRData$DriverTable$Drivers

data <- add_col_if_absent(data, "code", NA_character_)
data <- add_col_if_absent(data, "permanentNumber", NA_integer_)

data %>%
dplyr::select(
"driverId",
"givenName",
"familyName",
"nationality",
"dateOfBirth",
"code",
"permanentNumber"
) %>%
tibble::as_tibble() %>%
janitor::clean_names()
}
Loading

0 comments on commit 55d1e72

Please sign in to comment.