Skip to content

Commit

Permalink
Merge pull request #311 from alan-turing-institute/develop
Browse files Browse the repository at this point in the history
Update AIrsenal to v0.3.0
  • Loading branch information
jack89roberts authored Jan 15, 2021
2 parents 28cfdd8 + c71070d commit 5f7b745
Show file tree
Hide file tree
Showing 51 changed files with 5,872 additions and 2,535 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ jobs:

script:
- pytest
- flake8
- black --check .
2 changes: 1 addition & 1 deletion airsenal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# AIrsenal package version. When merging changes to master:
# - increment 2nd digit for new features
# - increment 3rd digit for bug fixes
__version__ = "0.2.0"
__version__ = "0.3.0"

# Cross-platform temporary directory
if os.name == "posix":
Expand Down
7 changes: 2 additions & 5 deletions airsenal/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def get_session_id():
return "DEFAULT_SESSION_ID"


## Use a flask blueprint rather than creating the app directly
## so that we can also make a test app
# Use a flask blueprint rather than creating the app directly
# so that we can also make a test app

blueprint = Blueprint("airsenal", __name__)

Expand Down Expand Up @@ -196,9 +196,6 @@ def session_budget():
return create_response(get_session_budget(get_session_id()))


###########################################


def create_app(name=__name__):
app = Flask(name)
app.config["SESSION_TYPE"] = "filesystem"
Expand Down
11 changes: 6 additions & 5 deletions airsenal/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
API_SESSION_ID = "TESTSESSION"

testengine_dummy = create_engine("sqlite:///{}/test.db".format(TMPDIR))
# testengine_past = create_engine("sqlite:////Users/nbarlow/AIrsenal/airsenal/tests/testdata/testdata_1718_1819.db")
# .format(os.path.dirname(__file__)))
# testengine_past = create_engine(
# "sqlite:////Users/nbarlow/AIrsenal/airsenal/tests/testdata/testdata_1718_1819.db"
# ).format(os.path.dirname(__file__)))

