Code Monkey home page Code Monkey logo

nflgame's Introduction

nflgame's People

Contributors

benschroeder avatar burntsushi avatar cjmcdonn avatar cminton avatar derek-adair avatar front9tech avatar hugovk avatar jclapp23 avatar jthomm avatar leighlondon avatar ochawkeye avatar panzarino avatar sebasttj avatar snake-plissken avatar stchiang avatar weixiyen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nflgame's Issues

Average Points Per Game

How would I be able to figure out the average points per game? I can't figure out how to add up all scores from a team's games for the current season. I tried to contact you on IRC for a couple days and still hadn't heard back.

2013 Games Now Working?

I'm new to Python and I thought using a great API like this would help me learn (easier to come up with ideas to learn to complete). It seems I can pull data fro previous years, but not able to get 2013. I'm doing the following:

import nflgame
game = nflgame.one(2013, 1, "GB", "SF")
print game.players.passing()

I get AttributeError: 'NoneType' object has no attribute 'players' as an error. Like I said, it will work for 2012. Not sure if it's something I'm doing or not.

Newbie foolishness

I am trying to get the stats for week 1 of 2012... getting this error. Never programed python until this test program, am pretty good PHP programmer though.. I know this is something I am doing wrong, I just cannot figure it out.

Ray

all_stats
Traceback (most recent call last):
File "./testnfl.py", line 23, in
nflgame.combine(nflgame.games(2012, 1)).csv('Week1-2012.csv')
File "/usr/lib/python2.7/site-packages/nflgame/seq.py", line 301, in csv
for category, stats in p.all_stats().iteritems():
File "/usr/lib/python2.7/site-packages/nflgame/player.py", line 219, in getattr
raise AttributeError
AttributeError

Getting started

Hi, I try to import nflgame, and keep getting that there is no urllib2 .
where can i find this?

More examples (drives, play-by-play)

More wiki articles need to be created to showcase some of the recent developments of nflgame. This includes, but is not limited to, accessing play-by-play data, searching drive data, and using the GameClock, PossessionTime and FieldPosition types effectively to perform advanced searches.

List of all players who lined up for a play?

Hi, thanks so much for this great tool! I enjoy coding games using game rewind (i.e. noting all players lined up in a play, their formation, etc. and using that data for analysis).

To make my life easier, I would very much like to have a list of all players who line up on defense and offense for any given snap. Is this possible with the data we're provided?

If it's possible, how would I go about accessing that data with nflgames?

Here's some test code so far:

import nflgame
agame = nflgame.one(2011, 17, "NE", "BUF")
gamedata = agame.data
gamedata['drives'].pop('crntdrv') #we're not interested in live games
delim = ';'
gamelog = [] # data to go into csv file
csvhdr = 'Qtr' + delim + 'Time' + delim + 'Down' + delim + 'Dist' + delim + 'YdsNet' + delim + 'Desc\n'
gamelog.append(csvhdr)

hdr = ""
logline = ""

# sort drives which are strings into numerical order using split to take care of
#1 vs 10, 11, 12 sort issues.
for k in sorted(gamedata['drives'].keys(), key=lambda a:map(int,a.split('.'))):
    adrive = gamedata['drives'][str(k)]
    for x in sorted(adrive['plays'].keys(), key=lambda a:map(int,a.split('.'))):
        aplay = adrive['plays'][str(x)]
        p = [aplay['qtr'], aplay['time'], aplay['down'], aplay['ydstogo'], aplay['yrdln'], aplay['ydsnet'], \
             aplay['desc'], aplay['note']]
        for attr in p:
            logline = logline + unicode(attr).encode('unicode_escape') + delim
        logline = logline + '\n'
        print logline

nflgame on Android

Has anyone tried to setup nflgame on an Android phone? If it works with QPython 2.7.2, then it's probably pretty easy to install, but I'm not sure how.

clean up height data

It would be nice if the player update script sanitized height data so that it was in inches. If a height cannot be parsed, it should default to 0.

Tracking injuries programmatically

Wow, a lot has happened over the holiday regarding the Complete List of Active Players, thanks to cminton and BurntSushi.
The team rosters look very nice, but does anyone else notice the number of duplicate names, seams like more than ever. These are just the ones on my existing -----'excel generated' ------injury list.

Jonathan Stewart
Brandon Marshall
Andre Smith
Chris Givens
A.J. Davis
Brandon Williams
Steve Williams
Chris Clemons
Zach Miller
Michael Smith

