Exemple #1
0
def get_team_defense_stat(game, team, stat):
    """Returns the count of a particular stat awarded to a team on a given week
    of play.

    Code modified from https://github.com/BurntSushi/nflgame/wiki/Cookbook
    #calculate-number-of-sacks-for-a-team
    Filter part of modification comes from last comment of
    https://github.com/BurntSushi/nflgame/issues/48
    """
    plays = nflgame.combine_plays([game])

    count = 0
    if stat == 'defense_frec':
        for play in plays.filter(defense_frec__gt=0):
            if (play.punting_tot == 0) and (play.kicking_tot == 0):
                # defensive fumble recoveries go to the team that doesn't start
                # with the ball if it's not a punting/kicking play
                if play.team != team:
                    count += getattr(play, stat)
            else:
                # defensive fumble recoveries go to the team that starts with
                # the ball if it's a punting/kicking play
                if play.team == team:
                    count += getattr(play, stat)
    elif stat == 'defense_two_pt_return':
        raise ValueError("defense_two_pt_return should be handled outside of "
                         "get_team_defense_stat()!")
    else:
        for play in plays.filter(team__ne=team):
            count += getattr(play, stat)
    return count
Exemple #2
0
def get_defense_two_pt_returns(year, week):
    """Returns a dictionary of `players` and `teams` Counters, which store the
    number of defensive 2 point returns awarded to any player and team for a
    given week of play. This function is necessary because there don't seem to
    be easy ways to get this stat otherwise.
    """
    defense_two_pt_returns_dict = {
        'players': Counter(),
        'teams': Counter(),
    }
    for game in nflgame.games(year, week):
        home_team, away_team = game.home, game.away
        for play in nflgame.combine_plays([game]):
            if ('DEFENSIVE TWO-POINT ATTEMPT' in str(play)
                    and 'ATTEMPT SUCCEEDS' in str(play)):
                # Determine scoring team
                if play.team == home_team:
                    team = away_team
                else:
                    team = home_team
                # Guess scoring player. Can't find any stats for this, but the
                # scoring player is usually listed at the beginning of the
                # sentence before "ATTEMPT SUCCEEDS" in the play description.
                sentences = str(play).split('. ')
                i = -1
                while 'ATTEMPT SUCCEEDS' not in sentences[i + 1]:
                    i += 1
                player = sentences[i].split()[0]
                # Add to counters for player and team
                defense_two_pt_returns_dict['players'][player] += 1
                defense_two_pt_returns_dict['teams'][team] += 1
    return defense_two_pt_returns_dict
def defensePlays(team, year, week, games):
    plays = nflgame.combine_plays(games)
    dfrectd=0
    dffum=0
    dsk=0
    dint=0
    dinttd=0
    punttd=0
    kicktd=0
    dsafe=0
    sack=0
    for p in plays.filter(team__ne=team):
        if p.defense_ffum > 0: 
            dffum += 1
        if p.defense_int > 0:
            dint += 1
        if p.defense_int_tds > 0:
            dinttd += 1
        if p.defense_frec_tds > 0:
            dfrectd += 1
        if p.defense_safe > 0:
            dsafe += 1
        if p.puntret_tds > 0:
            punttd += 1
        if p.kickret_tds > 0:
            kicktd += 1
        if p.defense_sk > 0:
            sack += 1
    return 2*dffum + 2*dint + 6*dinttd + 6*dfrectd + 2*dsafe + 6*punttd + 6*kicktd + 1*sack
Exemple #4
0
def conversionPercentage():
    onePointAttempt = 0
    onePointSucceed = 0

    twoPointAttempt = 0
    twoPointSucceed = 0

    plays = nflgame.combine_plays(games)
    for key in plays:
        playString = str(key)
        if "two-point conversion" in playString.lower():
            twoPointAttempt += 1
            if ("attempt succeeds" in playString.lower()):
                twoPointSucceed += 1
        if "extra point" in playString.lower():
            onePointAttempt += 1
            if ("good" in playString.lower()):
                onePointSucceed += 1

    print("---- one point conversion ----")
    print("success = ", onePointSucceed, " twoPointAttempt = ",
          onePointAttempt)
    print("success rate = ", float(onePointSucceed / onePointAttempt))

    print("---- two point conversion ----")
    print("success = ", twoPointSucceed, " twoPointAttempt = ",
          twoPointAttempt)
    print("success rate = ", float(twoPointSucceed / twoPointAttempt))
def process_game(game, year, f_penalty):
    players = {}
    hometeam = game.home
    plays = nflgame.combine_plays([game])
    for play in plays:

        # Preprocess and then parse a play string
        play = str(play)
        parsed_play = preprocess_play(play, f_penalty)
        if not parsed_play:
            continue  # Something irrelevant happened, e.g. running play.
        team, name, yds = parsed_play[0], parsed_play[1], parsed_play[2]

        # Make or get a player from the player dictionary
        if (name, team, year) not in players.keys():
            player = Player(name, team, year)
            if team == hometeam:
                player.home.games = 1
            else:
                player.away.games = 1
            players[(name, team, year)] = player
        else:
            player = players[(name, team, year)]

        # Update a player's receiving stats
        player.update_player(play, hometeam, yds)

    return players
Exemple #6
0
 def game_pbp(self, team, year, week):
     """
     Returns a list of all plays in a game.
     """
     game = self.teams[team][year][week]['OWN']['game']
     plays = ng.combine_plays([game])
     return [game.nice_score(), ''] + [str(play) for play in plays]
def total_sacks_given(s,w,t):
  games = nflgame.games_gen(s,w,t,t)
  plays = nflgame.combine_plays(games)

  sks = 0
  for p in plays.filter(team=t):
    if p.defense_sk > 0:
      sks += 1
  return sks
Exemple #8
0
def gen_player_stats(season, week, player_id, team, game=None):
    fp = 'nflleague/espn-league-json/cache/C{}.json'
    filepath = fp.format(player_id)
    cache = get_json(filepath, {})
    game_eid = nflleague.players[player_id]['schedule'].get(
        str(season), {}).get(str(week), 'bye')
    week, season = str(week), str(season)
    if season not in cache.keys():
        cache[season] = {}
    if week not in cache[season].keys() or (season, week) == (str(
            nflleague.c_year), str(nflleague.c_week)):
        if game_eid == 'bye':
            cache[season][week] = {'BYE': True}
            save_json(filepath, cache)
            return cache[season][week]

        if game == None:
            game = nflgame.game.Game(game_eid)

        game_status = get_game_status(game)
        #Only update if game is currently playing or player stats haven't been cached yet
        if game_status in ['NOT PLAYED', 'PREGAME']:
            return {}
        if game_status in ['PLAYING', 'HALFTIME'
                           ] or week not in cache[season].keys():
            player = None
            if player_id in nflleague.players:
                player = Player(season, week, player_id)
            print('Caching Player {}, {} {} (Y:{}  W:{})'.format(
                player.full_name, team, player.position, season, week))
            play_stats = nflgame.combine_max_stats([game])
            player_stats = list(play_stats.filter(playerid=player_id))
            if len(player_stats) != 0:
                cache[season][week] = player_stats[0].stats
            else:
                cache[season][week] = {}
            #Any specialty stats that need to be broken down by play or some other metric can be added here
            if player.position == 'K':
                #Need to break down kicker scoring by play here because most efficient way to find length of indvl field goal.
                #Adds num of field goals made in 0-39,40-49,50+ ranges to kicker's stats dictionary.
                play_stats = nflgame.combine_plays([game])
                plays = list(
                    filter(lambda p: p.has_player(player_id), play_stats))
                cache[season][week] = defaultdict(int, cache[season][week])
                for play in plays:
                    if 'kicking_fgm' in play._stats:
                        if play._stats['kicking_fgm_yds'] <= 39:
                            cache[season][week]['kicking_fgm_0_39'] += 1
                        elif play._stats['kicking_fgm_yds'] <= 49:
                            cache[season][week]['kicking_fgm_40_49'] += 1
                        elif play._stats['kicking_fgm_yds'] >= 50:
                            cache[season][week]['kicking_fgm_50_100'] += 1
                    elif 'kicking_fgmissed' in play._stats and int(
                            play._stats['kicking_fgmissed_yds']) <= 39:
                        cache[season][week]['kicking_fgmissed_0_39'] += 1
            save_json(filepath, cache)
    return cache[season][week]
Exemple #9
0
 def getDefSafetysMade(self,s, w):
     games = nflgame.games_gen(s, w, self.team ,self.team)
     sftys = 0
     # Bye Week ? If yes, get out of the function
     if games is None:
         return sftys
     plays = nflgame.combine_plays(games)
     for play in plays.filter(team__ne=self.team):
         if play.defense_safe>0:
             sftys = sftys+1
     return sftys
Exemple #10
0
 def getDefRecoveryTDsScored(self,s, w):
     games = nflgame.games_gen(s, w, self.team ,self.team)
     defRecTDs = 0
     # Bye Week ? If yes, get out of the function
     if games is None:
         return defRecTDs
     plays = nflgame.combine_plays(games)
     for play in plays.filter(team__ne=self.team):
         if play.defense_frec_tds>0:
             defRecTDs = defRecTDs+1
     return defRecTDs
Exemple #11
0
 def getDefFGBlkMade(self,s, w):
     games = nflgame.games_gen(s, w, self.team ,self.team)
     fgblk = 0
     # Bye Week ? If yes, get out of the function
     if games is None:
         return fgblk
     plays = nflgame.combine_plays(games)
     for play in plays.filter(team__ne=self.team):
         if play.defense_fgblk>0:
             fgblk = fgblk+1
     return fgblk
Exemple #12
0
 def getKickRetTDsMade(self,s, w):
     games = nflgame.games_gen(s, w, self.team ,self.team)
     krtds = 0
     # Bye Week ? If yes, get out of the function
     if games is None:
         return krtds
     plays = nflgame.combine_plays(games)
     for play in plays.filter(team__ne=self.team):
         if play.kickret_tds>0:
             krtds = krtds+1
     return krtds
def kicker_pts(year, week):
    games = nflgame.games(year, week)
    plays = nflgame.combine_plays(games)

    fgs_xps = defaultdict(list)
    for play in plays.filter(kicking_fga=True):
        for p in play.players:
            points = score_player(p)
            fgs_xps[str(p.playerid)].append(points)
            # print(str(p), p.kicking_fgm_yds, points)

    plays = nflgame.combine_plays(games)
    for play in plays.filter(kicking_xpa=True):
        for p in play.players:
            points = score_player(p)
            fgs_xps[str(p.playerid)].append(points)
            # print(str(p), p.kicking_xpmade, points)

    for kicker in fgs_xps:
        fgs_xps[kicker] = sum(fgs_xps[kicker])

    return fgs_xps
Exemple #14
0
def rushingyards(playerid,team,year,week=None):
  try:
    rushing_yds_per_att = []
    current_year = 2016
    current_week = 17
    if week:
      weeks = [int(week)]
    else:
      current_year, current_week = nflgame.live.current_year_and_week()
      weeks = [x for x in range(1, current_week+1)] if int(year) == int(current_year) else [x for x in range(1, 18)]

    try:
      games = nflgame.games(int(year), week=weeks, home=team, away=team)
    except (ValueError, KeyError, TypeError):
      print "FAILED"
      return jsonify(result = rushing_yds_per_att)

    if games != []:
      allplays = nflgame.combine_plays(games)
      player_position = nflgame.players[playerid].position
      for p in allplays:
        if p.has_player(playerid):
          if (p.receiving_tar==1) or (p.rushing_att==1):
            if p.rushing_att==1:
              type = 'RUSH'
            elif p.receiving_rec==1:
              type = 'PASS'
            else:
              type = 'INCOMPLETE'
            if (player_position == 'QB') and (type == 'PASS' or type == 'INCOMPLETE'):
              pass
            else:
              play = {
                'type': type, 
                'yards': p.rushing_yds if p.rushing_att==1 else p.receiving_yds, 
                # 'desc': str(re.sub(r'\([^)]*\)', '', p.desc)), 
                'desc': str(p.desc), 
                'down': str(p.down) + ' and ' + str(p.yards_togo),
                'time': str(p.time),
                'position': str(p.yardline),
                'game': str(p.drive.game), 
                'week': p.drive.game.schedule['week']
              }
              rushing_yds_per_att.append(play)

    else:
      print "EMPTY"
    gc.collect()
    return jsonify(result = rushing_yds_per_att)
  except (ValueError, KeyError, TypeError):
    abort(400, 'custom error message to appear in body')
Exemple #15
0
 def getDefRushingYardsAllowed(self,s, w):
     games = nflgame.games_gen(s, w, self.team , self.team)
     rushYards = 0
     
     # Bye Week ? If yes, get out of the function
     if games is None:
         return rushYards    
     
     plays = nflgame.combine_plays(games)
     
     for p in plays.filter(team__ne=self.team, rushing_att__ge=1):
             rushYards += p.rushing_yds
     
     return rushYards
