コード例 #1
0
def get_coefficients_from_schedule(team):
    # [H-A] * HFA + N * self - (1 * opp) for each = differential
    filename_format_name = build_filename_format(team)
    schedule_source = '%s/output/schedule_ratings/%s.csv' % (
        root_path, filename_format_name)
    coefficients = [0] * 129
    total_differential = 0
    location_adv = 0
    own_idx = idx_lookup[team]
    with open(schedule_source) as f:
        for line in f:
            coefficients[own_idx] += 1
            columns = line.strip().split(',')
            if columns[0] == "H":
                location_adv += 2.8
            elif columns[0] == "A":
                location_adv -= 2.8
            opponent = columns[1]
            if opponent in idx_lookup:
                coefficients[idx_lookup[opponent]] -= 1
            if columns[6] == "True":
                if int(columns[5]) > 0:
                    total_differential += 0.5
                else:
                    total_differential -= 0.5
            else:
                total_differential += int(columns[5])
    raw_a.append(coefficients)
    raw_b.append(total_differential - location_adv)
コード例 #2
0
def get_probabilities(team, own_rating):
    filename_format_name = build_filename_format(team)
    source = root_path + "/output/schedule_ratings/%s.csv" % filename_format_name
    game_ratings = []
    with open(source, 'r') as f:
        for line in f:
            _, _, _, rating = line.strip().split(',')
            game_ratings.append(float(rating))
    return calculate_probabilities(game_ratings, own_rating)
コード例 #3
0
    def add_all_games(self, schedule_path, teams_by_schedule_name):
        filename_format_name = build_filename_format(self.name)
        schedule_source = schedule_path + filename_format_name + ".csv"
        with open(schedule_source, 'r') as read_file:
            for line in read_file:
                game_data = read_schedule(line, teams_by_schedule_name)
                if game_data:
                    if game_data[GAME_RESULT] == "W":
                        self.wins += 1
                    elif game_data[GAME_RESULT] == "L":
                        self.losses += 1
                    else:
                        raise "Unexpected result %s" % game_data[GAME_RESULT]

                    self.games.append(Game(game_data))
コード例 #4
0
def generate_write_file_path(team, sport):
    filename_format_name = build_filename_format(team.name)
    write_file_path = PRETTY_SCHEDULE_GENERIC_PATH % (ROOT_PATH, sport)
    return write_file_path + filename_format_name + ".md"
コード例 #5
0
 def _build_ouput_file_path(self, standard_name):
     # Takes in a raw name from a performance page, returns the write file path
     # param @standard_name: string, standardized name
     # Output: string, file path to csv file created from standardized name to filname convention
     filename_format_name = build_filename_format(standard_name) + '.csv'
     return self.OUTPUT_DIRECTORY + filename_format_name
コード例 #6
0
teams = Team.build_teams_from_file(team_list_source)

records_source = "%s/output/standings-week%s.csv" % (ROOT_PATH, CURRENT_WEEK)
with open(records_source) as f:
    for line in f:
        name, record = line.strip().split(",")
        name = translate_ncaa_name(name)
        teams[name].record = record

# Create game objects
schedule_root = '%s/output/football/schedules/' % ROOT_PATH
idx = 0
for name, team in teams.items():
    team.id = idx
    idx += 1
    filename_format_name = build_filename_format(name)
    schedule_source = '%s%s.csv' % (schedule_root, filename_format_name)
    with open(schedule_source) as f:
        for line in f:
            # Read schedule data
            columns = line.strip().split(",")
            location, opponent, result, score = columns

            if result == "--":
                continue
            opponent = translate_schedule_name(opponent)
            if opponent not in teams:
                print('Skipped %s' % opponent)
                continue

            location = translate_location(location)