Currently I’m getting the injuries here , http://www.pro-football-reference.com/years/2013/injuries.htm . The name formats are a very good match. But because of the lack of a gsisid simple name matching will be troublesome again this year. Ultimately, I believe my answer is here; http://www.nfl.com/injuries?week=1 which just became active this morning but has yet to be populated. I’m confident in being able to scrape this page with excel. However, if the gsisid is present it certainly won’t be listed in the table format, but rather obscured in the source code. Does anyone know how to scrape that type of page of all its contents?

Python 2.6.X

Not so much a bug, as outdated documentation. The main doc says it works with 2.6 and 2.7. When I tried to run nflgame on 2.6.5, I got the old "No module named ordereddict" error on import. It works fine on 2.7.5 - I didn't test with other versions. I would guess it only works on 2.7.X now.

Plays / drives missing

I noticed some plays are missing from some games when I print all plays using the following.

plays = game.drives.plays()
for p in plays:
print p

Both of the following examples are week 3 games and both began at noon and both happen around the same time during the first quarter. Perhaps all games from week 3 are missing stats for this time frame, but I'm not sure. I'm also a novice programmer, I tried deleting the cached stats to see if they would be re-pulled with complete data, but it made no difference.

2013 Week 3 ARI @ NO - Game clock jumps from 9:19 to 7:27

(ARI, NO 3, Q1, 2 and 3) (9:19) A.Smith right end for 3 yards, TOUCHDOWN.
(ARI, NO 2, Q1) J.Feely extra point is GOOD, Center-M.Leach, Holder-D.Zastudil.
(ARI, ARI 35, Q1) J.Feely kicks 74 yards from ARI 35 to NO -9. D.Sproles, Touchback.
(NO, NO 41, Q1, 3 and 3) (7:27) (Shotgun) D.Brees pass short right to J.Graham pushed ob at ARI 45 for 14 yards (J.Powers). Pass 0, YAC 14

2013 Week 3 TB @ NE - Game clock jumps from 10:15 to 7:48

(TB, NE 20, Q1, 4 and 2) (10:15) R.Lindell 38 yard field goal is No Good, Wide Right, Center-A.Economos, Holder-M.Koenen.
(TB, TB 12, Q1, 1 and 10) (7:48) (Shotgun) J.Freeman sacked at TB 10 for -2 yards (sack split by R.Ninkovich and C.Jones).

Anyway to discern a player's position?

Hello!

I hope this isn't a duplicate issue...

Is there anyway to determine a player's position? For instance, when returning a list of receiving leaders for a week, it would be nice to further filter that list to show only Tight Ends or Running Backs.

Love this API! Thanks for all of the hard work.

Proposed enhancement to Play structure

The default structure for some_play['players'] is a dictionary of lists of events indexed by player ID. For example (JSON format):

{
    "ydstogo": 5,
    "note": null,
    "qtr": 1,
    "yrdln": "NYG 18",
    "sp": 0,
    "down": 3,
    "players": {
        "0": [
            {
                "playerName": "",
                "clubcode": "NYG",
                "yards": 0,
                "statId": 4,
                "sequence": 1
            },
            {
                "playerName": "",
                "clubcode": "NYG",
                "yards": 0,
                "statId": 6,
                "sequence": 2
            }
        ],
        "00-0023590": [
            {
                "playerName": "G.Sensabaugh",
                "clubcode": "DAL",
                "yards": 0,
                "statId": 79,
                "sequence": 8
            }
        ],
        "00-0022803": [
            {
                "playerName": "E.Manning",
                "clubcode": "NYG",
                "yards": 26,
                "statId": 15,
                "sequence": 3
            },
            {
                "playerName": "E.Manning",
                "clubcode": "NYG",
                "yards": 17,
                "statId": 111,
                "sequence": 4
            }
        ],
        "00-0027265": [
            {
                "playerName": "V.Cruz",
                "clubcode": "NYG",
                "yards": 26,
                "statId": 21,
                "sequence": 5
            },
            {
                "playerName": "V.Cruz",
                "clubcode": "NYG",
                "yards": 0,
                "statId": 115,
                "sequence": 6
            },
            {
                "playerName": "V.Cruz",
                "clubcode": "NYG",
                "yards": 9,
                "statId": 113,
                "sequence": 7
            }
        ]
    },
    "time": "10:50",
    "ydsnet": 31,
    "posteam": "NYG",
    "desc": "(10:50) (Shotgun) E.Manning pass deep left to V.Cruz ran ob at NYG 44 for 26 yards (G.Sensabaugh)."
},

I find this format a little bit unintuitive because it seems like the value of some_play['players'] is just a perversely structured list of events.

Instead of the above, what would you think about adopting a format more along the lines of the below (JSON again)?