Exemple #16
0
    def getDefSacks(self,s, w):
        games = nflgame.games_gen(s, w, self.team , self.team)
        sks = 0
        # Bye Week ? If yes, get out of the function
        if games is None:
            return sks    

        plays = nflgame.combine_plays(games)

        for p in plays.filter(team__ne=self.team):
            if p.defense_sk > 0:
                sks += 1
        
        return sks
Exemple #17
0
def get_rushing_yds_against(seas, week, team):
    '''
    Returns a tuple containing a team and rushing yards against said team for season and week(s)
    '''
    games = nflgame.games_gen(seas, week, team, team)
    if games == None:
        # Catch all for teams that have moved or joined the league.
        return team, 0
    plays = nflgame.combine_plays(games)

    yds = 0
    for p in plays.filter(team__ne=team, rushing_yds__ge=1):
        yds += p.rushing_yds
    return team, yds
Exemple #18
0
 def getDefReceptionsAllowed(self,s, w):
     games = nflgame.games_gen(s, w, self.team , self.team)
     receptions = 0
     
     # Bye Week ? If yes, get out of the function
     if games is None:
         return receptions    
     
     plays = nflgame.combine_plays(games)
     
     for p in plays.filter(team__ne=self.team, passing_att__ge=1):
             receptions += p.receiving_rec
     
     return receptions
Exemple #19
0
 def getDefPassingTDsAllowed(self,s, w):
     games = nflgame.games_gen(s, w, self.team , self.team)
     passTDs = 0
     
     # Bye Week ? If yes, get out of the function
     if games is None:
         return passTDs    
     
     plays = nflgame.combine_plays(games)
     
     for p in plays.filter(team__ne=self.team, passing_att__ge=1):
             passTDs += p.passing_tds
     
     return passTDs
Exemple #20
0
    def getDefInterceptions(self,s, w):
        games = nflgame.games_gen(s, w, self.team , self.team)
        interceptions = 0
        
        # Bye Week ? If yes, get out of the function
        if games is None:
            return interceptions    
        
        plays = nflgame.combine_plays(games)
        
        for p in plays.filter(team__ne=self.team, passing_int__gt=0):
                interceptions += p.passing_int

        return interceptions
Exemple #21
0
def get_rushing_yds(seas, week, team):
    '''
    This will return a teams rushing yards for a specific season and week.
    '''
    games = nflgame.games_gen(seas, week, team, team)
    if games == None:
        # Catch all for teams that have moved or joined the league.
        return team, 0
    plays = nflgame.combine_plays(games)

    yds = 0
    for p in plays.filter(team=team, rushing_yds__ge=1):
        if isDebug:
            print p
        yds += p.rushing_yds
    return team, yds
Exemple #22
0
def rushingyards(playerid, team, year, week=None):
    try:
        if week:
            weeks = [int(week)]
        else:
            weeks = ypc.get_weeks(year)
        games = nflgame.games(int(year), week=weeks, home=team, away=team)
        if games != []:
            all_plays = nflgame.combine_plays(games)
            rushing_yds_per_att = list(
                ifilter(
                    ypc.exists,
                    imap(lambda x: ypc.parse_rushing_play(x, playerid),
                         all_plays)))
        return jsonify(result=rushing_yds_per_att)
    except Exception as e:
        app.logger.error("error: {}".format(e))
        return jsonify(result=[])
Exemple #23
0
def receivingyards(playerid,team,year,week=None):
  try:
    receiving_yds_per_att = []
    current_year = 2016
    current_week = 17
    if week:
      weeks = [int(week)]
    else:
      current_year, current_week = nflgame.live.current_year_and_week()
      weeks = [x for x in range(1, current_week+1)] if int(year) == int(current_year) else [x for x in range(1, 18)]

    try:
      games = nflgame.games(int(year), week=weeks, home=team, away=team)
    except (ValueError, KeyError, TypeError):
      return jsonify(result = receiving_yds_per_att)

    if games != []:
      allplays = nflgame.combine_plays(games)
      for p in allplays:
        if p.has_player(playerid):
          if (p.receiving_tar==1):
            if p.receiving_rec==1:
              type = 'PASS'
            else:
              type = 'INCOMPLETE'
            play = {
              'type': type,
              'complete': p.receiving_rec,
              'yards': p.receiving_yds, 
              'yac_yards': p.receiving_yac_yds, 
              'desc': str(p.desc), 
              'down': str(p.down) + ' and ' + str(p.yards_togo), 
              'time': str(p.time),
              'position': str(p.yardline),
              'game': str(p.drive.game), 
              'week': p.drive.game.schedule['week']}
            receiving_yds_per_att.append(play)
    gc.collect()
    return jsonify(result = receiving_yds_per_att)
  except (ValueError, KeyError, TypeError):
    abort(400, 'custom error message to appear in body')
Exemple #24
0
def plays_by_team():
    name, year, week = parse_request_arguments(request.args)
    team = None
    player = None

    # The NFL API only recognizes proper upper case names
    name = ' '.join(n.capitalize() for n in name.split(' '))

    players = fetch_player(name)
    if len(players) > 0:
        player = players[0]
        team = player.team

    '''
    Try to perform some arithmetic on our inputs, if they aren't ints, our API
    will throw errors
    '''
    try:
        year = year + 1 - 1
        week = week + 1 - 1
    except TypeError as e:
        return e

    plays = []
    if team and year and week:
        try:
            key = 'plays_by_team|{}|{}|{}'.format(player.name, year, week)
            data = fetch_data_from_redis_by_key(key)
            if not data:
                data = nflgame.combine_plays(fetch_games(year, week, team))
                plays = set_data_on_redis_key(key, [
                    play.data for play in data if team == play.team
                ], True)
            else:
                plays = data
        except TypeError as e: pass

    return plays
def kickerScore(player, year, week):
    points = fpPlayer.fantasyPoints(player,year,week)
    lf = player
    statistics = lf.stats(year, week)
    if 'kicking_xpmissed' in statistics.stats:
        xpMissed = statistics.stats['kicking_xpmissed']
    if 'kicking_xpmade' in statistics.stats:
        xpMade = statistics.stats['kicking_xpmade']
    if 'kicking_fgm' in statistics.stats:
        fgMade = statistics.stats['kicking_fgm']
    if 'kicking_fgm' in statistics.stats and 'kicking_fga' in statistics.stats:
        fgMissed = statistics.stats['kicking_fga'] - statistics.stats['kicking_fgm']
    points = points + xpMade*1 - xpMissed*1 + fgMade*3 - fgMissed*3
    games = nflgame.games(year, week)
    plays = nflgame.combine_plays(games)
    allMadeFGs = plays.filter(kicking_fgm = True)
    for p in allMadeFGs:
        if p.players.playerid(lf.playerid):
            yards = p.kicking_fgm_yds
            if yards >= 40:
                points = points + 1
            if yards >= 50:
                points = points + 1
    return points
def total_touchdowns(first_year=2009, final_year=2016):

    ten_total_data = []
    for i in range(1, 100):
        ten_total_data.append([0, 0])

    for years in range(first_year, final_year + 1):
        print "Starting year {}".format(years)
        touchdown_yards = []
        all_yards = []

        for wk in range(1, 17 + 1):
            print "Gathering week {} games, this may take a second".format(wk)
            games = nflgame.games(year=years, week=wk)
            print "Done gathering week {} games".format(wk)
            plays = nflgame.combine_plays(games)

            for p in plays:
                if p.data['yrdln'] != '' and p.data['yrdln'] != None and p.data[
                        'note'] != 'XP' and p.data['note'] != 'XPM':
                    yards = get_play_yards(p)
                    all_yards.append(yards)
                    if p.touchdown is True:
                        touchdown_yards.append(yards)

        total_data = []
        for i in range(1, 100):
            total_data.append([0, 0])
        for i in all_yards:
            total_data[i - 1][0] += 1
            ten_total_data[i - 1][0] += 1

        for i in touchdown_yards:
            total_data[i - 1][1] += 1
            ten_total_data[i - 1][1] += 1

        for i in range(len(total_data)):
            print "{} yards: All Plays: {} Touchdowns: {}".format(
                i + 1, total_data[i][0], total_data[i][1])

        file = "{}.csv".format(years)
        with open(file, 'wb') as f:
            writer = csv.writer(f,
                                delimiter=',',
                                quotechar='|',
                                quoting=csv.QUOTE_MINIMAL)
            writer.writerow(["Yard Line", "Number of Plays", "Touchdowns"])
            yard = 1
            for i in total_data:
                writer.writerow([yard, i[0], i[1]])
                yard += 1

    with open("2009_2016.csv", 'wb') as f:
        writer = csv.writer(f,
                            delimiter=',',
                            quotechar='|',
                            quoting=csv.QUOTE_MINIMAL)
        writer.writerow(["Yard Line", "Number of Plays", "Touchdowns"])
        yard = 1
        for i in ten_total_data:
            writer.writerow([yard, i[0], i[1]])
            yard += 1
def score_at_play(play):
    """
    Gets the current score at the time of the play.
    (Does so by iterating up until the point of the play.)
    Arg: play: a Play object
    Returns: List containing score of home team, score of away team
    """
    # Identify home team and away team
    home_team = play.drive.game.home
    away_team = play.drive.game.away

    # Initialize score at beginning of game; score[0] = away, score[1] = home
    score = [0, 0]

    # Initialize current game's plays
    current_game = play.drive.game
    current_game_plays = nflgame.combine_plays([current_game])

    for i in current_game_plays:
        # Using this to truncate in case of play reviews
        description = i.desc

        # If this is the same play as our identified play
        if (play.desc == i.desc):
            return score

        # If the play was reviewed and reversed, truncate to actual result.
        if ("and the play was REVERSED" in description):
            description = description.split("and the play was REVERSED.")[1]

        # Identify team in possession
        pos_team = i.data['posteam']

        # Identify if interception happens
        if ("INTERCEPTED" in description):
            if (home_team == pos_team):
                pos_team = away_team  # Possession changes hands
            else:
                pos_team = home_team

        # Identify if fumble recovery turnover happens
        if ("RECOVERED by" in description):
            if (
                    "MUFFS" not in description
            ):  # Muffed punt recovered by kicking team, everything stays the same
                if (home_team == pos_team):
                    pos_team = away_team  # Possession changes hands
                else:
                    pos_team = home_team

        # If a touchdown was scored
        if ("TOUCHDOWN" in description):
            if (home_team == pos_team):
                score[1] += 6
            else:
                score[0] += 6

        # If a field goal is scored
        elif ("field goal is GOOD" in description):
            if (home_team == pos_team):
                score[1] += 3
            else:
                score[0] += 3

        # If an extra point is scored
        elif ("extra point is GOOD" in description):
            if (home_team == pos_team):
                score[1] += 1
            else:
                score[0] += 1

        # If two-point conversion is scored
        elif ("TWO-POINT CONVERSION ATTEMPT" in description
              and "ATTEMPT SUCCEEDS" in description):
            if (home_team == pos_team):
                score[1] += 2
            else:
                score[0] += 2

        # If safety is scored
        elif ("SAFETY" in description):
            if (home_team == pos_team):
                score[
                    0] += 2  # Because on a safety, the other team gets points
            else:
                score[1] += 2

    # Ideally we never have to return from here because we find the play
    print("PLAY NOT FOUND")
    return score
    final = final[final[:, 1].argsort()]
    return final[::-1]


if __name__ == "__main__":
    years = range(2009, 2019)
    weeks = range(1, 18)
    seasons_dict = {}

    for i in years:
        week_dict = {}
        for j in weeks:
            try:
                week_dict[j] = nflgame.games(i, week=j)
            except:
                print("YEAR {} WEEK {} FAILED".format(i, j))

        seasons_dict[i] = week_dict

    punts = []
    for i in years:
        for j in weeks:
            try:
                for game in seasons_dict[i][j]:
                    for play in nflgame.combine_plays([game]):
                        if ("punts" in play.desc):
                            punts.append(play)
            except:
                print("Exception Found in SEASON {} WEEK {}".format(i, j))

    print("RESULTS: ", get_final_stackrank(punts))
