def calculate_ap(combined, total_actual):
    """
    Calculate single object average precision.
    Args:
        combined: `pd.DataFrame` containing true positives + false positives.
        total_actual: Total instances of an object in the dataset.

    Returns:
        Updated combined with average precision calculated.
    """
    combined = combined.sort_values(by='score', ascending=False).reset_index(drop=True)
    combined['acc_tp'] = combined['true_positive'].cumsum()
    combined['acc_fp'] = combined['false_positive'].cumsum()
    combined['precision'] = combined['acc_tp'] / (
        combined['acc_tp'] + combined['acc_fp']
    )
    combined['recall'] = combined['acc_tp'] / total_actual
    combined['m_pre1'] = combined['precision'].shift(1, fill_value=0)
    combined['m_pre'] = combined[['m_pre1', 'precision']].max(axis=1)
    combined['m_rec1'] = combined['recall'].shift(1, fill_value=0)
    combined.loc[combined['m_rec1'] != combined['recall'], 'valid_m_rec'] = 1
    combined['average_precision'] = (
        combined['recall'] - combined['m_rec1']
    ) * combined['m_pre']
    return combined