{
    "down": 3,
    "note": null,
    "qtr": 1,
    "yrdln": "NYG 18",
    "sp": 0,
    "ydstogo": 5,
    "time": "10:50",
    "ydsnet": 31,
    "events": [
        {
            "playerId": null,
            "playerName": "",
            "statId": 4,
            "yards": 0,
            "clubcode": "NYG"
        },
        {
            "playerId": null,
            "playerName": "",
            "statId": 6,
            "yards": 0,
            "clubcode": "NYG"
        },
        {
            "playerId": "00-0022803",
            "playerName": "E.Manning",
            "statId": 15,
            "yards": 26,
            "clubcode": "NYG"
        },
        {
            "playerId": "00-0022803",
            "playerName": "E.Manning",
            "statId": 111,
            "yards": 17,
            "clubcode": "NYG"
        },
        {
            "playerId": "00-0027265",
            "playerName": "V.Cruz",
            "statId": 21,
            "yards": 26,
            "clubcode": "NYG"
        },
        {
            "playerId": "00-0027265",
            "playerName": "V.Cruz",
            "statId": 115,
            "yards": 0,
            "clubcode": "NYG"
        },
        {
            "playerId": "00-0027265",
            "playerName": "V.Cruz",
            "statId": 113,
            "yards": 9,
            "clubcode": "NYG"
        },
        {
            "playerId": "00-0023590",
            "playerName": "G.Sensabaugh",
            "statId": 79,
            "yards": 0,
            "clubcode": "DAL"
        }
    ],
    "posteam": "NYG",
    "desc": "(10:50) (Shotgun) E.Manning pass deep left to V.Cruz ran ob at NYG 44 for 26 yards (G.Sensabaugh)."
}

The key difference here is that each play boils down to an ordered sequence of "events" (whatever you want to call them), each of which is associated with a player (unless it's not).

In my mind, the two main advantages of this are:

  1. You can ask: "Show me all pass completions of at least 26 yards where at least 9 yards came after the catch."
  2. It would be easier to flatten into a relational structure. Since it is just a list of dictionaries (or, in JSON, an array of objects), it is agnostic to whatever lookup method you prefer, as opposed to the default pattern which assumes you want to look up events based on player ID.

Here is one way you could achieve this:

def rotate_events(players):
    """
    The variable `players` is a dictionary of lists of events indexed
    by player ID; e.g.:
    {"00-0023590": [{"playerName": "G.Sensabaugh",
                    "clubcode": "DAL",
                    "yards": 0,
                    "statId": 79,
                    "sequence": 8}],
     "00-0022803": [{"playerName": "E.Manning",
                     "clubcode": "NYG",
                     "yards": 26,
                     "statId": 15,
                     "sequence": 3},
                    {"playerName": "E.Manning",
                     "clubcode": "NYG",
                     "yards": 17,
                     "statId": 111,
                     "sequence": 4}]}
    """
    temp = list()
    for key, val in players.items():
        for event in val:
            # Append `temp` with a tuple containing the value of 
            # event['sequence'] and the reformatted dictionary representing 
            # the event; e.g.:
            # (4, {"playerName": "E.Manning", "clubcode": "NYG", "statId": 111})
            temp.append((event['sequence'],
                         dict(playerName=event['playerName'] or None,
                              playerId=event['playerId'] or None,
                              clubcode=event['clubcode'],
                              statId=event['statId'],
                              yards=event['yards'],)))
    # Return 2nd element of each tuple after sorting by the first
    return [t[1] for t in sorted(temp, key=lambda t: t[0])]

License

What license is nflgame released with? Or, in simple terms, are there any restrictions on its use?

make nflgame-update-players a python module

This will allow us to invoke it in a cross platform manner. For example:

import subprocess
import sys

subprocess.check([sys.executable, '-m', 'update_players.py'])

And the actual nflgame-update-players script will be replaced with a few lines that import update_players and run it.

Week 2 Data Error, Probably a user error...

Once again I am sure I am doing something wrong, but if I look at week 1 game stats things work well, if I look at week 2 I get the following error (code is posted below output:

[root@AZtest ~]# ./defstat.py 2012 1

Year: 2012 Week: 1
Sacks: C.Matthews , 00-0027002 , 2.5
Interceptions: T.Jennings , 00-0024277 , 2
Fumble Recoveries: B.Church , 00-0027416 , 1
[root@AZtest ~]# ./defstat.py 2012 2

Year: 2012 Week: 2
Traceback (most recent call last):
File "./defstat.py", line 16, in
players = nflgame.combine_max_stats(games)
File "/usr/lib/python2.7/site-packages/nflgame/init.py", line 473, in combine_max_stats
return reduce(lambda a, b: a + b, [g.max_player_stats() for g in games])
AttributeError: 'NoneType' object has no attribute 'max_player_stats'
[root@AZtest ~]#

CODE:

!/usr/bin/env python2.7

import nflgame
import sys

Set if the year and week was passed, if nothing passed, exit.

if len(sys.argv) > 2:
stat_year = int(sys.argv[1])
stat_week = int(sys.argv[2])
else:
print "\nSorry, you must specify the year AND week.\n\n"
sys.exit()

print "\nYear: ", stat_year, "Week: ", stat_week

games = nflgame.games(stat_year, week = stat_week)
players = nflgame.combine_max_stats(games)
for p in players.filter(defense_sk=lambda x: x > 0).sort("defense_sk"):
print "Sacks: ", p, ", ", p.playerid, ", ", p.defense_sk
break
for p in players.filter(defense_int=lambda x: x > 0).sort("defense_int"):
print "Interceptions: ", p, ", ", p.playerid, ", ", p.defense_int
break
for p in players.filter(defense_frec=lambda x: x > 0).sort("defense_frec"):
print "Fumble Recoveries: ", p, ", ", p.playerid, ", ", p.defense_frec
break

Use with SublimeText2

I am using Windows 7, SublimeText2 and Python 2.7.3 (32-bit).

Upon using the msi installer, I can import the module and complete the following (as an example) with IDLE Python Shell.

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

import nflgame
game = nflgame.one(2011, 17, "NE", "BUF")
print game
NE (49) vs. BUF (21)

However, the exact same command results in this error:

Traceback (most recent call last):
File "C:\Users\jbaylin\Dropbox\P\nflgame.py", line 1, in
import nflgame
File "C:\Users\jbaylin\Dropbox\P\nflgame.py", line 2, in
game = nflgame.one(2011, 17, "NE","BUF")
AttributeError: 'module' object has no attribute 'one'
[Finished in 0.1s with exit code 1]

Now, upon checking to see whether the Python version is identical and whether the Path includes the proper directories, I get the following.

import sys, os
print sys.version
print sys.path

2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)]
['C:\Users\jbaylin\Dropbox\P', 'C:\Windows\system32\python27.zip', 'C:\Python27\DLLs', 'C:\Python27\lib', 'C:\Python27\lib\plat-win', 'C:\Python27\lib\lib-tk', 'C:\Python27', 'C:\Python27\lib\site-packages']

