import warnings


def get_superpixel_mapping(mapping):
    """
    Reorganise a CHECLabPy mapping dataframe to represent the positions of the
    superpixels on the camera

    Parameters
    ----------
    mapping : `pandas.DataFrame`
        The CHECLabPy dataframe representation of the mapping

    Returns
    -------
    `pandas.DataFrame`

    """
    df = mapping[['superpixel', 'slot', 'asic', 'row', 'col', 'xpix', 'ypix']]
    f_rowcol = lambda v: v.values[0] // 2
    f = dict(slot='first', asic='first', row=f_rowcol, col=f_rowcol,
             xpix='mean', ypix='mean')
    df = df.groupby('superpixel').agg(f).reset_index()
    with warnings.catch_warnings():
        warnings.simplefilter('ignore', UserWarning)
        df.metadata = mapping.metadata.copy()
    df.metadata['n_rows'] = df['row'].max() + 1
    df.metadata['n_columns'] = df['col'].max() + 1
    df.metadata['size'] *= 2
    return df