示例#1
0
def main():

    parser = argparse.ArgumentParser(
        description="fill db tables with recent scores and transactions")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--noattr",
                        help="don't update player attributes",
                        action="store_true")

    args = parser.parse_args()

    season = args.season
    do_attributes = not args.noattr

    with session_scope() as session:
        # see if any new players have been added
        num_new_players = update_players(season, session)

        # update player attributes (if requested)
        if not do_attributes and num_new_players > 0:
            print("New players added - enforcing update of attributes table")
            do_attributes = True

        if do_attributes:
            update_attributes(season, session)

        # update results and playerscores
        update_results(season, session)
        # update our squad
        update_transactions(season, session)
def main():

    parser = argparse.ArgumentParser(
        description="fill db tables with recent scores and transactions")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--tag",
                        help="identifying tag",
                        default="AIrsenal" + CURRENT_SEASON)
    parser.add_argument("--noattr",
                        help="don't update player attributes",
                        action="store_true")

    args = parser.parse_args()

    season = args.season
    tag = args.tag
    do_attributes = not args.noattr

    with session_scope() as session:

        last_in_db = get_last_gameweek_in_db(season, session)
        if not last_in_db:
            # no results in database for this season yet
            last_in_db = 0
        last_finished = get_last_finished_gameweek()

        # TODO update players table
        # TODO update fixtures table (e.g. in case of rescheduling)?

        if do_attributes:
            print("Updating attributes")
            fill_attributes_table_from_api(season,
                                           session,
                                           gw_start=last_in_db,
                                           gw_end=NEXT_GAMEWEEK)

        if NEXT_GAMEWEEK != 1:
            if last_finished > last_in_db:
                # need to update
                fill_results_from_api(last_in_db + 1, NEXT_GAMEWEEK, season,
                                      session)
                fill_playerscores_from_api(season, session, last_in_db + 1,
                                           NEXT_GAMEWEEK)
            else:
                print("Matches and player-scores already up-to-date")
            # now check transfers
            print("Checking team")
            db_players = sorted(
                get_current_players(season=season, dbsession=session))
            api_players = sorted(get_players_for_gameweek(last_finished))
            if db_players != api_players:
                update_team(season=season, session=session, verbose=True)
            else:
                print("Team is up-to-date")
        else:
            print("Skipping team and result updates - season hasn't started.")
示例#3
0
def main():
    """
    fill the player_prediction db table
    """
    parser = argparse.ArgumentParser(description="fill player predictions")
    parser.add_argument("--weeks_ahead",
                        help="how many weeks ahead to fill",
                        type=int)
    parser.add_argument("--gameweek_start",
                        help="first gameweek to look at",
                        type=int)
    parser.add_argument("--gameweek_end",
                        help="last gameweek to look at",
                        type=int)
    parser.add_argument("--ep_filename",
                        help="csv filename for FPL expected points")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--num_thread",
                        help="number of threads to parallelise over",
                        type=int)
    args = parser.parse_args()
    if args.weeks_ahead and (args.gameweek_start or args.gameweek_end):
        print(
            "Please specify either gameweek_start and gameweek_end, OR weeks_ahead"
        )
        raise RuntimeError("Inconsistent arguments")
    if args.weeks_ahead and not args.season == CURRENT_SEASON:
        print(
            "For past seasons, please specify gameweek_start and gameweek_end")
        raise RuntimeError("Inconsistent arguments")
    if args.weeks_ahead:
        gw_range = list(range(NEXT_GAMEWEEK, NEXT_GAMEWEEK + args.weeks_ahead))
    elif args.gameweek_start and args.gameweek_end:
        gw_range = list(range(args.gameweek_start, args.gameweek_end))
    elif args.gameweek_start:  # by default go three weeks ahead
        gw_range = list(range(args.gameweek_start, args.gameweek_start + 3))
    else:
        gw_range = list(range(NEXT_GAMEWEEK, NEXT_GAMEWEEK + 3))
    num_thread = args.num_thread if args.num_thread else None
    with session_scope() as session:
        tag = make_predictedscore_table(session,
                                        gw_range=gw_range,
                                        season=args.season,
                                        num_thread=num_thread)

        # print players with top predicted points
        get_top_predicted_points(
            gameweek=gw_range,
            tag=tag,
            season=args.season,
            dbsession=session,
            per_position=True,
            n_players=5,
        )
示例#4
0
def main():

    with session_scope() as session:
        make_team_table(dbsession=session)
        make_fixture_table(dbsession=session)
        make_result_table(dbsession=session)
        make_fifa_ratings_table(dbsession=session)

        make_player_table(dbsession=session)
        make_attributes_table(dbsession=session)
        make_playerscore_table(dbsession=session)

        fill_initial_squad(dbsession=session)

        print("DONE!")