PLEASE HELP... I love this module and can't understand why I can't get it going in SublimeText2!

Thank you!

Calculate 3rd down conversion rate

@poppers112 Asked about how one would go about calculating 3rd down conversion rates for the two different teams involved in a game. I was interested in that as well so attempted to do the same. This isn't an issue, and probably belongs in a cookbook that may one day exist, but I couldn't think of a better place to put this.

My attempt as follows:

import nflgame

year, week, season_type = 2013, 1, 'REG'

def third_down2(teamname):
    games = nflgame.games_gen(
        year, week, teamname, teamname, kind=season_type)
    plays = nflgame.combine_plays(games)
    attempts = 0
    conversions = 0
    for p in plays.filter(team=teamname, third_down_att=True):
        attempts += 1
        conversions += p.third_down_conv
    percentage = float(conversions)/float(attempts)*100
    return '%s: %s of %s (%.2f%%)' % (
        teamname, conversions, attempts, percentage)


games = nflgame.games(year, week, kind=season_type)
for game in games:
    print third_down2(game.home)
    print third_down2(game.away)

This gets the job done.

DEN: 8 of 15 (53.33%)
BAL: 8 of 22 (36.36%)
BUF: 4 of 13 (30.77%)
NE: 11 of 20 (55.00%)
CAR: 5 of 11 (45.45%)
SEA: 6 of 13 (46.15%)
CHI: 6 of 14 (42.86%)
CIN: 7 of 11 (63.64%)
CLE: 1 of 14 (7.14%)
MIA: 8 of 16 (50.00%)
DET: 5 of 13 (38.46%)
MIN: 2 of 10 (20.00%)
IND: 6 of 10 (60.00%)
OAK: 7 of 13 (53.85%)
JAC: 5 of 19 (26.32%)
KC: 5 of 15 (33.33%)
NYJ: 7 of 18 (38.89%)
TB: 6 of 16 (37.50%)
NO: 6 of 13 (46.15%)
ATL: 3 of 11 (27.27%)
PIT: 4 of 13 (30.77%)
TEN: 6 of 15 (40.00%)
STL: 4 of 11 (36.36%)
ARI: 7 of 14 (50.00%)
SF: 9 of 18 (50.00%)
GB: 4 of 10 (40.00%)
DAL: 5 of 15 (33.33%)
NYG: 6 of 11 (54.55%)

How can I improve this method?

Filter by Opponent?

