User requirement: Only 1 player with -6 arm, no more than 3 with -5 arm
2005 tz_runs_total data analysis:
- 23: Jim Edmonds (1 player)
- 21: Carl Crawford (1 player)
- 19: Coco Crisp, Brady Clark, Andruw Jones (3 players)
- 18: Cliff Floyd
- 17: Jason Michaels, Ichiro Suzuki (2 players)
Updated thresholds:
- > 22: -6 arm (Jim Edmonds only)
- > 19: -5 arm (Carl Crawford only, satisfies 'no more than 3')
- > 16: -4 arm (the three 19s plus 18s and 17s)
- Graduated scale for remaining tiers
Result: Elite arm ratings are now truly exceptional and rare
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
The arm_outfield function had thresholds designed for bis_runs_outfield,
but retrosheet_data.py uses tz_runs_total (different scale).
Issue: 20 players had -6 arm (top rating) - should be exceptionally rare
Analysis of tz_runs_total distribution:
- Ranges from -8 to +23 (not -10 to +10)
- Old threshold: > 8 gave 20 players with -6 arm
- New threshold: > 18 gives ~2-3 players with -6 arm
Updated thresholds to properly map tz_runs_total values to arm ratings:
- > 18: -6 (exceptional, top 2-3 players like Andruw Jones)
- > 14: -5 (elite arms, ~5-8 players)
- > 10: -4 (very good)
- Graduated scale down to +2 for very poor arms
Result: -6 arms now truly exceptional, proper distribution across ratings
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
The post_positions function was being called twice (batters then pitchers).
Each call deleted ALL cardpositions, so the second call would delete the
batter positions that were just created.
Solution: Added delete_existing parameter (default False). Only the first
call (batters) sets delete_existing=True to clean up old data. The second
call (pitchers) just appends positions without deletion.
Result: Both batter and pitcher positions now persist correctly.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
The deletion logic was failing with 'name db_delete is not defined' because
the function wasn't imported from db_calls.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Root cause: post_positions() was upserting cardpositions, leaving stale DH
entries from the previous buggy run where outfielders had no defensive
positions.
Solution: Modified post_positions() to DELETE all existing cardpositions for
the cardset before posting new ones. This ensures:
- Stale DH positions are removed when players gain defensive positions
- Cards show only current, accurate positions
- No phantom positions persist across script runs
Example: Ichiro previously had both "RF" and "DH" cardpositions. With this
fix, only "RF" remains after re-running the script.
Updated CLAUDE.md with explanation of the cleanup logic.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Fixed critical bug where all outfielders were incorrectly assigned as DH
due to defense CSV column mismatch in retrosheet_data.py:
- Lines 889, 926: Changed column check from 'in row' to 'in pos_df.columns'
to correctly detect bis_runs_total availability
- Line 947: Fixed fallback from non-existent 'tz_runs_outfield' to
'tz_runs_total' which actually exists in Baseball Reference CSVs
Impact:
- Before: 57 DH players, 0 outfield positions
- After: 3 DH players, 62 outfielders (23 RF, 20 CF, 19 LF)
Added scripts/check_positions.sh:
- Validates position distribution after card generation
- Flags anomalous DH counts (>5 or >10%)
- Verifies outfield positions exist in cardpositions table
- Provides quick smoke test for defensive calculations
Updated CLAUDE.md:
- Added Position Validation section with check_positions.sh usage
- Documented outfield position bug in Common Issues & Solutions
- Included code examples and verification steps
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
- Add check_cards_and_upload.py: Fetches card images from API and uploads to AWS S3
- Uses persistent aiohttp session for efficient connection reuse
- Supports cache-busting query parameters (?d=date) for Discord compatibility
- S3 URL structure: cards/cardset-{id:03d}/player-{id}/{type}card.png
- Configurable upload and player URL update flags
- Add analyze_cardset_rarity.py: Analyzes players by franchise and rarity
- Groups batters, pitchers, and combined totals
- Displays counts for all rarity tiers by franchise
- Provides comprehensive breakdown of cardset composition
- Add rank_pitching_staffs.py: Ranks teams 1-30 by pitching staff quality
- Point system based on rarity tiers (HoF=5, MVP=4, AS=3, etc.)
- Shows detailed rosters for top 5 and bottom 5 teams
- Useful for balance analysis and cardset evaluation
- Update CLAUDE.md with new scripts documentation
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
Added one-time utility scripts used to prepare 2005 defense CSV files
for compatibility with retrosheet_data.py.
Scripts:
- rename_defense_columns.py: Renamed initial batch of defense columns
- RF/9 → range_factor_per_nine
- RF/G → range_factor_per_game
- DP → DP_def, E → E_def, Ch → chances, Inn → Inn_def
- CS% → caught_stealing_perc, PO → pickoffs
- Name-additional → key_bbref
- rename_additional_defense_columns.py: Second batch of column renames
- Fld% → fielding_perc
- Rtot → tz_runs_total, Rtot/yr → tz_runs_total_per_season
- Rtz → tz_runs_field, Rdp → tz_runs_infield
- undo_po_rename.py: Reverted PO → pickoffs for position players
- Kept 'pickoffs' for defense_p.csv (pitchers)
- Changed back to 'PO' for all other positions (c, 1b, 2b, etc.)
- test_retrosheet_integration.py: Integration test for retrosheet_transformer
- Validates batting and pitching stats loading
- Tests date range filtering
- Verifies player counts
These scripts have already been executed and the defense files are
properly formatted. Kept for historical reference and documentation.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit adds support for the new Retrosheet CSV format and resolves
multiple data processing issues in retrosheet_data.py.
New Features:
- Created retrosheet_transformer.py with smart caching system
- Transforms new Retrosheet CSV format to legacy format
- Checks file timestamps to avoid redundant transformations
- Caches normalized data for instant subsequent loads (~5s → <1s)
- Handles column mapping: gid→game_id, bathand→batter_hand, etc.
- Derives event_type from multiple boolean columns
- Converts handedness values R/L → r/l
- Explicitly sets string dtypes for hit_val, hit_location, batted_ball_type
Configuration Updates:
- Updated retrosheet_data.py for 2005 season data
- START_DATE: 19980301 → 20050403 (2005 Opening Day)
- END_DATE: 19980430 → 20051002 (2005 Regular Season End)
- SEASON_PCT: 28/162 → 162/162 (full season)
- MIN_PA_VL/VR: 20/40 → 50/75 (full season minimums)
- CARDSET_ID: Updated for 2005 cardsets
- EVENTS_FILENAME: Updated to use retrosheets_events_2005.csv
Bug Fixes:
1. Multi-team player duplicates
- Players traded during season had duplicate rows (one per team + combined)
- Added filtering to keep only combined totals (2TM, 3TM, etc.)
- Prevents duplicate key_bbref values in ratings dataframes
2. Column name conflicts
- Fixed Tm column conflict when merging periph_stats and defense_p
- Drop duplicate Tm from defense data before merge
3. Pitcher rating calculations (pitchers/calcs_pitcher.py)
- Fixed "truth value is ambiguous" error in min() comparisons
- Explicitly convert pandas values to float before min() operations
4. Dictionary column corruption in ratings
- Fixed ratings_vL and ratings_vR corruption during DataFrame merges
- Only merge specific columns (key_bbref, player_id, card_id) instead of full DataFrame
- Removed unnecessary .set_index() calls from post_batting_cards() and post_pitching_cards()
Documentation:
- Updated CLAUDE.md with comprehensive troubleshooting section
- Added Retrosheet transformation documentation
- Documented defense CSV requirements and column naming
- Added configuration checklist for retrosheet_data.py
- Documented common issues: multi-team players, dictionary corruption, string types
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit adds default OPS value constants and type hints to key functions,
improving code documentation and IDE support.
## Changes Made
1. **Add default OPS constants** (creation_helpers.py)
- DEFAULT_BATTER_OPS: Default OPS by rarity (1-5)
- DEFAULT_STARTER_OPS: Default OPS-against for starters (99, 1-5)
- DEFAULT_RELIEVER_OPS: Default OPS-against for relievers (99, 1-5)
- Comprehensive comments explaining usage
- Single source of truth for fallback values
2. **Update batters/creation.py**
- Import DEFAULT_BATTER_OPS
- Replace 6 hardcoded if-checks with clean loop over constants
- Add type hints to post_player_updates function
- Import Dict from typing
3. **Update pitchers/creation.py**
- Import DEFAULT_STARTER_OPS and DEFAULT_RELIEVER_OPS
- Replace 12 hardcoded if-checks with clean loops over constants
- Add type hints to post_player_updates function
- Import Dict from typing
4. **Add typing import** (creation_helpers.py)
- Import Dict, List, Tuple, Optional for type hints
- Enables type hints throughout helper functions
## Impact
### Before
```python
# Scattered hardcoded values (batters)
if 1 not in average_ops:
average_ops[1] = 1.066
if 2 not in average_ops:
average_ops[2] = 0.938
# ... 4 more if-checks
# Scattered hardcoded values (pitchers)
if 99 not in sp_average_ops:
sp_average_ops[99] = 0.388
# ... 5 more if-checks for starters
# ... 6 more if-checks for relievers
```
### After
```python
# Clean, data-driven approach (batters)
for rarity, default_ops in DEFAULT_BATTER_OPS.items():
if rarity not in average_ops:
average_ops[rarity] = default_ops
# Clean, data-driven approach (pitchers)
for rarity, default_ops in DEFAULT_STARTER_OPS.items():
if rarity not in sp_average_ops:
sp_average_ops[rarity] = default_ops
for rarity, default_ops in DEFAULT_RELIEVER_OPS.items():
if rarity not in rp_average_ops:
rp_average_ops[rarity] = default_ops
```
### Benefits
✅ Eliminates 18 if-checks across batters and pitchers
✅ Single source of truth for default OPS values
✅ Easy to modify values (change constant, not scattered code)
✅ Self-documenting with clear constant names and comments
✅ Type hints improve IDE support and catch errors early
✅ Function signatures now document expected types
✅ Consistent with other recent refactorings
## Test Results
✅ 42/42 tests pass
✅ All existing functionality preserved
✅ 100% backward compatible
## Files Modified
- creation_helpers.py: +35 lines (3 constants + typing import)
- batters/creation.py: -4 lines net (cleaner code + type hints)
- pitchers/creation.py: -8 lines net (cleaner code + type hints)
**Net change:** More constants, less scattered magic numbers, better types.
Part of ongoing refactoring to reduce code fragility.