BurntSushi / nflgame

An API to retrieve and read NFL Game Center JSON data. It can work with real-time data, which can be used for fantasy football.
http://pdoc.burntsushi.net/nflgame
The Unlicense
1.27k stars 413 forks source link

Punt and Kickoff Return Yardage #76

Closed Sorcerer54 closed 10 years ago

Sorcerer54 commented 10 years ago

I fall into the non programmer category so I’m not sure if the issue is with the program or, more likely, with me. Any help would be appreciated. I’m interested in the preseason games and have a few questions, but I’ll limit it to one at this time. I used the following to extract Passing(), Rushing() and Receiving () stats without issue. However, when attempting to retrieve return stats (kickoff return yardage and punt return yardage) this is what I input:

>>> import nflgame 
>>> games = nflgame.games(2013, kind='PRE',week=1)
>>> players = nflgame.combine(games)
>>> for p in players.puntret():
    print p, p.puntret_yds, p.puntret_tds 

And this is what was returned:

T.Benjamin 0 1
N.Johnson 0 0
H.Douglas 0 0
R.Alford 0 0
G.Bernard 0 0
B.Tate 0 0
D.Sanzenbacher 0 1 

I used just a small sample to illustrate what I am running up against. Many more players were listed when I ran “puntret_yds” and “kickret_yds”. I’m fairly sure I’m calling for the right statistical fields as both Travis Benjamin and Dane Sanzenbacher ran back punts for TDs. However, as you can see, nobody gets credit for any return yardage. Am I doing something wrong here?

BurntSushi commented 10 years ago

Try changing nflgame.combine(games) to nflgame.combine_max_stats(games).

While it's technically a problem in your code, this is really my fault. It's a result of my most significant regret when building nflgame: there is a divide between summary statistics available per game and statistics available per play. The per game statistics aren't as varied and they don't include, for example, punt return yards. The combine_max_stats function actually combines both game statistics and play statistics. Because it has to compute all of this on the fly for each play, it's very slow.

FYI, nfldb doesn't have this problem since it's all done in the database. For example, this code will do what your code does, but for the entire season:

import nfldb

q = nfldb.Query(nfldb.connect(db))
q.game(season_year=2013, season_type='Regular')
for p in q.sort('puntret_yds').limit(10).as_aggregate():
    print p.player, p.puntret_yds, p.puntret_tds

But it takes less than a second. The equivalent nflgame code takes almost 20 seconds.

Sorcerer54 commented 10 years ago

I wanted to thank you for your prompt response. This works great!! I had hoped someone could help, but I certainly didn’t expect the author of the program to take time out from his busy schedule to help me on this. Maintaining this program must be very time consuming.

I browsed some of the database stuff and will, no doubt, give it a try in the future when I’ve more time. For right now I am trying to organize a preseason for my 25 year-old fantasy league and your program will make this possible with a little assist from Excel. As far as I know, no service offers preseason fantasy football. I’m not really interested in real-time stats for this. Post-game or several hours after the game(s) have ended will be more than adequate. With that in mind, do you have any idea how long after a game ends, that I would be able to access the final game stats (through Nflgame) without causing a “ping” to NFL.COM?

Also, is there any way to access the preseason Hall of Fame Game statistics? I have seen XML feeds that refer to this game as “Week=0” in some documents. Obviously, that’s not the way it goes in your program. I tried it and got an error.

Is there a way to access Defensive Team stats? Things like how many interceptions, sacks, fumbles, blocked punts, etc. were accomplished by a team? Everything I’ve tried gives me individual defensive player stats. I recall reading an issue or something of that nature dealing with this, but I don’t seem to be able to find it now.

I don’t want to be a pest and I really am appreciative of the help you’ve already given me. I sure wish I would’ve had access to a program like this 16 or more years ago when we fantasy football commissioners were doing our leagues weekly scoring with pencil, paper and a copy of the USA Today Sports Section.

On Tue, May 13, 2014 at 9:22 AM, Andrew Gallant wrote:

Try changing nflgame.combine(games) to nflgame.combine_max_stats(games) . While it's technically a problem in your code, this is really my fault. It's a result of my most significant regret when building nflgame: there is a divide between summary statistics available per game and statistics available per play . The per game statistics aren't as varied and they don't include, for example, punt return yards. The combine_max_stats function actually combines both game statistics and play statistics. Because it has to compute all of this on the fly for each play, it's very slow. FYI, nfldb doesn't have this problem since it's all done in the database. For example, this code will do what your code does, but for the entire season : import nfldb q = nfldb . Query ( nfldb . connect ( db )) q . game ( season_year = 2013 , season_type = 'Regular' ) for p in q . sort ( 'puntret_yds' ) . limit ( 10 ) . as_aggregate (): print p . player , p . puntret_yds , p . puntret_tds But it takes less than a second. The equivalent nflgame code takes almost 20 seconds. — Reply to this email directly or view it on GitHub https://github.com/BurntSushi/nflgame/issues/76#issuecomment-42977641 . https://github.com/BurntSushi/nflgame/issues/76#issuecomment-42977641