Is it possible to filter by opponent played against? For example, I want to filter on all QBs that played against the Buffalo Bills.

Possible issue with combine_plays

I noticed an issue anytime I try to aggregate New England stats using combine_plays. Take the example of Getting a team's total incompletions for the season.


def getTotalIncomplete(s, w, t):
    games = nflgame.games_gen(s, w, t, t)
    plays = nflgame.combine_plays(games)

    totinc = 0.0
    for p in plays.filter(team=t, passing_incmp__gt=0):
        totinc += p.passing_incmp

    return totinc

print getTotalIncomplete(2013, None, "BUF")

You should be able to run that successfully. However, when I attempt to run that for NE, I get the following returned:

Traceback (most recent call last):
  File "C:/Users/jljones1/Google Drive/Dev/Python/NFL/inctest", line 15, in <module>
    print getTotalIncomplete(s, w, t)
  File "C:/Users/jljones1/Google Drive/Dev/Python/NFL/inctest", line 5, in getTotalIncomplete
    plays = nflgame.combine_plays(games)
  File "C:\Python27\lib\site-packages\nflgame\__init__.py", line 486, in combine_plays
    chain = itertools.chain(*[g.drives.plays() for g in games])
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 407, in __getattr__
    self.__drives = _json_drives(self, self.home, self.data['drives'])
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 675, in _json_drives
    drives.append(Drive(game, i, home_team, data[str(drive_num)]))
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 509, in __init__
    maxq = str(max(map(int, [p['qtr'] for p in data['plays'].values()])))
ValueError: max() arg is an empty sequence

From what I can tell, any aggregation that I have made that uses the combine_plays is erroring with this on NE ONLY.

Any ideas/insight? Is this just because nobody likes the Patriots? ;)

Thanks

nflgame-update-players fail

Just installed via pip. ran the update players script, and got the following:

sudo nflgame-update-players
Loading games for REG 2013 week 4
Downloading team rosters...
/Library/Python/2.7/site-packages/bs4/builder/_htmlparser.py:155: RuntimeWarning: Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help.
"Python's built-in HTMLParser cannot parse the given document. This is not a bug in Beautiful Soup. The best solution is to install an external parser (lxml or html5lib), and use Beautiful Soup with that parser. See http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-a-parser for help."))
Traceback (most recent call last):
File "/usr/local/bin/nflgame-update-players", line 4, in
nflgame.update_players.run()
File "/Library/Python/2.7/site-packages/nflgame/update_players.py", line 359, in run
for i, (team, soup) in enumerate(pool.imap(fetch, teams), 1):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/multiprocessing/pool.py", line 620, in next
raise value
HTMLParser.HTMLParseError: bad end tag: u"</sc'+'ript>", at line 1791, column 94

how do you find total field goal yards?

I received this question on my blog:

Just curious, how do you find the distances of the field goals. Our league awards 1 point per yard so a 45 yard field goal and a 25 yard field goal nets the kicker 70 points. We have a high scoring league.

I see when you output the stats for the week to a csv, it will give the kickers field goals attempted and made and there is a column called kicking_fgyds but that seems to give the long field goal. I'm hoping for the total yards of all the field goals made. Is that possible?

Having issues with live updates

Im new to python so im not sure if this is my issue or not. Im running live updates and at first all was fine but now I keep getting the error below:

Traceback (most recent call last):
  File "run.py", line 10, in <module>
    nflgame.live.run(cb)
  File "/usr/local/lib/python2.7/dist-packages/nflgame/live.py", line 234, in run
    active = _run_active(callback, games)
  File "/usr/local/lib/python2.7/dist-packages/nflgame/live.py", line 287, in _run_active
    diffs.append(game - last_game)
  File "/usr/local/lib/python2.7/dist-packages/nflgame/game.py", line 413, in __sub__
    return diff(other, self)
  File "/usr/local/lib/python2.7/dist-packages/nflgame/game.py", line 432, in diff
    after_plays = list(after.drives.plays())
  File "/usr/local/lib/python2.7/dist-packages/nflgame/game.py", line 407, in __getattr__
    self.__drives = _json_drives(self, self.home, self.data['drives'])
  File "/usr/local/lib/python2.7/dist-packages/nflgame/game.py", line 675, in _json_drives
    drives.append(Drive(game, i, home_team, data[str(drive_num)]))
  File "/usr/local/lib/python2.7/dist-packages/nflgame/game.py", line 509, in __init__
   maxq = str(max(map(int, [p['qtr'] for p in data['plays'].values()])))
 ValueError: max() arg is an empty sequence

This code that im running is below

import nflgame.live

print 'Running live update script'

