def deviation_from_ideal(partition, attribute="population"):
    """Computes the deviation of the given ``attribute`` from exact equality
    among parts of the partition. Usually ``attribute`` is the population, and
    this function is used to compute how far a districting plan is from exact population
    equality.

    By "deviation" we mean ``(actual_value - ideal)/ideal`` (not the absolute value).

    :param partition: A partition.
    :param attribute: (optional) The :class:`Tally <gerrychain.updaters.Tally>` to
        compute deviation for. Default is ``"population"``.
    :return: dictionary from parts to their deviation
    """
    number_of_districts = len(partition[attribute].keys())
    total = sum(partition[attribute].values())
    ideal = total / number_of_districts

    return {
        part: (value - ideal) / ideal for part, value in partition[attribute].items()
    }