def calc_all_predicted_points(
    gw_range,
    season,
    include_bonus=True,
    include_cards=True,
    include_saves=True,
    num_thread=4,
    tag="",
    dbsession=None,
):
    """
    Do the full prediction for players.
    """
    model_team = get_fitted_team_model(season,
                                       gameweek=min(gw_range),
                                       dbsession=dbsession)
    model_file = pkg_resources.resource_filename(
        "airsenal", "stan_model/player_forecasts.pkl")
    print("Loading pre-compiled player model from {}".format(model_file))
    with open(model_file, "rb") as f:
        model_player = pickle.load(f)

    df_player = get_all_fitted_player_models(model_player, season, gw_range[0])

    if include_bonus:
        df_bonus = fit_bonus_points(gameweek=gw_range[0], season=season)
    else:
        df_bonus = None
    if include_saves:
        df_saves = fit_save_points(gameweek=gw_range[0], season=season)
    else:
        df_saves = None
    if include_cards:
        df_cards = fit_card_points(gameweek=gw_range[0], season=season)
    else:
        df_cards = None

    players = list_players(season=season,
                           gameweek=gw_range[0],
                           dbsession=dbsession)

    if num_thread is not None and num_thread > 1:
        queue = Queue()
        procs = []
        for _ in range(num_thread):
            processor = Process(
                target=allocate_predictions,
                args=(
                    queue,
                    gw_range,
                    model_team,
                    df_player,
                    df_bonus,
                    df_saves,
                    df_cards,
                    season,
                    tag,
                    dbsession,
                ),
            )
            processor.daemon = True
            processor.start()
            procs.append(processor)

        for p in players:
            queue.put(p.player_id)
        for _ in range(num_thread):
            queue.put("DONE")

        for _, p in enumerate(procs):
            p.join()
    else:
        # single threaded
        for player in players:
            predictions = calc_predicted_points_for_player(
                player,
                model_team,
                df_player,
                df_bonus,
                df_saves,
                df_cards,
                season,
                gw_range=gw_range,
                tag=tag,
                dbsession=dbsession,
            )
            for p in predictions:
                dbsession.add(p)
        dbsession.commit()
        print("Finished adding predictions to db")
def calc_all_predicted_points(
        gw_range,
        season,
        include_bonus=True,
        include_cards=True,
        include_saves=True,
        num_thread=4,
        tag="",
        player_model=ConjugatePlayerModel(),
        dbsession=None,
):
    """
    Do the full prediction for players.
    """
    model_team = get_fitted_team_model(season,
                                       gameweek=min(gw_range),
                                       dbsession=dbsession)
    print("Calculating fixture score probabilities...")
    fixtures = get_fixtures_for_gameweek(gw_range,
                                         season=season,
                                         dbsession=dbsession)
    fixture_goal_probs = get_goal_probabilities_for_fixtures(
        fixtures, model_team, max_goals=MAX_GOALS)

    df_player = get_all_fitted_player_data(season,
                                           gw_range[0],
                                           model=player_model,
                                           dbsession=dbsession)

    if include_bonus:
        df_bonus = fit_bonus_points(gameweek=gw_range[0], season=season)
    else:
        df_bonus = None
    if include_saves:
        df_saves = fit_save_points(gameweek=gw_range[0], season=season)
    else:
        df_saves = None
    if include_cards:
        df_cards = fit_card_points(gameweek=gw_range[0], season=season)
    else:
        df_cards = None

    players = list_players(season=season,
                           gameweek=gw_range[0],
                           dbsession=dbsession)

    if num_thread is not None and num_thread > 1:
        queue = Queue()
        procs = []
        for _ in range(num_thread):
            processor = Process(
                target=allocate_predictions,
                args=(
                    queue,
                    gw_range,
                    fixture_goal_probs,
                    df_player,
                    df_bonus,
                    df_saves,
                    df_cards,
                    season,
                    tag,
                    dbsession,
                ),
            )
            processor.daemon = True
            processor.start()
            procs.append(processor)

        for p in players:
            queue.put(p.player_id)
        for _ in range(num_thread):
            queue.put("DONE")

        for _, p in enumerate(procs):
            p.join()
    else:
        # single threaded
        for player in players:
            predictions = calc_predicted_points_for_player(
                player,
                fixture_goal_probs,
                df_player,
                df_bonus,
                df_saves,
                df_cards,
                season,
                gw_range=gw_range,
                tag=tag,
                dbsession=dbsession,
            )
            for p in predictions:
                dbsession.add(p)
        dbsession.commit()
        print("Finished adding predictions to db")
예제 #3
0
def test_fit_saves():
    with test_past_data_session_scope() as ts:
        df_saves = fit_save_points(gameweek=1, season="1819", dbsession=ts)
        assert isinstance(df_saves, pd.Series)
        assert len(df_saves) > 0
        assert all(df_saves >= 0)
def calc_all_predicted_points(
    gw_range,
    season,
    include_bonus=True,
    include_cards=True,
    include_saves=True,
    num_thread=4,
    tag="",
    dbsession=None,
):
    """
    Do the full prediction for players.
    """
    model_team = get_fitted_team_model(
        season, gameweek=min(gw_range), dbsession=dbsession
    )
    model_file = pkg_resources.resource_filename(
        "airsenal", "stan_model/player_forecasts.pkl"
    )
    print("Loading pre-compiled player model from {}".format(model_file))
    with open(model_file, "rb") as f:
        model_player = pickle.load(f)

    #    model_player = get_player_model()

    if include_bonus:
        df_bonus = fit_bonus_points(gameweek=gw_range[0], season=season)
    else:
        df_bonus = None
    if include_saves:
        df_saves = fit_save_points(gameweek=gw_range[0], season=season)
    else:
        df_saves = None
    if include_cards:
        df_cards = fit_card_points(gameweek=gw_range[0], season=season)
    else:
        df_cards = None

    all_predictions = {}
    if num_thread:
        queue = Queue()
        procs = []
        for i in range(num_thread):
            processor = Process(
                target=allocate_predictions,
                args=(
                    queue,
                    gw_range,
                    model_team,
                    model_player,
                    df_bonus,
                    df_saves,
                    df_cards,
                    season,
                    tag,
                    dbsession,
                ),
            )
            processor.daemon = True
            processor.start()
            procs.append(processor)

        for pos in ["GK", "DEF", "MID", "FWD"]:
            queue.put(pos)
        for i in range(num_thread):
            queue.put("DONE")

        for i, p in enumerate(procs):
            p.join()
    else:
        # single threaded
        for pos in ["GK", "DEF", "MID", "FWD"]:
            predictions = calc_predicted_points_for_pos(
                pos=pos,
                team_model=model_team,
                player_model=model_player,
                df_bonus=df_bonus,
                df_saves=df_saves,
                df_cards=df_cards,
                season=season,
                gw_range=gw_range,
                tag=tag,
                dbsession=dbsession,
            )
            for k, v in predictions.items():
                for playerprediction in v:
                    dbsession.add(playerprediction)
        dbsession.commit()
        print("Finished adding predictions to db for {}".format(pos))