def cb(active, completed, diffs):
        for g in active:
            h, sh, a, sa = g.home, g.score_home, g.away, g.score_away
            print '%s (%d) vs. %s (%d)' % (h, sh, a, sa)

nflgame.live.run(cb)

What am I doing wrong?

guinea pig for the new player update script needs help!

Can anyone help me execute Andrews newest addition “nflgame-update-players”..?
You need the 1.1.14 update. I’m using the windows XP command prompt.

C:\Documents and Settings\geo>assoc .py
.py=Python.File

C:\Documents and Settings\geo>ftype python.file
python.file="C:\Python27\python.exe" "%1" %*

C:\Documents and Settings\geo>type c:\python27\scripts\nflgame-update-players.py

The system cannot find the file specified.

I assume that nflgame-update-players is a script and should carry the extension dot.py
So I copied it and renamed as udp.py

C:\Python27\Scripts>udp.py
Traceback (most recent call last):
  File "C:\Python27\Scripts\udp.py", line 52, in <module>
    import httplib2
ImportError: No module named httplib2

I searched my Pyhton folder for httplib2, and it’s missing.

ValueError: max() arg is an empty sequence

A problem with my go to stats, had a similar issue updating players too.
Anyone else having problems?

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import tfb2013
  File "C:\Python27\lib\tfb2013.py", line 81, in <module>
    players = g.max_player_stats()
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 368, in max_player_stats
    play_players = list(self.drives.plays().players())
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 407, in __getattr__
    self.__drives = _json_drives(self, self.home, self.data['drives'])
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 675, in _json_drives
    drives.append(Drive(game, i, home_team, data[str(drive_num)]))
  File "C:\Python27\lib\site-packages\nflgame\game.py", line 509, in __init__
    maxq = str(max(map(int, [p['qtr'] for p in data['plays'].values()])))
ValueError: max() arg is an empty sequence

show all winning teams for a season?

I can return the winning team if I specify the index of one of the objects, but not all winning from a season. (this api is awesome. thank you!)

Confused about player filters

Hi,

I have a similar question to what I posted on the blog recently. I'm trying to aggregate team statistics together across multiple weeks. Shouldn't be too hard, but I'm getting stuck.

For example:

games = nflgame.games(2011,home='NE',kind='REG')
players = nflgame.combine_game_stats(games)
>>> for p in players.passing():
    print p, p.passing_yds


T.Brady 2544
P.Rivers 378
M.Sanchez 166
T.Romo 317
E.Manning 250
T.Palko 236
D.Orlovsky 353
M.Moore 294
B.Hoyer 22
R.Fitzpatrick 307

Seems to work correctly. But I want only Patriots...

So when I try:

>>> for p in players.passing().filter(home=True):
    print p, p.passing_yds

B.Hoyer 22

Why am I only getting one player? Thanks!

(And btw, how do I tag code in these posts?)

Sometimes play-by-play stats are wrong. Sometimes game stats are wrong. What to do?

Using (please excuse the awful code):

games = nflgame.games(2012, week=1)
players = nflgame.combine(games)

for p in players:
    if p.receiving_twopta > 0 or p.receiving_twoptm > 0 or p.rushing_twoptm > 0 or p.rushing_twopta > 0:
        print "%s has %s 2pta and %s 2ptm and %s rush_2ptm and %s rush_2pta" % (p.name, p.receiving_twopta, p.receiving_twoptm, p.rushing_twoptm, p.rushing_twopta)

I get

J.Blackmon has 1 2pta and 1 2ptm and 0 rush_2ptm and 0 rush_2pta
J.Nelson has 1 2pta and 1 2ptm and 0 rush_2ptm and 0 rush_2pta
W.McGahee has 0 2pta and 0 2ptm and 0 rush_2ptm and 1 rush_2pta
R.Streater has 1 2pta and 1 2ptm and 0 rush_2ptm and 0 rush_2pta 

Yet Darren Sproles also had a 2 point conversion wish is listed in the play-by-play on nfl.com. Outputting only Sproles' formatted_stats() gives him 0's for rushing_twoptm and rushing_pta. Check here for PFR's expected statistics.

Receiver targets

I don't know if I am doing something stupid (entirely possible), but I am not able to access receiving targets, and none of the other advanced statistics.

This is the small piece of code I wrote to test things out, but I get the error below:

import nflgame
games = nflgame.games(2010, week=[10, 11, 12, 13, 14])
players = nflgame.combine(games)
for p in players.receiving().sort("receiving_tar").limit(10):
print p, p.receiving_tar

Traceback (most recent call last):
File "<pyshell#8>", line 1, in
for p in players.receiving().sort("receiving_tar").limit(10):
File "C:\Python27\lib\site-packages\nflgame\seq.py", line 115, in sort
key=lambda item: item.dict[field]))
File "C:\Python27\lib\site-packages\nflgame\seq.py", line 115, in
key=lambda item: item.dict[field]))
KeyError: 'receiving_tar'

