-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc_h2h_points.py
151 lines (97 loc) · 3.8 KB
/
calc_h2h_points.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
Calculate valuations for the rest of season and the next few weeks, based on the game schedule
"""
import datetime
import math
import pandas
from config import SEASON_START, LAST_WEEK, MY_TEAM_ID
from utils import load_projections, load_rosters
NEXT_WEEK = int(math.ceil((datetime.datetime.today() - SEASON_START).days / 7.0) + 1)
WEEKS_AHEAD = 3
def main():
rosters = load_rosters()
projections = load_projections()
valuation = projections.merge(rosters, on='name', how='left')
schedule = load_schedule()
valuation = valuation.merge(schedule, on='team')
valuation = add_weekly_valuation(valuation)
valuation = check_if_top_n(valuation)
output_csv(valuation)
def load_schedule():
"""
Load rest of year schedule
"""
schedule = pandas.read_csv("schedule_{}.csv".format(SEASON_START.year))
schedule['ros'] = 0
for i in range(NEXT_WEEK, LAST_WEEK + 1):
schedule['ros'] += schedule['W{}'.format(i)]
return schedule
def add_weekly_valuation(valuation):
"""
Based on the next few weeks, calculate the projected points
"""
for week_num in range(NEXT_WEEK, LAST_WEEK + 1):
valuation['W{} fpoints'.format(week_num)] = valuation['W{}'.format(week_num)] * valuation['fpoints']
# assume each player plays a projected % of games
# apply that % to the remaining games on schedule
valuation['ros gtp'] = valuation['gp'] / valuation['team_gtp'] * valuation['ros']
valuation['ros fpoints'] = valuation['ros gtp'] * valuation['fpoints']
valuation['ros pct'] = valuation['ros fpoints'] / valuation['ros fpoints'].max()
return valuation
def check_if_top_n(valuation):
"""
For each remaining week, check if the player would rank in the top N per week for your team
"""
top_n_data = []
current_team = valuation[valuation['team_id'] == MY_TEAM_ID].copy()
for i in range(len(valuation)):
playable_weeks = []
playable_points = 0
if any(valuation.iloc[i]['name'] == current_team['name']):
consider = current_team
else:
consider = current_team.copy().append(valuation.iloc[i])
for week_num in range(NEXT_WEEK, LAST_WEEK + 1):
consider.sort_values(['W{} fpoints'.format(week_num), 'fpoints'], ascending=False, inplace=True)
if any(valuation.iloc[i]['name'] == consider.head(10)['name']):
playable_weeks.append('{}'.format(week_num))
playable_points += valuation.iloc[i]['W{} fpoints'.format(week_num)]
top_n_data.append({
'playable_weeks': ','.join(playable_weeks),
'n_playable_weeks': len(playable_weeks),
'playable_points': playable_points
})
valuation['n_playable_weeks'] = [x['n_playable_weeks'] for x in top_n_data]
valuation['playable_weeks'] = [x['playable_weeks'] for x in top_n_data]
valuation['playable_points'] = [x['playable_points'] for x in top_n_data]
return valuation
def output_csv(valuation):
"""
Export valuations to CSV
"""
csv_columns = [
'name',
'team',
'pos',
'team_id',
'fpoints',
# 'gtp',
'mpg',
]
weekly_columns_base = ['W{}'.format(i) for i in range(NEXT_WEEK, NEXT_WEEK + WEEKS_AHEAD)]
weekly_columns_base += ['ros']
weekly_columns = ['{} fpoints'.format(x) for x in weekly_columns_base]
csv_columns += weekly_columns_base
csv_columns += weekly_columns
csv_columns += [
'ros gtp',
'ros pct',
'yahoo_id',
'n_playable_weeks',
'playable_weeks',
'playable_points',
]
valuation.sort_values(weekly_columns[0], ascending=False, inplace=True)
valuation[csv_columns].to_csv('valuation.csv', encoding='utf8', index=False)
if __name__ == '__main__':
main()