示例#1
0
def example_partition():
    df = gp.read_file(os.path.join(TEST_DATA_PATH, "mo_cleaned_vtds.shp"))

    with open(os.path.join(TEST_DATA_PATH, "MO_graph.json")) as f:
        graph_json = json.load(f)

    graph = networkx.readwrite.json_graph.adjacency_graph(graph_json)

    assignment = get_assignment_dict(df, "GEOID10", "CD")

    add_data_to_graph(
        df,
        graph, ['PR_DV08', 'PR_RV08', 'POP100', 'ALAND10', 'COUNTYFP10'],
        id_col='GEOID10')

    updaters = {
        **votes_updaters(['PR_DV08', 'PR_RV08'], election_name='08'), 'population':
        Tally('POP100', alias='population'),
        'counties':
        county_splits('counties', 'COUNTYFP10'),
        'cut_edges':
        cut_edges,
        'cut_edges_by_part':
        cut_edges_by_part
    }
    return Partition(graph, assignment, updaters)
示例#2
0
def PA_partition():
    # this is a networkx adjancency data json file with CD, area, population, and vote data
    graph = construct_graph("./testData/PA_graph_with_data.json")

    # Add frozen attributes to graph
    # data = gp.read_file("./testData/frozen.shp")
    # add_data_to_graph(data, graph, ['Frozen'], 'wes_id')

    assignment = dict(
        zip(graph.nodes(), [graph.node[x]['CD'] for x in graph.nodes()]))

    updaters = {
        **votes_updaters(['VoteA', 'VoteB']), 'population':
        Tally('POP100', alias='population'),
        'perimeters':
        perimeters,
        'exterior_boundaries':
        exterior_boundaries,
        'boundary_nodes':
        boundary_nodes,
        'cut_edges':
        cut_edges,
        'areas':
        Tally('ALAND10', alias='areas'),
        'polsby_popper':
        polsby_popper,
        'cut_edges_by_part':
        cut_edges_by_part
    }

    return Partition(graph, assignment, updaters)
示例#3
0
def setup_for_proportion_updaters(columns):
    graph = three_by_three_grid()

    attach_random_data(graph, columns)

    assignment = random_assignment(graph, 3)

    updaters = votes_updaters(columns)
    return Partition(graph, assignment, updaters)
示例#4
0
def test_vote_proportion_returns_nan_if_total_votes_is_zero():
    columns = ['D', 'R']
    graph = three_by_three_grid()
    for node in graph.nodes:
        for col in columns:
            graph.nodes[node][col] = 0
    updaters = votes_updaters(columns)
    assignment = random_assignment(graph, 3)

    partition = Partition(graph, assignment, updaters)

    assert all(math.isnan(value) for value in partition['D%'].values())
    assert all(math.isnan(value) for value in partition['R%'].values())
示例#5
0
def set_up_chain(plan, total_steps, adjacency_type='queen'):
    graph = Graph.load(f"./PA_{adjacency_type}.json").graph

    assignment = {node: graph.nodes[node][plan] for node in graph.nodes}

    updaters = {
        **votes_updaters(elections["2016_Presidential"],
                         election_name="2016_Presidential"),
        **votes_updaters(elections["2016_Senate"], election_name="2016_Senate"), 'population':
        Tally('population', alias='population'),
        'perimeters':
        perimeters,
        'exterior_boundaries':
        exterior_boundaries,
        'interior_boundaries':
        interior_boundaries,
        'boundary_nodes':
        boundary_nodes,
        'cut_edges':
        cut_edges,
        'areas':
        Tally('area', alias='areas'),
        'polsby_popper':
        polsby_popper,
        'cut_edges_by_part':
        cut_edges_by_part
    }

    partition = Partition(graph, assignment, updaters)

    population_constraint = within_percent_of_ideal_population(partition, 0.01)
    compactness_constraint = SelfConfiguringLowerBound(L_minus_1_polsby_popper,
                                                       epsilon=0.1)

    is_valid = Validator(default_constraints +
                         [population_constraint, compactness_constraint])

    return partition, MarkovChain(propose_random_flip, is_valid, always_accept,
                                  partition, total_steps)
示例#6
0
def test_vote_proportion_updater_returns_percentage_or_nan_on_later_steps():
    columns = ['D', 'R']
    graph = three_by_three_grid()
    attach_random_data(graph, columns)
    assignment = random_assignment(graph, 3)
    updaters = {**votes_updaters(columns), 'cut_edges': cut_edges}

    initial_partition = Partition(graph, assignment, updaters)

    chain = MarkovChain(propose_random_flip, Validator([no_vanishing_districts]),
                        lambda x: True, initial_partition, total_steps=10)
    for partition in chain:
        assert all(is_percentage_or_nan(value) for value in partition['D%'].values())
        assert all(is_percentage_or_nan(value) for value in partition['R%'].values())