testengine_past = create_engine(
"sqlite:///{}/tests/testdata/testdata_1718_1819.db".format(
Expand All @@ -38,7 +39,7 @@ def test_session_scope():
try:
yield testsession
testsession.commit()
except:
except Exception:
testsession.rollback()
raise
finally:
Expand All @@ -53,7 +54,7 @@ def test_past_data_session_scope():
try:
yield testsession
testsession.commit()
except:
except Exception:
testsession.rollback()
raise
finally:
Expand Down Expand Up @@ -94,7 +95,7 @@ def fill_players():
print("Filling {} {}".format(i, n))
try:
ts.add(p)
except:
except Exception:
print("Error adding {} {}".format(i, n))
# now fill player_attributes
if i % 15 < 2:
Expand Down
3 changes: 2 additions & 1 deletion airsenal/framework/FPL_scoring_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

def get_appearance_points(minutes):
"""
get points for being on the pitch at all, and more for being on for most of the match.
get points for being on the pitch at all, and more for being on
for most of the match.
"""
app_points = 0.0
if minutes > 0:
Expand Down
17 changes: 7 additions & 10 deletions airsenal/framework/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ def combine_player_info(player_id, dbsession=DBSESSION):
info_dict["player_name"] = p.name
team = p.team(CURRENT_SEASON, NEXT_GAMEWEEK)
info_dict["team"] = team
## get recent scores for the player
# get recent scores for the player
rs = get_recent_scores_for_player(p, dbsession=dbsession)
recent_scores = []
for k, v in rs.items():
recent_scores.append({"gameweek": k, "score": v})
info_dict["recent_scores"] = recent_scores
## get upcoming fixtures
# get upcoming fixtures
fixtures = get_fixtures_for_player(p, dbsession=dbsession)[:3]
info_dict["fixtures"] = []
for f in fixtures:
Expand Down Expand Up @@ -138,7 +138,7 @@ def remove_session_player(player_id, session_id, dbsession=DBSESSION):
player_id = int(player_id)
if player_id not in pids: # player not there
return False
st = (
(
dbsession.query(SessionSquad)
.filter_by(session_id=session_id, player_id=player_id)
.delete()
Expand Down Expand Up @@ -185,9 +185,7 @@ def set_session_budget(budget, session_id, dbsession=DBSESSION):
then enter a new row
"""
print("Deleting old budget")
old_budget = (
dbsession.query(SessionBudget).filter_by(session_id=session_id).delete()
)
dbsession.query(SessionBudget).filter_by(session_id=session_id).delete()
dbsession.commit()
print("Setting budget for {} to {}".format(session_id, budget))
sb = SessionBudget(session_id=session_id, budget=budget)
Expand Down Expand Up @@ -294,7 +292,7 @@ def best_transfer_suggestions(n_transfer, session_id, dbsession=DBSESSION):
Use our predicted playerscores to suggest the best transfers.
"""
n_transfer = int(n_transfer)
if not n_transfer in range(1, 3):
if n_transfer not in range(1, 3):
raise RuntimeError("Need to choose 1 or 2 transfers")
if not validate_session_squad(session_id, dbsession):
raise RuntimeError("Cannot suggest transfer without complete squad")
Expand All @@ -307,9 +305,8 @@ def best_transfer_suggestions(n_transfer, session_id, dbsession=DBSESSION):
if not added_ok:
raise RuntimeError("Cannot add player {}".format(p))
pred_tag = get_latest_prediction_tag()
gw = NEXT_GAMEWEEK
if n_transfer == 1:
new_squad, pid_out, pid_in = make_optimum_single_transfer(t, pred_tag)
_, pid_out, pid_in = make_optimum_single_transfer(t, pred_tag)
elif n_transfer == 2:
new_squad, pid_out, pid_in = make_optimum_double_transfer(t, pred_tag)
_, pid_out, pid_in = make_optimum_double_transfer(t, pred_tag)
return {"transfers_out": pid_out, "transfers_in": pid_in}
2 changes: 1 addition & 1 deletion airsenal/framework/aws_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def get_suggestions_string():
"""
Query the suggested_transfers table and format the output.
"""
## first need to download sqlite file from S3
# first need to download sqlite file from S3

result = download_sqlite_file()
if result != "OK":
Expand Down
21 changes: 12 additions & 9 deletions airsenal/framework/bpl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

from airsenal.framework.schema import Result, FifaTeamRating
from airsenal.framework.utils import (
CURRENT_TEAMS,
get_fixtures_for_gameweek,
is_future_gameweek,
)
from airsenal.framework.season import CURRENT_SEASON, get_teams_for_season

from airsenal.framework.season import (
CURRENT_SEASON,
CURRENT_TEAMS,
get_teams_for_season,
)

np.random.seed(42)

Expand All @@ -35,7 +37,8 @@ def get_result_df(season, gameweek, dbsession):
s.away_score,
]
for s in dbsession.query(Result).all()
if not is_future_gameweek(
if s.fixture
and not is_future_gameweek(
s.fixture.season,
s.fixture.gameweek,
current_season=season,
Expand Down Expand Up @@ -77,15 +80,15 @@ def create_and_fit_team_model(df, df_X, teams=CURRENT_TEAMS):
model_team.fit()
# check if each team is known to the model, and if not, add it using FIFA rankings
for team in teams:
if not team in model_team.team_indices.keys():
if team not in model_team.team_indices.keys():
try:
strvals = df_X.loc[
(df_X["team"] == team), ["att", "mid", "defn", "ovr"]
].values
intvals = [int(v) for v in strvals[0]]
model_team.add_new_team(team, intvals)
print("Adding new team {} with covariates".format(team))
except:
except Exception:
model_team.add_new_team(team)
print("Adding new team {} without covariates".format(team))

Expand All @@ -107,9 +110,9 @@ def get_fitted_team_model(season, gameweek, dbsession):

def fixture_probabilities(gameweek, season=CURRENT_SEASON, dbsession=None):
"""
Returns probabilities for all fixtures in a given gameweek and season, as a data frame with a row
for each fixture and columns being fixture_id, home_team, away_team, home_win_probability,
draw_probability, away_win_probability.
Returns probabilities for all fixtures in a given gameweek and season, as a data
frame with a row for each fixture and columns being fixture_id, home_team,
away_team, home_win_probability, draw_probability, away_win_probability.
"""
model_team = get_fitted_team_model(season, gameweek, dbsession)
fixture_probabilities_list = []
Expand Down
Loading

0 comments on commit 5f7b745

Please sign in to comment.