示例#1
0
def plotHomeAwayRecords(name):
    '''
    generate relevant plots and saves them in the corresponding folder
    @param name: the input team name
    '''
    plt.clf()
    path = './' + name + '/home_away_records'
    
    all_seasons = ['09-10','10-11','11-12','12-13','13-14']
    # a helper counter
    count = 0
    idx = [] 
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            count = count + 1
            idx.append(season)
    
    # initialize array for home and away record
    record = np.zeros((count,2))
    
    count = 0
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            team = getTeamInfo(name, info_list, season)
            r_1 = team.getHomeAwayRecord()[0,:] # home record
            r_2 = team.getHomeAwayRecord()[1,:] # away record
            record[count,0] = np.average(r_1, weights = [3,1,0])
            record[count,1] = np.average(r_2, weights = [3,1,0])
            
            count = count + 1
    
    colnames = ['Home', 'Away']
    df = p.DataFrame(record, index=idx, columns=colnames)
        
    df.plot(kind = 'barh', stacked = True)
    plt.title('Home and Away Records of ' + name)
    plt.xlabel('Average Points Obtained per Game')
    plt.ylabel('Season')
    plt.savefig(path)
    
    
    
    
    
            
    
    
        
    
示例#2
0
def askRound(season):
    '''
    ask round from the user
    @param season: the input season from the user
    '''
    all_rounds = []
    for i in range(1,39):
        all_rounds.append(str(i))
    
    try:
        round_input = raw_input('Which round would you like to check? Please enter an integer from 1 to 38.')
        if round_input in all_rounds:
            print 'generating table... \n'
            round = int(round_input)
            info_list = processData(season)
            table = getTable(season, round, info_list)
            print table
            print ''
        else:
            print 'Sorry! The input is not valid. Please enter a valid round'
            askRound(season)
    
    except (EOFError, KeyboardInterrupt):
        print 'Program Terminated'
        sys.exit()
示例#3
0
def askRound(season):
    '''
    ask round from the user
    @param season: the input season from the user
    '''
    all_rounds = []
    for i in range(1, 39):
        all_rounds.append(str(i))

    try:
        round_input = raw_input(
            'Which round would you like to check? Please enter an integer from 1 to 38.'
        )
        if round_input in all_rounds:
            print 'generating table... \n'
            round = int(round_input)
            info_list = processData(season)
            table = getTable(season, round, info_list)
            print table
            print ''
        else:
            print 'Sorry! The input is not valid. Please enter a valid round'
            askRound(season)

    except (EOFError, KeyboardInterrupt):
        print 'Program Terminated'
        sys.exit()
示例#4
0
def generateReport(name):
    '''
    generate the team report and saves it in the corresponding folder
    @param name: the input team name
    '''
    path = './' + name + '/report.txt'

    try:
        report = open(path, 'w')
        report.write('Team Name: ' + name + '\n')
        report.write('\n')

        all_seasons = ['09-10', '10-11', '11-12', '12-13', '13-14']
        for season in all_seasons:
            info_list = processData(season)
            names = getTeamNames(info_list)
            if name in names:
                # if the team plays in the premier league during the season
                report.write(season + ' Season: \n')
                table = getTable(season, 38, info_list)
                record = table[table.Team == name]
                rank = record.index[0]
                W = record.iloc[0, 2]
                D = record.iloc[0, 3]
                L = record.iloc[0, 4]
                GF = record.iloc[0, 5]
                GA = record.iloc[0, 6]
                # writes the basic information of the season.
                report.write(name + ' ranks ' + str(rank) + ' this season. ')
                report.write('They win ' + str(W) + ', draw ' + str(D) +
                             ', and lose ' + str(L) + ' games.')
                report.write('\n')
                report.write('They score ' + str(GF) + ' goals and concede ' +
                             str(GA) + ' goals in total.')
                report.write('\n')

                team = getTeamInfo(name, info_list, season)
                # write the largest victory of the season
                idx_diff = team.getLargestDifference()
                report.write('The largest victory is against ' +
                             team.opponents[idx_diff[0]] + '. ')
                report.write('The score is ' +
                             str(team.goals_scored[idx_diff[0]]) + '-')
                report.write(str(team.goals_conceded[idx_diff[0]]) + '. \n')
                # write the largest defeat of the season
                report.write('The largest defeat is against ' +
                             team.opponents[idx_diff[1]] + '. ')
                report.write('The score is ' +
                             str(team.goals_scored[idx_diff[1]]) + '-')
                report.write(str(team.goals_conceded[idx_diff[1]]) + '. \n')

                report.write('\n')

        report.close()

    except IOError:
        print 'Error occurs when writing the report file'
示例#5
0
def plotHomeAwayRecords(name):
    '''
    generate relevant plots and saves them in the corresponding folder
    @param name: the input team name
    '''
    plt.clf()
    path = './' + name + '/home_away_records'

    all_seasons = ['09-10', '10-11', '11-12', '12-13', '13-14']
    # a helper counter
    count = 0
    idx = []
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            count = count + 1
            idx.append(season)

    # initialize array for home and away record
    record = np.zeros((count, 2))

    count = 0
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            team = getTeamInfo(name, info_list, season)
            r_1 = team.getHomeAwayRecord()[0, :]  # home record
            r_2 = team.getHomeAwayRecord()[1, :]  # away record
            record[count, 0] = np.average(r_1, weights=[3, 1, 0])
            record[count, 1] = np.average(r_2, weights=[3, 1, 0])

            count = count + 1

    colnames = ['Home', 'Away']
    df = p.DataFrame(record, index=idx, columns=colnames)

    df.plot(kind='barh', stacked=True)
    plt.title('Home and Away Records of ' + name)
    plt.xlabel('Average Points Obtained per Game')
    plt.ylabel('Season')
    plt.savefig(path)