def total_touchdown_types(first_year=2009, final_year=2016):

    ten_total_data = [[0, 0, 0, 0, 0] for i in range(100)]
    #for i in range(1,100):
    #            ten_total_data.append([0,0])

    for years in range(first_year, final_year + 1):
        print "Starting year {}".format(years)

        rushing_touchdown_yards = []
        passing_touchdown_yards = []
        rushing_yards = []
        passing_yards = []
        all_yards = []

        for wk in range(1, 17 + 1):
            print "Gathering week {} games, this may take a second".format(wk)
            print type(years)
            print type(wk)
            games = nflgame.games(year=years, week=wk)
            print "Done gathering week {} games".format(wk)
            plays = nflgame.combine_plays(games)

            for p in plays:
                if p.data['yrdln'] != '' and p.data['yrdln'] != None and p.data[
                        'note'] != 'XP' and p.data['note'] != 'XPM':
                    yards = get_play_yards(p)
                    all_yards.append(yards)
                    if 'pass' in p.desc.lower() or 'sacked' in p.desc.lower():
                        passing_yards.append(yards)
                        if p.touchdown is True:
                            passing_touchdown_yards.append(yards)

                    #/r/programminghorror
                    elif 'up the middle' in p.desc.lower(
                    ) or 'left end' in p.desc.lower(
                    ) or 'right end' in p.desc.lower(
                    ) or 'right guard' in p.desc.lower(
                    ) or 'right tackle' in p.desc.lower(
                    ) or 'left guard' in p.desc.lower(
                    ) or 'left tackle' in p.desc.lower():
                        rushing_yards.append(yards)
                        if p.touchdown is True:
                            rushing_touchdown_yards.append(yards)
                    else:
                        pass

        total_data = [[0, 0, 0, 0, 0] for i in range(99)]
        #for i in range(1,100):
        #        total_data.append([0,0])
        for i in all_yards:
            total_data[i - 1][0] += 1
            ten_total_data[i - 1][0] += 1

        for i in passing_yards:
            total_data[i - 1][1] += 1
            ten_total_data[i - 1][1] += 1

        for i in passing_touchdown_yards:
            total_data[i - 1][2] += 1
            ten_total_data[i - 1][2] += 1

        for i in rushing_yards:
            total_data[i - 1][3] += 1
            ten_total_data[i - 1][3] += 1

        for i in rushing_touchdown_yards:
            total_data[i - 1][4] += 1
            ten_total_data[i - 1][4] += 1

    # for i in range(len(total_data)):
    #    print "{} yards: All Plays: {} Touchdowns: {}".format(i+1, total_data[i][0],total_data[i][1])

        file = "{}.csv".format(years)
        with open(file, 'wb') as f:
            writer = csv.writer(f,
                                delimiter=',',
                                quotechar='|',
                                quoting=csv.QUOTE_MINIMAL)
            writer.writerow([
                "Yard Line", "Number of Plays", "Passing Plays",
                "Passing Touchdowns", "Rushing Plays", "Rushing Touchdowns"
            ])
            yard = 1
            for i in total_data:
                writer.writerow([yard, i[0], i[1], i[2], i[3], i[4]])
                yard += 1

    with open("2009_2016.csv", 'wb') as f:
        writer = csv.writer(f,
                            delimiter=',',
                            quotechar='|',
                            quoting=csv.QUOTE_MINIMAL)
        writer.writerow([
            "Yard Line", "Number of Plays", "Passing Plays",
            "Passing Touchdowns", "Rushing Plays", "Rushing Touchdowns"
        ])
        yard = 1
        for i in ten_total_data:
            writer.writerow([yard, i[0], i[1], i[2], i[3], i[4]])
            yard += 1
Exemple #30
0
def get_all_plays2(game, spread):
    plays = nflgame.combine_plays([game])
    scores = game.scores
    score_index = 0
    home = 0
    away = 0
    win = (game.score_home >= game.score_away)
    plays2 = []
    for p in plays:
        plays2.append(p)

    all_plays = []
    for i in range(len(plays2)):
        p = plays2[i]
        if (p.down > 0 and str(p.time) != "None"
                and str(p.yardline) != "None"):  # or p.note == "KICKOFF":
            # add play to all plays
            temp_play = [
                game.home, game.away,
                str(p.time), p.down, p.yards_togo,
                str(p.yardline), p.home, home, away, spread, win
            ]
            all_plays.append(convert_play(temp_play))
#         if (not p.note == None) and len(p.note) >= 2 and (p.note[:2] == "2P" or p.note[:2] == "XP"):
#             print(p.note)
# do scoring stuff
        if p.note == "TD":
            pts = 6
            is_home = p.home
            if p.punting_tot == 1:
                is_home = (not is_home)
            if p.defense_tds == 1:
                is_home = (not is_home)


#             is_home = True
#             if (not p.home and p.defense_tds != 1) or (p.home and p.defense_tds == 1):
#                 is_home = False
            conv, score_index = get_conversion(plays2, scores, score_index)
            # add conversion points if it wasn't a defensive 2 point conversion
            if conv != -1:
                pts += conv
            if is_home:
                home += pts
            else:
                away += pts
            # handle defensive 2 point conversion
            if conv == -1:
                if is_home:
                    away += 2
                else:
                    home += 2
            #print(str(home) + "-" + str(away))

        elif p.note == "FG":
            if p.home:
                home += 3
            else:
                away += 3
            score_index += 1

        elif p.note == "SAF":
            if scores[score_index].split("-")[0][:-1] == str(game.home):
                home += 2
            else:
                away += 2
            score_index += 1

    return home, away, all_plays
Exemple #31
0
def get_team_plays(team, week=None):
    games = nflgame.games(2017, home=team, away=team, week=week)
    plays = nflgame.combine_plays(games)
    team_plays = plays.filter(team=team)
    return team_plays
                currentGame]] = currentGameTemp[currentGame]
            currentWeekLocationAndWeatherCondition[currentWeekLocations[
                currentGame]] = currentGameWeatherCondition[currentGame]
            currentGame += 1
        currentWeekLocationAndTemperatures[
            currentWeekLocations[currentGame]] = currentGameTemp[currentGame]
        currentWeekLocationAndWeatherCondition[currentWeekLocations[
            currentGame]] = currentGameWeatherCondition[currentGame]

        currentGame += 2
        for current in currentWeekLocationAndTemperatures:
            print current, currentWeekLocationAndTemperatures[current]
        print ""
        games = nflgame.games(year, week, kind="REG")
        players = nflgame.combine_game_stats(games)
        plays = nflgame.combine_plays(games)
        opponents = dict([(g.home, g.away) for g in games] + [(g.away, g.home)
                                                              for g in games])
        print year, week
        for player in players.receiving().sort('receiving_yds').limit(50):
            currentTeam = str(player.team)
            if len(currentTeam) < 3:
                currentTeam += " "
            currentOpponentTeam = str(opponents[player.team])
            if len(currentOpponentTeam) < 3:
                currentOpponentTeam += " "
            if player.home:
                if (currentWeekLocationAndTemperatures[currentTeam] <= 10):
                    totalColdReceptions += player.receiving_rec
                    totalColdRecYards += player.receiving_yds
                    totalColdGames += 1
def extract_features(start_year, end_year):
    play_features = []
    success_labels = []
    yard_labels = []
    progress_labels = []
    success_cnt = 0

    for year in range(start_year, end_year + 1):
        # split into individual weeks in order to avoid having to load
        # large chunks of data at once
        for week in range(1, 18):
            games = nflgame.games(year, week=week)

            for play in nflgame.combine_plays(games):
                features = defaultdict(float)
                success = 0
                yards = 0
                progress = 0
                desc = ''

                # TODO: include sacks? probably not since we can't assign them to any play option
                # TODO: Additonally maybe even booth review, official timeout?
                # TODO: Fumble plays should count as if Fumble didn't happen?
                # TODO: plays with declined penalties should be counted ((4:52) A.Foster right tackle to HOU 43 for 13 yards (J.Cyprien). Penalty on JAC-S.Marks, Defensive Offside, declined.)
                # TODO: plays with accepted penalties that do not nullify the play should be counted (keyword: No Play)
                # TODO: error with group when using 2013
                # TODO: Should we count Def. Pass Interference? Def. Holding?

                if (play.note == None or play.note == 'TD' or play.note == 'INT') \
                        and (' punt' not in play.desc) \
                        and ('END ' != play.desc[:4]) \
                        and ('End ' != play.desc[:4]) \
                        and ('Two-Minute Warning' not in play.desc) \
                        and ('spiked the ball to stop the clock' not in play.desc) \
                        and ('kneels to ' not in play.desc) \
                        and ('Delay of Game' not in play.desc) \
                        and (play.time is not None) \
                        and ('Penalty on' not in play.desc) \
                        and ('Delay of Game' not in play.desc) \
                        and ('sacked at' not in play.desc) \
                        and ('Punt formation' not in play.desc) \
                        and ('Direct snap to' not in play.desc) \
                        and ('Aborted' not in play.desc) \
                        and ('temporary suspension of play' not in play.desc) \
                        and ('TWO-POINT CONVERSION ATTEMPT' not in play.desc) \
                        and ('warned for substitution infraction' not in play.desc) \
                        and ('no play run - clock started' not in play.desc) \
                        and ('challenged the first down ruling' not in play.desc) \
                        and ('*** play under review ***' not in play.desc) \
                        and ('Direct Snap' not in play.desc) \
                        and ('Direct snap' not in play.desc):

                    features['team'] = play.team
                    if play.drive.game.away == play.team:
                        features['opponent'] = play.drive.game.home
                    else:
                        features['opponent'] = play.drive.game.away
                    timeclock = play.time.clock.split(':')

                    features['time'] = float(timeclock[0]) * 60 + float(
                        timeclock[1])
                    if (play.time.qtr == 1) or (play.time.qtr == 3):
                        features['time'] += 15 * 60

                    if play.time.qtr <= 2:
                        features['half'] = 1
                    else:
                        features['half'] = 2

                    features['position'] = 50 - play.yardline.offset
                    features['down'] = play.down
                    features['togo'] = play.yards_togo

                    if 'Shotgun' in play.desc:
                        features['shotgun'] = 1
                    else:
                        features['shotgun'] = 0

                    full_desc = play.desc
                    full_desc = full_desc.replace('No. ', 'No.')
                    while re.search(r" [A-Z]\. ", full_desc) is not None:
                        match = re.search(r" [A-Z]\. ", full_desc).group(0)
                        full_desc = full_desc.replace(match, match.rstrip())
                    if re.search(r"[^\.] \(Shotgun\)", full_desc) is not None:
                        full_desc = full_desc.replace(" (Shotgun)",
                                                      ". (Shotgun)")
                    full_desc = full_desc.replace('.(Shotgun)', '. (Shotgun)')

                    if re.search(r" a[st] QB for the \w+ ",
                                 full_desc) is not None:
                        match = re.search(r" a[st] QB for the \w+ ",
                                          full_desc).group(0)
                        full_desc = full_desc.replace(match,
                                                      match.rstrip() + '. ')

                    if re.search(r"New QB.{0,20}[0-9]+ \w+?\.w+? ",
                                 full_desc) is not None:
                        match = re.search(r"New QB.{0,20}[0-9]+ \w+?\.w+? ",
                                          full_desc).group(0)
                        full_desc = full_desc.replace(match,
                                                      match.rstrip() + '. ')

                    if re.search(r"New QB.{0,20}[0-9]+ \w+?[\.\, ] ?\w+? ",
                                 full_desc) is not None:
                        match = re.search(
                            r"New QB.{0,20}[0-9]+ \w+?[\.\, ] ?\w+? ",
                            full_desc).group(0)
                        full_desc = full_desc.replace(match,
                                                      match.rstrip() + '. ')

                    if re.search(r"\#[0-9]+ Eligible ", full_desc) is not None:
                        match = re.search(r"\#[0-9]+ Eligible ",
                                          full_desc).group(0)
                        full_desc = full_desc.replace(match,
                                                      match.rstrip() + '. ')

                    full_desc = full_desc.replace(
                        'New QB for Denver - No.6 - Brock Osweiler ',
                        'New QB for Denver - No.6 - B.Osweiler. ')

                    full_desc = full_desc.replace(' at QB ', ' at QB. ')
                    full_desc = full_desc.replace(' at qb ', ' at QB. ')
                    full_desc = full_desc.replace(' at Qb ', ' at QB. ')
                    full_desc = full_desc.replace(' in as QB for this play ',
                                                  ' in as QB for this play. ')
                    full_desc = full_desc.replace(' in as QB ', ' in as QB. ')
                    full_desc = full_desc.replace(' in as quarterback ',
                                                  ' in as QB. ')
                    full_desc = full_desc.replace(' in at Quarterback ',
                                                  ' in as QB. ')
                    full_desc = full_desc.replace(' is now playing ',
                                                  ' is now playing. ')
                    full_desc = full_desc.replace(' Seminole Formation ', ' ')
                    full_desc = full_desc.replace(' St. ', ' St.')
                    full_desc = full_desc.replace(' A.Randle El ',
                                                  ' A.Randle ')
                    full_desc = full_desc.replace('Alex Smith ', 'A.Smith ')

                    if (re.search(r"New QB \#[0-9]+ \w+?\.\w+? ", full_desc)
                            is not None):
                        match = re.search(r"New QB \#[0-9]+ \w+?\.\w+? ",
                                          full_desc).group(0)
                        full_desc = full_desc.replace(match,
                                                      match.rstrip() + '. ')
                    if (re.search(r"took the reverse handoff from #[0-9]+",
                                  full_desc) is not None):
                        match = re.search(
                            r"took the reverse handoff from #[0-9]+ \S+ ",
                            full_desc).group(0)
                        full_desc = full_desc.replace(match,
                                                      match.rstrip() + '. ')

                    sentences = full_desc.split('. ')
                    flag = 0
                    for i in range(len(sentences)):

                        if ('as eligible (Shotgun) ' in sentences[i]):
                            sentences[i] = re.sub(r"^.+ \(Shotgun\) ", "",
                                                  sentences[i]).strip()

                        if (re.search(r' eligible \S+\.\S+ ', sentences[i])
                                is not None):
                            sentences[i] = re.sub(r"^.+ eligible ", "",
                                                  sentences[i]).strip()

                        if ' as eligible' in sentences[i]:
                            continue

                        if 'was injured during the play' in sentences[i]:
                            continue
                        if 'lines up at ' in sentences[i]:
                            continue

                        if (re.search(r' at QB$', sentences[i]) is not None):
                            continue

                        if ' in at QB' in sentences[i]:
                            sentences[i] = re.sub(r"^.+ in at QB", "",
                                                  sentences[i]).strip()

                        if ' report as eligible' in sentences[i]:
                            sentences[i] = re.sub(r"^.+ report as eligible",
                                                  "", sentences[i]).strip()

                        if ('at QB' in sentences[i]) and ('at WR'
                                                          in sentences[i]):
                            # QB and WR switched positions
                            continue

                        desc = sentences[i]
                        desc = re.sub(r"\(.+?\)", "", desc).strip()
                        desc = re.sub(r"\{.+?\}", "", desc).strip()

                        if ((re.search(r'to \w+$', desc) is not None) or
                            (re.search(r'^\w+$', desc) is not None)) and (
                                i < len(sentences) - 1) and ('respotted to'
                                                             not in desc):
                            desc = desc + '.' + re.sub(
                                r"\(.+?\)", "", sentences[i + 1]).strip()

                        if ((i < len(sentences) - 1)
                                and (sentences[i + 1][:3] == 'to ')):
                            desc = desc + '.' + re.sub(
                                r"\(.+?\)", "", sentences[i + 1]).strip()

                        if ' at QB' in desc:
                            desc = ''
                            continue
                        if ' eligible' in desc:
                            desc = ''
                            continue

                        if 'Injury update: ' in desc:
                            desc = ''
                            continue
                        if desc.startswith('Reverse') == True:
                            desc = ''
                            continue
                        if desc.startswith('Direction change') == True:
                            desc = ''
                            continue
                        if desc.startswith('Direction Change') == True:
                            desc = ''
                            continue

                        # if (re.search(r'^\S+\.\S+ ', desc) is not None):
                        # if((' pass ' ) in desc) and ((
                        if ' pass ' in desc:
                            if (' short ' in desc) or (' deep' in desc):
                                if (' left' in desc) or (' right'
                                                         in desc) or (' middle'
                                                                      in desc):
                                    if (' incomplete '
                                            in desc) or (' for ' in desc) or (
                                                ' INTERCEPTED ' in desc):
                                        break

                        else:
                            if (' up the middle'
                                    in desc) or (' left' in desc) or (' right'
                                                                      in desc):
                                if (' for ' in desc):
                                    break
                        desc = ''

                    if desc == '':
                        continue

                    if 'incomplete' in desc:
                        features['pass'] = 1
                        rematch = re.search(r'incomplete \S+ \S+ to ', desc)

                        if rematch is None:
                            # ball just thrown away, no intended target -> ignore
                            continue

                        match = rematch.group(0).split()
                        features['passlen'] = match[1]
                        features['side'] = match[2]
                    else:
                        if 'no gain' in desc:
                            yards = 0
                        else:
                            if (play.note != 'INT') and ('INTERCEPTED'
                                                         not in desc):
                                rematch = re.search(r'[-]?[0-9]+ yard\s?',
                                                    desc)
                                if rematch is None:
                                    print desc
                                    print play.desc
                                match = rematch.group(0)
                                yards = float(match[:match.find(' ')])

                        if ' pass ' in desc:
                            features['pass'] = 1
                            match = re.search(r'pass \S+ \S+',
                                              desc).group(0).split()
                            if match[1] == 'to':
                                continue
                            features['passlen'] = match[1]
                            features['side'] = match[2]
                        else:
                            features['pass'] = 0
                            if 'up the middle' in desc:
                                features['side'] = 'middle'
                            else:
                                rematch = re.search(
                                    r'^\S+ (scrambles )?\S+ \S+', desc)
                                if rematch is None:
                                    print desc
                                    print play.desc
                                offset = 0
                                match = rematch.group(0).split()
                                if match[1] == 'scrambles':
                                    features['qbrun'] = 1
                                    offset = 1

                                if match[2 + offset] == "guard":
                                    features['side'] = 'middle'
                                else:
                                    features['side'] = match[1 + offset]

                        if (play.note == 'INT') or ('INTERCEPTED' in desc):
                            success = 0
                        else:
                            if (play.touchdown == True) and (' fumble'
                                                             not in play.desc):
                                success = 1
                                success_cnt += 1
                            elif yards >= play.yards_togo:
                                success = 1
                                success_cnt += 1

                            # progress label calculation
                            if yards >= play.yards_togo:
                                # new first down reached
                                progress == 1
                            elif (play.down in [1, 2]) and (yards > 0):
                                progress = (float(yards) /
                                            float(play.yards_togo))**play.down
                            else:
                                # 3rd or 4th down attempt without conversion
                                progress = 0

                    if features['side'] not in ['middle', 'left', 'right']:
                        print play.desc
                        print
                        continue

                    play_features.append(features)
                    success_labels.append(success)
                    yard_labels.append(yards)
                    progress_labels.append(progress)

    print len(play_features)

    data = {}
    data['features'] = np.array(play_features)
    data['success'] = np.array(success_labels)
    data['yards'] = np.array(yard_labels)
    data['progress'] = np.array(progress_labels)
    data['categorical_features'], data[
        'encoder'] = encode_categorical_features(data['features'],
                                                 sparse=False)

    return data