コード例 #7
0
def get_and_save_rating(name):
    filename_format_name = build_filename_format(name)
    schedule_file = root_path + "/output/schedules/%s.csv" % filename_format_name
    # Open schedule file
    game_list = []
    with open(schedule_file) as f:
        for line in f:
            columns = line.strip().split(',')
            if columns[2] == 'False':
                continue
            if len(columns) == 5:
                location, opponent, _, result, score = columns
            if location == 'vs':
                location = 'H'
            elif location == '@':
                location = 'A'
            # Build list of games
            if opponent in SCHEDULE_NAMES:
                opponent = SCHEDULE_NAMES[opponent]
            if INCLUDE_FCS or opponent in combined_ratings:
                win_score, lose_score = score.split('-')
                overtime = False
                if len(lose_score.split(' ')) > 1:
                    lose_score, _ = lose_score.split(' ')
                    overtime = True
                if result == 'W':
                    differential = int(win_score) - int(lose_score)
                    own_score = win_score
                    opp_score = lose_score
                elif result == 'L':
                    differential = int(lose_score) - int(win_score)
                    own_score = lose_score
                    opp_score = win_score
                else:
                    raise "Invalid result found: %s" % result
                game_list.append({
                    'location': location,
                    'opponent': opponent,
                    'result': result,
                    'own_score': own_score,
                    'opp_score': opp_score,
                    'differential': differential,
                    'overtime': overtime
                })

    # For each game
    for game in game_list:
        # Look up rating from combined
        if game['opponent'] in combined_ratings:
            game['combined_rating'] = combined_ratings[game['opponent']]
            # Look up rating from power
            game['power_rating'] = power_ratings[game['opponent']]
        elif INCLUDE_FCS:
            print("Couldn't find %s" % game['opponent'])
            game['combined_rating'] = 40
            game['power_rating'] = 40
        else:
            game['combined_rating'] = '--'
            game['power_rating'] = '--'

    def calculate_res(result, opp_rating, differential, overtime):
        if overtime:
            if result == 'W':
                return opp_rating + 2.0
            elif result == 'L':
                return opp_rating - 2.0
            else:
                raise 'Unknown result found %s' % result
        else:
            if result == 'W':
                return opp_rating + 4.0 * math.sqrt(differential)
            elif result == 'L':
                return opp_rating - 4.0 * math.sqrt(-1.0 * differential)
            else:
                raise 'Unknown result found %s' % result

    # Write file
    write_file = root_path + '/output/schedule_ratings/%s.csv' % filename_format_name
    with open(write_file, 'w+') as f:
        adjusted_differential = 0
        result_emphasized_differential = 0
        game_count = 0
        game_scores = []
        for game in game_list:
            if game['combined_rating'] != '--':
                game['location_adjustment'] = 0
                if game['location'] == 'H':
                    game['location_adjustment'] = -2.8
                elif game['location'] == 'A':
                    game['location_adjustment'] = 2.8
                game['combined_rating'] = game['combined_rating'] + game[
                    'location_adjustment']
                game['power_rating'] = game['power_rating'] + game[
                    'location_adjustment']
                game_score = float(game['power_rating']) + game['differential']
                game_scores.append(game_score)
                adjusted_differential += game_score
                result_emphasized_score = calculate_res(
                    game['result'], game['power_rating'], game['differential'],
                    game['overtime'])
                result_emphasized_differential += result_emphasized_score
                game_count += 1
                text = "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n" % (
                    game['location'], game['opponent'],
                    game['combined_rating'], game['power_rating'],
                    game['result'], game['differential'], game['overtime'],
                    game_score, game['own_score'], game['opp_score'])
                f.write(text)

    sorted_games = sorted(game_list,
                          key=lambda game: game['power_rating'],
                          reverse=True)
    reddit_write_file = root_path + '/output/reddit_schedules/%s.txt' % filename_format_name
    with open(reddit_write_file, 'w+') as f:
        columns = ["Opponent", "Loc", "Rating"]
        header = " | ".join(columns) + "\n"
        barrier = "|".join(map(lambda _: "---", columns)) + "\n"
        f.write(header)
        f.write(barrier)
        for game in sorted_games:
            rated_difficulty = round((game['power_rating'] - 35) * 100 / 70)
            opponent = game['opponent']
            if opponent in REDDIT_NAMES:
                opponent = REDDIT_NAMES[opponent]
            else:
                combo_string = "".join(opponent.split(" ")).lower()
                opponent = "[%s](#f/%s) %s" % (opponent, combo_string,
                                               opponent)
            text = "%(opponent)s | %(location)s | %(rating)s |\n" % {
                'opponent': opponent,
                'location': game['location'],
                'rating': rated_difficulty,
            }
            f.write(text)

    team_sor_ratings.append({
        'team':
        name,
        'rating':
        adjusted_differential / game_count,
        'median':
        statistics.median(game_scores),
        'results_emphasized_rating':
        float(result_emphasized_differential) / game_count
    })
    print('Done with %s' % name)