示例#6
0
def plotHomeAwayStat(name):
    '''
    generate relevant plots and saves them in the corresponding folder
    @param name: the input team name
    '''
    plt.clf()  # clear previous figures
    path = './' + name + '/home_away_stats'

    all_seasons = ['09-10', '10-11', '11-12', '12-13', '13-14']

    # a helper counter
    count = 0
    idx = []
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            count = count + 1
            idx.append(season)

    # initializes a matrix of the corresponding size
    stats = np.zeros((count, 4))

    count = 0
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            team = getTeamInfo(name, info_list, season)
            stats[count, ] = team.computeAvgGoal()
            count = count + 1

    colnames = ['Score-Home', 'Concede-Home', 'Score-Away', 'Concede-Away']
    df = p.DataFrame(stats,
                     index=idx,
                     columns=p.Index(colnames, name='Avg. Goals per Game'))
    df.plot(kind='bar')
    plt.legend(prop={'size': 12})
    plt.title('Average Goals Scored and Conceded per Game of ' + name)
    plt.xlabel('Season')
    plt.ylabel('Average Goals per Game')
    plt.savefig(path)
示例#7
0
def plotHomeAwayStat(name):
    '''
    generate relevant plots and saves them in the corresponding folder
    @param name: the input team name
    '''
    plt.clf() # clear previous figures
    path = './' + name + '/home_away_stats'
    
    all_seasons = ['09-10','10-11','11-12','12-13','13-14']
    
    # a helper counter
    count = 0
    idx = [] 
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            count = count + 1
            idx.append(season)
    
    # initializes a matrix of the corresponding size
    stats = np.zeros((count, 4))
    
    count = 0
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        if name in names:
            team = getTeamInfo(name, info_list, season)
            stats[count,] = team.computeAvgGoal()
            count = count + 1
    
    colnames = ['Score-Home', 'Concede-Home', 'Score-Away', 'Concede-Away']
    df = p.DataFrame(stats, index = idx, columns = p.Index(colnames,name = 'Avg. Goals per Game'))
    df.plot(kind = 'bar')
    plt.legend(prop={'size':12})
    plt.title('Average Goals Scored and Conceded per Game of ' + name)
    plt.xlabel('Season')
    plt.ylabel('Average Goals per Game')
    plt.savefig(path)
示例#8
0
def getAllTeams():
    '''
    get all teams that have played in the last 5 seasons in the premier league
    @return: the set of teams 
    '''
    all_names = set()
    all_seasons = ['09-10','10-11','11-12','12-13','13-14']
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        all_names = all_names.union(names)
        
    return all_names
示例#9
0
def getAllTeams():
    '''
    get all teams that have played in the last 5 seasons in the premier league
    @return: the set of teams 
    '''
    all_names = set()
    all_seasons = ['09-10', '10-11', '11-12', '12-13', '13-14']
    for season in all_seasons:
        info_list = processData(season)
        names = getTeamNames(info_list)
        all_names = all_names.union(names)

    return all_names
示例#10
0
def generateReport(name):
    '''
    generate the team report and saves it in the corresponding folder
    @param name: the input team name
    '''
    path = './' + name + '/report.txt'
    
    try:
        report = open(path, 'w')
        report.write('Team Name: ' + name + '\n')
        report.write('\n')
    
        all_seasons = ['09-10','10-11','11-12','12-13','13-14']
        for season in all_seasons:
            info_list = processData(season)
            names = getTeamNames(info_list)
            if name in names:
                # if the team plays in the premier league during the season
                report.write(season + ' Season: \n')
                table = getTable(season, 38, info_list)
                record = table[table.Team == name]
                rank = record.index[0]
                W = record.iloc[0,2]
                D = record.iloc[0,3]
                L = record.iloc[0,4]
                GF = record.iloc[0,5]
                GA = record.iloc[0,6]
                # writes the basic information of the season.
                report.write(name + ' ranks ' + str(rank) + ' this season. ')
                report.write('They win ' + str(W) + ', draw ' + str(D) + ', and lose ' + str(L) + ' games.')
                report.write('\n')
                report.write('They score ' + str(GF) + ' goals and concede ' + str(GA) + ' goals in total.')
                report.write('\n')
            
                team = getTeamInfo(name, info_list, season)
                # write the largest victory of the season
                idx_diff = team.getLargestDifference()
                report.write('The largest victory is against ' + team.opponents[idx_diff[0]] + '. ')
                report.write('The score is ' + str(team.goals_scored[idx_diff[0]]) + '-')
                report.write(str(team.goals_conceded[idx_diff[0]]) + '. \n')
                # write the largest defeat of the season
                report.write('The largest defeat is against ' + team.opponents[idx_diff[1]] + '. ')
                report.write('The score is ' + str(team.goals_scored[idx_diff[1]]) + '-')
                report.write(str(team.goals_conceded[idx_diff[1]]) + '. \n')
                 
                report.write('\n')
        
        report.close()
    
    except IOError:
        print 'Error occurs when writing the report file'