示例#7
0
def example_partition():
    df = gp.read_file("./testData/mo_cleaned_vtds.shp")

    with open("./testData/MO_graph.json") as f:
        graph_json = json.load(f)

    graph = networkx.readwrite.json_graph.adjacency_graph(graph_json)

    assignment = get_assignment_dict(df, "GEOID10", "CD")

    add_data_to_graph(
        df,
        graph, ['PR_DV08', 'PR_RV08', 'POP100', 'ALAND10', 'COUNTYFP10'],
        id_col='GEOID10')

    updaters = {
        **votes_updaters(['PR_DV08', 'PR_RV08'], election_name='08'), 'population':
        Tally('POP100', alias='population'),
        'areas':
        Tally('ALAND10', alias='areas'),
        'counties':
        county_splits('counties', 'COUNTYFP10'),
        'perimeters':
        perimeters,
        'exterior_boundaries':
        exterior_boundaries,
        'boundary_nodes':
        boundary_nodes,
        'polsby_popper':
        polsby_popper,
        'cut_edges':
        cut_edges,
        'cut_edges_by_part':
        cut_edges_by_part
    }
    return Partition(graph, assignment, updaters)
示例#8
0
    'perimeters': perimeters,
    'exterior_boundaries': exterior_boundaries,
    'interior_boundaries': interior_boundaries,
    'boundary_nodes': boundary_nodes,
    'cut_edges': cut_edges,
    'areas': Tally('areas'),
    'polsby_popper': polsby_popper,
    'cut_edges_by_part': cut_edges_by_part,
    #'County_Splits': county_splits('County_Splits',county_col)
}

# Add the vote updaters for multiple plans
for i in range(num_elections):
    updaters = {
        **updaters,
        **votes_updaters(election_columns[i], election_names[i])
    }

# This builds the partition object
initial_partition = Partition(graph, assignment, updaters)

# Choose which binary constraints to enforce
# Options are in validity.py

pop_limit = .2
population_constraint = within_percent_of_ideal_population(
    initial_partition, pop_limit)

compactness_constraint_Lm1 = LowerBound(
    L_minus_1_polsby_popper, L_minus_1_polsby_popper(initial_partition))
示例#9
0
add_data_to_graph(df, graph, data_list, id_col=unique_label)

# Desired proposal method
proposal_method = propose_random_flip_no_loops

# Desired proposal method
acceptance_method = always_accept

# Number of steps to run
steps = 1000

print("loaded data")