Am I being dumb?

Statistical Categories

Thanks Andrew Gallant for this wonderful module!

Here is a (possibly incomplete) data dictionary containing descriptions for each statId. Based on the link caption in the parent directory, it looks like it was last updated for the 2009 season.

Filter Stats in CSV

Is there anyway to filter which stats get exported into a CSV file? I know I can do the following to limit and filter the players exported, but let's say I only want receptions, receiving yards and touchdowns?

players.receiving().sort("receiving_yds").limit(25).csv("stats.csv")

Sacks against?

Prior to embarking on a mission to aggregate Sack totals against a team, is there a Sacks against per game stat that is recorded? Or, more likely, I suppose, does anyone know of a Sacks per game and a Sack yards per game (for the defense)?

I'm not looking for someone to write anything for me, I'm just trying to refrain from reinventing the wheel.

Complete List of Active Players

I would like to dump out a complete list of active players. I'm starting to look at statistics for players and thinking about the players with similar gsis_names. For example, Calvin and Chris Johnson. Is Calvin 'Ca' or is Chris 'Ch'? I want this because I'm going to enter the players on teams in my league and look at their stats and the stats. I figured I would dump out the players with:

All players

players=nflgame.player._create_players()

Active players

act=[k for k in players.keys() if players[k].status=='ACT']

Active offense players

pos=['QB','RB','WR','TE','K']
offense=[k for k in act if players[k].position in pos]

Not all these players have the attribute 'gsis_id' but, I can use the key for them. However, not all of them have a gsis_name. Why is that?

Is there an easy way to get a list of player names, teams, gsis_names and gsis_ids?

Kerry Collins not in players list

I am finding statistics for Kerry Collins in 2009 games, but I don't find his playerid in nflgame.players. Code below.

import nflgame
my_id='00-0003292'
[i.name for i in nflgame.combine(nflgame.games(2009)) if i.playerid == my_id]
nflgame.players[my_id]

produces :

KeyError Traceback (most recent call last)
in ()
----> 1 nflgame.players[my_id]

KeyError: '00-0003292'

Access NFL Game from a C program?

Hi: this is really fantastic...thank you for making this available. I normally use Python, so this is great, but I wonder if I can make HTTP calls and then parse the data in my C program? Is there a HTTP API? Thanks, Ian

Showing 1 changed file with 287 additions and 245 deletions.

Andrew
I noticed yesterday that you worked on player data.
I’m still at a loss as to how that script is used to update the player json file.
You also acknowledged that you think it’s possible to slim down that process is that what seen yesterday..? If so I would love to monitor these modifications for timeliness and accuracy.

List statistical fields

As of right now, there are no convenient facilities for discovering what kinds of statistics are available. Moreover, player statistics vary depending upon whether you're looking at accumulated game statistics or play-by-play statistics.

There are also heaps of data describing the game, each individual drive and each individual play.

These should be in a list somewhere. (Probably in the wiki.)

Unit tests please.

Write some unit tests for this project, please.

Some ideas/concerns:

What do we use for verifying data? We know that GameCenter's JSON data is erroneous frequently enough that any comparison with verified data (say, from PFR) will fail on any non-trivial data set. Do we empirically determine some sort of error margin that allows stats to be a little wrong? Yuck.

A simplifying assumption may be to assume a priori that what we have now is OK. Generate some output now, then write tests that verifies that the same output is generated in the future. This doesn't do much for verifying data, but it certainly helps prevent us from breaking things in the future.

Modules like nflgame.live will be difficult to test correctly.

Also, Python's basic example for the unittest module looks OK. I think that will probably be enough to make us dangerous.

Player college meta data

Have seen a few examples where the college data does not match what I am seeing on nfl.com.

Is it possible that the scrape is sometimes clipping the data? It is not always happening, otherwise there would be zero 'Iowa State' players in the database which isn't true.

Examples include:

Namenflgame collegeactual collegelink
Sage RosenfelsIowaIowa Statelink
Collin FranklinIowaIowa Statelink
Alexander RobinsonIowaIowa Statelink
Marquis HamiltonIowaIowa Statelink
Tyson SmithIowaIowa Statelink
Alvin BowenIowaIowa Statelink

Could use a hand

I'm not a developer, I'm a statistician. I have a project I'm working on, and I use nflgame to get statistical data for players which I find very helpful. I still need two more types of information. I read through the API, but unfortunately due to my lack of programming expierence, I cannot figure out how to structure a call to get what I am looking for.

  1. I would like a list of all games for the 2013 season, which could show me home team and away team. I'd like this in a csv.
  2. I was also wondering if there was a way to execute a call that could return demographic information about each player (ie Height, Weight, date of birth, college). This would also help me with the trending work I've been asked to complete. Again, csv would be ideal.