#!python
import nflgame

games = nflgame.games(2013, week=1)
plays = nflgame.combine_plays(games)

#value = getattr(nflgame, "first_name")
# Pull data from nflgame where name is T.Brady - use a query where clause (use of conditions)
# Retrieve architecture - JSON, XML


for p in plays.sort('passing_yds').limit(5):
    print p
Exemple #35
0
def run_stats_import(week_number, year):
    def left(s, amount):
        return s[:amount]

    def right(s, amount):
        return s[-amount:]

    def mid(s, offset, amount):
        return s[offset:offset + amount]

    #season = [2]
    #season = range(1,3)

    #year = 2016
    year_string = str(year) + right(str(year + 1), 2)

    #print("Week, Owner, Punter, Team, Punts, Punt Yards, Blocks, Touchbacks, Fair Catches, Out-of_Bounds, 50+, 60+, 70+, Under 20, Under 10, Under 5, 1 Yd Line, Returns, Return Yards")
    open_file = 'data/season/season_{year}.csv'.format(year=year_string)
    with open(open_file, "wb") as data_csv:
        outputWriter = csv.writer(data_csv, delimiter=',')
        header_row = list()
        header_row.append("Week")
        header_row.append("Owner")
        header_row.append("Punter")
        header_row.append("Team")
        header_row.append("Punts")
        header_row.append("Punt Yards")
        header_row.append("Blocks")
        header_row.append("Touchbacks")
        header_row.append("Fair Catches")
        header_row.append("Out-of_Bounds")
        header_row.append("50+")
        header_row.append("60+")
        header_row.append("70+")
        header_row.append("Under 20")
        header_row.append("Under 10")
        header_row.append("Under 5")
        header_row.append("1 Yd Line")
        header_row.append("Returns")
        header_row.append("Return Yards")
        header_row.append("Holds")
        header_row.append("Misses")
        outputWriter.writerow(header_row)

        for week in week_number:
            print "Week: " + str(week)
            try:
                file = "roster/{year_number}/week{week_number}.txt".format(
                    week_number=week, year_number=year)
                #print file
                f = open(file, "r")
                owner_set = eval(f.read())
                print owner_set
                owner_set = dict((v, k) for k, v in owner_set.iteritems())

            except:
                owner_set = dict()
                print owner_set

            games = nflgame.games(year, week=week)
            print year
            stats = nflgame.combine_max_stats(games)
            plays = nflgame.combine_plays(games)

            #print games

            for player in stats.punting():
                #print week

                punt_name = player
                team = player.team
                punt_yards = player.punting_yds
                punt_blocks = player.punting_blk
                punt_under_20s = player.punting_i20
                punt_count = player.punting_tot
                punt_touch_back = player.punting_touchback
                punt_downs = player.puntret_downed
                punts_under_20 = 0
                punts_under_10 = 0
                punts_under_5 = 0
                punts_under_2 = 0
                out_of_bounds = 0
                fair_catch = 0
                punts_over_50 = 0
                punts_over_60 = 0
                punts_over_70 = 0
                returns = 0
                holds = 0
                misses = 0

                plays = nflgame.combine_plays(games)
                #play = ''

                return_list = []

                for play in plays:
                    if str(punt_name) in str(play) and "punts" in str(
                            play) and "No Play" not in str(play):
                        #description = play
                        #print play
                        punts_character = str(play).find("punts")
                        punts_string = str(play)[int(punts_character):]
                        comma_character = punts_string.find(",")
                        yard_line = punts_string[:int(comma_character)]
                        yard_line = right(yard_line, 2)
                        try:
                            yard_line = (re.findall('\d+', yard_line))  #[0]
                            yard_line = int(yard_line[0])
                            touch_back = 0
                        except:
                            yard_line = 0
                            touch_back = 1
                        #print yard_line
                        return_yards = int(play.puntret_yds)
                        return_list.append(return_yards)
                        punt_length = int(play.punting_yds)
                        #print punt_length
                        if yard_line + return_yards < 20 and yard_line + return_yards > 0:
                            punts_under_20 = punts_under_20 + 1
                        if yard_line + return_yards < 10 and yard_line + return_yards > 0:
                            punts_under_10 = punts_under_10 + 1
                        if yard_line + return_yards < 5 and yard_line + return_yards > 0:
                            punts_under_5 = punts_under_5 + 1
                        if yard_line + return_yards < 2 and yard_line + return_yards > 0:
                            punts_under_2 = punts_under_2 + 1
                        if "out of bounds" in str(punts_string):
                            out_of_bounds = out_of_bounds + 1
                        if "fair catch" in str(punts_string):
                            fair_catch = fair_catch + 1
                        if punt_length >= 50:
                            punts_over_50 = punts_over_50 + 1
                        if punt_length >= 60:
                            punts_over_60 = punts_over_60 + 1
                        if punt_length >= 70:
                            punts_over_70 = punts_over_70 + 1
                        if "out of bounds" not in str(punts_string) and "fair catch" not in str(punts_string) \
                            and touch_back == 0 and "downed" not in str(play):
                            returns = returns + 1

                    elif str(punt_name) in str(play) and (
                            "holder" in str(play) or "Holder"
                            in str(play)) and "No Play" not in str(play):
                        #print play

                        if "is GOOD" in str(play):
                            holds = holds + 1

                        elif "is No Good" in str(play):
                            #print play
                            misses = misses + 1

                    else:
                        continue

                #print str(punt_name) + " Holds: " + str(holds)

                #print return_list
                total_return_yards = sum(return_list)

                owner = owner_set.get(str(player), "Free Agent")
                #print owner

                time.sleep(1)

                # print(str(week) +", " + str(owner) +", "   + str(punt_name) +", " +  \
                #      str(team) + ", " + str(punt_count) +", " + str(punt_yards) +", " + \
                #      str(punt_blocks) +", " + str(punt_touch_back) +", " + str(fair_catch) +", " + \
                #      str(out_of_bounds) +", " + str(punts_over_50) +", " + str(punts_over_60) +", " + \
                #      str(punts_over_70) +", " + str(punt_under_20s) +", " + str(punts_under_10) +", " + \
                #      str(punts_under_5) +", " + str(punts_under_2) +", " + str(returns) +", " + \
                #      str(total_return_yards))

                csv_data = list()
                #Convert date to string
                csv_data.append(week)
                csv_data.append(str(owner))
                csv_data.append(str(punt_name))
                csv_data.append(str(team))
                csv_data.append(punt_count)
                csv_data.append(punt_yards)
                csv_data.append(punt_blocks)
                csv_data.append(punt_touch_back)
                csv_data.append(fair_catch)
                csv_data.append(out_of_bounds)
                csv_data.append(punts_over_50)
                csv_data.append(punts_over_60)
                csv_data.append(punts_over_70)
                csv_data.append(punt_under_20s)
                csv_data.append(punts_under_10)
                csv_data.append(punts_under_5)
                csv_data.append(punts_under_2)
                csv_data.append(returns)
                csv_data.append(total_return_yards)
                csv_data.append(holds)
                csv_data.append(misses)

                outputWriter.writerow(csv_data)

    data_csv.close
Exemple #36
0
def plays_in_game(year, week, team):
    games = nflgame.games(year, week, home=team, away=team)
    plays = nflgame.combine_plays(games)
    for p in plays:
        print(p)
