def rewrite(labels, rewrites, inplace=False):
    """
    Args:
        labels(List[str]): list of class labels
        rewrites(dict): mapping from target to source labels to rewrite
    """
    if rewrites:
        labels = inplace and labels or list(labels)
        for target, sources in rewrites.items():
            if not isinstance(sources, list):
                sources = [sources]
            for src in sources:
                idx = labels.index(src)
                labels[idx] = target
    return labels