application_vnd.jupyter.stderr_output_2.png text_plain_output_1.png
import numpy as np

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import trueskill as ts

import pandas as pd
import numpy as np
import trueskill as ts
pd.set_option('display.max_rows', len(dfRatings))

def cleanResults(raceColumns,dfResultsTemp,appendScore):
    for raceCol in raceColumns:
        
        dfResultsTemp.index = dfResultsTemp.index.str.replace(r"(\w)([A-Z])", r"\1 \2")
        dfResultsTemp.index = dfResultsTemp.index.str.title()
        dfResultsTemp.index = dfResultsTemp.index.str.replace('\([A-Z\ 0-9]*\)','')
        dfResultsTemp.index = dfResultsTemp.index.str.strip()
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Riccardo Andrea Leccese','Rikki Leccese')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Nicolas Parlier','Nico Parlier')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Alejandro Climent Hernã¥_ Ndez', 'Alejandro Climent Hernandez')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Alexandre Caizergues','Alex Caizergues')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Florian Trittel Paul','Florian Trittel')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Jean Guillaume Rivaud','Jean-Guillaume Rivaud')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('^Kieran Le$','Kieran Le Borgne')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Marvin Baumeisterschoenian','Marvin Baumeister Schoenian')    
        dfResultsTemp.index = dfResultsTemp.index.str.replace('Theo De Ramecourt','Theo De-Ramecourt')
        dfResultsTemp.index = dfResultsTemp.index.str.replace('James Johnson','James Johnsen')

         
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].astype(str)
        
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].str.replace('D\+D|DSQ|D\+0|^-[A-Z0-9]*$|\([A-Z0-9\.-]*\)|UFD|SCP|RDG|RCT|DCT|DNS-[0-9]*|DNC-[0-9]*|OCS-[0-9]*|[0-9\.]*DNC|\/','')
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].str.replace('DNS','')
        
        #Count DNF or Retired as last place
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol].str.replace('RET[0-9]*|DNF-[0-9]*|^DNF$|[0-9\.]*DNF',str(len(dfResultsTemp)+1))
        
        
        dfResultsTemp[raceCol] = pd.to_numeric(dfResultsTemp[raceCol])  
        dfResultsTemp[raceCol] = dfResultsTemp[raceCol] + appendScore
        
    return dfResultsTemp
    

def mergeResults(raceColumns, raceName, dfResultsTemp, dfResults):
    for raceCol in raceColumns:
        raceIndex = raceName + '-' + raceCol
        dfResultsTemp[raceIndex] = dfResultsTemp[raceCol]
        del dfResultsTemp[raceCol]
        dfResults = pd.merge(dfResults, dfResultsTemp[[raceIndex]], left_index=True, right_index=True, how='outer')
    return dfResults

def doRating(dfResults, dfRatings):
    dfRatings = pd.merge(dfRatings, dfResults, left_on=['Name'], right_index=True, how='outer')
    dfRatings['Name'] = dfRatings.index
    ratingsColumns = ['Name', 'mu_minus_3sigma', 'NumRaces', 'Rating']
    dfRatings = dfRatings[ratingsColumns]
    dfRatings['Rating'][dfRatings['Rating'].isnull()] = pd.Series(np.repeat(ts.Rating(), len(dfRatings['Rating'].isnull()))).T.values.tolist()
    for raceCol in dfResults:
        competed = dfRatings['Name'].isin(dfResults.index[dfResults[raceCol].notnull()])
        rating_group = list(zip(dfRatings['Rating'][competed].T.values.tolist()))
        dfRatings['Rating'][competed] = ts.rate(rating_group, ranks=dfResults[raceCol][competed].T.values.tolist())
    dfRatings = pd.DataFrame(dfRatings)
    dfRatings['mu_minus_3sigma'] = pd.Series(np.repeat(0.0, len(dfRatings)))
    for i in range(0, len(dfRatings['Rating'])):
        dfRatings['mu_minus_3sigma'][i] = float(dfRatings['Rating'][i].mu) - 3 * float(dfRatings['Rating'][i].sigma)
    dfRatings['Name'] = dfRatings.index
    dfRatings.index = dfRatings['mu_minus_3sigma'].rank(ascending=False).astype(int)
    dfRatings.index.names = ['Rank']
    return dfRatings.sort('mu_minus_3sigma', ascending=False)

ratingsColumns = ['Name', 'mu_minus_3sigma', 'NumRaces', 'Rating']
dfRatings = pd.DataFrame(columns=ratingsColumns)

dfResults = pd.DataFrame()
raceName = '20160323-LaVentana-HydrofoilProTour'
raceColumns = ['Q1', 'Q2', 'R1', 'R2', 'R3', 'R4', 'R5', 'R6']
dfResultsTempGold = pd.read_csv('../input/' + raceName + '-Gold.csv')
dfResultsTempGold = dfResultsTempGold.set_index(dfResultsTempGold['Name'] + ' ' + dfResultsTempGold['LastName'])
dfResultsTempGold = cleanResults(raceColumns, dfResultsTempGold, 0)
dfResultsTempSilver = pd.read_csv('../input/' + raceName + '-Silver.csv')
dfResultsTempSilver = dfResultsTempSilver.set_index(dfResultsTempSilver['Name'] + ' ' + dfResultsTempSilver['LastName'])
dfResultsTempSilver = cleanResults(raceColumns, dfResultsTempSilver, len(dfResultsTempGold))
dfResultsTemp = dfResultsTempGold.append(dfResultsTempSilver)
dfResults = mergeResults(raceColumns, raceName, dfResultsTemp, dfResults)

dfRatings = doRating(dfResults, dfRatings)
dfRatings