Exemple #37
0
def extract_features(start_year, end_year):
    play_features = []
    success_labels = []
    yard_labels = []
    progress_labels = []
    success_cnt = 0

    for year in range(start_year, end_year + 1):
        # split into individual weeks in order to avoid having to load
        # large chunks of data at once
        for week in range(1, 18):
            games = nflgame.games(year, week=week)

            for play in nflgame.combine_plays(games):
                features = defaultdict(float)
                success = 0
                yards = 0
                progress = 0
                pv2 = 0
                desc = ''

                # TODO: include sacks? probably not since we can't assign them to any play option
                # TODO: Additonally maybe even booth review, official timeout?
                # TODO: Fumble plays should count as if Fumble didn't happen?
                # TODO: plays with declined penalties should be counted ((4:52) A.Foster right tackle to HOU 43 for 13 yards (J.Cyprien). Penalty on JAC-S.Marks, Defensive Offside, declined.)
                # TODO: plays with accepted penalties that do not nullify the play should be counted (keyword: No Play)
                # TODO: error with group when using 2013
                # TODO: Should we count Def. Pass Interference? Def. Holding?

                if (play.note == None or play.note == 'TD' or play.note =='INT') \
                    and (' punt' not in play.desc) \
                    and ('END ' != play.desc[:4]) \
                    and ('End ' != play.desc[:4]) \
                    and ('Two-Minute Warning' not in play.desc) \
                    and ('spiked the ball to stop the clock' not in play.desc) \
                    and ('kneels to ' not in play.desc) \
                    and ('Delay of Game' not in play.desc)\
                    and (play.time is not None)\
                    and ('Penalty on' not in play.desc)\
                    and ('Delay of Game' not in play.desc)\
                    and ('sacked at' not in play.desc)\
                    and ('Punt formation' not in play.desc)\
                    and ('Direct snap to' not in play.desc)\
                    and ('Aborted' not in play.desc)\
                    and ('temporary suspension of play' not in play.desc)\
                    and ('TWO-POINT CONVERSION ATTEMPT' not in play.desc)\
                    and ('warned for substitution infraction' not in play.desc)\
                    and ('no play run - clock started' not in play.desc)\
                    and ('challenged the first down ruling' not in play.desc)\
                    and ('*** play under review ***' not in play.desc)\
                    and ('Direct Snap' not in play.desc)\
                    and ('Direct snap' not in play.desc): 

                    features['team'] = play.team
                    if play.drive.game.away == play.team:
                        features['opponent'] = play.drive.game.home
                    else:
                        features['opponent'] = play.drive.game.away
                    timeclock = play.time.clock.split(':')
                    
                    features['time'] = float(timeclock[0])*60 + float(timeclock[1])
                    if (play.time.qtr == 1) or (play.time.qtr == 3):
                        features['time'] += 15*60                        
                 
                    if play.time.qtr <= 2:
                        features['half'] = 1
                    else:
                        features['half'] = 2
                    
                    features['position'] = 50-play.yardline.offset
                    features['down'] = play.down
                    features['togo'] = play.yards_togo

                    if 'Shotgun' in play.desc:
                        features['shotgun'] = 1

                    full_desc = play.desc
                    full_desc = full_desc.replace('No. ','No.')
                    while (re.search(r" [A-Z]\. ", full_desc) is not None):
                        match = re.search(r" [A-Z]\. ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip())
                    if(re.search(r"[^\.] \(Shotgun\)", full_desc) is not None):
                        full_desc = full_desc.replace(" (Shotgun)",". (Shotgun)") 
                    full_desc = full_desc.replace('.(Shotgun)','. (Shotgun)')
                        
                    if(re.search(r" a[st] QB for the \w+ ", full_desc) is not None):
                        match = re.search(r" a[st] QB for the \w+ ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip() + '. ')
                    
                    if(re.search(r"New QB.{0,20}[0-9]+ \w+?\.w+? ", full_desc) is not None):
                        match = re.search(r"New QB.{0,20}[0-9]+ \w+?\.w+? ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip() + '. ')
                     
                    if(re.search(r"New QB.{0,20}[0-9]+ \w+?[\.\, ] ?\w+? ", full_desc) is not None):
                        match = re.search(r"New QB.{0,20}[0-9]+ \w+?[\.\, ] ?\w+? ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip() + '. ')
                        
                    if(re.search(r"\#[0-9]+ Eligible ", full_desc) is not None):
                        match = re.search(r"\#[0-9]+ Eligible ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip() + '. ')
                        
                    full_desc = full_desc.replace('New QB for Denver - No.6 - Brock Osweiler ','New QB for Denver - No.6 - B.Osweiler. ')
                    
                    full_desc = full_desc.replace(' at QB ',' at QB. ')
                    full_desc = full_desc.replace(' at qb ',' at QB. ')
                    full_desc = full_desc.replace(' at Qb ',' at QB. ')
                    full_desc = full_desc.replace(' in as QB for this play ',' in as QB for this play. ')
                    full_desc = full_desc.replace(' in as QB ',' in as QB. ')
                    full_desc = full_desc.replace(' in as quarterback ',' in as QB. ')
                    full_desc = full_desc.replace(' in at Quarterback ',' in as QB. ')
                    full_desc = full_desc.replace(' is now playing ',' is now playing. ')
                    full_desc = full_desc.replace(' Seminole Formation ',' ')
                    full_desc = full_desc.replace(' St. ',' St.')
                    full_desc = full_desc.replace(' A.Randle El ',' A.Randle ')
                    full_desc = full_desc.replace('Alex Smith ','A.Smith ')
                    
                    if(re.search(r"New QB \#[0-9]+ \w+?\.\w+? ", full_desc) is not None):
                        match = re.search(r"New QB \#[0-9]+ \w+?\.\w+? ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip() + '. ')
                    if(re.search(r"took the reverse handoff from #[0-9]+", full_desc) is not None):
                        match = re.search(r"took the reverse handoff from #[0-9]+ \S+ ", full_desc).group(0)
                        full_desc = full_desc.replace(match,match.rstrip() + '. ')
                        
                    sentences = full_desc.split('. ')
                    flag = 0
                    for i in range(len(sentences)):                       
                        
                        if ('as eligible (Shotgun) ' in sentences[i]):
                            sentences[i] = re.sub(r"^.+ \(Shotgun\) ", "", sentences[i]).strip()
  
                        if (re.search(r' eligible \S+\.\S+ ', sentences[i]) is not None):
                            sentences[i] = re.sub(r"^.+ eligible ", "", sentences[i]).strip()
                            
                        if ' as eligible' in sentences[i]:
                            continue       
                            
                        if 'was injured during the play' in sentences[i]:
                            continue
                        if 'lines up at ' in sentences[i]:
                            continue
                            

                        if (re.search(r' at QB$', sentences[i]) is not None):
                            continue

                        if ' in at QB' in sentences[i]:
                            sentences[i] = re.sub(r"^.+ in at QB", "", sentences[i]).strip()
                            
                        if ' report as eligible' in sentences[i]:
                            sentences[i] = re.sub(r"^.+ report as eligible", "", sentences[i]).strip()
                            
                        if ('at QB' in sentences[i]) and ('at WR' in sentences[i]):
                            #QB and WR switched positions
                            continue

                        desc = sentences[i]
                        desc = re.sub(r"\(.+?\)", "", desc).strip()
                        desc = re.sub(r"\{.+?\}", "", desc).strip()
                        
                        if ((re.search(r'to \w+$', desc) is not None) or (re.search(r'^\w+$', desc) is not None)) and (i<len(sentences)-1) and ('respotted to' not in desc):
                            desc = desc + '.' + re.sub(r"\(.+?\)", "", sentences[i+1]).strip()
                            

                        if ((i<len(sentences)-1) and (sentences[i+1][:3] == 'to ')):
                            desc = desc + '.' + re.sub(r"\(.+?\)", "", sentences[i+1]).strip()
                            
                        if ' at QB' in desc:
                            desc = ''
                            continue
                        if ' eligible' in desc:
                            desc = ''
                            continue
                            
                        if 'Injury update: ' in desc:
                            desc = ''
                            continue
                        if desc.startswith('Reverse') == True:
                            desc = ''
                            continue
                        if desc.startswith('Direction change') == True:
                            desc = ''
                            continue
                        if desc.startswith('Direction Change') == True:
                            desc = ''
                            continue
                            

                        #if (re.search(r'^\S+\.\S+ ', desc) is not None):
                        #if((' pass ' ) in desc) and ((
                        if ' pass ' in desc:
                            if (' short ' in desc) or (' deep' in desc):
                                if (' left' in desc) or (' right' in desc) or (' middle' in desc):
                                    if (' incomplete ' in desc) or (' for ' in desc) or (' INTERCEPTED ' in desc):
                                        break
                            
                        else:
                            if (' up the middle' in desc) or (' left' in desc) or (' right' in desc):
                                if (' for ' in desc):
                                    break
                        #print desc
                        #print full_desc
                        #print
                        desc = ''
                    
                    if desc == '':
                        continue
                    
                    if 'incomplete' in desc:
                        features['pass'] = 1
                        rematch = re.search(r'incomplete \S+ \S+ to ', desc)

                        if rematch is None:
                            # ball just thrown away, no intended target -> ignore
                            continue;

                        match = rematch.group(0).split()
                        features['passlen'] = match[1]
                        features['side'] = match[2]
                    else:
                        if 'no gain' in desc:
                            yards = 0
                        else:
                            if (play.note!='INT') and ('INTERCEPTED' not in desc):
                                rematch = re.search(r'[-]?[0-9]+ yard\s?', desc)
                                if rematch is None:
                                    print desc
                                    print play.desc
                                match = rematch.group(0)
                                yards = float(match[:match.find(' ')])

                        if ' pass ' in desc:
                            features['pass'] = 1
                            match = re.search(r'pass \S+ \S+', desc).group(0).split()
                            if match[1] == 'to':
                                continue
                            features['passlen'] = match[1]
                            features['side'] = match[2]
                        else:
                            features['pass'] = 0
                            if 'up the middle' in desc:
                                features['side'] = 'middle'
                            else:               
                                rematch = re.search(r'^\S+ (scrambles )?\S+ \S+', desc) 
                                if rematch is None:
                                    print desc
                                    print play.desc
                                offset = 0
                                match = rematch.group(0).split()
                                if match[1] == 'scrambles':
                                    features['qbrun'] = 1
                                    offset = 1

                                if match[2+offset] == "guard":
                                    features['side'] = 'middle'
                                else:
                                    features['side'] = match[1+offset]

                        if (play.note=='INT') or ('INTERCEPTED' in desc) :
                            success = 0
                        else:
                            if (play.touchdown == True) and (' fumble' not in play.desc):
                                success = 1
                                success_cnt += 1
                            elif yards >= play.yards_togo:
                                success = 1
                                success_cnt += 1
                            
                            # progress label calculation
                            if yards >= play.yards_togo:
                                # new first down reached
                                progress == 1
                            elif (play.down in [1, 2]) and (yards > 0):                                
                                progress = (float(yards) / float(play.yards_togo))**play.down
                            else:
                                # 3rd or 4th down attempt without conversion
                                progress = 0
                            
                                 
                                
                    if features['side'] not in ['middle','left','right']:                        
                        print play.desc
                        print
                        continue

                    play_features.append(features)
                    success_labels.append(success)
                    yard_labels.append(yards)
                    progress_labels.append(progress)
                    
                    

                # Debug information
                """
                import random
                if random.randint(0,1000) < 2:
                    print desc
                    print play.desc
                    if len(features) == 0:
                        print '>>> IGNORED PLAY <<<'                     
                    else:
                        print features
                    print 'SUCCESS:',success,'| YARDS:',yards
                    print "############################################################"
                """



    print len(play_features)

    return np.array(play_features), np.array(success_labels), np.array(yard_labels), np.array(progress_labels)
Exemple #38
0
def getFeatures(start, end):

    all_features = []
    isPass = []

    for year in range(start, end + 1):
        for week in range(1, 18):
            games = nflgame.games(year=year,
                                  week=week)  #Get games by year/week

            for play in nflgame.combine_plays(
                    games):  #Iterate through all plays

                features = defaultdict(float)

                # TODO: Change the conditions, spikes might be interesting to add to play prediction

                if (play.note == None or play.note == 'TD' or play.note == 'INT') \
                        and (' punt' not in play.desc) \
                        and ('END ' != play.desc[:4]) \
                        and ('End ' != play.desc[:4]) \
                        and ('Two-Minute Warning' not in play.desc) \
                        and ('spiked the ball to stop the clock' not in play.desc) \
                        and ('kneels to ' not in play.desc) \
                        and ('Delay of Game' not in play.desc) \
                        and (play.time is not None) \
                        and ('Penalty on' not in play.desc) \
                        and ('Delay of Game' not in play.desc) \
                        and ('sacked at' not in play.desc) \
                        and ('Punt formation' not in play.desc) \
                        and ('Direct snap to' not in play.desc) \
                        and ('Aborted' not in play.desc) \
                        and ('temporary suspension of play' not in play.desc) \
                        and ('TWO-POINT CONVERSION ATTEMPT' not in play.desc) \
                        and ('warned for substitution infraction' not in play.desc) \
                        and ('no play run - clock started' not in play.desc) \
                        and ('challenged the first down ruling' not in play.desc) \
                        and ('*** play under review ***' not in play.desc) \
                        and ('Direct Snap' not in play.desc) \
                        and ('Direct snap' not in play.desc):

                    # Features: team, opponent, time of play,

                    features['team'] = play.team
                    features['isHome'] = (play.home)
                    if play.drive.game.away == play.team:
                        features['opponent'] = play.drive.game.home
                    else:
                        features['opponent'] = play.drive.game.away

                    #Time left in quarter
                    timeclock = play.time.clock.split(':')
                    features['time'] = float(timeclock[0]) * 60 + float(
                        timeclock[1])

                    features['position'] = 50 - play.yardline.offset
                    features['down'] = play.down
                    features['togo'] = play.yards_togo
                    features['quarter'] = play.time.qtr

                    if 'Shotgun' in play.desc:
                        features['shotgun'] = 1
                    else:
                        features['shotgun'] = 0

                    if 'incomplete' in play.desc or ' pass ' in play.desc:
                        isPass.append(1)
                    else:
                        isPass.append(0)

                    all_features.append(features)

    return all_features, isPass
Exemple #39
0
def extract_features(start_year, end_year):
    play_features = []
    success_labels = []
    yard_labels = []
    progress_labels = []
    success_cnt = 0

    for year in range(start_year, end_year + 1):
        # split into individual weeks in order to avoid having to load
        # large chunks of data at once
        for week in range(1, 18):
            games = nflgame.games(year, week=week)

            for play in nflgame.combine_plays(games):
                features = defaultdict(float)
                success = 0
                yards = 0
                desc = ''

                # TODO: include sacks? probably not since we can't assign them to any play option
                # TODO: time as time left in half?
                # TODO: Additonally maybe even booth review, official timeout?
                # TODO: Fumble plays should count as if Fumble didn't happen?
                # TODO: plays with declined penalties should be counted ((4:52) A.Foster right tackle to HOU 43 for 13 yards (J.Cyprien). Penalty on JAC-S.Marks, Defensive Offside, declined.)
                # TODO: plays with accepted penalties that do not nullify the play should be counted (keyword: No Play)
                # TODO: error with group when using 2013
                # TODO: Should we count Def. Pass Interference? Def. Holding?

                if (play.note == None or play.note == 'TD' or play.note =='INT') \
                    and (' punt' not in play.desc) \
                    and ('END ' != play.desc[:4]) \
                    and ('End ' != play.desc[:4]) \
                    and ('Two-Minute Warning' not in play.desc) \
                    and ('spiked the ball to stop the clock' not in play.desc) \
                    and ('kneels to ' not in play.desc) \
                    and ('Delay of Game' not in play.desc)\
                    and (play.time is not None)\
                    and ('Penalty on' not in play.desc)\
                    and ('Delay of Game' not in play.desc)\
                    and ('sacked at' not in play.desc)\
                    and ('Punt formation' not in play.desc)\
                    and ('Direct snap to' not in play.desc)\
                    and ('Aborted' not in play.desc):

                    features['team'] = play.team
                    if play.drive.game.away == play.team:
                        features['opponent'] = play.drive.game.home
                    else:
                        features['opponent'] = play.drive.game.away
                    timeclock = play.time.clock.split(':')
                    features['time'] = float(timeclock[0]) * 60 + float(
                        timeclock[1])
                    features['quarter'] = play.time.qtr
                    features['position'] = 50 - play.yardline.offset
                    features['down'] = play.down
                    features['togo'] = play.yards_togo

                    if 'Shotgun' in play.desc:
                        features['shotgun'] = 1

                    sentences = play.desc.split('. ')
                    for i in range(len(sentences)):
                        if 'reported in as eligible' in sentences[i]:
                            continue

                        if (re.search(r'in at QB$', desc) is not None):
                            continue

                        if ' in at QB' in sentences[i]:
                            sentences[i] = re.sub(r"^.+ in at QB", "",
                                                  sentences[i]).strip()

                        desc = sentences[i]
                        desc = re.sub(r"\(.+?\)", "", desc).strip()

                        if ((re.search(r'to \S+$', desc) is not None) or
                            (re.search(r'^\S+$', desc)
                             is not None)) and (i < len(sentences) - 1):
                            desc = desc + '.' + re.sub(
                                r"\(.+?\)", "", sentences[i + 1]).strip()

                        if ((i < len(sentences) - 1)
                                and (sentences[i + 1][:3] == 'to ')):
                            desc = desc + '.' + re.sub(
                                r"\(.+?\)", "", sentences[i + 1]).strip()

                        if (re.search(r'^\S+\.\S+ ', desc) is not None):
                            break

                    if 'incomplete' in desc:
                        features['pass'] = 1
                        rematch = re.search(r'incomplete \S+ \S+ to ', desc)

                        if rematch is None:
                            # ball just thrown away, no intended target -> ignore
                            continue

                        match = rematch.group(0).split()
                        features['passlen'] = match[1]
                        features['side'] = match[2]
                    else:
                        if 'no gain' in desc:
                            yards = 0
                        else:
                            if (play.note != 'INT') and ('INTERCEPTED'
                                                         not in desc):
                                rematch = re.search(r'[-]?[0-9]+ yard\s?',
                                                    desc)
                                match = rematch.group(0)
                                yards = float(match[:match.find(' ')])

                        if ' pass ' in desc:
                            features['pass'] = 1
                            match = re.search(r'pass \S+ \S+',
                                              desc).group(0).split()
                            if match[1] == 'to':
                                continue
                            features['passlen'] = match[1]
                            features['side'] = match[2]
                        else:
                            features['pass'] = 0
                            if 'up the middle' in desc:
                                features['side'] = 'middle'
                            else:
                                rematch = re.search(
                                    r'^\S+ (scrambles )?\S+ \S+', desc)
                                if rematch is None:
                                    print desc
                                    print play.desc
                                offset = 0
                                match = rematch.group(0).split()
                                if match[1] == 'scrambles':
                                    features['qbrun'] = 1
                                    offset = 1

                                features['side'] = match[
                                    1 + offset] + ' ' + match[2 + offset]

                        if (play.note == 'INT') or ('INTERCEPTED' in desc):
                            success = 0
                        else:
                            if (play.touchdown == True) and (' fumble'
                                                             not in play.desc):
                                success = 1
                                success_cnt += 1
                            elif yards >= play.yards_togo:
                                success = 1
                                success_cnt += 1

                            # progress label calculation
                            if yards < play.yards_togo:
                                if play.down > 2:
                                    progress = 0
                                elif play.down == 2:
                                    progress = float(yards) / float(
                                        play.yards_togo)
                                else:  # 1st down - two attempts left
                                    progress = float(yards) * 2.0 / float(
                                        play.yards_togo)
                            else:
                                progress = 1 + float(yards -
                                                     play.yards_togo) / 10.0

                    play_features.append(features)
                    success_labels.append(success)
                    yard_labels.append(yards)
                    progress_labels.append(progress)

                # Debug information
                #if random.randint(0,1000) < 2:
                #    print desc
                #    print p.desc
                #    print features
                #    print 'SUCCESS:',success,'| YARDS:',yards
                #    print "############################################################"
                '''
                # Some debug code (Roman)
                else:
                    if 'Timeout' not in play.desc and \
                                    'kicks' not in play.desc and \
                                    'kneels' not in play.desc and \
                                    'Field Goal' not in play.desc and\
                                    'field goal' not in play.desc and\
                                    'Two-Minute-Warning' not in play.desc and \
                                    'END' not in play.desc and\
                                    'Two-Point' not in play.desc and\
                                    'TWO-POINT' not in play.desc and\
                                    'Two-Minute' not in play.desc and\
                                    'punts' not in play.desc and\
                                    'Punt' not in play.desc and\
                                    'spiked' not in play.desc and\
                                    'extra point' not in play.desc and\
                                    'False Start' not in play.desc and\
                                    'Delay of Game' not in play.desc and\
                                    'No Play' not in play.desc and\
                                    'BLOCKED' not in play.desc and\
                                    'FUMBLES' not in play.desc and\
                                    'sacked' not in play.desc:
                        print play.desc
                '''

    print len(play_features)

    return np.array(play_features), np.array(success_labels), np.array(
        yard_labels), np.array(progress_labels)
 def confirm():
     player1area.clear()
     player2area.clear()
     #Player One stuff
     player1info = []
     points1 = 0
     player1 = str(input1.text()).strip()
     if not nflgame.find(player1, team=None) and player1 != "BUF":
         onenoexist = QListWidgetItem("Player does not exist")
         player1area.addItem(onenoexist)
     else:
         player1info.append(player1)
         def handleActivated():
             int(input12.currentText())
         input12.activated['QString'].connect(handleActivated)
         player1year = int(input12.currentText())
         player1info.append(player1year)
         player1week = str(input13.text()).strip()
         p1w = map(int, player1week.split())
         oneonlyweek = QListWidgetItem("You have not entered any weeks")
         if len(p1w)==0:
             player1area.addItem(oneonlyweek)
         else:
             def currentweek(i):
                 onenotcurrent = QListWidgetItem("Week %d hasn't occurred yet" % p1w[i])
                 if len(p1w) > 0:
                     if p1w[i]>(nflgame.live.current_year_and_week()[1]) and (p1w[i]>0 and p1w[i]<18):
                         if i == len(p1w)-1:
                             player1area.addItem(onenotcurrent)
                             p1w.remove(p1w[i])
                         else:
                             player1area.addItem(onenotcurrent)
                             p1w.remove(p1w[i])
                             if len(p1w)>0:
                                 currentweek(i)
                     else:
                         if i < len(p1w)-1:
                             currentweek(i+1)
             def existweek(i):
                 weeknoexist1 = QListWidgetItem("Week %d does not exist" % p1w[i])
                 if len(p1w) == 0:
                     player1area.addItem(oneonlyweek)
                 else:
                     if p1w[i]<1 or p1w[i]>17:
                         if i == len(p1w)-1:
                             player1area.addItem(weeknoexist1)
                             p1w.remove(p1w[i])
                         else:
                             player1area.addItem(weeknoexist1)
                             p1w.remove(p1w[i])
                             existweek(i)
                     else:
                         if i < len(p1w)-1:
                             existweek(i+1)
             i=0
             existweek(i)
             if len(p1w)>0:
                 currentweek(0)
             
             def byecheck(i):
                 if player1info[0] == "BUF":
                     for r in p1w:
                         if r == 7:
                             onebye = QListWidgetItem("%s has a bye in week %d" % ("BUF", 7))
                             player1area.addItem(onebye)
                             p1w.remove(7)
                 else:
                 
                     for w in byeWeekLists.byeWeeks:
                         for t in w:
                             if nflgame.find(player1info[0], team=None)[0].team == t:
                                 if w[-1] in p1w:
                                     onebye = QListWidgetItem("%s has a bye in week %d" % (t, w[-1]))
                                     player1area.addItem(onebye)
                                     p1w[:] = (value for value in p1w if value != w[-1])
                                     print p1w
             if len(p1w)>0:
                 byecheck(0)
             if len(p1w)==0:
                 onenomoreweeks = QListWidgetItem("You have not entered any more weeks")
                 player1area.addItem(onenomoreweeks)
             if len(p1w) > 0:
                 p1wset = list(set(p1w))
                 player1info.append(p1wset)
                 print "ok"
                 print p1wset
                 p1g = str(ginput1.currentText())
                 player1info.append(p1g)
                 if player1info[0] == "BUF":
                     points1 = fpDefense.fpDefense(player1info[0],player1info[1],player1info[2])
                     item1 = QListWidgetItem("%f" % float(points1))
                     player1area.addItem(item1)
                 else:
                     points1 = fbPlayerPoints.playerPoints(player1info)
                     item1 = QListWidgetItem("%f" % float(points1))
                     if points1 == float('inf'):
                         onenoexist = QListWidgetItem("Player does not exist")
                         player1area.addItem(onenoexist)
                     else:
                         player1found = nflgame.find(player1info[0], team = None)
                         player1area.addItem(item1)
                         p1 = player1found[0]
                         m1 = p1.stats(player1info[1],player1info[2],player1info[3])
                         if 'kicking_xpmade' in m1.stats and 'kicking_xpmissed' in m1.stats:
                             p1xpmade = m1.stats['kicking_xpmade']
                             p1xpm = m1.stats['kicking_xpmissed']
                             p1xpmadein = QListWidgetItem("Extra points made: %d/%d" % (int(p1xpmade),int(p1xpmade + p1xpm)))
                             player1area.addItem(p1xpmadein)
                         if 'kicking_fgm' in m1.stats and 'kicking_fga' in m1.stats :
                             p1fgmade = m1.stats['kicking_fgm']
                             p1fga = m1.stats['kicking_fga']
                             p1fgmadein = QListWidgetItem("Field goals made: %d/%d" % (int(p1fgmade),int(p1fga)))
                             player1area.addItem(p1fgmadein)
                         
                         if p1.position == "K":
                             for w in player1info[2]:
                                 games = nflgame.games(player1info[1], w)
                                 plays = nflgame.combine_plays(games)
                                 allMadeFGs = plays.filter(kicking_fgm = True)
                                 player1area.addItem("Field goals made in week " + str(w) + ":")
                                 for p in allMadeFGs:
                                     if p.players.playerid(p1.playerid):
                                         yards = p.kicking_fgm_yds
                                         player1area.addItem(str(yards) + " ")
                         if 'passing_yds' in m1.stats:
                             p1passyds = m1.stats['passing_yds']
                             p1passydsin = QListWidgetItem("Passing yards: %d" % int(p1passyds))
                             player1area.addItem(p1passydsin)
                         if 'passing_twoptm' in m1.stats:
                             p1tpm = m1.stats['passing_twoptm']
                             p1tpmin = QListWidgetItem("Two-pt conversions made: %d" % int(p1tpm))
                             player1area.addItem(p1tpmin)
                         if 'passing_ints' in m1.stats:
                             p1passints = m1.stats['passing_ints']
                             p1passintsin = QListWidgetItem("Passing ints: %d" % int(p1passints))
                             player1area.addItem(p1passintsin)
                         if 'passing_tds' in m1.stats:
                             p1passtds = m1.stats['passing_tds']
                             p1passtdsin = QListWidgetItem("Passing TDs: %d" % int(p1passtds))
                             player1area.addItem(p1passtdsin)
                         if 'rushing_tds' in m1.stats:
                             p1rushtds = m1.stats['rushing_tds']
                             p1rushtdsin = QListWidgetItem("Rushing TDs: %d" % int(p1rushtds))
                             player1area.addItem(p1rushtdsin)
                         if 'rushing_yds' in m1.stats:
                             p1rushyds = m1.stats['rushing_yds']
                             p1rushydsin = QListWidgetItem("Rushing yards: %d" % int(p1rushyds))
                             player1area.addItem(p1rushydsin)
                         if 'receiving_yds' in m1.stats:
                             p1recyds = m1.stats['receiving_yds']
                             p1recydsin = QListWidgetItem("Receiving yards: %d" % int(p1recyds))
                             player1area.addItem(p1recydsin)
                         if 'receiving_tds' in m1.stats:
                             p1rectds = m1.stats['receiving_tds']
                             p1rectdsin = QListWidgetItem("Receiving TDs: %d" % int(p1rectds))
                             player1area.addItem(p1rectdsin)
                         if 'receiving_twoptm' in m1.stats:
                             p1rectpm = m1.stats['receiving_twoptm']
                             p1rectpmin = QListWidgetItem("Receiving two-pt conversions made: %d" % int(p1rectpm))
                             player1area.addItem(p1rectpmin)
                         if 'fumbles_lost' in m1.stats:
                             p1fum = m1.stats['fumbles_lost']
                             p1fumin = QListWidgetItem("Fumbles lost: %d" % int(p1fum))
                             player1area.addItem(p1fumin)
                         if 'rushing_twoptm' in m1.stats:
                             p1rushtpm = m1.stats['rushing_twoptm']
                             p1rushtpmin = QListWidgetItem("Rushing two-pt conversions made: %d" % int(p1rushtpm))
                             player1area.addItem(p1rushtpmin)
             #Player Two stuff
     player2info = []
     points2 = 0
     player2 = str(input2.text()).strip()
     if not nflgame.find(player2, team=None):
         twonoexist = QListWidgetItem("Player does not exist")
         player2area.addItem(twonoexist)
     else:
         player2info.append(player2)
         def handleActivated():
             int(input22.currentText())
         input22.activated['QString'].connect(handleActivated)
         player2year = int(input22.currentText())
         player2info.append(player2year)
         player2week = str(input23.text()).strip()
         p2w = map(int, player2week.split())
         twoonlyweek = QListWidgetItem("You have not entered any weeks")
         if len(p2w)==0:
             player2area.addItem(twoonlyweek)
         else:
             def currentweek(i):
                 twonotcurrent = QListWidgetItem("Week %d hasn't occurred yet" % p2w[i])
                 if len(p2w) > 0:
                     if p2w[i]>(nflgame.live.current_year_and_week()[1]) and (p2w[i]>0 and p2w[i]<18):
                         if i == len(p2w)-1:
                             player2area.addItem(twonotcurrent)
                             p2w.remove(p2w[i])
                         else:
                             player2area.addItem(twonotcurrent)
                             p2w.remove(p2w[i])
                             if len(p2w)>0:
                                 currentweek(i)
                     else:
                         if i < len(p2w)-1:
                             currentweek(i+1)
             def existweek(i):
                 weeknoexist2 = QListWidgetItem("Week %d does not exist" % p2w[i])
                 if len(p2w) == 0:
                     player2area.addItem(twoonlyweek)
                 else:
                     if p2w[i]<1 or p2w[i]>17:
                         if i == len(p2w)-1:
                             player2area.addItem(weeknoexist2)
                             p2w.remove(p2w[i])
                         else:
                             player2area.addItem(weeknoexist2)
                             p2w.remove(p2w[i])
                             existweek(i)
                     else:
                         if i < len(p2w)-1:
                             existweek(i+1)
             i=0
             existweek(i)
             if len(p2w)>0:
                 currentweek(0)
             
             def byecheck(i):
                 for w in byeWeekLists.byeWeeks:
                     for t in w:
                         if nflgame.find(player2info[0], team=None)[0].team == t:
                             if w[-1] in p2w:
                                 twobye = QListWidgetItem("%s has a bye in week %d" % (t, w[-1]))
                                 player2area.addItem(twobye)
                                 p2w.remove(w[-1])
             if len(p2w)>0:
                 byecheck(0)
             if len(p2w)==0:
                 twonomoreweeks = QListWidgetItem("You have not entered any more weeks")
                 player2area.addItem(twonomoreweeks)
             if len(p2w) > 0:
                 p2wset = list(set(p2w))
                 player2info.append(p2wset)
                 points2 = fbPlayerPoints.playerPoints(player2info)
                 item2 = QListWidgetItem("%f" % float(points2))
                 if points2 == float('inf'):
                     twonoexist = QListWidgetItem("Player does not exist")
                     player2area.addItem(twonoexist)
                 else:
                     player2found = nflgame.find(player2info[0], team = None)
                     player2area.addItem(item2)
                     p2 = player2found[0]
                     m2 = p2.stats(player2info[1],player2info[2])
                     if 'kicking_xpmade' in m2.stats and 'kicking_xpmissed' in m2.stats:
                         p2xpmade = m2.stats['kicking_xpmade']
                         p2xpm = m2.stats['kicking_xpmissed']
                         p2xpmadein = QListWidgetItem("Extra points made: %d/%d" % (int(p2xpmade),int(p2xpmade + p2xpm)))
                         player2area.addItem(p2xpmadein)
                     if 'kicking_fgm' in m2.stats and 'kicking_fga' in m2.stats :
                         p2fgmade = m2.stats['kicking_fgm']
                         p2fga = m2.stats['kicking_fga']
                         p2fgmadein = QListWidgetItem("Field goals made: %d/%d" % (int(p2fgmade),int(p2fga)))
                         player2area.addItem(p2fgmadein)
                     if p2.position == "K":
                         for w in player2info[2]:
                             games = nflgame.games(player2info[1], w)
                             plays = nflgame.combine_plays(games)
                             allMadeFGs = plays.filter(kicking_fgm = True)
                             player2area.addItem("Field goals made in week " + str(w) + ":")
                             for p in allMadeFGs:
                                 if p.players.playerid(p2.playerid):
                                     yards = p.kicking_fgm_yds
                                     player2area.addItem(str(yards) + " ")
                     if 'passing_yds' in m2.stats:
                         p2passyds = m2.stats['passing_yds']
                         p2passydsin = QListWidgetItem("Passing yards: %d" % int(p2passyds))
                         player2area.addItem(p2passydsin)
                     if 'passing_twoptm' in m2.stats:
                         p2tpm = m2.stats['passing_twoptm']
                         p2tpmin = QListWidgetItem("Two-pt conversions made: %d" % int(p2tpm))
                         player2area.addItem(p2tpmin)
                     if 'passing_ints' in m2.stats:
                         p2passints = m2.stats['passing_ints']
                         p2passintsin = QListWidgetItem("Passing ints: %d" % int(p2passints))
                         player2area.addItem(p2passintsin)
                     if 'passing_tds' in m2.stats:
                         p2passtds = m2.stats['passing_tds']
                         p2passtdsin = QListWidgetItem("Passing TDs: %d" % int(p2passtds))
                         player2area.addItem(p2passtdsin)
                     if 'rushing_tds' in m2.stats:
                         p2rushtds = m2.stats['rushing_tds']
                         p2rushtdsin = QListWidgetItem("Rushing TDs: %d" % int(p2rushtds))
                         player2area.addItem(p2rushtdsin)
                     if 'rushing_yds' in m2.stats:
                         p2rushyds = m2.stats['rushing_yds']
                         p2rushydsin = QListWidgetItem("Rushing yards: %d" % int(p2rushyds))
                         player2area.addItem(p2rushydsin)
                     if 'receiving_yds' in m2.stats:
                         p2recyds = m2.stats['receiving_yds']
                         p2recydsin = QListWidgetItem("Receiving yards: %d" % int(p2recyds))
                         player2area.addItem(p2recydsin)
                     if 'receiving_tds' in m2.stats:
                         p2rectds = m2.stats['receiving_tds']
                         p2rectdsin = QListWidgetItem("Receiving TDs: %d" % int(p2rectds))
                         player2area.addItem(p2rectdsin)
                     if 'receiving_twoptm' in m2.stats:
                         p2rectpm = m2.stats['receiving_twoptm']
                         p2rectpmin = QListWidgetItem("Receiving two-pt conversions made: %d" % int(p2rectpm))
                         player2area.addItem(p2rectpmin)
                     if 'fumbles_lost' in m2.stats:
                         p2fum = m2.stats['fumbles_lost']
                         p2fumin = QListWidgetItem("Fumbles lost: %d" % int(p2fum))
                         player2area.addItem(p2fumin)
                     if 'rushing_twoptm' in m2.stats:
                         p2rushtpm = m2.stats['rushing_twoptm']
                         p2rushtpmin = QListWidgetItem("Rushing two-pt conversions made: %d" % int(p2rushtpm))
                         player2area.addItem(p2rushtpmin)
def run_stats_import(week, year):
    def left(s, amount):
        return s[:amount]

    def right(s, amount):
        return s[-amount:]

    def mid(s, offset, amount):
        return s[offset:offset + amount]

    year_string = str(year) + right(str(year + 1), 2)

    #print("Week, Owner, Punter, Team, Punts, Punt Yards, Blocks, Touchbacks, Fair Catches, Out-of_Bounds, 50+, 60+, 70+, Under 20, Under 10, Under 5, 1 Yd Line, Returns, Return Yards")
    open_file = 'data/season/season_{year}.csv'.format(year=year_string)
    with open(open_file, "wb") as data_csv:
        outputWriter = csv.writer(data_csv, delimiter=',')
        header_row = list()
        header_row.append("Week")
        header_row.append("Punter")
        header_row.append("Team")
        header_row.append("Punts")
        header_row.append("Punt Yards")
        header_row.append("Blocks")
        header_row.append("Touchbacks")
        header_row.append("Fair Catches")
        header_row.append("Out-of_Bounds")
        header_row.append("50+")
        header_row.append("60+")
        header_row.append("70+")
        header_row.append("Under 20")
        header_row.append("Under 10")
        header_row.append("Under 5")
        header_row.append("1 Yd Line")
        header_row.append("Returns")
        header_row.append("Return Yards")
        header_row.append("Holds")
        header_row.append("Misses")
        header_row.append("First Downs")
        header_row.append("TD")
        header_row.append("Fumbles")
        header_row.append("Int")
        header_row.append("Conduct")
        outputWriter.writerow(header_row)

        for i in week:
            print("Week " + str(i))
            kind = 'REG' if i <= 17 else 'POST'
            woke = i if i <= 17 else i - 17
            #print kind
            #print woke
            games = nflgame.games(year, week=i)
            #print year
            stats = nflgame.combine_max_stats(games)
            plays = nflgame.combine_plays(games)

            #print games

            for player in stats.punting():
                #print week

                punt_name = player
                team = player.team
                punt_yards = player.punting_yds
                punt_under_20s = player.punting_i20
                punt_blocks = player.punting_blk
                punt_count = player.punting_tot
                punt_touch_back = player.punting_touchback
                punt_downs = player.puntret_downed
                punts_under_20 = 0
                punts_under_10 = 0
                punts_under_5 = 0
                punts_under_2 = 0
                out_of_bounds = 0
                fair_catch = 0
                punts_over_50 = 0
                punts_over_60 = 0
                punts_over_70 = 0
                returns = 0
                holds = 0
                misses = 0
                fumbles = 0
                interceptions = 0
                first_downs = 0
                passing_yds = 0
                passing_tds = 0
                rushing_yds = 0
                rushing_tds = 0
                touchdowns = 0
                conduct = 0

                for passing in stats.passing():
                    if passing == player:
                        passing_yds = passing.passing_yds
                        passing_tds = passing.passing_tds

                    else:
                        passing_yds = 0
                        passing_tds = 0

                    #print passing_yards
                    #print passing_tds

                for rushing in stats.rushing():
                    if rushing == player:
                        rushing_yds = rushing.rushing_yds
                        rushing_tds = rushing.rushing_tds
                    else:
                        rushing_yds = 0
                        rushing_tds = 0

                    #print rushing_yards
                    #print rushing_tds

                plays = nflgame.combine_plays(games)
                #play = ''

                return_list = []

                for play in plays:
                    if str(punt_name) in str(play) and "punts" in str(
                            play) and "No Play" not in str(play):
                        #description = play
                        #print play
                        punts_character = str(play).find("punts")
                        punts_string = str(play)[int(punts_character):]
                        comma_character = punts_string.find(",")
                        yard_line = punts_string[:int(comma_character)]
                        yard_line = right(yard_line, 2)
                        try:
                            yard_line = (re.findall('\d+', yard_line))  #[0]
                            yard_line = int(yard_line[0])
                            touch_back = 0
                        except:
                            yard_line = 0
                            touch_back = 1
                        #print yard_line
                        return_yards = int(play.puntret_yds)
                        return_list.append(return_yards)
                        punt_length = int(play.punting_yds)
                        #print punt_length
                        if yard_line + return_yards < 20 and yard_line + return_yards > 0:
                            punts_under_20 = punts_under_20 + 1
                        if yard_line + return_yards < 10 and yard_line + return_yards > 0:
                            punts_under_10 = punts_under_10 + 1
                        if yard_line + return_yards < 5 and yard_line + return_yards > 0:
                            punts_under_5 = punts_under_5 + 1
                        if yard_line + return_yards < 2 and yard_line + return_yards > 0:
                            punts_under_2 = punts_under_2 + 1
                        if "out of bounds" in str(punts_string):
                            out_of_bounds = out_of_bounds + 1
                        if "fair catch" in str(punts_string):
                            fair_catch = fair_catch + 1
                        if punt_length >= 50:
                            punts_over_50 = punts_over_50 + 1
                        if punt_length >= 60:
                            punts_over_60 = punts_over_60 + 1
                        if punt_length >= 70:
                            punts_over_70 = punts_over_70 + 1
                        if "out of bounds" not in str(punts_string) and "fair catch" not in str(punts_string) \
                            and touch_back == 0 and "downed" not in str(play):
                            returns = returns + 1

                    elif str(punt_name) in str(play) and (
                            "holder" in str(play) or "Holder"
                            in str(play)) and "No Play" not in str(play):
                        #print play

                        if "is GOOD" in str(play):
                            holds = holds + 1

                        elif "is No Good" in str(play):
                            #print play
                            misses = misses + 1

                    elif str(punt_name) in str(
                            play) and "Punt formation" in str(
                                play) and "No Play" not in str(
                                    play) and "Delay of Game" not in str(play):
                        print str(play)
                        first_down_character = str(play).find("and")
                        remaining_string = str(
                            play)[int(first_down_character) + 4:]
                        parentheses_character = remaining_string.find(")")
                        remaining_string = remaining_string[:
                                                            parentheses_character]
                        yards_remaining = remaining_string

                        yards_character = str(play).find("for ")
                        remaining_string = str(play)[int(yards_character) + 4:]
                        yards_character = remaining_string.find("yards")
                        remaining_string = remaining_string[:yards_character]
                        yards_gained = remaining_string

                        #print yards_remaining
                        #print yards_gained

                        if "PENALTY" in str(
                                play) and "Unsportsmanlike Conduct" in str(
                                    play):
                            if "PENALTY on {team}-{punter}".format(
                                    team=team, punter=punt_name) in str(play):
                                conduct = conduct + 1
                        if "INTERCEPTED" in str(play):
                            interceptions = interceptions + 1
                        elif "FUMBLES" in str(play):
                            if "{punter} FUMBLES".format(
                                    punter=punt_name) in str(play):
                                fumbles = fumbles + 1
                        elif ("pass" in str(play)
                              or "right end ran" in str(play)
                              or "left end ran" in str(play) or "up the middle"
                              in str(play)) and "INTERCEPTED" not in str(
                                  play) and "incomplete" not in str(play):
                            if int(yards_gained) >= int(
                                    yards_remaining) and yards_gained > 0:
                                first_downs = first_downs + 1
                            if "TOUCHDOWN" in str(play):
                                touchdowns = touchdowns + 1

                        #print conduct
                        #print interceptions
                        #print fumbles
                        #print first_downs
                        #print touchdowns

                    else:
                        continue

                    total_return_yards = sum(return_list)

                csv_data = list()
                #Convert date to string
                csv_data.append(i)
                csv_data.append(str(punt_name))
                csv_data.append(str(team))
                csv_data.append(punt_count)
                csv_data.append(punt_yards)
                csv_data.append(punt_blocks)
                csv_data.append(punt_touch_back)
                csv_data.append(fair_catch)
                csv_data.append(out_of_bounds)
                csv_data.append(punts_over_50)
                csv_data.append(punts_over_60)
                csv_data.append(punts_over_70)
                csv_data.append(punt_under_20s)
                csv_data.append(punts_under_10)
                csv_data.append(punts_under_5)
                csv_data.append(punts_under_2)
                csv_data.append(returns)
                csv_data.append(total_return_yards)
                csv_data.append(holds)
                csv_data.append(misses)
                csv_data.append(first_downs)
                csv_data.append(touchdowns)
                csv_data.append(fumbles)
                csv_data.append(interceptions)
                csv_data.append(conduct)

                outputWriter.writerow(csv_data)
    #
    data_csv.close
Exemple #42
0
def extract_features(start_year, end_year):
    play_features = []
    success_labels = []
    yard_labels = []
    progress_labels = []
    success_cnt = 0

    for year in range(start_year, end_year + 1):
        # split into individual weeks in order to avoid having to load
        # large chunks of data at once
        for week in range(1, 18):
            games = nflgame.games(year, week=week)

            for play in nflgame.combine_plays(games):
                features = defaultdict(float)
                success = 0
                yards = 0
                desc = ''

                # TODO: include sacks? probably not since we can't assign them to any play option
                # TODO: time as time left in half?
                # TODO: Additonally maybe even booth review, official timeout?
                # TODO: Fumble plays should count as if Fumble didn't happen?
                # TODO: plays with declined penalties should be counted ((4:52) A.Foster right tackle to HOU 43 for 13 yards (J.Cyprien). Penalty on JAC-S.Marks, Defensive Offside, declined.)
                # TODO: plays with accepted penalties that do not nullify the play should be counted (keyword: No Play)
                # TODO: error with group when using 2013
                # TODO: Should we count Def. Pass Interference? Def. Holding?

                if (play.note == None or play.note == 'TD' or play.note =='INT') \
                    and (' punt' not in play.desc) \
                    and ('END ' != play.desc[:4]) \
                    and ('End ' != play.desc[:4]) \
                    and ('Two-Minute Warning' not in play.desc) \
                    and ('spiked the ball to stop the clock' not in play.desc) \
                    and ('kneels to ' not in play.desc) \
                    and ('Delay of Game' not in play.desc)\
                    and (play.time is not None)\
                    and ('Penalty on' not in play.desc)\
                    and ('Delay of Game' not in play.desc)\
                    and ('sacked at' not in play.desc)\
                    and ('Punt formation' not in play.desc)\
                    and ('Direct snap to' not in play.desc)\
                    and ('Aborted' not in play.desc):

                    features['team'] = play.team
                    if play.drive.game.away == play.team:
                        features['opponent'] = play.drive.game.home
                    else:
                        features['opponent'] = play.drive.game.away
                    timeclock = play.time.clock.split(':')
                    features['time'] = float(timeclock[0])*60 + float(timeclock[1])
                    features['quarter'] = play.time.qtr
                    features['position'] = 50-play.yardline.offset
                    features['down'] = play.down
                    features['togo'] = play.yards_togo

                    if 'Shotgun' in play.desc:
                        features['shotgun'] = 1

                    sentences = play.desc.split('. ')
                    for i in range(len(sentences)):
                        if 'reported in as eligible' in sentences[i]:
                            continue

                        if (re.search(r'in at QB$', desc) is not None):
                            continue

                        if ' in at QB' in sentences[i]:
                            sentences[i] = re.sub(r"^.+ in at QB", "", sentences[i]).strip()

                        desc = sentences[i]
                        desc = re.sub(r"\(.+?\)", "", desc).strip()

                        if ((re.search(r'to \S+$', desc) is not None) or (re.search(r'^\S+$', desc) is not None)) and (i<len(sentences)-1):
                            desc = desc + '.' + re.sub(r"\(.+?\)", "", sentences[i+1]).strip()

                        if ((i<len(sentences)-1) and (sentences[i+1][:3] == 'to ')):
                            desc = desc + '.' + re.sub(r"\(.+?\)", "", sentences[i+1]).strip()

                        if (re.search(r'^\S+\.\S+ ', desc) is not None):
                            break


                    if 'incomplete' in desc:
                        features['pass'] = 1
                        rematch = re.search(r'incomplete \S+ \S+ to ', desc)

                        if rematch is None:
                            # ball just thrown away, no intended target -> ignore
                            continue;

                        match = rematch.group(0).split()
                        features['passlen'] = match[1]
                        features['side'] = match[2]
                    else:
                        if 'no gain' in desc:
                            yards = 0
                        else:
                            if (play.note!='INT') and ('INTERCEPTED' not in desc):
                                rematch = re.search(r'[-]?[0-9]+ yard\s?', desc)
                                match = rematch.group(0)
                                yards = float(match[:match.find(' ')])

                        if ' pass ' in desc:
                            features['pass'] = 1
                            match = re.search(r'pass \S+ \S+', desc).group(0).split()
                            if match[1] == 'to':
                                continue
                            features['passlen'] = match[1]
                            features['side'] = match[2]
                        else:
                            features['pass'] = 0
                            if 'up the middle' in desc:
                                features['side'] = 'middle'
                            else:
                                rematch = re.search(r'^\S+ (scrambles )?\S+ \S+', desc)
                                if rematch is None:
                                    print desc
                                    print play.desc
                                offset = 0
                                match = rematch.group(0).split()
                                if match[1] == 'scrambles':
                                    features['qbrun'] = 1
                                    offset = 1

                                features['side'] = match[1+offset] + ' ' + match[2+offset]

                        if (play.note=='INT') or ('INTERCEPTED' in desc) :
                            success = 0
                        else:
                            if (play.touchdown == True) and (' fumble' not in play.desc):
                                success = 1
                                success_cnt += 1
                            elif yards >= play.yards_togo:
                                success = 1
                                success_cnt += 1

                            # progress label calculation
                            if yards < play.yards_togo:
                                if play.down > 2:
                                    progress = 0
                                elif play.down == 2:
                                    progress = float(yards) / float(play.yards_togo)
                                else: # 1st down - two attempts left
                                    progress = float(yards) * 2.0 / float(play.yards_togo)
                            else:
                                progress = 1 + float(yards - play.yards_togo) / 10.0

                    play_features.append(features)
                    success_labels.append(success)
                    yard_labels.append(yards)
                    progress_labels.append(progress)

                # Debug information
                #if random.randint(0,1000) < 2:
                #    print desc
                #    print p.desc
                #    print features
                #    print 'SUCCESS:',success,'| YARDS:',yards
                #    print "############################################################"

                '''
                # Some debug code (Roman)
                else:
                    if 'Timeout' not in play.desc and \
                                    'kicks' not in play.desc and \
                                    'kneels' not in play.desc and \
                                    'Field Goal' not in play.desc and\
                                    'field goal' not in play.desc and\
                                    'Two-Minute-Warning' not in play.desc and \
                                    'END' not in play.desc and\
                                    'Two-Point' not in play.desc and\
                                    'TWO-POINT' not in play.desc and\
                                    'Two-Minute' not in play.desc and\
                                    'punts' not in play.desc and\
                                    'Punt' not in play.desc and\
                                    'spiked' not in play.desc and\
                                    'extra point' not in play.desc and\
                                    'False Start' not in play.desc and\
                                    'Delay of Game' not in play.desc and\
                                    'No Play' not in play.desc and\
                                    'BLOCKED' not in play.desc and\
                                    'FUMBLES' not in play.desc and\
                                    'sacked' not in play.desc:
                        print play.desc
                '''

    print len(play_features)

    return np.array(play_features), np.array(success_labels), np.array(yard_labels), np.array(progress_labels)
import nflgame as ng
import teams
import pprint

teams = teams.teams()

year = 2017

data = [[[], [], [], []] for _ in range(32)]

for week in range(1, 18):
    games = ng.games(year, week)
    plays = ng.combine_plays(games)
    for p in plays:
        tNum = teams.teamNumber(p.team)
        if p.down not in [1, 2, 3, 4]:
            continue
        data = p.data
        #pprint.pprint(data)
        print data
        raw_input()