def test_single_flip_contiguity_equals_contiguity(): import random random.seed(1887) def equality_validator(partition): val = partition["contiguous"] == partition["flip_check"] assert val return partition["contiguous"] df = gp.read_file("rundmcmc/testData/mo_cleaned_vtds.shp") with open("rundmcmc/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") validator = Validator([equality_validator]) updaters = {"contiguous": contiguous, "cut_edges": cut_edges, "flip_check": single_flip_contiguous} initial_partition = Partition(graph, assignment, updaters) accept = lambda x: True chain = MarkovChain(propose_random_flip, validator, accept, initial_partition, total_steps=100) list(chain)
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)
def main(): # Sketch: # 1. Load dataframe. # 2. Construct neighbor information. # 3. Make a graph from this. # 4. Throw attributes into graph. df = gp.read_file("./testData/mo_cleaned_vtds.shp") graph = networkx.readwrite.read_gpickle('example_graph.gpickle') add_data_to_graph(df, graph, ["PR_DV08", "PR_RV08", "P_08"], "GEOID10") assignment = get_assignment_dict(df, "GEOID10", "CD") updaters = { 'd_votes': statistic_factory('PR_DV08', alias='d_votes'), 'r_votes': statistic_factory('PR_RV08', alias='r_votes'), 'cut_edges': cut_edges } initial_partition = Partition(graph, assignment, updaters) validator = Validator([contiguous]) accept = lambda x: True chain = MarkovChain(propose_random_flip, validator, accept, initial_partition, total_steps=100) mm = [] mt = [] #eg=[] for state in chain: mm.append( mean_median2(state, data_column1='d_votes', data_column2='r_votes')) mt.append( mean_thirdian2(state, data_column1='d_votes', data_column2='r_votes')) #eg.append(efficiency_gap(state, data_column1='d_votes',data_column2='r_votes)) #print(graph.nodes(data=True)) mm_outs = [mm] #,eg] mt_outs = [mt] #eg_outs=[eg] with open('mm_chain_out', "w") as output: writer = csv.writer(output, lineterminator='\n') writer.writerows(mm_outs) with open('mt_chain_out', "w") as output: writer = csv.writer(output, lineterminator='\n') writer.writerows(mt_outs)
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)