Пример #1
0
    def __init__(self, name, parties_to_columns, alias=None):
        """
        :param name: The name of the election. (e.g. "2008 Presidential")
        :param parties_to_columns: A dictionary matching party names to their
            data columns, either as actual columns (list-like, indexed by nodes)
            or as string keys for the node attributes that hold the party's
            vote totals. Or, a list of strings which will serve as both
            the party names and the node attribute keys.
        :param alias: (optional) Alias that the election is registered under
            in the Partition's dictionary of updaters.
        """
        self.name = name

        if alias is None:
            alias = name
        self.alias = alias

        if isinstance(parties_to_columns, dict):
            self.parties = list(parties_to_columns.keys())
            self.columns = list(parties_to_columns.values())
            self.parties_to_columns = parties_to_columns
        elif isinstance(parties_to_columns, list):
            self.parties = parties_to_columns
            self.columns = parties_to_columns
            self.parties_to_columns = dict(zip(self.parties, self.columns))
        else:
            raise TypeError(
                "Election expects parties_to_columns to be a dict or list")

        self.tallies = {
            party: DataTally(self.parties_to_columns[party], party)
            for party in self.parties
        }

        self.updater = ElectionUpdater(self)
Пример #2
0
def test_data_tally_mimics_old_tally_usage(graph_with_random_data_factory):
    graph = graph_with_random_data_factory(["total"])

    # Make a DataTally the same way you make a Tally
    updaters = {"total": DataTally("total", alias="total")}
    assignment = {i: 1 if i in range(4) else 2 for i in range(9)}

    partition = Partition(graph, assignment, updaters)
    expected_total_in_district_one = sum(graph.nodes[i]["total"] for i in range(4))
    assert partition["total"][1] == expected_total_in_district_one
Пример #3
0
def test_data_tally_gives_expected_value(three_by_three_grid):
    assignment = {node: 1 for node in three_by_three_grid.nodes}
    data = {node: 1 for node in three_by_three_grid}
    updaters = {'tally': DataTally(data, alias='tally')}
    partition = Partition(three_by_three_grid, assignment, updaters)

    flip = {list(three_by_three_grid.nodes)[0]: 0}
    new_partition = partition.merge(flip)

    assert new_partition['tally'][1] == partition['tally'][1] - 1
Пример #4
0
def test_data_tally_works_as_an_updater(three_by_three_grid):
    assignment = random_assignment(three_by_three_grid, 4)
    data = {node: random.randint(1, 100) for node in three_by_three_grid.nodes}
    parts = tuple(set(assignment.values()))
    updaters = {"tally": DataTally(data, alias="tally")}
    partition = Partition(three_by_three_grid, assignment, updaters)

    flip = {random.choice(list(partition.graph.nodes)): random.choice(parts)}
    new_partition = partition.flip(flip)

    assert new_partition["tally"]
Пример #5
0
def test_data_tally_works_as_an_updater(three_by_three_grid):
    assignment = random_assignment(three_by_three_grid, 4)
    data = {node: random.randint(1, 100) for node in three_by_three_grid.nodes}
    updaters = {'tally': DataTally(data, alias='tally')}
    partition = Partition(three_by_three_grid, assignment, updaters)

    flip = {
        random.choice(list(partition.graph.nodes)): random.choice(range(4))
    }
    new_partition = partition.merge(flip)

    assert new_partition['tally']
Пример #6
0
def test_data_tally_gives_expected_value(three_by_three_grid):
    first_node = next(iter(three_by_three_grid.nodes))
    assignment = {node: 1 for node in three_by_three_grid.nodes}
    assignment[first_node] = 2

    data = {node: 1 for node in three_by_three_grid}
    updaters = {"tally": DataTally(data, alias="tally")}
    partition = Partition(three_by_three_grid, assignment, updaters)

    flip = {first_node: 1}
    new_partition = partition.flip(flip)

    assert new_partition["tally"][1] == partition["tally"][1] + 1