def get_scores(self, year, week=None): games = nflgame.games_gen(year, week) players = nflgame.combine_play_stats(games) output = {} for p in players: score = 0 score += p.passing_tds * self.passing_tds_pts score += p.passing_yds * self.passing_yds_pts score += self.passing_yds_300_bonus if p.passing_yds >= 300 else 0 score += p.passing_ints * self.passing_ints_pts score += p.rushing_yds * self.rushing_yds_pts score += p.rushing_tds * self.rushing_tds_pts score += self.rushing_yds_100_bonus if p.rushing_yds >= 100 else 0 score += p.receiving_yds * self.receiving_yds_pts score += p.receiving_rec * self.receiving_rec_pts score += p.receiving_tds * self.receiving_tds_pts score += self.receiving_yds_100_bonus if p.receiving_yds >= 100 else 0 score += p.puntret_tds * self.puntret_tds_pts score += p.kickret_tds * self.kickret_tds_pts score += p.fumbles_lost * self.fumbles_lost_pts score += p.passing_twoptm * self.passing_twoptm_pts score += p.rushing_twoptm * self.rushing_twoptm_pts score += p.receiving_twoptm * self.receiving_twoptm_pts output[p.name] = score return output
def find_stat_columns(): """ Find all statistic names for players in nflgame. :return: list of set of statistic names """ stat_columns = set() phases = [('PRE', 4), ('REG', 17), ('POST', 4)] seasons = [i for i in range(2009, datetime.datetime.now().year)] for season in seasons: for phase, num_weeks in phases: for week in range(1, num_weeks + 1): try: games = nflgame.games(year=season, week=week, kind=phase) except TypeError: continue players = nflgame.combine_play_stats(games) for player in players: for stat in player._stats: stat_columns.add(stat) return stat_columns
def update_week(week): """ Update stats & points for the Week's DefenseWeek and PlayerWeek children. """ teams = {team.abbreviation: initialize_defense_week(week, team) for team in NFLTeam.query.all()} games = nflgame.games(week.season.year, week=week.number) for game in games: try: teams[game.winner].wins = 1 except (KeyError, AttributeError): pass teams[game.home].points_allowed = game.score_away teams[game.away].points_allowed = game.score_home player_stats = nflgame.combine_play_stats(games) for player in player_stats: update_defense_by_player(teams[player.team], player) update_player_week(player, week) for defense in teams.values(): try: defense._score_defense() except TypeError: # for teams on bye pass db.session.merge(week) db.session.commit()
def build_week_players(self, week, season, games): """ This method gets all of the information for each player. """ #Dictionary to hold the defense information for the week defense_week = {} #Get player stats from nflgame player_stats = nflgame.combine_play_stats(games) #Process every player for player in player_stats: self.process_player(season, week, player, defense_week) #Copy final defense values into defense dictionary for team in defense_week: if team in self.defense: if self.teams[team].home: defense_week[team].df['points_allowed'] = self.game_list[ season][week][team].score_away else: defense_week[team].df['points_allowed'] = self.game_list[ season][week][team].score_home self.defense[team].update(defense_week[team].df, 1, 1) else: temp_def = Defense(self.teams[team]) if self.teams[team].home: defense_week[team].df['points_allowed'] = self.game_list[ season][week][team].score_away else: defense_week[team].df['points_allowed'] = self.game_list[ season][week][team].score_home temp_def.update(defense_week[team].df, 1, 1) self.defense[team] = temp_def #Update ranks for game in games: self.ranks.add_game(game)
def receiving_targets(game, team): # plays = get_team_combine_stats(team, week=week) plays = nflgame.combine_play_stats([game]) team_plays = plays.filter(team=team) receiving_targets = { str(i.name): i.receiving_tar for i in team_plays.receiving() } return receiving_targets
def correct_team(player_id,season,week): print('IN DEPRECATED FUNCTION') filename='nflleague/espn-league-json/cached/teamcorrections.json' teams={} print(player_id) if os.path.isfile(filename): teams=json.loads(open(filename).read()) try: return teams['{}{}'.format(player_id,season)] except KeyError: try: games=nflgame.games(season,week) stats=list(nflgame.combine_play_stats(games).filter(playerid=player_id))[0] teams['{}{}'.format(player_id,season)]=stats.team with open(filename,'w') as out: json.dump(teams,out,indent=4,separators=(',',': ')) out.close() return stats.team except IndexError: print(player_id)
def test_reset_with_data(self): self.db.insert_teams(nflgame.teams) self.db.insert_games(nflgame.sched.games) self.db.insert_players(nflgame.players.values()) games = nflgame.games(year=2011, week=16, kind='REG') for game in games: players = nflgame.combine_play_stats([game]) for p in players: self.db.insert_player_game_statistics(p.playerid, game.eid, p._stats) self.db.reset() valid_tables = { 'Players', 'Games', 'Teams', 'Player_Game_Statistics', 'Team_Game_Statistics' } for table in valid_tables: self.assertRaises(sql.OperationalError, self.db.cursor.execute, "SELECT * FROM " + table)
def using_nfl_game(year, weeknum): games = nflgame.games(year, week=weeknum) # players = nflgame.combine_game_stats(games) players = nflgame.combine_play_stats(games) player_list = [] player_dict = {} for p in players: # print p.formatted_stats() player_dict = { 'player': '%s' %p, 'player_pos': str(p.player), 'playerid': p.playerid, 'team': p.team, 'rushing_att' + str(weeknum): p.rushing_att, 'rushing_yds' + str(weeknum): p.rushing_yds, 'rushing_tds' + str(weeknum): p.rushing_tds, 'passing_att' + str(weeknum): p.passing_att, 'passing_cmp' + str(weeknum): p.passing_cmp, 'passing_cmp_air_yds' + str(weeknum): p.passing_cmp_air_yds, 'passing_incmp' + str(weeknum): p.passing_incmp, 'passing_incmp_air_yds' + str(weeknum): p.passing_incmp_air_yds, 'passing_int' + str(weeknum): p.passing_int, 'passing_sk' + str(weeknum): p.passing_sk, 'passing_sk_yds' + str(weeknum): p.passing_sk_yds, 'passing_yds' + str(weeknum): p.passing_yds, 'passing_tds' + str(weeknum): p.passing_tds, 'receiving_tar' + str(weeknum): p.receiving_tar, 'receiving_rec' + str(weeknum): p.receiving_rec, 'receiving_yds' + str(weeknum): p.receiving_yds, 'receiving_tds' + str(weeknum): p.receiving_tds, 'receiving_yac_yds' + str(weeknum): p.receiving_yac_yds # 'formatted_stats' + str(weeknum): p.formatted_stats() } player_list.append(player_dict) df_players = pd.DataFrame(player_list) return df_players
def get_team_combine_stats(team, week=None): games = nflgame.games(2017, home=team, away=team, week=week) plays = nflgame.combine_play_stats(games) team_plays = plays.filter(team=team) return team_plays
for x in team_list: xTeamRoster = [] for y in player_list_fant: if getTeam(y)==x: playerInfo = formatName(y) + "#" + print_playerid(formatName(y),x)+"$"+x+"&"+retPos(y) xTeamRoster.append(playerInfo) team_rosters[x]=xTeamRoster print x+" roster loaded." #yearly games combined #kickers need play by play since kick distances are not recorded in combine for x in team_list: teamGames=nflgame.games(year,home=x,away=x,kind='REG') teamCombined=nflgame.combine_play_stats(teamGames) playnum=-1 print x+" stats loaded." for y in team_rosters[x]: playnum=playnum+1 playerStats = teamCombined.playerid(retID(y)) try: team_rosters[x][playnum]=team_rosters[x][playnum]+"%"+playerStats.formatted_stats() #print playerStats.formatted_stats() except AttributeError: print "No stats for "+y outBook = xlwt.Workbook() outSheet=outBook.add_sheet("IndivStats") cols = ["First","Last","Team","Pos","GSIS"]
def _insert_game_statistics(self, update): """ Insert game statistics for all players and teams in games in all phases. Players with ids not found in nflgame.players are ignored. :param update: If True, only add games not completely present in the database. Completeness is checked by ensuring both teams in the game have team statistics recorded. Else, add all games starting in 2009. :return: None """ phases = [('PRE', 4), ('REG', 17), ('POST', 4)] seasons = [i for i in range(2009, datetime.datetime.now().year)] # I acknowledge this is terribly ugly. # This can potentially be multi-threaded, but I figured the speedup is # kind of small due to the bottleneck of writing to the db. for season in seasons: for phase, num_weeks in phases: for week in range(1, num_weeks + 1): try: games = nflgame.games(year=season, week=week, kind=phase) except TypeError: continue for game in games: if update is True: res = self.db.cursor.execute("SELECT * FROM " "Team_Game_Statistics " "WHERE eid = ?", (game.eid,)).fetchall() if len(res) == 2: continue elif len(res) == 1: self.db.cursor.execute("DELETE FROM " "Team_Game_Statistics " "WHERE eid = ?", (game.eid,)) self.db.cursor.execute("DELETE FROM " "Player_Game_Statistics " "WHERE eid = ?", (game.eid,)) players = nflgame.combine_play_stats([game]) team_stats = { game.home: Counter({}), game.away: Counter({}) } for p in players: if p.playerid not in nflgame.players: continue self.db.insert_player_game_statistics(p.playerid, game.eid, p._stats) team_stats[p.team] += Counter(p._stats) for team, stats in team_stats.items(): self.db.insert_team_game_statistics(team, game.eid, stats)
import nflgame import csv import os #Define a list for every week in the NFL regular season weeks = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17] #For every week in the weeks list, grab the player game logs and create a csv file for each week for week in weeks: nflgame.combine_play_stats(nflgame.games(2017, week=week)).csv('output/'+str(week)+'.csv', allfields=True) # nflgame.combine_play_stats(nflgame.games(2011, week=week)) #The previous function created an empty row between each row of data. The function below brings back in every csv, removes the empty rows, and creates new csv files for week in weeks: input = open('output/'+str(week)+'.csv', 'rb') output = open('output/'+'Week'+str(week)+'.csv', 'wb') writer = csv.writer(output) for row in csv.reader(input): if row: writer.writerow(row) input.close() output.close() #Delete unused csv files for week in weeks: os.remove('output/'+str(week)+'.csv')