Skip to main content
Version: 0.2.0

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

ParameterTypeDefaultDescription
rule_idstrThe rule's stable <group>.<name> identifier.
severitystr"error", "warn" or "info" after era / league / source scoping.
n_rowsintRows (or teams / games) that broke the rule.
n_checkedintRows the rule applied to -- the denominator behind n_rows.
sample_row_idslist[Any]<factory>Up to five offending play ids (or the sample's own keys).
messagestr''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

ParameterTypeDefaultDescription
game_idint | NoneThe game's ESPN id, or None when neither the frame nor the header carries one.
leaguestr"nfl" or "cfb".
sourcestrThe source the game was processed from ("espn" by default).
seasonint | NoneThe season the era scoping was resolved against, or None.
processing_versionstrThe installed sportsdataverse version that produced the frame.
n_rowsintRows in the validated frame.
okboolTrue when no rule fired at error severity.
errorslist[Finding]<factory>Findings at error severity.
warningslist[Finding]<factory>Findings at warn severity (info findings are counted, not listed).
counts_by_ruledict[str, int]<factory>Violation count per fired rule, whatever its severity.
not_applicablelist[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

ParameterTypeDefaultDescription
idstrThe rule's stable <group>.<name> identifier.
severitySeverity<Severity.ERROR: 'error'>Severity inside the rule's scope (error / warn / info).
era_scopeMapping[str, tuple[int, int]] | NoneNone{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.
leaguestuple[str, ...]('nfl', 'cfb')Leagues the rule applies to at all; others skip it.
sourcestuple[str, ...]('espn', 'shield', 'cbs', 'yahoo', 'fox', 'ncaa')Sources the rule applies to at all; others skip it.
notestr''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

ParameterTypeDefaultDescription
leaguestrThe game's league ("nfl" or "cfb").
sourcestrThe 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

ParameterTypeDefaultDescription
leaguestrThe game's league.
seasonint | NoneThe 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

ParameterTypeDefaultDescription
frameDataFrameone game's processed plays, in processor row order -- the plays_frame attribute of NFLPlayProcess / CFBPlayProcess.
leaguestr"nfl" or "cfb".
headerdict | NoneNonethe game's ESPN-shaped header dict. Only used to resolve game_id / season when the frame carries neither.
sourcestr'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.
summarydict | NoneNonethe full ESPN-shaped summary, when available. Enables the header-score, final-WP, dropped-play, drive-count and ESPN box rules.
boxdict | NoneNonethe processor's advBoxScore dict. Enables the team box and team EPA aggregations.

Returns

ok is True when no rule fired at error severity.

Raises

ExceptionDescription
ValueErrorIf 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)