nick-leland commited on
Commit
5307707
·
1 Parent(s): 5c5f11f
Files changed (1) hide show
  1. app.py +40 -54
app.py CHANGED
@@ -38,84 +38,70 @@ def load_model():
38
  def process_player_data(player_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
39
  """Process player data similar to training pipeline"""
40
  try:
 
 
 
 
 
 
 
 
 
 
41
  # Clean player ID from URL if needed
42
  if "/" in player_id:
43
  player_id = player_id.split("/")[-1]
44
 
45
  # Create initial player series
46
  player_data = {
47
- "player_id": player_id,
48
  "mmr": float(mmr),
49
  "p1": int(comf_1),
50
  "p2": int(comf_2),
51
  "p3": int(comf_3),
52
  "p4": int(comf_4),
53
- "p5": int(comf_5)
 
 
 
 
54
  }
55
 
56
- # Read the example row from prediction_data_prepped.csv to get the expected structure
57
- try:
58
- pred_data = pd.read_csv("prediction_data_prepped.csv")
59
- print("\nReference columns from prediction_data_prepped.csv:")
60
- print(sorted(pred_data.columns.tolist()))
61
- print(f"Number of reference columns: {len(pred_data.columns)}")
62
-
63
- if not pred_data.empty:
64
- # Get column structure from the first row
65
- for col in pred_data.columns:
66
- if col not in player_data and col != 'Predicted_Cost': # Skip the target variable
67
- player_data[col] = 0
68
- except Exception as e:
69
- print(f"Warning - Error reading prediction data template: {str(e)}")
70
-
71
  # Get hero statistics using OpenDota API
72
  try:
73
  hero_stats = hero_information(player_id)
74
- player_data.update(hero_stats.to_dict())
75
-
76
- # Add season identifier to match training data format
77
- player_season = f"{player_id}_S34" # Assuming current season is 34
78
- temp_dict = {}
79
- temp_dict[player_season] = 1.0 # Set current season flag to 1.0
80
- player_data.update(temp_dict)
81
 
 
 
 
 
 
 
 
 
82
  except Exception as e:
83
  print(f"Warning - Error fetching hero data: {str(e)}")
84
- # If hero stats fail, add placeholder values
85
- player_data.update({
86
- "total_games_played": 0,
87
- "total_winrate": 0.0
88
- })
89
-
90
- # Convert to DataFrame for consistency with training
91
- df = pd.DataFrame([player_data])
92
-
93
- # Print out the columns we have in our processed data
94
- print("\nProcessed data columns:")
95
- print(sorted(df.columns.tolist()))
96
- print(f"Number of processed columns: {len(df.columns)}")
97
 
98
- # Find missing columns
99
- expected_cols = set(pred_data.columns) - {'Predicted_Cost'} # Remove target variable
100
- actual_cols = set(df.columns)
101
-
102
- missing_cols = expected_cols - actual_cols
103
- extra_cols = actual_cols - expected_cols
104
 
 
 
105
  if missing_cols:
106
- print("\nMissing columns:")
107
- print(sorted(list(missing_cols)))
108
-
109
- if extra_cols:
110
- print("\nExtra columns:")
111
- print(sorted(list(extra_cols)))
112
-
113
- # Ensure we have all needed columns and remove any extras
114
- for col in missing_cols:
115
- df[col] = 0
116
- df = df[list(expected_cols)]
117
 
118
  print(f"\nFinal number of columns: {len(df.columns)}")
 
 
119
  return df
120
  except Exception as e:
121
  return f"Error processing player data: {str(e)}"
 
38
  def process_player_data(player_id, mmr, comf_1, comf_2, comf_3, comf_4, comf_5):
39
  """Process player data similar to training pipeline"""
40
  try:
41
+ # Define expected columns based on the model's requirements
42
+ expected_columns = ['mmr', 'p1', 'p2', 'p3', 'p4', 'p5', 'count', 'mean', 'std', 'min']
43
+ # Add hero-specific columns
44
+ for hero_id in range(1, 139): # Based on max hero ID 138 from your data
45
+ if hero_id in [139, 140, 141, 142, 143, 144]: # Skip any known gaps
46
+ continue
47
+ expected_columns.extend([f'games_{hero_id}', f'winrate_{hero_id}'])
48
+
49
+ print(f"\nExpected columns: {len(expected_columns)}")
50
+
51
  # Clean player ID from URL if needed
52
  if "/" in player_id:
53
  player_id = player_id.split("/")[-1]
54
 
55
  # Create initial player series
56
  player_data = {
 
57
  "mmr": float(mmr),
58
  "p1": int(comf_1),
59
  "p2": int(comf_2),
60
  "p3": int(comf_3),
61
  "p4": int(comf_4),
62
+ "p5": int(comf_5),
63
+ "count": 0,
64
+ "mean": 0,
65
+ "std": 0,
66
+ "min": 0
67
  }
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  # Get hero statistics using OpenDota API
70
  try:
71
  hero_stats = hero_information(player_id)
72
+ hero_data = hero_stats.to_dict()
 
 
 
 
 
 
73
 
74
+ # Add hero-specific stats
75
+ for col in expected_columns:
76
+ if col.startswith('games_') or col.startswith('winrate_'):
77
+ if col not in hero_data:
78
+ player_data[col] = 0
79
+ else:
80
+ player_data[col] = hero_data[col]
81
+
82
  except Exception as e:
83
  print(f"Warning - Error fetching hero data: {str(e)}")
84
+ # If hero stats fail, add placeholder values for all hero columns
85
+ for col in expected_columns:
86
+ if col.startswith('games_') or col.startswith('winrate_'):
87
+ player_data[col] = 0
 
 
 
 
 
 
 
 
 
88
 
89
+ # Convert to DataFrame and ensure column order
90
+ df = pd.DataFrame([player_data])
 
 
 
 
91
 
92
+ # Verify we have all expected columns
93
+ missing_cols = set(expected_columns) - set(df.columns)
94
  if missing_cols:
95
+ print("\nMissing columns:", missing_cols)
96
+ for col in missing_cols:
97
+ df[col] = 0
98
+
99
+ # Ensure correct column order
100
+ df = df[expected_columns]
 
 
 
 
 
101
 
102
  print(f"\nFinal number of columns: {len(df.columns)}")
103
+ print(f"Column list: {df.columns.tolist()}")
104
+
105
  return df
106
  except Exception as e:
107
  return f"Error processing player data: {str(e)}"