Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pseudocode for accepting a filtered pbp #507

Draft
wants to merge 1 commit into
base: tan/maintenance
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions R/calculate_stats.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
calculate_stats <- function(seasons = nflreadr::most_recent_season(),
summary_level = c("season", "week"),
stat_type = c("player", "team"),
season_type = c("REG", "POST", "REG+POST")){
season_type = c("REG", "POST", "REG+POST"),
pbp = NULL){

summary_level <- rlang::arg_match(summary_level)
stat_type <- rlang::arg_match(stat_type)
season_type <- rlang::arg_match(season_type)
custom_pbp <- !is.null(pbp)

if (custom_pbp) validate_pbp(pbp)
if (!custom_pbp) pbp <- nflreadr::load_pbp(seasons = seasons)

pbp <- nflreadr::load_pbp(seasons = seasons)
if (season_type %in% c("REG", "POST") && summary_level == "season") {
pbp <- dplyr::filter(pbp, .data$season_type == .env$season_type)
if (nrow(pbp) == 0){
Expand Down Expand Up @@ -105,9 +109,10 @@ calculate_stats <- function(seasons = nflreadr::most_recent_season(),
# we need those to identify things like fumbles depending on playtype or
# first downs depending on playtype
playstats <- load_playstats(seasons = seasons) %>%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to pull seasons from custom pbp. So the seasons arg is actually ignored in that case

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, can probably warn/check somehow?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think I'd print a message when custom_pbp is TRUE. But not every time. One of those "once every 8 hours" messages

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah that's fine. we'd probably want to validate the seasons are in pbp and probably validate that playstats is nonzero rows as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually as long as pbp is nonzero rows playstats should be nonzero rows

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I never really thought about validation of the seasons arg because load_pbp checks for valid seasons.

Copy link
Member Author

@tanho63 tanho63 Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd even skip the message about ignoring pbp and just check the custom pbp for the seasons, if any are missing it should warn. then we can call it a day on the season arg since it does what the user expects it to do

# if season_type is REG or POST, we filter pbp.
# That's why we have to filter playstats as well
dplyr::filter(.data$game_id %in% pbp$game_id) %>%
# apply filtering on play stats so that it matches pbp, e.g.
# - game filters for REG/POST
# - only plays included in pbp in case it was provided manually
dplyr::semi_join(pbp, by = c("game_id", "play_id")) %>%
dplyr::rename("player_id" = "gsis_player_id", "team" = "team_abbr") %>%
dplyr::group_by(.data$season, .data$week, .data$play_id, .data$player_id) %>%
dplyr::mutate(
Expand Down Expand Up @@ -448,6 +453,8 @@ calculate_stats <- function(seasons = nflreadr::most_recent_season(),
)
}

if (custom_pbp) attr(stats, "custom_pbp") <- TRUE

stats
}

Expand Down
Loading