Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 + 2 fixes #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*.py[cod]
__pycache__/
__pycache__

# OSX
.DS_Store

# Eclipse
.project
.pydevproject
/.settings

# Do not commit data files
/data/*.csv
/models/*.pkl
/models/*.npy

# generally do not commit node_modules (controversial...)
/node_modules
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ The Armchair Analysis data is *not free*. It costs $49 (one-time) to gain access
data from the 2000-2014 NFL seasons. There is also a professional package that will provide
weekly updates. Without the Armchair Analysis data, you will not be able to use much of this code.

This code currently requires Python 2.7 and is not Python 3 compliant to our knowledge. Questions about the Python code can be directed to [Trey Causey](mailto:[email protected]).
This code currently requires Python 2.7 and or Python 3.4+.
Questions about the Python code can be directed to [Trey Causey](mailto:[email protected]).

Please note that none of the file operations are supported on Windows.

Expand All @@ -27,6 +28,10 @@ NOTE: If you are unable to purchase the Armchair Analysis data, [Ben Dilday](htt
- pandas
- scikit-learn

## Node.js package requirements
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this useful given that these are already enumerated in package.json (and will be installed on npm install)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thought is that parallels the list of python requirements above (which are listed in requirements.txt and installed on pip install) -- it wasn't clear to me from the code and the "This code currently requires Python 2.7" note above that Node.js was required to be installed in order to run -- since Python uses .json also now and there are no .js files in the main directory. But I could be persuaded.


- underscore

## Usage

Unzip the play-by-play data into a directory. Run the following code from the directory
Expand Down
15 changes: 13 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

import winprob as wp

try:
raw_input
except NameError:
raw_input = input # python3

import sys

def load_data():
click.echo('Loading data and setting up model.')
Expand All @@ -27,9 +33,14 @@ def load_data():
return data, model

def fg_make_prob(situation):
args = ' '.join("--%s=%r" % (key,val) for (key,val) in situation.iteritems())
if sys.version_info[0] >= 3:
args = ' '.join("--%s=%r" % (key,val) for (key,val) in situation.items())
else:
args = ' '.join("--%s=%r" % (key,val) for (key,val) in situation.iteritems())
model_fg = muterun_js('model-fg/model-fg.js', args)
return model_fg.stdout.split()[-1]
stdoutResults = model_fg.stdout
stdoutSplit = stdoutResults.split()
return stdoutSplit[-1]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about splitting this line into three -- I separated them for debugging (in order to figure out where some Py3 incorrect behavior was coming from) and forgot to restore to one line.

@click.command()
def run_bot():
Expand Down
7 changes: 4 additions & 3 deletions data_prep.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import division, print_function

import os

import sys
import click
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -68,8 +68,9 @@ def load_pbp(pbp_data_fname, games, remove_knees=False):
pbp = pbp[pbp.qtr <= 4]

# pid 183134 should have a value of 0 for min, but has "0:00"
pbp['min'] = pbp['min'].replace({'0:00': 0})
pbp['min'] = pbp['min'].astype(np.int64)
if sys.version_info[0] == 2:
pbp['min'] = pbp['min'].replace({'0:00': 0})
pbp['min'] = pbp['min'].astype(np.int64)

# Restrict to regular season games after 2000
pbp = pbp[pbp.gid.isin(games.index)]
Expand Down
9 changes: 8 additions & 1 deletion winprob.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import random
import sys

from sklearn.utils.validation import NotFittedError

from collections import OrderedDict

import plays as p
Expand Down Expand Up @@ -126,7 +128,12 @@ def generate_win_probabilities(situation, scenarios, model, data, **kwargs):
# Note there is more information in situation than just model features.

feature_vec = [val for key, val in situation.items() if key in features]
feature_vec = data['scaler'].transform(feature_vec)
try:
feature_vec = data['scaler'].transform(feature_vec)
except NotFittedError:
raise Exception("Sklearn reports that the instance is not yet fitted. " +
"This usually means that the version of python used to train " +
"the model is different from the version you are currently running.")

probs['pre_play_wp'] = model.predict_proba(feature_vec)[0][1]

Expand Down