示例#5
0
def rerun_predictions(season, gw_start, gw_end, weeks_ahead=3, num_thread=4):
    """
    Run the predictions each week for gw_start to gw_end in chosen season.
    """
    with session_scope() as session:
        for gw in range(gw_start, gw_end + 1):
            print("======== Running predictions for {} week {} ======".format(
                season, gw))
            tag_prefix = season + "_" + str(gw) + "_"
            make_predictedscore_table(
                gw_range=range(gw, gw + weeks_ahead),
                season=season,
                num_thread=num_thread,
                tag_prefix=tag_prefix,
                dbsession=session,
            )
示例#6
0
def main():

    parser = argparse.ArgumentParser(
        description="fill db tables with recent scores and transactions"
    )
    parser.add_argument(
        "--season", help="season, in format e.g. '1819'", default=CURRENT_SEASON
    )
    parser.add_argument(
        "--noattr", help="don't update player attributes", action="store_true"
    )
    parser.add_argument(
        "--fpl_team_id",
        help="specify fpl team id",
        type=int,
        required=False,
    )

    args = parser.parse_args()

    season = args.season
    do_attributes = not args.noattr
    fpl_team_id = args.fpl_team_id if args.fpl_team_id else None

    with session_scope() as session:
        # see if any new players have been added
        num_new_players = update_players(season, session)

        # update player attributes (if requested)
        if not do_attributes and num_new_players > 0:
            print("New players added - enforcing update of attributes table")
            do_attributes = True
        if do_attributes:
            update_attributes(season, session)

        # update fixtures (which may have been rescheduled)
        print("Updating fixture table...")
        fill_fixtures_from_api(season, session)
        # update results and playerscores
        update_results(season, session)
        # update our squad
        update_transactions(season, fpl_team_id, session)
示例#7
0
def main():
    parser = argparse.ArgumentParser(
        description="fill db tables with recent scores and transactions")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--noattr",
                        help="don't update player attributes",
                        action="store_true")
    parser.add_argument(
        "--fpl_team_id",
        help="specify fpl team id",
        type=int,
        required=False,
    )
    args = parser.parse_args()

    season = args.season
    do_attributes = not args.noattr
    fpl_team_id = args.fpl_team_id or None

    with session_scope() as session:
        update_db(season, do_attributes, fpl_team_id, session)
示例#8
0
def main():

    parser = argparse.ArgumentParser(description="Customise fpl team id")
    parser.add_argument("--fpl_team_id",
                        help="specify fpl team id",
                        type=int,
                        required=False)

    args = parser.parse_args()

    with session_scope() as session:
        make_team_table(dbsession=session)
        make_fixture_table(dbsession=session)
        make_result_table(dbsession=session)
        make_fifa_ratings_table(dbsession=session)

        make_player_table(dbsession=session)
        make_attributes_table(dbsession=session)
        make_playerscore_table(dbsession=session)

        fill_initial_squad(dbsession=session, fpl_team_id=args.fpl_team_id)

        print("DONE!")