# Necessary updaters go here
updaters = {
    **votes_updaters([vote_col1, vote_col2]), 'population':
    Tally(pop_col, alias='population'),
    'perimeters':
    perimeters,
    'exterior_boundaries':
    exterior_boundaries,
    'interior_boundaries':
    interior_boundaries,
    'boundary_nodes':
    boundary_nodes,
    'cut_edges':
    cut_edges,
    'areas':
    Tally(area_col, alias='areas'),
    'polsby_popper':
    polsby_popper,
示例#10
0
def read_basic_config(configFileName):
    """Reads basic configuration file and sets up a chain run

    :configFileName: relative path to config file
    :returns: Partition instance and MarkovChain instance

    """
    # set up the config file parser
    config = configparser.ConfigParser()
    config.read(configFileName)

    # SET UP GRAPH AND PARTITION SECTION
    # create graph and get global names for required graph attributes
    graph, POP, AREA, CD = gsource_gdata(config, 'GRAPH_SOURCE', 'GRAPH_DATA')

    voteDataList = vsource_vdata(graph, config, 'VOTE_DATA_SOURCE',
                                 'VOTE_DATA')
    # create a list of vote columns to update
    DataUpdaters = {**votes_updaters(voteDataList)}

    # Previously used individual columns
    # {v: updates.Tally(v) for v in voteDataList}

    # original plan for fixing %'s:
    # for v in voteDataList:
    #    DataUpdaters = {**DataUpdaters, v+"%": updates.Tally(v+"%")}

    # construct initial districting plan
    assignment = {x[0]: x[1][CD] for x in graph.nodes(data=True)}
    # set up validator functions and create Validator class instance
    validatorsUpdaters = []
    validators = []
    if config.has_section('VALIDITY') and len(list(
            config['VALIDITY'].keys())) > 0:
        validators = list(config['VALIDITY'].values())
        for i, x in enumerate(validators):
            if len(x.split(',')) == 1:
                validators[i] = getattr(valids, x)
            else:
                [y, z] = x.split(',')
                validators[i] = valids.WithinPercentRangeOfBounds(
                    getattr(valids, y), z)
        validatorsUpdaters.extend(
            [x.split(',')[0] for x in config['VALIDITY'].values()])

    validators = valids.Validator(validators)
    # add updaters required by this list of validators to list of updaters
    for x in validatorsUpdaters:
        DataUpdaters.update(dependencies(x, POP, AREA))
    # END SET UP GRAPH AND PARTITION SECTION

    # SET UP MARKOVCHAIN RUN SECTION
    # set up parameters for markovchain run
    chainparams = config['MARKOV_CHAIN']
    # number of steps to run
    num_steps = 1000
    if 'num_steps' in list(chainparams.keys()):
        num_steps = int(chainparams['num_steps'])
    # type of flip to use
    proposal = proposals.propose_random_flip
    if 'proposal' in list(chainparams.keys()):
        proposal = getattr(proposals, chainparams['proposal'])
    # acceptance function to use
    accept = accepts.always_accept
    if 'accept' in list(chainparams.keys()):
        accept = getattr(accepts, chainparams['accept'])
    # END SET UP MARKOVCHAIN RUN SECTION

    # SET UP DATA PROCESSOR FOR CHAIN RUN
    # get evaluation scores to compute and the columns to use for each
    escores, cfunc, elist, sVisType, outFName = escores_edata(
        config, "EVALUATION_SCORES", "EVALUATION_SCORES_DATA")

    # add evaluation scores updaters to list of updators
    for x in elist:
        DataUpdaters.update(dependencies(x, POP, AREA))

    # END SET UP DATA PROCESSOR FOR CHAIN RUN
    updaters = DataUpdaters

    # create markovchain instance
    initial_partition = Partition(graph, assignment, updaters)
    chain = MarkovChain(proposal, validators, accept, initial_partition,
                        num_steps)

    return chain, cfunc, escores, sVisType, outFName
示例#11
0
def main():

    #graph = construct_graph_from_file("/Users/caranix/Desktop/Alaska_Chain/AK_data.shp", geoid_col="DISTRICT")

    with open('./alaska_graph.json') as f:
        data = json.load(f)
    graph = networkx.readwrite.json_graph.adjacency_graph(data)

    df = gp.read_file(
        "/Users/caranix/Desktop/Alaska_Chain/AK_data.shp"
    )  #    assignment = dict(zip(graph.nodes(), [graph.node[x]['HOUSEDIST'] for x in graph.nodes()]))
    add_data_to_graph(df,
                      graph, [
                          'join_Distr', 'POPULATION', 'join_Dem', 'join_Rep',
                          'perc_Dem', 'perc_Rep', 'AREA'
                      ],
                      id_col='DISTRICT')
    data = json.dumps(networkx.readwrite.json_graph.adjacency_data(graph))
    with open('./alaska_graph.json', 'w') as f:
        f.write(data)

    assignment = dict(
        zip(graph.nodes(),
            [graph.node[x]['join_Distr'] for x in graph.nodes()]))

    updaters = {
        'population':
        Tally('POPULATION', alias='population'),
        'cut_edges':
        cut_edges,
        'cut_edges_by_part':
        cut_edges_by_part,
        **votes_updaters(['join_Dem', 'join_Rep'], election_name='12'), 'perimeters':
        perimeters,
        'exterior_boundaries':
        exterior_boundaries,
        'boundary_nodes':
        boundary_nodes,
        'cut_edges':
        cut_edges,
        'areas':
        Tally('AREA', alias='areas'),
        'polsby_popper':
        polsby_popper
    }

    p = Partition(graph, assignment, updaters)
    print("Starting Chain")

    chain = BasicChain(p, 1000000)
    allAssignments = {0: chain.state.assignment}
    for step in chain:
        allAssignments[chain.counter + 1] = step.flips
    # print(mean_median(step, 'join_Dem%'))

# with open("chain_outputnew.json", "w") as f:
#     f.write(json.dumps(allAssignments))

#efficiency_gap(p)

# mean_median(p, 'join_Dem%')

    scores = {
        'Mean-Median':
        functools.partial(mean_median, proportion_column_name='join_Dem%'),
        'Mean-Thirdian':
        functools.partial(mean_thirdian, proportion_column_name='join_Dem%'),
        'Efficiency Gap':
        functools.partial(efficiency_gap, col1='join_Dem', col2='join_Rep'),
        'L1 Reciprocal Polsby-Popper':
        L1_reciprocal_polsby_popper
    }

    initial_scores = {key: score(p) for key, score in scores.items()}

    table = pipe_to_table(chain, scores)

    fig, axes = plt.subplots(2, 2)

    quadrants = {
        'Mean-Median': (0, 0),
        'Mean-Thirdian': (0, 1),
        'Efficiency Gap': (1, 0),
        'L1 Reciprocal Polsby-Popper': (1, 1)
    }

    for key in scores:
        quadrant = quadrants[key]
        axes[quadrant].hist(table[key], bins=50)
        axes[quadrant].set_title(key)
        axes[quadrant].axvline(x=initial_scores[key], color='r')
    plt.show()
    '''