If someone could help me, that would be fantastic. Thanks.

Expanded Player Information

Is expanded player info not implemented yet? I attempted to use player.position but it's throwing an Attribute Error. Furthermore, from browsing through the player.py code it looks like print player.self should print name, position, and team, but it only prints player.name (which seem to be how it works in the tutorial).

Team statistics

This is a fantastic module; there is not much else out there to work with stats like this programmatically.

There are a couple of things that I have been stuck on, though...:

How would you go about finding defensive touchdowns and safeties? TDs seem like they should end up in the defensive player's stats (for the player that scored), but they aren't there. Safeties are mentioned in the statmap, but I don't see where they are tallied.

How about blocked kicks?
I could possibly add up blocked kicks by all offensive players..., but is there a better way to do this, from the perspective of the defensive player?

Describe the data model

The data model for the GameCenter JSON data desperately needs to be described. The fact that there are three different ways to access player statistics (game level, play level and combined) will be baffling to new users. This may be somewhat addressable in the API, but this needs to be explained in detail.

The data model should describe the relationship between Game, Drive, Play, Player and {Game,Play}PlayerStats objects. It should also describe how statistics are computed in play-by-play data using the nflgame.statmap module.

Any help would be greatly appreciated.

""Issues should be open for just about anything—including questions about how to use nflgame, or even problems with NFL.com data. There should be a pretty low threshold to opening one.""

Okay then, here goes. I’m clearly too old and not smart enough to understand Python or this little slice of heaven you call, nflgame. I haven’t been able to a damn thing on my own, if the code hasn’t been published here or in the tutorial, all I get are errors.

I'm using version ‘1.1.8’ of nflgame. My main focus is player stats, post game at rest data. I’ve used this code successfully, mostly because it’s in the tutorial.

nflgame.combine_max_stats(nflgame.games(2012)).csv('PD2012.csv',allfields=True)

The new allfields parameter, ‘thank you Andrew’ is very helpful, however, now I see inconsistent results, And I’m not sure why. ??
total players receiving_rec receiving_tar receiving_yac_yds receiving_yds
ver. 1.1.7 1812 10838 17550 53939 125983
ver. 1.1.8 all fields=T 1812 10779 17429 53656 125259
ver. 1.1.8 1812 10838 17550 53939 125983
Additionally, most weeks yield 135 columns of data when all-fields is true, but other weeks only 134 columns. The “def_misc_yds” column appears to the culprit. This column misalignment makes summing values across multiple excel sheets a challenge.
So how could I query just the data columns I want.?

nflgame.combine_max_stats(nflgame.games(2012,week=2)) .csv('PD2012-2.csv',fields= name, id, team, pos, receiving_rec, receiving_tar, receiving_yac_yds, receiving_yds ) --------for example…?
Is something like this possible?

This is the only query I’ve been able to work on my own.

nflgame.teams
However, it has no value to me just trying to get something working on my own.
I can’t get .csv file dump to work.
nflgame.teams.csv(“teams.csv”)

Additional things I’m tying without success are:

· Drive charts

import nflgame
game = nflgame.one(2011, 17, "NE", "BUF")
print game.drives
o This works but only yields scoring drives per game
o Need all drives by all teams, including starting field position per given week
o Also in the form of a .csv dump

· Schedule
o All teams for entire season, in the form of a .csv dump
· Team rosters
o All teams for any given week, in the form of a .csv dump
· Injury report, player status
o All teams for any given week, in the form of a .csv dump

Any help would be greatly appreciated.
And thank you in advance.

Defensive Fumble Recovery Count is Wrong (IMHO)

When counting the Defensive Fumble recoveries what is needed is to take the count of LOST fumbles from the opposing team. If you look at the Washington/New Orleans Game in Week 1 you will see that the Saints Colston fumbled out of the side of the end zone (from the 3 yard line) which means that WAS gets a fumble recovery (because WAS got the ball on their 20 as a result of the Satins fumble). But the stats csv file has WAS with 0 recoveries. Now, some may (purely speaking) not want to count that as a fumble recovery, but statistically, that is exactly what it is... IMHO

Ray

2012 Playoffs

Got a very bad feeling this morning that I was gearing up for the playoffs and things were going to puke on Saturday. Can anyone calm my fears that things are going to work this weekend?

games = nflgame.games(2012, week=1, kind='POST')

Will that line work just like this one does I hope?

games = nflgame.games(2011, week=1, kind='POST')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.