paper-dynasty-card-creation/.claude/ops-rework/test_rarity_fix.py
2025-11-08 16:57:35 -06:00

124 lines
4.8 KiB
Python

"""
Test script to verify the rarity assignment fix for players without ratings.
"""
import pandas as pd
import numpy as np
def test_batter_merge_fix():
"""Simulate the batter merge with LEFT JOIN."""
print("Testing Batter Merge Fix")
print("=" * 80)
# Simulate batting cards (including new players)
batting_cards = pd.DataFrame({
'battingcard_id': [1, 2, 3, 4, 5],
'player': [101, 102, 103, 104, 105],
'player_name': ['Aaron Judge', 'Jason Heyward', 'Tucker Barnhart', 'New Player 1', 'New Player 2']
})
# Simulate ratings (only some players have ratings due to ts filter)
batting_ratings = pd.DataFrame({
'battingcard_id': [1], # Only Aaron Judge has ratings
'obp_vL': [0.450],
'slg_vL': [0.700],
'obp_vR': [0.420],
'slg_vR': [0.650],
'total_OPS': [1.379],
'new_rarity_id': [99]
})
# OLD WAY: Inner join (loses players without ratings)
old_merge = pd.merge(batting_cards, batting_ratings, on='battingcard_id')
print(f"\nOLD WAY (inner join):")
print(f" Players with cards: {len(batting_cards)}")
print(f" Players with ratings: {len(batting_ratings)}")
print(f" Players after merge: {len(old_merge)}")
print(f" Lost {len(batting_cards) - len(old_merge)} players!")
# NEW WAY: Left join (keeps all players)
new_merge = pd.merge(batting_cards, batting_ratings, on='battingcard_id', how='left')
new_merge['new_rarity_id'] = new_merge['new_rarity_id'].fillna(5) # Default to Common
print(f"\nNEW WAY (left join + default rarity):")
print(f" Players with cards: {len(batting_cards)}")
print(f" Players with ratings: {len(batting_ratings)}")
print(f" Players after merge: {len(new_merge)}")
print(f" Lost 0 players ✓")
print(f"\nRarity assignments:")
print(new_merge[['player_name', 'new_rarity_id']].to_string(index=False))
# Verify
assert len(new_merge) == len(batting_cards), "Should keep all players"
assert new_merge['new_rarity_id'].iloc[0] == 99, "Aaron Judge should be HOF"
assert new_merge['new_rarity_id'].iloc[1] == 5, "New players should be Common"
assert new_merge['new_rarity_id'].iloc[2] == 5, "New players should be Common"
print("\n✓ Batter merge fix works correctly!")
def test_pitcher_merge_fix():
"""Simulate the pitcher merge with LEFT JOIN."""
print("\n\n")
print("Testing Pitcher Merge Fix")
print("=" * 80)
# Simulate pitching cards (including new players)
pitching_cards = pd.DataFrame({
'pitchingcard_id': [1, 2, 3, 4],
'player': [201, 202, 203, 204],
'player_name': ['Zack Wheeler', 'Joe Boyle', 'New Pitcher 1', 'New Pitcher 2'],
'starter_rating': [6, 4, 5, 1]
})
# Simulate ratings (only some pitchers have ratings)
pitching_ratings = pd.DataFrame({
'pitchingcard_id': [1, 2],
'obp_vL': [0.290, 0.137],
'slg_vL': [0.310, 0.221],
'obp_vR': [0.285, 0.140],
'slg_vR': [0.305, 0.229],
'total_OPS': [0.297, 0.365],
'new_rarity_id': [99, 2] # Using old 2024 thresholds: Wheeler=HOF, Boyle=Gold
})
# OLD WAY: Inner join (loses players without ratings)
old_merge = pd.merge(pitching_cards, pitching_ratings, on='pitchingcard_id')
print(f"\nOLD WAY (inner join):")
print(f" Pitchers with cards: {len(pitching_cards)}")
print(f" Pitchers with ratings: {len(pitching_ratings)}")
print(f" Pitchers after merge: {len(old_merge)}")
print(f" Lost {len(pitching_cards) - len(old_merge)} pitchers!")
# NEW WAY: Left join (keeps all pitchers)
new_merge = pd.merge(pitching_cards, pitching_ratings, on='pitchingcard_id', how='left')
new_merge['new_rarity_id'] = new_merge['new_rarity_id'].fillna(5) # Default to Common
print(f"\nNEW WAY (left join + default rarity):")
print(f" Pitchers with cards: {len(pitching_cards)}")
print(f" Pitchers with ratings: {len(pitching_ratings)}")
print(f" Pitchers after merge: {len(new_merge)}")
print(f" Lost 0 pitchers ✓")
print(f"\nRarity assignments:")
print(new_merge[['player_name', 'new_rarity_id']].to_string(index=False))
# Verify
assert len(new_merge) == len(pitching_cards), "Should keep all pitchers"
assert new_merge['new_rarity_id'].iloc[0] == 99, "Wheeler should be HOF"
assert new_merge['new_rarity_id'].iloc[1] == 2, "Boyle should be Gold"
assert new_merge['new_rarity_id'].iloc[2] == 5, "New pitchers should be Common"
assert new_merge['new_rarity_id'].iloc[3] == 5, "New pitchers should be Common"
print("\n✓ Pitcher merge fix works correctly!")
if __name__ == '__main__':
test_batter_merge_fix()
test_pitcher_merge_fix()
print("\n\n" + "=" * 80)
print("✅ All rarity assignment fixes verified!")
print("=" * 80)