BurntSushi commented 10 years ago

I had hoped someone could help, but I certainly didn’t expect the author of the program to take time out from his busy schedule to help me on this.

No problem. Things get much busier 'round these parts once the season kicks into gear. There are a few more active and very helpful folks that help out with answering questions on the issue tracker. But there is always a bit of a lull during the offseason.

For right now I am trying to organize a preseason for my 25 year-old fantasy league and your program will make this possible with a little assist from Excel. As far as I know, no service offers preseason fantasy football. I’m not really interested in real-time stats for this. Post-game or several hours after the game(s) have ended will be more than adequate. With that in mind, do you have any idea how long after a game ends, that I would be able to access the final game stats (through Nflgame) without causing a “ping” to NFL.COM?

Great! You're definitely not the first I've heard of doing something like this with nflgame.

Out of curiosity, what are your concerns with pinging NFL.com? Practically speaking, you should have no problems. nflgame does its best to only send a request to NFL.com when it has to. (And if you're using nflgame, then it's automatically pinging for schedule data at most once a day.) Note that once a game is over, nflgame will ask NFL.com for the data, but then cache it so that it doesn't need to keep pinging NFL.com.

But to answer your question... It depends on when I make a release. For the most part, I don't make releases every week because nflgame is designed to download the data for you independently of my release schedule. That is, you shouldn't be depending on my releases for anything other than bug fixes (or the occasional feature).

Also, is there any way to access the preseason Hall of Fame Game statistics? I have seen XML feeds that refer to this game as “Week=0” in some documents. Obviously, that’s not the way it goes in your program. I tried it and got an error.

Hmm, I don't believe that game is currently accounted for in the schedule. I've filed #77 for that. I'll try to get to it soon.

Is there a way to access Defensive Team stats? Things like how many interceptions, sacks, fumbles, blocked punts, etc. were accomplished by a team? Everything I’ve tried gives me individual defensive player stats. I recall reading an issue or something of that nature dealing with this, but I don’t seem to be able to find it now.

Nope. You have to sum the statistics yourself. If you like, I can help you write the loops to do that. (The basic idea is to keep a dictionary of defensive stats for a game and sum them up by looking at each play.)

You can then stick that code in a function and reuse it whenever you want without having to think about it.

I don’t want to be a pest and I really am appreciative of the help you’ve already given me. I sure wish I would’ve had access to a program like this 16 or more years ago when we fantasy football commissioners were doing our leagues weekly scoring with pencil, paper and a copy of the USA Today Sports Section.

No problem! Happy to help. One of the things I really like about this project is that it attracts people who wouldn't traditionally call themselves programmers. It's a lot of fun helping them optimize with Python!

But yeah, I can't imagine having to do this manually from a newspaper. I'd sooner figure out how to use OCR (which has been around commercially since at least 1978) and process data that way. :-)

Sorcerer54 commented 10 years ago

Okay, let’s talk Defense. We don’t use IDP (Individual Defensive Players) in our league and you had stated that I’d need to calculate this separately. Since I would be wanting totals over several days of exhibition games (they usually play Thursday, Friday, Saturday and either Sunday or Monday nights during the preseason), I figured that getting the stats for individual games would be the easiest way to sort these stats especially for eventual exportation to Excel.

Of course, to a programmer, this might not seem the most expeditious way of doing things. Consequently, I’d love to hear your take on this as I “fumble” my way through this.

I considered that I would need to use a whole bunch of Stat Types such as defense_fgblk, defense_frec, defense_frec_tds, defense_int and on and on. I believe I read that using the combine_game_stats module would’ve given me the best option, so that’s the route I went and:

import nflgame games = nflgame.one(2013, 1, "TAM", "BAL", kind='PRE') players = nflgame.combine_game_stats(games)

Traceback (most recent call last): File "<pyshell#2>", line 1, in players = nflgame.combine_game_stats(games) File "C:\Python27\lib\site-packages\nflgameinit.py", line 349, in combine_game_stats [g.players for g in games if g is not None]) TypeError: 'NoneType' object is not iterable

That’s the error I got. Obviously, I don’t understand what I’m doing as I try to extract Tampa Bay players individual player defensive stats.