示例#9
0
    player details JSON files) and the current season (from API)
    """
    if not seasons:
        seasons = get_past_seasons(3)
        seasons.append(CURRENT_SEASON)

    for season in seasons:
        if season == CURRENT_SEASON:
            continue
        input_path = os.path.join(
            os.path.dirname(__file__),
            "../data/player_details_{}.json".format(season))
        with open(input_path, "r") as f:
            input_data = json.load(f)

        fill_attributes_table_from_file(detail_data=input_data,
                                        season=season,
                                        dbsession=dbsession)

    # this season's data from the API
    if CURRENT_SEASON in seasons:
        fill_attributes_table_from_api(season=CURRENT_SEASON,
                                       dbsession=dbsession)

    dbsession.commit()


if __name__ == "__main__":
    with session_scope() as dbsession:
        make_attributes_table(dbsession=dbsession)
                    # don't need to add to session if updating pre-existing row
                    session.add(pa)

                break  # done this gameweek now


def make_attributes_table(session):
    """Create the player attributes table using the previous 3 seasons (from
    player details JSON files) and the current season (from API)
    """
    seasons = get_past_seasons(3)

    for season in seasons:
        input_path = os.path.join(
            os.path.dirname(__file__),
            "../data/player_details_{}.json".format(season))
        with open(input_path, "r") as f:
            input_data = json.load(f)

        fill_attributes_table_from_file(input_data, season, session)

    # this season's data from the API
    fill_attributes_table_from_api(CURRENT_SEASON, session)

    session.commit()


if __name__ == "__main__":
    with session_scope() as session:
        make_attributes_table(session)
def main():
    """
    fill the player_prediction db table
    """
    parser = argparse.ArgumentParser(description="fill player predictions")
    parser.add_argument("--weeks_ahead",
                        help="how many weeks ahead to fill",
                        type=int)
    parser.add_argument("--gameweek_start",
                        help="first gameweek to look at",
                        type=int)
    parser.add_argument("--gameweek_end",
                        help="last gameweek to look at",
                        type=int)
    parser.add_argument("--ep_filename",
                        help="csv filename for FPL expected points")
    parser.add_argument("--season",
                        help="season, in format e.g. '1819'",
                        default=CURRENT_SEASON)
    parser.add_argument("--num_thread",
                        help="number of threads to parallelise over",
                        type=int)
    parser.add_argument(
        "--no_bonus",
        help="don't include bonus points",
        action="store_true",
    )
    parser.add_argument(
        "--no_cards",
        help="don't include points lost to yellow and red cards",
        action="store_true",
    )
    parser.add_argument(
        "--no_saves",
        help="don't include save points for goalkeepers",
        action="store_true",
    )
    parser.add_argument(
        "--sampling",
        help="If set use fit the model using sampling with numpyro",
        action="store_true",
    )

    args = parser.parse_args()
    if args.weeks_ahead and (args.gameweek_start or args.gameweek_end):
        print(
            "Please specify either gameweek_start and gameweek_end, OR weeks_ahead"
        )
        raise RuntimeError("Inconsistent arguments")
    if args.weeks_ahead and args.season != CURRENT_SEASON:
        print(
            "For past seasons, please specify gameweek_start and gameweek_end")
        raise RuntimeError("Inconsistent arguments")
    if args.weeks_ahead:
        gw_range = get_gameweeks_array(args.weeks_ahead)
    elif args.gameweek_start and args.gameweek_end:
        gw_range = list(range(args.gameweek_start, args.gameweek_end))
    elif args.gameweek_start:  # by default go three weeks ahead
        gw_range = list(range(args.gameweek_start, args.gameweek_start + 3))
    else:
        gw_range = list(range(NEXT_GAMEWEEK, NEXT_GAMEWEEK + 3))
    num_thread = args.num_thread or None
    include_bonus = not args.no_bonus
    include_cards = not args.no_cards
    include_saves = not args.no_saves
    if args.sampling:
        player_model = NumpyroPlayerModel()
    else:
        player_model = ConjugatePlayerModel()

    set_multiprocessing_start_method(num_thread)

    with session_scope() as session:
        session.expire_on_commit = False

        tag = make_predictedscore_table(
            gw_range=gw_range,
            season=args.season,
            num_thread=num_thread,
            include_bonus=include_bonus,
            include_cards=include_cards,
            include_saves=include_saves,
            player_model=player_model,
            dbsession=session,
        )

        # print players with top predicted points
        get_top_predicted_points(
            gameweek=gw_range,
            tag=tag,
            season=args.season,
            per_position=True,
            n_players=5,
            dbsession=session,
        )
示例#12
0
def run_pipeline(
    num_thread,
    weeks_ahead,
    fpl_team_id,
    clean,
    apply_transfers,
    wildcard_week,
    free_hit_week,
    triple_captain_week,
    bench_boost_week,
):
    """
    Run the full pipeline, from setting up the database and filling
    with players, teams, fixtures, and results (if it didn't already exist),
    then updating with the latest info, then running predictions to get a
    score estimate for every player, and finally optimization, to choose
    the best squad.
    """
    if fpl_team_id is None:
        fpl_team_id = fetcher.FPL_TEAM_ID
    print("Running for FPL Team ID {}".format(fpl_team_id))
    if not num_thread:
        num_thread = multiprocessing.cpu_count()
    set_multiprocessing_start_method(num_thread)

    with session_scope() as dbsession:
        if check_clean_db(clean, dbsession):
            click.echo("Setting up Database..")
            setup_ok = setup_database(fpl_team_id, dbsession)
            if not setup_ok:
                raise RuntimeError("Problem setting up initial db")
            click.echo("Database setup complete..")
            update_attr = False
        else:
            click.echo("Found pre-existing AIrsenal database.")
            update_attr = True
        click.echo("Updating database..")
        update_ok = update_database(fpl_team_id, update_attr, dbsession)
        if not update_ok:
            raise RuntimeError("Problem updating db")
        click.echo("Database update complete..")
        click.echo("Running prediction..")
        predict_ok = run_prediction(num_thread, weeks_ahead, dbsession)
        if not predict_ok:
            raise RuntimeError("Problem running prediction")
        click.echo("Prediction complete..")
        if NEXT_GAMEWEEK == 1:
            click.echo("Generating a squad..")
            new_squad_ok = run_make_squad(weeks_ahead, fpl_team_id, dbsession)
            if not new_squad_ok:
                raise RuntimeError("Problem creating a new squad")
        else:
            click.echo("Running optimization..")
            chips_played = setup_chips(wildcard_week, free_hit_week,
                                       triple_captain_week, bench_boost_week)
            opt_ok = run_optimize_squad(num_thread, weeks_ahead, fpl_team_id,
                                        dbsession, chips_played)
            if not opt_ok:
                raise RuntimeError("Problem running optimization")

        click.echo("Optimization complete..")
        if apply_transfers:
            click.echo("Applying suggested transfers...")
            transfers_ok = make_transfers(fpl_team_id)
            if not transfers_ok:
                raise RuntimeError("Problem applying the transfers")
            click.echo("Setting Lineup...")
            lineup_ok = set_starting_11(fpl_team_id)
            if not lineup_ok:
                raise RuntimeError("Problem setting the lineup")
        click.echo("Pipeline finished OK!")