def test_rankings_integration(self, *args, **kwargs): flexmock(utils) \ .should_receive('_find_year_for_season') \ .and_return(YEAR) rankings = Rankings() assert rankings.current_extended == self.results_extended assert rankings.current == self.results assert rankings.complete == self.results_complete
def test_invalid_default_year_reverts_to_previous_year( self, *args, **kwargs): flexmock(utils) \ .should_receive('_find_year_for_season') \ .and_return(2019) rankings = Rankings() assert rankings.current_extended == self.results_extended assert rankings.current == self.results assert rankings.complete == self.results_complete
def projections(): home = request.args.get('home') away = request.args.get('away') if home is None and away is None: today = dt.today() today_start = today.replace(hour=0, minute=0, second=0, microsecond=0) today_end = today_start + timedelta(1) games = db.session.query(Game).filter(Game.date > today_start, Game.date < today_end).all() all_games = db.session.query(Game).all() if len(all_games) > 9500: db.session.query(Game).delete() db.session.commit() # If today's games have not been retrieved yet then do so from sports # And get the projected score from the data science API if len(games) == 0: games_today = Boxscores(dt.today()) games_today = games_today.games rankings = Rankings() rankings = rankings.current for date in games_today.keys(): for game in games_today[date]: home_team = db.session.query(Team).filter( Team.name == game["home_name"]).first() away_team = db.session.query(Team).filter( Team.name == game["away_name"]).first() if home_team is None: home_team = Team(id=game["home_abbr"], name=game["home_name"]) db.session.add(home_team) try: db.session.commit() home_team = db.session.query(Team).filter( Team.name == game["home_name"]).first() except: db.session.rollback() elif away_team is None: away_team = Team(id=game["away_abbr"], name=game["away_name"]) db.session.add(away_team) try: db.session.commit() away_team = db.session.query(Team).filter( Team.name == game["away_name"]).first() except: db.session.rollback() print("000ooof") if home_team and away_team: data = { "year": dt.today().year, "month": dt.today().month, "day": dt.today().day, "home_name": home_team.name, "home_rank": float(rankings.get(home_team.id.lower(), 0)), "away_name": away_team.name, "away_rank": float(rankings.get(away_team.id.lower(), 0)) } res = requests.post("http://ncaab.herokuapp.com/", data=json.dumps(data)) res = res.json() home_projection = res["home_score"] away_projection = res["away_score"] new_game = Game(date=date, home=home_team.id, away=away_team.id, home_projection=home_projection, away_projection=away_projection) db.session.add(new_game) games.append(new_game) # TODO: Test this out... # Update yesterday's games with the scores # yesterday_start = today_start - timedelta(1) # games_yesterday = Boxscores(yesterday_start, today_start) # games_yesterday = games_yesterday.games # for date in games_yesterday.keys(): # for game in games_yesterday[date]: # home_team = db.session.query(Team).filter( # Team.name == game["home_name"]).first() # away_team = db.session.query(Team).filter( # Team.name == game["away_name"]).first() # print(home_team, away_team) # applicable_game = db.session.query(Game).filter( # Game.date > yesterday_start, Game.date < today_start, Game.home == home_team.id, Game.away == away_team.id).first() # print(applicable_game) # applicable_game.home_score = game["home_score"] # applicable_game.away_score = game["away_score"] try: db.session.commit() except: db.session.rollback() games_without_names = [g.serialize() for g in games] games_with_names = [] for game in games_without_names: home_team = db.session.query(Team).filter( Team.id == game["home_id"]).first() away_team = db.session.query(Team).filter( Team.id == game["away_id"]).first() print("oof") game["home_name"] = home_team.name game["away_name"] = away_team.name games_with_names.append(game) return jsonify(games_with_names) elif home is None or away is None: return ( "'home' and 'away' are necessary query parameters when not using the 'all' query parameter" ) else: rankings = Rankings() rankings = rankings.current print(home) print(away) home_team = db.session.query(Team).filter(Team.id == home).first() away_team = db.session.query(Team).filter(Team.id == away).first() print("ooooof") data = { "year": dt.today().year, "month": dt.today().month, "day": dt.today().day, "home_name": home_team.name, "home_rank": float(rankings.get(home_team.id.lower(), 0)), "away_name": away_team.name, "away_rank": float(rankings.get(away_team.id.lower(), 0)) } res = requests.post("http://ncaab.herokuapp.com/", data=json.dumps(data)) res = res.json() home_projection = res["home_score"] away_projection = res["away_score"] print("ummmmm") return jsonify({ 'home_id': home_team.id, 'away_id': away_team.id, 'home_name': home_team.name, 'away_name': away_team.name, 'home_projection': home_projection, 'away_projection': away_projection })
def test_rankings_integration_bad_url(self, *args, **kwargs): with pytest.raises(ValueError): rankings = Rankings('BAD')
def build_dataset(data): frames = [] for i, match in enumerate(iglob('matches/*/*')): if not i % 1000: data = pd.concat([data] + frames) frames = [] continue frames.append(pd.read_pickle(match)) return data def process_dataset(data): data.drop_duplicates(inplace=True) data = filter_stats(data) data = data.dropna() data['home_free_throw_percentage'].fillna(0, inplace=True) data['away_free_throw_percentage'].fillna(0, inplace=True) data['points_difference'] = data['home_points'] - data['away_points'] data.to_pickle(DATASET_NAME) if __name__ == '__main__': data = pd.read_pickle(DATASET_NAME) teams = Teams() rankings = Rankings().current pull_match_stats(teams, rankings, data) data = build_dataset(data) process_dataset(data)
def test_rankings_string_representation(self, *args, **kwargs): rankings = Rankings() assert rankings.__repr__() == 'NCAAB Rankings'
def test_rankings_integration(self, *args, **kwargs): results_extended = [{ 'abbreviation': 'virginia', 'name': 'Virginia', 'rank': 1, 'week': 19, 'date': 'Final', 'previous': '1', 'change': 0 }, { 'abbreviation': 'villanova', 'name': 'Villanova', 'rank': 2, 'week': 19, 'date': 'Final', 'previous': '2', 'change': 0 }, { 'abbreviation': 'xavier', 'name': 'Xavier', 'rank': 3, 'week': 19, 'date': 'Final', 'previous': '3', 'change': 0 }, { 'abbreviation': 'kansas', 'name': 'Kansas', 'rank': 4, 'week': 19, 'date': 'Final', 'previous': '9', 'change': 5 }, { 'abbreviation': 'michigan-state', 'name': 'Michigan State', 'rank': 5, 'week': 19, 'date': 'Final', 'previous': '4', 'change': -1 }, { 'abbreviation': 'cincinnati', 'name': 'Cincinnati', 'rank': 6, 'week': 19, 'date': 'Final', 'previous': '8', 'change': 2 }, { 'abbreviation': 'michigan', 'name': 'Michigan', 'rank': 7, 'week': 19, 'date': 'Final', 'previous': '7', 'change': 0 }, { 'abbreviation': 'gonzaga', 'name': 'Gonzaga', 'rank': 8, 'week': 19, 'date': 'Final', 'previous': '6', 'change': -2 }, { 'abbreviation': 'duke', 'name': 'Duke', 'rank': 9, 'week': 19, 'date': 'Final', 'previous': '5', 'change': -4 }, { 'abbreviation': 'north-carolina', 'name': 'North Carolina', 'rank': 10, 'week': 19, 'date': 'Final', 'previous': '12', 'change': 2 }, { 'abbreviation': 'purdue', 'name': 'Purdue', 'rank': 11, 'week': 19, 'date': 'Final', 'previous': '10', 'change': -1 }, { 'abbreviation': 'arizona', 'name': 'Arizona', 'rank': 12, 'week': 19, 'date': 'Final', 'previous': '15', 'change': 3 }, { 'abbreviation': 'tennessee', 'name': 'Tennessee', 'rank': 13, 'week': 19, 'date': 'Final', 'previous': '13', 'change': 0 }, { 'abbreviation': 'texas-tech', 'name': 'Texas Tech', 'rank': 14, 'week': 19, 'date': 'Final', 'previous': '14', 'change': 0 }, { 'abbreviation': 'west-virginia', 'name': 'West Virginia', 'rank': 15, 'week': 19, 'date': 'Final', 'previous': '18', 'change': 3 }, { 'abbreviation': 'wichita-state', 'name': 'Wichita State', 'rank': 16, 'week': 19, 'date': 'Final', 'previous': '11', 'change': -5 }, { 'abbreviation': 'ohio-state', 'name': 'Ohio State', 'rank': 17, 'week': 19, 'date': 'Final', 'previous': '17', 'change': 0 }, { 'abbreviation': 'kentucky', 'name': 'Kentucky', 'rank': 18, 'week': 19, 'date': 'Final', 'previous': '', 'change': 0 }, { 'abbreviation': 'auburn', 'name': 'Auburn', 'rank': 19, 'week': 19, 'date': 'Final', 'previous': '16', 'change': -3 }, { 'abbreviation': 'clemson', 'name': 'Clemson', 'rank': 20, 'week': 19, 'date': 'Final', 'previous': '19', 'change': -1 }, { 'abbreviation': 'houston', 'name': 'Houston', 'rank': 21, 'week': 19, 'date': 'Final', 'previous': '21', 'change': 0 }, { 'abbreviation': 'miami-fl', 'name': 'Miami (FL)', 'rank': 22, 'week': 19, 'date': 'Final', 'previous': '24', 'change': 2 }, { 'abbreviation': 'florida', 'name': 'Florida', 'rank': 23, 'week': 19, 'date': 'Final', 'previous': '23', 'change': 0 }, { 'abbreviation': 'nevada', 'name': 'Nevada', 'rank': 24, 'week': 19, 'date': 'Final', 'previous': '22', 'change': -2 }, { 'abbreviation': 'saint-marys-ca', 'name': "Saint Mary's (CA)", 'rank': 25, 'week': 19, 'date': 'Final', 'previous': '20', 'change': -5 }] results = { 'virginia': 1, 'villanova': 2, 'xavier': 3, 'kansas': 4, 'michigan-state': 5, 'cincinnati': 6, 'michigan': 7, 'gonzaga': 8, 'duke': 9, 'north-carolina': 10, 'purdue': 11, 'arizona': 12, 'tennessee': 13, 'texas-tech': 14, 'west-virginia': 15, 'wichita-state': 16, 'ohio-state': 17, 'kentucky': 18, 'auburn': 19, 'clemson': 20, 'houston': 21, 'miami-fl': 22, 'florida': 23, 'nevada': 24, 'saint-marys-ca': 25 } results_complete = { 19: [{ 'abbreviation': 'virginia', 'name': 'Virginia', 'rank': 1, 'week': 19, 'date': 'Final', 'previous': '1', 'change': 0 }, { 'abbreviation': 'villanova', 'name': 'Villanova', 'rank': 2, 'week': 19, 'date': 'Final', 'previous': '2', 'change': 0 }, { 'abbreviation': 'xavier', 'name': 'Xavier', 'rank': 3, 'week': 19, 'date': 'Final', 'previous': '3', 'change': 0 }, { 'abbreviation': 'kansas', 'name': 'Kansas', 'rank': 4, 'week': 19, 'date': 'Final', 'previous': '9', 'change': 5 }, { 'abbreviation': 'michigan-state', 'name': 'Michigan State', 'rank': 5, 'week': 19, 'date': 'Final', 'previous': '4', 'change': -1 }, { 'abbreviation': 'cincinnati', 'name': 'Cincinnati', 'rank': 6, 'week': 19, 'date': 'Final', 'previous': '8', 'change': 2 }, { 'abbreviation': 'michigan', 'name': 'Michigan', 'rank': 7, 'week': 19, 'date': 'Final', 'previous': '7', 'change': 0 }, { 'abbreviation': 'gonzaga', 'name': 'Gonzaga', 'rank': 8, 'week': 19, 'date': 'Final', 'previous': '6', 'change': -2 }, { 'abbreviation': 'duke', 'name': 'Duke', 'rank': 9, 'week': 19, 'date': 'Final', 'previous': '5', 'change': -4 }, { 'abbreviation': 'north-carolina', 'name': 'North Carolina', 'rank': 10, 'week': 19, 'date': 'Final', 'previous': '12', 'change': 2 }, { 'abbreviation': 'purdue', 'name': 'Purdue', 'rank': 11, 'week': 19, 'date': 'Final', 'previous': '10', 'change': -1 }, { 'abbreviation': 'arizona', 'name': 'Arizona', 'rank': 12, 'week': 19, 'date': 'Final', 'previous': '15', 'change': 3 }, { 'abbreviation': 'tennessee', 'name': 'Tennessee', 'rank': 13, 'week': 19, 'date': 'Final', 'previous': '13', 'change': 0 }, { 'abbreviation': 'texas-tech', 'name': 'Texas Tech', 'rank': 14, 'week': 19, 'date': 'Final', 'previous': '14', 'change': 0 }, { 'abbreviation': 'west-virginia', 'name': 'West Virginia', 'rank': 15, 'week': 19, 'date': 'Final', 'previous': '18', 'change': 3 }, { 'abbreviation': 'wichita-state', 'name': 'Wichita State', 'rank': 16, 'week': 19, 'date': 'Final', 'previous': '11', 'change': -5 }, { 'abbreviation': 'ohio-state', 'name': 'Ohio State', 'rank': 17, 'week': 19, 'date': 'Final', 'previous': '17', 'change': 0 }, { 'abbreviation': 'kentucky', 'name': 'Kentucky', 'rank': 18, 'week': 19, 'date': 'Final', 'previous': '', 'change': 0 }, { 'abbreviation': 'auburn', 'name': 'Auburn', 'rank': 19, 'week': 19, 'date': 'Final', 'previous': '16', 'change': -3 }, { 'abbreviation': 'clemson', 'name': 'Clemson', 'rank': 20, 'week': 19, 'date': 'Final', 'previous': '19', 'change': -1 }, { 'abbreviation': 'houston', 'name': 'Houston', 'rank': 21, 'week': 19, 'date': 'Final', 'previous': '21', 'change': 0 }, { 'abbreviation': 'miami-fl', 'name': 'Miami (FL)', 'rank': 22, 'week': 19, 'date': 'Final', 'previous': '24', 'change': 2 }, { 'abbreviation': 'florida', 'name': 'Florida', 'rank': 23, 'week': 19, 'date': 'Final', 'previous': '23', 'change': 0 }, { 'abbreviation': 'nevada', 'name': 'Nevada', 'rank': 24, 'week': 19, 'date': 'Final', 'previous': '22', 'change': -2 }, { 'abbreviation': 'saint-marys-ca', 'name': "Saint Mary's (CA)", 'rank': 25, 'week': 19, 'date': 'Final', 'previous': '20', 'change': -5 }], 18: [{ 'abbreviation': 'virginia', 'name': 'Virginia', 'rank': 1, 'week': 18, 'date': '2018-03-05', 'previous': '1', 'change': 0 }, { 'abbreviation': 'villanova', 'name': 'Villanova', 'rank': 2, 'week': 18, 'date': '2018-03-05', 'previous': '4', 'change': 2 }, { 'abbreviation': 'xavier', 'name': 'Xavier', 'rank': 3, 'week': 18, 'date': '2018-03-05', 'previous': '3', 'change': 0 }, { 'abbreviation': 'michigan-state', 'name': 'Michigan State', 'rank': 4, 'week': 18, 'date': '2018-03-05', 'previous': '2', 'change': -2 }, { 'abbreviation': 'duke', 'name': 'Duke', 'rank': 5, 'week': 18, 'date': '2018-03-05', 'previous': '5', 'change': 0 }, { 'abbreviation': 'gonzaga', 'name': 'Gonzaga', 'rank': 6, 'week': 18, 'date': '2018-03-05', 'previous': '7', 'change': 1 }, { 'abbreviation': 'michigan', 'name': 'Michigan', 'rank': 7, 'week': 18, 'date': '2018-03-05', 'previous': '15', 'change': 8 }, { 'abbreviation': 'cincinnati', 'name': 'Cincinnati', 'rank': 8, 'week': 18, 'date': '2018-03-05', 'previous': '10', 'change': 2 }, { 'abbreviation': 'kansas', 'name': 'Kansas', 'rank': 9, 'week': 18, 'date': '2018-03-05', 'previous': '6', 'change': -3 }, { 'abbreviation': 'purdue', 'name': 'Purdue', 'rank': 10, 'week': 18, 'date': '2018-03-05', 'previous': '8', 'change': -2 }, { 'abbreviation': 'wichita-state', 'name': 'Wichita State', 'rank': 11, 'week': 18, 'date': '2018-03-05', 'previous': '11', 'change': 0 }, { 'abbreviation': 'north-carolina', 'name': 'North Carolina', 'rank': 12, 'week': 18, 'date': '2018-03-05', 'previous': '9', 'change': -3 }, { 'abbreviation': 'tennessee', 'name': 'Tennessee', 'rank': 13, 'week': 18, 'date': '2018-03-05', 'previous': '16', 'change': 3 }, { 'abbreviation': 'texas-tech', 'name': 'Texas Tech', 'rank': 14, 'week': 18, 'date': '2018-03-05', 'previous': '12', 'change': -2 }, { 'abbreviation': 'arizona', 'name': 'Arizona', 'rank': 15, 'week': 18, 'date': '2018-03-05', 'previous': '19', 'change': 4 }, { 'abbreviation': 'auburn', 'name': 'Auburn', 'rank': 16, 'week': 18, 'date': '2018-03-05', 'previous': '14', 'change': -2 }, { 'abbreviation': 'ohio-state', 'name': 'Ohio State', 'rank': 17, 'week': 18, 'date': '2018-03-05', 'previous': '13', 'change': -4 }, { 'abbreviation': 'west-virginia', 'name': 'West Virginia', 'rank': 18, 'week': 18, 'date': '2018-03-05', 'previous': '20', 'change': 2 }, { 'abbreviation': 'clemson', 'name': 'Clemson', 'rank': 19, 'week': 18, 'date': '2018-03-05', 'previous': '18', 'change': -1 }, { 'abbreviation': 'saint-marys-ca', 'name': "Saint Mary's (CA)", 'rank': 20, 'week': 18, 'date': '2018-03-05', 'previous': '22', 'change': 2 }, { 'abbreviation': 'houston', 'name': 'Houston', 'rank': 21, 'week': 18, 'date': '2018-03-05', 'previous': '25', 'change': 4 }, { 'abbreviation': 'nevada', 'name': 'Nevada', 'rank': 22, 'week': 18, 'date': '2018-03-05', 'previous': '21', 'change': -1 }, { 'abbreviation': 'florida', 'name': 'Florida', 'rank': 23, 'week': 18, 'date': '2018-03-05', 'previous': '', 'change': 0 }, { 'abbreviation': 'miami-fl', 'name': 'Miami (FL)', 'rank': 24, 'week': 18, 'date': '2018-03-05', 'previous': '', 'change': 0 }, { 'abbreviation': 'rhode-island', 'name': 'Rhode Island', 'rank': 25, 'week': 18, 'date': '2018-03-05', 'previous': '17', 'change': -8 }] } flexmock(utils) \ .should_receive('_find_year_for_season') \ .and_return(YEAR) rankings = Rankings() assert rankings.current_extended == results_extended assert rankings.current == results assert rankings.complete == results_complete
def main(): args = arguments() teams = Teams() rankings = Rankings().current predictor = Predictor() initiate_algorithm(args, predictor, teams, rankings)
import sys, os sys.path.append(os.path.dirname(os.path.dirname(sys.path[0]))) from sportsreference.ncaab.conferences import Conferences from sportsreference.ncaab.rankings import Rankings from sportsreference.ncaab.teams import Teams for team in Teams(): print(team.name) for player in team.roster.players: print(player.name) for game in team.schedule: print(game.dataframe) print(game.dataframe_extended) conferences = Conferences() print(conferences.conferences) print(conferences.team_conference) rankings = Rankings() print(rankings.current) print(rankings.current_extended) print(rankings.complete)
def getSchools(): rankings = Rankings() return list(rankings.current.keys())