import re


def rename_populations(stat_vars):
    """ Applies various rules to remap population names for the stat vars.

    1) Strips measurement prefixes. E.g. BLS_Worker -> Worker.
    2) Renames populations entirely. E.g. MortalityEvent -> Death.

    Args:
      stat_vars: List of all statistical variables before human readable name
        is generated.
    Returns:
      stat_vars with all populations renamed as defined in this function.
  """
    # Prefixes to strip.
    population_prefixes_to_strip = ['BLS', 'USC', 'ACSED']

    def strip_population_prefixes(population):
        for prefix in population_prefixes_to_strip:
            if population.startswith(prefix):
                return re.sub(f"^{prefix}", "", population)
        return population

    stat_vars['populationType'] = stat_vars['populationType'].apply(
        strip_population_prefixes)

    # Rename some populations entirely.
    populations_to_rename = {
        'MortalityEvent': 'Death',
    }
    stat_vars['populationType'] = stat_vars['populationType'].apply(
        lambda pop: populations_to_rename.get(pop, pop))
    return stat_vars