Example #1
0
# print '1:1'
# print serv
# print retn
#
# print np.nanmean(serv)
# print np.nanmean(retn)
#
# print np.nanstd(serv)
# print np.nanstd(retn)
#

#
Player('Andy Murray')
Player('Novak Djokovic')

Player.calculate_all_ts()

for p in Player.cached_players:
    if -1 in Player.cached_players[p].game_baseline.keys():
        if Player.cached_players[p].match_baseline.serv_counts > 10:
            if Player.cached_players[p].game_baseline[-1].serv_counts > 50:
                if Player.cached_players[
                        p].match_baseline.serv_mean < Player.cached_players[
                            p].game_baseline[-1].serv_mean:
                    if Player.cached_players[
                            p].match_baseline.retn_mean < Player.cached_players[
                                p].game_baseline[-1].retn_mean:
                        if Player.cached_players[
                                p].match_impact.serv_mean > Player.cached_players[
                                    p].game_impact[-1].serv_mean:
                            if Player.cached_players[
Example #2
0
def get_multi_player():
    start_year = int(request.args.get('start_year'))
    end_year = int(request.args.get('end_year'))
    surface = request.args.get('surface')
    two_step = request.args.get('two_step')
    if (start_year != Player.start_year) or (end_year != Player.end_year) or (surface != Player.surface):
        Player.cached_players = {}
        Player.start_year = start_year
        Player.end_year = end_year
        Player.surface = surface
    player_names = request.args.get('player_name')
    player_names = player_names.split(',')
    metrics_level = request.args.get('metrics_level')
    baseline_impact = request.args.get('baseline_impact')
    serve_return = request.args.get('serve_return')
    players = {}
    for player_name in player_names:
        players[player_name] = Player(player_name)

    if two_step == 'true':
        Player.calculate_all_ts()

    all_players_stats = {}

    # grand 5 sets missing

    if metrics_level == 'set':
        for player in players:

            if two_step == 'true':
                if baseline_impact == 'baseline':
                    player_stat = players[player].set_baseline_3_ts
                    player_match_stat = players[player].match_baseline_ts
                elif baseline_impact == 'impact':
                    player_stat = players[player].set_impact_3_ts
                    player_match_stat = players[player].match_impact_ts
            else:
                if baseline_impact == 'baseline':
                    player_stat = players[player].set_baseline_3
                    player_match_stat = players[player].match_baseline
                elif baseline_impact == 'impact':
                    player_stat = players[player].set_impact_3
                    player_match_stat = players[player].match_impact


            if serve_return == 'serve':
                player_stats = [player_match_stat.serv_mean,
                                player_stat['0:0'].serv_mean, player_stat['1:0'].serv_mean, player_stat['0:1'].serv_mean,
                                player_stat['1:1'].serv_mean]
                player_stats_ci = [player_match_stat.serv_std * stats.t._ppf(0.9,player_match_stat.serv_counts),
                                   player_stat['0:0'].serv_std * stats.t._ppf(0.9,player_stat['0:0'].serv_counts),
                                   player_stat['1:0'].serv_std * stats.t._ppf(0.9,player_stat['1:0'].serv_counts),
                                   player_stat['0:1'].serv_std * stats.t._ppf(0.9,player_stat['0:1'].serv_counts),
                                   player_stat['1:1'].serv_std * stats.t._ppf(0.9,player_stat['1:1'].serv_counts)]
            elif serve_return == 'return':
                player_stats = [player_match_stat.retn_mean,
                                player_stat['0:0'].retn_mean, player_stat['1:0'].retn_mean, player_stat['0:1'].retn_mean,
                                player_stat['1:1'].retn_mean]
                player_stats_ci = [player_match_stat.retn_std * stats.t._ppf(0.9, player_match_stat.retn_counts),
                                   player_stat['0:0'].retn_std * stats.t._ppf(0.9, player_stat['0:0'].retn_counts),
                                   player_stat['1:0'].retn_std * stats.t._ppf(0.9, player_stat['1:0'].retn_counts),
                                   player_stat['0:1'].retn_std * stats.t._ppf(0.9, player_stat['0:1'].retn_counts),
                                   player_stat['1:1'].retn_std * stats.t._ppf(0.9, player_stat['1:1'].retn_counts)]
            for x in range(player_stats.__len__()):
                if np.isnan(player_stats[x]):
                    player_stats[x] = float(0.0)
            for x in range(player_stats_ci.__len__()):
                if np.isnan(player_stats_ci[x]):
                    player_stats_ci[x] = float(0.0)
            all_players_stats[player] = {'mean':player_stats,'ci':player_stats_ci}

        header = ['match', '0:0', '1:0', '0:1', '1:1']

    elif metrics_level == 'game':
        for player in players:

            if two_step == 'true':
                if baseline_impact == 'baseline':
                    player_stat = players[player].game_baseline_ts
                    player_match_stat = players[player].match_baseline_ts
                elif baseline_impact == 'impact':
                    player_stat = players[player].game_impact_ts
                    player_match_stat = players[player].match_impact_ts
            else:
                if baseline_impact == 'baseline':
                    player_stat = players[player].game_baseline
                    player_match_stat = players[player].match_baseline
                elif baseline_impact == 'impact':
                    player_stat = players[player].game_impact
                    player_match_stat = players[player].match_impact

            if serve_return == 'serve':
                player_stats = [player_match_stat.serv_mean,
                                player_stat[3].serv_mean, player_stat[2].serv_mean, player_stat[1].serv_mean,
                                player_stat[0].serv_mean, player_stat[-1].serv_mean, player_stat[-2].serv_mean,
                                player_stat[-3].serv_mean]
                player_stats_ci = [player_match_stat.serv_std * stats.t._ppf(0.9, player_match_stat.serv_counts),
                                   player_stat[3].serv_std * stats.t._ppf(0.9, player_stat[3].serv_counts),
                                   player_stat[2].serv_std * stats.t._ppf(0.9, player_stat[2].serv_counts),
                                   player_stat[1].serv_std * stats.t._ppf(0.9, player_stat[1].serv_counts),
                                   player_stat[0].serv_std * stats.t._ppf(0.9, player_stat[0].serv_counts),
                                   player_stat[-1].serv_std * stats.t._ppf(0.9, player_stat[-1].serv_counts),
                                   player_stat[-2].serv_std * stats.t._ppf(0.9, player_stat[-2].serv_counts),
                                   player_stat[-3].serv_std * stats.t._ppf(0.9, player_stat[-3].serv_counts)]
            elif serve_return == 'return':
                player_stats = [player_match_stat.retn_mean,
                                player_stat[3].retn_mean, player_stat[2].retn_mean, player_stat[1].retn_mean,
                                player_stat[0].retn_mean, player_stat[-1].retn_mean, player_stat[-2].retn_mean,
                                player_stat[-3].retn_mean]
                player_stats_ci = [player_match_stat.retn_std * stats.t._ppf(0.9, player_match_stat.retn_counts),
                                   player_stat[3].retn_std * stats.t._ppf(0.9, player_stat[3].retn_counts),
                                   player_stat[2].retn_std * stats.t._ppf(0.9, player_stat[2].retn_counts),
                                   player_stat[1].retn_std * stats.t._ppf(0.9, player_stat[1].retn_counts),
                                   player_stat[0].retn_std * stats.t._ppf(0.9, player_stat[0].retn_counts),
                                   player_stat[-1].retn_std * stats.t._ppf(0.9, player_stat[-1].retn_counts),
                                   player_stat[-2].retn_std * stats.t._ppf(0.9, player_stat[-2].retn_counts),
                                   player_stat[-3].retn_std * stats.t._ppf(0.9, player_stat[-3].retn_counts)]
            for x in range(player_stats.__len__()):
                if np.isnan(player_stats[x]):
                    player_stats[x] = float(0.0)
            for x in range(player_stats_ci.__len__()):
                if np.isnan(player_stats_ci[x]):
                    player_stats_ci[x] = float(0.0)
            all_players_stats[player] = {'mean': player_stats, 'ci': player_stats_ci}

        header = ['match', '3', '2', '1', '0', '-1', '-2', '-3']

    elif metrics_level == 'point':
        for player in players:

            if two_step == 'true':
                if baseline_impact == 'baseline':
                    player_stat = players[player].point_baseline_ts
                    player_match_stat = players[player].match_baseline_ts
                elif baseline_impact == 'impact':
                    player_stat = players[player].point_impact_ts
                    player_match_stat = players[player].match_impact_ts
            else:
                if baseline_impact == 'baseline':
                    player_stat = players[player].point_baseline
                    player_match_stat = players[player].match_baseline
                elif baseline_impact == 'impact':
                    player_stat = players[player].point_impact
                    player_match_stat = players[player].match_impact

            if serve_return == 'serve':
                player_stats = [player_match_stat.serv_mean,
                                player_stat['0:0'].serv_mean, player_stat['15:15'].serv_mean, player_stat['30:30'].serv_mean,
                                player_stat['40:40'].serv_mean, player_stat['15:0'].serv_mean, player_stat['30:15'].serv_mean,
                                player_stat['40:30'].serv_mean, player_stat['AD:40'].serv_mean, player_stat['30:0'].serv_mean,
                                player_stat['40:15'].serv_mean, player_stat['40:0'].serv_mean, player_stat['0:15'].serv_mean,
                                player_stat['15:30'].serv_mean, player_stat['30:40'].serv_mean, player_stat['40:AD'].serv_mean,
                                player_stat['0:30'].serv_mean, player_stat['15:40'].serv_mean, player_stat['0:40'].serv_mean]
                player_stats_ci = [player_match_stat.serv_std * stats.t._ppf(0.9, player_match_stat.serv_counts),
                                   player_stat['0:0'].serv_std * stats.t._ppf(0.9, player_stat['0:0'].serv_counts),
                                   player_stat['15:15'].serv_std * stats.t._ppf(0.9, player_stat['15:15'].serv_counts),
                                   player_stat['30:30'].serv_std * stats.t._ppf(0.9, player_stat['30:30'].serv_counts),
                                   player_stat['40:40'].serv_std * stats.t._ppf(0.9, player_stat['40:40'].serv_counts),
                                   player_stat['15:0'].serv_std * stats.t._ppf(0.9, player_stat['15:0'].serv_counts),
                                   player_stat['30:15'].serv_std * stats.t._ppf(0.9, player_stat['30:15'].serv_counts),
                                   player_stat['40:30'].serv_std * stats.t._ppf(0.9, player_stat['40:30'].serv_counts),
                                   player_stat['AD:40'].serv_std * stats.t._ppf(0.9, player_stat['AD:40'].serv_counts),
                                   player_stat['30:0'].serv_std * stats.t._ppf(0.9, player_stat['30:0'].serv_counts),
                                   player_stat['40:15'].serv_std * stats.t._ppf(0.9, player_stat['40:15'].serv_counts),
                                   player_stat['40:0'].serv_std * stats.t._ppf(0.9, player_stat['40:0'].serv_counts),
                                   player_stat['0:15'].serv_std * stats.t._ppf(0.9, player_stat['0:15'].serv_counts),
                                   player_stat['15:30'].serv_std * stats.t._ppf(0.9, player_stat['15:30'].serv_counts),
                                   player_stat['30:40'].serv_std * stats.t._ppf(0.9, player_stat['30:40'].serv_counts),
                                   player_stat['40:AD'].serv_std * stats.t._ppf(0.9, player_stat['40:AD'].serv_counts),
                                   player_stat['0:30'].serv_std * stats.t._ppf(0.9, player_stat['0:30'].serv_counts),
                                   player_stat['15:40'].serv_std * stats.t._ppf(0.9, player_stat['15:40'].serv_counts),
                                   player_stat['0:40'].serv_std * stats.t._ppf(0.9, player_stat['0:40'].serv_counts)]
            elif serve_return == 'return':
                player_stats = [player_match_stat.retn_mean,
                                player_stat['0:0'].retn_mean, player_stat['15:15'].retn_mean, player_stat['30:30'].retn_mean,
                                player_stat['40:40'].retn_mean, player_stat['15:0'].retn_mean, player_stat['30:15'].retn_mean,
                                player_stat['40:30'].retn_mean, player_stat['AD:40'].retn_mean, player_stat['30:0'].retn_mean,
                                player_stat['40:15'].retn_mean, player_stat['40:0'].retn_mean, player_stat['0:15'].retn_mean,
                                player_stat['15:30'].retn_mean, player_stat['30:40'].retn_mean, player_stat['40:AD'].retn_mean,
                                player_stat['0:30'].retn_mean, player_stat['15:40'].retn_mean, player_stat['0:40'].retn_mean]
                player_stats_ci = [player_match_stat.retn_std * stats.t._ppf(0.9, player_match_stat.retn_counts),
                                   player_stat['0:0'].retn_std * stats.t._ppf(0.9, player_stat['0:0'].retn_counts),
                                   player_stat['15:15'].retn_std * stats.t._ppf(0.9, player_stat['15:15'].retn_counts),
                                   player_stat['30:30'].retn_std * stats.t._ppf(0.9, player_stat['30:30'].retn_counts),
                                   player_stat['40:40'].retn_std * stats.t._ppf(0.9, player_stat['40:40'].retn_counts),
                                   player_stat['15:0'].retn_std * stats.t._ppf(0.9, player_stat['15:0'].retn_counts),
                                   player_stat['30:15'].retn_std * stats.t._ppf(0.9, player_stat['30:15'].retn_counts),
                                   player_stat['40:30'].retn_std * stats.t._ppf(0.9, player_stat['40:30'].retn_counts),
                                   player_stat['AD:40'].retn_std * stats.t._ppf(0.9, player_stat['AD:40'].retn_counts),
                                   player_stat['30:0'].retn_std * stats.t._ppf(0.9, player_stat['30:0'].retn_counts),
                                   player_stat['40:15'].retn_std * stats.t._ppf(0.9, player_stat['40:15'].retn_counts),
                                   player_stat['40:0'].retn_std * stats.t._ppf(0.9, player_stat['40:0'].retn_counts),
                                   player_stat['0:15'].retn_std * stats.t._ppf(0.9, player_stat['0:15'].retn_counts),
                                   player_stat['15:30'].retn_std * stats.t._ppf(0.9, player_stat['15:30'].retn_counts),
                                   player_stat['30:40'].retn_std * stats.t._ppf(0.9, player_stat['30:40'].retn_counts),
                                   player_stat['40:AD'].retn_std * stats.t._ppf(0.9, player_stat['40:AD'].retn_counts),
                                   player_stat['0:30'].retn_std * stats.t._ppf(0.9, player_stat['0:30'].retn_counts),
                                   player_stat['15:40'].retn_std * stats.t._ppf(0.9, player_stat['15:40'].retn_counts),
                                   player_stat['0:40'].retn_std * stats.t._ppf(0.9, player_stat['0:40'].retn_counts)]
            for x in range(player_stats.__len__()):
                if np.isnan(player_stats[x]):
                    player_stats[x] = float(0.0)
            for x in range(player_stats_ci.__len__()):
                if np.isnan(player_stats_ci[x]):
                    player_stats_ci[x] = float(0.0)
            all_players_stats[player] = {'mean': player_stats, 'ci': player_stats_ci}

        header = ['match', '0:0', '15:15', '30:30', '40:40', '15:0', '30:15', '40:30', 'AD:40', '30:0',
                  '40:15', '40:0', '0:15', '15:30', '30:40', '40:AD', '0:30', '15:40', '0:40']


    return flask.jsonify({'player_stats': all_players_stats, 'header': header})