Module ranky.utilities

Functions

def read_codalab_csv(csv_file)
Expand source code
def read_codalab_csv(csv_file):
    """ Read a leaderboard generated by Codalab as a CSV file.

    Args:
        csv_file: Path to the leaderboard downloaded from Codalab.

    Returns:
        A pd.DataFrame with one row per user and one column per score.
    """
    # Data rows have one more field than the header, so we add a name for the extra column
    col_names = pd.read_csv(csv_file, nrows=0).columns.tolist()
    col_names.append('extra')
    m = pd.read_csv(csv_file, names=col_names, skiprows=1)
    m = m.drop(columns=['extra', 'submission_pk'])
    m.index = m['User']
    m = m.drop('User', axis=1)
    if hasattr(pd.DataFrame, 'map'): # pandas >= 2.1
        m = m.map(str_to_float)
    else:
        m = m.applymap(str_to_float)
    return m

Read a leaderboard generated by Codalab as a CSV file.

Args

csv_file
Path to the leaderboard downloaded from Codalab.

Returns

A pd.DataFrame with one row per user and one column per score.

def str_to_float(s)
Expand source code
def str_to_float(s):
    """ Convert a Codalab leaderboard cell to float.

    Cells like '0.78 (2)' (score and rank) become 0.78. Other values are returned unchanged.
    """
    if isinstance(s, str) and s.endswith(')'):
        s = float(s.split(' ')[0])
    return s

Convert a Codalab leaderboard cell to float.

Cells like '0.78 (2)' (score and rank) become 0.78. Other values are returned unchanged.