You asked about my concerns “pinging” NFL.com. Perhaps I am overly paranoid, but the National Football League seems to zealously guard their brand, logos and anything else they perceive to be theirs. A few years back CBS Sports sued the NFL over who owns the rights to sports statistics and the NFL filed a counter-suit. Apparently the NFL was going to attempt to charge a sevice fee for the “use” of “their” statistics. At least for now, the courts have ruled in favor of public domain for fantasy leagues. There are also the data feed providers such as Stats, Inc. and NFLdata API. They charge a pretty penny for real-time statistic feeds. Since fantasy football has become a cash cow for a number of entities, I see everyone and anyone trying to get their slice of the pie as well as the lawyers. If it comes to that, I guess I just don’t want to be the guy who “killed the goose that laid the golden egg”. In this case, nflgame.

On Thu, May 15, 2014 at 8:19 PM, Andrew Gallant wrote:

I had hoped someone could help, but I certainly didn’t expect the author of the program to take time out from his busy schedule to help me on this. No problem. Things get much busier 'round these parts once the season kicks into gear. There are a few more active and very helpful folks that help out with answering questions on the issue tracker. But there is always a bit of a lull during the offseason. For right now I am trying to organize a preseason for my 25 year-old fantasy league and your program will make this possible with a little assist from Excel. As far as I know, no service offers preseason fantasy football. I’m not really interested in real-time stats for this. Post-game or several hours after the game(s) have ended will be more than adequate. With that in mind, do you have any idea how long after a game ends, that I would be able to access the final game stats (through Nflgame) without causing a “ping” to NFL.COM? Great! You're definitely not the first I've heard of doing something like this with nflgame . Out of curiosity, what are your concerns with pinging NFL.com? Practically speaking, you should have no problems. nflgame does its best to only send a request to NFL.com when it has to. (And if you're using nflgame , then it's automatically pinging for schedule data at most once a day.) Note that once a game is over, nflgame will ask NFL.com for the data, but then cache it so that it doesn't need to keep pinging NFL.com. But to answer your question... It depends on when I make a release. For the most part, I don't make releases every week because nflgame is designed to download the data for you independently of my release schedule. That is, you shouldn't be depending on my releases for anything other than bug fixes (or the occasional feature). Also, is there any way to access the preseason Hall of Fame Game statistics? I have seen XML feeds that refer to this game as “Week=0” in some documents. Obviously, that’s not the way it goes in your program. I tried it and got an error. Hmm, I don't believe that game is currently accounted for in the schedule. I've filed #77 https://github.com/BurntSushi/nflgame/issues/77 for that. I'll try to get to it soon. https://github.com/BurntSushi/nflgame/issues/77 Is there a way to access Defensive Team stats? Things like how many interceptions, sacks, fumbles, blocked punts, etc. were accomplished by a team? Everything I’ve tried gives me individual defensive player stats. I recall reading an issue or something of that nature dealing with this, but I don’t seem to be able to find it now. https://github.com/BurntSushi/nflgame/issues/77 Nope. You have to sum the statistics yourself. If you like, I can help you write the loops to do that. (The basic idea is to keep a dictionary of defensive stats for a game and sum them up by looking at each play.) https://github.com/BurntSushi/nflgame/issues/77 You can then stick that code in a function and reuse it whenever you want without having to think about it. https://github.com/BurntSushi/nflgame/issues/77 I don’t want to be a pest and I really am appreciative of the help you’ve already given me. I sure wish I would’ve had access to a program like this 16 or more years ago when we fantasy football commissioners were doing our leagues weekly scoring with pencil, paper and a copy of the USA Today Sports Section. https://github.com/BurntSushi/nflgame/issues/77 No problem! Happy to help. One of the things I really like about this project is that it attracts people who wouldn't traditionally call themselves programmers. It's a lot of fun helping them optimize with Python! https://github.com/BurntSushi/nflgame/issues/77 But yeah, I can't imagine having to do this manually from a newspaper. I'd sooner figure out how to use OCR (which has been around commercially since at least 1978) and process data that way. :-) https://github.com/BurntSushi/nflgame/issues/77 — Reply to this email directly or view it on GitHub https://github.com/BurntSushi/nflgame/issues/76#issuecomment-43291973 . https://github.com/BurntSushi/nflgame/issues/76#issuecomment-43291973

BurntSushi commented 10 years ago

My apologies for the late reply. I think your response slipped through the cracks.

It looks like your error is caused by using an invalid team abbreviation. For Tampa Bay, you'll need to use TB. Your approach otherwise seems pretty reasonable.

RE pinging NFL.com: I think there's a point where it's good to be paranoid, but fretting over a few requests for a personal project doesn't come close. Unless you're hitting NFL.com thousands of times an hour and/or using it to run a commercial application, then I think you're OK.

For what it's worth, while games are playing, I send a request to NFL.com for data for each game every 30 seconds. This sounds like a lot, but it's chump change. (It's actually cheaper than opening the game in your browser and leaving the tab open since it auto-refreshes!)