How jldbc/pybaseball works
Project Type and Purpose
• pybaseball is a Python library/package (not a web app or service) whose purpose is to provide a unified, programmatic API for scraping, caching, transforming, and analyzing baseball data from multiple external sources (Baseball Savant/Statcast, FanGraphs, Baseball Reference, Retrosheet, Chadwick Bureau, and Lahman CSV).
File Structure
• Top-level folders/files:
• pybaseball/ ← main package
• docs/ ← documentation pages
• EXAMPLES/ ← Jupyter notebooks demonstrating usage
• scripts/ ← utility scripts (CLI entry points)
• tests/ ← unit & integration tests
• setup.py, pyproject.toml, Makefile, CI workflows ← packaging & CI
• Within pybaseball/:
• datasources/ ← modules that fetch raw HTML/JSON from external sites
• cache/ ← local-disk caching layer (enable/disable, store/retrieve raw data)
• datahelpers/ ← transformation utilities (column mappers, post-processing, unit conversions, singleton patterns)
• analysis/ ← higher-level algorithms (projections: marcels, trajectories: batted_balls)
• enums/ ← enumeration definitions for data columns & API parameters
• top-level modules ← user-facing API functions (statcast, batting_stats, standings, schedule_and_record, etc.)
• utils.py, profiling.py ← miscellany
README for Additional Insights
• Confirms primary data sources & functions (statcast, statcast_batter, statcast_pitcher, batting_stats, pitching_stats, standings, etc.)
• Mentions optional caching, multiprocessing considerations, and primary dependencies (pandas, requests, beautifulsoup/pandas.read_html)
a. Components
1. External Data Sources
• Baseball Savant (Statcast API/CSV)
• FanGraphs (HTML tables / endpoints)
• Baseball Reference (season-level & range queries)
• Retrosheet / Chadwick / Lahman CSVs (local/static data)
2. Data Acquisition Layer (pybaseball/datasources)
• Modules: statcast.py, fangraphs.py, bref.py, html_table_processor.py
• Responsibilities: perform HTTP requests, parse HTML/CSV, return raw DataFrames
3. Caching Layer (pybaseball/cache)
• Modules: cache.py, cache_config.py, cache_record.py
• Responsibilities: intercept raw downloads, store to disk, manage TTL, purge/enable/disable
4. Data Processing Layer (pybaseball/datahelpers + utils)
• Modules: column_mapper.py, postprocessing.py, transform.py, unit_conversions.py, statcast_utils.py
• Responsibilities: clean & normalize raw DataFrames, map column names/enums, convert units, fill missing data
5. Core API Layer (pybaseball top-level modules)
• Functions: statcast(), batting_stats(), pitching_stats(), schedule_and_record(), standings(), playerid_lookup(), etc.
• Responsibilities: orchestrate acquisition → caching → processing → return final pandas.DataFrame or list of DataFrames
6. Analysis Layer (pybaseball/analysis)
• Subpackages: projections/marcels (age adjustment, batting & pitching projection), trajectories/batted_balls (calculator, parameters)
• Responsibilities: apply statistical algorithms to processed DataFrames, output enhanced metrics or model projections
7. Examples & Scripts
• EXAMPLES notebooks for interactive exploration
• scripts/*.py for ad-hoc utilities (e.g., validate_cache.py, statcast_timing.py)
8. Tests & CI
• tests/pybaseball & tests/integration ensure correctness across modules
• GitHub workflows trigger pytest and publish to PyPI
b. Relationships & Data Flow
1. User calls a Core API function (e.g., statcast(start, end))
2. Core API invokes Data Acquisition module →
3. Acquisition layer checks Caching layer:
– If cache miss: fetch from External Data Sources → pass raw DataFrame to Caching layer → store on disk → forward to Data Processing
– If cache hit: load raw DataFrame from disk → forward to Data Processing
4. Data Processing layer cleans and normalizes → returns final DataFrame
5. Analysis layer (if called explicitly) takes DataFrame → runs projection/trajectory algorithms → returns enhanced results
6. Output delivered back to the user’s code or notebook
c. Architectural Patterns & Principles
• Layered Architecture (Separation of Concerns)
• Decorator/Singleton in caching module for transparent caching
• Modular design: datasources can be extended with new providers; analysis algorithms can be swapped/extended
• Use of pandas DataFrames as canonical data structure across layers
d. Key Technologies & Libraries
• Python 3.x
• pandas, numpy
• requests (or pandas.read_html under the hood)
• BeautifulSoup or lxml (via pandas)
• pytest for testing
• mypy for static typing
• GitHub Actions for CI/CD