Validation
The per-game validation gate: invariant rules over a game's play-by-play and box score, the findings they raise, and the report validate_game returns.
Validation
Finding
Finding(rule_id: 'str', severity: 'str', n_rows: 'int', n_checked: 'int', sample_row_ids: 'list[Any]' = <factory>, message: 'str' = '') -> None
One rule that fired on one game (a frozen dataclass; validate_game builds them).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
rule_id | str | The rule's stable <group>.<name> identifier. | |
severity | str | "error", "warn" or "info" after era / league / source scoping. | |
n_rows | int | Rows (or teams / games) that broke the rule. | |
n_checked | int | Rows the rule applied to -- the denominator behind n_rows. | |
sample_row_ids | list[Any] | <factory> | Up to five offending play ids (or the sample's own keys). |
message | str | '' | The definition the rows break. |
Example
from sportsdataverse.nfl import NFLPlayProcess
proc = NFLPlayProcess(gameId=401671801)
proc.espn_nfl_pbp()
game = proc.run_processing_pipeline()
from sportsdataverse.validation import validate_game
report = validate_game(proc.plays_frame, "nfl")
[f.to_dict() for f in report.errors + report.warnings]
Methods
Finding.to_dict
Finding.to_dict() -> 'dict[str, Any]'
The finding as a plain dict.
Returns
One key per field, sample_row_ids copied to a new list.
GameReport
GameReport(game_id: 'int | None', league: 'str', source: 'str', season: 'int | None', processing_version: 'str', n_rows: 'int', ok: 'bool', errors: 'list[Finding]' = <factory>, warnings: 'list[Finding]' = <factory>, counts_by_rule: 'dict[str, int]' = <factory>, not_applicable: 'list[str]' = <factory>) -> None
The per-game verdict validate_game returns (a frozen dataclass).
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
game_id | int | None | The game's ESPN id, or None when neither the frame nor the header carries one. | |
league | str | "nfl" or "cfb". | |
source | str | The source the game was processed from ("espn" by default). | |
season | int | None | The season the era scoping was resolved against, or None. | |
processing_version | str | The installed sportsdataverse version that produced the frame. | |
n_rows | int | Rows in the validated frame. | |
ok | bool | True when no rule fired at error severity. | |
errors | list[Finding] | <factory> | Findings at error severity. |
warnings | list[Finding] | <factory> | Findings at warn severity (info findings are counted, not listed). |
counts_by_rule | dict[str, int] | <factory> | Violation count per fired rule, whatever its severity. |
not_applicable | list[str] | <factory> | Rules the source cannot support, skipped with a reason in NOT_APPLICABLE -- a declared skip, not a silent one. |
Example
from sportsdataverse.nfl import NFLPlayProcess
proc = NFLPlayProcess(gameId=401671801)
proc.espn_nfl_pbp()
game = proc.run_processing_pipeline()
from sportsdataverse.validation import validate_game
report = validate_game(proc.plays_frame, "nfl", summary=proc.json, box=game.get("advBoxScore"))
report.ok, report.to_dict()["n_errors"]
Methods
GameReport.to_dict
GameReport.to_dict() -> 'dict[str, Any]'
A stable, JSON-serialisable dict (key order is part of the contract).
Returns
The fields, plus n_errors / n_warnings / n_not_applicable counts, with each finding as its to_dict() and counts_by_rule sorted by rule id.
GameReport.to_row
GameReport.to_row() -> 'dict[str, Any]'
One flat row for a <family>_qa_{season} asset (no nested values).
Returns
The scalar fields and counts, with the failed and warned rule ids as sorted lists.
Rule
Rule(id: 'str', severity: 'Severity' = <Severity.ERROR: 'error'>, era_scope: 'Mapping[str, tuple[int, int]] | None' = None, leagues: 'tuple[str, ...]' = ('nfl', 'cfb'), sources: 'tuple[str, ...]' = ('espn', 'shield', 'cbs', 'yahoo', 'fox', 'ncaa'), note: 'str' = '') -> None
Scope metadata for one invariant rule (a frozen dataclass).
The packaged rules live in RULE_SCOPE, keyed by id; validate_game
reads each one's scope to decide whether the rule runs and at what severity.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
id | str | The rule's stable <group>.<name> identifier. | |
severity | Severity | <Severity.ERROR: 'error'> | Severity inside the rule's scope (error / warn / info). |
era_scope | Mapping[str, tuple[int, int]] | None | None | {league: (first_season, last_season)} the rule is meaningful in; outside it the finding is demoted to warn, and a league missing from the map is always warn. None means every league and season. |
leagues | tuple[str, ...] | ('nfl', 'cfb') | Leagues the rule applies to at all; others skip it. |
sources | tuple[str, ...] | ('espn', 'shield', 'cbs', 'yahoo', 'fox', 'ncaa') | Sources the rule applies to at all; others skip it. |
note | str | '' | Why the rule is scoped this way. |
Example
from sportsdataverse.validation import RULE_SCOPE
rule = next(iter(RULE_SCOPE.values()))
rule.applies("nfl", "espn"), rule.severity_for("nfl", 2024)
Methods
Rule.applies
Rule.applies(league: 'str', source: 'str') -> 'bool'
True when the rule runs at all for this league and source.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
league | str | The game's league ("nfl" or "cfb"). | |
source | str | The source the game was processed from ("espn", "ncaa", ...). |
Returns
True when both are in the rule's leagues and sources.
Rule.severity_for
Rule.severity_for(league: 'str', season: 'int | None') -> 'Severity'
The rule's severity for this game: demoted to warn outside era_scope.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
league | str | The game's league. | |
season | int | None | The game's season; None keeps the rule's own severity. |
Returns
severity inside the era scope, warn outside it or for a league the scope does not list; an info rule stays info.
validate_game
validate_game(frame: 'pl.DataFrame', league: 'str', *, header: 'dict | None' = None, source: 'str' = 'espn', summary: 'dict | None' = None, box: 'dict | None' = None) -> 'GameReport'
Validate one processed game against the packaged invariant rules.
Pure and offline: nothing is fetched, nothing is written, the frame is not mutated. A rule whose columns the frame lacks is skipped rather than failed, so a slim frame validates the rules it can support.
For a source whose producer names the same quantities differently,
SOURCE_COLUMNS supplies the ESPN-shaped aliases on a view of the
frame, and NOT_APPLICABLE names the rules that source cannot
support at all -- those are reported in not_applicable rather than
skipped silently.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
frame | DataFrame | one game's processed plays, in processor row order -- the plays_frame attribute of NFLPlayProcess / CFBPlayProcess. | |
league | str | "nfl" or "cfb". | |
header | dict | None | None | the game's ESPN-shaped header dict. Only used to resolve game_id / season when the frame carries neither. |
source | str | 'espn' | the source the game came from ("espn", "shield", "cbs", "yahoo", "fox", "ncaa"). Rules that only judge ESPN's own feed are skipped for an adapted source. |
summary | dict | None | None | the full ESPN-shaped summary, when available. Enables the header-score, final-WP, dropped-play, drive-count and ESPN box rules. |
box | dict | None | None | the processor's advBoxScore dict. Enables the team box and team EPA aggregations. |
Returns
ok is True when no rule fired at error severity.
Raises
| Exception | Description |
|---|---|
ValueError | If league is neither "nfl" nor "cfb". |
Example
from sportsdataverse.nfl import NFLPlayProcess
from sportsdataverse.validation import validate_game
proc = NFLPlayProcess(gameId=401671801)
proc.espn_nfl_pbp()
game = proc.run_processing_pipeline()
report = validate_game(proc.plays_frame, "nfl", summary=proc.json, box=game.get("advBoxScore"))
report.ok, sorted(report.counts_by_rule)